Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
643f12db | 2 | * thinkpad_acpi.c - ThinkPad ACPI Extras |
1da177e4 LT |
3 | * |
4 | * | |
78f81cc4 | 5 | * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net> |
1c762ca4 | 6 | * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br> |
1da177e4 LT |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
a62bc916 HMH |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | * 02110-1301, USA. | |
78f81cc4 BD |
22 | */ |
23 | ||
aa2fbcec | 24 | #define TPACPI_VERSION "0.22" |
2586d566 | 25 | #define TPACPI_SYSFS_VERSION 0x020300 |
78f81cc4 BD |
26 | |
27 | /* | |
1da177e4 | 28 | * Changelog: |
4b45cc07 HMH |
29 | * 2007-10-20 changelog trimmed down |
30 | * | |
643f12db HMH |
31 | * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to |
32 | * drivers/misc. | |
f9ff43a6 HMH |
33 | * |
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. | |
837ca6dd | 37 | * |
78f81cc4 BD |
38 | * 2005-03-17 0.11 support for 600e, 770x |
39 | * thanks to Jamie Lentin <lentinj@dial.pipex.com> | |
4b45cc07 HMH |
40 | * |
41 | * 2005-01-16 0.9 use MODULE_VERSION | |
78f81cc4 BD |
42 | * 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> | |
1da177e4 LT |
48 | */ |
49 | ||
0c78039f HMH |
50 | #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> | |
73a94d86 | 57 | #include <linux/sched.h> |
0c78039f HMH |
58 | #include <linux/kthread.h> |
59 | #include <linux/freezer.h> | |
60 | #include <linux/delay.h> | |
61 | ||
62 | #include <linux/nvram.h> | |
63 | #include <linux/proc_fs.h> | |
64 | #include <linux/sysfs.h> | |
65 | #include <linux/backlight.h> | |
66 | #include <linux/fb.h> | |
67 | #include <linux/platform_device.h> | |
68 | #include <linux/hwmon.h> | |
69 | #include <linux/hwmon-sysfs.h> | |
70 | #include <linux/input.h> | |
4fa6811b | 71 | #include <linux/leds.h> |
0e74dc26 | 72 | #include <linux/rfkill.h> |
0c78039f HMH |
73 | #include <asm/uaccess.h> |
74 | ||
75 | #include <linux/dmi.h> | |
76 | #include <linux/jiffies.h> | |
77 | #include <linux/workqueue.h> | |
78 | ||
79 | #include <acpi/acpi_drivers.h> | |
0c78039f HMH |
80 | |
81 | #include <linux/pci_ids.h> | |
82 | ||
0c78039f HMH |
83 | |
84 | /* ThinkPad CMOS commands */ | |
85 | #define TP_CMOS_VOLUME_DOWN 0 | |
86 | #define TP_CMOS_VOLUME_UP 1 | |
87 | #define TP_CMOS_VOLUME_MUTE 2 | |
88 | #define TP_CMOS_BRIGHTNESS_UP 4 | |
89 | #define TP_CMOS_BRIGHTNESS_DOWN 5 | |
4fa6811b HMH |
90 | #define TP_CMOS_THINKLIGHT_ON 12 |
91 | #define TP_CMOS_THINKLIGHT_OFF 13 | |
0c78039f HMH |
92 | |
93 | /* NVRAM Addresses */ | |
94 | enum tp_nvram_addr { | |
95 | TP_NVRAM_ADDR_HK2 = 0x57, | |
96 | TP_NVRAM_ADDR_THINKLIGHT = 0x58, | |
97 | TP_NVRAM_ADDR_VIDEO = 0x59, | |
98 | TP_NVRAM_ADDR_BRIGHTNESS = 0x5e, | |
99 | TP_NVRAM_ADDR_MIXER = 0x60, | |
100 | }; | |
101 | ||
102 | /* NVRAM bit masks */ | |
103 | enum { | |
104 | TP_NVRAM_MASK_HKT_THINKPAD = 0x08, | |
105 | TP_NVRAM_MASK_HKT_ZOOM = 0x20, | |
106 | TP_NVRAM_MASK_HKT_DISPLAY = 0x40, | |
107 | TP_NVRAM_MASK_HKT_HIBERNATE = 0x80, | |
108 | TP_NVRAM_MASK_THINKLIGHT = 0x10, | |
109 | TP_NVRAM_MASK_HKT_DISPEXPND = 0x30, | |
110 | TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20, | |
111 | TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f, | |
112 | TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0, | |
113 | TP_NVRAM_MASK_MUTE = 0x40, | |
114 | TP_NVRAM_MASK_HKT_VOLUME = 0x80, | |
115 | TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f, | |
116 | TP_NVRAM_POS_LEVEL_VOLUME = 0, | |
117 | }; | |
118 | ||
b21a15f6 | 119 | /* ACPI HIDs */ |
e0c7dfe7 | 120 | #define TPACPI_ACPI_HKEY_HID "IBM0068" |
b21a15f6 HMH |
121 | |
122 | /* Input IDs */ | |
123 | #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */ | |
124 | #define TPACPI_HKEY_INPUT_VERSION 0x4101 | |
125 | ||
153f8220 HMH |
126 | /* ACPI \WGSV commands */ |
127 | enum { | |
128 | TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */ | |
129 | TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */ | |
130 | TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */ | |
131 | TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */ | |
132 | }; | |
133 | ||
134 | /* TP_ACPI_WGSV_GET_STATE bits */ | |
135 | enum { | |
136 | TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */ | |
137 | TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */ | |
138 | TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */ | |
139 | TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */ | |
140 | TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */ | |
141 | TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */ | |
142 | TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */ | |
143 | TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */ | |
144 | TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */ | |
145 | TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */ | |
146 | }; | |
b21a15f6 HMH |
147 | |
148 | /**************************************************************************** | |
149 | * Main driver | |
150 | */ | |
151 | ||
e0c7dfe7 HMH |
152 | #define TPACPI_NAME "thinkpad" |
153 | #define TPACPI_DESC "ThinkPad ACPI Extras" | |
154 | #define TPACPI_FILE TPACPI_NAME "_acpi" | |
155 | #define TPACPI_URL "http://ibm-acpi.sf.net/" | |
156 | #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net" | |
b21a15f6 | 157 | |
e0c7dfe7 HMH |
158 | #define TPACPI_PROC_DIR "ibm" |
159 | #define TPACPI_ACPI_EVENT_PREFIX "ibm" | |
160 | #define TPACPI_DRVR_NAME TPACPI_FILE | |
95e57ab2 | 161 | #define TPACPI_DRVR_SHORTNAME "tpacpi" |
e0c7dfe7 | 162 | #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon" |
b21a15f6 | 163 | |
95e57ab2 | 164 | #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd" |
e0e3c061 | 165 | #define TPACPI_WORKQUEUE_NAME "ktpacpid" |
95e57ab2 | 166 | |
e0c7dfe7 | 167 | #define TPACPI_MAX_ACPI_ARGS 3 |
b21a15f6 | 168 | |
0e74dc26 HMH |
169 | /* rfkill switches */ |
170 | enum { | |
171 | TPACPI_RFK_BLUETOOTH_SW_ID = 0, | |
172 | TPACPI_RFK_WWAN_SW_ID, | |
0045c0aa | 173 | TPACPI_RFK_UWB_SW_ID, |
0e74dc26 HMH |
174 | }; |
175 | ||
7ff8d62f | 176 | /* printk headers */ |
e0c7dfe7 | 177 | #define TPACPI_LOG TPACPI_FILE ": " |
7ff8d62f HMH |
178 | #define TPACPI_EMERG KERN_EMERG TPACPI_LOG |
179 | #define TPACPI_ALERT KERN_ALERT TPACPI_LOG | |
180 | #define TPACPI_CRIT KERN_CRIT TPACPI_LOG | |
181 | #define TPACPI_ERR KERN_ERR TPACPI_LOG | |
182 | #define TPACPI_WARN KERN_WARNING TPACPI_LOG | |
183 | #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG | |
184 | #define TPACPI_INFO KERN_INFO TPACPI_LOG | |
185 | #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG | |
186 | ||
187 | /* Debugging printk groups */ | |
0c78039f | 188 | #define TPACPI_DBG_ALL 0xffff |
73a94d86 | 189 | #define TPACPI_DBG_DISCLOSETASK 0x8000 |
0c78039f HMH |
190 | #define TPACPI_DBG_INIT 0x0001 |
191 | #define TPACPI_DBG_EXIT 0x0002 | |
0c78039f | 192 | |
35ff8b9f HMH |
193 | #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off") |
194 | #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled") | |
195 | #define strlencmp(a, b) (strncmp((a), (b), strlen(b))) | |
0c78039f | 196 | |
0c78039f HMH |
197 | |
198 | /**************************************************************************** | |
b21a15f6 | 199 | * Driver-wide structs and misc. variables |
0c78039f HMH |
200 | */ |
201 | ||
202 | struct ibm_struct; | |
203 | ||
204 | struct tp_acpi_drv_struct { | |
205 | const struct acpi_device_id *hid; | |
206 | struct acpi_driver *driver; | |
207 | ||
208 | void (*notify) (struct ibm_struct *, u32); | |
209 | acpi_handle *handle; | |
210 | u32 type; | |
211 | struct acpi_device *device; | |
212 | }; | |
213 | ||
214 | struct ibm_struct { | |
215 | char *name; | |
216 | ||
217 | int (*read) (char *); | |
218 | int (*write) (char *); | |
219 | void (*exit) (void); | |
220 | void (*resume) (void); | |
083f1760 | 221 | void (*suspend) (pm_message_t state); |
90d9d3c7 | 222 | void (*shutdown) (void); |
0c78039f HMH |
223 | |
224 | struct list_head all_drivers; | |
225 | ||
226 | struct tp_acpi_drv_struct *acpi; | |
227 | ||
228 | struct { | |
229 | u8 acpi_driver_registered:1; | |
230 | u8 acpi_notify_installed:1; | |
231 | u8 proc_created:1; | |
232 | u8 init_called:1; | |
233 | u8 experimental:1; | |
234 | } flags; | |
235 | }; | |
236 | ||
237 | struct ibm_init_struct { | |
238 | char param[32]; | |
239 | ||
240 | int (*init) (struct ibm_init_struct *); | |
241 | struct ibm_struct *data; | |
242 | }; | |
243 | ||
244 | static struct { | |
245 | #ifdef CONFIG_THINKPAD_ACPI_BAY | |
246 | u32 bay_status:1; | |
247 | u32 bay_eject:1; | |
248 | u32 bay_status2:1; | |
249 | u32 bay_eject2:1; | |
250 | #endif | |
251 | u32 bluetooth:1; | |
252 | u32 hotkey:1; | |
253 | u32 hotkey_mask:1; | |
254 | u32 hotkey_wlsw:1; | |
6c231bd5 | 255 | u32 hotkey_tablet:1; |
0c78039f HMH |
256 | u32 light:1; |
257 | u32 light_status:1; | |
258 | u32 bright_16levels:1; | |
b5972796 | 259 | u32 bright_acpimode:1; |
0c78039f | 260 | u32 wan:1; |
0045c0aa | 261 | u32 uwb:1; |
0c78039f HMH |
262 | u32 fan_ctrl_status_undef:1; |
263 | u32 input_device_registered:1; | |
264 | u32 platform_drv_registered:1; | |
265 | u32 platform_drv_attrs_registered:1; | |
266 | u32 sensors_pdrv_registered:1; | |
267 | u32 sensors_pdrv_attrs_registered:1; | |
268 | u32 sensors_pdev_attrs_registered:1; | |
269 | u32 hotkey_poll_active:1; | |
270 | } tp_features; | |
271 | ||
92889022 HMH |
272 | static struct { |
273 | u16 hotkey_mask_ff:1; | |
2d5e94d7 | 274 | u16 bright_cmos_ec_unsync:1; |
92889022 HMH |
275 | } tp_warned; |
276 | ||
0c78039f HMH |
277 | struct thinkpad_id_data { |
278 | unsigned int vendor; /* ThinkPad vendor: | |
279 | * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */ | |
280 | ||
281 | char *bios_version_str; /* Something like 1ZET51WW (1.03z) */ | |
282 | char *ec_version_str; /* Something like 1ZHT51WW-1.04a */ | |
283 | ||
284 | u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */ | |
285 | u16 ec_model; | |
286 | ||
8c74adbc HMH |
287 | char *model_str; /* ThinkPad T43 */ |
288 | char *nummodel_str; /* 9384A9C for a 9384-A9C model */ | |
0c78039f | 289 | }; |
0c78039f HMH |
290 | static struct thinkpad_id_data thinkpad_id; |
291 | ||
8fef502e HMH |
292 | static enum { |
293 | TPACPI_LIFE_INIT = 0, | |
294 | TPACPI_LIFE_RUNNING, | |
295 | TPACPI_LIFE_EXITING, | |
296 | } tpacpi_lifecycle; | |
297 | ||
b21a15f6 HMH |
298 | static int experimental; |
299 | static u32 dbg_level; | |
300 | ||
e0e3c061 HMH |
301 | static struct workqueue_struct *tpacpi_wq; |
302 | ||
4fa6811b HMH |
303 | /* Special LED class that can defer work */ |
304 | struct tpacpi_led_classdev { | |
305 | struct led_classdev led_classdev; | |
306 | struct work_struct work; | |
307 | enum led_brightness new_brightness; | |
af116101 | 308 | unsigned int led; |
4fa6811b HMH |
309 | }; |
310 | ||
a73f3091 HMH |
311 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
312 | static int dbg_wlswemul; | |
313 | static int tpacpi_wlsw_emulstate; | |
314 | static int dbg_bluetoothemul; | |
315 | static int tpacpi_bluetooth_emulstate; | |
316 | static int dbg_wwanemul; | |
317 | static int tpacpi_wwan_emulstate; | |
0045c0aa HMH |
318 | static int dbg_uwbemul; |
319 | static int tpacpi_uwb_emulstate; | |
a73f3091 HMH |
320 | #endif |
321 | ||
322 | ||
3dcc2c3b HMH |
323 | /************************************************************************* |
324 | * Debugging helpers | |
325 | */ | |
326 | ||
327 | #define dbg_printk(a_dbg_level, format, arg...) \ | |
328 | do { if (dbg_level & (a_dbg_level)) \ | |
329 | printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \ | |
330 | } while (0) | |
331 | ||
332 | #ifdef CONFIG_THINKPAD_ACPI_DEBUG | |
333 | #define vdbg_printk dbg_printk | |
334 | static const char *str_supported(int is_supported); | |
335 | #else | |
336 | #define vdbg_printk(a_dbg_level, format, arg...) \ | |
337 | do { } while (0) | |
338 | #endif | |
339 | ||
73a94d86 HMH |
340 | static void tpacpi_log_usertask(const char * const what) |
341 | { | |
342 | printk(TPACPI_DEBUG "%s: access by process with PID %d\n", | |
343 | what, task_tgid_vnr(current)); | |
344 | } | |
345 | ||
346 | #define tpacpi_disclose_usertask(what, format, arg...) \ | |
347 | do { \ | |
348 | if (unlikely( \ | |
349 | (dbg_level & TPACPI_DBG_DISCLOSETASK) && \ | |
350 | (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \ | |
351 | printk(TPACPI_DEBUG "%s: PID %d: " format, \ | |
352 | what, task_tgid_vnr(current), ## arg); \ | |
353 | } \ | |
354 | } while (0) | |
3dcc2c3b | 355 | |
56b6aeb0 HMH |
356 | /**************************************************************************** |
357 | **************************************************************************** | |
358 | * | |
359 | * ACPI Helpers and device model | |
360 | * | |
361 | **************************************************************************** | |
362 | ****************************************************************************/ | |
363 | ||
364 | /************************************************************************* | |
365 | * ACPI basic handles | |
366 | */ | |
1da177e4 | 367 | |
94954cc6 | 368 | static acpi_handle root_handle; |
1da177e4 | 369 | |
e0c7dfe7 | 370 | #define TPACPI_HANDLE(object, parent, paths...) \ |
1da177e4 LT |
371 | static acpi_handle object##_handle; \ |
372 | static acpi_handle *object##_parent = &parent##_handle; \ | |
78f81cc4 | 373 | static char *object##_path; \ |
1da177e4 LT |
374 | static char *object##_paths[] = { paths } |
375 | ||
e0c7dfe7 | 376 | TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */ |
78f81cc4 BD |
377 | "\\_SB.PCI.ISA.EC", /* 570 */ |
378 | "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */ | |
379 | "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */ | |
380 | "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */ | |
381 | "\\_SB.PCI0.ICH3.EC0", /* R31 */ | |
382 | "\\_SB.PCI0.LPC.EC", /* all others */ | |
56b6aeb0 | 383 | ); |
78f81cc4 | 384 | |
e0c7dfe7 HMH |
385 | TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */ |
386 | TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */ | |
78f81cc4 | 387 | |
35ff8b9f HMH |
388 | TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */ |
389 | /* T4x, X31, X40 */ | |
78f81cc4 BD |
390 | "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */ |
391 | "\\CMS", /* R40, R40e */ | |
56b6aeb0 | 392 | ); /* all others */ |
78f81cc4 | 393 | |
e0c7dfe7 | 394 | TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */ |
78f81cc4 BD |
395 | "^HKEY", /* R30, R31 */ |
396 | "HKEY", /* all others */ | |
56b6aeb0 | 397 | ); /* 570 */ |
78f81cc4 | 398 | |
d7c1d17d HMH |
399 | TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */ |
400 | "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */ | |
401 | "\\_SB.PCI0.VID0", /* 770e */ | |
402 | "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */ | |
403 | "\\_SB.PCI0.AGP.VID", /* all others */ | |
404 | ); /* R30, R31 */ | |
405 | ||
78f81cc4 | 406 | |
56b6aeb0 HMH |
407 | /************************************************************************* |
408 | * ACPI helpers | |
a8b7a662 HMH |
409 | */ |
410 | ||
1da177e4 LT |
411 | static int acpi_evalf(acpi_handle handle, |
412 | void *res, char *method, char *fmt, ...) | |
413 | { | |
414 | char *fmt0 = fmt; | |
78f81cc4 | 415 | struct acpi_object_list params; |
e0c7dfe7 | 416 | union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS]; |
78f81cc4 BD |
417 | struct acpi_buffer result, *resultp; |
418 | union acpi_object out_obj; | |
419 | acpi_status status; | |
420 | va_list ap; | |
421 | char res_type; | |
422 | int success; | |
423 | int quiet; | |
1da177e4 LT |
424 | |
425 | if (!*fmt) { | |
e0c7dfe7 | 426 | printk(TPACPI_ERR "acpi_evalf() called with empty format\n"); |
1da177e4 LT |
427 | return 0; |
428 | } | |
429 | ||
430 | if (*fmt == 'q') { | |
431 | quiet = 1; | |
432 | fmt++; | |
433 | } else | |
434 | quiet = 0; | |
435 | ||
436 | res_type = *(fmt++); | |
437 | ||
438 | params.count = 0; | |
439 | params.pointer = &in_objs[0]; | |
440 | ||
441 | va_start(ap, fmt); | |
442 | while (*fmt) { | |
443 | char c = *(fmt++); | |
444 | switch (c) { | |
445 | case 'd': /* int */ | |
446 | in_objs[params.count].integer.value = va_arg(ap, int); | |
447 | in_objs[params.count++].type = ACPI_TYPE_INTEGER; | |
448 | break; | |
78f81cc4 | 449 | /* add more types as needed */ |
1da177e4 | 450 | default: |
e0c7dfe7 | 451 | printk(TPACPI_ERR "acpi_evalf() called " |
1da177e4 LT |
452 | "with invalid format character '%c'\n", c); |
453 | return 0; | |
454 | } | |
455 | } | |
456 | va_end(ap); | |
457 | ||
78f81cc4 BD |
458 | if (res_type != 'v') { |
459 | result.length = sizeof(out_obj); | |
460 | result.pointer = &out_obj; | |
461 | resultp = &result; | |
462 | } else | |
463 | resultp = NULL; | |
1da177e4 | 464 | |
78f81cc4 | 465 | status = acpi_evaluate_object(handle, method, ¶ms, resultp); |
1da177e4 LT |
466 | |
467 | switch (res_type) { | |
78f81cc4 | 468 | case 'd': /* int */ |
1da177e4 LT |
469 | if (res) |
470 | *(int *)res = out_obj.integer.value; | |
471 | success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER; | |
472 | break; | |
78f81cc4 | 473 | case 'v': /* void */ |
1da177e4 LT |
474 | success = status == AE_OK; |
475 | break; | |
78f81cc4 | 476 | /* add more types as needed */ |
1da177e4 | 477 | default: |
e0c7dfe7 | 478 | printk(TPACPI_ERR "acpi_evalf() called " |
1da177e4 LT |
479 | "with invalid format character '%c'\n", res_type); |
480 | return 0; | |
481 | } | |
482 | ||
56b6aeb0 | 483 | if (!success && !quiet) |
e0c7dfe7 | 484 | printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n", |
56b6aeb0 HMH |
485 | method, fmt0, status); |
486 | ||
487 | return success; | |
488 | } | |
489 | ||
35ff8b9f | 490 | static int acpi_ec_read(int i, u8 *p) |
56b6aeb0 HMH |
491 | { |
492 | int v; | |
493 | ||
494 | if (ecrd_handle) { | |
495 | if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i)) | |
496 | return 0; | |
497 | *p = v; | |
498 | } else { | |
499 | if (ec_read(i, p) < 0) | |
500 | return 0; | |
501 | } | |
502 | ||
503 | return 1; | |
504 | } | |
505 | ||
506 | static int acpi_ec_write(int i, u8 v) | |
507 | { | |
508 | if (ecwr_handle) { | |
509 | if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v)) | |
510 | return 0; | |
511 | } else { | |
512 | if (ec_write(i, v) < 0) | |
513 | return 0; | |
514 | } | |
515 | ||
516 | return 1; | |
517 | } | |
518 | ||
013c40e4 | 519 | #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY) |
56b6aeb0 HMH |
520 | static int _sta(acpi_handle handle) |
521 | { | |
522 | int status; | |
523 | ||
524 | if (!handle || !acpi_evalf(handle, &status, "_STA", "d")) | |
525 | status = 0; | |
526 | ||
527 | return status; | |
528 | } | |
013c40e4 | 529 | #endif |
56b6aeb0 | 530 | |
c9bea99c HMH |
531 | static int issue_thinkpad_cmos_command(int cmos_cmd) |
532 | { | |
533 | if (!cmos_handle) | |
534 | return -ENXIO; | |
535 | ||
536 | if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd)) | |
537 | return -EIO; | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
56b6aeb0 HMH |
542 | /************************************************************************* |
543 | * ACPI device model | |
544 | */ | |
545 | ||
35ff8b9f HMH |
546 | #define TPACPI_ACPIHANDLE_INIT(object) \ |
547 | drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \ | |
f74a27d4 HMH |
548 | object##_paths, ARRAY_SIZE(object##_paths), &object##_path) |
549 | ||
8d376cd6 | 550 | static void drv_acpi_handle_init(char *name, |
5fba344c HMH |
551 | acpi_handle *handle, acpi_handle parent, |
552 | char **paths, int num_paths, char **path) | |
56b6aeb0 HMH |
553 | { |
554 | int i; | |
555 | acpi_status status; | |
556 | ||
5ae930e6 HMH |
557 | vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n", |
558 | name); | |
559 | ||
56b6aeb0 HMH |
560 | for (i = 0; i < num_paths; i++) { |
561 | status = acpi_get_handle(parent, paths[i], handle); | |
562 | if (ACPI_SUCCESS(status)) { | |
563 | *path = paths[i]; | |
5ae930e6 HMH |
564 | dbg_printk(TPACPI_DBG_INIT, |
565 | "Found ACPI handle %s for %s\n", | |
566 | *path, name); | |
56b6aeb0 HMH |
567 | return; |
568 | } | |
569 | } | |
570 | ||
5ae930e6 HMH |
571 | vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n", |
572 | name); | |
56b6aeb0 HMH |
573 | *handle = NULL; |
574 | } | |
575 | ||
8d376cd6 | 576 | static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data) |
56b6aeb0 HMH |
577 | { |
578 | struct ibm_struct *ibm = data; | |
579 | ||
8fef502e HMH |
580 | if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) |
581 | return; | |
582 | ||
8d376cd6 | 583 | if (!ibm || !ibm->acpi || !ibm->acpi->notify) |
56b6aeb0 HMH |
584 | return; |
585 | ||
8d376cd6 | 586 | ibm->acpi->notify(ibm, event); |
56b6aeb0 HMH |
587 | } |
588 | ||
8d376cd6 | 589 | static int __init setup_acpi_notify(struct ibm_struct *ibm) |
56b6aeb0 HMH |
590 | { |
591 | acpi_status status; | |
5ae930e6 | 592 | int rc; |
56b6aeb0 | 593 | |
8d376cd6 HMH |
594 | BUG_ON(!ibm->acpi); |
595 | ||
596 | if (!*ibm->acpi->handle) | |
56b6aeb0 HMH |
597 | return 0; |
598 | ||
5ae930e6 | 599 | vdbg_printk(TPACPI_DBG_INIT, |
fe08bc4b HMH |
600 | "setting up ACPI notify for %s\n", ibm->name); |
601 | ||
5ae930e6 HMH |
602 | rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device); |
603 | if (rc < 0) { | |
e0c7dfe7 | 604 | printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n", |
5ae930e6 | 605 | ibm->name, rc); |
56b6aeb0 HMH |
606 | return -ENODEV; |
607 | } | |
608 | ||
db89b4f0 | 609 | ibm->acpi->device->driver_data = ibm; |
8d376cd6 | 610 | sprintf(acpi_device_class(ibm->acpi->device), "%s/%s", |
e0c7dfe7 | 611 | TPACPI_ACPI_EVENT_PREFIX, |
643f12db | 612 | ibm->name); |
56b6aeb0 | 613 | |
8d376cd6 HMH |
614 | status = acpi_install_notify_handler(*ibm->acpi->handle, |
615 | ibm->acpi->type, dispatch_acpi_notify, ibm); | |
56b6aeb0 HMH |
616 | if (ACPI_FAILURE(status)) { |
617 | if (status == AE_ALREADY_EXISTS) { | |
35ff8b9f HMH |
618 | printk(TPACPI_NOTICE |
619 | "another device driver is already " | |
620 | "handling %s events\n", ibm->name); | |
56b6aeb0 | 621 | } else { |
35ff8b9f HMH |
622 | printk(TPACPI_ERR |
623 | "acpi_install_notify_handler(%s) failed: %d\n", | |
624 | ibm->name, status); | |
56b6aeb0 HMH |
625 | } |
626 | return -ENODEV; | |
627 | } | |
8d376cd6 | 628 | ibm->flags.acpi_notify_installed = 1; |
56b6aeb0 HMH |
629 | return 0; |
630 | } | |
631 | ||
8d376cd6 | 632 | static int __init tpacpi_device_add(struct acpi_device *device) |
56b6aeb0 HMH |
633 | { |
634 | return 0; | |
635 | } | |
636 | ||
6700121b | 637 | static int __init register_tpacpi_subdriver(struct ibm_struct *ibm) |
56b6aeb0 | 638 | { |
5ae930e6 | 639 | int rc; |
56b6aeb0 | 640 | |
fe08bc4b HMH |
641 | dbg_printk(TPACPI_DBG_INIT, |
642 | "registering %s as an ACPI driver\n", ibm->name); | |
643 | ||
8d376cd6 HMH |
644 | BUG_ON(!ibm->acpi); |
645 | ||
646 | ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL); | |
647 | if (!ibm->acpi->driver) { | |
4f778b92 MK |
648 | printk(TPACPI_ERR |
649 | "failed to allocate memory for ibm->acpi->driver\n"); | |
5fba344c | 650 | return -ENOMEM; |
56b6aeb0 HMH |
651 | } |
652 | ||
e0c7dfe7 | 653 | sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name); |
8d376cd6 | 654 | ibm->acpi->driver->ids = ibm->acpi->hid; |
1ba90e3a | 655 | |
8d376cd6 | 656 | ibm->acpi->driver->ops.add = &tpacpi_device_add; |
56b6aeb0 | 657 | |
5ae930e6 HMH |
658 | rc = acpi_bus_register_driver(ibm->acpi->driver); |
659 | if (rc < 0) { | |
e0c7dfe7 | 660 | printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n", |
1ba90e3a | 661 | ibm->name, rc); |
8d376cd6 HMH |
662 | kfree(ibm->acpi->driver); |
663 | ibm->acpi->driver = NULL; | |
5ae930e6 | 664 | } else if (!rc) |
8d376cd6 | 665 | ibm->flags.acpi_driver_registered = 1; |
56b6aeb0 | 666 | |
5ae930e6 | 667 | return rc; |
56b6aeb0 HMH |
668 | } |
669 | ||
670 | ||
671 | /**************************************************************************** | |
672 | **************************************************************************** | |
673 | * | |
674 | * Procfs Helpers | |
675 | * | |
676 | **************************************************************************** | |
677 | ****************************************************************************/ | |
678 | ||
8d376cd6 HMH |
679 | static int dispatch_procfs_read(char *page, char **start, off_t off, |
680 | int count, int *eof, void *data) | |
56b6aeb0 HMH |
681 | { |
682 | struct ibm_struct *ibm = data; | |
683 | int len; | |
684 | ||
685 | if (!ibm || !ibm->read) | |
686 | return -EINVAL; | |
687 | ||
688 | len = ibm->read(page); | |
689 | if (len < 0) | |
690 | return len; | |
691 | ||
692 | if (len <= off + count) | |
693 | *eof = 1; | |
694 | *start = page + off; | |
695 | len -= off; | |
696 | if (len > count) | |
697 | len = count; | |
698 | if (len < 0) | |
699 | len = 0; | |
700 | ||
701 | return len; | |
702 | } | |
703 | ||
8d376cd6 | 704 | static int dispatch_procfs_write(struct file *file, |
35ff8b9f | 705 | const char __user *userbuf, |
8d376cd6 | 706 | unsigned long count, void *data) |
56b6aeb0 HMH |
707 | { |
708 | struct ibm_struct *ibm = data; | |
709 | char *kernbuf; | |
710 | int ret; | |
711 | ||
712 | if (!ibm || !ibm->write) | |
713 | return -EINVAL; | |
714 | ||
715 | kernbuf = kmalloc(count + 2, GFP_KERNEL); | |
716 | if (!kernbuf) | |
717 | return -ENOMEM; | |
1da177e4 | 718 | |
56b6aeb0 HMH |
719 | if (copy_from_user(kernbuf, userbuf, count)) { |
720 | kfree(kernbuf); | |
721 | return -EFAULT; | |
722 | } | |
1da177e4 | 723 | |
56b6aeb0 HMH |
724 | kernbuf[count] = 0; |
725 | strcat(kernbuf, ","); | |
726 | ret = ibm->write(kernbuf); | |
727 | if (ret == 0) | |
728 | ret = count; | |
1da177e4 | 729 | |
56b6aeb0 HMH |
730 | kfree(kernbuf); |
731 | ||
732 | return ret; | |
1da177e4 LT |
733 | } |
734 | ||
735 | static char *next_cmd(char **cmds) | |
736 | { | |
737 | char *start = *cmds; | |
738 | char *end; | |
739 | ||
740 | while ((end = strchr(start, ',')) && end == start) | |
741 | start = end + 1; | |
742 | ||
743 | if (!end) | |
744 | return NULL; | |
745 | ||
746 | *end = 0; | |
747 | *cmds = end + 1; | |
748 | return start; | |
749 | } | |
750 | ||
56b6aeb0 | 751 | |
54ae1501 HMH |
752 | /**************************************************************************** |
753 | **************************************************************************** | |
754 | * | |
7f5d1cd6 | 755 | * Device model: input, hwmon and platform |
54ae1501 HMH |
756 | * |
757 | **************************************************************************** | |
758 | ****************************************************************************/ | |
759 | ||
94954cc6 | 760 | static struct platform_device *tpacpi_pdev; |
7fd40029 | 761 | static struct platform_device *tpacpi_sensors_pdev; |
1beeffe4 | 762 | static struct device *tpacpi_hwmon; |
7f5d1cd6 | 763 | static struct input_dev *tpacpi_inputdev; |
8523ed6f | 764 | static struct mutex tpacpi_inputdev_send_mutex; |
b21a15f6 | 765 | static LIST_HEAD(tpacpi_all_drivers); |
e295e850 | 766 | |
083f1760 HMH |
767 | static int tpacpi_suspend_handler(struct platform_device *pdev, |
768 | pm_message_t state) | |
769 | { | |
770 | struct ibm_struct *ibm, *itmp; | |
771 | ||
772 | list_for_each_entry_safe(ibm, itmp, | |
773 | &tpacpi_all_drivers, | |
774 | all_drivers) { | |
775 | if (ibm->suspend) | |
776 | (ibm->suspend)(state); | |
777 | } | |
778 | ||
779 | return 0; | |
780 | } | |
781 | ||
e295e850 HMH |
782 | static int tpacpi_resume_handler(struct platform_device *pdev) |
783 | { | |
784 | struct ibm_struct *ibm, *itmp; | |
785 | ||
786 | list_for_each_entry_safe(ibm, itmp, | |
787 | &tpacpi_all_drivers, | |
788 | all_drivers) { | |
789 | if (ibm->resume) | |
790 | (ibm->resume)(); | |
791 | } | |
792 | ||
793 | return 0; | |
794 | } | |
795 | ||
90d9d3c7 HMH |
796 | static void tpacpi_shutdown_handler(struct platform_device *pdev) |
797 | { | |
798 | struct ibm_struct *ibm, *itmp; | |
799 | ||
800 | list_for_each_entry_safe(ibm, itmp, | |
801 | &tpacpi_all_drivers, | |
802 | all_drivers) { | |
803 | if (ibm->shutdown) | |
804 | (ibm->shutdown)(); | |
805 | } | |
806 | } | |
807 | ||
54ae1501 HMH |
808 | static struct platform_driver tpacpi_pdriver = { |
809 | .driver = { | |
e0c7dfe7 | 810 | .name = TPACPI_DRVR_NAME, |
54ae1501 HMH |
811 | .owner = THIS_MODULE, |
812 | }, | |
083f1760 | 813 | .suspend = tpacpi_suspend_handler, |
e295e850 | 814 | .resume = tpacpi_resume_handler, |
90d9d3c7 | 815 | .shutdown = tpacpi_shutdown_handler, |
54ae1501 HMH |
816 | }; |
817 | ||
7fd40029 HMH |
818 | static struct platform_driver tpacpi_hwmon_pdriver = { |
819 | .driver = { | |
e0c7dfe7 | 820 | .name = TPACPI_HWMON_DRVR_NAME, |
7fd40029 HMH |
821 | .owner = THIS_MODULE, |
822 | }, | |
823 | }; | |
54ae1501 | 824 | |
176750d6 | 825 | /************************************************************************* |
b21a15f6 | 826 | * sysfs support helpers |
176750d6 HMH |
827 | */ |
828 | ||
b21a15f6 HMH |
829 | struct attribute_set { |
830 | unsigned int members, max_members; | |
831 | struct attribute_group group; | |
176750d6 HMH |
832 | }; |
833 | ||
7252374a HMH |
834 | struct attribute_set_obj { |
835 | struct attribute_set s; | |
836 | struct attribute *a; | |
837 | } __attribute__((packed)); | |
838 | ||
839 | static struct attribute_set *create_attr_set(unsigned int max_members, | |
35ff8b9f | 840 | const char *name) |
7252374a HMH |
841 | { |
842 | struct attribute_set_obj *sobj; | |
843 | ||
844 | if (max_members == 0) | |
845 | return NULL; | |
846 | ||
847 | /* Allocates space for implicit NULL at the end too */ | |
848 | sobj = kzalloc(sizeof(struct attribute_set_obj) + | |
849 | max_members * sizeof(struct attribute *), | |
850 | GFP_KERNEL); | |
851 | if (!sobj) | |
852 | return NULL; | |
853 | sobj->s.max_members = max_members; | |
854 | sobj->s.group.attrs = &sobj->a; | |
855 | sobj->s.group.name = name; | |
856 | ||
857 | return &sobj->s; | |
858 | } | |
859 | ||
f74a27d4 HMH |
860 | #define destroy_attr_set(_set) \ |
861 | kfree(_set); | |
862 | ||
7252374a | 863 | /* not multi-threaded safe, use it in a single thread per set */ |
35ff8b9f | 864 | static int add_to_attr_set(struct attribute_set *s, struct attribute *attr) |
7252374a HMH |
865 | { |
866 | if (!s || !attr) | |
867 | return -EINVAL; | |
868 | ||
869 | if (s->members >= s->max_members) | |
870 | return -ENOMEM; | |
871 | ||
872 | s->group.attrs[s->members] = attr; | |
873 | s->members++; | |
874 | ||
875 | return 0; | |
876 | } | |
877 | ||
35ff8b9f | 878 | static int add_many_to_attr_set(struct attribute_set *s, |
7252374a HMH |
879 | struct attribute **attr, |
880 | unsigned int count) | |
881 | { | |
882 | int i, res; | |
883 | ||
884 | for (i = 0; i < count; i++) { | |
885 | res = add_to_attr_set(s, attr[i]); | |
886 | if (res) | |
887 | return res; | |
888 | } | |
889 | ||
890 | return 0; | |
891 | } | |
892 | ||
35ff8b9f | 893 | static void delete_attr_set(struct attribute_set *s, struct kobject *kobj) |
7252374a HMH |
894 | { |
895 | sysfs_remove_group(kobj, &s->group); | |
896 | destroy_attr_set(s); | |
897 | } | |
898 | ||
f74a27d4 HMH |
899 | #define register_attr_set_with_sysfs(_attr_set, _kobj) \ |
900 | sysfs_create_group(_kobj, &_attr_set->group) | |
901 | ||
7252374a HMH |
902 | static int parse_strtoul(const char *buf, |
903 | unsigned long max, unsigned long *value) | |
904 | { | |
905 | char *endp; | |
906 | ||
32afbf07 HMH |
907 | while (*buf && isspace(*buf)) |
908 | buf++; | |
7252374a HMH |
909 | *value = simple_strtoul(buf, &endp, 0); |
910 | while (*endp && isspace(*endp)) | |
911 | endp++; | |
912 | if (*endp || *value > max) | |
913 | return -EINVAL; | |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
d64c81c4 HMH |
918 | static void tpacpi_disable_brightness_delay(void) |
919 | { | |
920 | if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0)) | |
921 | printk(TPACPI_NOTICE | |
922 | "ACPI backlight control delay disabled\n"); | |
923 | } | |
924 | ||
b5972796 HMH |
925 | static int __init tpacpi_query_bcl_levels(acpi_handle handle) |
926 | { | |
927 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
928 | union acpi_object *obj; | |
929 | int rc; | |
930 | ||
931 | if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) { | |
932 | obj = (union acpi_object *)buffer.pointer; | |
933 | if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) { | |
934 | printk(TPACPI_ERR "Unknown _BCL data, " | |
935 | "please report this to %s\n", TPACPI_MAIL); | |
936 | rc = 0; | |
937 | } else { | |
938 | rc = obj->package.count; | |
939 | } | |
940 | } else { | |
941 | return 0; | |
942 | } | |
943 | ||
944 | kfree(buffer.pointer); | |
945 | return rc; | |
946 | } | |
947 | ||
948 | static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle, | |
949 | u32 lvl, void *context, void **rv) | |
950 | { | |
951 | char name[ACPI_PATH_SEGMENT_LENGTH]; | |
952 | struct acpi_buffer buffer = { sizeof(name), &name }; | |
953 | ||
954 | if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) && | |
955 | !strncmp("_BCL", name, sizeof(name) - 1)) { | |
956 | BUG_ON(!rv || !*rv); | |
957 | **(int **)rv = tpacpi_query_bcl_levels(handle); | |
958 | return AE_CTRL_TERMINATE; | |
959 | } else { | |
960 | return AE_OK; | |
961 | } | |
962 | } | |
963 | ||
964 | /* | |
965 | * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map | |
966 | */ | |
967 | static int __init tpacpi_check_std_acpi_brightness_support(void) | |
968 | { | |
969 | int status; | |
970 | int bcl_levels = 0; | |
971 | void *bcl_ptr = &bcl_levels; | |
972 | ||
973 | if (!vid_handle) { | |
974 | TPACPI_ACPIHANDLE_INIT(vid); | |
975 | } | |
976 | if (!vid_handle) | |
977 | return 0; | |
978 | ||
979 | /* | |
980 | * Search for a _BCL method, and execute it. This is safe on all | |
981 | * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista | |
982 | * BIOS in ACPI backlight control mode. We do NOT have to care | |
983 | * about calling the _BCL method in an enabled video device, any | |
984 | * will do for our purposes. | |
985 | */ | |
986 | ||
987 | status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3, | |
988 | tpacpi_acpi_walk_find_bcl, NULL, | |
989 | &bcl_ptr); | |
990 | ||
991 | if (ACPI_SUCCESS(status) && bcl_levels > 2) { | |
992 | tp_features.bright_acpimode = 1; | |
993 | return (bcl_levels - 2); | |
994 | } | |
995 | ||
996 | return 0; | |
997 | } | |
998 | ||
0e74dc26 HMH |
999 | static int __init tpacpi_new_rfkill(const unsigned int id, |
1000 | struct rfkill **rfk, | |
1001 | const enum rfkill_type rfktype, | |
1002 | const char *name, | |
0045c0aa | 1003 | const bool set_default, |
0e74dc26 HMH |
1004 | int (*toggle_radio)(void *, enum rfkill_state), |
1005 | int (*get_state)(void *, enum rfkill_state *)) | |
1006 | { | |
1007 | int res; | |
90d9d3c7 HMH |
1008 | enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED; |
1009 | ||
1010 | res = get_state(NULL, &initial_state); | |
1011 | if (res < 0) { | |
1012 | printk(TPACPI_ERR | |
1013 | "failed to read initial state for %s, error %d; " | |
1014 | "will turn radio off\n", name, res); | |
0045c0aa | 1015 | } else if (set_default) { |
90d9d3c7 HMH |
1016 | /* try to set the initial state as the default for the rfkill |
1017 | * type, since we ask the firmware to preserve it across S5 in | |
1018 | * NVRAM */ | |
1019 | rfkill_set_default(rfktype, | |
1020 | (initial_state == RFKILL_STATE_UNBLOCKED) ? | |
1021 | RFKILL_STATE_UNBLOCKED : | |
1022 | RFKILL_STATE_SOFT_BLOCKED); | |
1023 | } | |
0e74dc26 HMH |
1024 | |
1025 | *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype); | |
1026 | if (!*rfk) { | |
1027 | printk(TPACPI_ERR | |
1028 | "failed to allocate memory for rfkill class\n"); | |
1029 | return -ENOMEM; | |
1030 | } | |
1031 | ||
1032 | (*rfk)->name = name; | |
1033 | (*rfk)->get_state = get_state; | |
1034 | (*rfk)->toggle_radio = toggle_radio; | |
90d9d3c7 | 1035 | (*rfk)->state = initial_state; |
0e74dc26 HMH |
1036 | |
1037 | res = rfkill_register(*rfk); | |
1038 | if (res < 0) { | |
1039 | printk(TPACPI_ERR | |
1040 | "failed to register %s rfkill switch: %d\n", | |
1041 | name, res); | |
1042 | rfkill_free(*rfk); | |
1043 | *rfk = NULL; | |
1044 | return res; | |
1045 | } | |
1046 | ||
1047 | return 0; | |
1048 | } | |
1049 | ||
73a94d86 HMH |
1050 | static void printk_deprecated_attribute(const char * const what, |
1051 | const char * const details) | |
1052 | { | |
1053 | tpacpi_log_usertask("deprecated sysfs attribute"); | |
1054 | printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and " | |
1055 | "will be removed. %s\n", | |
1056 | what, details); | |
1057 | } | |
1058 | ||
1059 | static void printk_deprecated_rfkill_attribute(const char * const what) | |
1060 | { | |
1061 | printk_deprecated_attribute(what, | |
1062 | "Please switch to generic rfkill before year 2010"); | |
1063 | } | |
1064 | ||
56b6aeb0 | 1065 | /************************************************************************* |
b21a15f6 | 1066 | * thinkpad-acpi driver attributes |
56b6aeb0 HMH |
1067 | */ |
1068 | ||
b21a15f6 HMH |
1069 | /* interface_version --------------------------------------------------- */ |
1070 | static ssize_t tpacpi_driver_interface_version_show( | |
1071 | struct device_driver *drv, | |
1072 | char *buf) | |
1da177e4 | 1073 | { |
b21a15f6 HMH |
1074 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION); |
1075 | } | |
1076 | ||
1077 | static DRIVER_ATTR(interface_version, S_IRUGO, | |
1078 | tpacpi_driver_interface_version_show, NULL); | |
1079 | ||
1080 | /* debug_level --------------------------------------------------------- */ | |
1081 | static ssize_t tpacpi_driver_debug_show(struct device_driver *drv, | |
1082 | char *buf) | |
1083 | { | |
1084 | return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level); | |
1085 | } | |
1086 | ||
1087 | static ssize_t tpacpi_driver_debug_store(struct device_driver *drv, | |
1088 | const char *buf, size_t count) | |
1089 | { | |
1090 | unsigned long t; | |
1091 | ||
1092 | if (parse_strtoul(buf, 0xffff, &t)) | |
1093 | return -EINVAL; | |
1094 | ||
1095 | dbg_level = t; | |
1096 | ||
1097 | return count; | |
1098 | } | |
1099 | ||
1100 | static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO, | |
1101 | tpacpi_driver_debug_show, tpacpi_driver_debug_store); | |
1102 | ||
1103 | /* version ------------------------------------------------------------- */ | |
1104 | static ssize_t tpacpi_driver_version_show(struct device_driver *drv, | |
1105 | char *buf) | |
1106 | { | |
35ff8b9f HMH |
1107 | return snprintf(buf, PAGE_SIZE, "%s v%s\n", |
1108 | TPACPI_DESC, TPACPI_VERSION); | |
b21a15f6 HMH |
1109 | } |
1110 | ||
1111 | static DRIVER_ATTR(version, S_IRUGO, | |
1112 | tpacpi_driver_version_show, NULL); | |
1113 | ||
1114 | /* --------------------------------------------------------------------- */ | |
1115 | ||
a73f3091 HMH |
1116 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
1117 | ||
1118 | static void tpacpi_send_radiosw_update(void); | |
1119 | ||
1120 | /* wlsw_emulstate ------------------------------------------------------ */ | |
1121 | static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv, | |
1122 | char *buf) | |
1123 | { | |
1124 | return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate); | |
1125 | } | |
1126 | ||
1127 | static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv, | |
1128 | const char *buf, size_t count) | |
1129 | { | |
1130 | unsigned long t; | |
1131 | ||
1132 | if (parse_strtoul(buf, 1, &t)) | |
1133 | return -EINVAL; | |
1134 | ||
1135 | if (tpacpi_wlsw_emulstate != t) { | |
1136 | tpacpi_wlsw_emulstate = !!t; | |
1137 | tpacpi_send_radiosw_update(); | |
1138 | } else | |
1139 | tpacpi_wlsw_emulstate = !!t; | |
1140 | ||
1141 | return count; | |
1142 | } | |
1143 | ||
1144 | static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO, | |
1145 | tpacpi_driver_wlsw_emulstate_show, | |
1146 | tpacpi_driver_wlsw_emulstate_store); | |
1147 | ||
1148 | /* bluetooth_emulstate ------------------------------------------------- */ | |
1149 | static ssize_t tpacpi_driver_bluetooth_emulstate_show( | |
1150 | struct device_driver *drv, | |
1151 | char *buf) | |
1152 | { | |
1153 | return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate); | |
1154 | } | |
1155 | ||
1156 | static ssize_t tpacpi_driver_bluetooth_emulstate_store( | |
1157 | struct device_driver *drv, | |
1158 | const char *buf, size_t count) | |
1159 | { | |
1160 | unsigned long t; | |
1161 | ||
1162 | if (parse_strtoul(buf, 1, &t)) | |
1163 | return -EINVAL; | |
1164 | ||
1165 | tpacpi_bluetooth_emulstate = !!t; | |
1166 | ||
1167 | return count; | |
1168 | } | |
1169 | ||
1170 | static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO, | |
1171 | tpacpi_driver_bluetooth_emulstate_show, | |
1172 | tpacpi_driver_bluetooth_emulstate_store); | |
1173 | ||
1174 | /* wwan_emulstate ------------------------------------------------- */ | |
1175 | static ssize_t tpacpi_driver_wwan_emulstate_show( | |
1176 | struct device_driver *drv, | |
1177 | char *buf) | |
1178 | { | |
1179 | return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate); | |
1180 | } | |
1181 | ||
1182 | static ssize_t tpacpi_driver_wwan_emulstate_store( | |
1183 | struct device_driver *drv, | |
1184 | const char *buf, size_t count) | |
1185 | { | |
1186 | unsigned long t; | |
1187 | ||
1188 | if (parse_strtoul(buf, 1, &t)) | |
1189 | return -EINVAL; | |
1190 | ||
1191 | tpacpi_wwan_emulstate = !!t; | |
1192 | ||
1193 | return count; | |
1194 | } | |
1195 | ||
1196 | static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO, | |
1197 | tpacpi_driver_wwan_emulstate_show, | |
1198 | tpacpi_driver_wwan_emulstate_store); | |
1199 | ||
0045c0aa HMH |
1200 | /* uwb_emulstate ------------------------------------------------- */ |
1201 | static ssize_t tpacpi_driver_uwb_emulstate_show( | |
1202 | struct device_driver *drv, | |
1203 | char *buf) | |
1204 | { | |
1205 | return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate); | |
1206 | } | |
1207 | ||
1208 | static ssize_t tpacpi_driver_uwb_emulstate_store( | |
1209 | struct device_driver *drv, | |
1210 | const char *buf, size_t count) | |
1211 | { | |
1212 | unsigned long t; | |
1213 | ||
1214 | if (parse_strtoul(buf, 1, &t)) | |
1215 | return -EINVAL; | |
1216 | ||
1217 | tpacpi_uwb_emulstate = !!t; | |
1218 | ||
1219 | return count; | |
1220 | } | |
1221 | ||
1222 | static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO, | |
1223 | tpacpi_driver_uwb_emulstate_show, | |
1224 | tpacpi_driver_uwb_emulstate_store); | |
a73f3091 HMH |
1225 | #endif |
1226 | ||
1227 | /* --------------------------------------------------------------------- */ | |
1228 | ||
35ff8b9f | 1229 | static struct driver_attribute *tpacpi_driver_attributes[] = { |
b21a15f6 HMH |
1230 | &driver_attr_debug_level, &driver_attr_version, |
1231 | &driver_attr_interface_version, | |
1232 | }; | |
1233 | ||
1234 | static int __init tpacpi_create_driver_attributes(struct device_driver *drv) | |
1235 | { | |
1236 | int i, res; | |
1237 | ||
1238 | i = 0; | |
1239 | res = 0; | |
1240 | while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) { | |
1241 | res = driver_create_file(drv, tpacpi_driver_attributes[i]); | |
1242 | i++; | |
1243 | } | |
1244 | ||
a73f3091 HMH |
1245 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
1246 | if (!res && dbg_wlswemul) | |
1247 | res = driver_create_file(drv, &driver_attr_wlsw_emulstate); | |
1248 | if (!res && dbg_bluetoothemul) | |
1249 | res = driver_create_file(drv, &driver_attr_bluetooth_emulstate); | |
1250 | if (!res && dbg_wwanemul) | |
1251 | res = driver_create_file(drv, &driver_attr_wwan_emulstate); | |
0045c0aa HMH |
1252 | if (!res && dbg_uwbemul) |
1253 | res = driver_create_file(drv, &driver_attr_uwb_emulstate); | |
a73f3091 HMH |
1254 | #endif |
1255 | ||
b21a15f6 HMH |
1256 | return res; |
1257 | } | |
1258 | ||
1259 | static void tpacpi_remove_driver_attributes(struct device_driver *drv) | |
1260 | { | |
1261 | int i; | |
1262 | ||
35ff8b9f | 1263 | for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++) |
b21a15f6 | 1264 | driver_remove_file(drv, tpacpi_driver_attributes[i]); |
a73f3091 HMH |
1265 | |
1266 | #ifdef THINKPAD_ACPI_DEBUGFACILITIES | |
1267 | driver_remove_file(drv, &driver_attr_wlsw_emulstate); | |
1268 | driver_remove_file(drv, &driver_attr_bluetooth_emulstate); | |
1269 | driver_remove_file(drv, &driver_attr_wwan_emulstate); | |
0045c0aa | 1270 | driver_remove_file(drv, &driver_attr_uwb_emulstate); |
a73f3091 | 1271 | #endif |
b21a15f6 HMH |
1272 | } |
1273 | ||
1274 | /**************************************************************************** | |
1275 | **************************************************************************** | |
1276 | * | |
1277 | * Subdrivers | |
1278 | * | |
1279 | **************************************************************************** | |
1280 | ****************************************************************************/ | |
1281 | ||
1282 | /************************************************************************* | |
1283 | * thinkpad-acpi init subdriver | |
1284 | */ | |
1285 | ||
1286 | static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm) | |
1287 | { | |
e0c7dfe7 HMH |
1288 | printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION); |
1289 | printk(TPACPI_INFO "%s\n", TPACPI_URL); | |
b21a15f6 | 1290 | |
e0c7dfe7 | 1291 | printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n", |
b21a15f6 HMH |
1292 | (thinkpad_id.bios_version_str) ? |
1293 | thinkpad_id.bios_version_str : "unknown", | |
1294 | (thinkpad_id.ec_version_str) ? | |
1295 | thinkpad_id.ec_version_str : "unknown"); | |
d5a2f2f1 HMH |
1296 | |
1297 | if (thinkpad_id.vendor && thinkpad_id.model_str) | |
8c74adbc | 1298 | printk(TPACPI_INFO "%s %s, model %s\n", |
d5a2f2f1 HMH |
1299 | (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ? |
1300 | "IBM" : ((thinkpad_id.vendor == | |
1301 | PCI_VENDOR_ID_LENOVO) ? | |
1302 | "Lenovo" : "Unknown vendor"), | |
8c74adbc HMH |
1303 | thinkpad_id.model_str, |
1304 | (thinkpad_id.nummodel_str) ? | |
1305 | thinkpad_id.nummodel_str : "unknown"); | |
3945ac36 | 1306 | |
1da177e4 LT |
1307 | return 0; |
1308 | } | |
1309 | ||
643f12db | 1310 | static int thinkpad_acpi_driver_read(char *p) |
1da177e4 LT |
1311 | { |
1312 | int len = 0; | |
1313 | ||
e0c7dfe7 HMH |
1314 | len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC); |
1315 | len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION); | |
1da177e4 LT |
1316 | |
1317 | return len; | |
1318 | } | |
1319 | ||
a5763f22 HMH |
1320 | static struct ibm_struct thinkpad_acpi_driver_data = { |
1321 | .name = "driver", | |
1322 | .read = thinkpad_acpi_driver_read, | |
1323 | }; | |
1324 | ||
56b6aeb0 HMH |
1325 | /************************************************************************* |
1326 | * Hotkey subdriver | |
1327 | */ | |
1328 | ||
f74a27d4 HMH |
1329 | enum { /* hot key scan codes (derived from ACPI DSDT) */ |
1330 | TP_ACPI_HOTKEYSCAN_FNF1 = 0, | |
1331 | TP_ACPI_HOTKEYSCAN_FNF2, | |
1332 | TP_ACPI_HOTKEYSCAN_FNF3, | |
1333 | TP_ACPI_HOTKEYSCAN_FNF4, | |
1334 | TP_ACPI_HOTKEYSCAN_FNF5, | |
1335 | TP_ACPI_HOTKEYSCAN_FNF6, | |
1336 | TP_ACPI_HOTKEYSCAN_FNF7, | |
1337 | TP_ACPI_HOTKEYSCAN_FNF8, | |
1338 | TP_ACPI_HOTKEYSCAN_FNF9, | |
1339 | TP_ACPI_HOTKEYSCAN_FNF10, | |
1340 | TP_ACPI_HOTKEYSCAN_FNF11, | |
1341 | TP_ACPI_HOTKEYSCAN_FNF12, | |
1342 | TP_ACPI_HOTKEYSCAN_FNBACKSPACE, | |
1343 | TP_ACPI_HOTKEYSCAN_FNINSERT, | |
1344 | TP_ACPI_HOTKEYSCAN_FNDELETE, | |
1345 | TP_ACPI_HOTKEYSCAN_FNHOME, | |
1346 | TP_ACPI_HOTKEYSCAN_FNEND, | |
1347 | TP_ACPI_HOTKEYSCAN_FNPAGEUP, | |
1348 | TP_ACPI_HOTKEYSCAN_FNPAGEDOWN, | |
1349 | TP_ACPI_HOTKEYSCAN_FNSPACE, | |
1350 | TP_ACPI_HOTKEYSCAN_VOLUMEUP, | |
1351 | TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, | |
1352 | TP_ACPI_HOTKEYSCAN_MUTE, | |
1353 | TP_ACPI_HOTKEYSCAN_THINKPAD, | |
1354 | }; | |
1355 | ||
01e88f25 HMH |
1356 | enum { /* Keys available through NVRAM polling */ |
1357 | TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U, | |
1358 | TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U, | |
1359 | }; | |
1360 | ||
1361 | enum { /* Positions of some of the keys in hotkey masks */ | |
1362 | TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7, | |
1363 | TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8, | |
1364 | TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12, | |
1365 | TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME, | |
1366 | TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND, | |
1367 | TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP, | |
1368 | TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE, | |
1369 | TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP, | |
1370 | TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, | |
1371 | TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE, | |
1372 | TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD, | |
1373 | }; | |
1374 | ||
1375 | enum { /* NVRAM to ACPI HKEY group map */ | |
1376 | TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK | | |
1377 | TP_ACPI_HKEY_ZOOM_MASK | | |
1378 | TP_ACPI_HKEY_DISPSWTCH_MASK | | |
1379 | TP_ACPI_HKEY_HIBERNATE_MASK, | |
1380 | TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK | | |
1381 | TP_ACPI_HKEY_BRGHTDWN_MASK, | |
1382 | TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK | | |
1383 | TP_ACPI_HKEY_VOLDWN_MASK | | |
1384 | TP_ACPI_HKEY_MUTE_MASK, | |
1385 | }; | |
1386 | ||
1387 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL | |
1388 | struct tp_nvram_state { | |
1389 | u16 thinkpad_toggle:1; | |
1390 | u16 zoom_toggle:1; | |
1391 | u16 display_toggle:1; | |
1392 | u16 thinklight_toggle:1; | |
1393 | u16 hibernate_toggle:1; | |
1394 | u16 displayexp_toggle:1; | |
1395 | u16 display_state:1; | |
1396 | u16 brightness_toggle:1; | |
1397 | u16 volume_toggle:1; | |
1398 | u16 mute:1; | |
1399 | ||
1400 | u8 brightness_level; | |
1401 | u8 volume_level; | |
1402 | }; | |
1403 | ||
1404 | static struct task_struct *tpacpi_hotkey_task; | |
1405 | static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */ | |
1406 | static int hotkey_poll_freq = 10; /* Hz */ | |
1407 | static struct mutex hotkey_thread_mutex; | |
1408 | static struct mutex hotkey_thread_data_mutex; | |
1409 | static unsigned int hotkey_config_change; | |
1410 | ||
1411 | #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ | |
1412 | ||
1413 | #define hotkey_source_mask 0U | |
1414 | ||
1415 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ | |
1416 | ||
f74a27d4 HMH |
1417 | static struct mutex hotkey_mutex; |
1418 | ||
a713b4d7 HMH |
1419 | static enum { /* Reasons for waking up */ |
1420 | TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */ | |
1421 | TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */ | |
1422 | TP_ACPI_WAKEUP_UNDOCK, /* Undock request */ | |
1423 | } hotkey_wakeup_reason; | |
1424 | ||
1425 | static int hotkey_autosleep_ack; | |
1426 | ||
ae92bd17 | 1427 | static u32 hotkey_orig_mask; |
9b010de5 | 1428 | static u32 hotkey_all_mask; |
6a38abbf | 1429 | static u32 hotkey_reserved_mask; |
b2c985e7 | 1430 | static u32 hotkey_mask; |
6a38abbf | 1431 | |
b21a15f6 HMH |
1432 | static unsigned int hotkey_report_mode; |
1433 | ||
edf0e0e5 | 1434 | static u16 *hotkey_keycode_map; |
78f81cc4 | 1435 | |
94954cc6 | 1436 | static struct attribute_set *hotkey_dev_attributes; |
a0416420 | 1437 | |
01e88f25 HMH |
1438 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
1439 | #define HOTKEY_CONFIG_CRITICAL_START \ | |
35ff8b9f HMH |
1440 | do { \ |
1441 | mutex_lock(&hotkey_thread_data_mutex); \ | |
1442 | hotkey_config_change++; \ | |
1443 | } while (0); | |
01e88f25 HMH |
1444 | #define HOTKEY_CONFIG_CRITICAL_END \ |
1445 | mutex_unlock(&hotkey_thread_data_mutex); | |
1446 | #else | |
1447 | #define HOTKEY_CONFIG_CRITICAL_START | |
1448 | #define HOTKEY_CONFIG_CRITICAL_END | |
1449 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ | |
1450 | ||
6c231bd5 HMH |
1451 | /* HKEY.MHKG() return bits */ |
1452 | #define TP_HOTKEY_TABLET_MASK (1 << 3) | |
1453 | ||
74941a69 HMH |
1454 | static int hotkey_get_wlsw(int *status) |
1455 | { | |
a73f3091 HMH |
1456 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
1457 | if (dbg_wlswemul) { | |
1458 | *status = !!tpacpi_wlsw_emulstate; | |
1459 | return 0; | |
1460 | } | |
1461 | #endif | |
74941a69 HMH |
1462 | if (!acpi_evalf(hkey_handle, status, "WLSW", "d")) |
1463 | return -EIO; | |
1464 | return 0; | |
1465 | } | |
1466 | ||
6c231bd5 HMH |
1467 | static int hotkey_get_tablet_mode(int *status) |
1468 | { | |
1469 | int s; | |
1470 | ||
1471 | if (!acpi_evalf(hkey_handle, &s, "MHKG", "d")) | |
1472 | return -EIO; | |
1473 | ||
cee47f5a HMH |
1474 | *status = ((s & TP_HOTKEY_TABLET_MASK) != 0); |
1475 | return 0; | |
6c231bd5 HMH |
1476 | } |
1477 | ||
b2c985e7 HMH |
1478 | /* |
1479 | * Call with hotkey_mutex held | |
1480 | */ | |
1481 | static int hotkey_mask_get(void) | |
1482 | { | |
01e88f25 HMH |
1483 | u32 m = 0; |
1484 | ||
b2c985e7 | 1485 | if (tp_features.hotkey_mask) { |
01e88f25 | 1486 | if (!acpi_evalf(hkey_handle, &m, "DHKN", "d")) |
b2c985e7 HMH |
1487 | return -EIO; |
1488 | } | |
01e88f25 | 1489 | hotkey_mask = m | (hotkey_source_mask & hotkey_mask); |
b2c985e7 HMH |
1490 | |
1491 | return 0; | |
1492 | } | |
1493 | ||
1494 | /* | |
1495 | * Call with hotkey_mutex held | |
1496 | */ | |
1497 | static int hotkey_mask_set(u32 mask) | |
1498 | { | |
1499 | int i; | |
1500 | int rc = 0; | |
1501 | ||
1502 | if (tp_features.hotkey_mask) { | |
92889022 HMH |
1503 | if (!tp_warned.hotkey_mask_ff && |
1504 | (mask == 0xffff || mask == 0xffffff || | |
1505 | mask == 0xffffffff)) { | |
1506 | tp_warned.hotkey_mask_ff = 1; | |
1507 | printk(TPACPI_NOTICE | |
1508 | "setting the hotkey mask to 0x%08x is likely " | |
1509 | "not the best way to go about it\n", mask); | |
1510 | printk(TPACPI_NOTICE | |
1511 | "please consider using the driver defaults, " | |
1512 | "and refer to up-to-date thinkpad-acpi " | |
1513 | "documentation\n"); | |
1514 | } | |
1515 | ||
01e88f25 | 1516 | HOTKEY_CONFIG_CRITICAL_START |
b2c985e7 HMH |
1517 | for (i = 0; i < 32; i++) { |
1518 | u32 m = 1 << i; | |
01e88f25 HMH |
1519 | /* enable in firmware mask only keys not in NVRAM |
1520 | * mode, but enable the key in the cached hotkey_mask | |
1521 | * regardless of mode, or the key will end up | |
1522 | * disabled by hotkey_mask_get() */ | |
b2c985e7 HMH |
1523 | if (!acpi_evalf(hkey_handle, |
1524 | NULL, "MHKM", "vdd", i + 1, | |
01e88f25 | 1525 | !!((mask & ~hotkey_source_mask) & m))) { |
b2c985e7 HMH |
1526 | rc = -EIO; |
1527 | break; | |
1528 | } else { | |
1529 | hotkey_mask = (hotkey_mask & ~m) | (mask & m); | |
1530 | } | |
1531 | } | |
01e88f25 | 1532 | HOTKEY_CONFIG_CRITICAL_END |
b2c985e7 HMH |
1533 | |
1534 | /* hotkey_mask_get must be called unconditionally below */ | |
01e88f25 HMH |
1535 | if (!hotkey_mask_get() && !rc && |
1536 | (hotkey_mask & ~hotkey_source_mask) != | |
1537 | (mask & ~hotkey_source_mask)) { | |
e0c7dfe7 | 1538 | printk(TPACPI_NOTICE |
b2c985e7 HMH |
1539 | "requested hot key mask 0x%08x, but " |
1540 | "firmware forced it to 0x%08x\n", | |
1541 | mask, hotkey_mask); | |
1542 | } | |
01e88f25 HMH |
1543 | } else { |
1544 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL | |
1545 | HOTKEY_CONFIG_CRITICAL_START | |
1546 | hotkey_mask = mask & hotkey_source_mask; | |
1547 | HOTKEY_CONFIG_CRITICAL_END | |
1548 | hotkey_mask_get(); | |
1549 | if (hotkey_mask != mask) { | |
e0c7dfe7 | 1550 | printk(TPACPI_NOTICE |
01e88f25 HMH |
1551 | "requested hot key mask 0x%08x, " |
1552 | "forced to 0x%08x (NVRAM poll mask is " | |
1553 | "0x%08x): no firmware mask support\n", | |
1554 | mask, hotkey_mask, hotkey_source_mask); | |
1555 | } | |
1556 | #else | |
1557 | hotkey_mask_get(); | |
1558 | rc = -ENXIO; | |
1559 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ | |
b2c985e7 HMH |
1560 | } |
1561 | ||
1562 | return rc; | |
1563 | } | |
1564 | ||
1565 | static int hotkey_status_get(int *status) | |
1566 | { | |
1567 | if (!acpi_evalf(hkey_handle, status, "DHKC", "d")) | |
1568 | return -EIO; | |
1569 | ||
1570 | return 0; | |
1571 | } | |
1572 | ||
2586d566 | 1573 | static int hotkey_status_set(bool enable) |
b2c985e7 | 1574 | { |
2586d566 | 1575 | if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0)) |
b2c985e7 HMH |
1576 | return -EIO; |
1577 | ||
1578 | return 0; | |
1579 | } | |
1580 | ||
6c231bd5 | 1581 | static void tpacpi_input_send_tabletsw(void) |
b3ec6f91 | 1582 | { |
6c231bd5 | 1583 | int state; |
b3ec6f91 | 1584 | |
6c231bd5 HMH |
1585 | if (tp_features.hotkey_tablet && |
1586 | !hotkey_get_tablet_mode(&state)) { | |
1587 | mutex_lock(&tpacpi_inputdev_send_mutex); | |
b3ec6f91 | 1588 | |
6c231bd5 HMH |
1589 | input_report_switch(tpacpi_inputdev, |
1590 | SW_TABLET_MODE, !!state); | |
1591 | input_sync(tpacpi_inputdev); | |
1592 | ||
1593 | mutex_unlock(&tpacpi_inputdev_send_mutex); | |
1594 | } | |
b3ec6f91 HMH |
1595 | } |
1596 | ||
b7c8c200 HMH |
1597 | static void tpacpi_input_send_key(unsigned int scancode) |
1598 | { | |
1599 | unsigned int keycode; | |
1600 | ||
1601 | keycode = hotkey_keycode_map[scancode]; | |
1602 | ||
1603 | if (keycode != KEY_RESERVED) { | |
1604 | mutex_lock(&tpacpi_inputdev_send_mutex); | |
1605 | ||
1606 | input_report_key(tpacpi_inputdev, keycode, 1); | |
1607 | if (keycode == KEY_UNKNOWN) | |
1608 | input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, | |
1609 | scancode); | |
1610 | input_sync(tpacpi_inputdev); | |
1611 | ||
1612 | input_report_key(tpacpi_inputdev, keycode, 0); | |
1613 | if (keycode == KEY_UNKNOWN) | |
1614 | input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, | |
1615 | scancode); | |
1616 | input_sync(tpacpi_inputdev); | |
1617 | ||
1618 | mutex_unlock(&tpacpi_inputdev_send_mutex); | |
1619 | } | |
1620 | } | |
1621 | ||
01e88f25 HMH |
1622 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
1623 | static struct tp_acpi_drv_struct ibm_hotkey_acpidriver; | |
1624 | ||
1625 | static void tpacpi_hotkey_send_key(unsigned int scancode) | |
1626 | { | |
1627 | tpacpi_input_send_key(scancode); | |
1628 | if (hotkey_report_mode < 2) { | |
1629 | acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device, | |
1630 | 0x80, 0x1001 + scancode); | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m) | |
1635 | { | |
1636 | u8 d; | |
1637 | ||
1638 | if (m & TP_NVRAM_HKEY_GROUP_HK2) { | |
1639 | d = nvram_read_byte(TP_NVRAM_ADDR_HK2); | |
1640 | n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD); | |
1641 | n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM); | |
1642 | n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY); | |
1643 | n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE); | |
1644 | } | |
1645 | if (m & TP_ACPI_HKEY_THNKLGHT_MASK) { | |
1646 | d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT); | |
1647 | n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT); | |
1648 | } | |
1649 | if (m & TP_ACPI_HKEY_DISPXPAND_MASK) { | |
1650 | d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO); | |
1651 | n->displayexp_toggle = | |
1652 | !!(d & TP_NVRAM_MASK_HKT_DISPEXPND); | |
1653 | } | |
1654 | if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) { | |
1655 | d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS); | |
1656 | n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) | |
1657 | >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; | |
1658 | n->brightness_toggle = | |
1659 | !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS); | |
1660 | } | |
1661 | if (m & TP_NVRAM_HKEY_GROUP_VOLUME) { | |
1662 | d = nvram_read_byte(TP_NVRAM_ADDR_MIXER); | |
1663 | n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME) | |
1664 | >> TP_NVRAM_POS_LEVEL_VOLUME; | |
1665 | n->mute = !!(d & TP_NVRAM_MASK_MUTE); | |
1666 | n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME); | |
1667 | } | |
1668 | } | |
1669 | ||
1670 | #define TPACPI_COMPARE_KEY(__scancode, __member) \ | |
35ff8b9f HMH |
1671 | do { \ |
1672 | if ((mask & (1 << __scancode)) && \ | |
1673 | oldn->__member != newn->__member) \ | |
1674 | tpacpi_hotkey_send_key(__scancode); \ | |
1675 | } while (0) | |
01e88f25 HMH |
1676 | |
1677 | #define TPACPI_MAY_SEND_KEY(__scancode) \ | |
1678 | do { if (mask & (1 << __scancode)) \ | |
1679 | tpacpi_hotkey_send_key(__scancode); } while (0) | |
1680 | ||
1681 | static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, | |
35ff8b9f | 1682 | struct tp_nvram_state *newn, |
01e88f25 HMH |
1683 | u32 mask) |
1684 | { | |
1685 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle); | |
1686 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle); | |
1687 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle); | |
1688 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle); | |
1689 | ||
1690 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle); | |
1691 | ||
1692 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle); | |
1693 | ||
1694 | /* handle volume */ | |
1695 | if (oldn->volume_toggle != newn->volume_toggle) { | |
1696 | if (oldn->mute != newn->mute) { | |
1697 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE); | |
1698 | } | |
1699 | if (oldn->volume_level > newn->volume_level) { | |
1700 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); | |
1701 | } else if (oldn->volume_level < newn->volume_level) { | |
1702 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); | |
1703 | } else if (oldn->mute == newn->mute) { | |
1704 | /* repeated key presses that didn't change state */ | |
1705 | if (newn->mute) { | |
1706 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE); | |
1707 | } else if (newn->volume_level != 0) { | |
1708 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); | |
1709 | } else { | |
1710 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); | |
1711 | } | |
1712 | } | |
1713 | } | |
1714 | ||
1715 | /* handle brightness */ | |
1716 | if (oldn->brightness_toggle != newn->brightness_toggle) { | |
1717 | if (oldn->brightness_level < newn->brightness_level) { | |
1718 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); | |
1719 | } else if (oldn->brightness_level > newn->brightness_level) { | |
1720 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); | |
1721 | } else { | |
1722 | /* repeated key presses that didn't change state */ | |
1723 | if (newn->brightness_level != 0) { | |
1724 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); | |
1725 | } else { | |
1726 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); | |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | } | |
1731 | ||
1732 | #undef TPACPI_COMPARE_KEY | |
1733 | #undef TPACPI_MAY_SEND_KEY | |
1734 | ||
1735 | static int hotkey_kthread(void *data) | |
1736 | { | |
1737 | struct tp_nvram_state s[2]; | |
1738 | u32 mask; | |
1739 | unsigned int si, so; | |
1740 | unsigned long t; | |
1741 | unsigned int change_detector, must_reset; | |
1742 | ||
1743 | mutex_lock(&hotkey_thread_mutex); | |
1744 | ||
1745 | if (tpacpi_lifecycle == TPACPI_LIFE_EXITING) | |
1746 | goto exit; | |
1747 | ||
1748 | set_freezable(); | |
1749 | ||
1750 | so = 0; | |
1751 | si = 1; | |
1752 | t = 0; | |
1753 | ||
1754 | /* Initial state for compares */ | |
1755 | mutex_lock(&hotkey_thread_data_mutex); | |
1756 | change_detector = hotkey_config_change; | |
1757 | mask = hotkey_source_mask & hotkey_mask; | |
1758 | mutex_unlock(&hotkey_thread_data_mutex); | |
1759 | hotkey_read_nvram(&s[so], mask); | |
1760 | ||
1761 | while (!kthread_should_stop() && hotkey_poll_freq) { | |
1762 | if (t == 0) | |
1763 | t = 1000/hotkey_poll_freq; | |
1764 | t = msleep_interruptible(t); | |
1765 | if (unlikely(kthread_should_stop())) | |
1766 | break; | |
1767 | must_reset = try_to_freeze(); | |
1768 | if (t > 0 && !must_reset) | |
1769 | continue; | |
1770 | ||
1771 | mutex_lock(&hotkey_thread_data_mutex); | |
1772 | if (must_reset || hotkey_config_change != change_detector) { | |
1773 | /* forget old state on thaw or config change */ | |
1774 | si = so; | |
1775 | t = 0; | |
1776 | change_detector = hotkey_config_change; | |
1777 | } | |
1778 | mask = hotkey_source_mask & hotkey_mask; | |
1779 | mutex_unlock(&hotkey_thread_data_mutex); | |
1780 | ||
1781 | if (likely(mask)) { | |
1782 | hotkey_read_nvram(&s[si], mask); | |
1783 | if (likely(si != so)) { | |
1784 | hotkey_compare_and_issue_event(&s[so], &s[si], | |
35ff8b9f | 1785 | mask); |
01e88f25 HMH |
1786 | } |
1787 | } | |
1788 | ||
1789 | so = si; | |
1790 | si ^= 1; | |
1791 | } | |
1792 | ||
1793 | exit: | |
1794 | mutex_unlock(&hotkey_thread_mutex); | |
1795 | return 0; | |
1796 | } | |
1797 | ||
1798 | static void hotkey_poll_stop_sync(void) | |
1799 | { | |
1800 | if (tpacpi_hotkey_task) { | |
1801 | if (frozen(tpacpi_hotkey_task) || | |
1802 | freezing(tpacpi_hotkey_task)) | |
1803 | thaw_process(tpacpi_hotkey_task); | |
1804 | ||
1805 | kthread_stop(tpacpi_hotkey_task); | |
1806 | tpacpi_hotkey_task = NULL; | |
1807 | mutex_lock(&hotkey_thread_mutex); | |
1808 | /* at this point, the thread did exit */ | |
1809 | mutex_unlock(&hotkey_thread_mutex); | |
1810 | } | |
1811 | } | |
1812 | ||
1813 | /* call with hotkey_mutex held */ | |
1814 | static void hotkey_poll_setup(int may_warn) | |
1815 | { | |
1816 | if ((hotkey_source_mask & hotkey_mask) != 0 && | |
1817 | hotkey_poll_freq > 0 && | |
1818 | (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) { | |
1819 | if (!tpacpi_hotkey_task) { | |
1820 | tpacpi_hotkey_task = kthread_run(hotkey_kthread, | |
95e57ab2 | 1821 | NULL, TPACPI_NVRAM_KTHREAD_NAME); |
01e88f25 HMH |
1822 | if (IS_ERR(tpacpi_hotkey_task)) { |
1823 | tpacpi_hotkey_task = NULL; | |
35ff8b9f HMH |
1824 | printk(TPACPI_ERR |
1825 | "could not create kernel thread " | |
01e88f25 HMH |
1826 | "for hotkey polling\n"); |
1827 | } | |
1828 | } | |
1829 | } else { | |
1830 | hotkey_poll_stop_sync(); | |
1831 | if (may_warn && | |
1832 | hotkey_source_mask != 0 && hotkey_poll_freq == 0) { | |
35ff8b9f HMH |
1833 | printk(TPACPI_NOTICE |
1834 | "hot keys 0x%08x require polling, " | |
01e88f25 HMH |
1835 | "which is currently disabled\n", |
1836 | hotkey_source_mask); | |
1837 | } | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | static void hotkey_poll_setup_safe(int may_warn) | |
1842 | { | |
1843 | mutex_lock(&hotkey_mutex); | |
1844 | hotkey_poll_setup(may_warn); | |
1845 | mutex_unlock(&hotkey_mutex); | |
1846 | } | |
1847 | ||
1bc6b9cd HMH |
1848 | #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ |
1849 | ||
1850 | static void hotkey_poll_setup_safe(int __unused) | |
1851 | { | |
1852 | } | |
1853 | ||
1854 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ | |
1855 | ||
01e88f25 HMH |
1856 | static int hotkey_inputdev_open(struct input_dev *dev) |
1857 | { | |
1858 | switch (tpacpi_lifecycle) { | |
1859 | case TPACPI_LIFE_INIT: | |
1860 | /* | |
1861 | * hotkey_init will call hotkey_poll_setup_safe | |
1862 | * at the appropriate moment | |
1863 | */ | |
1864 | return 0; | |
1865 | case TPACPI_LIFE_EXITING: | |
1866 | return -EBUSY; | |
1867 | case TPACPI_LIFE_RUNNING: | |
1868 | hotkey_poll_setup_safe(0); | |
1869 | return 0; | |
1870 | } | |
1871 | ||
1872 | /* Should only happen if tpacpi_lifecycle is corrupt */ | |
1873 | BUG(); | |
1874 | return -EBUSY; | |
1875 | } | |
1876 | ||
1877 | static void hotkey_inputdev_close(struct input_dev *dev) | |
1878 | { | |
1879 | /* disable hotkey polling when possible */ | |
1880 | if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING) | |
1881 | hotkey_poll_setup_safe(0); | |
1882 | } | |
01e88f25 | 1883 | |
a0416420 HMH |
1884 | /* sysfs hotkey enable ------------------------------------------------- */ |
1885 | static ssize_t hotkey_enable_show(struct device *dev, | |
1886 | struct device_attribute *attr, | |
1887 | char *buf) | |
1888 | { | |
ae92bd17 | 1889 | int res, status; |
a0416420 | 1890 | |
2586d566 HMH |
1891 | printk_deprecated_attribute("hotkey_enable", |
1892 | "Hotkey reporting is always enabled"); | |
1893 | ||
b2c985e7 | 1894 | res = hotkey_status_get(&status); |
a0416420 HMH |
1895 | if (res) |
1896 | return res; | |
1897 | ||
1898 | return snprintf(buf, PAGE_SIZE, "%d\n", status); | |
1899 | } | |
1900 | ||
1901 | static ssize_t hotkey_enable_store(struct device *dev, | |
1902 | struct device_attribute *attr, | |
1903 | const char *buf, size_t count) | |
1904 | { | |
1905 | unsigned long t; | |
2586d566 HMH |
1906 | |
1907 | printk_deprecated_attribute("hotkey_enable", | |
1908 | "Hotkeys can be disabled through hotkey_mask"); | |
a0416420 HMH |
1909 | |
1910 | if (parse_strtoul(buf, 1, &t)) | |
1911 | return -EINVAL; | |
1912 | ||
2586d566 HMH |
1913 | if (t == 0) |
1914 | return -EPERM; | |
a0416420 | 1915 | |
2586d566 | 1916 | return count; |
a0416420 HMH |
1917 | } |
1918 | ||
1919 | static struct device_attribute dev_attr_hotkey_enable = | |
cc4c24e1 | 1920 | __ATTR(hotkey_enable, S_IWUSR | S_IRUGO, |
a0416420 HMH |
1921 | hotkey_enable_show, hotkey_enable_store); |
1922 | ||
1923 | /* sysfs hotkey mask --------------------------------------------------- */ | |
1924 | static ssize_t hotkey_mask_show(struct device *dev, | |
1925 | struct device_attribute *attr, | |
1926 | char *buf) | |
1927 | { | |
b2c985e7 | 1928 | int res; |
a0416420 | 1929 | |
7646ea88 | 1930 | if (mutex_lock_killable(&hotkey_mutex)) |
b2c985e7 HMH |
1931 | return -ERESTARTSYS; |
1932 | res = hotkey_mask_get(); | |
1933 | mutex_unlock(&hotkey_mutex); | |
a0416420 | 1934 | |
b2c985e7 HMH |
1935 | return (res)? |
1936 | res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask); | |
a0416420 HMH |
1937 | } |
1938 | ||
1939 | static ssize_t hotkey_mask_store(struct device *dev, | |
1940 | struct device_attribute *attr, | |
1941 | const char *buf, size_t count) | |
1942 | { | |
1943 | unsigned long t; | |
b2c985e7 | 1944 | int res; |
a0416420 | 1945 | |
ae92bd17 | 1946 | if (parse_strtoul(buf, 0xffffffffUL, &t)) |
a0416420 HMH |
1947 | return -EINVAL; |
1948 | ||
7646ea88 | 1949 | if (mutex_lock_killable(&hotkey_mutex)) |
b2c985e7 HMH |
1950 | return -ERESTARTSYS; |
1951 | ||
1952 | res = hotkey_mask_set(t); | |
01e88f25 HMH |
1953 | |
1954 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL | |
1955 | hotkey_poll_setup(1); | |
1956 | #endif | |
1957 | ||
b2c985e7 | 1958 | mutex_unlock(&hotkey_mutex); |
a0416420 HMH |
1959 | |
1960 | return (res) ? res : count; | |
1961 | } | |
1962 | ||
1963 | static struct device_attribute dev_attr_hotkey_mask = | |
cc4c24e1 | 1964 | __ATTR(hotkey_mask, S_IWUSR | S_IRUGO, |
a0416420 HMH |
1965 | hotkey_mask_show, hotkey_mask_store); |
1966 | ||
1967 | /* sysfs hotkey bios_enabled ------------------------------------------- */ | |
1968 | static ssize_t hotkey_bios_enabled_show(struct device *dev, | |
1969 | struct device_attribute *attr, | |
1970 | char *buf) | |
1971 | { | |
2586d566 | 1972 | return sprintf(buf, "0\n"); |
a0416420 HMH |
1973 | } |
1974 | ||
1975 | static struct device_attribute dev_attr_hotkey_bios_enabled = | |
cc4c24e1 | 1976 | __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL); |
a0416420 HMH |
1977 | |
1978 | /* sysfs hotkey bios_mask ---------------------------------------------- */ | |
1979 | static ssize_t hotkey_bios_mask_show(struct device *dev, | |
1980 | struct device_attribute *attr, | |
1981 | char *buf) | |
1982 | { | |
ae92bd17 | 1983 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask); |
a0416420 HMH |
1984 | } |
1985 | ||
1986 | static struct device_attribute dev_attr_hotkey_bios_mask = | |
cc4c24e1 | 1987 | __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL); |
a0416420 | 1988 | |
9b010de5 HMH |
1989 | /* sysfs hotkey all_mask ----------------------------------------------- */ |
1990 | static ssize_t hotkey_all_mask_show(struct device *dev, | |
1991 | struct device_attribute *attr, | |
1992 | char *buf) | |
1993 | { | |
01e88f25 HMH |
1994 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", |
1995 | hotkey_all_mask | hotkey_source_mask); | |
9b010de5 HMH |
1996 | } |
1997 | ||
1998 | static struct device_attribute dev_attr_hotkey_all_mask = | |
1999 | __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL); | |
2000 | ||
2001 | /* sysfs hotkey recommended_mask --------------------------------------- */ | |
2002 | static ssize_t hotkey_recommended_mask_show(struct device *dev, | |
2003 | struct device_attribute *attr, | |
2004 | char *buf) | |
2005 | { | |
2006 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", | |
01e88f25 HMH |
2007 | (hotkey_all_mask | hotkey_source_mask) |
2008 | & ~hotkey_reserved_mask); | |
9b010de5 HMH |
2009 | } |
2010 | ||
2011 | static struct device_attribute dev_attr_hotkey_recommended_mask = | |
2012 | __ATTR(hotkey_recommended_mask, S_IRUGO, | |
2013 | hotkey_recommended_mask_show, NULL); | |
2014 | ||
01e88f25 HMH |
2015 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
2016 | ||
2017 | /* sysfs hotkey hotkey_source_mask ------------------------------------- */ | |
2018 | static ssize_t hotkey_source_mask_show(struct device *dev, | |
2019 | struct device_attribute *attr, | |
2020 | char *buf) | |
2021 | { | |
2022 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask); | |
2023 | } | |
2024 | ||
2025 | static ssize_t hotkey_source_mask_store(struct device *dev, | |
2026 | struct device_attribute *attr, | |
2027 | const char *buf, size_t count) | |
2028 | { | |
2029 | unsigned long t; | |
2030 | ||
2031 | if (parse_strtoul(buf, 0xffffffffUL, &t) || | |
2032 | ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0)) | |
2033 | return -EINVAL; | |
2034 | ||
7646ea88 | 2035 | if (mutex_lock_killable(&hotkey_mutex)) |
01e88f25 HMH |
2036 | return -ERESTARTSYS; |
2037 | ||
2038 | HOTKEY_CONFIG_CRITICAL_START | |
2039 | hotkey_source_mask = t; | |
2040 | HOTKEY_CONFIG_CRITICAL_END | |
2041 | ||
2042 | hotkey_poll_setup(1); | |
2043 | ||
2044 | mutex_unlock(&hotkey_mutex); | |
2045 | ||
2046 | return count; | |
2047 | } | |
2048 | ||
2049 | static struct device_attribute dev_attr_hotkey_source_mask = | |
2050 | __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO, | |
2051 | hotkey_source_mask_show, hotkey_source_mask_store); | |
2052 | ||
2053 | /* sysfs hotkey hotkey_poll_freq --------------------------------------- */ | |
2054 | static ssize_t hotkey_poll_freq_show(struct device *dev, | |
2055 | struct device_attribute *attr, | |
2056 | char *buf) | |
2057 | { | |
2058 | return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq); | |
2059 | } | |
2060 | ||
2061 | static ssize_t hotkey_poll_freq_store(struct device *dev, | |
2062 | struct device_attribute *attr, | |
2063 | const char *buf, size_t count) | |
2064 | { | |
2065 | unsigned long t; | |
2066 | ||
2067 | if (parse_strtoul(buf, 25, &t)) | |
2068 | return -EINVAL; | |
2069 | ||
7646ea88 | 2070 | if (mutex_lock_killable(&hotkey_mutex)) |
01e88f25 HMH |
2071 | return -ERESTARTSYS; |
2072 | ||
2073 | hotkey_poll_freq = t; | |
2074 | ||
2075 | hotkey_poll_setup(1); | |
2076 | mutex_unlock(&hotkey_mutex); | |
2077 | ||
2078 | return count; | |
2079 | } | |
2080 | ||
2081 | static struct device_attribute dev_attr_hotkey_poll_freq = | |
2082 | __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO, | |
2083 | hotkey_poll_freq_show, hotkey_poll_freq_store); | |
2084 | ||
2085 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ | |
2086 | ||
50ebec09 | 2087 | /* sysfs hotkey radio_sw (pollable) ------------------------------------ */ |
74941a69 HMH |
2088 | static ssize_t hotkey_radio_sw_show(struct device *dev, |
2089 | struct device_attribute *attr, | |
2090 | char *buf) | |
2091 | { | |
2092 | int res, s; | |
2093 | res = hotkey_get_wlsw(&s); | |
2094 | if (res < 0) | |
2095 | return res; | |
2096 | ||
2097 | return snprintf(buf, PAGE_SIZE, "%d\n", !!s); | |
2098 | } | |
2099 | ||
2100 | static struct device_attribute dev_attr_hotkey_radio_sw = | |
2101 | __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL); | |
2102 | ||
50ebec09 HMH |
2103 | static void hotkey_radio_sw_notify_change(void) |
2104 | { | |
2105 | if (tp_features.hotkey_wlsw) | |
2106 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, | |
2107 | "hotkey_radio_sw"); | |
2108 | } | |
2109 | ||
6c231bd5 HMH |
2110 | /* sysfs hotkey tablet mode (pollable) --------------------------------- */ |
2111 | static ssize_t hotkey_tablet_mode_show(struct device *dev, | |
2112 | struct device_attribute *attr, | |
2113 | char *buf) | |
2114 | { | |
2115 | int res, s; | |
2116 | res = hotkey_get_tablet_mode(&s); | |
2117 | if (res < 0) | |
2118 | return res; | |
2119 | ||
2120 | return snprintf(buf, PAGE_SIZE, "%d\n", !!s); | |
2121 | } | |
2122 | ||
2123 | static struct device_attribute dev_attr_hotkey_tablet_mode = | |
2124 | __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL); | |
2125 | ||
2126 | static void hotkey_tablet_mode_notify_change(void) | |
2127 | { | |
2128 | if (tp_features.hotkey_tablet) | |
2129 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, | |
2130 | "hotkey_tablet_mode"); | |
2131 | } | |
2132 | ||
ff80f137 HMH |
2133 | /* sysfs hotkey report_mode -------------------------------------------- */ |
2134 | static ssize_t hotkey_report_mode_show(struct device *dev, | |
2135 | struct device_attribute *attr, | |
2136 | char *buf) | |
2137 | { | |
2138 | return snprintf(buf, PAGE_SIZE, "%d\n", | |
2139 | (hotkey_report_mode != 0) ? hotkey_report_mode : 1); | |
2140 | } | |
2141 | ||
2142 | static struct device_attribute dev_attr_hotkey_report_mode = | |
2143 | __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL); | |
2144 | ||
50ebec09 | 2145 | /* sysfs wakeup reason (pollable) -------------------------------------- */ |
a713b4d7 HMH |
2146 | static ssize_t hotkey_wakeup_reason_show(struct device *dev, |
2147 | struct device_attribute *attr, | |
2148 | char *buf) | |
2149 | { | |
2150 | return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason); | |
2151 | } | |
2152 | ||
2153 | static struct device_attribute dev_attr_hotkey_wakeup_reason = | |
2154 | __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL); | |
2155 | ||
1d5a2b54 | 2156 | static void hotkey_wakeup_reason_notify_change(void) |
50ebec09 HMH |
2157 | { |
2158 | if (tp_features.hotkey_mask) | |
2159 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, | |
2160 | "wakeup_reason"); | |
2161 | } | |
2162 | ||
2163 | /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */ | |
a713b4d7 HMH |
2164 | static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev, |
2165 | struct device_attribute *attr, | |
2166 | char *buf) | |
2167 | { | |
2168 | return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack); | |
2169 | } | |
2170 | ||
2171 | static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete = | |
2172 | __ATTR(wakeup_hotunplug_complete, S_IRUGO, | |
2173 | hotkey_wakeup_hotunplug_complete_show, NULL); | |
2174 | ||
1d5a2b54 | 2175 | static void hotkey_wakeup_hotunplug_complete_notify_change(void) |
50ebec09 HMH |
2176 | { |
2177 | if (tp_features.hotkey_mask) | |
2178 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, | |
2179 | "wakeup_hotunplug_complete"); | |
2180 | } | |
2181 | ||
a0416420 HMH |
2182 | /* --------------------------------------------------------------------- */ |
2183 | ||
ff80f137 HMH |
2184 | static struct attribute *hotkey_attributes[] __initdata = { |
2185 | &dev_attr_hotkey_enable.attr, | |
01e88f25 | 2186 | &dev_attr_hotkey_bios_enabled.attr, |
ff80f137 | 2187 | &dev_attr_hotkey_report_mode.attr, |
01e88f25 HMH |
2188 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
2189 | &dev_attr_hotkey_mask.attr, | |
2190 | &dev_attr_hotkey_all_mask.attr, | |
2191 | &dev_attr_hotkey_recommended_mask.attr, | |
2192 | &dev_attr_hotkey_source_mask.attr, | |
2193 | &dev_attr_hotkey_poll_freq.attr, | |
2194 | #endif | |
ff80f137 HMH |
2195 | }; |
2196 | ||
2197 | static struct attribute *hotkey_mask_attributes[] __initdata = { | |
a0416420 | 2198 | &dev_attr_hotkey_bios_mask.attr, |
01e88f25 HMH |
2199 | #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
2200 | &dev_attr_hotkey_mask.attr, | |
9b010de5 HMH |
2201 | &dev_attr_hotkey_all_mask.attr, |
2202 | &dev_attr_hotkey_recommended_mask.attr, | |
01e88f25 | 2203 | #endif |
a713b4d7 HMH |
2204 | &dev_attr_hotkey_wakeup_reason.attr, |
2205 | &dev_attr_hotkey_wakeup_hotunplug_complete.attr, | |
a0416420 HMH |
2206 | }; |
2207 | ||
0e74dc26 HMH |
2208 | static void bluetooth_update_rfk(void); |
2209 | static void wan_update_rfk(void); | |
0045c0aa | 2210 | static void uwb_update_rfk(void); |
733e27c1 HMH |
2211 | static void tpacpi_send_radiosw_update(void) |
2212 | { | |
2213 | int wlsw; | |
2214 | ||
0e74dc26 HMH |
2215 | /* Sync these BEFORE sending any rfkill events */ |
2216 | if (tp_features.bluetooth) | |
2217 | bluetooth_update_rfk(); | |
2218 | if (tp_features.wan) | |
2219 | wan_update_rfk(); | |
0045c0aa HMH |
2220 | if (tp_features.uwb) |
2221 | uwb_update_rfk(); | |
0e74dc26 | 2222 | |
733e27c1 HMH |
2223 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) { |
2224 | mutex_lock(&tpacpi_inputdev_send_mutex); | |
2225 | ||
2226 | input_report_switch(tpacpi_inputdev, | |
2227 | SW_RFKILL_ALL, !!wlsw); | |
2228 | input_sync(tpacpi_inputdev); | |
2229 | ||
2230 | mutex_unlock(&tpacpi_inputdev_send_mutex); | |
2231 | } | |
2232 | hotkey_radio_sw_notify_change(); | |
2233 | } | |
2234 | ||
9c0a76e1 HMH |
2235 | static void hotkey_exit(void) |
2236 | { | |
2237 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL | |
2238 | hotkey_poll_stop_sync(); | |
2239 | #endif | |
2240 | ||
2241 | if (hotkey_dev_attributes) | |
2242 | delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj); | |
2243 | ||
2244 | kfree(hotkey_keycode_map); | |
2245 | ||
2246 | if (tp_features.hotkey) { | |
2247 | dbg_printk(TPACPI_DBG_EXIT, | |
2248 | "restoring original hot key mask\n"); | |
2249 | /* no short-circuit boolean operator below! */ | |
2250 | if ((hotkey_mask_set(hotkey_orig_mask) | | |
2586d566 | 2251 | hotkey_status_set(false)) != 0) |
9c0a76e1 HMH |
2252 | printk(TPACPI_ERR |
2253 | "failed to restore hot key mask " | |
2254 | "to BIOS defaults\n"); | |
2255 | } | |
2256 | } | |
2257 | ||
a5763f22 | 2258 | static int __init hotkey_init(struct ibm_init_struct *iibm) |
56b6aeb0 | 2259 | { |
0f089147 HMH |
2260 | /* Requirements for changing the default keymaps: |
2261 | * | |
2262 | * 1. Many of the keys are mapped to KEY_RESERVED for very | |
2263 | * good reasons. Do not change them unless you have deep | |
2264 | * knowledge on the IBM and Lenovo ThinkPad firmware for | |
2265 | * the various ThinkPad models. The driver behaves | |
2266 | * differently for KEY_RESERVED: such keys have their | |
2267 | * hot key mask *unset* in mask_recommended, and also | |
2268 | * in the initial hot key mask programmed into the | |
2269 | * firmware at driver load time, which means the firm- | |
2270 | * ware may react very differently if you change them to | |
2271 | * something else; | |
2272 | * | |
2273 | * 2. You must be subscribed to the linux-thinkpad and | |
2274 | * ibm-acpi-devel mailing lists, and you should read the | |
2275 | * list archives since 2007 if you want to change the | |
2276 | * keymaps. This requirement exists so that you will | |
2277 | * know the past history of problems with the thinkpad- | |
2278 | * acpi driver keymaps, and also that you will be | |
2279 | * listening to any bug reports; | |
2280 | * | |
2281 | * 3. Do not send thinkpad-acpi specific patches directly to | |
2282 | * for merging, *ever*. Send them to the linux-acpi | |
2283 | * mailinglist for comments. Merging is to be done only | |
2284 | * through acpi-test and the ACPI maintainer. | |
2285 | * | |
2286 | * If the above is too much to ask, don't change the keymap. | |
2287 | * Ask the thinkpad-acpi maintainer to do it, instead. | |
2288 | */ | |
edf0e0e5 HMH |
2289 | static u16 ibm_keycode_map[] __initdata = { |
2290 | /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ | |
2291 | KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP, | |
2292 | KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, | |
2293 | KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, | |
0f089147 HMH |
2294 | |
2295 | /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */ | |
edf0e0e5 HMH |
2296 | KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ |
2297 | KEY_UNKNOWN, /* 0x0D: FN+INSERT */ | |
2298 | KEY_UNKNOWN, /* 0x0E: FN+DELETE */ | |
0f089147 HMH |
2299 | |
2300 | /* brightness: firmware always reacts to them, unless | |
2301 | * X.org did some tricks in the radeon BIOS scratch | |
2302 | * registers of *some* models */ | |
e927c08d | 2303 | KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */ |
e927c08d | 2304 | KEY_RESERVED, /* 0x10: FN+END (brightness down) */ |
0f089147 HMH |
2305 | |
2306 | /* Thinklight: firmware always react to it */ | |
edf0e0e5 | 2307 | KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ |
0f089147 | 2308 | |
edf0e0e5 HMH |
2309 | KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ |
2310 | KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ | |
0f089147 HMH |
2311 | |
2312 | /* Volume: firmware always react to it and reprograms | |
2313 | * the built-in *extra* mixer. Never map it to control | |
2314 | * another mixer by default. */ | |
e927c08d HMH |
2315 | KEY_RESERVED, /* 0x14: VOLUME UP */ |
2316 | KEY_RESERVED, /* 0x15: VOLUME DOWN */ | |
2317 | KEY_RESERVED, /* 0x16: MUTE */ | |
0f089147 | 2318 | |
edf0e0e5 | 2319 | KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ |
0f089147 | 2320 | |
edf0e0e5 HMH |
2321 | /* (assignments unknown, please report if found) */ |
2322 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
2323 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
2324 | }; | |
2325 | static u16 lenovo_keycode_map[] __initdata = { | |
2326 | /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ | |
2327 | KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP, | |
2328 | KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, | |
2329 | KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, | |
0f089147 HMH |
2330 | |
2331 | /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */ | |
edf0e0e5 HMH |
2332 | KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ |
2333 | KEY_UNKNOWN, /* 0x0D: FN+INSERT */ | |
2334 | KEY_UNKNOWN, /* 0x0E: FN+DELETE */ | |
0f089147 | 2335 | |
b5972796 HMH |
2336 | /* These either have to go through ACPI video, or |
2337 | * act like in the IBM ThinkPads, so don't ever | |
2338 | * enable them by default */ | |
56a185b4 | 2339 | KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */ |
56a185b4 | 2340 | KEY_RESERVED, /* 0x10: FN+END (brightness down) */ |
0f089147 | 2341 | |
edf0e0e5 | 2342 | KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ |
0f089147 | 2343 | |
edf0e0e5 HMH |
2344 | KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ |
2345 | KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ | |
0f089147 HMH |
2346 | |
2347 | /* Volume: z60/z61, T60 (BIOS version?): firmware always | |
2348 | * react to it and reprograms the built-in *extra* mixer. | |
2349 | * Never map it to control another mixer by default. | |
2350 | * | |
2351 | * T60?, T61, R60?, R61: firmware and EC tries to send | |
2352 | * these over the regular keyboard, so these are no-ops, | |
2353 | * but there are still weird bugs re. MUTE, so do not | |
2354 | * change unless you get test reports from all Lenovo | |
2355 | * models. May cause the BIOS to interfere with the | |
2356 | * HDA mixer. | |
2357 | */ | |
e927c08d HMH |
2358 | KEY_RESERVED, /* 0x14: VOLUME UP */ |
2359 | KEY_RESERVED, /* 0x15: VOLUME DOWN */ | |
2360 | KEY_RESERVED, /* 0x16: MUTE */ | |
0f089147 | 2361 | |
edf0e0e5 | 2362 | KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ |
0f089147 | 2363 | |
edf0e0e5 HMH |
2364 | /* (assignments unknown, please report if found) */ |
2365 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
2366 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
2367 | }; | |
2368 | ||
2369 | #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map) | |
2370 | #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map) | |
2371 | #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0]) | |
2372 | ||
6a38abbf | 2373 | int res, i; |
74941a69 | 2374 | int status; |
1b6521dc | 2375 | int hkeyv; |
b86c4722 | 2376 | |
fe08bc4b HMH |
2377 | vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n"); |
2378 | ||
6a38abbf | 2379 | BUG_ON(!tpacpi_inputdev); |
01e88f25 HMH |
2380 | BUG_ON(tpacpi_inputdev->open != NULL || |
2381 | tpacpi_inputdev->close != NULL); | |
6a38abbf | 2382 | |
e0c7dfe7 | 2383 | TPACPI_ACPIHANDLE_INIT(hkey); |
40ca9fdf | 2384 | mutex_init(&hotkey_mutex); |
5fba344c | 2385 | |
01e88f25 HMH |
2386 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
2387 | mutex_init(&hotkey_thread_mutex); | |
2388 | mutex_init(&hotkey_thread_data_mutex); | |
2389 | #endif | |
2390 | ||
56b6aeb0 | 2391 | /* hotkey not supported on 570 */ |
d8fd94d9 | 2392 | tp_features.hotkey = hkey_handle != NULL; |
56b6aeb0 | 2393 | |
fe08bc4b | 2394 | vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n", |
d8fd94d9 | 2395 | str_supported(tp_features.hotkey)); |
fe08bc4b | 2396 | |
9c0a76e1 HMH |
2397 | if (!tp_features.hotkey) |
2398 | return 1; | |
a0416420 | 2399 | |
d64c81c4 HMH |
2400 | tpacpi_disable_brightness_delay(); |
2401 | ||
9c0a76e1 HMH |
2402 | hotkey_dev_attributes = create_attr_set(13, NULL); |
2403 | if (!hotkey_dev_attributes) | |
2404 | return -ENOMEM; | |
2405 | res = add_many_to_attr_set(hotkey_dev_attributes, | |
2406 | hotkey_attributes, | |
2407 | ARRAY_SIZE(hotkey_attributes)); | |
2408 | if (res) | |
2409 | goto err_exit; | |
2410 | ||
2411 | /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, | |
2412 | A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking | |
2413 | for HKEY interface version 0x100 */ | |
2414 | if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) { | |
2415 | if ((hkeyv >> 8) != 1) { | |
2416 | printk(TPACPI_ERR "unknown version of the " | |
2417 | "HKEY interface: 0x%x\n", hkeyv); | |
2418 | printk(TPACPI_ERR "please report this to %s\n", | |
2419 | TPACPI_MAIL); | |
2420 | } else { | |
2421 | /* | |
2422 | * MHKV 0x100 in A31, R40, R40e, | |
2423 | * T4x, X31, and later | |
2424 | */ | |
2425 | tp_features.hotkey_mask = 1; | |
1b6521dc | 2426 | } |
9c0a76e1 | 2427 | } |
56b6aeb0 | 2428 | |
9c0a76e1 HMH |
2429 | vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n", |
2430 | str_supported(tp_features.hotkey_mask)); | |
fe08bc4b | 2431 | |
9c0a76e1 HMH |
2432 | if (tp_features.hotkey_mask) { |
2433 | if (!acpi_evalf(hkey_handle, &hotkey_all_mask, | |
2434 | "MHKA", "qd")) { | |
2435 | printk(TPACPI_ERR | |
2436 | "missing MHKA handler, " | |
2437 | "please report this to %s\n", | |
2438 | TPACPI_MAIL); | |
2439 | /* FN+F12, FN+F4, FN+F3 */ | |
2440 | hotkey_all_mask = 0x080cU; | |
9b010de5 | 2441 | } |
9c0a76e1 | 2442 | } |
9b010de5 | 2443 | |
9c0a76e1 HMH |
2444 | /* hotkey_source_mask *must* be zero for |
2445 | * the first hotkey_mask_get */ | |
9c0a76e1 HMH |
2446 | if (tp_features.hotkey_mask) { |
2447 | res = hotkey_mask_get(); | |
2448 | if (res) | |
2449 | goto err_exit; | |
2450 | ||
2451 | hotkey_orig_mask = hotkey_mask; | |
2452 | res = add_many_to_attr_set( | |
2453 | hotkey_dev_attributes, | |
2454 | hotkey_mask_attributes, | |
2455 | ARRAY_SIZE(hotkey_mask_attributes)); | |
2456 | if (res) | |
2457 | goto err_exit; | |
2458 | } | |
74941a69 | 2459 | |
01e88f25 | 2460 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
9c0a76e1 HMH |
2461 | if (tp_features.hotkey_mask) { |
2462 | hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK | |
2463 | & ~hotkey_all_mask; | |
2464 | } else { | |
2465 | hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK; | |
2466 | } | |
01e88f25 | 2467 | |
9c0a76e1 HMH |
2468 | vdbg_printk(TPACPI_DBG_INIT, |
2469 | "hotkey source mask 0x%08x, polling freq %d\n", | |
2470 | hotkey_source_mask, hotkey_poll_freq); | |
01e88f25 HMH |
2471 | #endif |
2472 | ||
a73f3091 HMH |
2473 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
2474 | if (dbg_wlswemul) { | |
2475 | tp_features.hotkey_wlsw = 1; | |
2476 | printk(TPACPI_INFO | |
2477 | "radio switch emulation enabled\n"); | |
2478 | } else | |
2479 | #endif | |
9c0a76e1 HMH |
2480 | /* Not all thinkpads have a hardware radio switch */ |
2481 | if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) { | |
2482 | tp_features.hotkey_wlsw = 1; | |
2483 | printk(TPACPI_INFO | |
2484 | "radio switch found; radios are %s\n", | |
2485 | enabled(status, 0)); | |
3a872080 HMH |
2486 | } |
2487 | if (tp_features.hotkey_wlsw) | |
9c0a76e1 HMH |
2488 | res = add_to_attr_set(hotkey_dev_attributes, |
2489 | &dev_attr_hotkey_radio_sw.attr); | |
74941a69 | 2490 | |
9c0a76e1 HMH |
2491 | /* For X41t, X60t, X61t Tablets... */ |
2492 | if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) { | |
2493 | tp_features.hotkey_tablet = 1; | |
2494 | printk(TPACPI_INFO | |
2495 | "possible tablet mode switch found; " | |
2496 | "ThinkPad in %s mode\n", | |
2497 | (status & TP_HOTKEY_TABLET_MASK)? | |
2498 | "tablet" : "laptop"); | |
2499 | res = add_to_attr_set(hotkey_dev_attributes, | |
2500 | &dev_attr_hotkey_tablet_mode.attr); | |
2501 | } | |
6c231bd5 | 2502 | |
9c0a76e1 HMH |
2503 | if (!res) |
2504 | res = register_attr_set_with_sysfs( | |
2505 | hotkey_dev_attributes, | |
2506 | &tpacpi_pdev->dev.kobj); | |
2507 | if (res) | |
2508 | goto err_exit; | |
6a38abbf | 2509 | |
9c0a76e1 | 2510 | /* Set up key map */ |
edf0e0e5 | 2511 | |
9c0a76e1 HMH |
2512 | hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE, |
2513 | GFP_KERNEL); | |
2514 | if (!hotkey_keycode_map) { | |
2515 | printk(TPACPI_ERR | |
2516 | "failed to allocate memory for key map\n"); | |
2517 | res = -ENOMEM; | |
2518 | goto err_exit; | |
2519 | } | |
edf0e0e5 | 2520 | |
9c0a76e1 HMH |
2521 | if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) { |
2522 | dbg_printk(TPACPI_DBG_INIT, | |
2523 | "using Lenovo default hot key map\n"); | |
2524 | memcpy(hotkey_keycode_map, &lenovo_keycode_map, | |
2525 | TPACPI_HOTKEY_MAP_SIZE); | |
2526 | } else { | |
2527 | dbg_printk(TPACPI_DBG_INIT, | |
2528 | "using IBM default hot key map\n"); | |
2529 | memcpy(hotkey_keycode_map, &ibm_keycode_map, | |
2530 | TPACPI_HOTKEY_MAP_SIZE); | |
2531 | } | |
edf0e0e5 | 2532 | |
9c0a76e1 HMH |
2533 | set_bit(EV_KEY, tpacpi_inputdev->evbit); |
2534 | set_bit(EV_MSC, tpacpi_inputdev->evbit); | |
2535 | set_bit(MSC_SCAN, tpacpi_inputdev->mscbit); | |
2536 | tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE; | |
2537 | tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN; | |
2538 | tpacpi_inputdev->keycode = hotkey_keycode_map; | |
2539 | for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) { | |
2540 | if (hotkey_keycode_map[i] != KEY_RESERVED) { | |
2541 | set_bit(hotkey_keycode_map[i], | |
2542 | tpacpi_inputdev->keybit); | |
2543 | } else { | |
2544 | if (i < sizeof(hotkey_reserved_mask)*8) | |
2545 | hotkey_reserved_mask |= 1 << i; | |
6a38abbf | 2546 | } |
9c0a76e1 | 2547 | } |
6a38abbf | 2548 | |
9c0a76e1 HMH |
2549 | if (tp_features.hotkey_wlsw) { |
2550 | set_bit(EV_SW, tpacpi_inputdev->evbit); | |
2551 | set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit); | |
2552 | } | |
2553 | if (tp_features.hotkey_tablet) { | |
2554 | set_bit(EV_SW, tpacpi_inputdev->evbit); | |
2555 | set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit); | |
2556 | } | |
5c29d58f | 2557 | |
9c0a76e1 HMH |
2558 | /* Do not issue duplicate brightness change events to |
2559 | * userspace */ | |
2560 | if (!tp_features.bright_acpimode) | |
2561 | /* update bright_acpimode... */ | |
2562 | tpacpi_check_std_acpi_brightness_support(); | |
b5972796 | 2563 | |
9c0a76e1 HMH |
2564 | if (tp_features.bright_acpimode) { |
2565 | printk(TPACPI_INFO | |
2566 | "This ThinkPad has standard ACPI backlight " | |
2567 | "brightness control, supported by the ACPI " | |
2568 | "video driver\n"); | |
2569 | printk(TPACPI_NOTICE | |
2570 | "Disabling thinkpad-acpi brightness events " | |
2571 | "by default...\n"); | |
2572 | ||
2573 | /* The hotkey_reserved_mask change below is not | |
2574 | * necessary while the keys are at KEY_RESERVED in the | |
2575 | * default map, but better safe than sorry, leave it | |
2576 | * here as a marker of what we have to do, especially | |
2577 | * when we finally become able to set this at runtime | |
2578 | * on response to X.org requests */ | |
2579 | hotkey_reserved_mask |= | |
2580 | (1 << TP_ACPI_HOTKEYSCAN_FNHOME) | |
2581 | | (1 << TP_ACPI_HOTKEYSCAN_FNEND); | |
2582 | } | |
ff80f137 | 2583 | |
9c0a76e1 | 2584 | dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n"); |
2586d566 | 2585 | res = hotkey_status_set(true); |
9c0a76e1 HMH |
2586 | if (res) { |
2587 | hotkey_exit(); | |
2588 | return res; | |
2589 | } | |
2590 | res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask) | |
2591 | & ~hotkey_reserved_mask) | |
2592 | | hotkey_orig_mask); | |
2593 | if (res < 0 && res != -ENXIO) { | |
2594 | hotkey_exit(); | |
2595 | return res; | |
2596 | } | |
01e88f25 | 2597 | |
9c0a76e1 HMH |
2598 | dbg_printk(TPACPI_DBG_INIT, |
2599 | "legacy hot key reporting over procfs %s\n", | |
2600 | (hotkey_report_mode < 2) ? | |
2601 | "enabled" : "disabled"); | |
01e88f25 | 2602 | |
9c0a76e1 HMH |
2603 | tpacpi_inputdev->open = &hotkey_inputdev_open; |
2604 | tpacpi_inputdev->close = &hotkey_inputdev_close; | |
56b6aeb0 | 2605 | |
9c0a76e1 | 2606 | hotkey_poll_setup_safe(1); |
733e27c1 | 2607 | tpacpi_send_radiosw_update(); |
9c0a76e1 | 2608 | tpacpi_input_send_tabletsw(); |
56b6aeb0 | 2609 | |
9c0a76e1 | 2610 | return 0; |
01e88f25 | 2611 | |
9c0a76e1 HMH |
2612 | err_exit: |
2613 | delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj); | |
2614 | hotkey_dev_attributes = NULL; | |
a0416420 | 2615 | |
9c0a76e1 | 2616 | return (res < 0)? res : 1; |
56b6aeb0 HMH |
2617 | } |
2618 | ||
3827e7a3 HMH |
2619 | static bool hotkey_notify_hotkey(const u32 hkey, |
2620 | bool *send_acpi_ev, | |
2621 | bool *ignore_acpi_ev) | |
2622 | { | |
2623 | /* 0x1000-0x1FFF: key presses */ | |
2624 | unsigned int scancode = hkey & 0xfff; | |
2625 | *send_acpi_ev = true; | |
2626 | *ignore_acpi_ev = false; | |
2627 | ||
2628 | if (scancode > 0 && scancode < 0x21) { | |
2629 | scancode--; | |
2630 | if (!(hotkey_source_mask & (1 << scancode))) { | |
2631 | tpacpi_input_send_key(scancode); | |
2632 | *send_acpi_ev = false; | |
2633 | } else { | |
2634 | *ignore_acpi_ev = true; | |
2635 | } | |
2636 | return true; | |
2637 | } | |
2638 | return false; | |
2639 | } | |
2640 | ||
2641 | static bool hotkey_notify_wakeup(const u32 hkey, | |
2642 | bool *send_acpi_ev, | |
2643 | bool *ignore_acpi_ev) | |
2644 | { | |
2645 | /* 0x2000-0x2FFF: Wakeup reason */ | |
2646 | *send_acpi_ev = true; | |
2647 | *ignore_acpi_ev = false; | |
2648 | ||
2649 | switch (hkey) { | |
2650 | case 0x2304: /* suspend, undock */ | |
2651 | case 0x2404: /* hibernation, undock */ | |
2652 | hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK; | |
2653 | *ignore_acpi_ev = true; | |
2654 | break; | |
2655 | ||
2656 | case 0x2305: /* suspend, bay eject */ | |
2657 | case 0x2405: /* hibernation, bay eject */ | |
2658 | hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ; | |
2659 | *ignore_acpi_ev = true; | |
2660 | break; | |
2661 | ||
106b4e66 HMH |
2662 | case 0x2313: /* Battery on critical low level (S3) */ |
2663 | case 0x2413: /* Battery on critical low level (S4) */ | |
2664 | printk(TPACPI_ALERT | |
2665 | "EMERGENCY WAKEUP: battery almost empty\n"); | |
2666 | /* how to auto-heal: */ | |
2667 | /* 2313: woke up from S3, go to S4/S5 */ | |
2668 | /* 2413: woke up from S4, go to S5 */ | |
2669 | break; | |
2670 | ||
3827e7a3 HMH |
2671 | default: |
2672 | return false; | |
2673 | } | |
2674 | ||
2675 | if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) { | |
2676 | printk(TPACPI_INFO | |
2677 | "woke up due to a hot-unplug " | |
2678 | "request...\n"); | |
2679 | hotkey_wakeup_reason_notify_change(); | |
2680 | } | |
2681 | return true; | |
2682 | } | |
2683 | ||
2684 | static bool hotkey_notify_usrevent(const u32 hkey, | |
2685 | bool *send_acpi_ev, | |
2686 | bool *ignore_acpi_ev) | |
2687 | { | |
2688 | /* 0x5000-0x5FFF: human interface helpers */ | |
2689 | *send_acpi_ev = true; | |
2690 | *ignore_acpi_ev = false; | |
2691 | ||
2692 | switch (hkey) { | |
2693 | case 0x5010: /* Lenovo new BIOS: brightness changed */ | |
2694 | case 0x500b: /* X61t: tablet pen inserted into bay */ | |
2695 | case 0x500c: /* X61t: tablet pen removed from bay */ | |
2696 | return true; | |
2697 | ||
2698 | case 0x5009: /* X41t-X61t: swivel up (tablet mode) */ | |
2699 | case 0x500a: /* X41t-X61t: swivel down (normal mode) */ | |
2700 | tpacpi_input_send_tabletsw(); | |
2701 | hotkey_tablet_mode_notify_change(); | |
2702 | *send_acpi_ev = false; | |
2703 | return true; | |
2704 | ||
2705 | case 0x5001: | |
2706 | case 0x5002: | |
2707 | /* LID switch events. Do not propagate */ | |
2708 | *ignore_acpi_ev = true; | |
2709 | return true; | |
2710 | ||
2711 | default: | |
2712 | return false; | |
2713 | } | |
2714 | } | |
2715 | ||
106b4e66 HMH |
2716 | static bool hotkey_notify_thermal(const u32 hkey, |
2717 | bool *send_acpi_ev, | |
2718 | bool *ignore_acpi_ev) | |
2719 | { | |
2720 | /* 0x6000-0x6FFF: thermal alarms */ | |
2721 | *send_acpi_ev = true; | |
2722 | *ignore_acpi_ev = false; | |
2723 | ||
2724 | switch (hkey) { | |
2725 | case 0x6011: | |
2726 | printk(TPACPI_CRIT | |
2727 | "THERMAL ALARM: battery is too hot!\n"); | |
2728 | /* recommended action: warn user through gui */ | |
2729 | return true; | |
2730 | case 0x6012: | |
2731 | printk(TPACPI_ALERT | |
2732 | "THERMAL EMERGENCY: battery is extremely hot!\n"); | |
2733 | /* recommended action: immediate sleep/hibernate */ | |
2734 | return true; | |
2735 | case 0x6021: | |
2736 | printk(TPACPI_CRIT | |
2737 | "THERMAL ALARM: " | |
2738 | "a sensor reports something is too hot!\n"); | |
2739 | /* recommended action: warn user through gui, that */ | |
2740 | /* some internal component is too hot */ | |
2741 | return true; | |
2742 | case 0x6022: | |
2743 | printk(TPACPI_ALERT | |
2744 | "THERMAL EMERGENCY: " | |
2745 | "a sensor reports something is extremely hot!\n"); | |
2746 | /* recommended action: immediate sleep/hibernate */ | |
2747 | return true; | |
54926ce8 HMH |
2748 | case 0x6030: |
2749 | printk(TPACPI_INFO | |
2750 | "EC reports that Thermal Table has changed\n"); | |
2751 | /* recommended action: do nothing, we don't have | |
2752 | * Lenovo ATM information */ | |
2753 | return true; | |
106b4e66 HMH |
2754 | default: |
2755 | printk(TPACPI_ALERT | |
2756 | "THERMAL ALERT: unknown thermal alarm received\n"); | |
2757 | return false; | |
2758 | } | |
2759 | } | |
2760 | ||
56b6aeb0 HMH |
2761 | static void hotkey_notify(struct ibm_struct *ibm, u32 event) |
2762 | { | |
6a38abbf | 2763 | u32 hkey; |
3827e7a3 HMH |
2764 | bool send_acpi_ev; |
2765 | bool ignore_acpi_ev; | |
2766 | bool known_ev; | |
3eea123d HMH |
2767 | |
2768 | if (event != 0x80) { | |
35ff8b9f HMH |
2769 | printk(TPACPI_ERR |
2770 | "unknown HKEY notification event %d\n", event); | |
3eea123d | 2771 | /* forward it to userspace, maybe it knows how to handle it */ |
35ff8b9f HMH |
2772 | acpi_bus_generate_netlink_event( |
2773 | ibm->acpi->device->pnp.device_class, | |
e0b36fc5 | 2774 | dev_name(&ibm->acpi->device->dev), |
35ff8b9f | 2775 | event, 0); |
3eea123d HMH |
2776 | return; |
2777 | } | |
2778 | ||
2779 | while (1) { | |
2780 | if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) { | |
e0c7dfe7 | 2781 | printk(TPACPI_ERR "failed to retrieve HKEY event\n"); |
3eea123d HMH |
2782 | return; |
2783 | } | |
2784 | ||
2785 | if (hkey == 0) { | |
2786 | /* queue empty */ | |
2787 | return; | |
2788 | } | |
2789 | ||
3827e7a3 HMH |
2790 | send_acpi_ev = true; |
2791 | ignore_acpi_ev = false; | |
56b6aeb0 | 2792 | |
ff80f137 HMH |
2793 | switch (hkey >> 12) { |
2794 | case 1: | |
2795 | /* 0x1000-0x1FFF: key presses */ | |
3827e7a3 HMH |
2796 | known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev, |
2797 | &ignore_acpi_ev); | |
ff80f137 | 2798 | break; |
a713b4d7 | 2799 | case 2: |
3827e7a3 HMH |
2800 | /* 0x2000-0x2FFF: Wakeup reason */ |
2801 | known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev, | |
2802 | &ignore_acpi_ev); | |
a713b4d7 HMH |
2803 | break; |
2804 | case 3: | |
3827e7a3 | 2805 | /* 0x3000-0x3FFF: bay-related wakeups */ |
a713b4d7 HMH |
2806 | if (hkey == 0x3003) { |
2807 | hotkey_autosleep_ack = 1; | |
2808 | printk(TPACPI_INFO | |
2809 | "bay ejected\n"); | |
50ebec09 | 2810 | hotkey_wakeup_hotunplug_complete_notify_change(); |
3827e7a3 | 2811 | known_ev = true; |
a713b4d7 | 2812 | } else { |
3827e7a3 | 2813 | known_ev = false; |
a713b4d7 HMH |
2814 | } |
2815 | break; | |
2816 | case 4: | |
3827e7a3 | 2817 | /* 0x4000-0x4FFF: dock-related wakeups */ |
a713b4d7 HMH |
2818 | if (hkey == 0x4003) { |
2819 | hotkey_autosleep_ack = 1; | |
2820 | printk(TPACPI_INFO | |
2821 | "undocked\n"); | |
50ebec09 | 2822 | hotkey_wakeup_hotunplug_complete_notify_change(); |
3827e7a3 | 2823 | known_ev = true; |
a713b4d7 | 2824 | } else { |
3827e7a3 | 2825 | known_ev = false; |
a713b4d7 HMH |
2826 | } |
2827 | break; | |
ff80f137 | 2828 | case 5: |
d1edb2b5 | 2829 | /* 0x5000-0x5FFF: human interface helpers */ |
3827e7a3 HMH |
2830 | known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev, |
2831 | &ignore_acpi_ev); | |
ff80f137 | 2832 | break; |
106b4e66 HMH |
2833 | case 6: |
2834 | /* 0x6000-0x6FFF: thermal alarms */ | |
2835 | known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev, | |
2836 | &ignore_acpi_ev); | |
2837 | break; | |
ff80f137 HMH |
2838 | case 7: |
2839 | /* 0x7000-0x7FFF: misc */ | |
2840 | if (tp_features.hotkey_wlsw && hkey == 0x7000) { | |
733e27c1 | 2841 | tpacpi_send_radiosw_update(); |
3b64b51d | 2842 | send_acpi_ev = 0; |
3827e7a3 | 2843 | known_ev = true; |
6a38abbf | 2844 | break; |
6a38abbf | 2845 | } |
ff80f137 HMH |
2846 | /* fallthrough to default */ |
2847 | default: | |
3827e7a3 | 2848 | known_ev = false; |
3b64b51d | 2849 | } |
3827e7a3 | 2850 | if (!known_ev) { |
35ff8b9f HMH |
2851 | printk(TPACPI_NOTICE |
2852 | "unhandled HKEY event 0x%04x\n", hkey); | |
cb429358 HMH |
2853 | printk(TPACPI_NOTICE |
2854 | "please report the conditions when this " | |
2855 | "event happened to %s\n", TPACPI_MAIL); | |
6a38abbf | 2856 | } |
ff80f137 | 2857 | |
3eea123d | 2858 | /* Legacy events */ |
35ff8b9f HMH |
2859 | if (!ignore_acpi_ev && |
2860 | (send_acpi_ev || hotkey_report_mode < 2)) { | |
2861 | acpi_bus_generate_proc_event(ibm->acpi->device, | |
2862 | event, hkey); | |
3e5ce914 | 2863 | } |
ff80f137 | 2864 | |
3eea123d | 2865 | /* netlink events */ |
3e5ce914 | 2866 | if (!ignore_acpi_ev && send_acpi_ev) { |
35ff8b9f HMH |
2867 | acpi_bus_generate_netlink_event( |
2868 | ibm->acpi->device->pnp.device_class, | |
e0b36fc5 | 2869 | dev_name(&ibm->acpi->device->dev), |
35ff8b9f | 2870 | event, hkey); |
3eea123d | 2871 | } |
56b6aeb0 HMH |
2872 | } |
2873 | } | |
2874 | ||
a713b4d7 HMH |
2875 | static void hotkey_suspend(pm_message_t state) |
2876 | { | |
2877 | /* Do these on suspend, we get the events on early resume! */ | |
2878 | hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE; | |
2879 | hotkey_autosleep_ack = 0; | |
2880 | } | |
2881 | ||
5c29d58f HMH |
2882 | static void hotkey_resume(void) |
2883 | { | |
d64c81c4 HMH |
2884 | tpacpi_disable_brightness_delay(); |
2885 | ||
b2c985e7 | 2886 | if (hotkey_mask_get()) |
35ff8b9f HMH |
2887 | printk(TPACPI_ERR |
2888 | "error while trying to read hot key mask " | |
2889 | "from firmware\n"); | |
733e27c1 | 2890 | tpacpi_send_radiosw_update(); |
6c231bd5 | 2891 | hotkey_tablet_mode_notify_change(); |
50ebec09 HMH |
2892 | hotkey_wakeup_reason_notify_change(); |
2893 | hotkey_wakeup_hotunplug_complete_notify_change(); | |
01e88f25 | 2894 | hotkey_poll_setup_safe(0); |
5c29d58f HMH |
2895 | } |
2896 | ||
a0416420 | 2897 | /* procfs -------------------------------------------------------------- */ |
78f81cc4 | 2898 | static int hotkey_read(char *p) |
1da177e4 | 2899 | { |
ae92bd17 | 2900 | int res, status; |
1da177e4 LT |
2901 | int len = 0; |
2902 | ||
d8fd94d9 | 2903 | if (!tp_features.hotkey) { |
78f81cc4 BD |
2904 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
2905 | return len; | |
2906 | } | |
2907 | ||
7646ea88 | 2908 | if (mutex_lock_killable(&hotkey_mutex)) |
fc589a3c | 2909 | return -ERESTARTSYS; |
b2c985e7 HMH |
2910 | res = hotkey_status_get(&status); |
2911 | if (!res) | |
2912 | res = hotkey_mask_get(); | |
40ca9fdf | 2913 | mutex_unlock(&hotkey_mutex); |
b86c4722 HMH |
2914 | if (res) |
2915 | return res; | |
1da177e4 LT |
2916 | |
2917 | len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0)); | |
d8fd94d9 | 2918 | if (tp_features.hotkey_mask) { |
b2c985e7 | 2919 | len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask); |
1da177e4 LT |
2920 | len += sprintf(p + len, |
2921 | "commands:\tenable, disable, reset, <mask>\n"); | |
2922 | } else { | |
2923 | len += sprintf(p + len, "mask:\t\tnot supported\n"); | |
2924 | len += sprintf(p + len, "commands:\tenable, disable, reset\n"); | |
2925 | } | |
2926 | ||
2927 | return len; | |
2928 | } | |
2929 | ||
2586d566 HMH |
2930 | static void hotkey_enabledisable_warn(void) |
2931 | { | |
2932 | tpacpi_log_usertask("procfs hotkey enable/disable"); | |
2933 | WARN(1, TPACPI_WARN | |
2934 | "hotkey enable/disable functionality has been " | |
2935 | "removed from the driver. Hotkeys are always enabled.\n"); | |
2936 | } | |
2937 | ||
78f81cc4 | 2938 | static int hotkey_write(char *buf) |
1da177e4 | 2939 | { |
2586d566 | 2940 | int res; |
ae92bd17 | 2941 | u32 mask; |
1da177e4 | 2942 | char *cmd; |
1da177e4 | 2943 | |
d8fd94d9 | 2944 | if (!tp_features.hotkey) |
1da177e4 LT |
2945 | return -ENODEV; |
2946 | ||
7646ea88 | 2947 | if (mutex_lock_killable(&hotkey_mutex)) |
fc589a3c | 2948 | return -ERESTARTSYS; |
40ca9fdf | 2949 | |
b2c985e7 | 2950 | mask = hotkey_mask; |
78f81cc4 | 2951 | |
40ca9fdf | 2952 | res = 0; |
1da177e4 LT |
2953 | while ((cmd = next_cmd(&buf))) { |
2954 | if (strlencmp(cmd, "enable") == 0) { | |
2586d566 | 2955 | hotkey_enabledisable_warn(); |
1da177e4 | 2956 | } else if (strlencmp(cmd, "disable") == 0) { |
2586d566 HMH |
2957 | hotkey_enabledisable_warn(); |
2958 | res = -EPERM; | |
1da177e4 | 2959 | } else if (strlencmp(cmd, "reset") == 0) { |
78f81cc4 | 2960 | mask = hotkey_orig_mask; |
1da177e4 LT |
2961 | } else if (sscanf(cmd, "0x%x", &mask) == 1) { |
2962 | /* mask set */ | |
2963 | } else if (sscanf(cmd, "%x", &mask) == 1) { | |
2964 | /* mask set */ | |
40ca9fdf HMH |
2965 | } else { |
2966 | res = -EINVAL; | |
2967 | goto errexit; | |
2968 | } | |
1da177e4 | 2969 | } |
b2c985e7 HMH |
2970 | if (!res && mask != hotkey_mask) |
2971 | res = hotkey_mask_set(mask); | |
1da177e4 | 2972 | |
40ca9fdf HMH |
2973 | errexit: |
2974 | mutex_unlock(&hotkey_mutex); | |
2975 | return res; | |
78f81cc4 | 2976 | } |
1da177e4 | 2977 | |
1ba90e3a | 2978 | static const struct acpi_device_id ibm_htk_device_ids[] = { |
e0c7dfe7 | 2979 | {TPACPI_ACPI_HKEY_HID, 0}, |
1ba90e3a TR |
2980 | {"", 0}, |
2981 | }; | |
2982 | ||
8d376cd6 | 2983 | static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = { |
1ba90e3a | 2984 | .hid = ibm_htk_device_ids, |
8d376cd6 HMH |
2985 | .notify = hotkey_notify, |
2986 | .handle = &hkey_handle, | |
2987 | .type = ACPI_DEVICE_NOTIFY, | |
2988 | }; | |
2989 | ||
a5763f22 HMH |
2990 | static struct ibm_struct hotkey_driver_data = { |
2991 | .name = "hotkey", | |
a5763f22 HMH |
2992 | .read = hotkey_read, |
2993 | .write = hotkey_write, | |
2994 | .exit = hotkey_exit, | |
5c29d58f | 2995 | .resume = hotkey_resume, |
a713b4d7 | 2996 | .suspend = hotkey_suspend, |
8d376cd6 | 2997 | .acpi = &ibm_hotkey_acpidriver, |
a5763f22 HMH |
2998 | }; |
2999 | ||
56b6aeb0 HMH |
3000 | /************************************************************************* |
3001 | * Bluetooth subdriver | |
3002 | */ | |
1da177e4 | 3003 | |
f74a27d4 HMH |
3004 | enum { |
3005 | /* ACPI GBDC/SBDC bits */ | |
3006 | TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */ | |
3007 | TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */ | |
153f8220 HMH |
3008 | TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume: |
3009 | off / last state */ | |
3010 | }; | |
3011 | ||
3012 | enum { | |
3013 | /* ACPI \BLTH commands */ | |
3014 | TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */ | |
3015 | TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */ | |
3016 | TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */ | |
3017 | TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */ | |
3018 | TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */ | |
f74a27d4 HMH |
3019 | }; |
3020 | ||
0e74dc26 HMH |
3021 | static struct rfkill *tpacpi_bluetooth_rfkill; |
3022 | ||
153f8220 HMH |
3023 | static void bluetooth_suspend(pm_message_t state) |
3024 | { | |
3025 | /* Try to make sure radio will resume powered off */ | |
3026 | acpi_evalf(NULL, NULL, "\\BLTH", "vd", | |
3027 | TP_ACPI_BLTH_PWR_OFF_ON_RESUME); | |
3028 | } | |
3029 | ||
07431ec8 HMH |
3030 | static int bluetooth_get_radiosw(void) |
3031 | { | |
3032 | int status; | |
3033 | ||
3034 | if (!tp_features.bluetooth) | |
3035 | return -ENODEV; | |
3036 | ||
133ec3bd HMH |
3037 | /* WLSW overrides bluetooth in firmware/hardware, reflect that */ |
3038 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status) | |
0e74dc26 | 3039 | return RFKILL_STATE_HARD_BLOCKED; |
133ec3bd | 3040 | |
a73f3091 HMH |
3041 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
3042 | if (dbg_bluetoothemul) | |
3043 | return (tpacpi_bluetooth_emulstate) ? | |
3044 | RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; | |
3045 | #endif | |
3046 | ||
07431ec8 HMH |
3047 | if (!acpi_evalf(hkey_handle, &status, "GBDC", "d")) |
3048 | return -EIO; | |
3049 | ||
0e74dc26 HMH |
3050 | return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ? |
3051 | RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; | |
07431ec8 HMH |
3052 | } |
3053 | ||
0e74dc26 HMH |
3054 | static void bluetooth_update_rfk(void) |
3055 | { | |
3056 | int status; | |
3057 | ||
3058 | if (!tpacpi_bluetooth_rfkill) | |
3059 | return; | |
3060 | ||
3061 | status = bluetooth_get_radiosw(); | |
3062 | if (status < 0) | |
3063 | return; | |
3064 | rfkill_force_state(tpacpi_bluetooth_rfkill, status); | |
3065 | } | |
3066 | ||
3067 | static int bluetooth_set_radiosw(int radio_on, int update_rfk) | |
07431ec8 HMH |
3068 | { |
3069 | int status; | |
3070 | ||
3071 | if (!tp_features.bluetooth) | |
3072 | return -ENODEV; | |
3073 | ||
133ec3bd HMH |
3074 | /* WLSW overrides bluetooth in firmware/hardware, but there is no |
3075 | * reason to risk weird behaviour. */ | |
3076 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status | |
3077 | && radio_on) | |
3078 | return -EPERM; | |
3079 | ||
a73f3091 HMH |
3080 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
3081 | if (dbg_bluetoothemul) { | |
3082 | tpacpi_bluetooth_emulstate = !!radio_on; | |
3083 | if (update_rfk) | |
3084 | bluetooth_update_rfk(); | |
3085 | return 0; | |
3086 | } | |
3087 | #endif | |
3088 | ||
153f8220 | 3089 | /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */ |
07431ec8 | 3090 | if (radio_on) |
153f8220 | 3091 | status = TP_ACPI_BLUETOOTH_RADIOSSW; |
07431ec8 | 3092 | else |
153f8220 | 3093 | status = 0; |
07431ec8 HMH |
3094 | if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status)) |
3095 | return -EIO; | |
3096 | ||
0e74dc26 HMH |
3097 | if (update_rfk) |
3098 | bluetooth_update_rfk(); | |
3099 | ||
07431ec8 HMH |
3100 | return 0; |
3101 | } | |
f74a27d4 | 3102 | |
d3a6ade4 HMH |
3103 | /* sysfs bluetooth enable ---------------------------------------------- */ |
3104 | static ssize_t bluetooth_enable_show(struct device *dev, | |
3105 | struct device_attribute *attr, | |
3106 | char *buf) | |
3107 | { | |
3108 | int status; | |
3109 | ||
73a94d86 HMH |
3110 | printk_deprecated_rfkill_attribute("bluetooth_enable"); |
3111 | ||
d3a6ade4 HMH |
3112 | status = bluetooth_get_radiosw(); |
3113 | if (status < 0) | |
3114 | return status; | |
3115 | ||
0e74dc26 HMH |
3116 | return snprintf(buf, PAGE_SIZE, "%d\n", |
3117 | (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0); | |
d3a6ade4 HMH |
3118 | } |
3119 | ||
3120 | static ssize_t bluetooth_enable_store(struct device *dev, | |
3121 | struct device_attribute *attr, | |
3122 | const char *buf, size_t count) | |
3123 | { | |
3124 | unsigned long t; | |
3125 | int res; | |
3126 | ||
73a94d86 HMH |
3127 | printk_deprecated_rfkill_attribute("bluetooth_enable"); |
3128 | ||
d3a6ade4 HMH |
3129 | if (parse_strtoul(buf, 1, &t)) |
3130 | return -EINVAL; | |
3131 | ||
0e74dc26 | 3132 | res = bluetooth_set_radiosw(t, 1); |
d3a6ade4 HMH |
3133 | |
3134 | return (res) ? res : count; | |
3135 | } | |
3136 | ||
3137 | static struct device_attribute dev_attr_bluetooth_enable = | |
cc4c24e1 | 3138 | __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO, |
d3a6ade4 HMH |
3139 | bluetooth_enable_show, bluetooth_enable_store); |
3140 | ||
3141 | /* --------------------------------------------------------------------- */ | |
3142 | ||
3143 | static struct attribute *bluetooth_attributes[] = { | |
3144 | &dev_attr_bluetooth_enable.attr, | |
3145 | NULL | |
3146 | }; | |
3147 | ||
3148 | static const struct attribute_group bluetooth_attr_group = { | |
d3a6ade4 HMH |
3149 | .attrs = bluetooth_attributes, |
3150 | }; | |
3151 | ||
0e74dc26 HMH |
3152 | static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state) |
3153 | { | |
3154 | int bts = bluetooth_get_radiosw(); | |
3155 | ||
3156 | if (bts < 0) | |
3157 | return bts; | |
3158 | ||
3159 | *state = bts; | |
3160 | return 0; | |
3161 | } | |
3162 | ||
3163 | static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state) | |
3164 | { | |
3165 | return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0); | |
3166 | } | |
3167 | ||
90d9d3c7 HMH |
3168 | static void bluetooth_shutdown(void) |
3169 | { | |
3170 | /* Order firmware to save current state to NVRAM */ | |
3171 | if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd", | |
3172 | TP_ACPI_BLTH_SAVE_STATE)) | |
3173 | printk(TPACPI_NOTICE | |
3174 | "failed to save bluetooth state to NVRAM\n"); | |
3175 | } | |
3176 | ||
07431ec8 HMH |
3177 | static void bluetooth_exit(void) |
3178 | { | |
90d9d3c7 HMH |
3179 | bluetooth_shutdown(); |
3180 | ||
0e74dc26 HMH |
3181 | if (tpacpi_bluetooth_rfkill) |
3182 | rfkill_unregister(tpacpi_bluetooth_rfkill); | |
3183 | ||
07431ec8 HMH |
3184 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, |
3185 | &bluetooth_attr_group); | |
3186 | } | |
3187 | ||
a5763f22 | 3188 | static int __init bluetooth_init(struct ibm_init_struct *iibm) |
1da177e4 | 3189 | { |
d3a6ade4 | 3190 | int res; |
d6fdd1e9 HMH |
3191 | int status = 0; |
3192 | ||
fe08bc4b HMH |
3193 | vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n"); |
3194 | ||
e0c7dfe7 | 3195 | TPACPI_ACPIHANDLE_INIT(hkey); |
5fba344c | 3196 | |
78f81cc4 BD |
3197 | /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
3198 | G4x, R30, R31, R40e, R50e, T20-22, X20-21 */ | |
d8fd94d9 | 3199 | tp_features.bluetooth = hkey_handle && |
d6fdd1e9 HMH |
3200 | acpi_evalf(hkey_handle, &status, "GBDC", "qd"); |
3201 | ||
3202 | vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n", | |
3203 | str_supported(tp_features.bluetooth), | |
3204 | status); | |
3205 | ||
a73f3091 HMH |
3206 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
3207 | if (dbg_bluetoothemul) { | |
3208 | tp_features.bluetooth = 1; | |
3209 | printk(TPACPI_INFO | |
3210 | "bluetooth switch emulation enabled\n"); | |
3211 | } else | |
3212 | #endif | |
3a872080 HMH |
3213 | if (tp_features.bluetooth && |
3214 | !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) { | |
3215 | /* no bluetooth hardware present in system */ | |
3216 | tp_features.bluetooth = 0; | |
3217 | dbg_printk(TPACPI_DBG_INIT, | |
3218 | "bluetooth hardware not installed\n"); | |
3219 | } | |
3220 | ||
0e74dc26 HMH |
3221 | if (!tp_features.bluetooth) |
3222 | return 1; | |
3223 | ||
3224 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
3a872080 | 3225 | &bluetooth_attr_group); |
0e74dc26 HMH |
3226 | if (res) |
3227 | return res; | |
3228 | ||
3229 | res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID, | |
3230 | &tpacpi_bluetooth_rfkill, | |
3231 | RFKILL_TYPE_BLUETOOTH, | |
3232 | "tpacpi_bluetooth_sw", | |
0045c0aa | 3233 | true, |
0e74dc26 HMH |
3234 | tpacpi_bluetooth_rfk_set, |
3235 | tpacpi_bluetooth_rfk_get); | |
3236 | if (res) { | |
3237 | bluetooth_exit(); | |
3238 | return res; | |
d6fdd1e9 | 3239 | } |
fe08bc4b | 3240 | |
0e74dc26 | 3241 | return 0; |
1da177e4 LT |
3242 | } |
3243 | ||
d3a6ade4 | 3244 | /* procfs -------------------------------------------------------------- */ |
78f81cc4 | 3245 | static int bluetooth_read(char *p) |
1da177e4 LT |
3246 | { |
3247 | int len = 0; | |
d6fdd1e9 | 3248 | int status = bluetooth_get_radiosw(); |
1da177e4 | 3249 | |
d8fd94d9 | 3250 | if (!tp_features.bluetooth) |
1da177e4 | 3251 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
1da177e4 | 3252 | else { |
d6fdd1e9 | 3253 | len += sprintf(p + len, "status:\t\t%s\n", |
0e74dc26 HMH |
3254 | (status == RFKILL_STATE_UNBLOCKED) ? |
3255 | "enabled" : "disabled"); | |
1da177e4 LT |
3256 | len += sprintf(p + len, "commands:\tenable, disable\n"); |
3257 | } | |
3258 | ||
3259 | return len; | |
3260 | } | |
3261 | ||
78f81cc4 | 3262 | static int bluetooth_write(char *buf) |
1da177e4 | 3263 | { |
1da177e4 | 3264 | char *cmd; |
1da177e4 | 3265 | |
d8fd94d9 | 3266 | if (!tp_features.bluetooth) |
78f81cc4 | 3267 | return -ENODEV; |
1da177e4 LT |
3268 | |
3269 | while ((cmd = next_cmd(&buf))) { | |
3270 | if (strlencmp(cmd, "enable") == 0) { | |
0e74dc26 | 3271 | bluetooth_set_radiosw(1, 1); |
1da177e4 | 3272 | } else if (strlencmp(cmd, "disable") == 0) { |
0e74dc26 | 3273 | bluetooth_set_radiosw(0, 1); |
1da177e4 LT |
3274 | } else |
3275 | return -EINVAL; | |
1da177e4 LT |
3276 | } |
3277 | ||
1da177e4 LT |
3278 | return 0; |
3279 | } | |
3280 | ||
a5763f22 HMH |
3281 | static struct ibm_struct bluetooth_driver_data = { |
3282 | .name = "bluetooth", | |
3283 | .read = bluetooth_read, | |
3284 | .write = bluetooth_write, | |
d3a6ade4 | 3285 | .exit = bluetooth_exit, |
153f8220 | 3286 | .suspend = bluetooth_suspend, |
90d9d3c7 | 3287 | .shutdown = bluetooth_shutdown, |
a5763f22 HMH |
3288 | }; |
3289 | ||
56b6aeb0 HMH |
3290 | /************************************************************************* |
3291 | * Wan subdriver | |
3292 | */ | |
3293 | ||
f74a27d4 HMH |
3294 | enum { |
3295 | /* ACPI GWAN/SWAN bits */ | |
3296 | TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */ | |
3297 | TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */ | |
153f8220 HMH |
3298 | TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume: |
3299 | off / last state */ | |
f74a27d4 HMH |
3300 | }; |
3301 | ||
0e74dc26 HMH |
3302 | static struct rfkill *tpacpi_wan_rfkill; |
3303 | ||
153f8220 HMH |
3304 | static void wan_suspend(pm_message_t state) |
3305 | { | |
3306 | /* Try to make sure radio will resume powered off */ | |
3307 | acpi_evalf(NULL, NULL, "\\WGSV", "qvd", | |
3308 | TP_ACPI_WGSV_PWR_OFF_ON_RESUME); | |
3309 | } | |
3310 | ||
07431ec8 HMH |
3311 | static int wan_get_radiosw(void) |
3312 | { | |
3313 | int status; | |
3314 | ||
3315 | if (!tp_features.wan) | |
3316 | return -ENODEV; | |
3317 | ||
133ec3bd HMH |
3318 | /* WLSW overrides WWAN in firmware/hardware, reflect that */ |
3319 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status) | |
0e74dc26 | 3320 | return RFKILL_STATE_HARD_BLOCKED; |
133ec3bd | 3321 | |
a73f3091 HMH |
3322 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
3323 | if (dbg_wwanemul) | |
3324 | return (tpacpi_wwan_emulstate) ? | |
3325 | RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; | |
3326 | #endif | |
3327 | ||
07431ec8 HMH |
3328 | if (!acpi_evalf(hkey_handle, &status, "GWAN", "d")) |
3329 | return -EIO; | |
3330 | ||
0e74dc26 HMH |
3331 | return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ? |
3332 | RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; | |
07431ec8 HMH |
3333 | } |
3334 | ||
0e74dc26 HMH |
3335 | static void wan_update_rfk(void) |
3336 | { | |
3337 | int status; | |
3338 | ||
3339 | if (!tpacpi_wan_rfkill) | |
3340 | return; | |
3341 | ||
3342 | status = wan_get_radiosw(); | |
3343 | if (status < 0) | |
3344 | return; | |
3345 | rfkill_force_state(tpacpi_wan_rfkill, status); | |
3346 | } | |
3347 | ||
3348 | static int wan_set_radiosw(int radio_on, int update_rfk) | |
07431ec8 HMH |
3349 | { |
3350 | int status; | |
3351 | ||
3352 | if (!tp_features.wan) | |
3353 | return -ENODEV; | |
3354 | ||
133ec3bd HMH |
3355 | /* WLSW overrides bluetooth in firmware/hardware, but there is no |
3356 | * reason to risk weird behaviour. */ | |
3357 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status | |
3358 | && radio_on) | |
3359 | return -EPERM; | |
3360 | ||
a73f3091 HMH |
3361 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
3362 | if (dbg_wwanemul) { | |
3363 | tpacpi_wwan_emulstate = !!radio_on; | |
3364 | if (update_rfk) | |
3365 | wan_update_rfk(); | |
3366 | return 0; | |
3367 | } | |
3368 | #endif | |
3369 | ||
153f8220 | 3370 | /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */ |
07431ec8 | 3371 | if (radio_on) |
153f8220 | 3372 | status = TP_ACPI_WANCARD_RADIOSSW; |
07431ec8 | 3373 | else |
153f8220 | 3374 | status = 0; |
07431ec8 HMH |
3375 | if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status)) |
3376 | return -EIO; | |
3377 | ||
0e74dc26 HMH |
3378 | if (update_rfk) |
3379 | wan_update_rfk(); | |
3380 | ||
07431ec8 HMH |
3381 | return 0; |
3382 | } | |
f74a27d4 | 3383 | |
d3a6ade4 HMH |
3384 | /* sysfs wan enable ---------------------------------------------------- */ |
3385 | static ssize_t wan_enable_show(struct device *dev, | |
3386 | struct device_attribute *attr, | |
3387 | char *buf) | |
3388 | { | |
3389 | int status; | |
3390 | ||
73a94d86 HMH |
3391 | printk_deprecated_rfkill_attribute("wwan_enable"); |
3392 | ||
d3a6ade4 HMH |
3393 | status = wan_get_radiosw(); |
3394 | if (status < 0) | |
3395 | return status; | |
3396 | ||
0e74dc26 HMH |
3397 | return snprintf(buf, PAGE_SIZE, "%d\n", |
3398 | (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0); | |
d3a6ade4 HMH |
3399 | } |
3400 | ||
3401 | static ssize_t wan_enable_store(struct device *dev, | |
3402 | struct device_attribute *attr, | |
3403 | const char *buf, size_t count) | |
3404 | { | |
3405 | unsigned long t; | |
3406 | int res; | |
3407 | ||
73a94d86 HMH |
3408 | printk_deprecated_rfkill_attribute("wwan_enable"); |
3409 | ||
d3a6ade4 HMH |
3410 | if (parse_strtoul(buf, 1, &t)) |
3411 | return -EINVAL; | |
3412 | ||
0e74dc26 | 3413 | res = wan_set_radiosw(t, 1); |
d3a6ade4 HMH |
3414 | |
3415 | return (res) ? res : count; | |
3416 | } | |
3417 | ||
3418 | static struct device_attribute dev_attr_wan_enable = | |
cc4c24e1 | 3419 | __ATTR(wwan_enable, S_IWUSR | S_IRUGO, |
d3a6ade4 HMH |
3420 | wan_enable_show, wan_enable_store); |
3421 | ||
3422 | /* --------------------------------------------------------------------- */ | |
3423 | ||
3424 | static struct attribute *wan_attributes[] = { | |
3425 | &dev_attr_wan_enable.attr, | |
3426 | NULL | |
3427 | }; | |
3428 | ||
3429 | static const struct attribute_group wan_attr_group = { | |
d3a6ade4 HMH |
3430 | .attrs = wan_attributes, |
3431 | }; | |
3432 | ||
0e74dc26 HMH |
3433 | static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state) |
3434 | { | |
3435 | int wans = wan_get_radiosw(); | |
3436 | ||
3437 | if (wans < 0) | |
3438 | return wans; | |
3439 | ||
3440 | *state = wans; | |
3441 | return 0; | |
3442 | } | |
3443 | ||
3444 | static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state) | |
3445 | { | |
3446 | return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0); | |
3447 | } | |
3448 | ||
90d9d3c7 HMH |
3449 | static void wan_shutdown(void) |
3450 | { | |
3451 | /* Order firmware to save current state to NVRAM */ | |
3452 | if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd", | |
3453 | TP_ACPI_WGSV_SAVE_STATE)) | |
3454 | printk(TPACPI_NOTICE | |
3455 | "failed to save WWAN state to NVRAM\n"); | |
3456 | } | |
3457 | ||
07431ec8 HMH |
3458 | static void wan_exit(void) |
3459 | { | |
90d9d3c7 HMH |
3460 | wan_shutdown(); |
3461 | ||
0e74dc26 HMH |
3462 | if (tpacpi_wan_rfkill) |
3463 | rfkill_unregister(tpacpi_wan_rfkill); | |
3464 | ||
07431ec8 HMH |
3465 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, |
3466 | &wan_attr_group); | |
3467 | } | |
3468 | ||
a5763f22 | 3469 | static int __init wan_init(struct ibm_init_struct *iibm) |
42adb53c | 3470 | { |
d3a6ade4 | 3471 | int res; |
d6fdd1e9 HMH |
3472 | int status = 0; |
3473 | ||
fe08bc4b HMH |
3474 | vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n"); |
3475 | ||
e0c7dfe7 | 3476 | TPACPI_ACPIHANDLE_INIT(hkey); |
5fba344c | 3477 | |
d8fd94d9 | 3478 | tp_features.wan = hkey_handle && |
d6fdd1e9 HMH |
3479 | acpi_evalf(hkey_handle, &status, "GWAN", "qd"); |
3480 | ||
3481 | vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n", | |
3482 | str_supported(tp_features.wan), | |
3483 | status); | |
3484 | ||
a73f3091 HMH |
3485 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
3486 | if (dbg_wwanemul) { | |
3487 | tp_features.wan = 1; | |
3488 | printk(TPACPI_INFO | |
3489 | "wwan switch emulation enabled\n"); | |
3490 | } else | |
3491 | #endif | |
3a872080 HMH |
3492 | if (tp_features.wan && |
3493 | !(status & TP_ACPI_WANCARD_HWPRESENT)) { | |
3494 | /* no wan hardware present in system */ | |
3495 | tp_features.wan = 0; | |
3496 | dbg_printk(TPACPI_DBG_INIT, | |
3497 | "wan hardware not installed\n"); | |
3498 | } | |
3499 | ||
0e74dc26 HMH |
3500 | if (!tp_features.wan) |
3501 | return 1; | |
3502 | ||
3503 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
3a872080 | 3504 | &wan_attr_group); |
0e74dc26 HMH |
3505 | if (res) |
3506 | return res; | |
3507 | ||
3508 | res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID, | |
3509 | &tpacpi_wan_rfkill, | |
3510 | RFKILL_TYPE_WWAN, | |
3511 | "tpacpi_wwan_sw", | |
0045c0aa | 3512 | true, |
0e74dc26 HMH |
3513 | tpacpi_wan_rfk_set, |
3514 | tpacpi_wan_rfk_get); | |
3515 | if (res) { | |
3516 | wan_exit(); | |
3517 | return res; | |
d6fdd1e9 | 3518 | } |
fe08bc4b | 3519 | |
0e74dc26 | 3520 | return 0; |
42adb53c JF |
3521 | } |
3522 | ||
d3a6ade4 | 3523 | /* procfs -------------------------------------------------------------- */ |
42adb53c JF |
3524 | static int wan_read(char *p) |
3525 | { | |
3526 | int len = 0; | |
d6fdd1e9 | 3527 | int status = wan_get_radiosw(); |
42adb53c | 3528 | |
d8fd94d9 | 3529 | if (!tp_features.wan) |
42adb53c | 3530 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
42adb53c | 3531 | else { |
d6fdd1e9 | 3532 | len += sprintf(p + len, "status:\t\t%s\n", |
0e74dc26 HMH |
3533 | (status == RFKILL_STATE_UNBLOCKED) ? |
3534 | "enabled" : "disabled"); | |
42adb53c JF |
3535 | len += sprintf(p + len, "commands:\tenable, disable\n"); |
3536 | } | |
3537 | ||
3538 | return len; | |
3539 | } | |
3540 | ||
3541 | static int wan_write(char *buf) | |
3542 | { | |
42adb53c | 3543 | char *cmd; |
42adb53c | 3544 | |
d8fd94d9 | 3545 | if (!tp_features.wan) |
42adb53c JF |
3546 | return -ENODEV; |
3547 | ||
3548 | while ((cmd = next_cmd(&buf))) { | |
3549 | if (strlencmp(cmd, "enable") == 0) { | |
0e74dc26 | 3550 | wan_set_radiosw(1, 1); |
42adb53c | 3551 | } else if (strlencmp(cmd, "disable") == 0) { |
0e74dc26 | 3552 | wan_set_radiosw(0, 1); |
42adb53c JF |
3553 | } else |
3554 | return -EINVAL; | |
42adb53c JF |
3555 | } |
3556 | ||
42adb53c JF |
3557 | return 0; |
3558 | } | |
3559 | ||
a5763f22 HMH |
3560 | static struct ibm_struct wan_driver_data = { |
3561 | .name = "wan", | |
3562 | .read = wan_read, | |
3563 | .write = wan_write, | |
d3a6ade4 | 3564 | .exit = wan_exit, |
153f8220 | 3565 | .suspend = wan_suspend, |
90d9d3c7 | 3566 | .shutdown = wan_shutdown, |
a5763f22 HMH |
3567 | }; |
3568 | ||
0045c0aa HMH |
3569 | /************************************************************************* |
3570 | * UWB subdriver | |
3571 | */ | |
3572 | ||
3573 | enum { | |
3574 | /* ACPI GUWB/SUWB bits */ | |
3575 | TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */ | |
3576 | TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */ | |
3577 | }; | |
3578 | ||
3579 | static struct rfkill *tpacpi_uwb_rfkill; | |
3580 | ||
3581 | static int uwb_get_radiosw(void) | |
3582 | { | |
3583 | int status; | |
3584 | ||
3585 | if (!tp_features.uwb) | |
3586 | return -ENODEV; | |
3587 | ||
3588 | /* WLSW overrides UWB in firmware/hardware, reflect that */ | |
3589 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status) | |
3590 | return RFKILL_STATE_HARD_BLOCKED; | |
3591 | ||
3592 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES | |
3593 | if (dbg_uwbemul) | |
3594 | return (tpacpi_uwb_emulstate) ? | |
3595 | RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; | |
3596 | #endif | |
3597 | ||
3598 | if (!acpi_evalf(hkey_handle, &status, "GUWB", "d")) | |
3599 | return -EIO; | |
3600 | ||
3601 | return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ? | |
3602 | RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; | |
3603 | } | |
3604 | ||
3605 | static void uwb_update_rfk(void) | |
3606 | { | |
3607 | int status; | |
3608 | ||
3609 | if (!tpacpi_uwb_rfkill) | |
3610 | return; | |
3611 | ||
3612 | status = uwb_get_radiosw(); | |
3613 | if (status < 0) | |
3614 | return; | |
3615 | rfkill_force_state(tpacpi_uwb_rfkill, status); | |
3616 | } | |
3617 | ||
3618 | static int uwb_set_radiosw(int radio_on, int update_rfk) | |
3619 | { | |
3620 | int status; | |
3621 | ||
3622 | if (!tp_features.uwb) | |
3623 | return -ENODEV; | |
3624 | ||
3625 | /* WLSW overrides UWB in firmware/hardware, but there is no | |
3626 | * reason to risk weird behaviour. */ | |
3627 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status | |
3628 | && radio_on) | |
3629 | return -EPERM; | |
3630 | ||
3631 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES | |
3632 | if (dbg_uwbemul) { | |
3633 | tpacpi_uwb_emulstate = !!radio_on; | |
3634 | if (update_rfk) | |
3635 | uwb_update_rfk(); | |
3636 | return 0; | |
3637 | } | |
3638 | #endif | |
3639 | ||
3640 | status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0; | |
3641 | if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status)) | |
3642 | return -EIO; | |
3643 | ||
3644 | if (update_rfk) | |
3645 | uwb_update_rfk(); | |
3646 | ||
3647 | return 0; | |
3648 | } | |
3649 | ||
3650 | /* --------------------------------------------------------------------- */ | |
3651 | ||
3652 | static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state) | |
3653 | { | |
3654 | int uwbs = uwb_get_radiosw(); | |
3655 | ||
3656 | if (uwbs < 0) | |
3657 | return uwbs; | |
3658 | ||
3659 | *state = uwbs; | |
3660 | return 0; | |
3661 | } | |
3662 | ||
3663 | static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state) | |
3664 | { | |
3665 | return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0); | |
3666 | } | |
3667 | ||
3668 | static void uwb_exit(void) | |
3669 | { | |
3670 | if (tpacpi_uwb_rfkill) | |
3671 | rfkill_unregister(tpacpi_uwb_rfkill); | |
3672 | } | |
3673 | ||
3674 | static int __init uwb_init(struct ibm_init_struct *iibm) | |
3675 | { | |
3676 | int res; | |
3677 | int status = 0; | |
3678 | ||
3679 | vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n"); | |
3680 | ||
3681 | TPACPI_ACPIHANDLE_INIT(hkey); | |
3682 | ||
3683 | tp_features.uwb = hkey_handle && | |
3684 | acpi_evalf(hkey_handle, &status, "GUWB", "qd"); | |
3685 | ||
3686 | vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n", | |
3687 | str_supported(tp_features.uwb), | |
3688 | status); | |
3689 | ||
3690 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES | |
3691 | if (dbg_uwbemul) { | |
3692 | tp_features.uwb = 1; | |
3693 | printk(TPACPI_INFO | |
3694 | "uwb switch emulation enabled\n"); | |
3695 | } else | |
3696 | #endif | |
3697 | if (tp_features.uwb && | |
3698 | !(status & TP_ACPI_UWB_HWPRESENT)) { | |
3699 | /* no uwb hardware present in system */ | |
3700 | tp_features.uwb = 0; | |
3701 | dbg_printk(TPACPI_DBG_INIT, | |
3702 | "uwb hardware not installed\n"); | |
3703 | } | |
3704 | ||
3705 | if (!tp_features.uwb) | |
3706 | return 1; | |
3707 | ||
3708 | res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID, | |
3709 | &tpacpi_uwb_rfkill, | |
3710 | RFKILL_TYPE_UWB, | |
3711 | "tpacpi_uwb_sw", | |
3712 | false, | |
3713 | tpacpi_uwb_rfk_set, | |
3714 | tpacpi_uwb_rfk_get); | |
3715 | ||
3716 | return res; | |
3717 | } | |
3718 | ||
3719 | static struct ibm_struct uwb_driver_data = { | |
3720 | .name = "uwb", | |
3721 | .exit = uwb_exit, | |
3722 | .flags.experimental = 1, | |
3723 | }; | |
3724 | ||
56b6aeb0 HMH |
3725 | /************************************************************************* |
3726 | * Video subdriver | |
3727 | */ | |
3728 | ||
d7c1d17d HMH |
3729 | #ifdef CONFIG_THINKPAD_ACPI_VIDEO |
3730 | ||
f74a27d4 HMH |
3731 | enum video_access_mode { |
3732 | TPACPI_VIDEO_NONE = 0, | |
3733 | TPACPI_VIDEO_570, /* 570 */ | |
3734 | TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */ | |
3735 | TPACPI_VIDEO_NEW, /* all others */ | |
3736 | }; | |
3737 | ||
3738 | enum { /* video status flags, based on VIDEO_570 */ | |
3739 | TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */ | |
3740 | TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */ | |
3741 | TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */ | |
3742 | }; | |
3743 | ||
3744 | enum { /* TPACPI_VIDEO_570 constants */ | |
3745 | TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */ | |
3746 | TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to | |
3747 | * video_status_flags */ | |
3748 | TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */ | |
3749 | TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */ | |
3750 | }; | |
3751 | ||
9a8e1738 HMH |
3752 | static enum video_access_mode video_supported; |
3753 | static int video_orig_autosw; | |
78f81cc4 | 3754 | |
f74a27d4 HMH |
3755 | static int video_autosw_get(void); |
3756 | static int video_autosw_set(int enable); | |
3757 | ||
e0c7dfe7 | 3758 | TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */ |
56b6aeb0 | 3759 | |
a5763f22 | 3760 | static int __init video_init(struct ibm_init_struct *iibm) |
1da177e4 | 3761 | { |
78f81cc4 BD |
3762 | int ivga; |
3763 | ||
fe08bc4b HMH |
3764 | vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n"); |
3765 | ||
e0c7dfe7 HMH |
3766 | TPACPI_ACPIHANDLE_INIT(vid); |
3767 | TPACPI_ACPIHANDLE_INIT(vid2); | |
5fba344c | 3768 | |
78f81cc4 BD |
3769 | if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga) |
3770 | /* G41, assume IVGA doesn't change */ | |
3771 | vid_handle = vid2_handle; | |
3772 | ||
3773 | if (!vid_handle) | |
3774 | /* video switching not supported on R30, R31 */ | |
efa27145 | 3775 | video_supported = TPACPI_VIDEO_NONE; |
78f81cc4 BD |
3776 | else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd")) |
3777 | /* 570 */ | |
efa27145 | 3778 | video_supported = TPACPI_VIDEO_570; |
78f81cc4 BD |
3779 | else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd")) |
3780 | /* 600e/x, 770e, 770x */ | |
efa27145 | 3781 | video_supported = TPACPI_VIDEO_770; |
78f81cc4 BD |
3782 | else |
3783 | /* all others */ | |
efa27145 | 3784 | video_supported = TPACPI_VIDEO_NEW; |
1da177e4 | 3785 | |
fe08bc4b HMH |
3786 | vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n", |
3787 | str_supported(video_supported != TPACPI_VIDEO_NONE), | |
3788 | video_supported); | |
3789 | ||
5fba344c | 3790 | return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1; |
1da177e4 LT |
3791 | } |
3792 | ||
56b6aeb0 HMH |
3793 | static void video_exit(void) |
3794 | { | |
83f34724 HMH |
3795 | dbg_printk(TPACPI_DBG_EXIT, |
3796 | "restoring original video autoswitch mode\n"); | |
3797 | if (video_autosw_set(video_orig_autosw)) | |
e0c7dfe7 | 3798 | printk(TPACPI_ERR "error while trying to restore original " |
83f34724 | 3799 | "video autoswitch mode\n"); |
56b6aeb0 HMH |
3800 | } |
3801 | ||
83f34724 | 3802 | static int video_outputsw_get(void) |
1da177e4 LT |
3803 | { |
3804 | int status = 0; | |
3805 | int i; | |
3806 | ||
83f34724 HMH |
3807 | switch (video_supported) { |
3808 | case TPACPI_VIDEO_570: | |
3809 | if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", | |
3810 | TP_ACPI_VIDEO_570_PHSCMD)) | |
3811 | return -EIO; | |
3812 | status = i & TP_ACPI_VIDEO_570_PHSMASK; | |
3813 | break; | |
3814 | case TPACPI_VIDEO_770: | |
3815 | if (!acpi_evalf(NULL, &i, "\\VCDL", "d")) | |
3816 | return -EIO; | |
3817 | if (i) | |
3818 | status |= TP_ACPI_VIDEO_S_LCD; | |
3819 | if (!acpi_evalf(NULL, &i, "\\VCDC", "d")) | |
3820 | return -EIO; | |
3821 | if (i) | |
3822 | status |= TP_ACPI_VIDEO_S_CRT; | |
3823 | break; | |
3824 | case TPACPI_VIDEO_NEW: | |
3825 | if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) || | |
3826 | !acpi_evalf(NULL, &i, "\\VCDC", "d")) | |
3827 | return -EIO; | |
3828 | if (i) | |
3829 | status |= TP_ACPI_VIDEO_S_CRT; | |
3830 | ||
3831 | if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) || | |
3832 | !acpi_evalf(NULL, &i, "\\VCDL", "d")) | |
3833 | return -EIO; | |
3834 | if (i) | |
3835 | status |= TP_ACPI_VIDEO_S_LCD; | |
3836 | if (!acpi_evalf(NULL, &i, "\\VCDD", "d")) | |
3837 | return -EIO; | |
3838 | if (i) | |
3839 | status |= TP_ACPI_VIDEO_S_DVI; | |
3840 | break; | |
3841 | default: | |
3842 | return -ENOSYS; | |
78f81cc4 BD |
3843 | } |
3844 | ||
3845 | return status; | |
3846 | } | |
1da177e4 | 3847 | |
83f34724 | 3848 | static int video_outputsw_set(int status) |
78f81cc4 | 3849 | { |
83f34724 HMH |
3850 | int autosw; |
3851 | int res = 0; | |
3852 | ||
3853 | switch (video_supported) { | |
3854 | case TPACPI_VIDEO_570: | |
3855 | res = acpi_evalf(NULL, NULL, | |
3856 | "\\_SB.PHS2", "vdd", | |
3857 | TP_ACPI_VIDEO_570_PHS2CMD, | |
3858 | status | TP_ACPI_VIDEO_570_PHS2SET); | |
3859 | break; | |
3860 | case TPACPI_VIDEO_770: | |
3861 | autosw = video_autosw_get(); | |
3862 | if (autosw < 0) | |
3863 | return autosw; | |
1da177e4 | 3864 | |
83f34724 HMH |
3865 | res = video_autosw_set(1); |
3866 | if (res) | |
3867 | return res; | |
3868 | res = acpi_evalf(vid_handle, NULL, | |
3869 | "ASWT", "vdd", status * 0x100, 0); | |
3870 | if (!autosw && video_autosw_set(autosw)) { | |
35ff8b9f HMH |
3871 | printk(TPACPI_ERR |
3872 | "video auto-switch left enabled due to error\n"); | |
83f34724 HMH |
3873 | return -EIO; |
3874 | } | |
3875 | break; | |
3876 | case TPACPI_VIDEO_NEW: | |
3877 | res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) && | |
35ff8b9f | 3878 | acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1); |
83f34724 HMH |
3879 | break; |
3880 | default: | |
3881 | return -ENOSYS; | |
3882 | } | |
1da177e4 | 3883 | |
83f34724 | 3884 | return (res)? 0 : -EIO; |
1da177e4 LT |
3885 | } |
3886 | ||
83f34724 | 3887 | static int video_autosw_get(void) |
78f81cc4 | 3888 | { |
83f34724 | 3889 | int autosw = 0; |
78f81cc4 | 3890 | |
83f34724 HMH |
3891 | switch (video_supported) { |
3892 | case TPACPI_VIDEO_570: | |
3893 | if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d")) | |
3894 | return -EIO; | |
3895 | break; | |
3896 | case TPACPI_VIDEO_770: | |
3897 | case TPACPI_VIDEO_NEW: | |
3898 | if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d")) | |
3899 | return -EIO; | |
3900 | break; | |
3901 | default: | |
3902 | return -ENOSYS; | |
3903 | } | |
78f81cc4 | 3904 | |
83f34724 | 3905 | return autosw & 1; |
78f81cc4 BD |
3906 | } |
3907 | ||
83f34724 | 3908 | static int video_autosw_set(int enable) |
78f81cc4 | 3909 | { |
83f34724 HMH |
3910 | if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0)) |
3911 | return -EIO; | |
3912 | return 0; | |
78f81cc4 BD |
3913 | } |
3914 | ||
83f34724 | 3915 | static int video_outputsw_cycle(void) |
78f81cc4 | 3916 | { |
83f34724 HMH |
3917 | int autosw = video_autosw_get(); |
3918 | int res; | |
78f81cc4 | 3919 | |
83f34724 HMH |
3920 | if (autosw < 0) |
3921 | return autosw; | |
78f81cc4 | 3922 | |
83f34724 HMH |
3923 | switch (video_supported) { |
3924 | case TPACPI_VIDEO_570: | |
3925 | res = video_autosw_set(1); | |
3926 | if (res) | |
3927 | return res; | |
3928 | res = acpi_evalf(ec_handle, NULL, "_Q16", "v"); | |
3929 | break; | |
3930 | case TPACPI_VIDEO_770: | |
3931 | case TPACPI_VIDEO_NEW: | |
3932 | res = video_autosw_set(1); | |
3933 | if (res) | |
3934 | return res; | |
3935 | res = acpi_evalf(vid_handle, NULL, "VSWT", "v"); | |
3936 | break; | |
3937 | default: | |
3938 | return -ENOSYS; | |
3939 | } | |
3940 | if (!autosw && video_autosw_set(autosw)) { | |
35ff8b9f HMH |
3941 | printk(TPACPI_ERR |
3942 | "video auto-switch left enabled due to error\n"); | |
83f34724 | 3943 | return -EIO; |
78f81cc4 BD |
3944 | } |
3945 | ||
83f34724 HMH |
3946 | return (res)? 0 : -EIO; |
3947 | } | |
3948 | ||
3949 | static int video_expand_toggle(void) | |
3950 | { | |
3951 | switch (video_supported) { | |
3952 | case TPACPI_VIDEO_570: | |
3953 | return acpi_evalf(ec_handle, NULL, "_Q17", "v")? | |
3954 | 0 : -EIO; | |
3955 | case TPACPI_VIDEO_770: | |
3956 | return acpi_evalf(vid_handle, NULL, "VEXP", "v")? | |
3957 | 0 : -EIO; | |
3958 | case TPACPI_VIDEO_NEW: | |
3959 | return acpi_evalf(NULL, NULL, "\\VEXP", "v")? | |
3960 | 0 : -EIO; | |
3961 | default: | |
3962 | return -ENOSYS; | |
3963 | } | |
3964 | /* not reached */ | |
78f81cc4 BD |
3965 | } |
3966 | ||
56b6aeb0 HMH |
3967 | static int video_read(char *p) |
3968 | { | |
83f34724 | 3969 | int status, autosw; |
56b6aeb0 HMH |
3970 | int len = 0; |
3971 | ||
83f34724 | 3972 | if (video_supported == TPACPI_VIDEO_NONE) { |
56b6aeb0 HMH |
3973 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
3974 | return len; | |
3975 | } | |
3976 | ||
83f34724 HMH |
3977 | status = video_outputsw_get(); |
3978 | if (status < 0) | |
3979 | return status; | |
3980 | ||
3981 | autosw = video_autosw_get(); | |
3982 | if (autosw < 0) | |
3983 | return autosw; | |
3984 | ||
56b6aeb0 HMH |
3985 | len += sprintf(p + len, "status:\t\tsupported\n"); |
3986 | len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0)); | |
3987 | len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1)); | |
efa27145 | 3988 | if (video_supported == TPACPI_VIDEO_NEW) |
56b6aeb0 HMH |
3989 | len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3)); |
3990 | len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0)); | |
3991 | len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n"); | |
3992 | len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n"); | |
efa27145 | 3993 | if (video_supported == TPACPI_VIDEO_NEW) |
56b6aeb0 HMH |
3994 | len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n"); |
3995 | len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n"); | |
3996 | len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n"); | |
3997 | ||
3998 | return len; | |
3999 | } | |
4000 | ||
78f81cc4 | 4001 | static int video_write(char *buf) |
1da177e4 LT |
4002 | { |
4003 | char *cmd; | |
4004 | int enable, disable, status; | |
83f34724 | 4005 | int res; |
1da177e4 | 4006 | |
83f34724 | 4007 | if (video_supported == TPACPI_VIDEO_NONE) |
78f81cc4 BD |
4008 | return -ENODEV; |
4009 | ||
83f34724 HMH |
4010 | enable = 0; |
4011 | disable = 0; | |
1da177e4 LT |
4012 | |
4013 | while ((cmd = next_cmd(&buf))) { | |
4014 | if (strlencmp(cmd, "lcd_enable") == 0) { | |
83f34724 | 4015 | enable |= TP_ACPI_VIDEO_S_LCD; |
1da177e4 | 4016 | } else if (strlencmp(cmd, "lcd_disable") == 0) { |
83f34724 | 4017 | disable |= TP_ACPI_VIDEO_S_LCD; |
1da177e4 | 4018 | } else if (strlencmp(cmd, "crt_enable") == 0) { |
83f34724 | 4019 | enable |= TP_ACPI_VIDEO_S_CRT; |
1da177e4 | 4020 | } else if (strlencmp(cmd, "crt_disable") == 0) { |
83f34724 | 4021 | disable |= TP_ACPI_VIDEO_S_CRT; |
efa27145 | 4022 | } else if (video_supported == TPACPI_VIDEO_NEW && |
78f81cc4 | 4023 | strlencmp(cmd, "dvi_enable") == 0) { |
83f34724 | 4024 | enable |= TP_ACPI_VIDEO_S_DVI; |
efa27145 | 4025 | } else if (video_supported == TPACPI_VIDEO_NEW && |
78f81cc4 | 4026 | strlencmp(cmd, "dvi_disable") == 0) { |
83f34724 | 4027 | disable |= TP_ACPI_VIDEO_S_DVI; |
1da177e4 | 4028 | } else if (strlencmp(cmd, "auto_enable") == 0) { |
83f34724 HMH |
4029 | res = video_autosw_set(1); |
4030 | if (res) | |
4031 | return res; | |
1da177e4 | 4032 | } else if (strlencmp(cmd, "auto_disable") == 0) { |
83f34724 HMH |
4033 | res = video_autosw_set(0); |
4034 | if (res) | |
4035 | return res; | |
1da177e4 | 4036 | } else if (strlencmp(cmd, "video_switch") == 0) { |
83f34724 HMH |
4037 | res = video_outputsw_cycle(); |
4038 | if (res) | |
4039 | return res; | |
1da177e4 | 4040 | } else if (strlencmp(cmd, "expand_toggle") == 0) { |
83f34724 HMH |
4041 | res = video_expand_toggle(); |
4042 | if (res) | |
4043 | return res; | |
1da177e4 LT |
4044 | } else |
4045 | return -EINVAL; | |
4046 | } | |
4047 | ||
4048 | if (enable || disable) { | |
83f34724 HMH |
4049 | status = video_outputsw_get(); |
4050 | if (status < 0) | |
4051 | return status; | |
4052 | res = video_outputsw_set((status & ~disable) | enable); | |
4053 | if (res) | |
4054 | return res; | |
1da177e4 LT |
4055 | } |
4056 | ||
4057 | return 0; | |
4058 | } | |
4059 | ||
a5763f22 HMH |
4060 | static struct ibm_struct video_driver_data = { |
4061 | .name = "video", | |
4062 | .read = video_read, | |
4063 | .write = video_write, | |
4064 | .exit = video_exit, | |
4065 | }; | |
4066 | ||
d7c1d17d HMH |
4067 | #endif /* CONFIG_THINKPAD_ACPI_VIDEO */ |
4068 | ||
56b6aeb0 HMH |
4069 | /************************************************************************* |
4070 | * Light (thinklight) subdriver | |
4071 | */ | |
1da177e4 | 4072 | |
e0c7dfe7 HMH |
4073 | TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */ |
4074 | TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */ | |
56b6aeb0 | 4075 | |
4fa6811b HMH |
4076 | static int light_get_status(void) |
4077 | { | |
4078 | int status = 0; | |
4079 | ||
4080 | if (tp_features.light_status) { | |
4081 | if (!acpi_evalf(ec_handle, &status, "KBLT", "d")) | |
4082 | return -EIO; | |
4083 | return (!!status); | |
4084 | } | |
4085 | ||
4086 | return -ENXIO; | |
4087 | } | |
4088 | ||
4089 | static int light_set_status(int status) | |
4090 | { | |
4091 | int rc; | |
4092 | ||
4093 | if (tp_features.light) { | |
4094 | if (cmos_handle) { | |
4095 | rc = acpi_evalf(cmos_handle, NULL, NULL, "vd", | |
4096 | (status)? | |
4097 | TP_CMOS_THINKLIGHT_ON : | |
4098 | TP_CMOS_THINKLIGHT_OFF); | |
4099 | } else { | |
4100 | rc = acpi_evalf(lght_handle, NULL, NULL, "vd", | |
4101 | (status)? 1 : 0); | |
4102 | } | |
4103 | return (rc)? 0 : -EIO; | |
4104 | } | |
4105 | ||
4106 | return -ENXIO; | |
4107 | } | |
4108 | ||
e306501d HMH |
4109 | static void light_set_status_worker(struct work_struct *work) |
4110 | { | |
4111 | struct tpacpi_led_classdev *data = | |
4112 | container_of(work, struct tpacpi_led_classdev, work); | |
4113 | ||
4114 | if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING)) | |
4115 | light_set_status((data->new_brightness != LED_OFF)); | |
4116 | } | |
4117 | ||
4118 | static void light_sysfs_set(struct led_classdev *led_cdev, | |
4119 | enum led_brightness brightness) | |
4120 | { | |
4121 | struct tpacpi_led_classdev *data = | |
4122 | container_of(led_cdev, | |
4123 | struct tpacpi_led_classdev, | |
4124 | led_classdev); | |
4125 | data->new_brightness = brightness; | |
e0e3c061 | 4126 | queue_work(tpacpi_wq, &data->work); |
e306501d HMH |
4127 | } |
4128 | ||
4129 | static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev) | |
4130 | { | |
4131 | return (light_get_status() == 1)? LED_FULL : LED_OFF; | |
4132 | } | |
4133 | ||
4134 | static struct tpacpi_led_classdev tpacpi_led_thinklight = { | |
4135 | .led_classdev = { | |
4136 | .name = "tpacpi::thinklight", | |
4137 | .brightness_set = &light_sysfs_set, | |
4138 | .brightness_get = &light_sysfs_get, | |
4139 | } | |
4140 | }; | |
4141 | ||
a5763f22 | 4142 | static int __init light_init(struct ibm_init_struct *iibm) |
1da177e4 | 4143 | { |
9c0a76e1 | 4144 | int rc; |
e306501d | 4145 | |
fe08bc4b HMH |
4146 | vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n"); |
4147 | ||
e0c7dfe7 HMH |
4148 | TPACPI_ACPIHANDLE_INIT(ledb); |
4149 | TPACPI_ACPIHANDLE_INIT(lght); | |
4150 | TPACPI_ACPIHANDLE_INIT(cmos); | |
e306501d | 4151 | INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker); |
5fba344c | 4152 | |
78f81cc4 | 4153 | /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */ |
d8fd94d9 | 4154 | tp_features.light = (cmos_handle || lght_handle) && !ledb_handle; |
78f81cc4 | 4155 | |
d8fd94d9 | 4156 | if (tp_features.light) |
78f81cc4 BD |
4157 | /* light status not supported on |
4158 | 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */ | |
d8fd94d9 HMH |
4159 | tp_features.light_status = |
4160 | acpi_evalf(ec_handle, NULL, "KBLT", "qv"); | |
1da177e4 | 4161 | |
9c0a76e1 HMH |
4162 | vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n", |
4163 | str_supported(tp_features.light), | |
4164 | str_supported(tp_features.light_status)); | |
fe08bc4b | 4165 | |
9c0a76e1 HMH |
4166 | if (!tp_features.light) |
4167 | return 1; | |
4168 | ||
4169 | rc = led_classdev_register(&tpacpi_pdev->dev, | |
4170 | &tpacpi_led_thinklight.led_classdev); | |
e306501d HMH |
4171 | |
4172 | if (rc < 0) { | |
4173 | tp_features.light = 0; | |
4174 | tp_features.light_status = 0; | |
9c0a76e1 HMH |
4175 | } else { |
4176 | rc = 0; | |
e306501d | 4177 | } |
9c0a76e1 | 4178 | |
e306501d HMH |
4179 | return rc; |
4180 | } | |
4181 | ||
4182 | static void light_exit(void) | |
4183 | { | |
4184 | led_classdev_unregister(&tpacpi_led_thinklight.led_classdev); | |
4185 | if (work_pending(&tpacpi_led_thinklight.work)) | |
e0e3c061 | 4186 | flush_workqueue(tpacpi_wq); |
1da177e4 LT |
4187 | } |
4188 | ||
78f81cc4 | 4189 | static int light_read(char *p) |
1da177e4 LT |
4190 | { |
4191 | int len = 0; | |
4fa6811b | 4192 | int status; |
1da177e4 | 4193 | |
d8fd94d9 | 4194 | if (!tp_features.light) { |
78f81cc4 | 4195 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
d8fd94d9 | 4196 | } else if (!tp_features.light_status) { |
78f81cc4 BD |
4197 | len += sprintf(p + len, "status:\t\tunknown\n"); |
4198 | len += sprintf(p + len, "commands:\ton, off\n"); | |
4199 | } else { | |
4fa6811b HMH |
4200 | status = light_get_status(); |
4201 | if (status < 0) | |
4202 | return status; | |
1da177e4 | 4203 | len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0)); |
78f81cc4 BD |
4204 | len += sprintf(p + len, "commands:\ton, off\n"); |
4205 | } | |
1da177e4 LT |
4206 | |
4207 | return len; | |
4208 | } | |
4209 | ||
78f81cc4 | 4210 | static int light_write(char *buf) |
1da177e4 | 4211 | { |
1da177e4 | 4212 | char *cmd; |
4fa6811b | 4213 | int newstatus = 0; |
78f81cc4 | 4214 | |
d8fd94d9 | 4215 | if (!tp_features.light) |
78f81cc4 BD |
4216 | return -ENODEV; |
4217 | ||
1da177e4 LT |
4218 | while ((cmd = next_cmd(&buf))) { |
4219 | if (strlencmp(cmd, "on") == 0) { | |
4fa6811b | 4220 | newstatus = 1; |
1da177e4 | 4221 | } else if (strlencmp(cmd, "off") == 0) { |
4fa6811b | 4222 | newstatus = 0; |
1da177e4 LT |
4223 | } else |
4224 | return -EINVAL; | |
1da177e4 LT |
4225 | } |
4226 | ||
4fa6811b | 4227 | return light_set_status(newstatus); |
1da177e4 LT |
4228 | } |
4229 | ||
a5763f22 HMH |
4230 | static struct ibm_struct light_driver_data = { |
4231 | .name = "light", | |
4232 | .read = light_read, | |
4233 | .write = light_write, | |
e306501d | 4234 | .exit = light_exit, |
a5763f22 HMH |
4235 | }; |
4236 | ||
56b6aeb0 HMH |
4237 | /************************************************************************* |
4238 | * Dock subdriver | |
4239 | */ | |
1da177e4 | 4240 | |
85998248 | 4241 | #ifdef CONFIG_THINKPAD_ACPI_DOCK |
56b6aeb0 | 4242 | |
f74a27d4 HMH |
4243 | static void dock_notify(struct ibm_struct *ibm, u32 event); |
4244 | static int dock_read(char *p); | |
4245 | static int dock_write(char *buf); | |
4246 | ||
e0c7dfe7 | 4247 | TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */ |
56b6aeb0 HMH |
4248 | "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */ |
4249 | "\\_SB.PCI0.PCI1.DOCK", /* all others */ | |
4250 | "\\_SB.PCI.ISA.SLCE", /* 570 */ | |
4251 | ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */ | |
4252 | ||
5fba344c | 4253 | /* don't list other alternatives as we install a notify handler on the 570 */ |
e0c7dfe7 | 4254 | TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */ |
5fba344c | 4255 | |
1ba90e3a TR |
4256 | static const struct acpi_device_id ibm_pci_device_ids[] = { |
4257 | {PCI_ROOT_HID_STRING, 0}, | |
4258 | {"", 0}, | |
4259 | }; | |
4260 | ||
d94a7f16 HMH |
4261 | static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = { |
4262 | { | |
4263 | .notify = dock_notify, | |
4264 | .handle = &dock_handle, | |
4265 | .type = ACPI_SYSTEM_NOTIFY, | |
4266 | }, | |
4267 | { | |
996fba08 HMH |
4268 | /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING. |
4269 | * We just use it to get notifications of dock hotplug | |
4270 | * in very old thinkpads */ | |
1ba90e3a | 4271 | .hid = ibm_pci_device_ids, |
d94a7f16 HMH |
4272 | .notify = dock_notify, |
4273 | .handle = &pci_handle, | |
4274 | .type = ACPI_SYSTEM_NOTIFY, | |
4275 | }, | |
4276 | }; | |
4277 | ||
4278 | static struct ibm_struct dock_driver_data[2] = { | |
4279 | { | |
4280 | .name = "dock", | |
4281 | .read = dock_read, | |
4282 | .write = dock_write, | |
4283 | .acpi = &ibm_dock_acpidriver[0], | |
4284 | }, | |
4285 | { | |
4286 | .name = "dock", | |
4287 | .acpi = &ibm_dock_acpidriver[1], | |
4288 | }, | |
4289 | }; | |
4290 | ||
1da177e4 LT |
4291 | #define dock_docked() (_sta(dock_handle) & 1) |
4292 | ||
a5763f22 | 4293 | static int __init dock_init(struct ibm_init_struct *iibm) |
5fba344c | 4294 | { |
fe08bc4b HMH |
4295 | vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n"); |
4296 | ||
e0c7dfe7 | 4297 | TPACPI_ACPIHANDLE_INIT(dock); |
5fba344c | 4298 | |
fe08bc4b HMH |
4299 | vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n", |
4300 | str_supported(dock_handle != NULL)); | |
4301 | ||
5fba344c HMH |
4302 | return (dock_handle)? 0 : 1; |
4303 | } | |
4304 | ||
d94a7f16 HMH |
4305 | static int __init dock_init2(struct ibm_init_struct *iibm) |
4306 | { | |
4307 | int dock2_needed; | |
4308 | ||
4309 | vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n"); | |
4310 | ||
4311 | if (dock_driver_data[0].flags.acpi_driver_registered && | |
4312 | dock_driver_data[0].flags.acpi_notify_installed) { | |
e0c7dfe7 | 4313 | TPACPI_ACPIHANDLE_INIT(pci); |
d94a7f16 HMH |
4314 | dock2_needed = (pci_handle != NULL); |
4315 | vdbg_printk(TPACPI_DBG_INIT, | |
4316 | "dock PCI handler for the TP 570 is %s\n", | |
4317 | str_supported(dock2_needed)); | |
4318 | } else { | |
4319 | vdbg_printk(TPACPI_DBG_INIT, | |
4320 | "dock subdriver part 2 not required\n"); | |
4321 | dock2_needed = 0; | |
4322 | } | |
4323 | ||
4324 | return (dock2_needed)? 0 : 1; | |
4325 | } | |
4326 | ||
56b6aeb0 HMH |
4327 | static void dock_notify(struct ibm_struct *ibm, u32 event) |
4328 | { | |
4329 | int docked = dock_docked(); | |
1ba90e3a TR |
4330 | int pci = ibm->acpi->hid && ibm->acpi->device && |
4331 | acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids); | |
962ce8ca | 4332 | int data; |
56b6aeb0 HMH |
4333 | |
4334 | if (event == 1 && !pci) /* 570 */ | |
962ce8ca | 4335 | data = 1; /* button */ |
56b6aeb0 | 4336 | else if (event == 1 && pci) /* 570 */ |
962ce8ca | 4337 | data = 3; /* dock */ |
56b6aeb0 | 4338 | else if (event == 3 && docked) |
962ce8ca | 4339 | data = 1; /* button */ |
56b6aeb0 | 4340 | else if (event == 3 && !docked) |
962ce8ca | 4341 | data = 2; /* undock */ |
56b6aeb0 | 4342 | else if (event == 0 && docked) |
962ce8ca | 4343 | data = 3; /* dock */ |
56b6aeb0 | 4344 | else { |
e0c7dfe7 | 4345 | printk(TPACPI_ERR "unknown dock event %d, status %d\n", |
56b6aeb0 | 4346 | event, _sta(dock_handle)); |
962ce8ca | 4347 | data = 0; /* unknown */ |
56b6aeb0 | 4348 | } |
14e04fb3 | 4349 | acpi_bus_generate_proc_event(ibm->acpi->device, event, data); |
962ce8ca | 4350 | acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class, |
e0b36fc5 | 4351 | dev_name(&ibm->acpi->device->dev), |
962ce8ca | 4352 | event, data); |
56b6aeb0 HMH |
4353 | } |
4354 | ||
78f81cc4 | 4355 | static int dock_read(char *p) |
1da177e4 LT |
4356 | { |
4357 | int len = 0; | |
4358 | int docked = dock_docked(); | |
4359 | ||
4360 | if (!dock_handle) | |
4361 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
4362 | else if (!docked) | |
4363 | len += sprintf(p + len, "status:\t\tundocked\n"); | |
4364 | else { | |
4365 | len += sprintf(p + len, "status:\t\tdocked\n"); | |
4366 | len += sprintf(p + len, "commands:\tdock, undock\n"); | |
4367 | } | |
4368 | ||
4369 | return len; | |
4370 | } | |
4371 | ||
78f81cc4 | 4372 | static int dock_write(char *buf) |
1da177e4 LT |
4373 | { |
4374 | char *cmd; | |
4375 | ||
4376 | if (!dock_docked()) | |
78f81cc4 | 4377 | return -ENODEV; |
1da177e4 LT |
4378 | |
4379 | while ((cmd = next_cmd(&buf))) { | |
4380 | if (strlencmp(cmd, "undock") == 0) { | |
78f81cc4 BD |
4381 | if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) || |
4382 | !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1)) | |
56b6aeb0 HMH |
4383 | return -EIO; |
4384 | } else if (strlencmp(cmd, "dock") == 0) { | |
4385 | if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1)) | |
4386 | return -EIO; | |
4387 | } else | |
4388 | return -EINVAL; | |
1da177e4 | 4389 | } |
56b6aeb0 HMH |
4390 | |
4391 | return 0; | |
1da177e4 | 4392 | } |
56b6aeb0 | 4393 | |
85998248 | 4394 | #endif /* CONFIG_THINKPAD_ACPI_DOCK */ |
56b6aeb0 HMH |
4395 | |
4396 | /************************************************************************* | |
4397 | * Bay subdriver | |
4398 | */ | |
1da177e4 | 4399 | |
85998248 | 4400 | #ifdef CONFIG_THINKPAD_ACPI_BAY |
f74a27d4 | 4401 | |
e0c7dfe7 | 4402 | TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */ |
56b6aeb0 HMH |
4403 | "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */ |
4404 | "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */ | |
4405 | "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */ | |
4406 | ); /* A21e, R30, R31 */ | |
e0c7dfe7 | 4407 | TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */ |
56b6aeb0 HMH |
4408 | "_EJ0", /* all others */ |
4409 | ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */ | |
e0c7dfe7 | 4410 | TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */ |
56b6aeb0 HMH |
4411 | "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */ |
4412 | ); /* all others */ | |
e0c7dfe7 | 4413 | TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */ |
56b6aeb0 HMH |
4414 | "_EJ0", /* 770x */ |
4415 | ); /* all others */ | |
4416 | ||
a5763f22 | 4417 | static int __init bay_init(struct ibm_init_struct *iibm) |
1da177e4 | 4418 | { |
fe08bc4b HMH |
4419 | vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n"); |
4420 | ||
e0c7dfe7 | 4421 | TPACPI_ACPIHANDLE_INIT(bay); |
5fba344c | 4422 | if (bay_handle) |
e0c7dfe7 HMH |
4423 | TPACPI_ACPIHANDLE_INIT(bay_ej); |
4424 | TPACPI_ACPIHANDLE_INIT(bay2); | |
5fba344c | 4425 | if (bay2_handle) |
e0c7dfe7 | 4426 | TPACPI_ACPIHANDLE_INIT(bay2_ej); |
5fba344c | 4427 | |
d8fd94d9 HMH |
4428 | tp_features.bay_status = bay_handle && |
4429 | acpi_evalf(bay_handle, NULL, "_STA", "qv"); | |
4430 | tp_features.bay_status2 = bay2_handle && | |
4431 | acpi_evalf(bay2_handle, NULL, "_STA", "qv"); | |
78f81cc4 | 4432 | |
d8fd94d9 HMH |
4433 | tp_features.bay_eject = bay_handle && bay_ej_handle && |
4434 | (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental); | |
4435 | tp_features.bay_eject2 = bay2_handle && bay2_ej_handle && | |
4436 | (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental); | |
1da177e4 | 4437 | |
fe08bc4b HMH |
4438 | vdbg_printk(TPACPI_DBG_INIT, |
4439 | "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n", | |
d8fd94d9 HMH |
4440 | str_supported(tp_features.bay_status), |
4441 | str_supported(tp_features.bay_eject), | |
4442 | str_supported(tp_features.bay_status2), | |
4443 | str_supported(tp_features.bay_eject2)); | |
fe08bc4b | 4444 | |
d8fd94d9 HMH |
4445 | return (tp_features.bay_status || tp_features.bay_eject || |
4446 | tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1; | |
1da177e4 LT |
4447 | } |
4448 | ||
56b6aeb0 HMH |
4449 | static void bay_notify(struct ibm_struct *ibm, u32 event) |
4450 | { | |
14e04fb3 | 4451 | acpi_bus_generate_proc_event(ibm->acpi->device, event, 0); |
962ce8ca | 4452 | acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class, |
e0b36fc5 | 4453 | dev_name(&ibm->acpi->device->dev), |
962ce8ca | 4454 | event, 0); |
56b6aeb0 HMH |
4455 | } |
4456 | ||
78f81cc4 BD |
4457 | #define bay_occupied(b) (_sta(b##_handle) & 1) |
4458 | ||
4459 | static int bay_read(char *p) | |
1da177e4 LT |
4460 | { |
4461 | int len = 0; | |
78f81cc4 BD |
4462 | int occupied = bay_occupied(bay); |
4463 | int occupied2 = bay_occupied(bay2); | |
4464 | int eject, eject2; | |
4465 | ||
d8fd94d9 HMH |
4466 | len += sprintf(p + len, "status:\t\t%s\n", |
4467 | tp_features.bay_status ? | |
4468 | (occupied ? "occupied" : "unoccupied") : | |
4469 | "not supported"); | |
4470 | if (tp_features.bay_status2) | |
78f81cc4 BD |
4471 | len += sprintf(p + len, "status2:\t%s\n", occupied2 ? |
4472 | "occupied" : "unoccupied"); | |
4473 | ||
d8fd94d9 HMH |
4474 | eject = tp_features.bay_eject && occupied; |
4475 | eject2 = tp_features.bay_eject2 && occupied2; | |
78f81cc4 BD |
4476 | |
4477 | if (eject && eject2) | |
4478 | len += sprintf(p + len, "commands:\teject, eject2\n"); | |
4479 | else if (eject) | |
1da177e4 | 4480 | len += sprintf(p + len, "commands:\teject\n"); |
78f81cc4 BD |
4481 | else if (eject2) |
4482 | len += sprintf(p + len, "commands:\teject2\n"); | |
1da177e4 LT |
4483 | |
4484 | return len; | |
4485 | } | |
4486 | ||
78f81cc4 | 4487 | static int bay_write(char *buf) |
1da177e4 LT |
4488 | { |
4489 | char *cmd; | |
4490 | ||
d8fd94d9 | 4491 | if (!tp_features.bay_eject && !tp_features.bay_eject2) |
78f81cc4 BD |
4492 | return -ENODEV; |
4493 | ||
1da177e4 | 4494 | while ((cmd = next_cmd(&buf))) { |
d8fd94d9 | 4495 | if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) { |
78f81cc4 BD |
4496 | if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1)) |
4497 | return -EIO; | |
d8fd94d9 | 4498 | } else if (tp_features.bay_eject2 && |
78f81cc4 BD |
4499 | strlencmp(cmd, "eject2") == 0) { |
4500 | if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1)) | |
1da177e4 LT |
4501 | return -EIO; |
4502 | } else | |
4503 | return -EINVAL; | |
4504 | } | |
4505 | ||
4506 | return 0; | |
78f81cc4 | 4507 | } |
a5763f22 | 4508 | |
8d376cd6 HMH |
4509 | static struct tp_acpi_drv_struct ibm_bay_acpidriver = { |
4510 | .notify = bay_notify, | |
4511 | .handle = &bay_handle, | |
4512 | .type = ACPI_SYSTEM_NOTIFY, | |
4513 | }; | |
4514 | ||
a5763f22 HMH |
4515 | static struct ibm_struct bay_driver_data = { |
4516 | .name = "bay", | |
4517 | .read = bay_read, | |
4518 | .write = bay_write, | |
8d376cd6 | 4519 | .acpi = &ibm_bay_acpidriver, |
a5763f22 HMH |
4520 | }; |
4521 | ||
85998248 | 4522 | #endif /* CONFIG_THINKPAD_ACPI_BAY */ |
1da177e4 | 4523 | |
56b6aeb0 HMH |
4524 | /************************************************************************* |
4525 | * CMOS subdriver | |
4526 | */ | |
4527 | ||
b616004c HMH |
4528 | /* sysfs cmos_command -------------------------------------------------- */ |
4529 | static ssize_t cmos_command_store(struct device *dev, | |
4530 | struct device_attribute *attr, | |
4531 | const char *buf, size_t count) | |
4532 | { | |
4533 | unsigned long cmos_cmd; | |
4534 | int res; | |
4535 | ||
4536 | if (parse_strtoul(buf, 21, &cmos_cmd)) | |
4537 | return -EINVAL; | |
4538 | ||
4539 | res = issue_thinkpad_cmos_command(cmos_cmd); | |
4540 | return (res)? res : count; | |
4541 | } | |
4542 | ||
4543 | static struct device_attribute dev_attr_cmos_command = | |
4544 | __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store); | |
4545 | ||
4546 | /* --------------------------------------------------------------------- */ | |
4547 | ||
a5763f22 | 4548 | static int __init cmos_init(struct ibm_init_struct *iibm) |
5fba344c | 4549 | { |
b616004c HMH |
4550 | int res; |
4551 | ||
fe08bc4b HMH |
4552 | vdbg_printk(TPACPI_DBG_INIT, |
4553 | "initializing cmos commands subdriver\n"); | |
4554 | ||
e0c7dfe7 | 4555 | TPACPI_ACPIHANDLE_INIT(cmos); |
5fba344c | 4556 | |
fe08bc4b HMH |
4557 | vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n", |
4558 | str_supported(cmos_handle != NULL)); | |
b616004c HMH |
4559 | |
4560 | res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); | |
4561 | if (res) | |
4562 | return res; | |
4563 | ||
5fba344c HMH |
4564 | return (cmos_handle)? 0 : 1; |
4565 | } | |
4566 | ||
b616004c HMH |
4567 | static void cmos_exit(void) |
4568 | { | |
4569 | device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); | |
4570 | } | |
4571 | ||
78f81cc4 | 4572 | static int cmos_read(char *p) |
1da177e4 LT |
4573 | { |
4574 | int len = 0; | |
4575 | ||
78f81cc4 BD |
4576 | /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
4577 | R30, R31, T20-22, X20-21 */ | |
1da177e4 LT |
4578 | if (!cmos_handle) |
4579 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
4580 | else { | |
4581 | len += sprintf(p + len, "status:\t\tsupported\n"); | |
78f81cc4 | 4582 | len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n"); |
1da177e4 LT |
4583 | } |
4584 | ||
4585 | return len; | |
4586 | } | |
4587 | ||
78f81cc4 | 4588 | static int cmos_write(char *buf) |
1da177e4 LT |
4589 | { |
4590 | char *cmd; | |
c9bea99c | 4591 | int cmos_cmd, res; |
1da177e4 LT |
4592 | |
4593 | while ((cmd = next_cmd(&buf))) { | |
78f81cc4 BD |
4594 | if (sscanf(cmd, "%u", &cmos_cmd) == 1 && |
4595 | cmos_cmd >= 0 && cmos_cmd <= 21) { | |
1da177e4 LT |
4596 | /* cmos_cmd set */ |
4597 | } else | |
4598 | return -EINVAL; | |
4599 | ||
c9bea99c HMH |
4600 | res = issue_thinkpad_cmos_command(cmos_cmd); |
4601 | if (res) | |
4602 | return res; | |
1da177e4 LT |
4603 | } |
4604 | ||
4605 | return 0; | |
78f81cc4 BD |
4606 | } |
4607 | ||
a5763f22 HMH |
4608 | static struct ibm_struct cmos_driver_data = { |
4609 | .name = "cmos", | |
4610 | .read = cmos_read, | |
4611 | .write = cmos_write, | |
b616004c | 4612 | .exit = cmos_exit, |
a5763f22 | 4613 | }; |
56b6aeb0 HMH |
4614 | |
4615 | /************************************************************************* | |
4616 | * LED subdriver | |
4617 | */ | |
4618 | ||
f74a27d4 HMH |
4619 | enum led_access_mode { |
4620 | TPACPI_LED_NONE = 0, | |
4621 | TPACPI_LED_570, /* 570 */ | |
4622 | TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ | |
4623 | TPACPI_LED_NEW, /* all others */ | |
4624 | }; | |
4625 | ||
4626 | enum { /* For TPACPI_LED_OLD */ | |
4627 | TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */ | |
4628 | TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */ | |
4629 | TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */ | |
4630 | }; | |
4631 | ||
4fa6811b HMH |
4632 | enum led_status_t { |
4633 | TPACPI_LED_OFF = 0, | |
4634 | TPACPI_LED_ON, | |
4635 | TPACPI_LED_BLINK, | |
4636 | }; | |
4637 | ||
9a8e1738 | 4638 | static enum led_access_mode led_supported; |
78f81cc4 | 4639 | |
e0c7dfe7 | 4640 | TPACPI_HANDLE(led, ec, "SLED", /* 570 */ |
35ff8b9f HMH |
4641 | "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */ |
4642 | /* T20-22, X20-21 */ | |
56b6aeb0 HMH |
4643 | "LED", /* all others */ |
4644 | ); /* R30, R31 */ | |
4645 | ||
af116101 HMH |
4646 | #define TPACPI_LED_NUMLEDS 8 |
4647 | static struct tpacpi_led_classdev *tpacpi_leds; | |
4648 | static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS]; | |
e3aa51fe | 4649 | static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = { |
af116101 HMH |
4650 | /* there's a limit of 19 chars + NULL before 2.6.26 */ |
4651 | "tpacpi::power", | |
4652 | "tpacpi:orange:batt", | |
4653 | "tpacpi:green:batt", | |
4654 | "tpacpi::dock_active", | |
4655 | "tpacpi::bay_active", | |
4656 | "tpacpi::dock_batt", | |
4657 | "tpacpi::unknown_led", | |
4658 | "tpacpi::standby", | |
4659 | }; | |
4660 | ||
24e45bbe | 4661 | static int led_get_status(const unsigned int led) |
4fa6811b HMH |
4662 | { |
4663 | int status; | |
af116101 | 4664 | enum led_status_t led_s; |
4fa6811b HMH |
4665 | |
4666 | switch (led_supported) { | |
4667 | case TPACPI_LED_570: | |
4668 | if (!acpi_evalf(ec_handle, | |
4669 | &status, "GLED", "dd", 1 << led)) | |
4670 | return -EIO; | |
af116101 | 4671 | led_s = (status == 0)? |
4fa6811b HMH |
4672 | TPACPI_LED_OFF : |
4673 | ((status == 1)? | |
4674 | TPACPI_LED_ON : | |
4675 | TPACPI_LED_BLINK); | |
af116101 HMH |
4676 | tpacpi_led_state_cache[led] = led_s; |
4677 | return led_s; | |
4fa6811b HMH |
4678 | default: |
4679 | return -ENXIO; | |
4680 | } | |
4681 | ||
4682 | /* not reached */ | |
4683 | } | |
4684 | ||
24e45bbe HMH |
4685 | static int led_set_status(const unsigned int led, |
4686 | const enum led_status_t ledstatus) | |
4fa6811b HMH |
4687 | { |
4688 | /* off, on, blink. Index is led_status_t */ | |
24e45bbe HMH |
4689 | static const unsigned int led_sled_arg1[] = { 0, 1, 3 }; |
4690 | static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 }; | |
4fa6811b HMH |
4691 | |
4692 | int rc = 0; | |
4693 | ||
4694 | switch (led_supported) { | |
4695 | case TPACPI_LED_570: | |
24e45bbe HMH |
4696 | /* 570 */ |
4697 | if (led > 7) | |
4698 | return -EINVAL; | |
4699 | if (!acpi_evalf(led_handle, NULL, NULL, "vdd", | |
4700 | (1 << led), led_sled_arg1[ledstatus])) | |
4701 | rc = -EIO; | |
4702 | break; | |
4fa6811b | 4703 | case TPACPI_LED_OLD: |
24e45bbe HMH |
4704 | /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */ |
4705 | if (led > 7) | |
4706 | return -EINVAL; | |
4707 | rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led)); | |
4708 | if (rc >= 0) | |
4709 | rc = ec_write(TPACPI_LED_EC_HLBL, | |
4710 | (ledstatus == TPACPI_LED_BLINK) << led); | |
4711 | if (rc >= 0) | |
4712 | rc = ec_write(TPACPI_LED_EC_HLCL, | |
4713 | (ledstatus != TPACPI_LED_OFF) << led); | |
4714 | break; | |
4fa6811b | 4715 | case TPACPI_LED_NEW: |
24e45bbe HMH |
4716 | /* all others */ |
4717 | if (!acpi_evalf(led_handle, NULL, NULL, "vdd", | |
4718 | led, led_led_arg1[ledstatus])) | |
4719 | rc = -EIO; | |
4720 | break; | |
4fa6811b HMH |
4721 | default: |
4722 | rc = -ENXIO; | |
4723 | } | |
4724 | ||
af116101 HMH |
4725 | if (!rc) |
4726 | tpacpi_led_state_cache[led] = ledstatus; | |
4727 | ||
4728 | return rc; | |
4729 | } | |
4730 | ||
4731 | static void led_sysfs_set_status(unsigned int led, | |
4732 | enum led_brightness brightness) | |
4733 | { | |
4734 | led_set_status(led, | |
4735 | (brightness == LED_OFF) ? | |
4736 | TPACPI_LED_OFF : | |
4737 | (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ? | |
4738 | TPACPI_LED_BLINK : TPACPI_LED_ON); | |
4739 | } | |
4740 | ||
4741 | static void led_set_status_worker(struct work_struct *work) | |
4742 | { | |
4743 | struct tpacpi_led_classdev *data = | |
4744 | container_of(work, struct tpacpi_led_classdev, work); | |
4745 | ||
4746 | if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING)) | |
4747 | led_sysfs_set_status(data->led, data->new_brightness); | |
4748 | } | |
4749 | ||
4750 | static void led_sysfs_set(struct led_classdev *led_cdev, | |
4751 | enum led_brightness brightness) | |
4752 | { | |
4753 | struct tpacpi_led_classdev *data = container_of(led_cdev, | |
4754 | struct tpacpi_led_classdev, led_classdev); | |
4755 | ||
4756 | data->new_brightness = brightness; | |
e0e3c061 | 4757 | queue_work(tpacpi_wq, &data->work); |
af116101 HMH |
4758 | } |
4759 | ||
4760 | static int led_sysfs_blink_set(struct led_classdev *led_cdev, | |
4761 | unsigned long *delay_on, unsigned long *delay_off) | |
4762 | { | |
4763 | struct tpacpi_led_classdev *data = container_of(led_cdev, | |
4764 | struct tpacpi_led_classdev, led_classdev); | |
4765 | ||
4766 | /* Can we choose the flash rate? */ | |
4767 | if (*delay_on == 0 && *delay_off == 0) { | |
4768 | /* yes. set them to the hardware blink rate (1 Hz) */ | |
4769 | *delay_on = 500; /* ms */ | |
4770 | *delay_off = 500; /* ms */ | |
4771 | } else if ((*delay_on != 500) || (*delay_off != 500)) | |
4772 | return -EINVAL; | |
4773 | ||
4774 | data->new_brightness = TPACPI_LED_BLINK; | |
e0e3c061 | 4775 | queue_work(tpacpi_wq, &data->work); |
af116101 HMH |
4776 | |
4777 | return 0; | |
4778 | } | |
4779 | ||
4780 | static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev) | |
4781 | { | |
4782 | int rc; | |
4783 | ||
4784 | struct tpacpi_led_classdev *data = container_of(led_cdev, | |
4785 | struct tpacpi_led_classdev, led_classdev); | |
4786 | ||
4787 | rc = led_get_status(data->led); | |
4788 | ||
4789 | if (rc == TPACPI_LED_OFF || rc < 0) | |
4790 | rc = LED_OFF; /* no error handling in led class :( */ | |
4791 | else | |
4792 | rc = LED_FULL; | |
4793 | ||
4fa6811b HMH |
4794 | return rc; |
4795 | } | |
4796 | ||
af116101 HMH |
4797 | static void led_exit(void) |
4798 | { | |
4799 | unsigned int i; | |
4800 | ||
4801 | for (i = 0; i < TPACPI_LED_NUMLEDS; i++) { | |
4802 | if (tpacpi_leds[i].led_classdev.name) | |
4803 | led_classdev_unregister(&tpacpi_leds[i].led_classdev); | |
4804 | } | |
4805 | ||
4806 | kfree(tpacpi_leds); | |
af116101 HMH |
4807 | } |
4808 | ||
a5763f22 | 4809 | static int __init led_init(struct ibm_init_struct *iibm) |
78f81cc4 | 4810 | { |
af116101 HMH |
4811 | unsigned int i; |
4812 | int rc; | |
4813 | ||
fe08bc4b HMH |
4814 | vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n"); |
4815 | ||
e0c7dfe7 | 4816 | TPACPI_ACPIHANDLE_INIT(led); |
5fba344c | 4817 | |
78f81cc4 BD |
4818 | if (!led_handle) |
4819 | /* led not supported on R30, R31 */ | |
efa27145 | 4820 | led_supported = TPACPI_LED_NONE; |
78f81cc4 BD |
4821 | else if (strlencmp(led_path, "SLED") == 0) |
4822 | /* 570 */ | |
efa27145 | 4823 | led_supported = TPACPI_LED_570; |
78f81cc4 BD |
4824 | else if (strlencmp(led_path, "SYSL") == 0) |
4825 | /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ | |
efa27145 | 4826 | led_supported = TPACPI_LED_OLD; |
78f81cc4 BD |
4827 | else |
4828 | /* all others */ | |
efa27145 | 4829 | led_supported = TPACPI_LED_NEW; |
78f81cc4 | 4830 | |
fe08bc4b HMH |
4831 | vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n", |
4832 | str_supported(led_supported), led_supported); | |
4833 | ||
af116101 HMH |
4834 | tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS, |
4835 | GFP_KERNEL); | |
4836 | if (!tpacpi_leds) { | |
4837 | printk(TPACPI_ERR "Out of memory for LED data\n"); | |
4838 | return -ENOMEM; | |
4839 | } | |
4840 | ||
4841 | for (i = 0; i < TPACPI_LED_NUMLEDS; i++) { | |
4842 | tpacpi_leds[i].led = i; | |
4843 | ||
4844 | tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set; | |
4845 | tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set; | |
4846 | if (led_supported == TPACPI_LED_570) | |
4847 | tpacpi_leds[i].led_classdev.brightness_get = | |
4848 | &led_sysfs_get; | |
4849 | ||
4850 | tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i]; | |
4851 | ||
4852 | INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker); | |
4853 | ||
4854 | rc = led_classdev_register(&tpacpi_pdev->dev, | |
4855 | &tpacpi_leds[i].led_classdev); | |
4856 | if (rc < 0) { | |
4857 | tpacpi_leds[i].led_classdev.name = NULL; | |
4858 | led_exit(); | |
4859 | return rc; | |
4860 | } | |
4861 | } | |
4862 | ||
5fba344c | 4863 | return (led_supported != TPACPI_LED_NONE)? 0 : 1; |
78f81cc4 BD |
4864 | } |
4865 | ||
4fa6811b HMH |
4866 | #define str_led_status(s) \ |
4867 | ((s) == TPACPI_LED_OFF ? "off" : \ | |
4868 | ((s) == TPACPI_LED_ON ? "on" : "blinking")) | |
78f81cc4 BD |
4869 | |
4870 | static int led_read(char *p) | |
1da177e4 LT |
4871 | { |
4872 | int len = 0; | |
4873 | ||
78f81cc4 BD |
4874 | if (!led_supported) { |
4875 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
4876 | return len; | |
4877 | } | |
4878 | len += sprintf(p + len, "status:\t\tsupported\n"); | |
4879 | ||
efa27145 | 4880 | if (led_supported == TPACPI_LED_570) { |
78f81cc4 BD |
4881 | /* 570 */ |
4882 | int i, status; | |
4883 | for (i = 0; i < 8; i++) { | |
4fa6811b HMH |
4884 | status = led_get_status(i); |
4885 | if (status < 0) | |
78f81cc4 BD |
4886 | return -EIO; |
4887 | len += sprintf(p + len, "%d:\t\t%s\n", | |
4fa6811b | 4888 | i, str_led_status(status)); |
78f81cc4 BD |
4889 | } |
4890 | } | |
4891 | ||
1da177e4 | 4892 | len += sprintf(p + len, "commands:\t" |
78f81cc4 | 4893 | "<led> on, <led> off, <led> blink (<led> is 0-7)\n"); |
1da177e4 LT |
4894 | |
4895 | return len; | |
4896 | } | |
4897 | ||
78f81cc4 | 4898 | static int led_write(char *buf) |
1da177e4 LT |
4899 | { |
4900 | char *cmd; | |
4fa6811b HMH |
4901 | int led, rc; |
4902 | enum led_status_t s; | |
78f81cc4 BD |
4903 | |
4904 | if (!led_supported) | |
4905 | return -ENODEV; | |
1da177e4 LT |
4906 | |
4907 | while ((cmd = next_cmd(&buf))) { | |
78f81cc4 | 4908 | if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7) |
1da177e4 LT |
4909 | return -EINVAL; |
4910 | ||
78f81cc4 | 4911 | if (strstr(cmd, "off")) { |
4fa6811b | 4912 | s = TPACPI_LED_OFF; |
1da177e4 | 4913 | } else if (strstr(cmd, "on")) { |
4fa6811b | 4914 | s = TPACPI_LED_ON; |
78f81cc4 | 4915 | } else if (strstr(cmd, "blink")) { |
4fa6811b | 4916 | s = TPACPI_LED_BLINK; |
78f81cc4 | 4917 | } else { |
4fa6811b | 4918 | return -EINVAL; |
78f81cc4 | 4919 | } |
4fa6811b HMH |
4920 | |
4921 | rc = led_set_status(led, s); | |
4922 | if (rc < 0) | |
4923 | return rc; | |
78f81cc4 BD |
4924 | } |
4925 | ||
4926 | return 0; | |
4927 | } | |
4928 | ||
a5763f22 HMH |
4929 | static struct ibm_struct led_driver_data = { |
4930 | .name = "led", | |
4931 | .read = led_read, | |
4932 | .write = led_write, | |
af116101 | 4933 | .exit = led_exit, |
a5763f22 HMH |
4934 | }; |
4935 | ||
56b6aeb0 HMH |
4936 | /************************************************************************* |
4937 | * Beep subdriver | |
4938 | */ | |
4939 | ||
e0c7dfe7 | 4940 | TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */ |
56b6aeb0 | 4941 | |
a5763f22 | 4942 | static int __init beep_init(struct ibm_init_struct *iibm) |
5fba344c | 4943 | { |
fe08bc4b HMH |
4944 | vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n"); |
4945 | ||
e0c7dfe7 | 4946 | TPACPI_ACPIHANDLE_INIT(beep); |
5fba344c | 4947 | |
fe08bc4b HMH |
4948 | vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n", |
4949 | str_supported(beep_handle != NULL)); | |
4950 | ||
5fba344c HMH |
4951 | return (beep_handle)? 0 : 1; |
4952 | } | |
4953 | ||
78f81cc4 BD |
4954 | static int beep_read(char *p) |
4955 | { | |
4956 | int len = 0; | |
4957 | ||
4958 | if (!beep_handle) | |
4959 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
4960 | else { | |
4961 | len += sprintf(p + len, "status:\t\tsupported\n"); | |
4962 | len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n"); | |
4963 | } | |
4964 | ||
4965 | return len; | |
4966 | } | |
4967 | ||
4968 | static int beep_write(char *buf) | |
4969 | { | |
4970 | char *cmd; | |
4971 | int beep_cmd; | |
4972 | ||
4973 | if (!beep_handle) | |
4974 | return -ENODEV; | |
4975 | ||
4976 | while ((cmd = next_cmd(&buf))) { | |
4977 | if (sscanf(cmd, "%u", &beep_cmd) == 1 && | |
4978 | beep_cmd >= 0 && beep_cmd <= 17) { | |
4979 | /* beep_cmd set */ | |
4980 | } else | |
4981 | return -EINVAL; | |
4982 | if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0)) | |
4983 | return -EIO; | |
4984 | } | |
4985 | ||
4986 | return 0; | |
4987 | } | |
4988 | ||
a5763f22 HMH |
4989 | static struct ibm_struct beep_driver_data = { |
4990 | .name = "beep", | |
4991 | .read = beep_read, | |
4992 | .write = beep_write, | |
4993 | }; | |
4994 | ||
56b6aeb0 HMH |
4995 | /************************************************************************* |
4996 | * Thermal subdriver | |
4997 | */ | |
78f81cc4 | 4998 | |
f74a27d4 HMH |
4999 | enum thermal_access_mode { |
5000 | TPACPI_THERMAL_NONE = 0, /* No thermal support */ | |
5001 | TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */ | |
5002 | TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */ | |
5003 | TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */ | |
5004 | TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */ | |
5005 | }; | |
5006 | ||
5007 | enum { /* TPACPI_THERMAL_TPEC_* */ | |
5008 | TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */ | |
5009 | TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */ | |
5010 | TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */ | |
5011 | }; | |
5012 | ||
5013 | #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */ | |
5014 | struct ibm_thermal_sensors_struct { | |
5015 | s32 temp[TPACPI_MAX_THERMAL_SENSORS]; | |
5016 | }; | |
5017 | ||
a26f878a | 5018 | static enum thermal_access_mode thermal_read_mode; |
78f81cc4 | 5019 | |
b21a15f6 HMH |
5020 | /* idx is zero-based */ |
5021 | static int thermal_get_sensor(int idx, s32 *value) | |
5022 | { | |
5023 | int t; | |
5024 | s8 tmp; | |
5025 | char tmpi[5]; | |
5026 | ||
5027 | t = TP_EC_THERMAL_TMP0; | |
5028 | ||
5029 | switch (thermal_read_mode) { | |
5030 | #if TPACPI_MAX_THERMAL_SENSORS >= 16 | |
5031 | case TPACPI_THERMAL_TPEC_16: | |
5032 | if (idx >= 8 && idx <= 15) { | |
5033 | t = TP_EC_THERMAL_TMP8; | |
5034 | idx -= 8; | |
5035 | } | |
5036 | /* fallthrough */ | |
5037 | #endif | |
5038 | case TPACPI_THERMAL_TPEC_8: | |
5039 | if (idx <= 7) { | |
5040 | if (!acpi_ec_read(t + idx, &tmp)) | |
5041 | return -EIO; | |
5042 | *value = tmp * 1000; | |
5043 | return 0; | |
5044 | } | |
5045 | break; | |
5046 | ||
5047 | case TPACPI_THERMAL_ACPI_UPDT: | |
5048 | if (idx <= 7) { | |
5049 | snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); | |
5050 | if (!acpi_evalf(ec_handle, NULL, "UPDT", "v")) | |
5051 | return -EIO; | |
5052 | if (!acpi_evalf(ec_handle, &t, tmpi, "d")) | |
5053 | return -EIO; | |
5054 | *value = (t - 2732) * 100; | |
5055 | return 0; | |
5056 | } | |
5057 | break; | |
5058 | ||
5059 | case TPACPI_THERMAL_ACPI_TMP07: | |
5060 | if (idx <= 7) { | |
5061 | snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); | |
5062 | if (!acpi_evalf(ec_handle, &t, tmpi, "d")) | |
5063 | return -EIO; | |
5064 | if (t > 127 || t < -127) | |
5065 | t = TP_EC_THERMAL_TMP_NA; | |
5066 | *value = t * 1000; | |
5067 | return 0; | |
5068 | } | |
5069 | break; | |
5070 | ||
5071 | case TPACPI_THERMAL_NONE: | |
5072 | default: | |
5073 | return -ENOSYS; | |
5074 | } | |
5075 | ||
5076 | return -EINVAL; | |
5077 | } | |
5078 | ||
5079 | static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s) | |
5080 | { | |
5081 | int res, i; | |
5082 | int n; | |
5083 | ||
5084 | n = 8; | |
5085 | i = 0; | |
5086 | ||
5087 | if (!s) | |
5088 | return -EINVAL; | |
5089 | ||
5090 | if (thermal_read_mode == TPACPI_THERMAL_TPEC_16) | |
5091 | n = 16; | |
5092 | ||
35ff8b9f | 5093 | for (i = 0 ; i < n; i++) { |
b21a15f6 HMH |
5094 | res = thermal_get_sensor(i, &s->temp[i]); |
5095 | if (res) | |
5096 | return res; | |
5097 | } | |
5098 | ||
5099 | return n; | |
5100 | } | |
f74a27d4 | 5101 | |
2c37aa4e HMH |
5102 | /* sysfs temp##_input -------------------------------------------------- */ |
5103 | ||
5104 | static ssize_t thermal_temp_input_show(struct device *dev, | |
5105 | struct device_attribute *attr, | |
5106 | char *buf) | |
5107 | { | |
5108 | struct sensor_device_attribute *sensor_attr = | |
5109 | to_sensor_dev_attr(attr); | |
5110 | int idx = sensor_attr->index; | |
5111 | s32 value; | |
5112 | int res; | |
5113 | ||
5114 | res = thermal_get_sensor(idx, &value); | |
5115 | if (res) | |
5116 | return res; | |
5117 | if (value == TP_EC_THERMAL_TMP_NA * 1000) | |
5118 | return -ENXIO; | |
5119 | ||
5120 | return snprintf(buf, PAGE_SIZE, "%d\n", value); | |
5121 | } | |
5122 | ||
5123 | #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \ | |
35ff8b9f HMH |
5124 | SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \ |
5125 | thermal_temp_input_show, NULL, _idxB) | |
2c37aa4e HMH |
5126 | |
5127 | static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = { | |
5128 | THERMAL_SENSOR_ATTR_TEMP(1, 0), | |
5129 | THERMAL_SENSOR_ATTR_TEMP(2, 1), | |
5130 | THERMAL_SENSOR_ATTR_TEMP(3, 2), | |
5131 | THERMAL_SENSOR_ATTR_TEMP(4, 3), | |
5132 | THERMAL_SENSOR_ATTR_TEMP(5, 4), | |
5133 | THERMAL_SENSOR_ATTR_TEMP(6, 5), | |
5134 | THERMAL_SENSOR_ATTR_TEMP(7, 6), | |
5135 | THERMAL_SENSOR_ATTR_TEMP(8, 7), | |
5136 | THERMAL_SENSOR_ATTR_TEMP(9, 8), | |
5137 | THERMAL_SENSOR_ATTR_TEMP(10, 9), | |
5138 | THERMAL_SENSOR_ATTR_TEMP(11, 10), | |
5139 | THERMAL_SENSOR_ATTR_TEMP(12, 11), | |
5140 | THERMAL_SENSOR_ATTR_TEMP(13, 12), | |
5141 | THERMAL_SENSOR_ATTR_TEMP(14, 13), | |
5142 | THERMAL_SENSOR_ATTR_TEMP(15, 14), | |
5143 | THERMAL_SENSOR_ATTR_TEMP(16, 15), | |
5144 | }; | |
5145 | ||
5146 | #define THERMAL_ATTRS(X) \ | |
5147 | &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr | |
5148 | ||
5149 | static struct attribute *thermal_temp_input_attr[] = { | |
5150 | THERMAL_ATTRS(8), | |
5151 | THERMAL_ATTRS(9), | |
5152 | THERMAL_ATTRS(10), | |
5153 | THERMAL_ATTRS(11), | |
5154 | THERMAL_ATTRS(12), | |
5155 | THERMAL_ATTRS(13), | |
5156 | THERMAL_ATTRS(14), | |
5157 | THERMAL_ATTRS(15), | |
5158 | THERMAL_ATTRS(0), | |
5159 | THERMAL_ATTRS(1), | |
5160 | THERMAL_ATTRS(2), | |
5161 | THERMAL_ATTRS(3), | |
5162 | THERMAL_ATTRS(4), | |
5163 | THERMAL_ATTRS(5), | |
5164 | THERMAL_ATTRS(6), | |
5165 | THERMAL_ATTRS(7), | |
5166 | NULL | |
5167 | }; | |
5168 | ||
5169 | static const struct attribute_group thermal_temp_input16_group = { | |
5170 | .attrs = thermal_temp_input_attr | |
5171 | }; | |
5172 | ||
5173 | static const struct attribute_group thermal_temp_input8_group = { | |
5174 | .attrs = &thermal_temp_input_attr[8] | |
5175 | }; | |
5176 | ||
5177 | #undef THERMAL_SENSOR_ATTR_TEMP | |
5178 | #undef THERMAL_ATTRS | |
5179 | ||
5180 | /* --------------------------------------------------------------------- */ | |
5181 | ||
a5763f22 | 5182 | static int __init thermal_init(struct ibm_init_struct *iibm) |
78f81cc4 | 5183 | { |
60eb0b35 HMH |
5184 | u8 t, ta1, ta2; |
5185 | int i; | |
5fba344c | 5186 | int acpi_tmp7; |
2c37aa4e | 5187 | int res; |
5fba344c | 5188 | |
fe08bc4b HMH |
5189 | vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n"); |
5190 | ||
5fba344c | 5191 | acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv"); |
60eb0b35 | 5192 | |
3d6f99ca | 5193 | if (thinkpad_id.ec_model) { |
60eb0b35 HMH |
5194 | /* |
5195 | * Direct EC access mode: sensors at registers | |
5196 | * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for | |
5197 | * non-implemented, thermal sensors return 0x80 when | |
5198 | * not available | |
5199 | */ | |
78f81cc4 | 5200 | |
60eb0b35 HMH |
5201 | ta1 = ta2 = 0; |
5202 | for (i = 0; i < 8; i++) { | |
04cc862c | 5203 | if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) { |
60eb0b35 HMH |
5204 | ta1 |= t; |
5205 | } else { | |
5206 | ta1 = 0; | |
5207 | break; | |
5208 | } | |
04cc862c | 5209 | if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) { |
60eb0b35 HMH |
5210 | ta2 |= t; |
5211 | } else { | |
5212 | ta1 = 0; | |
5213 | break; | |
5214 | } | |
5215 | } | |
5216 | if (ta1 == 0) { | |
5217 | /* This is sheer paranoia, but we handle it anyway */ | |
5218 | if (acpi_tmp7) { | |
e0c7dfe7 | 5219 | printk(TPACPI_ERR |
60eb0b35 | 5220 | "ThinkPad ACPI EC access misbehaving, " |
35ff8b9f HMH |
5221 | "falling back to ACPI TMPx access " |
5222 | "mode\n"); | |
efa27145 | 5223 | thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; |
60eb0b35 | 5224 | } else { |
e0c7dfe7 | 5225 | printk(TPACPI_ERR |
60eb0b35 HMH |
5226 | "ThinkPad ACPI EC access misbehaving, " |
5227 | "disabling thermal sensors access\n"); | |
efa27145 | 5228 | thermal_read_mode = TPACPI_THERMAL_NONE; |
60eb0b35 HMH |
5229 | } |
5230 | } else { | |
5231 | thermal_read_mode = | |
5232 | (ta2 != 0) ? | |
efa27145 | 5233 | TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8; |
60eb0b35 HMH |
5234 | } |
5235 | } else if (acpi_tmp7) { | |
a26f878a HMH |
5236 | if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) { |
5237 | /* 600e/x, 770e, 770x */ | |
efa27145 | 5238 | thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT; |
a26f878a HMH |
5239 | } else { |
5240 | /* Standard ACPI TMPx access, max 8 sensors */ | |
efa27145 | 5241 | thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; |
a26f878a HMH |
5242 | } |
5243 | } else { | |
5244 | /* temperatures not supported on 570, G4x, R30, R31, R32 */ | |
efa27145 | 5245 | thermal_read_mode = TPACPI_THERMAL_NONE; |
a26f878a | 5246 | } |
78f81cc4 | 5247 | |
fe08bc4b HMH |
5248 | vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n", |
5249 | str_supported(thermal_read_mode != TPACPI_THERMAL_NONE), | |
5250 | thermal_read_mode); | |
5251 | ||
35ff8b9f | 5252 | switch (thermal_read_mode) { |
2c37aa4e | 5253 | case TPACPI_THERMAL_TPEC_16: |
7fd40029 | 5254 | res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj, |
2c37aa4e HMH |
5255 | &thermal_temp_input16_group); |
5256 | if (res) | |
5257 | return res; | |
5258 | break; | |
5259 | case TPACPI_THERMAL_TPEC_8: | |
5260 | case TPACPI_THERMAL_ACPI_TMP07: | |
5261 | case TPACPI_THERMAL_ACPI_UPDT: | |
7fd40029 | 5262 | res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj, |
2c37aa4e HMH |
5263 | &thermal_temp_input8_group); |
5264 | if (res) | |
5265 | return res; | |
5266 | break; | |
5267 | case TPACPI_THERMAL_NONE: | |
5268 | default: | |
5269 | return 1; | |
5270 | } | |
5271 | ||
5272 | return 0; | |
5273 | } | |
5274 | ||
5275 | static void thermal_exit(void) | |
5276 | { | |
35ff8b9f | 5277 | switch (thermal_read_mode) { |
2c37aa4e | 5278 | case TPACPI_THERMAL_TPEC_16: |
7fd40029 | 5279 | sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, |
2c37aa4e HMH |
5280 | &thermal_temp_input16_group); |
5281 | break; | |
5282 | case TPACPI_THERMAL_TPEC_8: | |
5283 | case TPACPI_THERMAL_ACPI_TMP07: | |
5284 | case TPACPI_THERMAL_ACPI_UPDT: | |
7fd40029 | 5285 | sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, |
2c37aa4e HMH |
5286 | &thermal_temp_input16_group); |
5287 | break; | |
5288 | case TPACPI_THERMAL_NONE: | |
5289 | default: | |
5290 | break; | |
5291 | } | |
78f81cc4 BD |
5292 | } |
5293 | ||
a26f878a HMH |
5294 | static int thermal_read(char *p) |
5295 | { | |
5296 | int len = 0; | |
5297 | int n, i; | |
5298 | struct ibm_thermal_sensors_struct t; | |
5299 | ||
5300 | n = thermal_get_sensors(&t); | |
5301 | if (unlikely(n < 0)) | |
5302 | return n; | |
5303 | ||
5304 | len += sprintf(p + len, "temperatures:\t"); | |
5305 | ||
5306 | if (n > 0) { | |
5307 | for (i = 0; i < (n - 1); i++) | |
5308 | len += sprintf(p + len, "%d ", t.temp[i] / 1000); | |
5309 | len += sprintf(p + len, "%d\n", t.temp[i] / 1000); | |
5310 | } else | |
5311 | len += sprintf(p + len, "not supported\n"); | |
78f81cc4 BD |
5312 | |
5313 | return len; | |
5314 | } | |
5315 | ||
a5763f22 HMH |
5316 | static struct ibm_struct thermal_driver_data = { |
5317 | .name = "thermal", | |
5318 | .read = thermal_read, | |
2c37aa4e | 5319 | .exit = thermal_exit, |
a5763f22 HMH |
5320 | }; |
5321 | ||
56b6aeb0 HMH |
5322 | /************************************************************************* |
5323 | * EC Dump subdriver | |
5324 | */ | |
5325 | ||
78f81cc4 BD |
5326 | static u8 ecdump_regs[256]; |
5327 | ||
5328 | static int ecdump_read(char *p) | |
5329 | { | |
5330 | int len = 0; | |
5331 | int i, j; | |
5332 | u8 v; | |
5333 | ||
5334 | len += sprintf(p + len, "EC " | |
5335 | " +00 +01 +02 +03 +04 +05 +06 +07" | |
5336 | " +08 +09 +0a +0b +0c +0d +0e +0f\n"); | |
5337 | for (i = 0; i < 256; i += 16) { | |
5338 | len += sprintf(p + len, "EC 0x%02x:", i); | |
5339 | for (j = 0; j < 16; j++) { | |
5340 | if (!acpi_ec_read(i + j, &v)) | |
5341 | break; | |
5342 | if (v != ecdump_regs[i + j]) | |
5343 | len += sprintf(p + len, " *%02x", v); | |
5344 | else | |
5345 | len += sprintf(p + len, " %02x", v); | |
5346 | ecdump_regs[i + j] = v; | |
5347 | } | |
5348 | len += sprintf(p + len, "\n"); | |
5349 | if (j != 16) | |
5350 | break; | |
5351 | } | |
5352 | ||
5353 | /* These are way too dangerous to advertise openly... */ | |
5354 | #if 0 | |
5355 | len += sprintf(p + len, "commands:\t0x<offset> 0x<value>" | |
5356 | " (<offset> is 00-ff, <value> is 00-ff)\n"); | |
5357 | len += sprintf(p + len, "commands:\t0x<offset> <value> " | |
5358 | " (<offset> is 00-ff, <value> is 0-255)\n"); | |
5359 | #endif | |
5360 | return len; | |
5361 | } | |
5362 | ||
5363 | static int ecdump_write(char *buf) | |
5364 | { | |
5365 | char *cmd; | |
5366 | int i, v; | |
5367 | ||
5368 | while ((cmd = next_cmd(&buf))) { | |
5369 | if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) { | |
5370 | /* i and v set */ | |
5371 | } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) { | |
5372 | /* i and v set */ | |
5373 | } else | |
5374 | return -EINVAL; | |
5375 | if (i >= 0 && i < 256 && v >= 0 && v < 256) { | |
5376 | if (!acpi_ec_write(i, v)) | |
1da177e4 LT |
5377 | return -EIO; |
5378 | } else | |
5379 | return -EINVAL; | |
5380 | } | |
5381 | ||
5382 | return 0; | |
78f81cc4 BD |
5383 | } |
5384 | ||
a5763f22 HMH |
5385 | static struct ibm_struct ecdump_driver_data = { |
5386 | .name = "ecdump", | |
5387 | .read = ecdump_read, | |
5388 | .write = ecdump_write, | |
92641177 | 5389 | .flags.experimental = 1, |
a5763f22 HMH |
5390 | }; |
5391 | ||
56b6aeb0 HMH |
5392 | /************************************************************************* |
5393 | * Backlight/brightness subdriver | |
5394 | */ | |
5395 | ||
f74a27d4 HMH |
5396 | #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen" |
5397 | ||
e11aecf1 HMH |
5398 | enum { |
5399 | TP_EC_BACKLIGHT = 0x31, | |
5400 | ||
5401 | /* TP_EC_BACKLIGHT bitmasks */ | |
5402 | TP_EC_BACKLIGHT_LVLMSK = 0x1F, | |
5403 | TP_EC_BACKLIGHT_CMDMSK = 0xE0, | |
5404 | TP_EC_BACKLIGHT_MAPSW = 0x20, | |
5405 | }; | |
5406 | ||
94954cc6 | 5407 | static struct backlight_device *ibm_backlight_device; |
f74a27d4 HMH |
5408 | static int brightness_mode; |
5409 | static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */ | |
5410 | ||
b21a15f6 | 5411 | static struct mutex brightness_mutex; |
56b6aeb0 | 5412 | |
b21a15f6 HMH |
5413 | /* |
5414 | * ThinkPads can read brightness from two places: EC 0x31, or | |
5415 | * CMOS NVRAM byte 0x5E, bits 0-3. | |
e11aecf1 HMH |
5416 | * |
5417 | * EC 0x31 has the following layout | |
5418 | * Bit 7: unknown function | |
5419 | * Bit 6: unknown function | |
5420 | * Bit 5: Z: honour scale changes, NZ: ignore scale changes | |
5421 | * Bit 4: must be set to zero to avoid problems | |
5422 | * Bit 3-0: backlight brightness level | |
5423 | * | |
5424 | * brightness_get_raw returns status data in the EC 0x31 layout | |
b21a15f6 | 5425 | */ |
e11aecf1 | 5426 | static int brightness_get_raw(int *status) |
b21a15f6 HMH |
5427 | { |
5428 | u8 lec = 0, lcmos = 0, level = 0; | |
5429 | ||
5430 | if (brightness_mode & 1) { | |
e11aecf1 | 5431 | if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec)) |
b21a15f6 | 5432 | return -EIO; |
e11aecf1 | 5433 | level = lec & TP_EC_BACKLIGHT_LVLMSK; |
b21a15f6 HMH |
5434 | }; |
5435 | if (brightness_mode & 2) { | |
5436 | lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS) | |
5437 | & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) | |
5438 | >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; | |
5439 | lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07; | |
5440 | level = lcmos; | |
5441 | } | |
5442 | ||
2d5e94d7 | 5443 | if (brightness_mode == 3) { |
e11aecf1 HMH |
5444 | *status = lec; /* Prefer EC, CMOS is just a backing store */ |
5445 | lec &= TP_EC_BACKLIGHT_LVLMSK; | |
2d5e94d7 HMH |
5446 | if (lec == lcmos) |
5447 | tp_warned.bright_cmos_ec_unsync = 0; | |
5448 | else { | |
5449 | if (!tp_warned.bright_cmos_ec_unsync) { | |
5450 | printk(TPACPI_ERR | |
5451 | "CMOS NVRAM (%u) and EC (%u) do not " | |
5452 | "agree on display brightness level\n", | |
5453 | (unsigned int) lcmos, | |
5454 | (unsigned int) lec); | |
5455 | tp_warned.bright_cmos_ec_unsync = 1; | |
5456 | } | |
5457 | return -EIO; | |
5458 | } | |
e11aecf1 HMH |
5459 | } else { |
5460 | *status = level; | |
b21a15f6 HMH |
5461 | } |
5462 | ||
e11aecf1 | 5463 | return 0; |
b21a15f6 HMH |
5464 | } |
5465 | ||
5466 | /* May return EINTR which can always be mapped to ERESTARTSYS */ | |
5467 | static int brightness_set(int value) | |
5468 | { | |
5469 | int cmos_cmd, inc, i, res; | |
5470 | int current_value; | |
e11aecf1 | 5471 | int command_bits; |
b21a15f6 | 5472 | |
e11aecf1 HMH |
5473 | if (value > ((tp_features.bright_16levels)? 15 : 7) || |
5474 | value < 0) | |
b21a15f6 HMH |
5475 | return -EINVAL; |
5476 | ||
7646ea88 | 5477 | res = mutex_lock_killable(&brightness_mutex); |
b21a15f6 HMH |
5478 | if (res < 0) |
5479 | return res; | |
5480 | ||
e11aecf1 HMH |
5481 | res = brightness_get_raw(¤t_value); |
5482 | if (res < 0) | |
b21a15f6 | 5483 | goto errout; |
e11aecf1 HMH |
5484 | |
5485 | command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK; | |
5486 | current_value &= TP_EC_BACKLIGHT_LVLMSK; | |
b21a15f6 HMH |
5487 | |
5488 | cmos_cmd = value > current_value ? | |
5489 | TP_CMOS_BRIGHTNESS_UP : | |
5490 | TP_CMOS_BRIGHTNESS_DOWN; | |
5491 | inc = (value > current_value)? 1 : -1; | |
5492 | ||
5493 | res = 0; | |
5494 | for (i = current_value; i != value; i += inc) { | |
5495 | if ((brightness_mode & 2) && | |
5496 | issue_thinkpad_cmos_command(cmos_cmd)) { | |
5497 | res = -EIO; | |
5498 | goto errout; | |
5499 | } | |
5500 | if ((brightness_mode & 1) && | |
e11aecf1 HMH |
5501 | !acpi_ec_write(TP_EC_BACKLIGHT, |
5502 | (i + inc) | command_bits)) { | |
b21a15f6 HMH |
5503 | res = -EIO; |
5504 | goto errout;; | |
5505 | } | |
5506 | } | |
5507 | ||
5508 | errout: | |
5509 | mutex_unlock(&brightness_mutex); | |
5510 | return res; | |
5511 | } | |
5512 | ||
5513 | /* sysfs backlight class ----------------------------------------------- */ | |
5514 | ||
5515 | static int brightness_update_status(struct backlight_device *bd) | |
5516 | { | |
5517 | /* it is the backlight class's job (caller) to handle | |
5518 | * EINTR and other errors properly */ | |
5519 | return brightness_set( | |
5520 | (bd->props.fb_blank == FB_BLANK_UNBLANK && | |
5521 | bd->props.power == FB_BLANK_UNBLANK) ? | |
5522 | bd->props.brightness : 0); | |
5523 | } | |
5524 | ||
e11aecf1 | 5525 | static int brightness_get(struct backlight_device *bd) |
a3f104c0 | 5526 | { |
e11aecf1 | 5527 | int status, res; |
a3f104c0 | 5528 | |
e11aecf1 HMH |
5529 | res = brightness_get_raw(&status); |
5530 | if (res < 0) | |
5531 | return 0; /* FIXME: teach backlight about error handling */ | |
e11e211a | 5532 | |
e11aecf1 | 5533 | return status & TP_EC_BACKLIGHT_LVLMSK; |
e11e211a HMH |
5534 | } |
5535 | ||
b21a15f6 | 5536 | static struct backlight_ops ibm_backlight_data = { |
35ff8b9f HMH |
5537 | .get_brightness = brightness_get, |
5538 | .update_status = brightness_update_status, | |
56b6aeb0 | 5539 | }; |
e11e211a | 5540 | |
b21a15f6 | 5541 | /* --------------------------------------------------------------------- */ |
e11e211a | 5542 | |
a5763f22 | 5543 | static int __init brightness_init(struct ibm_init_struct *iibm) |
56b6aeb0 HMH |
5544 | { |
5545 | int b; | |
5546 | ||
fe08bc4b HMH |
5547 | vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n"); |
5548 | ||
f432255e HMH |
5549 | mutex_init(&brightness_mutex); |
5550 | ||
b5972796 HMH |
5551 | /* |
5552 | * We always attempt to detect acpi support, so as to switch | |
5553 | * Lenovo Vista BIOS to ACPI brightness mode even if we are not | |
5554 | * going to publish a backlight interface | |
5555 | */ | |
5556 | b = tpacpi_check_std_acpi_brightness_support(); | |
5557 | if (b > 0) { | |
2dba1b5d TR |
5558 | |
5559 | if (acpi_video_backlight_support()) { | |
5560 | if (brightness_enable > 1) { | |
5561 | printk(TPACPI_NOTICE | |
5562 | "Standard ACPI backlight interface " | |
5563 | "available, not loading native one.\n"); | |
5564 | return 1; | |
5565 | } else if (brightness_enable == 1) { | |
5566 | printk(TPACPI_NOTICE | |
5567 | "Backlight control force enabled, even if standard " | |
5568 | "ACPI backlight interface is available\n"); | |
5569 | } | |
5570 | } else { | |
5571 | if (brightness_enable > 1) { | |
5572 | printk(TPACPI_NOTICE | |
5573 | "Standard ACPI backlight interface not " | |
5574 | "available, thinkpad_acpi native " | |
5575 | "brightness control enabled\n"); | |
5576 | } | |
e11e211a | 5577 | } |
87cc537a HMH |
5578 | } |
5579 | ||
b5972796 HMH |
5580 | if (!brightness_enable) { |
5581 | dbg_printk(TPACPI_DBG_INIT, | |
5582 | "brightness support disabled by " | |
5583 | "module parameter\n"); | |
5584 | return 1; | |
5585 | } | |
5586 | ||
5587 | if (b > 16) { | |
5588 | printk(TPACPI_ERR | |
5589 | "Unsupported brightness interface, " | |
5590 | "please contact %s\n", TPACPI_MAIL); | |
5591 | return 1; | |
5592 | } | |
5593 | if (b == 16) | |
5594 | tp_features.bright_16levels = 1; | |
5595 | ||
24d3b774 HMH |
5596 | if (!brightness_mode) { |
5597 | if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) | |
5598 | brightness_mode = 2; | |
5599 | else | |
5600 | brightness_mode = 3; | |
5601 | ||
5602 | dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n", | |
5603 | brightness_mode); | |
5604 | } | |
5605 | ||
5606 | if (brightness_mode > 3) | |
5607 | return -EINVAL; | |
5608 | ||
e11aecf1 | 5609 | if (brightness_get_raw(&b) < 0) |
24d3b774 | 5610 | return 1; |
56b6aeb0 | 5611 | |
a3f104c0 | 5612 | if (tp_features.bright_16levels) |
35ff8b9f HMH |
5613 | printk(TPACPI_INFO |
5614 | "detected a 16-level brightness capable ThinkPad\n"); | |
a3f104c0 | 5615 | |
7d5a015e HMH |
5616 | ibm_backlight_device = backlight_device_register( |
5617 | TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL, | |
5618 | &ibm_backlight_data); | |
56b6aeb0 | 5619 | if (IS_ERR(ibm_backlight_device)) { |
e0c7dfe7 | 5620 | printk(TPACPI_ERR "Could not register backlight device\n"); |
56b6aeb0 HMH |
5621 | return PTR_ERR(ibm_backlight_device); |
5622 | } | |
fe08bc4b | 5623 | vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n"); |
56b6aeb0 | 5624 | |
a3f104c0 HMH |
5625 | ibm_backlight_device->props.max_brightness = |
5626 | (tp_features.bright_16levels)? 15 : 7; | |
e11aecf1 | 5627 | ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK; |
56b6aeb0 HMH |
5628 | backlight_update_status(ibm_backlight_device); |
5629 | ||
5630 | return 0; | |
5631 | } | |
5632 | ||
5633 | static void brightness_exit(void) | |
5634 | { | |
5635 | if (ibm_backlight_device) { | |
fe08bc4b HMH |
5636 | vdbg_printk(TPACPI_DBG_EXIT, |
5637 | "calling backlight_device_unregister()\n"); | |
56b6aeb0 | 5638 | backlight_device_unregister(ibm_backlight_device); |
56b6aeb0 HMH |
5639 | } |
5640 | } | |
5641 | ||
56b6aeb0 HMH |
5642 | static int brightness_read(char *p) |
5643 | { | |
5644 | int len = 0; | |
5645 | int level; | |
5646 | ||
35ff8b9f HMH |
5647 | level = brightness_get(NULL); |
5648 | if (level < 0) { | |
56b6aeb0 HMH |
5649 | len += sprintf(p + len, "level:\t\tunreadable\n"); |
5650 | } else { | |
a3f104c0 | 5651 | len += sprintf(p + len, "level:\t\t%d\n", level); |
56b6aeb0 HMH |
5652 | len += sprintf(p + len, "commands:\tup, down\n"); |
5653 | len += sprintf(p + len, "commands:\tlevel <level>" | |
a3f104c0 HMH |
5654 | " (<level> is 0-%d)\n", |
5655 | (tp_features.bright_16levels) ? 15 : 7); | |
56b6aeb0 HMH |
5656 | } |
5657 | ||
5658 | return len; | |
5659 | } | |
5660 | ||
8acb0250 HM |
5661 | static int brightness_write(char *buf) |
5662 | { | |
5663 | int level; | |
4273af8d | 5664 | int rc; |
1da177e4 | 5665 | char *cmd; |
a3f104c0 | 5666 | int max_level = (tp_features.bright_16levels) ? 15 : 7; |
1da177e4 | 5667 | |
4273af8d HMH |
5668 | level = brightness_get(NULL); |
5669 | if (level < 0) | |
5670 | return level; | |
78f81cc4 | 5671 | |
4273af8d | 5672 | while ((cmd = next_cmd(&buf))) { |
78f81cc4 | 5673 | if (strlencmp(cmd, "up") == 0) { |
4273af8d HMH |
5674 | if (level < max_level) |
5675 | level++; | |
78f81cc4 | 5676 | } else if (strlencmp(cmd, "down") == 0) { |
4273af8d HMH |
5677 | if (level > 0) |
5678 | level--; | |
5679 | } else if (sscanf(cmd, "level %d", &level) == 1 && | |
5680 | level >= 0 && level <= max_level) { | |
5681 | /* new level set */ | |
78f81cc4 BD |
5682 | } else |
5683 | return -EINVAL; | |
78f81cc4 BD |
5684 | } |
5685 | ||
4273af8d HMH |
5686 | /* |
5687 | * Now we know what the final level should be, so we try to set it. | |
5688 | * Doing it this way makes the syscall restartable in case of EINTR | |
5689 | */ | |
5690 | rc = brightness_set(level); | |
5691 | return (rc == -EINTR)? ERESTARTSYS : rc; | |
78f81cc4 BD |
5692 | } |
5693 | ||
a5763f22 HMH |
5694 | static struct ibm_struct brightness_driver_data = { |
5695 | .name = "brightness", | |
5696 | .read = brightness_read, | |
5697 | .write = brightness_write, | |
5698 | .exit = brightness_exit, | |
5699 | }; | |
5700 | ||
56b6aeb0 HMH |
5701 | /************************************************************************* |
5702 | * Volume subdriver | |
5703 | */ | |
fb87a811 | 5704 | |
f74a27d4 HMH |
5705 | static int volume_offset = 0x30; |
5706 | ||
78f81cc4 BD |
5707 | static int volume_read(char *p) |
5708 | { | |
5709 | int len = 0; | |
5710 | u8 level; | |
5711 | ||
5712 | if (!acpi_ec_read(volume_offset, &level)) { | |
5713 | len += sprintf(p + len, "level:\t\tunreadable\n"); | |
5714 | } else { | |
5715 | len += sprintf(p + len, "level:\t\t%d\n", level & 0xf); | |
5716 | len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6)); | |
5717 | len += sprintf(p + len, "commands:\tup, down, mute\n"); | |
5718 | len += sprintf(p + len, "commands:\tlevel <level>" | |
5719 | " (<level> is 0-15)\n"); | |
5720 | } | |
5721 | ||
5722 | return len; | |
5723 | } | |
5724 | ||
78f81cc4 BD |
5725 | static int volume_write(char *buf) |
5726 | { | |
5727 | int cmos_cmd, inc, i; | |
5728 | u8 level, mute; | |
5729 | int new_level, new_mute; | |
5730 | char *cmd; | |
5731 | ||
5732 | while ((cmd = next_cmd(&buf))) { | |
5733 | if (!acpi_ec_read(volume_offset, &level)) | |
5734 | return -EIO; | |
5735 | new_mute = mute = level & 0x40; | |
5736 | new_level = level = level & 0xf; | |
5737 | ||
5738 | if (strlencmp(cmd, "up") == 0) { | |
5739 | if (mute) | |
5740 | new_mute = 0; | |
5741 | else | |
5742 | new_level = level == 15 ? 15 : level + 1; | |
5743 | } else if (strlencmp(cmd, "down") == 0) { | |
5744 | if (mute) | |
5745 | new_mute = 0; | |
5746 | else | |
5747 | new_level = level == 0 ? 0 : level - 1; | |
5748 | } else if (sscanf(cmd, "level %d", &new_level) == 1 && | |
5749 | new_level >= 0 && new_level <= 15) { | |
5750 | /* new_level set */ | |
5751 | } else if (strlencmp(cmd, "mute") == 0) { | |
5752 | new_mute = 0x40; | |
1da177e4 LT |
5753 | } else |
5754 | return -EINVAL; | |
5755 | ||
35ff8b9f HMH |
5756 | if (new_level != level) { |
5757 | /* mute doesn't change */ | |
5758 | ||
5759 | cmos_cmd = (new_level > level) ? | |
5760 | TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN; | |
78f81cc4 BD |
5761 | inc = new_level > level ? 1 : -1; |
5762 | ||
c9bea99c | 5763 | if (mute && (issue_thinkpad_cmos_command(cmos_cmd) || |
78f81cc4 BD |
5764 | !acpi_ec_write(volume_offset, level))) |
5765 | return -EIO; | |
5766 | ||
5767 | for (i = level; i != new_level; i += inc) | |
c9bea99c | 5768 | if (issue_thinkpad_cmos_command(cmos_cmd) || |
78f81cc4 BD |
5769 | !acpi_ec_write(volume_offset, i + inc)) |
5770 | return -EIO; | |
5771 | ||
35ff8b9f HMH |
5772 | if (mute && |
5773 | (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) || | |
5774 | !acpi_ec_write(volume_offset, new_level + mute))) { | |
78f81cc4 | 5775 | return -EIO; |
35ff8b9f | 5776 | } |
78f81cc4 BD |
5777 | } |
5778 | ||
35ff8b9f HMH |
5779 | if (new_mute != mute) { |
5780 | /* level doesn't change */ | |
5781 | ||
5782 | cmos_cmd = (new_mute) ? | |
5783 | TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP; | |
78f81cc4 | 5784 | |
c9bea99c | 5785 | if (issue_thinkpad_cmos_command(cmos_cmd) || |
78f81cc4 BD |
5786 | !acpi_ec_write(volume_offset, level + new_mute)) |
5787 | return -EIO; | |
5788 | } | |
5789 | } | |
5790 | ||
5791 | return 0; | |
5792 | } | |
5793 | ||
a5763f22 HMH |
5794 | static struct ibm_struct volume_driver_data = { |
5795 | .name = "volume", | |
5796 | .read = volume_read, | |
5797 | .write = volume_write, | |
5798 | }; | |
56b6aeb0 HMH |
5799 | |
5800 | /************************************************************************* | |
5801 | * Fan subdriver | |
5802 | */ | |
5803 | ||
5804 | /* | |
5805 | * FAN ACCESS MODES | |
5806 | * | |
efa27145 | 5807 | * TPACPI_FAN_RD_ACPI_GFAN: |
56b6aeb0 HMH |
5808 | * ACPI GFAN method: returns fan level |
5809 | * | |
efa27145 | 5810 | * see TPACPI_FAN_WR_ACPI_SFAN |
f51d1a39 | 5811 | * EC 0x2f (HFSP) not available if GFAN exists |
56b6aeb0 | 5812 | * |
efa27145 | 5813 | * TPACPI_FAN_WR_ACPI_SFAN: |
56b6aeb0 HMH |
5814 | * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max) |
5815 | * | |
f51d1a39 HMH |
5816 | * EC 0x2f (HFSP) might be available *for reading*, but do not use |
5817 | * it for writing. | |
56b6aeb0 | 5818 | * |
efa27145 | 5819 | * TPACPI_FAN_WR_TPEC: |
f51d1a39 HMH |
5820 | * ThinkPad EC register 0x2f (HFSP): fan control loop mode |
5821 | * Supported on almost all ThinkPads | |
56b6aeb0 HMH |
5822 | * |
5823 | * Fan speed changes of any sort (including those caused by the | |
5824 | * disengaged mode) are usually done slowly by the firmware as the | |
5825 | * maximum ammount of fan duty cycle change per second seems to be | |
5826 | * limited. | |
5827 | * | |
5828 | * Reading is not available if GFAN exists. | |
5829 | * Writing is not available if SFAN exists. | |
5830 | * | |
5831 | * Bits | |
5832 | * 7 automatic mode engaged; | |
5833 | * (default operation mode of the ThinkPad) | |
5834 | * fan level is ignored in this mode. | |
f51d1a39 | 5835 | * 6 full speed mode (takes precedence over bit 7); |
56b6aeb0 | 5836 | * not available on all thinkpads. May disable |
f51d1a39 HMH |
5837 | * the tachometer while the fan controller ramps up |
5838 | * the speed (which can take up to a few *minutes*). | |
5839 | * Speeds up fan to 100% duty-cycle, which is far above | |
5840 | * the standard RPM levels. It is not impossible that | |
5841 | * it could cause hardware damage. | |
56b6aeb0 HMH |
5842 | * 5-3 unused in some models. Extra bits for fan level |
5843 | * in others, but still useless as all values above | |
5844 | * 7 map to the same speed as level 7 in these models. | |
5845 | * 2-0 fan level (0..7 usually) | |
5846 | * 0x00 = stop | |
5847 | * 0x07 = max (set when temperatures critical) | |
5848 | * Some ThinkPads may have other levels, see | |
efa27145 | 5849 | * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41) |
56b6aeb0 HMH |
5850 | * |
5851 | * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at | |
5852 | * boot. Apparently the EC does not intialize it, so unless ACPI DSDT | |
5853 | * does so, its initial value is meaningless (0x07). | |
5854 | * | |
5855 | * For firmware bugs, refer to: | |
5856 | * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues | |
5857 | * | |
5858 | * ---- | |
5859 | * | |
5860 | * ThinkPad EC register 0x84 (LSB), 0x85 (MSB): | |
5861 | * Main fan tachometer reading (in RPM) | |
5862 | * | |
5863 | * This register is present on all ThinkPads with a new-style EC, and | |
5864 | * it is known not to be present on the A21m/e, and T22, as there is | |
5865 | * something else in offset 0x84 according to the ACPI DSDT. Other | |
5866 | * ThinkPads from this same time period (and earlier) probably lack the | |
5867 | * tachometer as well. | |
5868 | * | |
5869 | * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare | |
5870 | * was never fixed by IBM to report the EC firmware version string | |
5871 | * probably support the tachometer (like the early X models), so | |
5872 | * detecting it is quite hard. We need more data to know for sure. | |
5873 | * | |
5874 | * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings | |
5875 | * might result. | |
5876 | * | |
f51d1a39 HMH |
5877 | * FIRMWARE BUG: may go stale while the EC is switching to full speed |
5878 | * mode. | |
56b6aeb0 HMH |
5879 | * |
5880 | * For firmware bugs, refer to: | |
5881 | * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues | |
5882 | * | |
efa27145 | 5883 | * TPACPI_FAN_WR_ACPI_FANS: |
56b6aeb0 HMH |
5884 | * ThinkPad X31, X40, X41. Not available in the X60. |
5885 | * | |
5886 | * FANS ACPI handle: takes three arguments: low speed, medium speed, | |
5887 | * high speed. ACPI DSDT seems to map these three speeds to levels | |
5888 | * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH | |
5889 | * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3") | |
5890 | * | |
5891 | * The speeds are stored on handles | |
5892 | * (FANA:FAN9), (FANC:FANB), (FANE:FAND). | |
5893 | * | |
5894 | * There are three default speed sets, acessible as handles: | |
5895 | * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H | |
5896 | * | |
5897 | * ACPI DSDT switches which set is in use depending on various | |
5898 | * factors. | |
5899 | * | |
efa27145 | 5900 | * TPACPI_FAN_WR_TPEC is also available and should be used to |
56b6aeb0 HMH |
5901 | * command the fan. The X31/X40/X41 seems to have 8 fan levels, |
5902 | * but the ACPI tables just mention level 7. | |
5903 | */ | |
5904 | ||
f74a27d4 HMH |
5905 | enum { /* Fan control constants */ |
5906 | fan_status_offset = 0x2f, /* EC register 0x2f */ | |
5907 | fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM) | |
5908 | * 0x84 must be read before 0x85 */ | |
5909 | ||
5910 | TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */ | |
5911 | TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */ | |
5912 | ||
5913 | TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */ | |
5914 | }; | |
5915 | ||
5916 | enum fan_status_access_mode { | |
5917 | TPACPI_FAN_NONE = 0, /* No fan status or control */ | |
5918 | TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */ | |
5919 | TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */ | |
5920 | }; | |
5921 | ||
5922 | enum fan_control_access_mode { | |
5923 | TPACPI_FAN_WR_NONE = 0, /* No fan control */ | |
5924 | TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */ | |
5925 | TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */ | |
5926 | TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */ | |
5927 | }; | |
5928 | ||
5929 | enum fan_control_commands { | |
5930 | TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */ | |
5931 | TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */ | |
5932 | TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd, | |
5933 | * and also watchdog cmd */ | |
5934 | }; | |
5935 | ||
5936 | static int fan_control_allowed; | |
5937 | ||
69ba91cb HMH |
5938 | static enum fan_status_access_mode fan_status_access_mode; |
5939 | static enum fan_control_access_mode fan_control_access_mode; | |
5940 | static enum fan_control_commands fan_control_commands; | |
78f81cc4 | 5941 | |
778b4d74 | 5942 | static u8 fan_control_initial_status; |
fe98a52c | 5943 | static u8 fan_control_desired_level; |
0081b162 | 5944 | static u8 fan_control_resume_level; |
f74a27d4 HMH |
5945 | static int fan_watchdog_maxinterval; |
5946 | ||
5947 | static struct mutex fan_mutex; | |
778b4d74 | 5948 | |
25c68a33 | 5949 | static void fan_watchdog_fire(struct work_struct *ignored); |
25c68a33 | 5950 | static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire); |
16663a87 | 5951 | |
e0c7dfe7 HMH |
5952 | TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */ |
5953 | TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */ | |
56b6aeb0 HMH |
5954 | "\\FSPD", /* 600e/x, 770e, 770x */ |
5955 | ); /* all others */ | |
e0c7dfe7 | 5956 | TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */ |
56b6aeb0 HMH |
5957 | "JFNS", /* 770x-JL */ |
5958 | ); /* all others */ | |
5959 | ||
1c2ece75 HMH |
5960 | /* |
5961 | * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the | |
5962 | * HFSP register at boot, so it contains 0x07 but the Thinkpad could | |
5963 | * be in auto mode (0x80). | |
5964 | * | |
5965 | * This is corrected by any write to HFSP either by the driver, or | |
5966 | * by the firmware. | |
5967 | * | |
5968 | * We assume 0x07 really means auto mode while this quirk is active, | |
5969 | * as this is far more likely than the ThinkPad being in level 7, | |
5970 | * which is only used by the firmware during thermal emergencies. | |
5971 | */ | |
5972 | ||
5973 | static void fan_quirk1_detect(void) | |
5974 | { | |
5975 | /* In some ThinkPads, neither the EC nor the ACPI | |
5976 | * DSDT initialize the HFSP register, and it ends up | |
5977 | * being initially set to 0x07 when it *could* be | |
5978 | * either 0x07 or 0x80. | |
5979 | * | |
5980 | * Enable for TP-1Y (T43), TP-78 (R51e), | |
5981 | * TP-76 (R52), TP-70 (T43, R52), which are known | |
5982 | * to be buggy. */ | |
5983 | if (fan_control_initial_status == 0x07) { | |
5984 | switch (thinkpad_id.ec_model) { | |
5985 | case 0x5931: /* TP-1Y */ | |
5986 | case 0x3837: /* TP-78 */ | |
5987 | case 0x3637: /* TP-76 */ | |
5988 | case 0x3037: /* TP-70 */ | |
5989 | printk(TPACPI_NOTICE | |
5990 | "fan_init: initial fan status is unknown, " | |
5991 | "assuming it is in auto mode\n"); | |
5992 | tp_features.fan_ctrl_status_undef = 1; | |
5993 | ;; | |
5994 | } | |
5995 | } | |
5996 | } | |
5997 | ||
5998 | static void fan_quirk1_handle(u8 *fan_status) | |
5999 | { | |
6000 | if (unlikely(tp_features.fan_ctrl_status_undef)) { | |
6001 | if (*fan_status != fan_control_initial_status) { | |
6002 | /* something changed the HFSP regisnter since | |
6003 | * driver init time, so it is not undefined | |
6004 | * anymore */ | |
6005 | tp_features.fan_ctrl_status_undef = 0; | |
6006 | } else { | |
6007 | /* Return most likely status. In fact, it | |
6008 | * might be the only possible status */ | |
6009 | *fan_status = TP_EC_FAN_AUTO; | |
6010 | } | |
6011 | } | |
6012 | } | |
6013 | ||
fe98a52c | 6014 | /* |
b21a15f6 | 6015 | * Call with fan_mutex held |
fe98a52c | 6016 | */ |
b21a15f6 | 6017 | static void fan_update_desired_level(u8 status) |
fe98a52c | 6018 | { |
b21a15f6 HMH |
6019 | if ((status & |
6020 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { | |
6021 | if (status > 7) | |
6022 | fan_control_desired_level = 7; | |
6023 | else | |
6024 | fan_control_desired_level = status; | |
6025 | } | |
6026 | } | |
fe98a52c | 6027 | |
b21a15f6 HMH |
6028 | static int fan_get_status(u8 *status) |
6029 | { | |
6030 | u8 s; | |
fe98a52c | 6031 | |
b21a15f6 HMH |
6032 | /* TODO: |
6033 | * Add TPACPI_FAN_RD_ACPI_FANS ? */ | |
fe98a52c | 6034 | |
b21a15f6 HMH |
6035 | switch (fan_status_access_mode) { |
6036 | case TPACPI_FAN_RD_ACPI_GFAN: | |
6037 | /* 570, 600e/x, 770e, 770x */ | |
fe98a52c | 6038 | |
b21a15f6 HMH |
6039 | if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d"))) |
6040 | return -EIO; | |
fe98a52c | 6041 | |
b21a15f6 HMH |
6042 | if (likely(status)) |
6043 | *status = s & 0x07; | |
fe98a52c | 6044 | |
b21a15f6 HMH |
6045 | break; |
6046 | ||
6047 | case TPACPI_FAN_RD_TPEC: | |
6048 | /* all except 570, 600e/x, 770e, 770x */ | |
6049 | if (unlikely(!acpi_ec_read(fan_status_offset, &s))) | |
6050 | return -EIO; | |
6051 | ||
1c2ece75 | 6052 | if (likely(status)) { |
b21a15f6 | 6053 | *status = s; |
1c2ece75 HMH |
6054 | fan_quirk1_handle(status); |
6055 | } | |
fe98a52c | 6056 | |
fe98a52c | 6057 | break; |
b21a15f6 | 6058 | |
fe98a52c | 6059 | default: |
b21a15f6 | 6060 | return -ENXIO; |
fe98a52c HMH |
6061 | } |
6062 | ||
b21a15f6 HMH |
6063 | return 0; |
6064 | } | |
fe98a52c | 6065 | |
b21a15f6 HMH |
6066 | static int fan_get_status_safe(u8 *status) |
6067 | { | |
6068 | int rc; | |
6069 | u8 s; | |
fe98a52c | 6070 | |
7646ea88 | 6071 | if (mutex_lock_killable(&fan_mutex)) |
b21a15f6 HMH |
6072 | return -ERESTARTSYS; |
6073 | rc = fan_get_status(&s); | |
6074 | if (!rc) | |
6075 | fan_update_desired_level(s); | |
6076 | mutex_unlock(&fan_mutex); | |
fe98a52c | 6077 | |
b21a15f6 HMH |
6078 | if (status) |
6079 | *status = s; | |
fe98a52c | 6080 | |
b21a15f6 HMH |
6081 | return rc; |
6082 | } | |
6083 | ||
6084 | static int fan_get_speed(unsigned int *speed) | |
fe98a52c | 6085 | { |
b21a15f6 | 6086 | u8 hi, lo; |
fe98a52c | 6087 | |
b21a15f6 HMH |
6088 | switch (fan_status_access_mode) { |
6089 | case TPACPI_FAN_RD_TPEC: | |
6090 | /* all except 570, 600e/x, 770e, 770x */ | |
6091 | if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) || | |
6092 | !acpi_ec_read(fan_rpm_offset + 1, &hi))) | |
6093 | return -EIO; | |
fe98a52c | 6094 | |
b21a15f6 HMH |
6095 | if (likely(speed)) |
6096 | *speed = (hi << 8) | lo; | |
fe98a52c | 6097 | |
b21a15f6 | 6098 | break; |
fe98a52c | 6099 | |
b21a15f6 HMH |
6100 | default: |
6101 | return -ENXIO; | |
6102 | } | |
fe98a52c | 6103 | |
b21a15f6 | 6104 | return 0; |
fe98a52c HMH |
6105 | } |
6106 | ||
b21a15f6 | 6107 | static int fan_set_level(int level) |
fe98a52c | 6108 | { |
b21a15f6 HMH |
6109 | if (!fan_control_allowed) |
6110 | return -EPERM; | |
fe98a52c | 6111 | |
b21a15f6 HMH |
6112 | switch (fan_control_access_mode) { |
6113 | case TPACPI_FAN_WR_ACPI_SFAN: | |
6114 | if (level >= 0 && level <= 7) { | |
6115 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) | |
6116 | return -EIO; | |
6117 | } else | |
6118 | return -EINVAL; | |
6119 | break; | |
fe98a52c | 6120 | |
b21a15f6 HMH |
6121 | case TPACPI_FAN_WR_ACPI_FANS: |
6122 | case TPACPI_FAN_WR_TPEC: | |
0081b162 HMH |
6123 | if (!(level & TP_EC_FAN_AUTO) && |
6124 | !(level & TP_EC_FAN_FULLSPEED) && | |
b21a15f6 HMH |
6125 | ((level < 0) || (level > 7))) |
6126 | return -EINVAL; | |
fe98a52c | 6127 | |
b21a15f6 HMH |
6128 | /* safety net should the EC not support AUTO |
6129 | * or FULLSPEED mode bits and just ignore them */ | |
6130 | if (level & TP_EC_FAN_FULLSPEED) | |
6131 | level |= 7; /* safety min speed 7 */ | |
547266e4 | 6132 | else if (level & TP_EC_FAN_AUTO) |
b21a15f6 | 6133 | level |= 4; /* safety min speed 4 */ |
fe98a52c | 6134 | |
b21a15f6 HMH |
6135 | if (!acpi_ec_write(fan_status_offset, level)) |
6136 | return -EIO; | |
6137 | else | |
6138 | tp_features.fan_ctrl_status_undef = 0; | |
6139 | break; | |
fe98a52c | 6140 | |
b21a15f6 HMH |
6141 | default: |
6142 | return -ENXIO; | |
6143 | } | |
6144 | return 0; | |
fe98a52c HMH |
6145 | } |
6146 | ||
b21a15f6 | 6147 | static int fan_set_level_safe(int level) |
fe98a52c | 6148 | { |
b21a15f6 | 6149 | int rc; |
fe98a52c | 6150 | |
b21a15f6 HMH |
6151 | if (!fan_control_allowed) |
6152 | return -EPERM; | |
fe98a52c | 6153 | |
7646ea88 | 6154 | if (mutex_lock_killable(&fan_mutex)) |
b21a15f6 | 6155 | return -ERESTARTSYS; |
fe98a52c | 6156 | |
b21a15f6 HMH |
6157 | if (level == TPACPI_FAN_LAST_LEVEL) |
6158 | level = fan_control_desired_level; | |
fe98a52c | 6159 | |
b21a15f6 HMH |
6160 | rc = fan_set_level(level); |
6161 | if (!rc) | |
6162 | fan_update_desired_level(level); | |
6163 | ||
6164 | mutex_unlock(&fan_mutex); | |
6165 | return rc; | |
fe98a52c HMH |
6166 | } |
6167 | ||
b21a15f6 | 6168 | static int fan_set_enable(void) |
fe98a52c | 6169 | { |
b21a15f6 HMH |
6170 | u8 s; |
6171 | int rc; | |
fe98a52c | 6172 | |
ecf2a80a HMH |
6173 | if (!fan_control_allowed) |
6174 | return -EPERM; | |
6175 | ||
7646ea88 | 6176 | if (mutex_lock_killable(&fan_mutex)) |
b21a15f6 | 6177 | return -ERESTARTSYS; |
fe98a52c | 6178 | |
b21a15f6 HMH |
6179 | switch (fan_control_access_mode) { |
6180 | case TPACPI_FAN_WR_ACPI_FANS: | |
6181 | case TPACPI_FAN_WR_TPEC: | |
6182 | rc = fan_get_status(&s); | |
6183 | if (rc < 0) | |
6184 | break; | |
fe98a52c | 6185 | |
b21a15f6 HMH |
6186 | /* Don't go out of emergency fan mode */ |
6187 | if (s != 7) { | |
6188 | s &= 0x07; | |
6189 | s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */ | |
6190 | } | |
fe98a52c | 6191 | |
b21a15f6 HMH |
6192 | if (!acpi_ec_write(fan_status_offset, s)) |
6193 | rc = -EIO; | |
6194 | else { | |
6195 | tp_features.fan_ctrl_status_undef = 0; | |
6196 | rc = 0; | |
6197 | } | |
6198 | break; | |
fe98a52c | 6199 | |
b21a15f6 HMH |
6200 | case TPACPI_FAN_WR_ACPI_SFAN: |
6201 | rc = fan_get_status(&s); | |
6202 | if (rc < 0) | |
6203 | break; | |
fe98a52c | 6204 | |
b21a15f6 | 6205 | s &= 0x07; |
fe98a52c | 6206 | |
b21a15f6 HMH |
6207 | /* Set fan to at least level 4 */ |
6208 | s |= 4; | |
fe08bc4b | 6209 | |
b21a15f6 | 6210 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s)) |
35ff8b9f | 6211 | rc = -EIO; |
b21a15f6 HMH |
6212 | else |
6213 | rc = 0; | |
6214 | break; | |
78f81cc4 | 6215 | |
b21a15f6 HMH |
6216 | default: |
6217 | rc = -ENXIO; | |
6218 | } | |
5fba344c | 6219 | |
b21a15f6 HMH |
6220 | mutex_unlock(&fan_mutex); |
6221 | return rc; | |
69ba91cb | 6222 | } |
78f81cc4 | 6223 | |
b21a15f6 | 6224 | static int fan_set_disable(void) |
78f81cc4 | 6225 | { |
b21a15f6 | 6226 | int rc; |
c52f0aa5 | 6227 | |
b21a15f6 HMH |
6228 | if (!fan_control_allowed) |
6229 | return -EPERM; | |
78f81cc4 | 6230 | |
7646ea88 | 6231 | if (mutex_lock_killable(&fan_mutex)) |
b21a15f6 | 6232 | return -ERESTARTSYS; |
3ef8a609 | 6233 | |
b21a15f6 HMH |
6234 | rc = 0; |
6235 | switch (fan_control_access_mode) { | |
6236 | case TPACPI_FAN_WR_ACPI_FANS: | |
6237 | case TPACPI_FAN_WR_TPEC: | |
6238 | if (!acpi_ec_write(fan_status_offset, 0x00)) | |
6239 | rc = -EIO; | |
6240 | else { | |
6241 | fan_control_desired_level = 0; | |
6242 | tp_features.fan_ctrl_status_undef = 0; | |
6243 | } | |
3ef8a609 HMH |
6244 | break; |
6245 | ||
b21a15f6 HMH |
6246 | case TPACPI_FAN_WR_ACPI_SFAN: |
6247 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00)) | |
6248 | rc = -EIO; | |
6249 | else | |
6250 | fan_control_desired_level = 0; | |
c52f0aa5 HMH |
6251 | break; |
6252 | ||
6253 | default: | |
b21a15f6 | 6254 | rc = -ENXIO; |
78f81cc4 BD |
6255 | } |
6256 | ||
fe98a52c | 6257 | |
fe98a52c | 6258 | mutex_unlock(&fan_mutex); |
fe98a52c HMH |
6259 | return rc; |
6260 | } | |
6261 | ||
b21a15f6 | 6262 | static int fan_set_speed(int speed) |
c52f0aa5 | 6263 | { |
b21a15f6 | 6264 | int rc; |
c52f0aa5 | 6265 | |
b21a15f6 HMH |
6266 | if (!fan_control_allowed) |
6267 | return -EPERM; | |
3ef8a609 | 6268 | |
7646ea88 | 6269 | if (mutex_lock_killable(&fan_mutex)) |
b21a15f6 | 6270 | return -ERESTARTSYS; |
c52f0aa5 | 6271 | |
b21a15f6 HMH |
6272 | rc = 0; |
6273 | switch (fan_control_access_mode) { | |
6274 | case TPACPI_FAN_WR_ACPI_FANS: | |
6275 | if (speed >= 0 && speed <= 65535) { | |
6276 | if (!acpi_evalf(fans_handle, NULL, NULL, "vddd", | |
6277 | speed, speed, speed)) | |
6278 | rc = -EIO; | |
6279 | } else | |
6280 | rc = -EINVAL; | |
c52f0aa5 HMH |
6281 | break; |
6282 | ||
6283 | default: | |
b21a15f6 | 6284 | rc = -ENXIO; |
c52f0aa5 HMH |
6285 | } |
6286 | ||
b21a15f6 HMH |
6287 | mutex_unlock(&fan_mutex); |
6288 | return rc; | |
16663a87 HMH |
6289 | } |
6290 | ||
6291 | static void fan_watchdog_reset(void) | |
6292 | { | |
94954cc6 | 6293 | static int fan_watchdog_active; |
16663a87 | 6294 | |
4985cd0a HMH |
6295 | if (fan_control_access_mode == TPACPI_FAN_WR_NONE) |
6296 | return; | |
6297 | ||
16663a87 HMH |
6298 | if (fan_watchdog_active) |
6299 | cancel_delayed_work(&fan_watchdog_task); | |
6300 | ||
8fef502e HMH |
6301 | if (fan_watchdog_maxinterval > 0 && |
6302 | tpacpi_lifecycle != TPACPI_LIFE_EXITING) { | |
16663a87 | 6303 | fan_watchdog_active = 1; |
e0e3c061 | 6304 | if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task, |
16663a87 HMH |
6305 | msecs_to_jiffies(fan_watchdog_maxinterval |
6306 | * 1000))) { | |
35ff8b9f | 6307 | printk(TPACPI_ERR |
e0e3c061 | 6308 | "failed to queue the fan watchdog, " |
16663a87 HMH |
6309 | "watchdog will not trigger\n"); |
6310 | } | |
6311 | } else | |
6312 | fan_watchdog_active = 0; | |
6313 | } | |
6314 | ||
b21a15f6 | 6315 | static void fan_watchdog_fire(struct work_struct *ignored) |
78f81cc4 | 6316 | { |
b21a15f6 | 6317 | int rc; |
18ad7996 | 6318 | |
b21a15f6 HMH |
6319 | if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) |
6320 | return; | |
a12095c2 | 6321 | |
e0c7dfe7 | 6322 | printk(TPACPI_NOTICE "fan watchdog: enabling fan\n"); |
b21a15f6 HMH |
6323 | rc = fan_set_enable(); |
6324 | if (rc < 0) { | |
e0c7dfe7 | 6325 | printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, " |
b21a15f6 HMH |
6326 | "will try again later...\n", -rc); |
6327 | /* reschedule for later */ | |
6328 | fan_watchdog_reset(); | |
6329 | } | |
6330 | } | |
eaa7571b | 6331 | |
b21a15f6 HMH |
6332 | /* |
6333 | * SYSFS fan layout: hwmon compatible (device) | |
6334 | * | |
6335 | * pwm*_enable: | |
6336 | * 0: "disengaged" mode | |
6337 | * 1: manual mode | |
6338 | * 2: native EC "auto" mode (recommended, hardware default) | |
6339 | * | |
6340 | * pwm*: set speed in manual mode, ignored otherwise. | |
6341 | * 0 is level 0; 255 is level 7. Intermediate points done with linear | |
6342 | * interpolation. | |
6343 | * | |
6344 | * fan*_input: tachometer reading, RPM | |
6345 | * | |
6346 | * | |
6347 | * SYSFS fan layout: extensions | |
6348 | * | |
6349 | * fan_watchdog (driver): | |
6350 | * fan watchdog interval in seconds, 0 disables (default), max 120 | |
6351 | */ | |
6352 | ||
6353 | /* sysfs fan pwm1_enable ----------------------------------------------- */ | |
6354 | static ssize_t fan_pwm1_enable_show(struct device *dev, | |
6355 | struct device_attribute *attr, | |
6356 | char *buf) | |
6357 | { | |
6358 | int res, mode; | |
6359 | u8 status; | |
6360 | ||
6361 | res = fan_get_status_safe(&status); | |
6362 | if (res) | |
6363 | return res; | |
6364 | ||
b21a15f6 HMH |
6365 | if (status & TP_EC_FAN_FULLSPEED) { |
6366 | mode = 0; | |
6367 | } else if (status & TP_EC_FAN_AUTO) { | |
6368 | mode = 2; | |
6369 | } else | |
6370 | mode = 1; | |
6371 | ||
6372 | return snprintf(buf, PAGE_SIZE, "%d\n", mode); | |
6373 | } | |
a12095c2 | 6374 | |
b21a15f6 HMH |
6375 | static ssize_t fan_pwm1_enable_store(struct device *dev, |
6376 | struct device_attribute *attr, | |
6377 | const char *buf, size_t count) | |
6378 | { | |
6379 | unsigned long t; | |
6380 | int res, level; | |
6381 | ||
6382 | if (parse_strtoul(buf, 2, &t)) | |
6383 | return -EINVAL; | |
6384 | ||
6385 | switch (t) { | |
6386 | case 0: | |
6387 | level = TP_EC_FAN_FULLSPEED; | |
6388 | break; | |
6389 | case 1: | |
6390 | level = TPACPI_FAN_LAST_LEVEL; | |
6391 | break; | |
6392 | case 2: | |
6393 | level = TP_EC_FAN_AUTO; | |
6394 | break; | |
6395 | case 3: | |
6396 | /* reserved for software-controlled auto mode */ | |
6397 | return -ENOSYS; | |
18ad7996 | 6398 | default: |
b21a15f6 | 6399 | return -EINVAL; |
18ad7996 | 6400 | } |
b21a15f6 HMH |
6401 | |
6402 | res = fan_set_level_safe(level); | |
6403 | if (res == -ENXIO) | |
6404 | return -EINVAL; | |
6405 | else if (res < 0) | |
6406 | return res; | |
6407 | ||
6408 | fan_watchdog_reset(); | |
6409 | ||
6410 | return count; | |
18ad7996 HMH |
6411 | } |
6412 | ||
b21a15f6 HMH |
6413 | static struct device_attribute dev_attr_fan_pwm1_enable = |
6414 | __ATTR(pwm1_enable, S_IWUSR | S_IRUGO, | |
6415 | fan_pwm1_enable_show, fan_pwm1_enable_store); | |
6416 | ||
6417 | /* sysfs fan pwm1 ------------------------------------------------------ */ | |
6418 | static ssize_t fan_pwm1_show(struct device *dev, | |
6419 | struct device_attribute *attr, | |
6420 | char *buf) | |
fe98a52c | 6421 | { |
b21a15f6 HMH |
6422 | int res; |
6423 | u8 status; | |
fe98a52c | 6424 | |
b21a15f6 HMH |
6425 | res = fan_get_status_safe(&status); |
6426 | if (res) | |
6427 | return res; | |
ecf2a80a | 6428 | |
b21a15f6 HMH |
6429 | if ((status & |
6430 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0) | |
6431 | status = fan_control_desired_level; | |
fe98a52c | 6432 | |
b21a15f6 HMH |
6433 | if (status > 7) |
6434 | status = 7; | |
fe98a52c | 6435 | |
b21a15f6 | 6436 | return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7); |
fe98a52c HMH |
6437 | } |
6438 | ||
b21a15f6 HMH |
6439 | static ssize_t fan_pwm1_store(struct device *dev, |
6440 | struct device_attribute *attr, | |
6441 | const char *buf, size_t count) | |
18ad7996 | 6442 | { |
b21a15f6 | 6443 | unsigned long s; |
1c6a334e | 6444 | int rc; |
b21a15f6 | 6445 | u8 status, newlevel; |
1c6a334e | 6446 | |
b21a15f6 HMH |
6447 | if (parse_strtoul(buf, 255, &s)) |
6448 | return -EINVAL; | |
6449 | ||
6450 | /* scale down from 0-255 to 0-7 */ | |
6451 | newlevel = (s >> 5) & 0x07; | |
ecf2a80a | 6452 | |
7646ea88 | 6453 | if (mutex_lock_killable(&fan_mutex)) |
fc589a3c | 6454 | return -ERESTARTSYS; |
40ca9fdf | 6455 | |
b21a15f6 HMH |
6456 | rc = fan_get_status(&status); |
6457 | if (!rc && (status & | |
6458 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { | |
6459 | rc = fan_set_level(newlevel); | |
6460 | if (rc == -ENXIO) | |
6461 | rc = -EINVAL; | |
6462 | else if (!rc) { | |
6463 | fan_update_desired_level(newlevel); | |
6464 | fan_watchdog_reset(); | |
eaa7571b | 6465 | } |
b21a15f6 | 6466 | } |
1c6a334e | 6467 | |
b21a15f6 HMH |
6468 | mutex_unlock(&fan_mutex); |
6469 | return (rc)? rc : count; | |
6470 | } | |
1c6a334e | 6471 | |
b21a15f6 HMH |
6472 | static struct device_attribute dev_attr_fan_pwm1 = |
6473 | __ATTR(pwm1, S_IWUSR | S_IRUGO, | |
6474 | fan_pwm1_show, fan_pwm1_store); | |
1c6a334e | 6475 | |
b21a15f6 HMH |
6476 | /* sysfs fan fan1_input ------------------------------------------------ */ |
6477 | static ssize_t fan_fan1_input_show(struct device *dev, | |
6478 | struct device_attribute *attr, | |
6479 | char *buf) | |
6480 | { | |
6481 | int res; | |
6482 | unsigned int speed; | |
1c6a334e | 6483 | |
b21a15f6 HMH |
6484 | res = fan_get_speed(&speed); |
6485 | if (res < 0) | |
6486 | return res; | |
1c6a334e | 6487 | |
b21a15f6 HMH |
6488 | return snprintf(buf, PAGE_SIZE, "%u\n", speed); |
6489 | } | |
18ad7996 | 6490 | |
b21a15f6 HMH |
6491 | static struct device_attribute dev_attr_fan_fan1_input = |
6492 | __ATTR(fan1_input, S_IRUGO, | |
6493 | fan_fan1_input_show, NULL); | |
40ca9fdf | 6494 | |
b21a15f6 HMH |
6495 | /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */ |
6496 | static ssize_t fan_fan_watchdog_show(struct device_driver *drv, | |
6497 | char *buf) | |
6498 | { | |
6499 | return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval); | |
18ad7996 HMH |
6500 | } |
6501 | ||
b21a15f6 HMH |
6502 | static ssize_t fan_fan_watchdog_store(struct device_driver *drv, |
6503 | const char *buf, size_t count) | |
18ad7996 | 6504 | { |
b21a15f6 HMH |
6505 | unsigned long t; |
6506 | ||
6507 | if (parse_strtoul(buf, 120, &t)) | |
6508 | return -EINVAL; | |
40ca9fdf | 6509 | |
ecf2a80a HMH |
6510 | if (!fan_control_allowed) |
6511 | return -EPERM; | |
6512 | ||
b21a15f6 HMH |
6513 | fan_watchdog_maxinterval = t; |
6514 | fan_watchdog_reset(); | |
40ca9fdf | 6515 | |
b21a15f6 HMH |
6516 | return count; |
6517 | } | |
6518 | ||
6519 | static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO, | |
6520 | fan_fan_watchdog_show, fan_fan_watchdog_store); | |
6521 | ||
6522 | /* --------------------------------------------------------------------- */ | |
6523 | static struct attribute *fan_attributes[] = { | |
6524 | &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr, | |
6525 | &dev_attr_fan_fan1_input.attr, | |
6526 | NULL | |
6527 | }; | |
6528 | ||
6529 | static const struct attribute_group fan_attr_group = { | |
6530 | .attrs = fan_attributes, | |
6531 | }; | |
6532 | ||
6533 | static int __init fan_init(struct ibm_init_struct *iibm) | |
6534 | { | |
6535 | int rc; | |
6536 | ||
6537 | vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n"); | |
6538 | ||
6539 | mutex_init(&fan_mutex); | |
6540 | fan_status_access_mode = TPACPI_FAN_NONE; | |
6541 | fan_control_access_mode = TPACPI_FAN_WR_NONE; | |
6542 | fan_control_commands = 0; | |
6543 | fan_watchdog_maxinterval = 0; | |
6544 | tp_features.fan_ctrl_status_undef = 0; | |
6545 | fan_control_desired_level = 7; | |
6546 | ||
e0c7dfe7 HMH |
6547 | TPACPI_ACPIHANDLE_INIT(fans); |
6548 | TPACPI_ACPIHANDLE_INIT(gfan); | |
6549 | TPACPI_ACPIHANDLE_INIT(sfan); | |
b21a15f6 HMH |
6550 | |
6551 | if (gfan_handle) { | |
6552 | /* 570, 600e/x, 770e, 770x */ | |
6553 | fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN; | |
6554 | } else { | |
6555 | /* all other ThinkPads: note that even old-style | |
6556 | * ThinkPad ECs supports the fan control register */ | |
6557 | if (likely(acpi_ec_read(fan_status_offset, | |
6558 | &fan_control_initial_status))) { | |
6559 | fan_status_access_mode = TPACPI_FAN_RD_TPEC; | |
1c2ece75 | 6560 | fan_quirk1_detect(); |
b21a15f6 | 6561 | } else { |
e0c7dfe7 | 6562 | printk(TPACPI_ERR |
b21a15f6 HMH |
6563 | "ThinkPad ACPI EC access misbehaving, " |
6564 | "fan status and control unavailable\n"); | |
6565 | return 1; | |
6566 | } | |
6567 | } | |
6568 | ||
6569 | if (sfan_handle) { | |
6570 | /* 570, 770x-JL */ | |
6571 | fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN; | |
6572 | fan_control_commands |= | |
6573 | TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE; | |
6574 | } else { | |
6575 | if (!gfan_handle) { | |
6576 | /* gfan without sfan means no fan control */ | |
6577 | /* all other models implement TP EC 0x2f control */ | |
6578 | ||
6579 | if (fans_handle) { | |
6580 | /* X31, X40, X41 */ | |
6581 | fan_control_access_mode = | |
6582 | TPACPI_FAN_WR_ACPI_FANS; | |
6583 | fan_control_commands |= | |
6584 | TPACPI_FAN_CMD_SPEED | | |
6585 | TPACPI_FAN_CMD_LEVEL | | |
6586 | TPACPI_FAN_CMD_ENABLE; | |
6587 | } else { | |
6588 | fan_control_access_mode = TPACPI_FAN_WR_TPEC; | |
6589 | fan_control_commands |= | |
6590 | TPACPI_FAN_CMD_LEVEL | | |
6591 | TPACPI_FAN_CMD_ENABLE; | |
6592 | } | |
fe98a52c | 6593 | } |
b21a15f6 | 6594 | } |
18ad7996 | 6595 | |
b21a15f6 HMH |
6596 | vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n", |
6597 | str_supported(fan_status_access_mode != TPACPI_FAN_NONE || | |
6598 | fan_control_access_mode != TPACPI_FAN_WR_NONE), | |
6599 | fan_status_access_mode, fan_control_access_mode); | |
1c6a334e | 6600 | |
b21a15f6 HMH |
6601 | /* fan control master switch */ |
6602 | if (!fan_control_allowed) { | |
6603 | fan_control_access_mode = TPACPI_FAN_WR_NONE; | |
6604 | fan_control_commands = 0; | |
6605 | dbg_printk(TPACPI_DBG_INIT, | |
6606 | "fan control features disabled by parameter\n"); | |
18ad7996 | 6607 | } |
40ca9fdf | 6608 | |
b21a15f6 HMH |
6609 | /* update fan_control_desired_level */ |
6610 | if (fan_status_access_mode != TPACPI_FAN_NONE) | |
6611 | fan_get_status_safe(NULL); | |
fe98a52c | 6612 | |
b21a15f6 HMH |
6613 | if (fan_status_access_mode != TPACPI_FAN_NONE || |
6614 | fan_control_access_mode != TPACPI_FAN_WR_NONE) { | |
6615 | rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj, | |
6616 | &fan_attr_group); | |
b21a15f6 HMH |
6617 | if (rc < 0) |
6618 | return rc; | |
9c0a76e1 HMH |
6619 | |
6620 | rc = driver_create_file(&tpacpi_hwmon_pdriver.driver, | |
6621 | &driver_attr_fan_watchdog); | |
6622 | if (rc < 0) { | |
6623 | sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, | |
6624 | &fan_attr_group); | |
6625 | return rc; | |
6626 | } | |
b21a15f6 HMH |
6627 | return 0; |
6628 | } else | |
6629 | return 1; | |
56b6aeb0 HMH |
6630 | } |
6631 | ||
b21a15f6 | 6632 | static void fan_exit(void) |
56b6aeb0 | 6633 | { |
35ff8b9f HMH |
6634 | vdbg_printk(TPACPI_DBG_EXIT, |
6635 | "cancelling any pending fan watchdog tasks\n"); | |
56b6aeb0 | 6636 | |
b21a15f6 HMH |
6637 | /* FIXME: can we really do this unconditionally? */ |
6638 | sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group); | |
35ff8b9f HMH |
6639 | driver_remove_file(&tpacpi_hwmon_pdriver.driver, |
6640 | &driver_attr_fan_watchdog); | |
40ca9fdf | 6641 | |
b21a15f6 | 6642 | cancel_delayed_work(&fan_watchdog_task); |
e0e3c061 | 6643 | flush_workqueue(tpacpi_wq); |
56b6aeb0 HMH |
6644 | } |
6645 | ||
75700e53 HMH |
6646 | static void fan_suspend(pm_message_t state) |
6647 | { | |
0081b162 HMH |
6648 | int rc; |
6649 | ||
75700e53 HMH |
6650 | if (!fan_control_allowed) |
6651 | return; | |
6652 | ||
6653 | /* Store fan status in cache */ | |
0081b162 HMH |
6654 | fan_control_resume_level = 0; |
6655 | rc = fan_get_status_safe(&fan_control_resume_level); | |
6656 | if (rc < 0) | |
6657 | printk(TPACPI_NOTICE | |
6658 | "failed to read fan level for later " | |
6659 | "restore during resume: %d\n", rc); | |
6660 | ||
6661 | /* if it is undefined, don't attempt to restore it. | |
6662 | * KEEP THIS LAST */ | |
75700e53 | 6663 | if (tp_features.fan_ctrl_status_undef) |
0081b162 | 6664 | fan_control_resume_level = 0; |
75700e53 HMH |
6665 | } |
6666 | ||
6667 | static void fan_resume(void) | |
6668 | { | |
75700e53 HMH |
6669 | u8 current_level = 7; |
6670 | bool do_set = false; | |
0081b162 | 6671 | int rc; |
75700e53 HMH |
6672 | |
6673 | /* DSDT *always* updates status on resume */ | |
6674 | tp_features.fan_ctrl_status_undef = 0; | |
6675 | ||
75700e53 | 6676 | if (!fan_control_allowed || |
0081b162 | 6677 | !fan_control_resume_level || |
75700e53 HMH |
6678 | (fan_get_status_safe(¤t_level) < 0)) |
6679 | return; | |
6680 | ||
6681 | switch (fan_control_access_mode) { | |
6682 | case TPACPI_FAN_WR_ACPI_SFAN: | |
0081b162 HMH |
6683 | /* never decrease fan level */ |
6684 | do_set = (fan_control_resume_level > current_level); | |
75700e53 HMH |
6685 | break; |
6686 | case TPACPI_FAN_WR_ACPI_FANS: | |
6687 | case TPACPI_FAN_WR_TPEC: | |
0081b162 HMH |
6688 | /* never decrease fan level, scale is: |
6689 | * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO | |
6690 | * | |
6691 | * We expect the firmware to set either 7 or AUTO, but we | |
6692 | * handle FULLSPEED out of paranoia. | |
6693 | * | |
6694 | * So, we can safely only restore FULLSPEED or 7, anything | |
6695 | * else could slow the fan. Restoring AUTO is useless, at | |
6696 | * best that's exactly what the DSDT already set (it is the | |
6697 | * slower it uses). | |
6698 | * | |
6699 | * Always keep in mind that the DSDT *will* have set the | |
6700 | * fans to what the vendor supposes is the best level. We | |
6701 | * muck with it only to speed the fan up. | |
6702 | */ | |
6703 | if (fan_control_resume_level != 7 && | |
6704 | !(fan_control_resume_level & TP_EC_FAN_FULLSPEED)) | |
6705 | return; | |
6706 | else | |
6707 | do_set = !(current_level & TP_EC_FAN_FULLSPEED) && | |
6708 | (current_level != fan_control_resume_level); | |
75700e53 HMH |
6709 | break; |
6710 | default: | |
6711 | return; | |
6712 | } | |
6713 | if (do_set) { | |
6714 | printk(TPACPI_NOTICE | |
6715 | "restoring fan level to 0x%02x\n", | |
0081b162 HMH |
6716 | fan_control_resume_level); |
6717 | rc = fan_set_level_safe(fan_control_resume_level); | |
6718 | if (rc < 0) | |
6719 | printk(TPACPI_NOTICE | |
6720 | "failed to restore fan level: %d\n", rc); | |
75700e53 HMH |
6721 | } |
6722 | } | |
6723 | ||
56b6aeb0 HMH |
6724 | static int fan_read(char *p) |
6725 | { | |
6726 | int len = 0; | |
6727 | int rc; | |
6728 | u8 status; | |
6729 | unsigned int speed = 0; | |
6730 | ||
6731 | switch (fan_status_access_mode) { | |
efa27145 | 6732 | case TPACPI_FAN_RD_ACPI_GFAN: |
56b6aeb0 | 6733 | /* 570, 600e/x, 770e, 770x */ |
35ff8b9f HMH |
6734 | rc = fan_get_status_safe(&status); |
6735 | if (rc < 0) | |
56b6aeb0 HMH |
6736 | return rc; |
6737 | ||
6738 | len += sprintf(p + len, "status:\t\t%s\n" | |
6739 | "level:\t\t%d\n", | |
6740 | (status != 0) ? "enabled" : "disabled", status); | |
6741 | break; | |
6742 | ||
efa27145 | 6743 | case TPACPI_FAN_RD_TPEC: |
56b6aeb0 | 6744 | /* all except 570, 600e/x, 770e, 770x */ |
35ff8b9f HMH |
6745 | rc = fan_get_status_safe(&status); |
6746 | if (rc < 0) | |
56b6aeb0 HMH |
6747 | return rc; |
6748 | ||
56b6aeb0 HMH |
6749 | len += sprintf(p + len, "status:\t\t%s\n", |
6750 | (status != 0) ? "enabled" : "disabled"); | |
6751 | ||
35ff8b9f HMH |
6752 | rc = fan_get_speed(&speed); |
6753 | if (rc < 0) | |
56b6aeb0 HMH |
6754 | return rc; |
6755 | ||
6756 | len += sprintf(p + len, "speed:\t\t%d\n", speed); | |
6757 | ||
efa27145 | 6758 | if (status & TP_EC_FAN_FULLSPEED) |
56b6aeb0 HMH |
6759 | /* Disengaged mode takes precedence */ |
6760 | len += sprintf(p + len, "level:\t\tdisengaged\n"); | |
efa27145 | 6761 | else if (status & TP_EC_FAN_AUTO) |
56b6aeb0 HMH |
6762 | len += sprintf(p + len, "level:\t\tauto\n"); |
6763 | else | |
6764 | len += sprintf(p + len, "level:\t\t%d\n", status); | |
6765 | break; | |
6766 | ||
efa27145 | 6767 | case TPACPI_FAN_NONE: |
56b6aeb0 HMH |
6768 | default: |
6769 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
6770 | } | |
6771 | ||
efa27145 | 6772 | if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) { |
56b6aeb0 HMH |
6773 | len += sprintf(p + len, "commands:\tlevel <level>"); |
6774 | ||
6775 | switch (fan_control_access_mode) { | |
efa27145 | 6776 | case TPACPI_FAN_WR_ACPI_SFAN: |
56b6aeb0 HMH |
6777 | len += sprintf(p + len, " (<level> is 0-7)\n"); |
6778 | break; | |
6779 | ||
6780 | default: | |
6781 | len += sprintf(p + len, " (<level> is 0-7, " | |
fe98a52c | 6782 | "auto, disengaged, full-speed)\n"); |
56b6aeb0 HMH |
6783 | break; |
6784 | } | |
6785 | } | |
18ad7996 | 6786 | |
efa27145 | 6787 | if (fan_control_commands & TPACPI_FAN_CMD_ENABLE) |
56b6aeb0 | 6788 | len += sprintf(p + len, "commands:\tenable, disable\n" |
35ff8b9f HMH |
6789 | "commands:\twatchdog <timeout> (<timeout> " |
6790 | "is 0 (off), 1-120 (seconds))\n"); | |
1da177e4 | 6791 | |
efa27145 | 6792 | if (fan_control_commands & TPACPI_FAN_CMD_SPEED) |
56b6aeb0 HMH |
6793 | len += sprintf(p + len, "commands:\tspeed <speed>" |
6794 | " (<speed> is 0-65535)\n"); | |
6795 | ||
6796 | return len; | |
78f81cc4 BD |
6797 | } |
6798 | ||
18ad7996 HMH |
6799 | static int fan_write_cmd_level(const char *cmd, int *rc) |
6800 | { | |
6801 | int level; | |
6802 | ||
a12095c2 | 6803 | if (strlencmp(cmd, "level auto") == 0) |
efa27145 | 6804 | level = TP_EC_FAN_AUTO; |
fe98a52c | 6805 | else if ((strlencmp(cmd, "level disengaged") == 0) | |
35ff8b9f | 6806 | (strlencmp(cmd, "level full-speed") == 0)) |
efa27145 | 6807 | level = TP_EC_FAN_FULLSPEED; |
a12095c2 | 6808 | else if (sscanf(cmd, "level %d", &level) != 1) |
18ad7996 HMH |
6809 | return 0; |
6810 | ||
35ff8b9f HMH |
6811 | *rc = fan_set_level_safe(level); |
6812 | if (*rc == -ENXIO) | |
e0c7dfe7 | 6813 | printk(TPACPI_ERR "level command accepted for unsupported " |
18ad7996 HMH |
6814 | "access mode %d", fan_control_access_mode); |
6815 | ||
6816 | return 1; | |
6817 | } | |
6818 | ||
6819 | static int fan_write_cmd_enable(const char *cmd, int *rc) | |
6820 | { | |
6821 | if (strlencmp(cmd, "enable") != 0) | |
6822 | return 0; | |
6823 | ||
35ff8b9f HMH |
6824 | *rc = fan_set_enable(); |
6825 | if (*rc == -ENXIO) | |
e0c7dfe7 | 6826 | printk(TPACPI_ERR "enable command accepted for unsupported " |
18ad7996 HMH |
6827 | "access mode %d", fan_control_access_mode); |
6828 | ||
6829 | return 1; | |
6830 | } | |
6831 | ||
6832 | static int fan_write_cmd_disable(const char *cmd, int *rc) | |
6833 | { | |
6834 | if (strlencmp(cmd, "disable") != 0) | |
6835 | return 0; | |
6836 | ||
35ff8b9f HMH |
6837 | *rc = fan_set_disable(); |
6838 | if (*rc == -ENXIO) | |
e0c7dfe7 | 6839 | printk(TPACPI_ERR "disable command accepted for unsupported " |
18ad7996 HMH |
6840 | "access mode %d", fan_control_access_mode); |
6841 | ||
6842 | return 1; | |
6843 | } | |
6844 | ||
6845 | static int fan_write_cmd_speed(const char *cmd, int *rc) | |
6846 | { | |
6847 | int speed; | |
6848 | ||
a8b7a662 HMH |
6849 | /* TODO: |
6850 | * Support speed <low> <medium> <high> ? */ | |
6851 | ||
18ad7996 HMH |
6852 | if (sscanf(cmd, "speed %d", &speed) != 1) |
6853 | return 0; | |
6854 | ||
35ff8b9f HMH |
6855 | *rc = fan_set_speed(speed); |
6856 | if (*rc == -ENXIO) | |
e0c7dfe7 | 6857 | printk(TPACPI_ERR "speed command accepted for unsupported " |
18ad7996 HMH |
6858 | "access mode %d", fan_control_access_mode); |
6859 | ||
6860 | return 1; | |
6861 | } | |
6862 | ||
16663a87 HMH |
6863 | static int fan_write_cmd_watchdog(const char *cmd, int *rc) |
6864 | { | |
6865 | int interval; | |
6866 | ||
6867 | if (sscanf(cmd, "watchdog %d", &interval) != 1) | |
6868 | return 0; | |
6869 | ||
6870 | if (interval < 0 || interval > 120) | |
6871 | *rc = -EINVAL; | |
6872 | else | |
6873 | fan_watchdog_maxinterval = interval; | |
6874 | ||
6875 | return 1; | |
6876 | } | |
6877 | ||
18ad7996 HMH |
6878 | static int fan_write(char *buf) |
6879 | { | |
6880 | char *cmd; | |
6881 | int rc = 0; | |
6882 | ||
6883 | while (!rc && (cmd = next_cmd(&buf))) { | |
efa27145 | 6884 | if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) && |
18ad7996 | 6885 | fan_write_cmd_level(cmd, &rc)) && |
efa27145 | 6886 | !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) && |
18ad7996 | 6887 | (fan_write_cmd_enable(cmd, &rc) || |
16663a87 HMH |
6888 | fan_write_cmd_disable(cmd, &rc) || |
6889 | fan_write_cmd_watchdog(cmd, &rc))) && | |
efa27145 | 6890 | !((fan_control_commands & TPACPI_FAN_CMD_SPEED) && |
18ad7996 HMH |
6891 | fan_write_cmd_speed(cmd, &rc)) |
6892 | ) | |
6893 | rc = -EINVAL; | |
16663a87 HMH |
6894 | else if (!rc) |
6895 | fan_watchdog_reset(); | |
18ad7996 HMH |
6896 | } |
6897 | ||
6898 | return rc; | |
6899 | } | |
6900 | ||
a5763f22 HMH |
6901 | static struct ibm_struct fan_driver_data = { |
6902 | .name = "fan", | |
6903 | .read = fan_read, | |
6904 | .write = fan_write, | |
6905 | .exit = fan_exit, | |
75700e53 HMH |
6906 | .suspend = fan_suspend, |
6907 | .resume = fan_resume, | |
a5763f22 HMH |
6908 | }; |
6909 | ||
56b6aeb0 HMH |
6910 | /**************************************************************************** |
6911 | **************************************************************************** | |
6912 | * | |
6913 | * Infrastructure | |
6914 | * | |
6915 | **************************************************************************** | |
6916 | ****************************************************************************/ | |
6917 | ||
7fd40029 HMH |
6918 | /* sysfs name ---------------------------------------------------------- */ |
6919 | static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev, | |
6920 | struct device_attribute *attr, | |
6921 | char *buf) | |
6922 | { | |
e0c7dfe7 | 6923 | return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME); |
7fd40029 HMH |
6924 | } |
6925 | ||
6926 | static struct device_attribute dev_attr_thinkpad_acpi_pdev_name = | |
6927 | __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL); | |
6928 | ||
6929 | /* --------------------------------------------------------------------- */ | |
6930 | ||
56b6aeb0 | 6931 | /* /proc support */ |
94954cc6 | 6932 | static struct proc_dir_entry *proc_dir; |
16663a87 | 6933 | |
56b6aeb0 HMH |
6934 | /* |
6935 | * Module and infrastructure proble, init and exit handling | |
6936 | */ | |
1da177e4 | 6937 | |
b21a15f6 HMH |
6938 | static int force_load; |
6939 | ||
fe08bc4b | 6940 | #ifdef CONFIG_THINKPAD_ACPI_DEBUG |
a5763f22 | 6941 | static const char * __init str_supported(int is_supported) |
fe08bc4b | 6942 | { |
a5763f22 | 6943 | static char text_unsupported[] __initdata = "not supported"; |
fe08bc4b | 6944 | |
a5763f22 | 6945 | return (is_supported)? &text_unsupported[4] : &text_unsupported[0]; |
fe08bc4b HMH |
6946 | } |
6947 | #endif /* CONFIG_THINKPAD_ACPI_DEBUG */ | |
6948 | ||
b21a15f6 HMH |
6949 | static void ibm_exit(struct ibm_struct *ibm) |
6950 | { | |
6951 | dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name); | |
6952 | ||
6953 | list_del_init(&ibm->all_drivers); | |
6954 | ||
6955 | if (ibm->flags.acpi_notify_installed) { | |
6956 | dbg_printk(TPACPI_DBG_EXIT, | |
6957 | "%s: acpi_remove_notify_handler\n", ibm->name); | |
6958 | BUG_ON(!ibm->acpi); | |
6959 | acpi_remove_notify_handler(*ibm->acpi->handle, | |
6960 | ibm->acpi->type, | |
6961 | dispatch_acpi_notify); | |
6962 | ibm->flags.acpi_notify_installed = 0; | |
6963 | ibm->flags.acpi_notify_installed = 0; | |
6964 | } | |
6965 | ||
6966 | if (ibm->flags.proc_created) { | |
6967 | dbg_printk(TPACPI_DBG_EXIT, | |
6968 | "%s: remove_proc_entry\n", ibm->name); | |
6969 | remove_proc_entry(ibm->name, proc_dir); | |
6970 | ibm->flags.proc_created = 0; | |
6971 | } | |
6972 | ||
6973 | if (ibm->flags.acpi_driver_registered) { | |
6974 | dbg_printk(TPACPI_DBG_EXIT, | |
6975 | "%s: acpi_bus_unregister_driver\n", ibm->name); | |
6976 | BUG_ON(!ibm->acpi); | |
6977 | acpi_bus_unregister_driver(ibm->acpi->driver); | |
6978 | kfree(ibm->acpi->driver); | |
6979 | ibm->acpi->driver = NULL; | |
6980 | ibm->flags.acpi_driver_registered = 0; | |
6981 | } | |
6982 | ||
6983 | if (ibm->flags.init_called && ibm->exit) { | |
6984 | ibm->exit(); | |
6985 | ibm->flags.init_called = 0; | |
6986 | } | |
6987 | ||
6988 | dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name); | |
6989 | } | |
f74a27d4 | 6990 | |
a5763f22 | 6991 | static int __init ibm_init(struct ibm_init_struct *iibm) |
1da177e4 LT |
6992 | { |
6993 | int ret; | |
a5763f22 | 6994 | struct ibm_struct *ibm = iibm->data; |
1da177e4 LT |
6995 | struct proc_dir_entry *entry; |
6996 | ||
a5763f22 HMH |
6997 | BUG_ON(ibm == NULL); |
6998 | ||
6999 | INIT_LIST_HEAD(&ibm->all_drivers); | |
7000 | ||
92641177 | 7001 | if (ibm->flags.experimental && !experimental) |
1da177e4 LT |
7002 | return 0; |
7003 | ||
fe08bc4b HMH |
7004 | dbg_printk(TPACPI_DBG_INIT, |
7005 | "probing for %s\n", ibm->name); | |
7006 | ||
a5763f22 HMH |
7007 | if (iibm->init) { |
7008 | ret = iibm->init(iibm); | |
5fba344c HMH |
7009 | if (ret > 0) |
7010 | return 0; /* probe failed */ | |
7011 | if (ret) | |
1da177e4 | 7012 | return ret; |
a5763f22 | 7013 | |
92641177 | 7014 | ibm->flags.init_called = 1; |
1da177e4 LT |
7015 | } |
7016 | ||
8d376cd6 HMH |
7017 | if (ibm->acpi) { |
7018 | if (ibm->acpi->hid) { | |
7019 | ret = register_tpacpi_subdriver(ibm); | |
7020 | if (ret) | |
7021 | goto err_out; | |
7022 | } | |
5fba344c | 7023 | |
8d376cd6 HMH |
7024 | if (ibm->acpi->notify) { |
7025 | ret = setup_acpi_notify(ibm); | |
7026 | if (ret == -ENODEV) { | |
e0c7dfe7 | 7027 | printk(TPACPI_NOTICE "disabling subdriver %s\n", |
8d376cd6 HMH |
7028 | ibm->name); |
7029 | ret = 0; | |
7030 | goto err_out; | |
7031 | } | |
7032 | if (ret < 0) | |
7033 | goto err_out; | |
5fba344c | 7034 | } |
5fba344c HMH |
7035 | } |
7036 | ||
fe08bc4b HMH |
7037 | dbg_printk(TPACPI_DBG_INIT, |
7038 | "%s installed\n", ibm->name); | |
7039 | ||
78f81cc4 BD |
7040 | if (ibm->read) { |
7041 | entry = create_proc_entry(ibm->name, | |
7042 | S_IFREG | S_IRUGO | S_IWUSR, | |
7043 | proc_dir); | |
7044 | if (!entry) { | |
e0c7dfe7 | 7045 | printk(TPACPI_ERR "unable to create proc entry %s\n", |
78f81cc4 | 7046 | ibm->name); |
5fba344c HMH |
7047 | ret = -ENODEV; |
7048 | goto err_out; | |
78f81cc4 BD |
7049 | } |
7050 | entry->owner = THIS_MODULE; | |
7051 | entry->data = ibm; | |
8d376cd6 | 7052 | entry->read_proc = &dispatch_procfs_read; |
78f81cc4 | 7053 | if (ibm->write) |
8d376cd6 | 7054 | entry->write_proc = &dispatch_procfs_write; |
92641177 | 7055 | ibm->flags.proc_created = 1; |
78f81cc4 | 7056 | } |
1da177e4 | 7057 | |
a5763f22 HMH |
7058 | list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers); |
7059 | ||
1da177e4 | 7060 | return 0; |
5fba344c HMH |
7061 | |
7062 | err_out: | |
fe08bc4b HMH |
7063 | dbg_printk(TPACPI_DBG_INIT, |
7064 | "%s: at error exit path with result %d\n", | |
7065 | ibm->name, ret); | |
7066 | ||
5fba344c HMH |
7067 | ibm_exit(ibm); |
7068 | return (ret < 0)? ret : 0; | |
1da177e4 LT |
7069 | } |
7070 | ||
56b6aeb0 HMH |
7071 | /* Probing */ |
7072 | ||
bf20e740 HMH |
7073 | /* returns 0 - probe ok, or < 0 - probe error. |
7074 | * Probe ok doesn't mean thinkpad found. | |
7075 | * On error, kfree() cleanup on tp->* is not performed, caller must do it */ | |
7076 | static int __must_check __init get_thinkpad_model_data( | |
7077 | struct thinkpad_id_data *tp) | |
1da177e4 | 7078 | { |
1855256c | 7079 | const struct dmi_device *dev = NULL; |
56b6aeb0 | 7080 | char ec_fw_string[18]; |
bf20e740 | 7081 | char const *s; |
1da177e4 | 7082 | |
d5a2f2f1 | 7083 | if (!tp) |
bf20e740 | 7084 | return -EINVAL; |
d5a2f2f1 HMH |
7085 | |
7086 | memset(tp, 0, sizeof(*tp)); | |
7087 | ||
7088 | if (dmi_name_in_vendors("IBM")) | |
7089 | tp->vendor = PCI_VENDOR_ID_IBM; | |
7090 | else if (dmi_name_in_vendors("LENOVO")) | |
7091 | tp->vendor = PCI_VENDOR_ID_LENOVO; | |
7092 | else | |
bf20e740 | 7093 | return 0; |
d5a2f2f1 | 7094 | |
bf20e740 HMH |
7095 | s = dmi_get_system_info(DMI_BIOS_VERSION); |
7096 | tp->bios_version_str = kstrdup(s, GFP_KERNEL); | |
7097 | if (s && !tp->bios_version_str) | |
7098 | return -ENOMEM; | |
d5a2f2f1 | 7099 | if (!tp->bios_version_str) |
bf20e740 | 7100 | return 0; |
d5a2f2f1 HMH |
7101 | tp->bios_model = tp->bios_version_str[0] |
7102 | | (tp->bios_version_str[1] << 8); | |
7103 | ||
56b6aeb0 HMH |
7104 | /* |
7105 | * ThinkPad T23 or newer, A31 or newer, R50e or newer, | |
7106 | * X32 or newer, all Z series; Some models must have an | |
7107 | * up-to-date BIOS or they will not be detected. | |
7108 | * | |
7109 | * See http://thinkwiki.org/wiki/List_of_DMI_IDs | |
7110 | */ | |
7111 | while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) { | |
7112 | if (sscanf(dev->name, | |
7113 | "IBM ThinkPad Embedded Controller -[%17c", | |
7114 | ec_fw_string) == 1) { | |
7115 | ec_fw_string[sizeof(ec_fw_string) - 1] = 0; | |
7116 | ec_fw_string[strcspn(ec_fw_string, " ]")] = 0; | |
d5a2f2f1 HMH |
7117 | |
7118 | tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL); | |
bf20e740 HMH |
7119 | if (!tp->ec_version_str) |
7120 | return -ENOMEM; | |
d5a2f2f1 HMH |
7121 | tp->ec_model = ec_fw_string[0] |
7122 | | (ec_fw_string[1] << 8); | |
7123 | break; | |
78f81cc4 | 7124 | } |
1da177e4 | 7125 | } |
d5a2f2f1 | 7126 | |
bf20e740 HMH |
7127 | s = dmi_get_system_info(DMI_PRODUCT_VERSION); |
7128 | if (s && !strnicmp(s, "ThinkPad", 8)) { | |
7129 | tp->model_str = kstrdup(s, GFP_KERNEL); | |
7130 | if (!tp->model_str) | |
7131 | return -ENOMEM; | |
d5a2f2f1 | 7132 | } |
8c74adbc | 7133 | |
bf20e740 HMH |
7134 | s = dmi_get_system_info(DMI_PRODUCT_NAME); |
7135 | tp->nummodel_str = kstrdup(s, GFP_KERNEL); | |
7136 | if (s && !tp->nummodel_str) | |
7137 | return -ENOMEM; | |
7138 | ||
7139 | return 0; | |
1da177e4 LT |
7140 | } |
7141 | ||
5fba344c HMH |
7142 | static int __init probe_for_thinkpad(void) |
7143 | { | |
7144 | int is_thinkpad; | |
7145 | ||
7146 | if (acpi_disabled) | |
7147 | return -ENODEV; | |
7148 | ||
7149 | /* | |
7150 | * Non-ancient models have better DMI tagging, but very old models | |
7151 | * don't. | |
7152 | */ | |
d5a2f2f1 | 7153 | is_thinkpad = (thinkpad_id.model_str != NULL); |
5fba344c HMH |
7154 | |
7155 | /* ec is required because many other handles are relative to it */ | |
e0c7dfe7 | 7156 | TPACPI_ACPIHANDLE_INIT(ec); |
5fba344c HMH |
7157 | if (!ec_handle) { |
7158 | if (is_thinkpad) | |
e0c7dfe7 | 7159 | printk(TPACPI_ERR |
5fba344c HMH |
7160 | "Not yet supported ThinkPad detected!\n"); |
7161 | return -ENODEV; | |
7162 | } | |
7163 | ||
0dcef77c HMH |
7164 | /* |
7165 | * Risks a regression on very old machines, but reduces potential | |
7166 | * false positives a damn great deal | |
7167 | */ | |
7168 | if (!is_thinkpad) | |
d5a2f2f1 | 7169 | is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM); |
0dcef77c HMH |
7170 | |
7171 | if (!is_thinkpad && !force_load) | |
7172 | return -ENODEV; | |
7173 | ||
5fba344c HMH |
7174 | return 0; |
7175 | } | |
7176 | ||
7177 | ||
56b6aeb0 | 7178 | /* Module init, exit, parameters */ |
1da177e4 | 7179 | |
a5763f22 HMH |
7180 | static struct ibm_init_struct ibms_init[] __initdata = { |
7181 | { | |
7182 | .init = thinkpad_acpi_driver_init, | |
7183 | .data = &thinkpad_acpi_driver_data, | |
7184 | }, | |
7185 | { | |
7186 | .init = hotkey_init, | |
7187 | .data = &hotkey_driver_data, | |
7188 | }, | |
7189 | { | |
7190 | .init = bluetooth_init, | |
7191 | .data = &bluetooth_driver_data, | |
7192 | }, | |
7193 | { | |
7194 | .init = wan_init, | |
7195 | .data = &wan_driver_data, | |
7196 | }, | |
0045c0aa HMH |
7197 | { |
7198 | .init = uwb_init, | |
7199 | .data = &uwb_driver_data, | |
7200 | }, | |
d7c1d17d | 7201 | #ifdef CONFIG_THINKPAD_ACPI_VIDEO |
a5763f22 HMH |
7202 | { |
7203 | .init = video_init, | |
7204 | .data = &video_driver_data, | |
7205 | }, | |
d7c1d17d | 7206 | #endif |
a5763f22 HMH |
7207 | { |
7208 | .init = light_init, | |
7209 | .data = &light_driver_data, | |
7210 | }, | |
7211 | #ifdef CONFIG_THINKPAD_ACPI_DOCK | |
7212 | { | |
7213 | .init = dock_init, | |
7214 | .data = &dock_driver_data[0], | |
7215 | }, | |
7216 | { | |
d94a7f16 | 7217 | .init = dock_init2, |
a5763f22 HMH |
7218 | .data = &dock_driver_data[1], |
7219 | }, | |
7220 | #endif | |
7221 | #ifdef CONFIG_THINKPAD_ACPI_BAY | |
7222 | { | |
7223 | .init = bay_init, | |
7224 | .data = &bay_driver_data, | |
7225 | }, | |
7226 | #endif | |
7227 | { | |
7228 | .init = cmos_init, | |
7229 | .data = &cmos_driver_data, | |
7230 | }, | |
7231 | { | |
7232 | .init = led_init, | |
7233 | .data = &led_driver_data, | |
7234 | }, | |
7235 | { | |
7236 | .init = beep_init, | |
7237 | .data = &beep_driver_data, | |
7238 | }, | |
7239 | { | |
7240 | .init = thermal_init, | |
7241 | .data = &thermal_driver_data, | |
7242 | }, | |
7243 | { | |
7244 | .data = &ecdump_driver_data, | |
7245 | }, | |
7246 | { | |
7247 | .init = brightness_init, | |
7248 | .data = &brightness_driver_data, | |
7249 | }, | |
7250 | { | |
7251 | .data = &volume_driver_data, | |
7252 | }, | |
7253 | { | |
7254 | .init = fan_init, | |
7255 | .data = &fan_driver_data, | |
7256 | }, | |
7257 | }; | |
7258 | ||
3945ac36 | 7259 | static int __init set_ibm_param(const char *val, struct kernel_param *kp) |
1da177e4 LT |
7260 | { |
7261 | unsigned int i; | |
a5763f22 | 7262 | struct ibm_struct *ibm; |
1da177e4 | 7263 | |
59f91ff1 HMH |
7264 | if (!kp || !kp->name || !val) |
7265 | return -EINVAL; | |
7266 | ||
a5763f22 HMH |
7267 | for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { |
7268 | ibm = ibms_init[i].data; | |
59f91ff1 HMH |
7269 | WARN_ON(ibm == NULL); |
7270 | ||
7271 | if (!ibm || !ibm->name) | |
7272 | continue; | |
a5763f22 HMH |
7273 | |
7274 | if (strcmp(ibm->name, kp->name) == 0 && ibm->write) { | |
7275 | if (strlen(val) > sizeof(ibms_init[i].param) - 2) | |
78f81cc4 | 7276 | return -ENOSPC; |
a5763f22 HMH |
7277 | strcpy(ibms_init[i].param, val); |
7278 | strcat(ibms_init[i].param, ","); | |
78f81cc4 BD |
7279 | return 0; |
7280 | } | |
a5763f22 | 7281 | } |
1da177e4 | 7282 | |
1da177e4 LT |
7283 | return -EINVAL; |
7284 | } | |
7285 | ||
56b6aeb0 | 7286 | module_param(experimental, int, 0); |
f68080f8 | 7287 | MODULE_PARM_DESC(experimental, |
35ff8b9f | 7288 | "Enables experimental features when non-zero"); |
56b6aeb0 | 7289 | |
132ce091 | 7290 | module_param_named(debug, dbg_level, uint, 0); |
f68080f8 | 7291 | MODULE_PARM_DESC(debug, "Sets debug level bit-mask"); |
132ce091 | 7292 | |
86cc9445 | 7293 | module_param(force_load, bool, 0); |
f68080f8 | 7294 | MODULE_PARM_DESC(force_load, |
35ff8b9f HMH |
7295 | "Attempts to load the driver even on a " |
7296 | "mis-identified ThinkPad when true"); | |
0dcef77c | 7297 | |
86cc9445 | 7298 | module_param_named(fan_control, fan_control_allowed, bool, 0); |
f68080f8 | 7299 | MODULE_PARM_DESC(fan_control, |
35ff8b9f | 7300 | "Enables setting fan parameters features when true"); |
ecf2a80a | 7301 | |
24d3b774 | 7302 | module_param_named(brightness_mode, brightness_mode, int, 0); |
f68080f8 | 7303 | MODULE_PARM_DESC(brightness_mode, |
35ff8b9f HMH |
7304 | "Selects brightness control strategy: " |
7305 | "0=auto, 1=EC, 2=CMOS, 3=both"); | |
24d3b774 | 7306 | |
87cc537a | 7307 | module_param(brightness_enable, uint, 0); |
f68080f8 | 7308 | MODULE_PARM_DESC(brightness_enable, |
35ff8b9f | 7309 | "Enables backlight control when 1, disables when 0"); |
87cc537a | 7310 | |
ff80f137 | 7311 | module_param(hotkey_report_mode, uint, 0); |
f68080f8 | 7312 | MODULE_PARM_DESC(hotkey_report_mode, |
35ff8b9f HMH |
7313 | "used for backwards compatibility with userspace, " |
7314 | "see documentation"); | |
ff80f137 | 7315 | |
e0c7dfe7 | 7316 | #define TPACPI_PARAM(feature) \ |
f68080f8 | 7317 | module_param_call(feature, set_ibm_param, NULL, NULL, 0); \ |
cbb14842 | 7318 | MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \ |
35ff8b9f | 7319 | "at module load, see documentation") |
1da177e4 | 7320 | |
e0c7dfe7 HMH |
7321 | TPACPI_PARAM(hotkey); |
7322 | TPACPI_PARAM(bluetooth); | |
7323 | TPACPI_PARAM(video); | |
7324 | TPACPI_PARAM(light); | |
85998248 | 7325 | #ifdef CONFIG_THINKPAD_ACPI_DOCK |
e0c7dfe7 | 7326 | TPACPI_PARAM(dock); |
63e5f248 | 7327 | #endif |
85998248 | 7328 | #ifdef CONFIG_THINKPAD_ACPI_BAY |
e0c7dfe7 | 7329 | TPACPI_PARAM(bay); |
85998248 | 7330 | #endif /* CONFIG_THINKPAD_ACPI_BAY */ |
e0c7dfe7 HMH |
7331 | TPACPI_PARAM(cmos); |
7332 | TPACPI_PARAM(led); | |
7333 | TPACPI_PARAM(beep); | |
7334 | TPACPI_PARAM(ecdump); | |
7335 | TPACPI_PARAM(brightness); | |
7336 | TPACPI_PARAM(volume); | |
7337 | TPACPI_PARAM(fan); | |
78f81cc4 | 7338 | |
a73f3091 HMH |
7339 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
7340 | module_param(dbg_wlswemul, uint, 0); | |
7341 | MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation"); | |
7342 | module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0); | |
7343 | MODULE_PARM_DESC(wlsw_state, | |
7344 | "Initial state of the emulated WLSW switch"); | |
7345 | ||
7346 | module_param(dbg_bluetoothemul, uint, 0); | |
7347 | MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation"); | |
7348 | module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0); | |
7349 | MODULE_PARM_DESC(bluetooth_state, | |
7350 | "Initial state of the emulated bluetooth switch"); | |
7351 | ||
7352 | module_param(dbg_wwanemul, uint, 0); | |
7353 | MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation"); | |
7354 | module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0); | |
7355 | MODULE_PARM_DESC(wwan_state, | |
7356 | "Initial state of the emulated WWAN switch"); | |
0045c0aa HMH |
7357 | |
7358 | module_param(dbg_uwbemul, uint, 0); | |
7359 | MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation"); | |
7360 | module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0); | |
7361 | MODULE_PARM_DESC(uwb_state, | |
7362 | "Initial state of the emulated UWB switch"); | |
a73f3091 HMH |
7363 | #endif |
7364 | ||
b21a15f6 HMH |
7365 | static void thinkpad_acpi_module_exit(void) |
7366 | { | |
7367 | struct ibm_struct *ibm, *itmp; | |
7368 | ||
7369 | tpacpi_lifecycle = TPACPI_LIFE_EXITING; | |
7370 | ||
7371 | list_for_each_entry_safe_reverse(ibm, itmp, | |
7372 | &tpacpi_all_drivers, | |
7373 | all_drivers) { | |
7374 | ibm_exit(ibm); | |
7375 | } | |
7376 | ||
7377 | dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n"); | |
7378 | ||
7379 | if (tpacpi_inputdev) { | |
7380 | if (tp_features.input_device_registered) | |
7381 | input_unregister_device(tpacpi_inputdev); | |
7382 | else | |
7383 | input_free_device(tpacpi_inputdev); | |
7384 | } | |
7385 | ||
7386 | if (tpacpi_hwmon) | |
7387 | hwmon_device_unregister(tpacpi_hwmon); | |
7388 | ||
7389 | if (tp_features.sensors_pdev_attrs_registered) | |
7390 | device_remove_file(&tpacpi_sensors_pdev->dev, | |
7391 | &dev_attr_thinkpad_acpi_pdev_name); | |
7392 | if (tpacpi_sensors_pdev) | |
7393 | platform_device_unregister(tpacpi_sensors_pdev); | |
7394 | if (tpacpi_pdev) | |
7395 | platform_device_unregister(tpacpi_pdev); | |
7396 | ||
7397 | if (tp_features.sensors_pdrv_attrs_registered) | |
7398 | tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver); | |
7399 | if (tp_features.platform_drv_attrs_registered) | |
7400 | tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver); | |
7401 | ||
7402 | if (tp_features.sensors_pdrv_registered) | |
7403 | platform_driver_unregister(&tpacpi_hwmon_pdriver); | |
7404 | ||
7405 | if (tp_features.platform_drv_registered) | |
7406 | platform_driver_unregister(&tpacpi_pdriver); | |
7407 | ||
7408 | if (proc_dir) | |
e0c7dfe7 | 7409 | remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir); |
b21a15f6 | 7410 | |
e0e3c061 HMH |
7411 | if (tpacpi_wq) |
7412 | destroy_workqueue(tpacpi_wq); | |
7413 | ||
b21a15f6 HMH |
7414 | kfree(thinkpad_id.bios_version_str); |
7415 | kfree(thinkpad_id.ec_version_str); | |
7416 | kfree(thinkpad_id.model_str); | |
7417 | } | |
7418 | ||
f74a27d4 | 7419 | |
1def7115 | 7420 | static int __init thinkpad_acpi_module_init(void) |
1da177e4 LT |
7421 | { |
7422 | int ret, i; | |
7423 | ||
8fef502e HMH |
7424 | tpacpi_lifecycle = TPACPI_LIFE_INIT; |
7425 | ||
ff80f137 HMH |
7426 | /* Parameter checking */ |
7427 | if (hotkey_report_mode > 2) | |
7428 | return -EINVAL; | |
7429 | ||
54ae1501 | 7430 | /* Driver-level probe */ |
d5a2f2f1 | 7431 | |
bf20e740 HMH |
7432 | ret = get_thinkpad_model_data(&thinkpad_id); |
7433 | if (ret) { | |
7434 | printk(TPACPI_ERR | |
7435 | "unable to get DMI data: %d\n", ret); | |
7436 | thinkpad_acpi_module_exit(); | |
7437 | return ret; | |
7438 | } | |
5fba344c | 7439 | ret = probe_for_thinkpad(); |
d5a2f2f1 HMH |
7440 | if (ret) { |
7441 | thinkpad_acpi_module_exit(); | |
5fba344c | 7442 | return ret; |
d5a2f2f1 | 7443 | } |
1da177e4 | 7444 | |
54ae1501 | 7445 | /* Driver initialization */ |
d5a2f2f1 | 7446 | |
e0c7dfe7 HMH |
7447 | TPACPI_ACPIHANDLE_INIT(ecrd); |
7448 | TPACPI_ACPIHANDLE_INIT(ecwr); | |
1da177e4 | 7449 | |
e0e3c061 HMH |
7450 | tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME); |
7451 | if (!tpacpi_wq) { | |
7452 | thinkpad_acpi_module_exit(); | |
7453 | return -ENOMEM; | |
7454 | } | |
7455 | ||
e0c7dfe7 | 7456 | proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir); |
1da177e4 | 7457 | if (!proc_dir) { |
35ff8b9f HMH |
7458 | printk(TPACPI_ERR |
7459 | "unable to create proc dir " TPACPI_PROC_DIR); | |
1def7115 | 7460 | thinkpad_acpi_module_exit(); |
1da177e4 LT |
7461 | return -ENODEV; |
7462 | } | |
7463 | proc_dir->owner = THIS_MODULE; | |
78f81cc4 | 7464 | |
54ae1501 HMH |
7465 | ret = platform_driver_register(&tpacpi_pdriver); |
7466 | if (ret) { | |
35ff8b9f HMH |
7467 | printk(TPACPI_ERR |
7468 | "unable to register main platform driver\n"); | |
54ae1501 HMH |
7469 | thinkpad_acpi_module_exit(); |
7470 | return ret; | |
7471 | } | |
ac36393d HMH |
7472 | tp_features.platform_drv_registered = 1; |
7473 | ||
7fd40029 HMH |
7474 | ret = platform_driver_register(&tpacpi_hwmon_pdriver); |
7475 | if (ret) { | |
35ff8b9f HMH |
7476 | printk(TPACPI_ERR |
7477 | "unable to register hwmon platform driver\n"); | |
7fd40029 HMH |
7478 | thinkpad_acpi_module_exit(); |
7479 | return ret; | |
7480 | } | |
7481 | tp_features.sensors_pdrv_registered = 1; | |
7482 | ||
176750d6 | 7483 | ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver); |
2369cc94 HMH |
7484 | if (!ret) { |
7485 | tp_features.platform_drv_attrs_registered = 1; | |
35ff8b9f HMH |
7486 | ret = tpacpi_create_driver_attributes( |
7487 | &tpacpi_hwmon_pdriver.driver); | |
2369cc94 | 7488 | } |
176750d6 | 7489 | if (ret) { |
35ff8b9f HMH |
7490 | printk(TPACPI_ERR |
7491 | "unable to create sysfs driver attributes\n"); | |
176750d6 HMH |
7492 | thinkpad_acpi_module_exit(); |
7493 | return ret; | |
7494 | } | |
2369cc94 | 7495 | tp_features.sensors_pdrv_attrs_registered = 1; |
176750d6 | 7496 | |
54ae1501 HMH |
7497 | |
7498 | /* Device initialization */ | |
e0c7dfe7 | 7499 | tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1, |
54ae1501 HMH |
7500 | NULL, 0); |
7501 | if (IS_ERR(tpacpi_pdev)) { | |
7502 | ret = PTR_ERR(tpacpi_pdev); | |
7503 | tpacpi_pdev = NULL; | |
e0c7dfe7 | 7504 | printk(TPACPI_ERR "unable to register platform device\n"); |
54ae1501 HMH |
7505 | thinkpad_acpi_module_exit(); |
7506 | return ret; | |
7507 | } | |
7fd40029 | 7508 | tpacpi_sensors_pdev = platform_device_register_simple( |
35ff8b9f HMH |
7509 | TPACPI_HWMON_DRVR_NAME, |
7510 | -1, NULL, 0); | |
7fd40029 HMH |
7511 | if (IS_ERR(tpacpi_sensors_pdev)) { |
7512 | ret = PTR_ERR(tpacpi_sensors_pdev); | |
7513 | tpacpi_sensors_pdev = NULL; | |
35ff8b9f HMH |
7514 | printk(TPACPI_ERR |
7515 | "unable to register hwmon platform device\n"); | |
7fd40029 HMH |
7516 | thinkpad_acpi_module_exit(); |
7517 | return ret; | |
7518 | } | |
7519 | ret = device_create_file(&tpacpi_sensors_pdev->dev, | |
7520 | &dev_attr_thinkpad_acpi_pdev_name); | |
7521 | if (ret) { | |
e0c7dfe7 | 7522 | printk(TPACPI_ERR |
35ff8b9f | 7523 | "unable to create sysfs hwmon device attributes\n"); |
7fd40029 HMH |
7524 | thinkpad_acpi_module_exit(); |
7525 | return ret; | |
7526 | } | |
7527 | tp_features.sensors_pdev_attrs_registered = 1; | |
7528 | tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev); | |
54ae1501 HMH |
7529 | if (IS_ERR(tpacpi_hwmon)) { |
7530 | ret = PTR_ERR(tpacpi_hwmon); | |
7531 | tpacpi_hwmon = NULL; | |
e0c7dfe7 | 7532 | printk(TPACPI_ERR "unable to register hwmon device\n"); |
54ae1501 HMH |
7533 | thinkpad_acpi_module_exit(); |
7534 | return ret; | |
7535 | } | |
8523ed6f | 7536 | mutex_init(&tpacpi_inputdev_send_mutex); |
7f5d1cd6 HMH |
7537 | tpacpi_inputdev = input_allocate_device(); |
7538 | if (!tpacpi_inputdev) { | |
e0c7dfe7 | 7539 | printk(TPACPI_ERR "unable to allocate input device\n"); |
7f5d1cd6 HMH |
7540 | thinkpad_acpi_module_exit(); |
7541 | return -ENOMEM; | |
7542 | } else { | |
7543 | /* Prepare input device, but don't register */ | |
7544 | tpacpi_inputdev->name = "ThinkPad Extra Buttons"; | |
e0c7dfe7 | 7545 | tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0"; |
7f5d1cd6 | 7546 | tpacpi_inputdev->id.bustype = BUS_HOST; |
edf0e0e5 HMH |
7547 | tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ? |
7548 | thinkpad_id.vendor : | |
7549 | PCI_VENDOR_ID_IBM; | |
7f5d1cd6 HMH |
7550 | tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT; |
7551 | tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION; | |
7552 | } | |
a5763f22 HMH |
7553 | for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { |
7554 | ret = ibm_init(&ibms_init[i]); | |
7555 | if (ret >= 0 && *ibms_init[i].param) | |
7556 | ret = ibms_init[i].data->write(ibms_init[i].param); | |
1da177e4 | 7557 | if (ret < 0) { |
1def7115 | 7558 | thinkpad_acpi_module_exit(); |
1da177e4 LT |
7559 | return ret; |
7560 | } | |
7561 | } | |
7f5d1cd6 HMH |
7562 | ret = input_register_device(tpacpi_inputdev); |
7563 | if (ret < 0) { | |
e0c7dfe7 | 7564 | printk(TPACPI_ERR "unable to register input device\n"); |
7f5d1cd6 HMH |
7565 | thinkpad_acpi_module_exit(); |
7566 | return ret; | |
7567 | } else { | |
7568 | tp_features.input_device_registered = 1; | |
7569 | } | |
1da177e4 | 7570 | |
8fef502e | 7571 | tpacpi_lifecycle = TPACPI_LIFE_RUNNING; |
1da177e4 LT |
7572 | return 0; |
7573 | } | |
7574 | ||
95e57ab2 HMH |
7575 | MODULE_ALIAS(TPACPI_DRVR_SHORTNAME); |
7576 | ||
f68080f8 HMH |
7577 | /* |
7578 | * DMI matching for module autoloading | |
7579 | * | |
7580 | * See http://thinkwiki.org/wiki/List_of_DMI_IDs | |
7581 | * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads | |
7582 | * | |
7583 | * Only models listed in thinkwiki will be supported, so add yours | |
7584 | * if it is not there yet. | |
7585 | */ | |
7586 | #define IBM_BIOS_MODULE_ALIAS(__type) \ | |
b36a50f9 | 7587 | MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*") |
f68080f8 HMH |
7588 | |
7589 | /* Non-ancient thinkpads */ | |
7590 | MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*"); | |
7591 | MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*"); | |
7592 | ||
7593 | /* Ancient thinkpad BIOSes have to be identified by | |
7594 | * BIOS type or model number, and there are far less | |
7595 | * BIOS types than model numbers... */ | |
b36a50f9 MCS |
7596 | IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]"); |
7597 | IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]"); | |
7598 | IBM_BIOS_MODULE_ALIAS("K[UX-Z]"); | |
f68080f8 HMH |
7599 | |
7600 | MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh"); | |
e0c7dfe7 HMH |
7601 | MODULE_DESCRIPTION(TPACPI_DESC); |
7602 | MODULE_VERSION(TPACPI_VERSION); | |
f68080f8 HMH |
7603 | MODULE_LICENSE("GPL"); |
7604 | ||
1def7115 HMH |
7605 | module_init(thinkpad_acpi_module_init); |
7606 | module_exit(thinkpad_acpi_module_exit); |