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