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