Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
thinkpad_acpi.c
Go to the documentation of this file.
1 /*
2  * thinkpad_acpi.c - ThinkPad ACPI Extras
3  *
4  *
5  * Copyright (C) 2004-2005 Borislav Deianov <[email protected]>
6  * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <[email protected]>
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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 
26 #define TPACPI_VERSION "0.24"
27 #define TPACPI_SYSFS_VERSION 0x020700
28 
29 /*
30  * Changelog:
31  * 2007-10-20 changelog trimmed down
32  *
33  * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34  * drivers/misc.
35  *
36  * 2006-11-22 0.13 new maintainer
37  * changelog now lives in git commit history, and will
38  * not be updated further in-file.
39  *
40  * 2005-03-17 0.11 support for 600e, 770x
41  * thanks to Jamie Lentin <[email protected]>
42  *
43  * 2005-01-16 0.9 use MODULE_VERSION
44  * thanks to Henrik Brix Andersen <[email protected]>
45  * fix parameter passing on module loading
46  * thanks to Rusty Russell <[email protected]>
47  * thanks to Jim Radford <[email protected]>
48  * 2004-11-08 0.8 fix init error case, don't return from a macro
49  * thanks to Chris Wright <[email protected]>
50  */
51 
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 
65 #include <linux/nvram.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/sysfs.h>
69 #include <linux/backlight.h>
70 #include <linux/fb.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <asm/uaccess.h>
78 
79 #include <linux/dmi.h>
80 #include <linux/jiffies.h>
81 #include <linux/workqueue.h>
82 
83 #include <sound/core.h>
84 #include <sound/control.h>
85 #include <sound/initval.h>
86 
87 #include <acpi/acpi_drivers.h>
88 
89 #include <linux/pci_ids.h>
90 
91 
92 /* ThinkPad CMOS commands */
93 #define TP_CMOS_VOLUME_DOWN 0
94 #define TP_CMOS_VOLUME_UP 1
95 #define TP_CMOS_VOLUME_MUTE 2
96 #define TP_CMOS_BRIGHTNESS_UP 4
97 #define TP_CMOS_BRIGHTNESS_DOWN 5
98 #define TP_CMOS_THINKLIGHT_ON 12
99 #define TP_CMOS_THINKLIGHT_OFF 13
100 
101 /* NVRAM Addresses */
108 };
109 
110 /* NVRAM bit masks */
111 enum {
125 };
126 
127 /* Misc NVRAM-related */
128 enum {
130 };
131 
132 /* ACPI HIDs */
133 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
136 
137 /* Input IDs */
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
140 
141 /* ACPI \WGSV commands */
142 enum {
143  TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144  TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145  TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146  TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
147 };
148 
149 /* TP_ACPI_WGSV_GET_STATE bits */
150 enum {
151  TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152  TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153  TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154  TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155  TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156  TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157  TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158  TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159  TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160  TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161 };
162 
163 /* HKEY events */
165  /* Hotkey-related */
166  TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167  TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168  TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169  TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170  TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171  TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
172 
173  /* Reasons for waking up from S3/S4 */
174  TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175  TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176  TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177  TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178  TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179  TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
180 
181  /* Auto-sleep after eject request */
182  TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183  TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
184 
185  /* Misc bay events */
186  TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
187  TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
188  or port replicator */
189  TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
190  dock or port replicator */
191 
192  /* User-interface events */
193  TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
194  TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
195  TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
196  TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
197  TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
198  TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
199  TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
200 
201  /* Key-related user-interface events */
202  TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
203  TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
204 
205  /* Thermal events */
206  TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
207  TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
208  TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
209  TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
210  TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
211 
212  TP_HKEY_EV_UNK_6040 = 0x6040, /* Related to AC change?
213  some sort of APM hint,
214  W520 */
215 
216  /* Misc */
217  TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
218 };
219 
220 /****************************************************************************
221  * Main driver
222  */
223 
224 #define TPACPI_NAME "thinkpad"
225 #define TPACPI_DESC "ThinkPad ACPI Extras"
226 #define TPACPI_FILE TPACPI_NAME "_acpi"
227 #define TPACPI_URL "http://ibm-acpi.sf.net/"
228 #define TPACPI_MAIL "[email protected]"
229 
230 #define TPACPI_PROC_DIR "ibm"
231 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
232 #define TPACPI_DRVR_NAME TPACPI_FILE
233 #define TPACPI_DRVR_SHORTNAME "tpacpi"
234 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
235 
236 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
237 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
238 
239 #define TPACPI_MAX_ACPI_ARGS 3
240 
241 /* Debugging printk groups */
242 #define TPACPI_DBG_ALL 0xffff
243 #define TPACPI_DBG_DISCLOSETASK 0x8000
244 #define TPACPI_DBG_INIT 0x0001
245 #define TPACPI_DBG_EXIT 0x0002
246 #define TPACPI_DBG_RFKILL 0x0004
247 #define TPACPI_DBG_HKEY 0x0008
248 #define TPACPI_DBG_FAN 0x0010
249 #define TPACPI_DBG_BRGHT 0x0020
250 #define TPACPI_DBG_MIXER 0x0040
251 
252 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
253 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
254 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
255 
256 
257 /****************************************************************************
258  * Driver-wide structs and misc. variables
259  */
260 
261 struct ibm_struct;
262 
264  const struct acpi_device_id *hid;
265  struct acpi_driver *driver;
266 
267  void (*notify) (struct ibm_struct *, u32);
270  struct acpi_device *device;
271 };
272 
273 struct ibm_struct {
274  char *name;
275 
276  int (*read) (struct seq_file *);
277  int (*write) (char *);
278  void (*exit) (void);
282 
284 
286 
287  struct {
293  } flags;
294 };
295 
297  char param[32];
298 
299  int (*init) (struct ibm_init_struct *);
301  struct ibm_struct *data;
302 };
303 
304 static struct {
314  u32 wan:1;
315  u32 uwb:1;
327 } tp_features;
328 
329 static struct {
332 } tp_warned;
333 
335  unsigned int vendor; /* ThinkPad vendor:
336  * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
337 
338  char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
339  char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
340 
341  u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
343  u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
345 
346  char *model_str; /* ThinkPad T43 */
347  char *nummodel_str; /* 9384A9C for a 9384-A9C model */
348 };
349 static struct thinkpad_id_data thinkpad_id;
350 
351 static enum {
355 } tpacpi_lifecycle;
356 
357 static int experimental;
358 static u32 dbg_level;
359 
360 static struct workqueue_struct *tpacpi_wq;
361 
366 };
367 
368 /* Special LED class that can defer work */
373  unsigned int led;
374 };
375 
376 /* brightness level capabilities */
377 static unsigned int bright_maxlvl; /* 0 = unknown */
378 
379 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
380 static int dbg_wlswemul;
381 static bool tpacpi_wlsw_emulstate;
382 static int dbg_bluetoothemul;
383 static bool tpacpi_bluetooth_emulstate;
384 static int dbg_wwanemul;
385 static bool tpacpi_wwan_emulstate;
386 static int dbg_uwbemul;
387 static bool tpacpi_uwb_emulstate;
388 #endif
389 
390 
391 /*************************************************************************
392  * Debugging helpers
393  */
394 
395 #define dbg_printk(a_dbg_level, format, arg...) \
396 do { \
397  if (dbg_level & (a_dbg_level)) \
398  printk(KERN_DEBUG pr_fmt("%s: " format), \
399  __func__, ##arg); \
400 } while (0)
401 
402 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
403 #define vdbg_printk dbg_printk
404 static const char *str_supported(int is_supported);
405 #else
406 static inline const char *str_supported(int is_supported) { return ""; }
407 #define vdbg_printk(a_dbg_level, format, arg...) \
408  no_printk(format, ##arg)
409 #endif
410 
411 static void tpacpi_log_usertask(const char * const what)
412 {
413  printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
414  what, task_tgid_vnr(current));
415 }
416 
417 #define tpacpi_disclose_usertask(what, format, arg...) \
418 do { \
419  if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
420  (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
421  printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
422  what, task_tgid_vnr(current), ## arg); \
423  } \
424 } while (0)
425 
426 /*
427  * Quirk handling helpers
428  *
429  * ThinkPad IDs and versions seen in the field so far
430  * are two-characters from the set [0-9A-Z], i.e. base 36.
431  *
432  * We use values well outside that range as specials.
433  */
434 
435 #define TPACPI_MATCH_ANY 0xffffU
436 #define TPACPI_MATCH_UNKNOWN 0U
437 
438 /* TPID('1', 'Y') == 0x5931 */
439 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
440 
441 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
442  { .vendor = PCI_VENDOR_ID_IBM, \
443  .bios = TPID(__id1, __id2), \
444  .ec = TPACPI_MATCH_ANY, \
445  .quirks = (__quirk) }
446 
447 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
448  { .vendor = PCI_VENDOR_ID_LENOVO, \
449  .bios = TPID(__id1, __id2), \
450  .ec = TPACPI_MATCH_ANY, \
451  .quirks = (__quirk) }
452 
453 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
454  { .vendor = PCI_VENDOR_ID_LENOVO, \
455  .bios = TPACPI_MATCH_ANY, \
456  .ec = TPID(__id1, __id2), \
457  .quirks = (__quirk) }
458 
459 struct tpacpi_quirk {
460  unsigned int vendor;
463  unsigned long quirks;
464 };
465 
479 static unsigned long __init tpacpi_check_quirks(
480  const struct tpacpi_quirk *qlist,
481  unsigned int qlist_size)
482 {
483  while (qlist_size) {
484  if ((qlist->vendor == thinkpad_id.vendor ||
485  qlist->vendor == TPACPI_MATCH_ANY) &&
486  (qlist->bios == thinkpad_id.bios_model ||
487  qlist->bios == TPACPI_MATCH_ANY) &&
488  (qlist->ec == thinkpad_id.ec_model ||
489  qlist->ec == TPACPI_MATCH_ANY))
490  return qlist->quirks;
491 
492  qlist_size--;
493  qlist++;
494  }
495  return 0;
496 }
497 
498 static inline bool __pure __init tpacpi_is_lenovo(void)
499 {
500  return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
501 }
502 
503 static inline bool __pure __init tpacpi_is_ibm(void)
504 {
505  return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
506 }
507 
508 /****************************************************************************
509  ****************************************************************************
510  *
511  * ACPI Helpers and device model
512  *
513  ****************************************************************************
514  ****************************************************************************/
515 
516 /*************************************************************************
517  * ACPI basic handles
518  */
519 
520 static acpi_handle root_handle;
521 static acpi_handle ec_handle;
522 
523 #define TPACPI_HANDLE(object, parent, paths...) \
524  static acpi_handle object##_handle; \
525  static const acpi_handle * const object##_parent __initconst = \
526  &parent##_handle; \
527  static char *object##_paths[] __initdata = { paths }
528 
529 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
530 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
531 
532 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
533  /* T4x, X31, X40 */
534  "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
535  "\\CMS", /* R40, R40e */
536  ); /* all others */
537 
538 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
539  "^HKEY", /* R30, R31 */
540  "HKEY", /* all others */
541  ); /* 570 */
542 
543 /*************************************************************************
544  * ACPI helpers
545  */
546 
547 static int acpi_evalf(acpi_handle handle,
548  int *res, char *method, char *fmt, ...)
549 {
550  char *fmt0 = fmt;
551  struct acpi_object_list params;
552  union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
553  struct acpi_buffer result, *resultp;
554  union acpi_object out_obj;
556  va_list ap;
557  char res_type;
558  int success;
559  int quiet;
560 
561  if (!*fmt) {
562  pr_err("acpi_evalf() called with empty format\n");
563  return 0;
564  }
565 
566  if (*fmt == 'q') {
567  quiet = 1;
568  fmt++;
569  } else
570  quiet = 0;
571 
572  res_type = *(fmt++);
573 
574  params.count = 0;
575  params.pointer = &in_objs[0];
576 
577  va_start(ap, fmt);
578  while (*fmt) {
579  char c = *(fmt++);
580  switch (c) {
581  case 'd': /* int */
582  in_objs[params.count].integer.value = va_arg(ap, int);
583  in_objs[params.count++].type = ACPI_TYPE_INTEGER;
584  break;
585  /* add more types as needed */
586  default:
587  pr_err("acpi_evalf() called "
588  "with invalid format character '%c'\n", c);
589  va_end(ap);
590  return 0;
591  }
592  }
593  va_end(ap);
594 
595  if (res_type != 'v') {
596  result.length = sizeof(out_obj);
597  result.pointer = &out_obj;
598  resultp = &result;
599  } else
600  resultp = NULL;
601 
602  status = acpi_evaluate_object(handle, method, &params, resultp);
603 
604  switch (res_type) {
605  case 'd': /* int */
606  success = (status == AE_OK &&
607  out_obj.type == ACPI_TYPE_INTEGER);
608  if (success && res)
609  *res = out_obj.integer.value;
610  break;
611  case 'v': /* void */
612  success = status == AE_OK;
613  break;
614  /* add more types as needed */
615  default:
616  pr_err("acpi_evalf() called "
617  "with invalid format character '%c'\n", res_type);
618  return 0;
619  }
620 
621  if (!success && !quiet)
622  pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
623  method, fmt0, acpi_format_exception(status));
624 
625  return success;
626 }
627 
628 static int acpi_ec_read(int i, u8 *p)
629 {
630  int v;
631 
632  if (ecrd_handle) {
633  if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
634  return 0;
635  *p = v;
636  } else {
637  if (ec_read(i, p) < 0)
638  return 0;
639  }
640 
641  return 1;
642 }
643 
644 static int acpi_ec_write(int i, u8 v)
645 {
646  if (ecwr_handle) {
647  if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
648  return 0;
649  } else {
650  if (ec_write(i, v) < 0)
651  return 0;
652  }
653 
654  return 1;
655 }
656 
657 static int issue_thinkpad_cmos_command(int cmos_cmd)
658 {
659  if (!cmos_handle)
660  return -ENXIO;
661 
662  if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
663  return -EIO;
664 
665  return 0;
666 }
667 
668 /*************************************************************************
669  * ACPI device model
670  */
671 
672 #define TPACPI_ACPIHANDLE_INIT(object) \
673  drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
674  object##_paths, ARRAY_SIZE(object##_paths))
675 
676 static void __init drv_acpi_handle_init(const char *name,
677  acpi_handle *handle, const acpi_handle parent,
678  char **paths, const int num_paths)
679 {
680  int i;
682 
683  vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
684  name);
685 
686  for (i = 0; i < num_paths; i++) {
687  status = acpi_get_handle(parent, paths[i], handle);
688  if (ACPI_SUCCESS(status)) {
690  "Found ACPI handle %s for %s\n",
691  paths[i], name);
692  return;
693  }
694  }
695 
696  vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
697  name);
698  *handle = NULL;
699 }
700 
701 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
702  u32 level, void *context, void **return_value)
703 {
704  *(acpi_handle *)return_value = handle;
705 
706  return AE_CTRL_TERMINATE;
707 }
708 
709 static void __init tpacpi_acpi_handle_locate(const char *name,
710  const char *hid,
711  acpi_handle *handle)
712 {
714  acpi_handle device_found;
715 
716  BUG_ON(!name || !hid || !handle);
718  "trying to locate ACPI handle for %s, using HID %s\n",
719  name, hid);
720 
721  memset(&device_found, 0, sizeof(device_found));
722  status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
723  (void *)name, &device_found);
724 
725  *handle = NULL;
726 
727  if (ACPI_SUCCESS(status)) {
728  *handle = device_found;
730  "Found ACPI handle for %s\n", name);
731  } else {
733  "Could not locate an ACPI handle for %s: %s\n",
734  name, acpi_format_exception(status));
735  }
736 }
737 
738 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
739 {
740  struct ibm_struct *ibm = data;
741 
742  if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
743  return;
744 
745  if (!ibm || !ibm->acpi || !ibm->acpi->notify)
746  return;
747 
748  ibm->acpi->notify(ibm, event);
749 }
750 
751 static int __init setup_acpi_notify(struct ibm_struct *ibm)
752 {
754  int rc;
755 
756  BUG_ON(!ibm->acpi);
757 
758  if (!*ibm->acpi->handle)
759  return 0;
760 
762  "setting up ACPI notify for %s\n", ibm->name);
763 
764  rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
765  if (rc < 0) {
766  pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
767  return -ENODEV;
768  }
769 
770  ibm->acpi->device->driver_data = ibm;
771  sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
773  ibm->name);
774 
775  status = acpi_install_notify_handler(*ibm->acpi->handle,
776  ibm->acpi->type, dispatch_acpi_notify, ibm);
777  if (ACPI_FAILURE(status)) {
778  if (status == AE_ALREADY_EXISTS) {
779  pr_notice("another device driver is already "
780  "handling %s events\n", ibm->name);
781  } else {
782  pr_err("acpi_install_notify_handler(%s) failed: %s\n",
783  ibm->name, acpi_format_exception(status));
784  }
785  return -ENODEV;
786  }
787  ibm->flags.acpi_notify_installed = 1;
788  return 0;
789 }
790 
791 static int __init tpacpi_device_add(struct acpi_device *device)
792 {
793  return 0;
794 }
795 
796 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
797 {
798  int rc;
799 
801  "registering %s as an ACPI driver\n", ibm->name);
802 
803  BUG_ON(!ibm->acpi);
804 
805  ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
806  if (!ibm->acpi->driver) {
807  pr_err("failed to allocate memory for ibm->acpi->driver\n");
808  return -ENOMEM;
809  }
810 
811  sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
812  ibm->acpi->driver->ids = ibm->acpi->hid;
813 
814  ibm->acpi->driver->ops.add = &tpacpi_device_add;
815 
816  rc = acpi_bus_register_driver(ibm->acpi->driver);
817  if (rc < 0) {
818  pr_err("acpi_bus_register_driver(%s) failed: %d\n",
819  ibm->name, rc);
820  kfree(ibm->acpi->driver);
821  ibm->acpi->driver = NULL;
822  } else if (!rc)
823  ibm->flags.acpi_driver_registered = 1;
824 
825  return rc;
826 }
827 
828 
829 /****************************************************************************
830  ****************************************************************************
831  *
832  * Procfs Helpers
833  *
834  ****************************************************************************
835  ****************************************************************************/
836 
837 static int dispatch_proc_show(struct seq_file *m, void *v)
838 {
839  struct ibm_struct *ibm = m->private;
840 
841  if (!ibm || !ibm->read)
842  return -EINVAL;
843  return ibm->read(m);
844 }
845 
846 static int dispatch_proc_open(struct inode *inode, struct file *file)
847 {
848  return single_open(file, dispatch_proc_show, PDE(inode)->data);
849 }
850 
851 static ssize_t dispatch_proc_write(struct file *file,
852  const char __user *userbuf,
853  size_t count, loff_t *pos)
854 {
855  struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
856  char *kernbuf;
857  int ret;
858 
859  if (!ibm || !ibm->write)
860  return -EINVAL;
861  if (count > PAGE_SIZE - 2)
862  return -EINVAL;
863 
864  kernbuf = kmalloc(count + 2, GFP_KERNEL);
865  if (!kernbuf)
866  return -ENOMEM;
867 
868  if (copy_from_user(kernbuf, userbuf, count)) {
869  kfree(kernbuf);
870  return -EFAULT;
871  }
872 
873  kernbuf[count] = 0;
874  strcat(kernbuf, ",");
875  ret = ibm->write(kernbuf);
876  if (ret == 0)
877  ret = count;
878 
879  kfree(kernbuf);
880 
881  return ret;
882 }
883 
884 static const struct file_operations dispatch_proc_fops = {
885  .owner = THIS_MODULE,
886  .open = dispatch_proc_open,
887  .read = seq_read,
888  .llseek = seq_lseek,
889  .release = single_release,
890  .write = dispatch_proc_write,
891 };
892 
893 static char *next_cmd(char **cmds)
894 {
895  char *start = *cmds;
896  char *end;
897 
898  while ((end = strchr(start, ',')) && end == start)
899  start = end + 1;
900 
901  if (!end)
902  return NULL;
903 
904  *end = 0;
905  *cmds = end + 1;
906  return start;
907 }
908 
909 
910 /****************************************************************************
911  ****************************************************************************
912  *
913  * Device model: input, hwmon and platform
914  *
915  ****************************************************************************
916  ****************************************************************************/
917 
918 static struct platform_device *tpacpi_pdev;
919 static struct platform_device *tpacpi_sensors_pdev;
920 static struct device *tpacpi_hwmon;
921 static struct input_dev *tpacpi_inputdev;
922 static struct mutex tpacpi_inputdev_send_mutex;
923 static LIST_HEAD(tpacpi_all_drivers);
924 
925 #ifdef CONFIG_PM_SLEEP
926 static int tpacpi_suspend_handler(struct device *dev)
927 {
928  struct ibm_struct *ibm, *itmp;
929 
930  list_for_each_entry_safe(ibm, itmp,
931  &tpacpi_all_drivers,
932  all_drivers) {
933  if (ibm->suspend)
934  (ibm->suspend)();
935  }
936 
937  return 0;
938 }
939 
940 static int tpacpi_resume_handler(struct device *dev)
941 {
942  struct ibm_struct *ibm, *itmp;
943 
944  list_for_each_entry_safe(ibm, itmp,
945  &tpacpi_all_drivers,
946  all_drivers) {
947  if (ibm->resume)
948  (ibm->resume)();
949  }
950 
951  return 0;
952 }
953 #endif
954 
955 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
956  tpacpi_suspend_handler, tpacpi_resume_handler);
957 
958 static void tpacpi_shutdown_handler(struct platform_device *pdev)
959 {
960  struct ibm_struct *ibm, *itmp;
961 
962  list_for_each_entry_safe(ibm, itmp,
963  &tpacpi_all_drivers,
964  all_drivers) {
965  if (ibm->shutdown)
966  (ibm->shutdown)();
967  }
968 }
969 
970 static struct platform_driver tpacpi_pdriver = {
971  .driver = {
972  .name = TPACPI_DRVR_NAME,
973  .owner = THIS_MODULE,
974  .pm = &tpacpi_pm,
975  },
976  .shutdown = tpacpi_shutdown_handler,
977 };
978 
979 static struct platform_driver tpacpi_hwmon_pdriver = {
980  .driver = {
981  .name = TPACPI_HWMON_DRVR_NAME,
982  .owner = THIS_MODULE,
983  },
984 };
985 
986 /*************************************************************************
987  * sysfs support helpers
988  */
989 
991  unsigned int members, max_members;
993 };
994 
996  struct attribute_set s;
997  struct attribute *a;
998 } __attribute__((packed));
1000 static struct attribute_set *create_attr_set(unsigned int max_members,
1001  const char *name)
1002 {
1003  struct attribute_set_obj *sobj;
1004 
1005  if (max_members == 0)
1006  return NULL;
1007 
1008  /* Allocates space for implicit NULL at the end too */
1009  sobj = kzalloc(sizeof(struct attribute_set_obj) +
1010  max_members * sizeof(struct attribute *),
1011  GFP_KERNEL);
1012  if (!sobj)
1013  return NULL;
1014  sobj->s.max_members = max_members;
1015  sobj->s.group.attrs = &sobj->a;
1016  sobj->s.group.name = name;
1017 
1018  return &sobj->s;
1019 }
1020 
1021 #define destroy_attr_set(_set) \
1022  kfree(_set);
1023 
1024 /* not multi-threaded safe, use it in a single thread per set */
1025 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1026 {
1027  if (!s || !attr)
1028  return -EINVAL;
1029 
1030  if (s->members >= s->max_members)
1031  return -ENOMEM;
1032 
1033  s->group.attrs[s->members] = attr;
1034  s->members++;
1035 
1036  return 0;
1037 }
1038 
1039 static int add_many_to_attr_set(struct attribute_set *s,
1040  struct attribute **attr,
1041  unsigned int count)
1042 {
1043  int i, res;
1044 
1045  for (i = 0; i < count; i++) {
1046  res = add_to_attr_set(s, attr[i]);
1047  if (res)
1048  return res;
1049  }
1050 
1051  return 0;
1052 }
1053 
1054 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1055 {
1056  sysfs_remove_group(kobj, &s->group);
1057  destroy_attr_set(s);
1058 }
1059 
1060 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1061  sysfs_create_group(_kobj, &_attr_set->group)
1062 
1063 static int parse_strtoul(const char *buf,
1064  unsigned long max, unsigned long *value)
1065 {
1066  char *endp;
1067 
1068  *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1069  endp = skip_spaces(endp);
1070  if (*endp || *value > max)
1071  return -EINVAL;
1072 
1073  return 0;
1074 }
1075 
1076 static void tpacpi_disable_brightness_delay(void)
1077 {
1078  if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1079  pr_notice("ACPI backlight control delay disabled\n");
1080 }
1081 
1082 static void printk_deprecated_attribute(const char * const what,
1083  const char * const details)
1084 {
1085  tpacpi_log_usertask("deprecated sysfs attribute");
1086  pr_warn("WARNING: sysfs attribute %s is deprecated and "
1087  "will be removed. %s\n",
1088  what, details);
1089 }
1090 
1091 /*************************************************************************
1092  * rfkill and radio control support helpers
1093  */
1094 
1095 /*
1096  * ThinkPad-ACPI firmware handling model:
1097  *
1098  * WLSW (master wireless switch) is event-driven, and is common to all
1099  * firmware-controlled radios. It cannot be controlled, just monitored,
1100  * as expected. It overrides all radio state in firmware
1101  *
1102  * The kernel, a masked-off hotkey, and WLSW can change the radio state
1103  * (TODO: verify how WLSW interacts with the returned radio state).
1104  *
1105  * The only time there are shadow radio state changes, is when
1106  * masked-off hotkeys are used.
1107  */
1108 
1109 /*
1110  * Internal driver API for radio state:
1111  *
1112  * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1113  * bool: true means radio blocked (off)
1114  */
1118 };
1119 
1120 /* rfkill switches */
1126 };
1127 
1128 static const char *tpacpi_rfkill_names[] = {
1129  [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1130  [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1131  [TPACPI_RFK_UWB_SW_ID] = "uwb",
1133 };
1134 
1135 /* ThinkPad-ACPI rfkill subdriver */
1136 struct tpacpi_rfk {
1137  struct rfkill *rfkill;
1139  const struct tpacpi_rfk_ops *ops;
1140 };
1141 
1143  /* firmware interface */
1146 };
1147 
1148 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1149 
1150 /* Query FW and update rfkill sw state for a given rfkill switch */
1151 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1152 {
1153  int status;
1154 
1155  if (!tp_rfk)
1156  return -ENODEV;
1157 
1158  status = (tp_rfk->ops->get_status)();
1159  if (status < 0)
1160  return status;
1161 
1162  rfkill_set_sw_state(tp_rfk->rfkill,
1163  (status == TPACPI_RFK_RADIO_OFF));
1164 
1165  return status;
1166 }
1167 
1168 /* Query FW and update rfkill sw state for all rfkill switches */
1169 static void tpacpi_rfk_update_swstate_all(void)
1170 {
1171  unsigned int i;
1172 
1173  for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1174  tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1175 }
1176 
1177 /*
1178  * Sync the HW-blocking state of all rfkill switches,
1179  * do notice it causes the rfkill core to schedule uevents
1180  */
1181 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1182 {
1183  unsigned int i;
1184  struct tpacpi_rfk *tp_rfk;
1185 
1186  for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1187  tp_rfk = tpacpi_rfkill_switches[i];
1188  if (tp_rfk) {
1189  if (rfkill_set_hw_state(tp_rfk->rfkill,
1190  blocked)) {
1191  /* ignore -- we track sw block */
1192  }
1193  }
1194  }
1195 }
1196 
1197 /* Call to get the WLSW state from the firmware */
1198 static int hotkey_get_wlsw(void);
1199 
1200 /* Call to query WLSW state and update all rfkill switches */
1201 static bool tpacpi_rfk_check_hwblock_state(void)
1202 {
1203  int res = hotkey_get_wlsw();
1204  int hw_blocked;
1205 
1206  /* When unknown or unsupported, we have to assume it is unblocked */
1207  if (res < 0)
1208  return false;
1209 
1210  hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1211  tpacpi_rfk_update_hwblock_state(hw_blocked);
1212 
1213  return hw_blocked;
1214 }
1215 
1216 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1217 {
1218  struct tpacpi_rfk *tp_rfk = data;
1219  int res;
1220 
1222  "request to change radio state to %s\n",
1223  blocked ? "blocked" : "unblocked");
1224 
1225  /* try to set radio state */
1226  res = (tp_rfk->ops->set_status)(blocked ?
1228 
1229  /* and update the rfkill core with whatever the FW really did */
1230  tpacpi_rfk_update_swstate(tp_rfk);
1231 
1232  return (res < 0) ? res : 0;
1233 }
1234 
1235 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1236  .set_block = tpacpi_rfk_hook_set_block,
1237 };
1238 
1239 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1240  const struct tpacpi_rfk_ops *tp_rfkops,
1241  const enum rfkill_type rfktype,
1242  const char *name,
1243  const bool set_default)
1244 {
1245  struct tpacpi_rfk *atp_rfk;
1246  int res;
1247  bool sw_state = false;
1248  bool hw_state;
1249  int sw_status;
1250 
1251  BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1252 
1253  atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1254  if (atp_rfk)
1255  atp_rfk->rfkill = rfkill_alloc(name,
1256  &tpacpi_pdev->dev,
1257  rfktype,
1258  &tpacpi_rfk_rfkill_ops,
1259  atp_rfk);
1260  if (!atp_rfk || !atp_rfk->rfkill) {
1261  pr_err("failed to allocate memory for rfkill class\n");
1262  kfree(atp_rfk);
1263  return -ENOMEM;
1264  }
1265 
1266  atp_rfk->id = id;
1267  atp_rfk->ops = tp_rfkops;
1268 
1269  sw_status = (tp_rfkops->get_status)();
1270  if (sw_status < 0) {
1271  pr_err("failed to read initial state for %s, error %d\n",
1272  name, sw_status);
1273  } else {
1274  sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1275  if (set_default) {
1276  /* try to keep the initial state, since we ask the
1277  * firmware to preserve it across S5 in NVRAM */
1278  rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1279  }
1280  }
1281  hw_state = tpacpi_rfk_check_hwblock_state();
1282  rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1283 
1284  res = rfkill_register(atp_rfk->rfkill);
1285  if (res < 0) {
1286  pr_err("failed to register %s rfkill switch: %d\n", name, res);
1287  rfkill_destroy(atp_rfk->rfkill);
1288  kfree(atp_rfk);
1289  return res;
1290  }
1291 
1292  tpacpi_rfkill_switches[id] = atp_rfk;
1293 
1294  pr_info("rfkill switch %s: radio is %sblocked\n",
1295  name, (sw_state || hw_state) ? "" : "un");
1296  return 0;
1297 }
1298 
1299 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1300 {
1301  struct tpacpi_rfk *tp_rfk;
1302 
1303  BUG_ON(id >= TPACPI_RFK_SW_MAX);
1304 
1305  tp_rfk = tpacpi_rfkill_switches[id];
1306  if (tp_rfk) {
1307  rfkill_unregister(tp_rfk->rfkill);
1308  rfkill_destroy(tp_rfk->rfkill);
1309  tpacpi_rfkill_switches[id] = NULL;
1310  kfree(tp_rfk);
1311  }
1312 }
1313 
1314 static void printk_deprecated_rfkill_attribute(const char * const what)
1315 {
1316  printk_deprecated_attribute(what,
1317  "Please switch to generic rfkill before year 2010");
1318 }
1319 
1320 /* sysfs <radio> enable ------------------------------------------------ */
1321 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1322  struct device_attribute *attr,
1323  char *buf)
1324 {
1325  int status;
1326 
1327  printk_deprecated_rfkill_attribute(attr->attr.name);
1328 
1329  /* This is in the ABI... */
1330  if (tpacpi_rfk_check_hwblock_state()) {
1331  status = TPACPI_RFK_RADIO_OFF;
1332  } else {
1333  status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1334  if (status < 0)
1335  return status;
1336  }
1337 
1338  return snprintf(buf, PAGE_SIZE, "%d\n",
1339  (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1340 }
1341 
1342 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1343  struct device_attribute *attr,
1344  const char *buf, size_t count)
1345 {
1346  unsigned long t;
1347  int res;
1348 
1349  printk_deprecated_rfkill_attribute(attr->attr.name);
1350 
1351  if (parse_strtoul(buf, 1, &t))
1352  return -EINVAL;
1353 
1354  tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1355 
1356  /* This is in the ABI... */
1357  if (tpacpi_rfk_check_hwblock_state() && !!t)
1358  return -EPERM;
1359 
1360  res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1361  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1362  tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1363 
1364  return (res < 0) ? res : count;
1365 }
1366 
1367 /* procfs -------------------------------------------------------------- */
1368 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1369 {
1370  if (id >= TPACPI_RFK_SW_MAX)
1371  seq_printf(m, "status:\t\tnot supported\n");
1372  else {
1373  int status;
1374 
1375  /* This is in the ABI... */
1376  if (tpacpi_rfk_check_hwblock_state()) {
1377  status = TPACPI_RFK_RADIO_OFF;
1378  } else {
1379  status = tpacpi_rfk_update_swstate(
1380  tpacpi_rfkill_switches[id]);
1381  if (status < 0)
1382  return status;
1383  }
1384 
1385  seq_printf(m, "status:\t\t%s\n",
1386  (status == TPACPI_RFK_RADIO_ON) ?
1387  "enabled" : "disabled");
1388  seq_printf(m, "commands:\tenable, disable\n");
1389  }
1390 
1391  return 0;
1392 }
1393 
1394 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1395 {
1396  char *cmd;
1397  int status = -1;
1398  int res = 0;
1399 
1400  if (id >= TPACPI_RFK_SW_MAX)
1401  return -ENODEV;
1402 
1403  while ((cmd = next_cmd(&buf))) {
1404  if (strlencmp(cmd, "enable") == 0)
1405  status = TPACPI_RFK_RADIO_ON;
1406  else if (strlencmp(cmd, "disable") == 0)
1407  status = TPACPI_RFK_RADIO_OFF;
1408  else
1409  return -EINVAL;
1410  }
1411 
1412  if (status != -1) {
1413  tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1414  (status == TPACPI_RFK_RADIO_ON) ?
1415  "enable" : "disable",
1416  tpacpi_rfkill_names[id]);
1417  res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1418  tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1419  }
1420 
1421  return res;
1422 }
1423 
1424 /*************************************************************************
1425  * thinkpad-acpi driver attributes
1426  */
1427 
1428 /* interface_version --------------------------------------------------- */
1429 static ssize_t tpacpi_driver_interface_version_show(
1430  struct device_driver *drv,
1431  char *buf)
1432 {
1433  return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1434 }
1435 
1436 static DRIVER_ATTR(interface_version, S_IRUGO,
1437  tpacpi_driver_interface_version_show, NULL);
1438 
1439 /* debug_level --------------------------------------------------------- */
1440 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1441  char *buf)
1442 {
1443  return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1444 }
1445 
1446 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1447  const char *buf, size_t count)
1448 {
1449  unsigned long t;
1450 
1451  if (parse_strtoul(buf, 0xffff, &t))
1452  return -EINVAL;
1453 
1454  dbg_level = t;
1455 
1456  return count;
1457 }
1458 
1459 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1460  tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1461 
1462 /* version ------------------------------------------------------------- */
1463 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1464  char *buf)
1465 {
1466  return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1468 }
1469 
1470 static DRIVER_ATTR(version, S_IRUGO,
1471  tpacpi_driver_version_show, NULL);
1472 
1473 /* --------------------------------------------------------------------- */
1474 
1475 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1476 
1477 /* wlsw_emulstate ------------------------------------------------------ */
1478 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1479  char *buf)
1480 {
1481  return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1482 }
1483 
1484 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1485  const char *buf, size_t count)
1486 {
1487  unsigned long t;
1488 
1489  if (parse_strtoul(buf, 1, &t))
1490  return -EINVAL;
1491 
1492  if (tpacpi_wlsw_emulstate != !!t) {
1493  tpacpi_wlsw_emulstate = !!t;
1494  tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1495  }
1496 
1497  return count;
1498 }
1499 
1500 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1501  tpacpi_driver_wlsw_emulstate_show,
1502  tpacpi_driver_wlsw_emulstate_store);
1503 
1504 /* bluetooth_emulstate ------------------------------------------------- */
1505 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1506  struct device_driver *drv,
1507  char *buf)
1508 {
1509  return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1510 }
1511 
1512 static ssize_t tpacpi_driver_bluetooth_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_bluetooth_emulstate = !!t;
1522 
1523  return count;
1524 }
1525 
1526 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1527  tpacpi_driver_bluetooth_emulstate_show,
1528  tpacpi_driver_bluetooth_emulstate_store);
1529 
1530 /* wwan_emulstate ------------------------------------------------- */
1531 static ssize_t tpacpi_driver_wwan_emulstate_show(
1532  struct device_driver *drv,
1533  char *buf)
1534 {
1535  return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1536 }
1537 
1538 static ssize_t tpacpi_driver_wwan_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_wwan_emulstate = !!t;
1548 
1549  return count;
1550 }
1551 
1552 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1553  tpacpi_driver_wwan_emulstate_show,
1554  tpacpi_driver_wwan_emulstate_store);
1555 
1556 /* uwb_emulstate ------------------------------------------------- */
1557 static ssize_t tpacpi_driver_uwb_emulstate_show(
1558  struct device_driver *drv,
1559  char *buf)
1560 {
1561  return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1562 }
1563 
1564 static ssize_t tpacpi_driver_uwb_emulstate_store(
1565  struct device_driver *drv,
1566  const char *buf, size_t count)
1567 {
1568  unsigned long t;
1569 
1570  if (parse_strtoul(buf, 1, &t))
1571  return -EINVAL;
1572 
1573  tpacpi_uwb_emulstate = !!t;
1574 
1575  return count;
1576 }
1577 
1578 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1579  tpacpi_driver_uwb_emulstate_show,
1580  tpacpi_driver_uwb_emulstate_store);
1581 #endif
1582 
1583 /* --------------------------------------------------------------------- */
1584 
1585 static struct driver_attribute *tpacpi_driver_attributes[] = {
1586  &driver_attr_debug_level, &driver_attr_version,
1587  &driver_attr_interface_version,
1588 };
1589 
1590 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1591 {
1592  int i, res;
1593 
1594  i = 0;
1595  res = 0;
1596  while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1597  res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1598  i++;
1599  }
1600 
1601 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1602  if (!res && dbg_wlswemul)
1603  res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1604  if (!res && dbg_bluetoothemul)
1605  res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1606  if (!res && dbg_wwanemul)
1607  res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1608  if (!res && dbg_uwbemul)
1609  res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1610 #endif
1611 
1612  return res;
1613 }
1614 
1615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1616 {
1617  int i;
1618 
1619  for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1620  driver_remove_file(drv, tpacpi_driver_attributes[i]);
1621 
1622 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1623  driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1624  driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1625  driver_remove_file(drv, &driver_attr_wwan_emulstate);
1626  driver_remove_file(drv, &driver_attr_uwb_emulstate);
1627 #endif
1628 }
1629 
1630 /*************************************************************************
1631  * Firmware Data
1632  */
1633 
1634 /*
1635  * Table of recommended minimum BIOS versions
1636  *
1637  * Reasons for listing:
1638  * 1. Stable BIOS, listed because the unknown amount of
1639  * bugs and bad ACPI behaviour on older versions
1640  *
1641  * 2. BIOS or EC fw with known bugs that trigger on Linux
1642  *
1643  * 3. BIOS with known reduced functionality in older versions
1644  *
1645  * We recommend the latest BIOS and EC version.
1646  * We only support the latest BIOS and EC fw version as a rule.
1647  *
1648  * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1649  * Information from users in ThinkWiki
1650  *
1651  * WARNING: we use this table also to detect that the machine is
1652  * a ThinkPad in some cases, so don't remove entries lightly.
1653  */
1654 
1655 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1656  { .vendor = (__v), \
1657  .bios = TPID(__id1, __id2), \
1658  .ec = TPACPI_MATCH_ANY, \
1659  .quirks = TPACPI_MATCH_ANY << 16 \
1660  | (__bv1) << 8 | (__bv2) }
1661 
1662 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1663  __eid, __ev1, __ev2) \
1664  { .vendor = (__v), \
1665  .bios = TPID(__bid1, __bid2), \
1666  .ec = __eid, \
1667  .quirks = (__ev1) << 24 | (__ev2) << 16 \
1668  | (__bv1) << 8 | (__bv2) }
1669 
1670 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1671  TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1672 
1673 /* Outdated IBM BIOSes often lack the EC id string */
1674 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675  TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1676  __bv1, __bv2, TPID(__id1, __id2), \
1677  __ev1, __ev2), \
1678  TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1679  __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1680  __ev1, __ev2)
1681 
1682 /* Outdated IBM BIOSes often lack the EC id string */
1683 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1684  __eid1, __eid2, __ev1, __ev2) \
1685  TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1686  __bv1, __bv2, TPID(__eid1, __eid2), \
1687  __ev1, __ev2), \
1688  TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1689  __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1690  __ev1, __ev2)
1691 
1692 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1693  TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1694 
1695 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1696  TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1697  __bv1, __bv2, TPID(__id1, __id2), \
1698  __ev1, __ev2)
1699 
1700 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1701  __eid1, __eid2, __ev1, __ev2) \
1702  TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1703  __bv1, __bv2, TPID(__eid1, __eid2), \
1704  __ev1, __ev2)
1705 
1706 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1707  /* Numeric models ------------------ */
1708  /* FW MODEL BIOS VERS */
1709  TPV_QI0('I', 'M', '6', '5'), /* 570 */
1710  TPV_QI0('I', 'U', '2', '6'), /* 570E */
1711  TPV_QI0('I', 'B', '5', '4'), /* 600 */
1712  TPV_QI0('I', 'H', '4', '7'), /* 600E */
1713  TPV_QI0('I', 'N', '3', '6'), /* 600E */
1714  TPV_QI0('I', 'T', '5', '5'), /* 600X */
1715  TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1716  TPV_QI0('I', 'I', '4', '2'), /* 770X */
1717  TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1718 
1719  /* A-series ------------------------- */
1720  /* FW MODEL BIOS VERS EC VERS */
1721  TPV_QI0('I', 'W', '5', '9'), /* A20m */
1722  TPV_QI0('I', 'V', '6', '9'), /* A20p */
1723  TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1724  TPV_QI0('K', 'U', '3', '6'), /* A21e */
1725  TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1726  TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1727  TPV_QI0('1', 'B', '1', '7'), /* A22e */
1728  TPV_QI0('1', '3', '2', '0'), /* A22m */
1729  TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1730  TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1731  TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1732 
1733  /* G-series ------------------------- */
1734  /* FW MODEL BIOS VERS */
1735  TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1736  TPV_QI0('1', 'X', '5', '7'), /* G41 */
1737 
1738  /* R-series, T-series --------------- */
1739  /* FW MODEL BIOS VERS EC VERS */
1740  TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1741  TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1742  TPV_QI0('1', 'M', '9', '7'), /* R32 */
1743  TPV_QI0('1', 'O', '6', '1'), /* R40 */
1744  TPV_QI0('1', 'P', '6', '5'), /* R40 */
1745  TPV_QI0('1', 'S', '7', '0'), /* R40e */
1746  TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1747  T40/p, T41/p, T42/p (1) */
1748  TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1749  TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1750  TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1751  TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1752 
1753  TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1754  TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1755  TPV_QI0('1', '6', '3', '2'), /* T22 */
1756  TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1757  TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1758  TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1759 
1760  TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1761  TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1762  TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1763 
1764  /* BIOS FW BIOS VERS EC FW EC VERS */
1765  TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1766  TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1767 
1768  /* X-series ------------------------- */
1769  /* FW MODEL BIOS VERS EC VERS */
1770  TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1771  TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1772  TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1773  TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1774  TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1775  TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1776  TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1777 
1778  TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1779  TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1780 
1781  /* (0) - older versions lack DMI EC fw string and functionality */
1782  /* (1) - older versions known to lack functionality */
1783 };
1784 
1785 #undef TPV_QL1
1786 #undef TPV_QL0
1787 #undef TPV_QI2
1788 #undef TPV_QI1
1789 #undef TPV_QI0
1790 #undef TPV_Q_X
1791 #undef TPV_Q
1792 
1793 static void __init tpacpi_check_outdated_fw(void)
1794 {
1795  unsigned long fwvers;
1796  u16 ec_version, bios_version;
1797 
1798  fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1799  ARRAY_SIZE(tpacpi_bios_version_qtable));
1800 
1801  if (!fwvers)
1802  return;
1803 
1804  bios_version = fwvers & 0xffffU;
1805  ec_version = (fwvers >> 16) & 0xffffU;
1806 
1807  /* note that unknown versions are set to 0x0000 and we use that */
1808  if ((bios_version > thinkpad_id.bios_release) ||
1809  (ec_version > thinkpad_id.ec_release &&
1810  ec_version != TPACPI_MATCH_ANY)) {
1811  /*
1812  * The changelogs would let us track down the exact
1813  * reason, but it is just too much of a pain to track
1814  * it. We only list BIOSes that are either really
1815  * broken, or really stable to begin with, so it is
1816  * best if the user upgrades the firmware anyway.
1817  */
1818  pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1819  pr_warn("WARNING: This firmware may be missing critical bug "
1820  "fixes and/or important features\n");
1821  }
1822 }
1823 
1824 static bool __init tpacpi_is_fw_known(void)
1825 {
1826  return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1827  ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1828 }
1829 
1830 /****************************************************************************
1831  ****************************************************************************
1832  *
1833  * Subdrivers
1834  *
1835  ****************************************************************************
1836  ****************************************************************************/
1837 
1838 /*************************************************************************
1839  * thinkpad-acpi metadata subdriver
1840  */
1841 
1842 static int thinkpad_acpi_driver_read(struct seq_file *m)
1843 {
1844  seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1845  seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1846  return 0;
1847 }
1848 
1849 static struct ibm_struct thinkpad_acpi_driver_data = {
1850  .name = "driver",
1851  .read = thinkpad_acpi_driver_read,
1852 };
1853 
1854 /*************************************************************************
1855  * Hotkey subdriver
1856  */
1857 
1858 /*
1859  * ThinkPad firmware event model
1860  *
1861  * The ThinkPad firmware has two main event interfaces: normal ACPI
1862  * notifications (which follow the ACPI standard), and a private event
1863  * interface.
1864  *
1865  * The private event interface also issues events for the hotkeys. As
1866  * the driver gained features, the event handling code ended up being
1867  * built around the hotkey subdriver. This will need to be refactored
1868  * to a more formal event API eventually.
1869  *
1870  * Some "hotkeys" are actually supposed to be used as event reports,
1871  * such as "brightness has changed", "volume has changed", depending on
1872  * the ThinkPad model and how the firmware is operating.
1873  *
1874  * Unlike other classes, hotkey-class events have mask/unmask control on
1875  * non-ancient firmware. However, how it behaves changes a lot with the
1876  * firmware model and version.
1877  */
1878 
1879 enum { /* hot key scan codes (derived from ACPI DSDT) */
1912 
1913  /* Hotkey keymap size */
1915 };
1916 
1917 enum { /* Keys/events available through NVRAM polling */
1920 };
1921 
1922 enum { /* Positions of some of the keys in hotkey masks */
1934 };
1935 
1936 enum { /* NVRAM to ACPI HKEY group map */
1946 };
1947 
1948 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1949 struct tp_nvram_state {
1950  u16 thinkpad_toggle:1;
1951  u16 zoom_toggle:1;
1952  u16 display_toggle:1;
1953  u16 thinklight_toggle:1;
1954  u16 hibernate_toggle:1;
1955  u16 displayexp_toggle:1;
1956  u16 display_state:1;
1957  u16 brightness_toggle:1;
1958  u16 volume_toggle:1;
1959  u16 mute:1;
1960 
1961  u8 brightness_level;
1962  u8 volume_level;
1963 };
1964 
1965 /* kthread for the hotkey poller */
1966 static struct task_struct *tpacpi_hotkey_task;
1967 
1968 /* Acquired while the poller kthread is running, use to sync start/stop */
1969 static struct mutex hotkey_thread_mutex;
1970 
1971 /*
1972  * Acquire mutex to write poller control variables as an
1973  * atomic block.
1974  *
1975  * Increment hotkey_config_change when changing them if you
1976  * want the kthread to forget old state.
1977  *
1978  * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1979  */
1980 static struct mutex hotkey_thread_data_mutex;
1981 static unsigned int hotkey_config_change;
1982 
1983 /*
1984  * hotkey poller control variables
1985  *
1986  * Must be atomic or readers will also need to acquire mutex
1987  *
1988  * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1989  * should be used only when the changes need to be taken as
1990  * a block, OR when one needs to force the kthread to forget
1991  * old state.
1992  */
1993 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1994 static unsigned int hotkey_poll_freq = 10; /* Hz */
1995 
1996 #define HOTKEY_CONFIG_CRITICAL_START \
1997  do { \
1998  mutex_lock(&hotkey_thread_data_mutex); \
1999  hotkey_config_change++; \
2000  } while (0);
2001 #define HOTKEY_CONFIG_CRITICAL_END \
2002  mutex_unlock(&hotkey_thread_data_mutex);
2003 
2004 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2005 
2006 #define hotkey_source_mask 0U
2007 #define HOTKEY_CONFIG_CRITICAL_START
2008 #define HOTKEY_CONFIG_CRITICAL_END
2009 
2010 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2011 
2012 static struct mutex hotkey_mutex;
2013 
2014 static enum { /* Reasons for waking up */
2015  TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2016  TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2017  TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2018 } hotkey_wakeup_reason;
2019 
2020 static int hotkey_autosleep_ack;
2021 
2022 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2023 static u32 hotkey_all_mask; /* all events supported in fw */
2024 static u32 hotkey_reserved_mask; /* events better left disabled */
2025 static u32 hotkey_driver_mask; /* events needed by the driver */
2026 static u32 hotkey_user_mask; /* events visible to userspace */
2027 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2028 
2029 static unsigned int hotkey_report_mode;
2030 
2031 static u16 *hotkey_keycode_map;
2032 
2033 static struct attribute_set *hotkey_dev_attributes;
2034 
2035 static void tpacpi_driver_event(const unsigned int hkey_event);
2036 static void hotkey_driver_event(const unsigned int scancode);
2037 static void hotkey_poll_setup(const bool may_warn);
2038 
2039 /* HKEY.MHKG() return bits */
2040 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2041 
2042 static int hotkey_get_wlsw(void)
2043 {
2044  int status;
2045 
2046  if (!tp_features.hotkey_wlsw)
2047  return -ENODEV;
2048 
2049 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2050  if (dbg_wlswemul)
2051  return (tpacpi_wlsw_emulstate) ?
2052  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2053 #endif
2054 
2055  if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2056  return -EIO;
2057 
2058  return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2059 }
2060 
2061 static int hotkey_get_tablet_mode(int *status)
2062 {
2063  int s;
2064 
2065  if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2066  return -EIO;
2067 
2068  *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2069  return 0;
2070 }
2071 
2072 /*
2073  * Reads current event mask from firmware, and updates
2074  * hotkey_acpi_mask accordingly. Also resets any bits
2075  * from hotkey_user_mask that are unavailable to be
2076  * delivered (shadow requirement of the userspace ABI).
2077  *
2078  * Call with hotkey_mutex held
2079  */
2080 static int hotkey_mask_get(void)
2081 {
2082  if (tp_features.hotkey_mask) {
2083  u32 m = 0;
2084 
2085  if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2086  return -EIO;
2087 
2088  hotkey_acpi_mask = m;
2089  } else {
2090  /* no mask support doesn't mean no event support... */
2091  hotkey_acpi_mask = hotkey_all_mask;
2092  }
2093 
2094  /* sync userspace-visible mask */
2095  hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2096 
2097  return 0;
2098 }
2099 
2100 void static hotkey_mask_warn_incomplete_mask(void)
2101 {
2102  /* log only what the user can fix... */
2103  const u32 wantedmask = hotkey_driver_mask &
2104  ~(hotkey_acpi_mask | hotkey_source_mask) &
2105  (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2106 
2107  if (wantedmask)
2108  pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2109 }
2110 
2111 /*
2112  * Set the firmware mask when supported
2113  *
2114  * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2115  *
2116  * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2117  *
2118  * Call with hotkey_mutex held
2119  */
2120 static int hotkey_mask_set(u32 mask)
2121 {
2122  int i;
2123  int rc = 0;
2124 
2125  const u32 fwmask = mask & ~hotkey_source_mask;
2126 
2127  if (tp_features.hotkey_mask) {
2128  for (i = 0; i < 32; i++) {
2129  if (!acpi_evalf(hkey_handle,
2130  NULL, "MHKM", "vdd", i + 1,
2131  !!(mask & (1 << i)))) {
2132  rc = -EIO;
2133  break;
2134  }
2135  }
2136  }
2137 
2138  /*
2139  * We *must* make an inconditional call to hotkey_mask_get to
2140  * refresh hotkey_acpi_mask and update hotkey_user_mask
2141  *
2142  * Take the opportunity to also log when we cannot _enable_
2143  * a given event.
2144  */
2145  if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2146  pr_notice("asked for hotkey mask 0x%08x, but "
2147  "firmware forced it to 0x%08x\n",
2148  fwmask, hotkey_acpi_mask);
2149  }
2150 
2151  if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2152  hotkey_mask_warn_incomplete_mask();
2153 
2154  return rc;
2155 }
2156 
2157 /*
2158  * Sets hotkey_user_mask and tries to set the firmware mask
2159  *
2160  * Call with hotkey_mutex held
2161  */
2162 static int hotkey_user_mask_set(const u32 mask)
2163 {
2164  int rc;
2165 
2166  /* Give people a chance to notice they are doing something that
2167  * is bound to go boom on their users sooner or later */
2168  if (!tp_warned.hotkey_mask_ff &&
2169  (mask == 0xffff || mask == 0xffffff ||
2170  mask == 0xffffffff)) {
2171  tp_warned.hotkey_mask_ff = 1;
2172  pr_notice("setting the hotkey mask to 0x%08x is likely "
2173  "not the best way to go about it\n", mask);
2174  pr_notice("please consider using the driver defaults, "
2175  "and refer to up-to-date thinkpad-acpi "
2176  "documentation\n");
2177  }
2178 
2179  /* Try to enable what the user asked for, plus whatever we need.
2180  * this syncs everything but won't enable bits in hotkey_user_mask */
2181  rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2182 
2183  /* Enable the available bits in hotkey_user_mask */
2184  hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2185 
2186  return rc;
2187 }
2188 
2189 /*
2190  * Sets the driver hotkey mask.
2191  *
2192  * Can be called even if the hotkey subdriver is inactive
2193  */
2194 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2195 {
2196  int rc;
2197 
2198  /* Do the right thing if hotkey_init has not been called yet */
2199  if (!tp_features.hotkey) {
2200  hotkey_driver_mask = mask;
2201  return 0;
2202  }
2203 
2204  mutex_lock(&hotkey_mutex);
2205 
2207  hotkey_driver_mask = mask;
2208 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2209  hotkey_source_mask |= (mask & ~hotkey_all_mask);
2210 #endif
2212 
2213  rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2214  ~hotkey_source_mask);
2215  hotkey_poll_setup(true);
2216 
2217  mutex_unlock(&hotkey_mutex);
2218 
2219  return rc;
2220 }
2221 
2222 static int hotkey_status_get(int *status)
2223 {
2224  if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2225  return -EIO;
2226 
2227  return 0;
2228 }
2229 
2230 static int hotkey_status_set(bool enable)
2231 {
2232  if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2233  return -EIO;
2234 
2235  return 0;
2236 }
2237 
2238 static void tpacpi_input_send_tabletsw(void)
2239 {
2240  int state;
2241 
2242  if (tp_features.hotkey_tablet &&
2243  !hotkey_get_tablet_mode(&state)) {
2244  mutex_lock(&tpacpi_inputdev_send_mutex);
2245 
2246  input_report_switch(tpacpi_inputdev,
2247  SW_TABLET_MODE, !!state);
2248  input_sync(tpacpi_inputdev);
2249 
2250  mutex_unlock(&tpacpi_inputdev_send_mutex);
2251  }
2252 }
2253 
2254 /* Do NOT call without validating scancode first */
2255 static void tpacpi_input_send_key(const unsigned int scancode)
2256 {
2257  const unsigned int keycode = hotkey_keycode_map[scancode];
2258 
2259  if (keycode != KEY_RESERVED) {
2260  mutex_lock(&tpacpi_inputdev_send_mutex);
2261 
2262  input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2263  input_report_key(tpacpi_inputdev, keycode, 1);
2264  input_sync(tpacpi_inputdev);
2265 
2266  input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2267  input_report_key(tpacpi_inputdev, keycode, 0);
2268  input_sync(tpacpi_inputdev);
2269 
2270  mutex_unlock(&tpacpi_inputdev_send_mutex);
2271  }
2272 }
2273 
2274 /* Do NOT call without validating scancode first */
2275 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2276 {
2277  hotkey_driver_event(scancode);
2278  if (hotkey_user_mask & (1 << scancode))
2279  tpacpi_input_send_key(scancode);
2280 }
2281 
2282 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2283 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2284 
2285 /* Do NOT call without validating scancode first */
2286 static void tpacpi_hotkey_send_key(unsigned int scancode)
2287 {
2288  tpacpi_input_send_key_masked(scancode);
2289  if (hotkey_report_mode < 2) {
2290  acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2291  0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2292  }
2293 }
2294 
2295 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2296 {
2297  u8 d;
2298 
2299  if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2301  n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2302  n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2303  n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2304  n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2305  }
2306  if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2308  n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2309  }
2310  if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2312  n->displayexp_toggle =
2314  }
2317  n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2319  n->brightness_toggle =
2321  }
2322  if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2324  n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2326  n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2327  n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2328  }
2329 }
2330 
2331 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2332  struct tp_nvram_state *newn,
2333  const u32 event_mask)
2334 {
2335 
2336 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2337  do { \
2338  if ((event_mask & (1 << __scancode)) && \
2339  oldn->__member != newn->__member) \
2340  tpacpi_hotkey_send_key(__scancode); \
2341  } while (0)
2342 
2343 #define TPACPI_MAY_SEND_KEY(__scancode) \
2344  do { \
2345  if (event_mask & (1 << __scancode)) \
2346  tpacpi_hotkey_send_key(__scancode); \
2347  } while (0)
2348 
2349  void issue_volchange(const unsigned int oldvol,
2350  const unsigned int newvol)
2351  {
2352  unsigned int i = oldvol;
2353 
2354  while (i > newvol) {
2355  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2356  i--;
2357  }
2358  while (i < newvol) {
2359  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2360  i++;
2361  }
2362  }
2363 
2364  void issue_brightnesschange(const unsigned int oldbrt,
2365  const unsigned int newbrt)
2366  {
2367  unsigned int i = oldbrt;
2368 
2369  while (i > newbrt) {
2370  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2371  i--;
2372  }
2373  while (i < newbrt) {
2374  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2375  i++;
2376  }
2377  }
2378 
2379  TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2380  TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2381  TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2382  TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2383 
2384  TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2385 
2386  TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2387 
2388  /*
2389  * Handle volume
2390  *
2391  * This code is supposed to duplicate the IBM firmware behaviour:
2392  * - Pressing MUTE issues mute hotkey message, even when already mute
2393  * - Pressing Volume up/down issues volume up/down hotkey messages,
2394  * even when already at maximum or minimum volume
2395  * - The act of unmuting issues volume up/down notification,
2396  * depending which key was used to unmute
2397  *
2398  * We are constrained to what the NVRAM can tell us, which is not much
2399  * and certainly not enough if more than one volume hotkey was pressed
2400  * since the last poll cycle.
2401  *
2402  * Just to make our life interesting, some newer Lenovo ThinkPads have
2403  * bugs in the BIOS and may fail to update volume_toggle properly.
2404  */
2405  if (newn->mute) {
2406  /* muted */
2407  if (!oldn->mute ||
2408  oldn->volume_toggle != newn->volume_toggle ||
2409  oldn->volume_level != newn->volume_level) {
2410  /* recently muted, or repeated mute keypress, or
2411  * multiple presses ending in mute */
2412  issue_volchange(oldn->volume_level, newn->volume_level);
2413  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2414  }
2415  } else {
2416  /* unmute */
2417  if (oldn->mute) {
2418  /* recently unmuted, issue 'unmute' keypress */
2419  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2420  }
2421  if (oldn->volume_level != newn->volume_level) {
2422  issue_volchange(oldn->volume_level, newn->volume_level);
2423  } else if (oldn->volume_toggle != newn->volume_toggle) {
2424  /* repeated vol up/down keypress at end of scale ? */
2425  if (newn->volume_level == 0)
2426  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2427  else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2428  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2429  }
2430  }
2431 
2432  /* handle brightness */
2433  if (oldn->brightness_level != newn->brightness_level) {
2434  issue_brightnesschange(oldn->brightness_level,
2435  newn->brightness_level);
2436  } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2437  /* repeated key presses that didn't change state */
2438  if (newn->brightness_level == 0)
2439  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2440  else if (newn->brightness_level >= bright_maxlvl
2441  && !tp_features.bright_unkfw)
2442  TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2443  }
2444 
2445 #undef TPACPI_COMPARE_KEY
2446 #undef TPACPI_MAY_SEND_KEY
2447 }
2448 
2449 /*
2450  * Polling driver
2451  *
2452  * We track all events in hotkey_source_mask all the time, since
2453  * most of them are edge-based. We only issue those requested by
2454  * hotkey_user_mask or hotkey_driver_mask, though.
2455  */
2456 static int hotkey_kthread(void *data)
2457 {
2458  struct tp_nvram_state s[2];
2459  u32 poll_mask, event_mask;
2460  unsigned int si, so;
2461  unsigned long t;
2462  unsigned int change_detector;
2463  unsigned int poll_freq;
2464  bool was_frozen;
2465 
2466  mutex_lock(&hotkey_thread_mutex);
2467 
2468  if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2469  goto exit;
2470 
2471  set_freezable();
2472 
2473  so = 0;
2474  si = 1;
2475  t = 0;
2476 
2477  /* Initial state for compares */
2478  mutex_lock(&hotkey_thread_data_mutex);
2479  change_detector = hotkey_config_change;
2480  poll_mask = hotkey_source_mask;
2481  event_mask = hotkey_source_mask &
2482  (hotkey_driver_mask | hotkey_user_mask);
2483  poll_freq = hotkey_poll_freq;
2484  mutex_unlock(&hotkey_thread_data_mutex);
2485  hotkey_read_nvram(&s[so], poll_mask);
2486 
2487  while (!kthread_should_stop()) {
2488  if (t == 0) {
2489  if (likely(poll_freq))
2490  t = 1000/poll_freq;
2491  else
2492  t = 100; /* should never happen... */
2493  }
2494  t = msleep_interruptible(t);
2495  if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2496  break;
2497 
2498  if (t > 0 && !was_frozen)
2499  continue;
2500 
2501  mutex_lock(&hotkey_thread_data_mutex);
2502  if (was_frozen || hotkey_config_change != change_detector) {
2503  /* forget old state on thaw or config change */
2504  si = so;
2505  t = 0;
2506  change_detector = hotkey_config_change;
2507  }
2508  poll_mask = hotkey_source_mask;
2509  event_mask = hotkey_source_mask &
2510  (hotkey_driver_mask | hotkey_user_mask);
2511  poll_freq = hotkey_poll_freq;
2512  mutex_unlock(&hotkey_thread_data_mutex);
2513 
2514  if (likely(poll_mask)) {
2515  hotkey_read_nvram(&s[si], poll_mask);
2516  if (likely(si != so)) {
2517  hotkey_compare_and_issue_event(&s[so], &s[si],
2518  event_mask);
2519  }
2520  }
2521 
2522  so = si;
2523  si ^= 1;
2524  }
2525 
2526 exit:
2527  mutex_unlock(&hotkey_thread_mutex);
2528  return 0;
2529 }
2530 
2531 /* call with hotkey_mutex held */
2532 static void hotkey_poll_stop_sync(void)
2533 {
2534  if (tpacpi_hotkey_task) {
2535  kthread_stop(tpacpi_hotkey_task);
2536  tpacpi_hotkey_task = NULL;
2537  mutex_lock(&hotkey_thread_mutex);
2538  /* at this point, the thread did exit */
2539  mutex_unlock(&hotkey_thread_mutex);
2540  }
2541 }
2542 
2543 /* call with hotkey_mutex held */
2544 static void hotkey_poll_setup(const bool may_warn)
2545 {
2546  const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2547  const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2548 
2549  if (hotkey_poll_freq > 0 &&
2550  (poll_driver_mask ||
2551  (poll_user_mask && tpacpi_inputdev->users > 0))) {
2552  if (!tpacpi_hotkey_task) {
2553  tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2555  if (IS_ERR(tpacpi_hotkey_task)) {
2556  tpacpi_hotkey_task = NULL;
2557  pr_err("could not create kernel thread "
2558  "for hotkey polling\n");
2559  }
2560  }
2561  } else {
2562  hotkey_poll_stop_sync();
2563  if (may_warn && (poll_driver_mask || poll_user_mask) &&
2564  hotkey_poll_freq == 0) {
2565  pr_notice("hot keys 0x%08x and/or events 0x%08x "
2566  "require polling, which is currently "
2567  "disabled\n",
2568  poll_user_mask, poll_driver_mask);
2569  }
2570  }
2571 }
2572 
2573 static void hotkey_poll_setup_safe(const bool may_warn)
2574 {
2575  mutex_lock(&hotkey_mutex);
2576  hotkey_poll_setup(may_warn);
2577  mutex_unlock(&hotkey_mutex);
2578 }
2579 
2580 /* call with hotkey_mutex held */
2581 static void hotkey_poll_set_freq(unsigned int freq)
2582 {
2583  if (!freq)
2584  hotkey_poll_stop_sync();
2585 
2586  hotkey_poll_freq = freq;
2587 }
2588 
2589 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2590 
2591 static void hotkey_poll_setup(const bool __unused)
2592 {
2593 }
2594 
2595 static void hotkey_poll_setup_safe(const bool __unused)
2596 {
2597 }
2598 
2599 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2600 
2601 static int hotkey_inputdev_open(struct input_dev *dev)
2602 {
2603  switch (tpacpi_lifecycle) {
2604  case TPACPI_LIFE_INIT:
2605  case TPACPI_LIFE_RUNNING:
2606  hotkey_poll_setup_safe(false);
2607  return 0;
2608  case TPACPI_LIFE_EXITING:
2609  return -EBUSY;
2610  }
2611 
2612  /* Should only happen if tpacpi_lifecycle is corrupt */
2613  BUG();
2614  return -EBUSY;
2615 }
2616 
2617 static void hotkey_inputdev_close(struct input_dev *dev)
2618 {
2619  /* disable hotkey polling when possible */
2620  if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2621  !(hotkey_source_mask & hotkey_driver_mask))
2622  hotkey_poll_setup_safe(false);
2623 }
2624 
2625 /* sysfs hotkey enable ------------------------------------------------- */
2626 static ssize_t hotkey_enable_show(struct device *dev,
2627  struct device_attribute *attr,
2628  char *buf)
2629 {
2630  int res, status;
2631 
2632  printk_deprecated_attribute("hotkey_enable",
2633  "Hotkey reporting is always enabled");
2634 
2635  res = hotkey_status_get(&status);
2636  if (res)
2637  return res;
2638 
2639  return snprintf(buf, PAGE_SIZE, "%d\n", status);
2640 }
2641 
2642 static ssize_t hotkey_enable_store(struct device *dev,
2643  struct device_attribute *attr,
2644  const char *buf, size_t count)
2645 {
2646  unsigned long t;
2647 
2648  printk_deprecated_attribute("hotkey_enable",
2649  "Hotkeys can be disabled through hotkey_mask");
2650 
2651  if (parse_strtoul(buf, 1, &t))
2652  return -EINVAL;
2653 
2654  if (t == 0)
2655  return -EPERM;
2656 
2657  return count;
2658 }
2659 
2660 static struct device_attribute dev_attr_hotkey_enable =
2661  __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2662  hotkey_enable_show, hotkey_enable_store);
2663 
2664 /* sysfs hotkey mask --------------------------------------------------- */
2665 static ssize_t hotkey_mask_show(struct device *dev,
2666  struct device_attribute *attr,
2667  char *buf)
2668 {
2669  return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2670 }
2671 
2672 static ssize_t hotkey_mask_store(struct device *dev,
2673  struct device_attribute *attr,
2674  const char *buf, size_t count)
2675 {
2676  unsigned long t;
2677  int res;
2678 
2679  if (parse_strtoul(buf, 0xffffffffUL, &t))
2680  return -EINVAL;
2681 
2682  if (mutex_lock_killable(&hotkey_mutex))
2683  return -ERESTARTSYS;
2684 
2685  res = hotkey_user_mask_set(t);
2686 
2687 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2688  hotkey_poll_setup(true);
2689 #endif
2690 
2691  mutex_unlock(&hotkey_mutex);
2692 
2693  tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2694 
2695  return (res) ? res : count;
2696 }
2697 
2698 static struct device_attribute dev_attr_hotkey_mask =
2700  hotkey_mask_show, hotkey_mask_store);
2701 
2702 /* sysfs hotkey bios_enabled ------------------------------------------- */
2703 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2704  struct device_attribute *attr,
2705  char *buf)
2706 {
2707  return sprintf(buf, "0\n");
2708 }
2709 
2710 static struct device_attribute dev_attr_hotkey_bios_enabled =
2711  __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2712 
2713 /* sysfs hotkey bios_mask ---------------------------------------------- */
2714 static ssize_t hotkey_bios_mask_show(struct device *dev,
2715  struct device_attribute *attr,
2716  char *buf)
2717 {
2718  printk_deprecated_attribute("hotkey_bios_mask",
2719  "This attribute is useless.");
2720  return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2721 }
2722 
2723 static struct device_attribute dev_attr_hotkey_bios_mask =
2724  __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2725 
2726 /* sysfs hotkey all_mask ----------------------------------------------- */
2727 static ssize_t hotkey_all_mask_show(struct device *dev,
2728  struct device_attribute *attr,
2729  char *buf)
2730 {
2731  return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2732  hotkey_all_mask | hotkey_source_mask);
2733 }
2734 
2735 static struct device_attribute dev_attr_hotkey_all_mask =
2736  __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2737 
2738 /* sysfs hotkey recommended_mask --------------------------------------- */
2739 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2740  struct device_attribute *attr,
2741  char *buf)
2742 {
2743  return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2744  (hotkey_all_mask | hotkey_source_mask)
2745  & ~hotkey_reserved_mask);
2746 }
2747 
2748 static struct device_attribute dev_attr_hotkey_recommended_mask =
2749  __ATTR(hotkey_recommended_mask, S_IRUGO,
2750  hotkey_recommended_mask_show, NULL);
2751 
2752 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2753 
2754 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2755 static ssize_t hotkey_source_mask_show(struct device *dev,
2756  struct device_attribute *attr,
2757  char *buf)
2758 {
2759  return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2760 }
2761 
2762 static ssize_t hotkey_source_mask_store(struct device *dev,
2763  struct device_attribute *attr,
2764  const char *buf, size_t count)
2765 {
2766  unsigned long t;
2767  u32 r_ev;
2768  int rc;
2769 
2770  if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2771  ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2772  return -EINVAL;
2773 
2774  if (mutex_lock_killable(&hotkey_mutex))
2775  return -ERESTARTSYS;
2776 
2778  hotkey_source_mask = t;
2780 
2781  rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2782  ~hotkey_source_mask);
2783  hotkey_poll_setup(true);
2784 
2785  /* check if events needed by the driver got disabled */
2786  r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2787  & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2788 
2789  mutex_unlock(&hotkey_mutex);
2790 
2791  if (rc < 0)
2792  pr_err("hotkey_source_mask: "
2793  "failed to update the firmware event mask!\n");
2794 
2795  if (r_ev)
2796  pr_notice("hotkey_source_mask: "
2797  "some important events were disabled: 0x%04x\n",
2798  r_ev);
2799 
2800  tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2801 
2802  return (rc < 0) ? rc : count;
2803 }
2804 
2805 static struct device_attribute dev_attr_hotkey_source_mask =
2806  __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2807  hotkey_source_mask_show, hotkey_source_mask_store);
2808 
2809 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2810 static ssize_t hotkey_poll_freq_show(struct device *dev,
2811  struct device_attribute *attr,
2812  char *buf)
2813 {
2814  return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2815 }
2816 
2817 static ssize_t hotkey_poll_freq_store(struct device *dev,
2818  struct device_attribute *attr,
2819  const char *buf, size_t count)
2820 {
2821  unsigned long t;
2822 
2823  if (parse_strtoul(buf, 25, &t))
2824  return -EINVAL;
2825 
2826  if (mutex_lock_killable(&hotkey_mutex))
2827  return -ERESTARTSYS;
2828 
2829  hotkey_poll_set_freq(t);
2830  hotkey_poll_setup(true);
2831 
2832  mutex_unlock(&hotkey_mutex);
2833 
2834  tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2835 
2836  return count;
2837 }
2838 
2839 static struct device_attribute dev_attr_hotkey_poll_freq =
2840  __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2841  hotkey_poll_freq_show, hotkey_poll_freq_store);
2842 
2843 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2844 
2845 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2846 static ssize_t hotkey_radio_sw_show(struct device *dev,
2847  struct device_attribute *attr,
2848  char *buf)
2849 {
2850  int res;
2851  res = hotkey_get_wlsw();
2852  if (res < 0)
2853  return res;
2854 
2855  /* Opportunistic update */
2856  tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2857 
2858  return snprintf(buf, PAGE_SIZE, "%d\n",
2859  (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2860 }
2861 
2862 static struct device_attribute dev_attr_hotkey_radio_sw =
2863  __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2864 
2865 static void hotkey_radio_sw_notify_change(void)
2866 {
2867  if (tp_features.hotkey_wlsw)
2868  sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2869  "hotkey_radio_sw");
2870 }
2871 
2872 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2873 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2874  struct device_attribute *attr,
2875  char *buf)
2876 {
2877  int res, s;
2878  res = hotkey_get_tablet_mode(&s);
2879  if (res < 0)
2880  return res;
2881 
2882  return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2883 }
2884 
2885 static struct device_attribute dev_attr_hotkey_tablet_mode =
2886  __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2887 
2888 static void hotkey_tablet_mode_notify_change(void)
2889 {
2890  if (tp_features.hotkey_tablet)
2891  sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2892  "hotkey_tablet_mode");
2893 }
2894 
2895 /* sysfs hotkey report_mode -------------------------------------------- */
2896 static ssize_t hotkey_report_mode_show(struct device *dev,
2897  struct device_attribute *attr,
2898  char *buf)
2899 {
2900  return snprintf(buf, PAGE_SIZE, "%d\n",
2901  (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2902 }
2903 
2904 static struct device_attribute dev_attr_hotkey_report_mode =
2905  __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2906 
2907 /* sysfs wakeup reason (pollable) -------------------------------------- */
2908 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2909  struct device_attribute *attr,
2910  char *buf)
2911 {
2912  return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2913 }
2914 
2915 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2916  __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2917 
2918 static void hotkey_wakeup_reason_notify_change(void)
2919 {
2920  sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2921  "wakeup_reason");
2922 }
2923 
2924 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2925 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2926  struct device_attribute *attr,
2927  char *buf)
2928 {
2929  return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2930 }
2931 
2932 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2933  __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2934  hotkey_wakeup_hotunplug_complete_show, NULL);
2935 
2936 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2937 {
2938  sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2939  "wakeup_hotunplug_complete");
2940 }
2941 
2942 /* --------------------------------------------------------------------- */
2943 
2944 static struct attribute *hotkey_attributes[] __initdata = {
2945  &dev_attr_hotkey_enable.attr,
2946  &dev_attr_hotkey_bios_enabled.attr,
2947  &dev_attr_hotkey_bios_mask.attr,
2948  &dev_attr_hotkey_report_mode.attr,
2949  &dev_attr_hotkey_wakeup_reason.attr,
2950  &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2951  &dev_attr_hotkey_mask.attr,
2952  &dev_attr_hotkey_all_mask.attr,
2953  &dev_attr_hotkey_recommended_mask.attr,
2954 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2955  &dev_attr_hotkey_source_mask.attr,
2956  &dev_attr_hotkey_poll_freq.attr,
2957 #endif
2958 };
2959 
2960 /*
2961  * Sync both the hw and sw blocking state of all switches
2962  */
2963 static void tpacpi_send_radiosw_update(void)
2964 {
2965  int wlsw;
2966 
2967  /*
2968  * We must sync all rfkill controllers *before* issuing any
2969  * rfkill input events, or we will race the rfkill core input
2970  * handler.
2971  *
2972  * tpacpi_inputdev_send_mutex works as a synchronization point
2973  * for the above.
2974  *
2975  * We optimize to avoid numerous calls to hotkey_get_wlsw.
2976  */
2977 
2978  wlsw = hotkey_get_wlsw();
2979 
2980  /* Sync hw blocking state first if it is hw-blocked */
2981  if (wlsw == TPACPI_RFK_RADIO_OFF)
2982  tpacpi_rfk_update_hwblock_state(true);
2983 
2984  /* Sync sw blocking state */
2985  tpacpi_rfk_update_swstate_all();
2986 
2987  /* Sync hw blocking state last if it is hw-unblocked */
2988  if (wlsw == TPACPI_RFK_RADIO_ON)
2989  tpacpi_rfk_update_hwblock_state(false);
2990 
2991  /* Issue rfkill input event for WLSW switch */
2992  if (!(wlsw < 0)) {
2993  mutex_lock(&tpacpi_inputdev_send_mutex);
2994 
2995  input_report_switch(tpacpi_inputdev,
2996  SW_RFKILL_ALL, (wlsw > 0));
2997  input_sync(tpacpi_inputdev);
2998 
2999  mutex_unlock(&tpacpi_inputdev_send_mutex);
3000  }
3001 
3002  /*
3003  * this can be unconditional, as we will poll state again
3004  * if userspace uses the notify to read data
3005  */
3006  hotkey_radio_sw_notify_change();
3007 }
3008 
3009 static void hotkey_exit(void)
3010 {
3011 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3012  mutex_lock(&hotkey_mutex);
3013  hotkey_poll_stop_sync();
3014  mutex_unlock(&hotkey_mutex);
3015 #endif
3016 
3017  if (hotkey_dev_attributes)
3018  delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3019 
3021  "restoring original HKEY status and mask\n");
3022  /* yes, there is a bitwise or below, we want the
3023  * functions to be called even if one of them fail */
3024  if (((tp_features.hotkey_mask &&
3025  hotkey_mask_set(hotkey_orig_mask)) |
3026  hotkey_status_set(false)) != 0)
3027  pr_err("failed to restore hot key mask "
3028  "to BIOS defaults\n");
3029 }
3030 
3031 static void __init hotkey_unmap(const unsigned int scancode)
3032 {
3033  if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3034  clear_bit(hotkey_keycode_map[scancode],
3035  tpacpi_inputdev->keybit);
3036  hotkey_keycode_map[scancode] = KEY_RESERVED;
3037  }
3038 }
3039 
3040 /*
3041  * HKEY quirks:
3042  * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3043  */
3044 
3045 #define TPACPI_HK_Q_INIMASK 0x0001
3046 
3047 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3048  TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3049  TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3050  TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3051  TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3052  TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3053  TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3054  TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3055  TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3056  TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3057  TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3058  TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3059  TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3060  TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3061  TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3062  TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3063  TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3064  TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3065  TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3066  TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3067 };
3068 
3070 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3071 
3072 static int __init hotkey_init(struct ibm_init_struct *iibm)
3073 {
3074  /* Requirements for changing the default keymaps:
3075  *
3076  * 1. Many of the keys are mapped to KEY_RESERVED for very
3077  * good reasons. Do not change them unless you have deep
3078  * knowledge on the IBM and Lenovo ThinkPad firmware for
3079  * the various ThinkPad models. The driver behaves
3080  * differently for KEY_RESERVED: such keys have their
3081  * hot key mask *unset* in mask_recommended, and also
3082  * in the initial hot key mask programmed into the
3083  * firmware at driver load time, which means the firm-
3084  * ware may react very differently if you change them to
3085  * something else;
3086  *
3087  * 2. You must be subscribed to the linux-thinkpad and
3088  * ibm-acpi-devel mailing lists, and you should read the
3089  * list archives since 2007 if you want to change the
3090  * keymaps. This requirement exists so that you will
3091  * know the past history of problems with the thinkpad-
3092  * acpi driver keymaps, and also that you will be
3093  * listening to any bug reports;
3094  *
3095  * 3. Do not send thinkpad-acpi specific patches directly to
3096  * for merging, *ever*. Send them to the linux-acpi
3097  * mailinglist for comments. Merging is to be done only
3098  * through acpi-test and the ACPI maintainer.
3099  *
3100  * If the above is too much to ask, don't change the keymap.
3101  * Ask the thinkpad-acpi maintainer to do it, instead.
3102  */
3103 
3104  enum keymap_index {
3105  TPACPI_KEYMAP_IBM_GENERIC = 0,
3106  TPACPI_KEYMAP_LENOVO_GENERIC,
3107  };
3108 
3109  static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3110  /* Generic keymap for IBM ThinkPads */
3111  [TPACPI_KEYMAP_IBM_GENERIC] = {
3112  /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3116 
3117  /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3118  KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3119  KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3120  KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3121 
3122  /* brightness: firmware always reacts to them */
3123  KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3124  KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3125 
3126  /* Thinklight: firmware always react to it */
3127  KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3128 
3129  KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3130  KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3131 
3132  /* Volume: firmware always react to it and reprograms
3133  * the built-in *extra* mixer. Never map it to control
3134  * another mixer by default. */
3135  KEY_RESERVED, /* 0x14: VOLUME UP */
3136  KEY_RESERVED, /* 0x15: VOLUME DOWN */
3137  KEY_RESERVED, /* 0x16: MUTE */
3138 
3139  KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3140 
3141  /* (assignments unknown, please report if found) */
3144  },
3145 
3146  /* Generic keymap for Lenovo ThinkPads */
3147  [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3148  /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3152 
3153  /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3154  KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3155  KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3156  KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3157 
3158  /* These should be enabled --only-- when ACPI video
3159  * is disabled (i.e. in "vendor" mode), and are handled
3160  * in a special way by the init code */
3161  KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3162  KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3163 
3164  KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3165 
3166  KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3167  KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3168 
3169  /* Volume: z60/z61, T60 (BIOS version?): firmware always
3170  * react to it and reprograms the built-in *extra* mixer.
3171  * Never map it to control another mixer by default.
3172  *
3173  * T60?, T61, R60?, R61: firmware and EC tries to send
3174  * these over the regular keyboard, so these are no-ops,
3175  * but there are still weird bugs re. MUTE, so do not
3176  * change unless you get test reports from all Lenovo
3177  * models. May cause the BIOS to interfere with the
3178  * HDA mixer.
3179  */
3180  KEY_RESERVED, /* 0x14: VOLUME UP */
3181  KEY_RESERVED, /* 0x15: VOLUME DOWN */
3182  KEY_RESERVED, /* 0x16: MUTE */
3183 
3184  KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3185 
3186  /* (assignments unknown, please report if found) */
3188 
3189  /*
3190  * The mic mute button only sends 0x1a. It does not
3191  * automatically mute the mic or change the mute light.
3192  */
3193  KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3194 
3195  /* (assignments unknown, please report if found) */
3197  KEY_UNKNOWN,
3198  },
3199  };
3200 
3201  static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3202  /* Generic maps (fallback) */
3203  {
3205  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3206  .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3207  },
3208  {
3209  .vendor = PCI_VENDOR_ID_LENOVO,
3210  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3211  .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3212  },
3213  };
3214 
3215 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3216 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3217 
3218  int res, i;
3219  int status;
3220  int hkeyv;
3221  bool radiosw_state = false;
3222  bool tabletsw_state = false;
3223 
3224  unsigned long quirks;
3225  unsigned long keymap_id;
3226 
3228  "initializing hotkey subdriver\n");
3229 
3230  BUG_ON(!tpacpi_inputdev);
3231  BUG_ON(tpacpi_inputdev->open != NULL ||
3232  tpacpi_inputdev->close != NULL);
3233 
3234  TPACPI_ACPIHANDLE_INIT(hkey);
3235  mutex_init(&hotkey_mutex);
3236 
3237 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3238  mutex_init(&hotkey_thread_mutex);
3239  mutex_init(&hotkey_thread_data_mutex);
3240 #endif
3241 
3242  /* hotkey not supported on 570 */
3243  tp_features.hotkey = hkey_handle != NULL;
3244 
3246  "hotkeys are %s\n",
3247  str_supported(tp_features.hotkey));
3248 
3249  if (!tp_features.hotkey)
3250  return 1;
3251 
3252  quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3253  ARRAY_SIZE(tpacpi_hotkey_qtable));
3254 
3255  tpacpi_disable_brightness_delay();
3256 
3257  /* MUST have enough space for all attributes to be added to
3258  * hotkey_dev_attributes */
3259  hotkey_dev_attributes = create_attr_set(
3260  ARRAY_SIZE(hotkey_attributes) + 2,
3261  NULL);
3262  if (!hotkey_dev_attributes)
3263  return -ENOMEM;
3264  res = add_many_to_attr_set(hotkey_dev_attributes,
3265  hotkey_attributes,
3266  ARRAY_SIZE(hotkey_attributes));
3267  if (res)
3268  goto err_exit;
3269 
3270  /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3271  A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3272  for HKEY interface version 0x100 */
3273  if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3274  if ((hkeyv >> 8) != 1) {
3275  pr_err("unknown version of the HKEY interface: 0x%x\n",
3276  hkeyv);
3277  pr_err("please report this to %s\n", TPACPI_MAIL);
3278  } else {
3279  /*
3280  * MHKV 0x100 in A31, R40, R40e,
3281  * T4x, X31, and later
3282  */
3284  "firmware HKEY interface version: 0x%x\n",
3285  hkeyv);
3286 
3287  /* Paranoia check AND init hotkey_all_mask */
3288  if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3289  "MHKA", "qd")) {
3290  pr_err("missing MHKA handler, "
3291  "please report this to %s\n",
3292  TPACPI_MAIL);
3293  /* Fallback: pre-init for FN+F3,F4,F12 */
3294  hotkey_all_mask = 0x080cU;
3295  } else {
3296  tp_features.hotkey_mask = 1;
3297  }
3298  }
3299  }
3300 
3302  "hotkey masks are %s\n",
3303  str_supported(tp_features.hotkey_mask));
3304 
3305  /* Init hotkey_all_mask if not initialized yet */
3306  if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3307  (quirks & TPACPI_HK_Q_INIMASK))
3308  hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3309 
3310  /* Init hotkey_acpi_mask and hotkey_orig_mask */
3311  if (tp_features.hotkey_mask) {
3312  /* hotkey_source_mask *must* be zero for
3313  * the first hotkey_mask_get to return hotkey_orig_mask */
3314  res = hotkey_mask_get();
3315  if (res)
3316  goto err_exit;
3317 
3318  hotkey_orig_mask = hotkey_acpi_mask;
3319  } else {
3320  hotkey_orig_mask = hotkey_all_mask;
3321  hotkey_acpi_mask = hotkey_all_mask;
3322  }
3323 
3324 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3325  if (dbg_wlswemul) {
3326  tp_features.hotkey_wlsw = 1;
3327  radiosw_state = !!tpacpi_wlsw_emulstate;
3328  pr_info("radio switch emulation enabled\n");
3329  } else
3330 #endif
3331  /* Not all thinkpads have a hardware radio switch */
3332  if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3333  tp_features.hotkey_wlsw = 1;
3334  radiosw_state = !!status;
3335  pr_info("radio switch found; radios are %s\n",
3336  enabled(status, 0));
3337  }
3338  if (tp_features.hotkey_wlsw)
3339  res = add_to_attr_set(hotkey_dev_attributes,
3340  &dev_attr_hotkey_radio_sw.attr);
3341 
3342  /* For X41t, X60t, X61t Tablets... */
3343  if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3344  tp_features.hotkey_tablet = 1;
3345  tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3346  pr_info("possible tablet mode switch found; "
3347  "ThinkPad in %s mode\n",
3348  (tabletsw_state) ? "tablet" : "laptop");
3349  res = add_to_attr_set(hotkey_dev_attributes,
3350  &dev_attr_hotkey_tablet_mode.attr);
3351  }
3352 
3353  if (!res)
3355  hotkey_dev_attributes,
3356  &tpacpi_pdev->dev.kobj);
3357  if (res)
3358  goto err_exit;
3359 
3360  /* Set up key map */
3361  hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3362  GFP_KERNEL);
3363  if (!hotkey_keycode_map) {
3364  pr_err("failed to allocate memory for key map\n");
3365  res = -ENOMEM;
3366  goto err_exit;
3367  }
3368 
3369  keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3370  ARRAY_SIZE(tpacpi_keymap_qtable));
3371  BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3373  "using keymap number %lu\n", keymap_id);
3374 
3375  memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3377 
3378  input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3379  tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3380  tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3381  tpacpi_inputdev->keycode = hotkey_keycode_map;
3382  for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3383  if (hotkey_keycode_map[i] != KEY_RESERVED) {
3384  input_set_capability(tpacpi_inputdev, EV_KEY,
3385  hotkey_keycode_map[i]);
3386  } else {
3387  if (i < sizeof(hotkey_reserved_mask)*8)
3388  hotkey_reserved_mask |= 1 << i;
3389  }
3390  }
3391 
3392  if (tp_features.hotkey_wlsw) {
3393  input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3394  input_report_switch(tpacpi_inputdev,
3395  SW_RFKILL_ALL, radiosw_state);
3396  }
3397  if (tp_features.hotkey_tablet) {
3398  input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3399  input_report_switch(tpacpi_inputdev,
3400  SW_TABLET_MODE, tabletsw_state);
3401  }
3402 
3403  /* Do not issue duplicate brightness change events to
3404  * userspace. tpacpi_detect_brightness_capabilities() must have
3405  * been called before this point */
3407  pr_info("This ThinkPad has standard ACPI backlight "
3408  "brightness control, supported by the ACPI "
3409  "video driver\n");
3410  pr_notice("Disabling thinkpad-acpi brightness events "
3411  "by default...\n");
3412 
3413  /* Disable brightness up/down on Lenovo thinkpads when
3414  * ACPI is handling them, otherwise it is plain impossible
3415  * for userspace to do something even remotely sane */
3416  hotkey_reserved_mask |=
3418  | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3419  hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3420  hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3421  }
3422 
3423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3424  hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3425  & ~hotkey_all_mask
3426  & ~hotkey_reserved_mask;
3427 
3429  "hotkey source mask 0x%08x, polling freq %u\n",
3430  hotkey_source_mask, hotkey_poll_freq);
3431 #endif
3432 
3434  "enabling firmware HKEY event interface...\n");
3435  res = hotkey_status_set(true);
3436  if (res) {
3437  hotkey_exit();
3438  return res;
3439  }
3440  res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3441  | hotkey_driver_mask)
3442  & ~hotkey_source_mask);
3443  if (res < 0 && res != -ENXIO) {
3444  hotkey_exit();
3445  return res;
3446  }
3447  hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3448  & ~hotkey_reserved_mask;
3450  "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3451  hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3452 
3454  "legacy ibm/hotkey event reporting over procfs %s\n",
3455  (hotkey_report_mode < 2) ?
3456  "enabled" : "disabled");
3457 
3458  tpacpi_inputdev->open = &hotkey_inputdev_open;
3459  tpacpi_inputdev->close = &hotkey_inputdev_close;
3460 
3461  hotkey_poll_setup_safe(true);
3462 
3463  return 0;
3464 
3465 err_exit:
3466  delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3467  hotkey_dev_attributes = NULL;
3468 
3469  return (res < 0)? res : 1;
3470 }
3471 
3472 static bool hotkey_notify_hotkey(const u32 hkey,
3473  bool *send_acpi_ev,
3474  bool *ignore_acpi_ev)
3475 {
3476  /* 0x1000-0x1FFF: key presses */
3477  unsigned int scancode = hkey & 0xfff;
3478  *send_acpi_ev = true;
3479  *ignore_acpi_ev = false;
3480 
3481  /* HKEY event 0x1001 is scancode 0x00 */
3482  if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3483  scancode--;
3484  if (!(hotkey_source_mask & (1 << scancode))) {
3485  tpacpi_input_send_key_masked(scancode);
3486  *send_acpi_ev = false;
3487  } else {
3488  *ignore_acpi_ev = true;
3489  }
3490  return true;
3491  }
3492  return false;
3493 }
3494 
3495 static bool hotkey_notify_wakeup(const u32 hkey,
3496  bool *send_acpi_ev,
3497  bool *ignore_acpi_ev)
3498 {
3499  /* 0x2000-0x2FFF: Wakeup reason */
3500  *send_acpi_ev = true;
3501  *ignore_acpi_ev = false;
3502 
3503  switch (hkey) {
3504  case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3505  case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3506  hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3507  *ignore_acpi_ev = true;
3508  break;
3509 
3510  case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3511  case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3512  hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3513  *ignore_acpi_ev = true;
3514  break;
3515 
3516  case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3517  case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3518  pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3519  /* how to auto-heal: */
3520  /* 2313: woke up from S3, go to S4/S5 */
3521  /* 2413: woke up from S4, go to S5 */
3522  break;
3523 
3524  default:
3525  return false;
3526  }
3527 
3528  if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3529  pr_info("woke up due to a hot-unplug request...\n");
3530  hotkey_wakeup_reason_notify_change();
3531  }
3532  return true;
3533 }
3534 
3535 static bool hotkey_notify_dockevent(const u32 hkey,
3536  bool *send_acpi_ev,
3537  bool *ignore_acpi_ev)
3538 {
3539  /* 0x4000-0x4FFF: dock-related events */
3540  *send_acpi_ev = true;
3541  *ignore_acpi_ev = false;
3542 
3543  switch (hkey) {
3544  case TP_HKEY_EV_UNDOCK_ACK:
3545  /* ACPI undock operation completed after wakeup */
3546  hotkey_autosleep_ack = 1;
3547  pr_info("undocked\n");
3548  hotkey_wakeup_hotunplug_complete_notify_change();
3549  return true;
3550 
3551  case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3552  pr_info("docked into hotplug port replicator\n");
3553  return true;
3554  case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3555  pr_info("undocked from hotplug port replicator\n");
3556  return true;
3557 
3558  default:
3559  return false;
3560  }
3561 }
3562 
3563 static bool hotkey_notify_usrevent(const u32 hkey,
3564  bool *send_acpi_ev,
3565  bool *ignore_acpi_ev)
3566 {
3567  /* 0x5000-0x5FFF: human interface helpers */
3568  *send_acpi_ev = true;
3569  *ignore_acpi_ev = false;
3570 
3571  switch (hkey) {
3572  case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3573  case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3574  return true;
3575 
3576  case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3577  case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3578  tpacpi_input_send_tabletsw();
3579  hotkey_tablet_mode_notify_change();
3580  *send_acpi_ev = false;
3581  return true;
3582 
3583  case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3584  case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3585  case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3586  /* do not propagate these events */
3587  *ignore_acpi_ev = true;
3588  return true;
3589 
3590  default:
3591  return false;
3592  }
3593 }
3594 
3595 static void thermal_dump_all_sensors(void);
3596 
3597 static bool hotkey_notify_6xxx(const u32 hkey,
3598  bool *send_acpi_ev,
3599  bool *ignore_acpi_ev)
3600 {
3601  bool known = true;
3602 
3603  /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3604  *send_acpi_ev = true;
3605  *ignore_acpi_ev = false;
3606 
3607  switch (hkey) {
3609  pr_info("EC reports that Thermal Table has changed\n");
3610  /* recommended action: do nothing, we don't have
3611  * Lenovo ATM information */
3612  return true;
3614  pr_crit("THERMAL ALARM: battery is too hot!\n");
3615  /* recommended action: warn user through gui */
3616  break;
3618  pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3619  /* recommended action: immediate sleep/hibernate */
3620  break;
3622  pr_crit("THERMAL ALARM: "
3623  "a sensor reports something is too hot!\n");
3624  /* recommended action: warn user through gui, that */
3625  /* some internal component is too hot */
3626  break;
3628  pr_alert("THERMAL EMERGENCY: "
3629  "a sensor reports something is extremely hot!\n");
3630  /* recommended action: immediate sleep/hibernate */
3631  break;
3632 
3634  case TP_HKEY_EV_KEY_FN:
3635  /* key press events, we just ignore them as long as the EC
3636  * is still reporting them in the normal keyboard stream */
3637  *send_acpi_ev = false;
3638  *ignore_acpi_ev = true;
3639  return true;
3640 
3641  default:
3642  pr_warn("unknown possible thermal alarm or keyboard event received\n");
3643  known = false;
3644  }
3645 
3646  thermal_dump_all_sensors();
3647 
3648  return known;
3649 }
3650 
3651 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3652 {
3653  u32 hkey;
3654  bool send_acpi_ev;
3655  bool ignore_acpi_ev;
3656  bool known_ev;
3657 
3658  if (event != 0x80) {
3659  pr_err("unknown HKEY notification event %d\n", event);
3660  /* forward it to userspace, maybe it knows how to handle it */
3662  ibm->acpi->device->pnp.device_class,
3663  dev_name(&ibm->acpi->device->dev),
3664  event, 0);
3665  return;
3666  }
3667 
3668  while (1) {
3669  if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3670  pr_err("failed to retrieve HKEY event\n");
3671  return;
3672  }
3673 
3674  if (hkey == 0) {
3675  /* queue empty */
3676  return;
3677  }
3678 
3679  send_acpi_ev = true;
3680  ignore_acpi_ev = false;
3681 
3682  switch (hkey >> 12) {
3683  case 1:
3684  /* 0x1000-0x1FFF: key presses */
3685  known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3686  &ignore_acpi_ev);
3687  break;
3688  case 2:
3689  /* 0x2000-0x2FFF: Wakeup reason */
3690  known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3691  &ignore_acpi_ev);
3692  break;
3693  case 3:
3694  /* 0x3000-0x3FFF: bay-related wakeups */
3695  switch (hkey) {
3696  case TP_HKEY_EV_BAYEJ_ACK:
3697  hotkey_autosleep_ack = 1;
3698  pr_info("bay ejected\n");
3699  hotkey_wakeup_hotunplug_complete_notify_change();
3700  known_ev = true;
3701  break;
3702  case TP_HKEY_EV_OPTDRV_EJ:
3703  /* FIXME: kick libata if SATA link offline */
3704  known_ev = true;
3705  break;
3706  default:
3707  known_ev = false;
3708  }
3709  break;
3710  case 4:
3711  /* 0x4000-0x4FFF: dock-related events */
3712  known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3713  &ignore_acpi_ev);
3714  break;
3715  case 5:
3716  /* 0x5000-0x5FFF: human interface helpers */
3717  known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3718  &ignore_acpi_ev);
3719  break;
3720  case 6:
3721  /* 0x6000-0x6FFF: thermal alarms/notices and
3722  * keyboard events */
3723  known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3724  &ignore_acpi_ev);
3725  break;
3726  case 7:
3727  /* 0x7000-0x7FFF: misc */
3728  if (tp_features.hotkey_wlsw &&
3729  hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3730  tpacpi_send_radiosw_update();
3731  send_acpi_ev = 0;
3732  known_ev = true;
3733  break;
3734  }
3735  /* fallthrough to default */
3736  default:
3737  known_ev = false;
3738  }
3739  if (!known_ev) {
3740  pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3741  pr_notice("please report the conditions when this "
3742  "event happened to %s\n", TPACPI_MAIL);
3743  }
3744 
3745  /* Legacy events */
3746  if (!ignore_acpi_ev &&
3747  (send_acpi_ev || hotkey_report_mode < 2)) {
3748  acpi_bus_generate_proc_event(ibm->acpi->device,
3749  event, hkey);
3750  }
3751 
3752  /* netlink events */
3753  if (!ignore_acpi_ev && send_acpi_ev) {
3755  ibm->acpi->device->pnp.device_class,
3756  dev_name(&ibm->acpi->device->dev),
3757  event, hkey);
3758  }
3759  }
3760 }
3761 
3762 static void hotkey_suspend(void)
3763 {
3764  /* Do these on suspend, we get the events on early resume! */
3765  hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3766  hotkey_autosleep_ack = 0;
3767 }
3768 
3769 static void hotkey_resume(void)
3770 {
3771  tpacpi_disable_brightness_delay();
3772 
3773  if (hotkey_status_set(true) < 0 ||
3774  hotkey_mask_set(hotkey_acpi_mask) < 0)
3775  pr_err("error while attempting to reset the event "
3776  "firmware interface\n");
3777 
3778  tpacpi_send_radiosw_update();
3779  hotkey_tablet_mode_notify_change();
3780  hotkey_wakeup_reason_notify_change();
3781  hotkey_wakeup_hotunplug_complete_notify_change();
3782  hotkey_poll_setup_safe(false);
3783 }
3784 
3785 /* procfs -------------------------------------------------------------- */
3786 static int hotkey_read(struct seq_file *m)
3787 {
3788  int res, status;
3789 
3790  if (!tp_features.hotkey) {
3791  seq_printf(m, "status:\t\tnot supported\n");
3792  return 0;
3793  }
3794 
3795  if (mutex_lock_killable(&hotkey_mutex))
3796  return -ERESTARTSYS;
3797  res = hotkey_status_get(&status);
3798  if (!res)
3799  res = hotkey_mask_get();
3800  mutex_unlock(&hotkey_mutex);
3801  if (res)
3802  return res;
3803 
3804  seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3805  if (hotkey_all_mask) {
3806  seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3807  seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3808  } else {
3809  seq_printf(m, "mask:\t\tnot supported\n");
3810  seq_printf(m, "commands:\tenable, disable, reset\n");
3811  }
3812 
3813  return 0;
3814 }
3815 
3816 static void hotkey_enabledisable_warn(bool enable)
3817 {
3818  tpacpi_log_usertask("procfs hotkey enable/disable");
3819  if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3820  pr_fmt("hotkey enable/disable functionality has been "
3821  "removed from the driver. "
3822  "Hotkeys are always enabled.\n")))
3823  pr_err("Please remove the hotkey=enable module "
3824  "parameter, it is deprecated. "
3825  "Hotkeys are always enabled.\n");
3826 }
3827 
3828 static int hotkey_write(char *buf)
3829 {
3830  int res;
3831  u32 mask;
3832  char *cmd;
3833 
3834  if (!tp_features.hotkey)
3835  return -ENODEV;
3836 
3837  if (mutex_lock_killable(&hotkey_mutex))
3838  return -ERESTARTSYS;
3839 
3840  mask = hotkey_user_mask;
3841 
3842  res = 0;
3843  while ((cmd = next_cmd(&buf))) {
3844  if (strlencmp(cmd, "enable") == 0) {
3845  hotkey_enabledisable_warn(1);
3846  } else if (strlencmp(cmd, "disable") == 0) {
3847  hotkey_enabledisable_warn(0);
3848  res = -EPERM;
3849  } else if (strlencmp(cmd, "reset") == 0) {
3850  mask = (hotkey_all_mask | hotkey_source_mask)
3851  & ~hotkey_reserved_mask;
3852  } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3853  /* mask set */
3854  } else if (sscanf(cmd, "%x", &mask) == 1) {
3855  /* mask set */
3856  } else {
3857  res = -EINVAL;
3858  goto errexit;
3859  }
3860  }
3861 
3862  if (!res) {
3863  tpacpi_disclose_usertask("procfs hotkey",
3864  "set mask to 0x%08x\n", mask);
3865  res = hotkey_user_mask_set(mask);
3866  }
3867 
3868 errexit:
3869  mutex_unlock(&hotkey_mutex);
3870  return res;
3871 }
3872 
3873 static const struct acpi_device_id ibm_htk_device_ids[] = {
3876  {"", 0},
3877 };
3878 
3879 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3880  .hid = ibm_htk_device_ids,
3881  .notify = hotkey_notify,
3882  .handle = &hkey_handle,
3883  .type = ACPI_DEVICE_NOTIFY,
3884 };
3885 
3886 static struct ibm_struct hotkey_driver_data = {
3887  .name = "hotkey",
3888  .read = hotkey_read,
3889  .write = hotkey_write,
3890  .exit = hotkey_exit,
3891  .resume = hotkey_resume,
3892  .suspend = hotkey_suspend,
3893  .acpi = &ibm_hotkey_acpidriver,
3894 };
3895 
3896 /*************************************************************************
3897  * Bluetooth subdriver
3898  */
3899 
3900 enum {
3901  /* ACPI GBDC/SBDC bits */
3902  TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3903  TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3904  TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3905  0 = disable, 1 = enable */
3906 };
3907 
3908 enum {
3909  /* ACPI \BLTH commands */
3910  TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3911  TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3912  TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3913  TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3914  TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3915 };
3916 
3917 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3918 
3919 static int bluetooth_get_status(void)
3920 {
3921  int status;
3922 
3923 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3924  if (dbg_bluetoothemul)
3925  return (tpacpi_bluetooth_emulstate) ?
3926  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3927 #endif
3928 
3929  if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3930  return -EIO;
3931 
3932  return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3933  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3934 }
3935 
3936 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3937 {
3938  int status;
3939 
3941  "will attempt to %s bluetooth\n",
3942  (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3943 
3944 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3945  if (dbg_bluetoothemul) {
3946  tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3947  return 0;
3948  }
3949 #endif
3950 
3951  if (state == TPACPI_RFK_RADIO_ON)
3954  else
3955  status = 0;
3956 
3957  if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3958  return -EIO;
3959 
3960  return 0;
3961 }
3962 
3963 /* sysfs bluetooth enable ---------------------------------------------- */
3964 static ssize_t bluetooth_enable_show(struct device *dev,
3965  struct device_attribute *attr,
3966  char *buf)
3967 {
3968  return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3969  attr, buf);
3970 }
3971 
3972 static ssize_t bluetooth_enable_store(struct device *dev,
3973  struct device_attribute *attr,
3974  const char *buf, size_t count)
3975 {
3976  return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3977  attr, buf, count);
3978 }
3979 
3980 static struct device_attribute dev_attr_bluetooth_enable =
3981  __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3982  bluetooth_enable_show, bluetooth_enable_store);
3983 
3984 /* --------------------------------------------------------------------- */
3985 
3986 static struct attribute *bluetooth_attributes[] = {
3987  &dev_attr_bluetooth_enable.attr,
3988  NULL
3989 };
3990 
3991 static const struct attribute_group bluetooth_attr_group = {
3992  .attrs = bluetooth_attributes,
3993 };
3994 
3995 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3996  .get_status = bluetooth_get_status,
3997  .set_status = bluetooth_set_status,
3998 };
3999 
4000 static void bluetooth_shutdown(void)
4001 {
4002  /* Order firmware to save current state to NVRAM */
4003  if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4005  pr_notice("failed to save bluetooth state to NVRAM\n");
4006  else
4008  "bluetooth state saved to NVRAM\n");
4009 }
4010 
4011 static void bluetooth_exit(void)
4012 {
4013  sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4014  &bluetooth_attr_group);
4015 
4016  tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4017 
4018  bluetooth_shutdown();
4019 }
4020 
4021 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4022 {
4023  int res;
4024  int status = 0;
4025 
4027  "initializing bluetooth subdriver\n");
4028 
4029  TPACPI_ACPIHANDLE_INIT(hkey);
4030 
4031  /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4032  G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4033  tp_features.bluetooth = hkey_handle &&
4034  acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4035 
4037  "bluetooth is %s, status 0x%02x\n",
4038  str_supported(tp_features.bluetooth),
4039  status);
4040 
4041 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4042  if (dbg_bluetoothemul) {
4043  tp_features.bluetooth = 1;
4044  pr_info("bluetooth switch emulation enabled\n");
4045  } else
4046 #endif
4047  if (tp_features.bluetooth &&
4048  !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4049  /* no bluetooth hardware present in system */
4050  tp_features.bluetooth = 0;
4052  "bluetooth hardware not installed\n");
4053  }
4054 
4055  if (!tp_features.bluetooth)
4056  return 1;
4057 
4058  res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4059  &bluetooth_tprfk_ops,
4062  true);
4063  if (res)
4064  return res;
4065 
4066  res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4067  &bluetooth_attr_group);
4068  if (res) {
4069  tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4070  return res;
4071  }
4072 
4073  return 0;
4074 }
4075 
4076 /* procfs -------------------------------------------------------------- */
4077 static int bluetooth_read(struct seq_file *m)
4078 {
4079  return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4080 }
4081 
4082 static int bluetooth_write(char *buf)
4083 {
4084  return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4085 }
4086 
4087 static struct ibm_struct bluetooth_driver_data = {
4088  .name = "bluetooth",
4089  .read = bluetooth_read,
4090  .write = bluetooth_write,
4091  .exit = bluetooth_exit,
4092  .shutdown = bluetooth_shutdown,
4093 };
4094 
4095 /*************************************************************************
4096  * Wan subdriver
4097  */
4098 
4099 enum {
4100  /* ACPI GWAN/SWAN bits */
4101  TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4102  TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4103  TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4104  0 = disable, 1 = enable */
4105 };
4106 
4107 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4108 
4109 static int wan_get_status(void)
4110 {
4111  int status;
4112 
4113 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4114  if (dbg_wwanemul)
4115  return (tpacpi_wwan_emulstate) ?
4116  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4117 #endif
4118 
4119  if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4120  return -EIO;
4121 
4122  return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4123  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4124 }
4125 
4126 static int wan_set_status(enum tpacpi_rfkill_state state)
4127 {
4128  int status;
4129 
4131  "will attempt to %s wwan\n",
4132  (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4133 
4134 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4135  if (dbg_wwanemul) {
4136  tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4137  return 0;
4138  }
4139 #endif
4140 
4141  if (state == TPACPI_RFK_RADIO_ON)
4142  status = TP_ACPI_WANCARD_RADIOSSW
4144  else
4145  status = 0;
4146 
4147  if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4148  return -EIO;
4149 
4150  return 0;
4151 }
4152 
4153 /* sysfs wan enable ---------------------------------------------------- */
4154 static ssize_t wan_enable_show(struct device *dev,
4155  struct device_attribute *attr,
4156  char *buf)
4157 {
4158  return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4159  attr, buf);
4160 }
4161 
4162 static ssize_t wan_enable_store(struct device *dev,
4163  struct device_attribute *attr,
4164  const char *buf, size_t count)
4165 {
4166  return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4167  attr, buf, count);
4168 }
4169 
4170 static struct device_attribute dev_attr_wan_enable =
4171  __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4172  wan_enable_show, wan_enable_store);
4173 
4174 /* --------------------------------------------------------------------- */
4175 
4176 static struct attribute *wan_attributes[] = {
4177  &dev_attr_wan_enable.attr,
4178  NULL
4179 };
4180 
4181 static const struct attribute_group wan_attr_group = {
4182  .attrs = wan_attributes,
4183 };
4184 
4185 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4186  .get_status = wan_get_status,
4187  .set_status = wan_set_status,
4188 };
4189 
4190 static void wan_shutdown(void)
4191 {
4192  /* Order firmware to save current state to NVRAM */
4193  if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4195  pr_notice("failed to save WWAN state to NVRAM\n");
4196  else
4198  "WWAN state saved to NVRAM\n");
4199 }
4200 
4201 static void wan_exit(void)
4202 {
4203  sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4204  &wan_attr_group);
4205 
4206  tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4207 
4208  wan_shutdown();
4209 }
4210 
4211 static int __init wan_init(struct ibm_init_struct *iibm)
4212 {
4213  int res;
4214  int status = 0;
4215 
4217  "initializing wan subdriver\n");
4218 
4219  TPACPI_ACPIHANDLE_INIT(hkey);
4220 
4221  tp_features.wan = hkey_handle &&
4222  acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4223 
4225  "wan is %s, status 0x%02x\n",
4226  str_supported(tp_features.wan),
4227  status);
4228 
4229 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4230  if (dbg_wwanemul) {
4231  tp_features.wan = 1;
4232  pr_info("wwan switch emulation enabled\n");
4233  } else
4234 #endif
4235  if (tp_features.wan &&
4236  !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4237  /* no wan hardware present in system */
4238  tp_features.wan = 0;
4240  "wan hardware not installed\n");
4241  }
4242 
4243  if (!tp_features.wan)
4244  return 1;
4245 
4246  res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4247  &wan_tprfk_ops,
4250  true);
4251  if (res)
4252  return res;
4253 
4254  res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4255  &wan_attr_group);
4256 
4257  if (res) {
4258  tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4259  return res;
4260  }
4261 
4262  return 0;
4263 }
4264 
4265 /* procfs -------------------------------------------------------------- */
4266 static int wan_read(struct seq_file *m)
4267 {
4268  return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4269 }
4270 
4271 static int wan_write(char *buf)
4272 {
4273  return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4274 }
4275 
4276 static struct ibm_struct wan_driver_data = {
4277  .name = "wan",
4278  .read = wan_read,
4279  .write = wan_write,
4280  .exit = wan_exit,
4281  .shutdown = wan_shutdown,
4282 };
4283 
4284 /*************************************************************************
4285  * UWB subdriver
4286  */
4287 
4288 enum {
4289  /* ACPI GUWB/SUWB bits */
4290  TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4291  TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4292 };
4293 
4294 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4295 
4296 static int uwb_get_status(void)
4297 {
4298  int status;
4299 
4300 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4301  if (dbg_uwbemul)
4302  return (tpacpi_uwb_emulstate) ?
4303  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4304 #endif
4305 
4306  if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4307  return -EIO;
4308 
4309  return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4310  TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4311 }
4312 
4313 static int uwb_set_status(enum tpacpi_rfkill_state state)
4314 {
4315  int status;
4316 
4318  "will attempt to %s UWB\n",
4319  (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4320 
4321 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4322  if (dbg_uwbemul) {
4323  tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4324  return 0;
4325  }
4326 #endif
4327 
4328  if (state == TPACPI_RFK_RADIO_ON)
4329  status = TP_ACPI_UWB_RADIOSSW;
4330  else
4331  status = 0;
4332 
4333  if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4334  return -EIO;
4335 
4336  return 0;
4337 }
4338 
4339 /* --------------------------------------------------------------------- */
4340 
4341 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4342  .get_status = uwb_get_status,
4343  .set_status = uwb_set_status,
4344 };
4345 
4346 static void uwb_exit(void)
4347 {
4348  tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4349 }
4350 
4351 static int __init uwb_init(struct ibm_init_struct *iibm)
4352 {
4353  int res;
4354  int status = 0;
4355 
4357  "initializing uwb subdriver\n");
4358 
4359  TPACPI_ACPIHANDLE_INIT(hkey);
4360 
4361  tp_features.uwb = hkey_handle &&
4362  acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4363 
4365  "uwb is %s, status 0x%02x\n",
4366  str_supported(tp_features.uwb),
4367  status);
4368 
4369 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4370  if (dbg_uwbemul) {
4371  tp_features.uwb = 1;
4372  pr_info("uwb switch emulation enabled\n");
4373  } else
4374 #endif
4375  if (tp_features.uwb &&
4376  !(status & TP_ACPI_UWB_HWPRESENT)) {
4377  /* no uwb hardware present in system */
4378  tp_features.uwb = 0;
4380  "uwb hardware not installed\n");
4381  }
4382 
4383  if (!tp_features.uwb)
4384  return 1;
4385 
4386  res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4387  &uwb_tprfk_ops,
4390  false);
4391  return res;
4392 }
4393 
4394 static struct ibm_struct uwb_driver_data = {
4395  .name = "uwb",
4396  .exit = uwb_exit,
4397  .flags.experimental = 1,
4398 };
4399 
4400 /*************************************************************************
4401  * Video subdriver
4402  */
4403 
4404 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4405 
4406 enum video_access_mode {
4407  TPACPI_VIDEO_NONE = 0,
4408  TPACPI_VIDEO_570, /* 570 */
4409  TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4410  TPACPI_VIDEO_NEW, /* all others */
4411 };
4412 
4413 enum { /* video status flags, based on VIDEO_570 */
4414  TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4415  TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4416  TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4417 };
4418 
4419 enum { /* TPACPI_VIDEO_570 constants */
4420  TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4421  TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4422  * video_status_flags */
4423  TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4424  TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4425 };
4426 
4427 static enum video_access_mode video_supported;
4428 static int video_orig_autosw;
4429 
4430 static int video_autosw_get(void);
4431 static int video_autosw_set(int enable);
4432 
4433 TPACPI_HANDLE(vid, root,
4434  "\\_SB.PCI.AGP.VGA", /* 570 */
4435  "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4436  "\\_SB.PCI0.VID0", /* 770e */
4437  "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4438  "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4439  "\\_SB.PCI0.AGP.VID", /* all others */
4440  ); /* R30, R31 */
4441 
4442 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4443 
4444 static int __init video_init(struct ibm_init_struct *iibm)
4445 {
4446  int ivga;
4447 
4448  vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4449 
4451  if (tpacpi_is_ibm())
4452  TPACPI_ACPIHANDLE_INIT(vid2);
4453 
4454  if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4455  /* G41, assume IVGA doesn't change */
4456  vid_handle = vid2_handle;
4457 
4458  if (!vid_handle)
4459  /* video switching not supported on R30, R31 */
4460  video_supported = TPACPI_VIDEO_NONE;
4461  else if (tpacpi_is_ibm() &&
4462  acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4463  /* 570 */
4464  video_supported = TPACPI_VIDEO_570;
4465  else if (tpacpi_is_ibm() &&
4466  acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4467  /* 600e/x, 770e, 770x */
4468  video_supported = TPACPI_VIDEO_770;
4469  else
4470  /* all others */
4471  video_supported = TPACPI_VIDEO_NEW;
4472 
4473  vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4474  str_supported(video_supported != TPACPI_VIDEO_NONE),
4475  video_supported);
4476 
4477  return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4478 }
4479 
4480 static void video_exit(void)
4481 {
4483  "restoring original video autoswitch mode\n");
4484  if (video_autosw_set(video_orig_autosw))
4485  pr_err("error while trying to restore original "
4486  "video autoswitch mode\n");
4487 }
4488 
4489 static int video_outputsw_get(void)
4490 {
4491  int status = 0;
4492  int i;
4493 
4494  switch (video_supported) {
4495  case TPACPI_VIDEO_570:
4496  if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4497  TP_ACPI_VIDEO_570_PHSCMD))
4498  return -EIO;
4499  status = i & TP_ACPI_VIDEO_570_PHSMASK;
4500  break;
4501  case TPACPI_VIDEO_770:
4502  if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4503  return -EIO;
4504  if (i)
4505  status |= TP_ACPI_VIDEO_S_LCD;
4506  if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4507  return -EIO;
4508  if (i)
4509  status |= TP_ACPI_VIDEO_S_CRT;
4510  break;
4511  case TPACPI_VIDEO_NEW:
4512  if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4513  !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4514  return -EIO;
4515  if (i)
4516  status |= TP_ACPI_VIDEO_S_CRT;
4517 
4518  if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4519  !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4520  return -EIO;
4521  if (i)
4522  status |= TP_ACPI_VIDEO_S_LCD;
4523  if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4524  return -EIO;
4525  if (i)
4526  status |= TP_ACPI_VIDEO_S_DVI;
4527  break;
4528  default:
4529  return -ENOSYS;
4530  }
4531 
4532  return status;
4533 }
4534 
4535 static int video_outputsw_set(int status)
4536 {
4537  int autosw;
4538  int res = 0;
4539 
4540  switch (video_supported) {
4541  case TPACPI_VIDEO_570:
4542  res = acpi_evalf(NULL, NULL,
4543  "\\_SB.PHS2", "vdd",
4544  TP_ACPI_VIDEO_570_PHS2CMD,
4545  status | TP_ACPI_VIDEO_570_PHS2SET);
4546  break;
4547  case TPACPI_VIDEO_770:
4548  autosw = video_autosw_get();
4549  if (autosw < 0)
4550  return autosw;
4551 
4552  res = video_autosw_set(1);
4553  if (res)
4554  return res;
4555  res = acpi_evalf(vid_handle, NULL,
4556  "ASWT", "vdd", status * 0x100, 0);
4557  if (!autosw && video_autosw_set(autosw)) {
4558  pr_err("video auto-switch left enabled due to error\n");
4559  return -EIO;
4560  }
4561  break;
4562  case TPACPI_VIDEO_NEW:
4563  res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4564  acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4565  break;
4566  default:
4567  return -ENOSYS;
4568  }
4569 
4570  return (res)? 0 : -EIO;
4571 }
4572 
4573 static int video_autosw_get(void)
4574 {
4575  int autosw = 0;
4576 
4577  switch (video_supported) {
4578  case TPACPI_VIDEO_570:
4579  if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4580  return -EIO;
4581  break;
4582  case TPACPI_VIDEO_770:
4583  case TPACPI_VIDEO_NEW:
4584  if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4585  return -EIO;
4586  break;
4587  default:
4588  return -ENOSYS;
4589  }
4590 
4591  return autosw & 1;
4592 }
4593 
4594 static int video_autosw_set(int enable)
4595 {
4596  if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4597  return -EIO;
4598  return 0;
4599 }
4600 
4601 static int video_outputsw_cycle(void)
4602 {
4603  int autosw = video_autosw_get();
4604  int res;
4605 
4606  if (autosw < 0)
4607  return autosw;
4608 
4609  switch (video_supported) {
4610  case TPACPI_VIDEO_570:
4611  res = video_autosw_set(1);
4612  if (res)
4613  return res;
4614  res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4615  break;
4616  case TPACPI_VIDEO_770:
4617  case TPACPI_VIDEO_NEW:
4618  res = video_autosw_set(1);
4619  if (res)
4620  return res;
4621  res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4622  break;
4623  default:
4624  return -ENOSYS;
4625  }
4626  if (!autosw && video_autosw_set(autosw)) {
4627  pr_err("video auto-switch left enabled due to error\n");
4628  return -EIO;
4629  }
4630 
4631  return (res)? 0 : -EIO;
4632 }
4633 
4634 static int video_expand_toggle(void)
4635 {
4636  switch (video_supported) {
4637  case TPACPI_VIDEO_570:
4638  return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4639  0 : -EIO;
4640  case TPACPI_VIDEO_770:
4641  return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4642  0 : -EIO;
4643  case TPACPI_VIDEO_NEW:
4644  return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4645  0 : -EIO;
4646  default:
4647  return -ENOSYS;
4648  }
4649  /* not reached */
4650 }
4651 
4652 static int video_read(struct seq_file *m)
4653 {
4654  int status, autosw;
4655 
4656  if (video_supported == TPACPI_VIDEO_NONE) {
4657  seq_printf(m, "status:\t\tnot supported\n");
4658  return 0;
4659  }
4660 
4661  /* Even reads can crash X.org, so... */
4662  if (!capable(CAP_SYS_ADMIN))
4663  return -EPERM;
4664 
4665  status = video_outputsw_get();
4666  if (status < 0)
4667  return status;
4668 
4669  autosw = video_autosw_get();
4670  if (autosw < 0)
4671  return autosw;
4672 
4673  seq_printf(m, "status:\t\tsupported\n");
4674  seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4675  seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4676  if (video_supported == TPACPI_VIDEO_NEW)
4677  seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4678  seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4679  seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4680  seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4681  if (video_supported == TPACPI_VIDEO_NEW)
4682  seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4683  seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4684  seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4685 
4686  return 0;
4687 }
4688 
4689 static int video_write(char *buf)
4690 {
4691  char *cmd;
4692  int enable, disable, status;
4693  int res;
4694 
4695  if (video_supported == TPACPI_VIDEO_NONE)
4696  return -ENODEV;
4697 
4698  /* Even reads can crash X.org, let alone writes... */
4699  if (!capable(CAP_SYS_ADMIN))
4700  return -EPERM;
4701 
4702  enable = 0;
4703  disable = 0;
4704 
4705  while ((cmd = next_cmd(&buf))) {
4706  if (strlencmp(cmd, "lcd_enable") == 0) {
4707  enable |= TP_ACPI_VIDEO_S_LCD;
4708  } else if (strlencmp(cmd, "lcd_disable") == 0) {
4709  disable |= TP_ACPI_VIDEO_S_LCD;
4710  } else if (strlencmp(cmd, "crt_enable") == 0) {
4711  enable |= TP_ACPI_VIDEO_S_CRT;
4712  } else if (strlencmp(cmd, "crt_disable") == 0) {
4713  disable |= TP_ACPI_VIDEO_S_CRT;
4714  } else if (video_supported == TPACPI_VIDEO_NEW &&
4715  strlencmp(cmd, "dvi_enable") == 0) {
4716  enable |= TP_ACPI_VIDEO_S_DVI;
4717  } else if (video_supported == TPACPI_VIDEO_NEW &&
4718  strlencmp(cmd, "dvi_disable") == 0) {
4719  disable |= TP_ACPI_VIDEO_S_DVI;
4720  } else if (strlencmp(cmd, "auto_enable") == 0) {
4721  res = video_autosw_set(1);
4722  if (res)
4723  return res;
4724  } else if (strlencmp(cmd, "auto_disable") == 0) {
4725  res = video_autosw_set(0);
4726  if (res)
4727  return res;
4728  } else if (strlencmp(cmd, "video_switch") == 0) {
4729  res = video_outputsw_cycle();
4730  if (res)
4731  return res;
4732  } else if (strlencmp(cmd, "expand_toggle") == 0) {
4733  res = video_expand_toggle();
4734  if (res)
4735  return res;
4736  } else
4737  return -EINVAL;
4738  }
4739 
4740  if (enable || disable) {
4741  status = video_outputsw_get();
4742  if (status < 0)
4743  return status;
4744  res = video_outputsw_set((status & ~disable) | enable);
4745  if (res)
4746  return res;
4747  }
4748 
4749  return 0;
4750 }
4751 
4752 static struct ibm_struct video_driver_data = {
4753  .name = "video",
4754  .read = video_read,
4755  .write = video_write,
4756  .exit = video_exit,
4757 };
4758 
4759 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4760 
4761 /*************************************************************************
4762  * Light (thinklight) subdriver
4763  */
4764 
4765 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4766 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4767 
4768 static int light_get_status(void)
4769 {
4770  int status = 0;
4771 
4772  if (tp_features.light_status) {
4773  if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4774  return -EIO;
4775  return (!!status);
4776  }
4777 
4778  return -ENXIO;
4779 }
4780 
4781 static int light_set_status(int status)
4782 {
4783  int rc;
4784 
4785  if (tp_features.light) {
4786  if (cmos_handle) {
4787  rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4788  (status)?
4791  } else {
4792  rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4793  (status)? 1 : 0);
4794  }
4795  return (rc)? 0 : -EIO;
4796  }
4797 
4798  return -ENXIO;
4799 }
4800 
4801 static void light_set_status_worker(struct work_struct *work)
4802 {
4803  struct tpacpi_led_classdev *data =
4804  container_of(work, struct tpacpi_led_classdev, work);
4805 
4806  if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4807  light_set_status((data->new_state != TPACPI_LED_OFF));
4808 }
4809 
4810 static void light_sysfs_set(struct led_classdev *led_cdev,
4812 {
4813  struct tpacpi_led_classdev *data =
4814  container_of(led_cdev,
4815  struct tpacpi_led_classdev,
4816  led_classdev);
4817  data->new_state = (brightness != LED_OFF) ?
4819  queue_work(tpacpi_wq, &data->work);
4820 }
4821 
4822 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4823 {
4824  return (light_get_status() == 1)? LED_FULL : LED_OFF;
4825 }
4826 
4827 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4828  .led_classdev = {
4829  .name = "tpacpi::thinklight",
4830  .brightness_set = &light_sysfs_set,
4831  .brightness_get = &light_sysfs_get,
4832  }
4833 };
4834 
4835 static int __init light_init(struct ibm_init_struct *iibm)
4836 {
4837  int rc;
4838 
4839  vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4840 
4841  if (tpacpi_is_ibm()) {
4842  TPACPI_ACPIHANDLE_INIT(ledb);
4843  TPACPI_ACPIHANDLE_INIT(lght);
4844  }
4845  TPACPI_ACPIHANDLE_INIT(cmos);
4846  INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4847 
4848  /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4849  tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4850 
4851  if (tp_features.light)
4852  /* light status not supported on
4853  570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4854  tp_features.light_status =
4855  acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4856 
4857  vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4858  str_supported(tp_features.light),
4859  str_supported(tp_features.light_status));
4860 
4861  if (!tp_features.light)
4862  return 1;
4863 
4864  rc = led_classdev_register(&tpacpi_pdev->dev,
4865  &tpacpi_led_thinklight.led_classdev);
4866 
4867  if (rc < 0) {
4868  tp_features.light = 0;
4869  tp_features.light_status = 0;
4870  } else {
4871  rc = 0;
4872  }
4873 
4874  return rc;
4875 }
4876 
4877 static void light_exit(void)
4878 {
4879  led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4880  if (work_pending(&tpacpi_led_thinklight.work))
4881  flush_workqueue(tpacpi_wq);
4882 }
4883 
4884 static int light_read(struct seq_file *m)
4885 {
4886  int status;
4887 
4888  if (!tp_features.light) {
4889  seq_printf(m, "status:\t\tnot supported\n");
4890  } else if (!tp_features.light_status) {
4891  seq_printf(m, "status:\t\tunknown\n");
4892  seq_printf(m, "commands:\ton, off\n");
4893  } else {
4894  status = light_get_status();
4895  if (status < 0)
4896  return status;
4897  seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4898  seq_printf(m, "commands:\ton, off\n");
4899  }
4900 
4901  return 0;
4902 }
4903 
4904 static int light_write(char *buf)
4905 {
4906  char *cmd;
4907  int newstatus = 0;
4908 
4909  if (!tp_features.light)
4910  return -ENODEV;
4911 
4912  while ((cmd = next_cmd(&buf))) {
4913  if (strlencmp(cmd, "on") == 0) {
4914  newstatus = 1;
4915  } else if (strlencmp(cmd, "off") == 0) {
4916  newstatus = 0;
4917  } else
4918  return -EINVAL;
4919  }
4920 
4921  return light_set_status(newstatus);
4922 }
4923 
4924 static struct ibm_struct light_driver_data = {
4925  .name = "light",
4926  .read = light_read,
4927  .write = light_write,
4928  .exit = light_exit,
4929 };
4930 
4931 /*************************************************************************
4932  * CMOS subdriver
4933  */
4934 
4935 /* sysfs cmos_command -------------------------------------------------- */
4936 static ssize_t cmos_command_store(struct device *dev,
4937  struct device_attribute *attr,
4938  const char *buf, size_t count)
4939 {
4940  unsigned long cmos_cmd;
4941  int res;
4942 
4943  if (parse_strtoul(buf, 21, &cmos_cmd))
4944  return -EINVAL;
4945 
4946  res = issue_thinkpad_cmos_command(cmos_cmd);
4947  return (res)? res : count;
4948 }
4949 
4950 static struct device_attribute dev_attr_cmos_command =
4951  __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4952 
4953 /* --------------------------------------------------------------------- */
4954 
4955 static int __init cmos_init(struct ibm_init_struct *iibm)
4956 {
4957  int res;
4958 
4960  "initializing cmos commands subdriver\n");
4961 
4962  TPACPI_ACPIHANDLE_INIT(cmos);
4963 
4964  vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4965  str_supported(cmos_handle != NULL));
4966 
4967  res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4968  if (res)
4969  return res;
4970 
4971  return (cmos_handle)? 0 : 1;
4972 }
4973 
4974 static void cmos_exit(void)
4975 {
4976  device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4977 }
4978 
4979 static int cmos_read(struct seq_file *m)
4980 {
4981  /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4982  R30, R31, T20-22, X20-21 */
4983  if (!cmos_handle)
4984  seq_printf(m, "status:\t\tnot supported\n");
4985  else {
4986  seq_printf(m, "status:\t\tsupported\n");
4987  seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4988  }
4989 
4990  return 0;
4991 }
4992 
4993 static int cmos_write(char *buf)
4994 {
4995  char *cmd;
4996  int cmos_cmd, res;
4997 
4998  while ((cmd = next_cmd(&buf))) {
4999  if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5000  cmos_cmd >= 0 && cmos_cmd <= 21) {
5001  /* cmos_cmd set */
5002  } else
5003  return -EINVAL;
5004 
5005  res = issue_thinkpad_cmos_command(cmos_cmd);
5006  if (res)
5007  return res;
5008  }
5009 
5010  return 0;
5011 }
5012 
5013 static struct ibm_struct cmos_driver_data = {
5014  .name = "cmos",
5015  .read = cmos_read,
5016  .write = cmos_write,
5017  .exit = cmos_exit,
5018 };
5019 
5020 /*************************************************************************
5021  * LED subdriver
5022  */
5023 
5026  TPACPI_LED_570, /* 570 */
5027  TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5028  TPACPI_LED_NEW, /* all others */
5029 };
5030 
5031 enum { /* For TPACPI_LED_OLD */
5032  TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5033  TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5034  TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5035 };
5036 
5037 static enum led_access_mode led_supported;
5038 
5039 static acpi_handle led_handle;
5040 
5041 #define TPACPI_LED_NUMLEDS 16
5042 static struct tpacpi_led_classdev *tpacpi_leds;
5043 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5044 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5045  /* there's a limit of 19 chars + NULL before 2.6.26 */
5046  "tpacpi::power",
5047  "tpacpi:orange:batt",
5048  "tpacpi:green:batt",
5049  "tpacpi::dock_active",
5050  "tpacpi::bay_active",
5051  "tpacpi::dock_batt",
5052  "tpacpi::unknown_led",
5053  "tpacpi::standby",
5054  "tpacpi::dock_status1",
5055  "tpacpi::dock_status2",
5056  "tpacpi::unknown_led2",
5057  "tpacpi::unknown_led3",
5058  "tpacpi::thinkvantage",
5059 };
5060 #define TPACPI_SAFE_LEDS 0x1081U
5061 
5062 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5063 {
5064 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5065  return false;
5066 #else
5067  return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5068 #endif
5069 }
5070 
5071 static int led_get_status(const unsigned int led)
5072 {
5073  int status;
5074  enum led_status_t led_s;
5075 
5076  switch (led_supported) {
5077  case TPACPI_LED_570:
5078  if (!acpi_evalf(ec_handle,
5079  &status, "GLED", "dd", 1 << led))
5080  return -EIO;
5081  led_s = (status == 0)?
5082  TPACPI_LED_OFF :
5083  ((status == 1)?
5084  TPACPI_LED_ON :
5086  tpacpi_led_state_cache[led] = led_s;
5087  return led_s;
5088  default:
5089  return -ENXIO;
5090  }
5091 
5092  /* not reached */
5093 }
5094 
5095 static int led_set_status(const unsigned int led,
5096  const enum led_status_t ledstatus)
5097 {
5098  /* off, on, blink. Index is led_status_t */
5099  static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5100  static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5101 
5102  int rc = 0;
5103 
5104  switch (led_supported) {
5105  case TPACPI_LED_570:
5106  /* 570 */
5107  if (unlikely(led > 7))
5108  return -EINVAL;
5109  if (unlikely(tpacpi_is_led_restricted(led)))
5110  return -EPERM;
5111  if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5112  (1 << led), led_sled_arg1[ledstatus]))
5113  rc = -EIO;
5114  break;
5115  case TPACPI_LED_OLD:
5116  /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5117  if (unlikely(led > 7))
5118  return -EINVAL;
5119  if (unlikely(tpacpi_is_led_restricted(led)))
5120  return -EPERM;
5121  rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5122  if (rc >= 0)
5124  (ledstatus == TPACPI_LED_BLINK) << led);
5125  if (rc >= 0)
5127  (ledstatus != TPACPI_LED_OFF) << led);
5128  break;
5129  case TPACPI_LED_NEW:
5130  /* all others */
5131  if (unlikely(led >= TPACPI_LED_NUMLEDS))
5132  return -EINVAL;
5133  if (unlikely(tpacpi_is_led_restricted(led)))
5134  return -EPERM;
5135  if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5136  led, led_led_arg1[ledstatus]))
5137  rc = -EIO;
5138  break;
5139  default:
5140  rc = -ENXIO;
5141  }
5142 
5143  if (!rc)
5144  tpacpi_led_state_cache[led] = ledstatus;
5145 
5146  return rc;
5147 }
5148 
5149 static void led_set_status_worker(struct work_struct *work)
5150 {
5151  struct tpacpi_led_classdev *data =
5152  container_of(work, struct tpacpi_led_classdev, work);
5153 
5154  if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5155  led_set_status(data->led, data->new_state);
5156 }
5157 
5158 static void led_sysfs_set(struct led_classdev *led_cdev,
5160 {
5161  struct tpacpi_led_classdev *data = container_of(led_cdev,
5163 
5164  if (brightness == LED_OFF)
5165  data->new_state = TPACPI_LED_OFF;
5166  else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5167  data->new_state = TPACPI_LED_ON;
5168  else
5169  data->new_state = TPACPI_LED_BLINK;
5170 
5171  queue_work(tpacpi_wq, &data->work);
5172 }
5173 
5174 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5175  unsigned long *delay_on, unsigned long *delay_off)
5176 {
5177  struct tpacpi_led_classdev *data = container_of(led_cdev,
5179 
5180  /* Can we choose the flash rate? */
5181  if (*delay_on == 0 && *delay_off == 0) {
5182  /* yes. set them to the hardware blink rate (1 Hz) */
5183  *delay_on = 500; /* ms */
5184  *delay_off = 500; /* ms */
5185  } else if ((*delay_on != 500) || (*delay_off != 500))
5186  return -EINVAL;
5187 
5188  data->new_state = TPACPI_LED_BLINK;
5189  queue_work(tpacpi_wq, &data->work);
5190 
5191  return 0;
5192 }
5193 
5194 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5195 {
5196  int rc;
5197 
5198  struct tpacpi_led_classdev *data = container_of(led_cdev,
5200 
5201  rc = led_get_status(data->led);
5202 
5203  if (rc == TPACPI_LED_OFF || rc < 0)
5204  rc = LED_OFF; /* no error handling in led class :( */
5205  else
5206  rc = LED_FULL;
5207 
5208  return rc;
5209 }
5210 
5211 static void led_exit(void)
5212 {
5213  unsigned int i;
5214 
5215  for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5216  if (tpacpi_leds[i].led_classdev.name)
5217  led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5218  }
5219 
5220  flush_workqueue(tpacpi_wq);
5221  kfree(tpacpi_leds);
5222 }
5223 
5224 static int __init tpacpi_init_led(unsigned int led)
5225 {
5226  int rc;
5227 
5228  tpacpi_leds[led].led = led;
5229 
5230  /* LEDs with no name don't get registered */
5231  if (!tpacpi_led_names[led])
5232  return 0;
5233 
5234  tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5235  tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5236  if (led_supported == TPACPI_LED_570)
5237  tpacpi_leds[led].led_classdev.brightness_get =
5238  &led_sysfs_get;
5239 
5240  tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5241 
5242  INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5243 
5244  rc = led_classdev_register(&tpacpi_pdev->dev,
5245  &tpacpi_leds[led].led_classdev);
5246  if (rc < 0)
5247  tpacpi_leds[led].led_classdev.name = NULL;
5248 
5249  return rc;
5250 }
5251 
5252 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5253  TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5254  TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5255  TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5256 
5257  TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5258  TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5259  TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5260  TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5261  TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5262  TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5263  TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5264  TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5265 
5266  TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5267  TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5268  TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5269  TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5270  TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5271 
5272  TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5273  TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5274  TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5275  TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5276 
5277  /* (1) - may have excess leds enabled on MSB */
5278 
5279  /* Defaults (order matters, keep last, don't reorder!) */
5280  { /* Lenovo */
5281  .vendor = PCI_VENDOR_ID_LENOVO,
5282  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5283  .quirks = 0x1fffU,
5284  },
5285  { /* IBM ThinkPads with no EC version string */
5286  .vendor = PCI_VENDOR_ID_IBM,
5287  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5288  .quirks = 0x00ffU,
5289  },
5290  { /* IBM ThinkPads with EC version string */
5291  .vendor = PCI_VENDOR_ID_IBM,
5292  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5293  .quirks = 0x00bfU,
5294  },
5295 };
5296 
5297 #undef TPACPI_LEDQ_IBM
5298 #undef TPACPI_LEDQ_LNV
5299 
5300 static enum led_access_mode __init led_init_detect_mode(void)
5301 {
5303 
5304  if (tpacpi_is_ibm()) {
5305  /* 570 */
5306  status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5307  if (ACPI_SUCCESS(status))
5308  return TPACPI_LED_570;
5309 
5310  /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5311  status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5312  if (ACPI_SUCCESS(status))
5313  return TPACPI_LED_OLD;
5314  }
5315 
5316  /* most others */
5317  status = acpi_get_handle(ec_handle, "LED", &led_handle);
5318  if (ACPI_SUCCESS(status))
5319  return TPACPI_LED_NEW;
5320 
5321  /* R30, R31, and unknown firmwares */
5322  led_handle = NULL;
5323  return TPACPI_LED_NONE;
5324 }
5325 
5326 static int __init led_init(struct ibm_init_struct *iibm)
5327 {
5328  unsigned int i;
5329  int rc;
5330  unsigned long useful_leds;
5331 
5332  vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5333 
5334  led_supported = led_init_detect_mode();
5335 
5336  vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5337  str_supported(led_supported), led_supported);
5338 
5339  if (led_supported == TPACPI_LED_NONE)
5340  return 1;
5341 
5342  tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5343  GFP_KERNEL);
5344  if (!tpacpi_leds) {
5345  pr_err("Out of memory for LED data\n");
5346  return -ENOMEM;
5347  }
5348 
5349  useful_leds = tpacpi_check_quirks(led_useful_qtable,
5350  ARRAY_SIZE(led_useful_qtable));
5351 
5352  for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5353  if (!tpacpi_is_led_restricted(i) &&
5354  test_bit(i, &useful_leds)) {
5355  rc = tpacpi_init_led(i);
5356  if (rc < 0) {
5357  led_exit();
5358  return rc;
5359  }
5360  }
5361  }
5362 
5363 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5364  pr_notice("warning: userspace override of important "
5365  "firmware LEDs is enabled\n");
5366 #endif
5367  return 0;
5368 }
5369 
5370 #define str_led_status(s) \
5371  ((s) == TPACPI_LED_OFF ? "off" : \
5372  ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5373 
5374 static int led_read(struct seq_file *m)
5375 {
5376  if (!led_supported) {
5377  seq_printf(m, "status:\t\tnot supported\n");
5378  return 0;
5379  }
5380  seq_printf(m, "status:\t\tsupported\n");
5381 
5382  if (led_supported == TPACPI_LED_570) {
5383  /* 570 */
5384  int i, status;
5385  for (i = 0; i < 8; i++) {
5386  status = led_get_status(i);
5387  if (status < 0)
5388  return -EIO;
5389  seq_printf(m, "%d:\t\t%s\n",
5390  i, str_led_status(status));
5391  }
5392  }
5393 
5394  seq_printf(m, "commands:\t"
5395  "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5396 
5397  return 0;
5398 }
5399 
5400 static int led_write(char *buf)
5401 {
5402  char *cmd;
5403  int led, rc;
5404  enum led_status_t s;
5405 
5406  if (!led_supported)
5407  return -ENODEV;
5408 
5409  while ((cmd = next_cmd(&buf))) {
5410  if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5411  return -EINVAL;
5412 
5413  if (strstr(cmd, "off")) {
5414  s = TPACPI_LED_OFF;
5415  } else if (strstr(cmd, "on")) {
5416  s = TPACPI_LED_ON;
5417  } else if (strstr(cmd, "blink")) {
5418  s = TPACPI_LED_BLINK;
5419  } else {
5420  return -EINVAL;
5421  }
5422 
5423  rc = led_set_status(led, s);
5424  if (rc < 0)
5425  return rc;
5426  }
5427 
5428  return 0;
5429 }
5430 
5431 static struct ibm_struct led_driver_data = {
5432  .name = "led",
5433  .read = led_read,
5434  .write = led_write,
5435  .exit = led_exit,
5436 };
5437 
5438 /*************************************************************************
5439  * Beep subdriver
5440  */
5441 
5442 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5443 
5444 #define TPACPI_BEEP_Q1 0x0001
5445 
5446 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5447  TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5448  TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5449 };
5450 
5451 static int __init beep_init(struct ibm_init_struct *iibm)
5452 {
5453  unsigned long quirks;
5454 
5455  vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5456 
5457  TPACPI_ACPIHANDLE_INIT(beep);
5458 
5459  vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5460  str_supported(beep_handle != NULL));
5461 
5462  quirks = tpacpi_check_quirks(beep_quirk_table,
5463  ARRAY_SIZE(beep_quirk_table));
5464 
5465  tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5466 
5467  return (beep_handle)? 0 : 1;
5468 }
5469 
5470 static int beep_read(struct seq_file *m)
5471 {
5472  if (!beep_handle)
5473  seq_printf(m, "status:\t\tnot supported\n");
5474  else {
5475  seq_printf(m, "status:\t\tsupported\n");
5476  seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5477  }
5478 
5479  return 0;
5480 }
5481 
5482 static int beep_write(char *buf)
5483 {
5484  char *cmd;
5485  int beep_cmd;
5486 
5487  if (!beep_handle)
5488  return -ENODEV;
5489 
5490  while ((cmd = next_cmd(&buf))) {
5491  if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5492  beep_cmd >= 0 && beep_cmd <= 17) {
5493  /* beep_cmd set */
5494  } else
5495  return -EINVAL;
5496  if (tp_features.beep_needs_two_args) {
5497  if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5498  beep_cmd, 0))
5499  return -EIO;
5500  } else {
5501  if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5502  beep_cmd))
5503  return -EIO;
5504  }
5505  }
5506 
5507  return 0;
5508 }
5509 
5510 static struct ibm_struct beep_driver_data = {
5511  .name = "beep",
5512  .read = beep_read,
5513  .write = beep_write,
5514 };
5515 
5516 /*************************************************************************
5517  * Thermal subdriver
5518  */
5519 
5521  TPACPI_THERMAL_NONE = 0, /* No thermal support */
5522  TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5523  TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5524  TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5525  TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5526 };
5527 
5528 enum { /* TPACPI_THERMAL_TPEC_* */
5529  TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5530  TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5531  TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5532 
5533  TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5534 };
5535 
5536 
5537 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5540 };
5541 
5542 static enum thermal_access_mode thermal_read_mode;
5543 
5544 /* idx is zero-based */
5545 static int thermal_get_sensor(int idx, s32 *value)
5546 {
5547  int t;
5548  s8 tmp;
5549  char tmpi[5];
5550 
5551  t = TP_EC_THERMAL_TMP0;
5552 
5553  switch (thermal_read_mode) {
5554 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5556  if (idx >= 8 && idx <= 15) {
5557  t = TP_EC_THERMAL_TMP8;
5558  idx -= 8;
5559  }
5560  /* fallthrough */
5561 #endif
5562  case TPACPI_THERMAL_TPEC_8:
5563  if (idx <= 7) {
5564  if (!acpi_ec_read(t + idx, &tmp))
5565  return -EIO;
5566  *value = tmp * 1000;
5567  return 0;
5568  }
5569  break;
5570 
5572  if (idx <= 7) {
5573  snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5574  if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5575  return -EIO;
5576  if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5577  return -EIO;
5578  *value = (t - 2732) * 100;
5579  return 0;
5580  }
5581  break;
5582 
5584  if (idx <= 7) {
5585  snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5586  if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5587  return -EIO;
5588  if (t > 127 || t < -127)
5590  *value = t * 1000;
5591  return 0;
5592  }
5593  break;
5594 
5595  case TPACPI_THERMAL_NONE:
5596  default:
5597  return -ENOSYS;
5598  }
5599 
5600  return -EINVAL;
5601 }
5602 
5603 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5604 {
5605  int res, i;
5606  int n;
5607 
5608  n = 8;
5609  i = 0;
5610 
5611  if (!s)
5612  return -EINVAL;
5613 
5614  if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5615  n = 16;
5616 
5617  for (i = 0 ; i < n; i++) {
5618  res = thermal_get_sensor(i, &s->temp[i]);
5619  if (res)
5620  return res;
5621  }
5622 
5623  return n;
5624 }
5625 
5626 static void thermal_dump_all_sensors(void)
5627 {
5628  int n, i;
5629  struct ibm_thermal_sensors_struct t;
5630 
5631  n = thermal_get_sensors(&t);
5632  if (n <= 0)
5633  return;
5634 
5635  pr_notice("temperatures (Celsius):");
5636 
5637  for (i = 0; i < n; i++) {
5638  if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5639  pr_cont(" %d", (int)(t.temp[i] / 1000));
5640  else
5641  pr_cont(" N/A");
5642  }
5643 
5644  pr_cont("\n");
5645 }
5646 
5647 /* sysfs temp##_input -------------------------------------------------- */
5648 
5649 static ssize_t thermal_temp_input_show(struct device *dev,
5650  struct device_attribute *attr,
5651  char *buf)
5652 {
5653  struct sensor_device_attribute *sensor_attr =
5654  to_sensor_dev_attr(attr);
5655  int idx = sensor_attr->index;
5656  s32 value;
5657  int res;
5658 
5659  res = thermal_get_sensor(idx, &value);
5660  if (res)
5661  return res;
5662  if (value == TPACPI_THERMAL_SENSOR_NA)
5663  return -ENXIO;
5664 
5665  return snprintf(buf, PAGE_SIZE, "%d\n", value);
5666 }
5667 
5668 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5669  SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5670  thermal_temp_input_show, NULL, _idxB)
5671 
5672 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5682  THERMAL_SENSOR_ATTR_TEMP(10, 9),
5683