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