2 * Copyright (C) 2000 Tilmann Bitterberg
3 * (tilmann@bitterberg.de)
5 * RTAS (Runtime Abstraction Services) stuff
6 * Intention is to provide a clean user interface
10 * Split off a header file and maybe move it to a different
11 * location. Write Documentation on what the /proc/rtas/ entries
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/proc_fs.h>
18 #include <linux/stat.h>
19 #include <linux/ctype.h>
20 #include <linux/time.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/seq_file.h>
24 #include <linux/bitops.h>
25 #include <linux/rtc.h>
27 #include <asm/uaccess.h>
28 #include <asm/processor.h>
32 #include <asm/machdep.h> /* for ppc_md */
35 /* Token for Sensors */
36 #define KEY_SWITCH 0x0001
37 #define ENCLOSURE_SWITCH 0x0002
38 #define THERMAL_SENSOR 0x0003
39 #define LID_STATUS 0x0004
40 #define POWER_SOURCE 0x0005
41 #define BATTERY_VOLTAGE 0x0006
42 #define BATTERY_REMAINING 0x0007
43 #define BATTERY_PERCENTAGE 0x0008
44 #define EPOW_SENSOR 0x0009
45 #define BATTERY_CYCLESTATE 0x000a
46 #define BATTERY_CHARGING 0x000b
48 /* IBM specific sensors */
49 #define IBM_SURVEILLANCE 0x2328 /* 9000 */
50 #define IBM_FANRPM 0x2329 /* 9001 */
51 #define IBM_VOLTAGE 0x232a /* 9002 */
52 #define IBM_DRCONNECTOR 0x232b /* 9003 */
53 #define IBM_POWERSUPPLY 0x232c /* 9004 */
55 /* Status return values */
56 #define SENSOR_CRITICAL_HIGH 13
57 #define SENSOR_WARNING_HIGH 12
58 #define SENSOR_NORMAL 11
59 #define SENSOR_WARNING_LOW 10
60 #define SENSOR_CRITICAL_LOW 9
61 #define SENSOR_SUCCESS 0
62 #define SENSOR_HW_ERROR -1
63 #define SENSOR_BUSY -2
64 #define SENSOR_NOT_EXIST -3
65 #define SENSOR_DR_ENTITY -9000
68 #define LOC_SCSI_DEV_ADDR 'A'
69 #define LOC_SCSI_DEV_LOC 'B'
71 #define LOC_DISKETTE 'D'
72 #define LOC_ETHERNET 'E'
74 #define LOC_GRAPHICS 'G'
75 /* reserved / not used 'H' */
76 #define LOC_IO_ADAPTER 'I'
77 /* reserved / not used 'J' */
78 #define LOC_KEYBOARD 'K'
80 #define LOC_MEMORY 'M'
81 #define LOC_NV_MEMORY 'N'
83 #define LOC_PLANAR 'P'
84 #define LOC_OTHER_IO 'Q'
85 #define LOC_PARALLEL 'R'
86 #define LOC_SERIAL 'S'
87 #define LOC_DEAD_RING 'T'
88 #define LOC_RACKMOUNTED 'U' /* for _u_nit is rack mounted */
89 #define LOC_VOLTAGE 'V'
90 #define LOC_SWITCH_ADAPTER 'W'
92 #define LOC_FIRMWARE 'Y'
95 /* Tokens for indicators */
96 #define TONE_FREQUENCY 0x0001 /* 0 - 1000 (HZ)*/
97 #define TONE_VOLUME 0x0002 /* 0 - 100 (%) */
98 #define SYSTEM_POWER_STATE 0x0003
99 #define WARNING_LIGHT 0x0004
100 #define DISK_ACTIVITY_LIGHT 0x0005
101 #define HEX_DISPLAY_UNIT 0x0006
102 #define BATTERY_WARNING_TIME 0x0007
103 #define CONDITION_CYCLE_REQUEST 0x0008
104 #define SURVEILLANCE_INDICATOR 0x2328 /* 9000 */
105 #define DR_ACTION 0x2329 /* 9001 */
106 #define DR_INDICATOR 0x232a /* 9002 */
107 /* 9003 - 9004: Vendor specific */
108 /* 9006 - 9999: Vendor specific */
111 #define MAX_SENSORS 17 /* I only know of 17 sensors */
112 #define MAX_LINELENGTH 256
113 #define SENSOR_PREFIX "ibm,sensor-"
114 #define cel_to_fahr(x) ((x*9/5)+32)
116 struct individual_sensor
{
121 struct rtas_sensors
{
122 struct individual_sensor sensor
[MAX_SENSORS
];
127 static struct rtas_sensors sensors
;
128 static struct device_node
*rtas_node
= NULL
;
129 static unsigned long power_on_time
= 0; /* Save the time the user set */
130 static char progress_led
[MAX_LINELENGTH
];
132 static unsigned long rtas_tone_frequency
= 1000;
133 static unsigned long rtas_tone_volume
= 0;
135 /* ****************************************************************** */
137 static int ppc_rtas_sensors_show(struct seq_file
*m
, void *v
);
138 static int ppc_rtas_clock_show(struct seq_file
*m
, void *v
);
139 static ssize_t
ppc_rtas_clock_write(struct file
*file
,
140 const char __user
*buf
, size_t count
, loff_t
*ppos
);
141 static int ppc_rtas_progress_show(struct seq_file
*m
, void *v
);
142 static ssize_t
ppc_rtas_progress_write(struct file
*file
,
143 const char __user
*buf
, size_t count
, loff_t
*ppos
);
144 static int ppc_rtas_poweron_show(struct seq_file
*m
, void *v
);
145 static ssize_t
ppc_rtas_poweron_write(struct file
*file
,
146 const char __user
*buf
, size_t count
, loff_t
*ppos
);
148 static ssize_t
ppc_rtas_tone_freq_write(struct file
*file
,
149 const char __user
*buf
, size_t count
, loff_t
*ppos
);
150 static int ppc_rtas_tone_freq_show(struct seq_file
*m
, void *v
);
151 static ssize_t
ppc_rtas_tone_volume_write(struct file
*file
,
152 const char __user
*buf
, size_t count
, loff_t
*ppos
);
153 static int ppc_rtas_tone_volume_show(struct seq_file
*m
, void *v
);
154 static int ppc_rtas_rmo_buf_show(struct seq_file
*m
, void *v
);
156 static int sensors_open(struct inode
*inode
, struct file
*file
)
158 return single_open(file
, ppc_rtas_sensors_show
, NULL
);
161 static const struct file_operations ppc_rtas_sensors_operations
= {
162 .open
= sensors_open
,
165 .release
= single_release
,
168 static int poweron_open(struct inode
*inode
, struct file
*file
)
170 return single_open(file
, ppc_rtas_poweron_show
, NULL
);
173 static const struct file_operations ppc_rtas_poweron_operations
= {
174 .open
= poweron_open
,
177 .write
= ppc_rtas_poweron_write
,
178 .release
= single_release
,
181 static int progress_open(struct inode
*inode
, struct file
*file
)
183 return single_open(file
, ppc_rtas_progress_show
, NULL
);
186 static const struct file_operations ppc_rtas_progress_operations
= {
187 .open
= progress_open
,
190 .write
= ppc_rtas_progress_write
,
191 .release
= single_release
,
194 static int clock_open(struct inode
*inode
, struct file
*file
)
196 return single_open(file
, ppc_rtas_clock_show
, NULL
);
199 static const struct file_operations ppc_rtas_clock_operations
= {
203 .write
= ppc_rtas_clock_write
,
204 .release
= single_release
,
207 static int tone_freq_open(struct inode
*inode
, struct file
*file
)
209 return single_open(file
, ppc_rtas_tone_freq_show
, NULL
);
212 static const struct file_operations ppc_rtas_tone_freq_operations
= {
213 .open
= tone_freq_open
,
216 .write
= ppc_rtas_tone_freq_write
,
217 .release
= single_release
,
220 static int tone_volume_open(struct inode
*inode
, struct file
*file
)
222 return single_open(file
, ppc_rtas_tone_volume_show
, NULL
);
225 static const struct file_operations ppc_rtas_tone_volume_operations
= {
226 .open
= tone_volume_open
,
229 .write
= ppc_rtas_tone_volume_write
,
230 .release
= single_release
,
233 static int rmo_buf_open(struct inode
*inode
, struct file
*file
)
235 return single_open(file
, ppc_rtas_rmo_buf_show
, NULL
);
238 static const struct file_operations ppc_rtas_rmo_buf_ops
= {
239 .open
= rmo_buf_open
,
242 .release
= single_release
,
245 static int ppc_rtas_find_all_sensors(void);
246 static void ppc_rtas_process_sensor(struct seq_file
*m
,
247 struct individual_sensor
*s
, int state
, int error
, const char *loc
);
248 static char *ppc_rtas_process_error(int error
);
249 static void get_location_code(struct seq_file
*m
,
250 struct individual_sensor
*s
, const char *loc
);
251 static void check_location_string(struct seq_file
*m
, const char *c
);
252 static void check_location(struct seq_file
*m
, const char *c
);
254 static int __init
proc_rtas_init(void)
256 if (!machine_is(pseries
))
259 rtas_node
= of_find_node_by_name(NULL
, "rtas");
260 if (rtas_node
== NULL
)
263 proc_create("powerpc/rtas/progress", S_IRUGO
|S_IWUSR
, NULL
,
264 &ppc_rtas_progress_operations
);
265 proc_create("powerpc/rtas/clock", S_IRUGO
|S_IWUSR
, NULL
,
266 &ppc_rtas_clock_operations
);
267 proc_create("powerpc/rtas/poweron", S_IWUSR
|S_IRUGO
, NULL
,
268 &ppc_rtas_poweron_operations
);
269 proc_create("powerpc/rtas/sensors", S_IRUGO
, NULL
,
270 &ppc_rtas_sensors_operations
);
271 proc_create("powerpc/rtas/frequency", S_IWUSR
|S_IRUGO
, NULL
,
272 &ppc_rtas_tone_freq_operations
);
273 proc_create("powerpc/rtas/volume", S_IWUSR
|S_IRUGO
, NULL
,
274 &ppc_rtas_tone_volume_operations
);
275 proc_create("powerpc/rtas/rmo_buffer", S_IRUSR
, NULL
,
276 &ppc_rtas_rmo_buf_ops
);
280 __initcall(proc_rtas_init
);
282 static int parse_number(const char __user
*p
, size_t count
, unsigned long *val
)
290 if (copy_from_user(buf
, p
, count
))
295 *val
= simple_strtoul(buf
, &end
, 10);
296 if (*end
&& *end
!= '\n')
302 /* ****************************************************************** */
304 /* ****************************************************************** */
305 static ssize_t
ppc_rtas_poweron_write(struct file
*file
,
306 const char __user
*buf
, size_t count
, loff_t
*ppos
)
309 unsigned long nowtime
;
310 int error
= parse_number(buf
, count
, &nowtime
);
314 power_on_time
= nowtime
; /* save the time */
318 error
= rtas_call(rtas_token("set-time-for-power-on"), 7, 1, NULL
,
319 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
320 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, 0 /* nano */);
322 printk(KERN_WARNING
"error: setting poweron time returned: %s\n",
323 ppc_rtas_process_error(error
));
326 /* ****************************************************************** */
327 static int ppc_rtas_poweron_show(struct seq_file
*m
, void *v
)
329 if (power_on_time
== 0)
330 seq_printf(m
, "Power on time not set\n");
332 seq_printf(m
, "%lu\n",power_on_time
);
336 /* ****************************************************************** */
338 /* ****************************************************************** */
339 static ssize_t
ppc_rtas_progress_write(struct file
*file
,
340 const char __user
*buf
, size_t count
, loff_t
*ppos
)
344 if (count
>= MAX_LINELENGTH
)
345 count
= MAX_LINELENGTH
-1;
346 if (copy_from_user(progress_led
, buf
, count
)) { /* save the string */
349 progress_led
[count
] = 0;
351 /* Lets see if the user passed hexdigits */
352 hex
= simple_strtoul(progress_led
, NULL
, 10);
354 rtas_progress ((char *)progress_led
, hex
);
358 /* rtas_progress(" ", 0xffff);*/
360 /* ****************************************************************** */
361 static int ppc_rtas_progress_show(struct seq_file
*m
, void *v
)
364 seq_printf(m
, "%s\n", progress_led
);
368 /* ****************************************************************** */
370 /* ****************************************************************** */
371 static ssize_t
ppc_rtas_clock_write(struct file
*file
,
372 const char __user
*buf
, size_t count
, loff_t
*ppos
)
375 unsigned long nowtime
;
376 int error
= parse_number(buf
, count
, &nowtime
);
381 error
= rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL
,
382 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
383 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, 0);
385 printk(KERN_WARNING
"error: setting the clock returned: %s\n",
386 ppc_rtas_process_error(error
));
389 /* ****************************************************************** */
390 static int ppc_rtas_clock_show(struct seq_file
*m
, void *v
)
393 int error
= rtas_call(rtas_token("get-time-of-day"), 0, 8, ret
);
396 printk(KERN_WARNING
"error: reading the clock returned: %s\n",
397 ppc_rtas_process_error(error
));
400 unsigned int year
, mon
, day
, hour
, min
, sec
;
401 year
= ret
[0]; mon
= ret
[1]; day
= ret
[2];
402 hour
= ret
[3]; min
= ret
[4]; sec
= ret
[5];
403 seq_printf(m
, "%lu\n",
404 mktime(year
, mon
, day
, hour
, min
, sec
));
409 /* ****************************************************************** */
411 /* ****************************************************************** */
412 static int ppc_rtas_sensors_show(struct seq_file
*m
, void *v
)
416 int get_sensor_state
= rtas_token("get-sensor-state");
418 seq_printf(m
, "RTAS (RunTime Abstraction Services) Sensor Information\n");
419 seq_printf(m
, "Sensor\t\tValue\t\tCondition\tLocation\n");
420 seq_printf(m
, "********************************************************\n");
422 if (ppc_rtas_find_all_sensors() != 0) {
423 seq_printf(m
, "\nNo sensors are available\n");
427 for (i
=0; i
<sensors
.quant
; i
++) {
428 struct individual_sensor
*p
= &sensors
.sensor
[i
];
433 sprintf (rstr
, SENSOR_PREFIX
"%04d", p
->token
);
434 loc
= of_get_property(rtas_node
, rstr
, &llen
);
436 /* A sensor may have multiple instances */
437 for (j
= 0, offs
= 0; j
<= p
->quant
; j
++) {
438 error
= rtas_call(get_sensor_state
, 2, 2, &state
,
441 ppc_rtas_process_sensor(m
, p
, state
, error
, loc
);
444 offs
+= strlen(loc
) + 1;
445 loc
+= strlen(loc
) + 1;
454 /* ****************************************************************** */
456 static int ppc_rtas_find_all_sensors(void)
458 const unsigned int *utmp
;
461 utmp
= of_get_property(rtas_node
, "rtas-sensors", &len
);
463 printk (KERN_ERR
"error: could not get rtas-sensors\n");
467 sensors
.quant
= len
/ 8; /* int + int */
469 for (i
=0; i
<sensors
.quant
; i
++) {
470 sensors
.sensor
[i
].token
= *utmp
++;
471 sensors
.sensor
[i
].quant
= *utmp
++;
476 /* ****************************************************************** */
478 * Builds a string of what rtas returned
480 static char *ppc_rtas_process_error(int error
)
483 case SENSOR_CRITICAL_HIGH
:
484 return "(critical high)";
485 case SENSOR_WARNING_HIGH
:
486 return "(warning high)";
489 case SENSOR_WARNING_LOW
:
490 return "(warning low)";
491 case SENSOR_CRITICAL_LOW
:
492 return "(critical low)";
495 case SENSOR_HW_ERROR
:
496 return "(hardware error)";
499 case SENSOR_NOT_EXIST
:
500 return "(non existent)";
501 case SENSOR_DR_ENTITY
:
502 return "(dr entity removed)";
508 /* ****************************************************************** */
510 * Builds a string out of what the sensor said
513 static void ppc_rtas_process_sensor(struct seq_file
*m
,
514 struct individual_sensor
*s
, int state
, int error
, const char *loc
)
516 /* Defined return vales */
517 const char * key_switch
[] = { "Off\t", "Normal\t", "Secure\t",
519 const char * enclosure_switch
[] = { "Closed", "Open" };
520 const char * lid_status
[] = { " ", "Open", "Closed" };
521 const char * power_source
[] = { "AC\t", "Battery",
523 const char * battery_remaining
[] = { "Very Low", "Low", "Mid", "High" };
524 const char * epow_sensor
[] = {
525 "EPOW Reset", "Cooling warning", "Power warning",
526 "System shutdown", "System halt", "EPOW main enclosure",
528 const char * battery_cyclestate
[] = { "None", "In progress",
530 const char * battery_charging
[] = { "Charging", "Discharching",
532 const char * ibm_drconnector
[] = { "Empty", "Present", "Unusable",
535 int have_strings
= 0;
540 /* What kind of sensor do we have here? */
544 seq_printf(m
, "Key switch:\t");
545 num_states
= sizeof(key_switch
) / sizeof(char *);
546 if (state
< num_states
) {
547 seq_printf(m
, "%s\t", key_switch
[state
]);
551 case ENCLOSURE_SWITCH
:
552 seq_printf(m
, "Enclosure switch:\t");
553 num_states
= sizeof(enclosure_switch
) / sizeof(char *);
554 if (state
< num_states
) {
555 seq_printf(m
, "%s\t",
556 enclosure_switch
[state
]);
561 seq_printf(m
, "Temp. (C/F):\t");
565 seq_printf(m
, "Lid status:\t");
566 num_states
= sizeof(lid_status
) / sizeof(char *);
567 if (state
< num_states
) {
568 seq_printf(m
, "%s\t", lid_status
[state
]);
573 seq_printf(m
, "Power source:\t");
574 num_states
= sizeof(power_source
) / sizeof(char *);
575 if (state
< num_states
) {
576 seq_printf(m
, "%s\t",
577 power_source
[state
]);
581 case BATTERY_VOLTAGE
:
582 seq_printf(m
, "Battery voltage:\t");
584 case BATTERY_REMAINING
:
585 seq_printf(m
, "Battery remaining:\t");
586 num_states
= sizeof(battery_remaining
) / sizeof(char *);
587 if (state
< num_states
)
589 seq_printf(m
, "%s\t",
590 battery_remaining
[state
]);
594 case BATTERY_PERCENTAGE
:
595 seq_printf(m
, "Battery percentage:\t");
598 seq_printf(m
, "EPOW Sensor:\t");
599 num_states
= sizeof(epow_sensor
) / sizeof(char *);
600 if (state
< num_states
) {
601 seq_printf(m
, "%s\t", epow_sensor
[state
]);
605 case BATTERY_CYCLESTATE
:
606 seq_printf(m
, "Battery cyclestate:\t");
607 num_states
= sizeof(battery_cyclestate
) /
609 if (state
< num_states
) {
610 seq_printf(m
, "%s\t",
611 battery_cyclestate
[state
]);
615 case BATTERY_CHARGING
:
616 seq_printf(m
, "Battery Charging:\t");
617 num_states
= sizeof(battery_charging
) / sizeof(char *);
618 if (state
< num_states
) {
619 seq_printf(m
, "%s\t",
620 battery_charging
[state
]);
624 case IBM_SURVEILLANCE
:
625 seq_printf(m
, "Surveillance:\t");
628 seq_printf(m
, "Fan (rpm):\t");
631 seq_printf(m
, "Voltage (mv):\t");
633 case IBM_DRCONNECTOR
:
634 seq_printf(m
, "DR connector:\t");
635 num_states
= sizeof(ibm_drconnector
) / sizeof(char *);
636 if (state
< num_states
) {
637 seq_printf(m
, "%s\t",
638 ibm_drconnector
[state
]);
642 case IBM_POWERSUPPLY
:
643 seq_printf(m
, "Powersupply:\t");
646 seq_printf(m
, "Unknown sensor (type %d), ignoring it\n",
652 if (have_strings
== 0) {
654 seq_printf(m
, "%4d /%4d\t", state
, cel_to_fahr(state
));
656 seq_printf(m
, "%10d\t", state
);
659 seq_printf(m
, "%s\t", ppc_rtas_process_error(error
));
660 get_location_code(m
, s
, loc
);
664 /* ****************************************************************** */
666 static void check_location(struct seq_file
*m
, const char *c
)
670 seq_printf(m
, "Planar #%c", c
[1]);
673 seq_printf(m
, "CPU #%c", c
[1]);
676 seq_printf(m
, "Fan #%c", c
[1]);
678 case LOC_RACKMOUNTED
:
679 seq_printf(m
, "Rack #%c", c
[1]);
682 seq_printf(m
, "Voltage #%c", c
[1]);
685 seq_printf(m
, "LCD #%c", c
[1]);
688 seq_printf(m
, "- %c", c
[1]);
691 seq_printf(m
, "Unknown location");
697 /* ****************************************************************** */
700 * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
701 * the '.' may be an abbreviation
703 static void check_location_string(struct seq_file
*m
, const char *c
)
706 if (isalpha(*c
) || *c
== '.')
707 check_location(m
, c
);
708 else if (*c
== '/' || *c
== '-')
709 seq_printf(m
, " at ");
715 /* ****************************************************************** */
717 static void get_location_code(struct seq_file
*m
, struct individual_sensor
*s
,
721 seq_printf(m
, "---");/* does not have a location */
723 check_location_string(m
, loc
);
727 /* ****************************************************************** */
728 /* INDICATORS - Tone Frequency */
729 /* ****************************************************************** */
730 static ssize_t
ppc_rtas_tone_freq_write(struct file
*file
,
731 const char __user
*buf
, size_t count
, loff_t
*ppos
)
734 int error
= parse_number(buf
, count
, &freq
);
738 rtas_tone_frequency
= freq
; /* save it for later */
739 error
= rtas_call(rtas_token("set-indicator"), 3, 1, NULL
,
740 TONE_FREQUENCY
, 0, freq
);
742 printk(KERN_WARNING
"error: setting tone frequency returned: %s\n",
743 ppc_rtas_process_error(error
));
746 /* ****************************************************************** */
747 static int ppc_rtas_tone_freq_show(struct seq_file
*m
, void *v
)
749 seq_printf(m
, "%lu\n", rtas_tone_frequency
);
752 /* ****************************************************************** */
753 /* INDICATORS - Tone Volume */
754 /* ****************************************************************** */
755 static ssize_t
ppc_rtas_tone_volume_write(struct file
*file
,
756 const char __user
*buf
, size_t count
, loff_t
*ppos
)
758 unsigned long volume
;
759 int error
= parse_number(buf
, count
, &volume
);
766 rtas_tone_volume
= volume
; /* save it for later */
767 error
= rtas_call(rtas_token("set-indicator"), 3, 1, NULL
,
768 TONE_VOLUME
, 0, volume
);
770 printk(KERN_WARNING
"error: setting tone volume returned: %s\n",
771 ppc_rtas_process_error(error
));
774 /* ****************************************************************** */
775 static int ppc_rtas_tone_volume_show(struct seq_file
*m
, void *v
)
777 seq_printf(m
, "%lu\n", rtas_tone_volume
);
781 #define RMO_READ_BUF_MAX 30
783 /* RTAS Userspace access */
784 static int ppc_rtas_rmo_buf_show(struct seq_file
*m
, void *v
)
786 seq_printf(m
, "%016lx %x\n", rtas_rmo_buf
, RTAS_RMOBUF_MAX
);