thinkpad_acpi.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * thinkpad_acpi.h - ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #ifndef __THINKPAD_ACPI_H__
  24. #define __THINKPAD_ACPI_H__
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/string.h>
  30. #include <linux/list.h>
  31. #include <linux/mutex.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/backlight.h>
  35. #include <linux/fb.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/hwmon.h>
  38. #include <linux/hwmon-sysfs.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/dmi.h>
  41. #include <linux/jiffies.h>
  42. #include <linux/workqueue.h>
  43. #include <acpi/acpi_drivers.h>
  44. #include <acpi/acnamesp.h>
  45. /****************************************************************************
  46. * Main driver
  47. */
  48. #define IBM_NAME "thinkpad"
  49. #define IBM_DESC "ThinkPad ACPI Extras"
  50. #define IBM_FILE "thinkpad_acpi"
  51. #define IBM_URL "http://ibm-acpi.sf.net/"
  52. #define IBM_MAIL "ibm-acpi-devel@lists.sourceforge.net"
  53. #define IBM_PROC_DIR "ibm"
  54. #define IBM_ACPI_EVENT_PREFIX "ibm"
  55. #define IBM_DRVR_NAME IBM_FILE
  56. #define IBM_LOG IBM_FILE ": "
  57. #define IBM_ERR KERN_ERR IBM_LOG
  58. #define IBM_NOTICE KERN_NOTICE IBM_LOG
  59. #define IBM_INFO KERN_INFO IBM_LOG
  60. #define IBM_DEBUG KERN_DEBUG IBM_LOG
  61. #define IBM_MAX_ACPI_ARGS 3
  62. /* ThinkPad CMOS commands */
  63. #define TP_CMOS_VOLUME_DOWN 0
  64. #define TP_CMOS_VOLUME_UP 1
  65. #define TP_CMOS_VOLUME_MUTE 2
  66. #define TP_CMOS_BRIGHTNESS_UP 4
  67. #define TP_CMOS_BRIGHTNESS_DOWN 5
  68. #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
  69. #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  70. #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
  71. /* Debugging */
  72. #define TPACPI_DBG_ALL 0xffff
  73. #define TPACPI_DBG_ALL 0xffff
  74. #define TPACPI_DBG_INIT 0x0001
  75. #define TPACPI_DBG_EXIT 0x0002
  76. #define dbg_printk(a_dbg_level, format, arg...) \
  77. do { if (dbg_level & a_dbg_level) \
  78. printk(IBM_DEBUG "%s: " format, __func__ , ## arg); } while (0)
  79. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  80. #define vdbg_printk(a_dbg_level, format, arg...) \
  81. dbg_printk(a_dbg_level, format, ## arg)
  82. static const char *str_supported(int is_supported);
  83. #else
  84. #define vdbg_printk(a_dbg_level, format, arg...)
  85. #endif
  86. /* ACPI HIDs */
  87. #define IBM_HKEY_HID "IBM0068"
  88. #define IBM_PCI_HID "PNP0A03"
  89. /* ACPI helpers */
  90. static int __must_check acpi_evalf(acpi_handle handle,
  91. void *res, char *method, char *fmt, ...);
  92. static int __must_check acpi_ec_read(int i, u8 * p);
  93. static int __must_check acpi_ec_write(int i, u8 v);
  94. static int __must_check _sta(acpi_handle handle);
  95. /* ACPI handles */
  96. static acpi_handle root_handle; /* root namespace */
  97. static acpi_handle ec_handle; /* EC */
  98. static acpi_handle ecrd_handle, ecwr_handle; /* 570 EC access */
  99. static acpi_handle cmos_handle, hkey_handle; /* basic thinkpad handles */
  100. static void drv_acpi_handle_init(char *name,
  101. acpi_handle *handle, acpi_handle parent,
  102. char **paths, int num_paths, char **path);
  103. #define IBM_ACPIHANDLE_INIT(object) \
  104. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  105. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  106. /* ThinkPad ACPI helpers */
  107. static int issue_thinkpad_cmos_command(int cmos_cmd);
  108. /* procfs support */
  109. static struct proc_dir_entry *proc_dir;
  110. /* procfs helpers */
  111. static int dispatch_procfs_read(char *page, char **start, off_t off,
  112. int count, int *eof, void *data);
  113. static int dispatch_procfs_write(struct file *file,
  114. const char __user * userbuf,
  115. unsigned long count, void *data);
  116. static char *next_cmd(char **cmds);
  117. /* sysfs support */
  118. struct attribute_set {
  119. unsigned int members, max_members;
  120. struct attribute_group group;
  121. };
  122. static struct attribute_set *create_attr_set(unsigned int max_members,
  123. const char* name);
  124. #define destroy_attr_set(_set) \
  125. kfree(_set);
  126. static int add_to_attr_set(struct attribute_set* s, struct attribute *attr);
  127. static int add_many_to_attr_set(struct attribute_set* s,
  128. struct attribute **attr,
  129. unsigned int count);
  130. #define register_attr_set_with_sysfs(_attr_set, _kobj) \
  131. sysfs_create_group(_kobj, &_attr_set->group)
  132. static void delete_attr_set(struct attribute_set* s, struct kobject *kobj);
  133. static int parse_strtoul(const char *buf, unsigned long max,
  134. unsigned long *value);
  135. /* Device model */
  136. static struct platform_device *tpacpi_pdev;
  137. static struct class_device *tpacpi_hwmon;
  138. static struct platform_driver tpacpi_pdriver;
  139. static int tpacpi_create_driver_attributes(struct device_driver *drv);
  140. static void tpacpi_remove_driver_attributes(struct device_driver *drv);
  141. /* Module */
  142. static int experimental;
  143. static u32 dbg_level;
  144. static int force_load;
  145. static char *ibm_thinkpad_ec_found;
  146. static char* check_dmi_for_ec(void);
  147. static int thinkpad_acpi_module_init(void);
  148. static void thinkpad_acpi_module_exit(void);
  149. /****************************************************************************
  150. * Subdrivers
  151. */
  152. struct ibm_struct;
  153. struct tp_acpi_drv_struct {
  154. char *hid;
  155. struct acpi_driver *driver;
  156. void (*notify) (struct ibm_struct *, u32);
  157. acpi_handle *handle;
  158. u32 type;
  159. struct acpi_device *device;
  160. };
  161. struct ibm_struct {
  162. char *name;
  163. int (*read) (char *);
  164. int (*write) (char *);
  165. void (*exit) (void);
  166. struct list_head all_drivers;
  167. struct tp_acpi_drv_struct *acpi;
  168. struct {
  169. u8 acpi_driver_registered:1;
  170. u8 acpi_notify_installed:1;
  171. u8 proc_created:1;
  172. u8 init_called:1;
  173. u8 experimental:1;
  174. } flags;
  175. };
  176. struct ibm_init_struct {
  177. char param[32];
  178. int (*init) (struct ibm_init_struct *);
  179. struct ibm_struct *data;
  180. };
  181. static struct {
  182. #ifdef CONFIG_THINKPAD_ACPI_BAY
  183. u16 bay_status:1;
  184. u16 bay_eject:1;
  185. u16 bay_status2:1;
  186. u16 bay_eject2:1;
  187. #endif
  188. u16 bluetooth:1;
  189. u16 hotkey:1;
  190. u16 hotkey_mask:1;
  191. u16 light:1;
  192. u16 light_status:1;
  193. u16 wan:1;
  194. u16 fan_ctrl_status_undef:1;
  195. } tp_features;
  196. static struct list_head tpacpi_all_drivers;
  197. static struct ibm_init_struct ibms_init[];
  198. static int set_ibm_param(const char *val, struct kernel_param *kp);
  199. static int ibm_init(struct ibm_init_struct *iibm);
  200. static void ibm_exit(struct ibm_struct *ibm);
  201. /*
  202. * procfs master subdriver
  203. */
  204. static int thinkpad_acpi_driver_init(struct ibm_init_struct *iibm);
  205. static int thinkpad_acpi_driver_read(char *p);
  206. /*
  207. * Bay subdriver
  208. */
  209. #ifdef CONFIG_THINKPAD_ACPI_BAY
  210. static acpi_handle bay_handle, bay_ej_handle;
  211. static acpi_handle bay2_handle, bay2_ej_handle;
  212. static int bay_init(struct ibm_init_struct *iibm);
  213. static void bay_notify(struct ibm_struct *ibm, u32 event);
  214. static int bay_read(char *p);
  215. static int bay_write(char *buf);
  216. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  217. /*
  218. * Beep subdriver
  219. */
  220. static acpi_handle beep_handle;
  221. static int beep_read(char *p);
  222. static int beep_write(char *buf);
  223. /*
  224. * Bluetooth subdriver
  225. */
  226. #define TPACPI_BLUETH_SYSFS_GROUP "bluetooth"
  227. enum {
  228. /* ACPI GBDC/SBDC bits */
  229. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  230. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  231. TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
  232. };
  233. static int bluetooth_init(struct ibm_init_struct *iibm);
  234. static int bluetooth_get_radiosw(void);
  235. static int bluetooth_set_radiosw(int radio_on);
  236. static int bluetooth_read(char *p);
  237. static int bluetooth_write(char *buf);
  238. /*
  239. * Brightness (backlight) subdriver
  240. */
  241. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  242. static struct backlight_device *ibm_backlight_device;
  243. static int brightness_offset = 0x31;
  244. static int brightness_init(struct ibm_init_struct *iibm);
  245. static void brightness_exit(void);
  246. static int brightness_get(struct backlight_device *bd);
  247. static int brightness_set(int value);
  248. static int brightness_update_status(struct backlight_device *bd);
  249. static int brightness_read(char *p);
  250. static int brightness_write(char *buf);
  251. /*
  252. * CMOS subdriver
  253. */
  254. static int cmos_read(char *p);
  255. static int cmos_write(char *buf);
  256. /*
  257. * Dock subdriver
  258. */
  259. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  260. static acpi_handle pci_handle;
  261. static acpi_handle dock_handle;
  262. static void dock_notify(struct ibm_struct *ibm, u32 event);
  263. static int dock_read(char *p);
  264. static int dock_write(char *buf);
  265. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  266. /*
  267. * EC dump subdriver
  268. */
  269. static int ecdump_read(char *p) ;
  270. static int ecdump_write(char *buf);
  271. /*
  272. * Fan subdriver
  273. */
  274. enum { /* Fan control constants */
  275. fan_status_offset = 0x2f, /* EC register 0x2f */
  276. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  277. * 0x84 must be read before 0x85 */
  278. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  279. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  280. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  281. };
  282. enum fan_status_access_mode {
  283. TPACPI_FAN_NONE = 0, /* No fan status or control */
  284. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  285. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  286. };
  287. enum fan_control_access_mode {
  288. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  289. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  290. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  291. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  292. };
  293. enum fan_control_commands {
  294. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  295. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  296. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  297. * and also watchdog cmd */
  298. };
  299. static int fan_control_allowed;
  300. static enum fan_status_access_mode fan_status_access_mode;
  301. static enum fan_control_access_mode fan_control_access_mode;
  302. static enum fan_control_commands fan_control_commands;
  303. static u8 fan_control_initial_status;
  304. static u8 fan_control_desired_level;
  305. static int fan_watchdog_maxinterval;
  306. static struct mutex fan_mutex;
  307. static acpi_handle fans_handle, gfan_handle, sfan_handle;
  308. static int fan_init(struct ibm_init_struct *iibm);
  309. static void fan_exit(void);
  310. static int fan_get_status(u8 *status);
  311. static int fan_get_status_safe(u8 *status);
  312. static int fan_get_speed(unsigned int *speed);
  313. static void fan_update_desired_level(u8 status);
  314. static void fan_watchdog_fire(struct work_struct *ignored);
  315. static void fan_watchdog_reset(void);
  316. static int fan_set_level(int level);
  317. static int fan_set_level_safe(int level);
  318. static int fan_set_enable(void);
  319. static int fan_set_disable(void);
  320. static int fan_set_speed(int speed);
  321. static int fan_read(char *p);
  322. static int fan_write(char *buf);
  323. static int fan_write_cmd_level(const char *cmd, int *rc);
  324. static int fan_write_cmd_enable(const char *cmd, int *rc);
  325. static int fan_write_cmd_disable(const char *cmd, int *rc);
  326. static int fan_write_cmd_speed(const char *cmd, int *rc);
  327. static int fan_write_cmd_watchdog(const char *cmd, int *rc);
  328. /*
  329. * Hotkey subdriver
  330. */
  331. #define TPACPI_HOTKEY_SYSFS_GROUP "hotkey"
  332. static int hotkey_orig_status;
  333. static int hotkey_orig_mask;
  334. static struct mutex hotkey_mutex;
  335. static int hotkey_init(struct ibm_init_struct *iibm);
  336. static void hotkey_exit(void);
  337. static int hotkey_get(int *status, int *mask);
  338. static int hotkey_set(int status, int mask);
  339. static void hotkey_notify(struct ibm_struct *ibm, u32 event);
  340. static int hotkey_read(char *p);
  341. static int hotkey_write(char *buf);
  342. /*
  343. * LED subdriver
  344. */
  345. enum led_access_mode {
  346. TPACPI_LED_NONE = 0,
  347. TPACPI_LED_570, /* 570 */
  348. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  349. TPACPI_LED_NEW, /* all others */
  350. };
  351. enum { /* For TPACPI_LED_OLD */
  352. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  353. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  354. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  355. };
  356. static enum led_access_mode led_supported;
  357. static acpi_handle led_handle;
  358. static int led_init(struct ibm_init_struct *iibm);
  359. static int led_read(char *p);
  360. static int led_write(char *buf);
  361. /*
  362. * Light (thinklight) subdriver
  363. */
  364. static acpi_handle lght_handle, ledb_handle;
  365. static int light_init(struct ibm_init_struct *iibm);
  366. static int light_read(char *p);
  367. static int light_write(char *buf);
  368. /*
  369. * Thermal subdriver
  370. */
  371. enum thermal_access_mode {
  372. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  373. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  374. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  375. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  376. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  377. };
  378. enum { /* TPACPI_THERMAL_TPEC_* */
  379. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  380. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  381. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  382. };
  383. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  384. struct ibm_thermal_sensors_struct {
  385. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  386. };
  387. static enum thermal_access_mode thermal_read_mode;
  388. static int thermal_init(struct ibm_init_struct *iibm);
  389. static int thermal_get_sensor(int idx, s32 *value);
  390. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s);
  391. static int thermal_read(char *p);
  392. /*
  393. * Video subdriver
  394. */
  395. enum video_access_mode {
  396. TPACPI_VIDEO_NONE = 0,
  397. TPACPI_VIDEO_570, /* 570 */
  398. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  399. TPACPI_VIDEO_NEW, /* all others */
  400. };
  401. enum { /* video status flags, based on VIDEO_570 */
  402. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  403. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  404. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  405. };
  406. enum { /* TPACPI_VIDEO_570 constants */
  407. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  408. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  409. * video_status_flags */
  410. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  411. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  412. };
  413. static enum video_access_mode video_supported;
  414. static int video_orig_autosw;
  415. static acpi_handle vid_handle, vid2_handle;
  416. static int video_init(struct ibm_init_struct *iibm);
  417. static void video_exit(void);
  418. static int video_outputsw_get(void);
  419. static int video_outputsw_set(int status);
  420. static int video_autosw_get(void);
  421. static int video_autosw_set(int enable);
  422. static int video_outputsw_cycle(void);
  423. static int video_expand_toggle(void);
  424. static int video_read(char *p);
  425. static int video_write(char *buf);
  426. /*
  427. * Volume subdriver
  428. */
  429. static int volume_offset = 0x30;
  430. static int volume_read(char *p);
  431. static int volume_write(char *buf);
  432. /*
  433. * Wan subdriver
  434. */
  435. #define TPACPI_WAN_SYSFS_GROUP "wwan"
  436. enum {
  437. /* ACPI GWAN/SWAN bits */
  438. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  439. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  440. TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
  441. };
  442. static int wan_init(struct ibm_init_struct *iibm);
  443. static int wan_get_radiosw(void);
  444. static int wan_set_radiosw(int radio_on);
  445. static int wan_read(char *p);
  446. static int wan_write(char *buf);
  447. #endif /* __THINKPAD_ACPI_H */