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