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