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)
118 static struct rtas_sensors sensors
;
119 static struct device_node
*rtas_node
= NULL
;
120 static unsigned long power_on_time
= 0; /* Save the time the user set */
121 static char progress_led
[MAX_LINELENGTH
];
123 static unsigned long rtas_tone_frequency
= 1000;
124 static unsigned long rtas_tone_volume
= 0;
126 /* ****************STRUCTS******************************************* */
127 struct individual_sensor
{
132 struct rtas_sensors
{
133 struct individual_sensor sensor
[MAX_SENSORS
];
137 /* ****************************************************************** */
139 static int ppc_rtas_sensors_show(struct seq_file
*m
, void *v
);
140 static int ppc_rtas_clock_show(struct seq_file
*m
, void *v
);
141 static ssize_t
ppc_rtas_clock_write(struct file
*file
,
142 const char __user
*buf
, size_t count
, loff_t
*ppos
);
143 static int ppc_rtas_progress_show(struct seq_file
*m
, void *v
);
144 static ssize_t
ppc_rtas_progress_write(struct file
*file
,
145 const char __user
*buf
, size_t count
, loff_t
*ppos
);
146 static int ppc_rtas_poweron_show(struct seq_file
*m
, void *v
);
147 static ssize_t
ppc_rtas_poweron_write(struct file
*file
,
148 const char __user
*buf
, size_t count
, loff_t
*ppos
);
150 static ssize_t
ppc_rtas_tone_freq_write(struct file
*file
,
151 const char __user
*buf
, size_t count
, loff_t
*ppos
);
152 static int ppc_rtas_tone_freq_show(struct seq_file
*m
, void *v
);
153 static ssize_t
ppc_rtas_tone_volume_write(struct file
*file
,
154 const char __user
*buf
, size_t count
, loff_t
*ppos
);
155 static int ppc_rtas_tone_volume_show(struct seq_file
*m
, void *v
);
156 static int ppc_rtas_rmo_buf_show(struct seq_file
*m
, void *v
);
158 static int sensors_open(struct inode
*inode
, struct file
*file
)
160 return single_open(file
, ppc_rtas_sensors_show
, NULL
);
163 static const struct file_operations ppc_rtas_sensors_operations
= {
164 .open
= sensors_open
,
167 .release
= single_release
,
170 static int poweron_open(struct inode
*inode
, struct file
*file
)
172 return single_open(file
, ppc_rtas_poweron_show
, NULL
);
175 static const struct file_operations ppc_rtas_poweron_operations
= {
176 .open
= poweron_open
,
179 .write
= ppc_rtas_poweron_write
,
180 .release
= single_release
,
183 static int progress_open(struct inode
*inode
, struct file
*file
)
185 return single_open(file
, ppc_rtas_progress_show
, NULL
);
188 static const struct file_operations ppc_rtas_progress_operations
= {
189 .open
= progress_open
,
192 .write
= ppc_rtas_progress_write
,
193 .release
= single_release
,
196 static int clock_open(struct inode
*inode
, struct file
*file
)
198 return single_open(file
, ppc_rtas_clock_show
, NULL
);
201 static const struct file_operations ppc_rtas_clock_operations
= {
205 .write
= ppc_rtas_clock_write
,
206 .release
= single_release
,
209 static int tone_freq_open(struct inode
*inode
, struct file
*file
)
211 return single_open(file
, ppc_rtas_tone_freq_show
, NULL
);
214 static const struct file_operations ppc_rtas_tone_freq_operations
= {
215 .open
= tone_freq_open
,
218 .write
= ppc_rtas_tone_freq_write
,
219 .release
= single_release
,
222 static int tone_volume_open(struct inode
*inode
, struct file
*file
)
224 return single_open(file
, ppc_rtas_tone_volume_show
, NULL
);
227 static const struct file_operations ppc_rtas_tone_volume_operations
= {
228 .open
= tone_volume_open
,
231 .write
= ppc_rtas_tone_volume_write
,
232 .release
= single_release
,
235 static int rmo_buf_open(struct inode
*inode
, struct file
*file
)
237 return single_open(file
, ppc_rtas_rmo_buf_show
, NULL
);
240 static const struct file_operations ppc_rtas_rmo_buf_ops
= {
241 .open
= rmo_buf_open
,
244 .release
= single_release
,
247 static int ppc_rtas_find_all_sensors(void);
248 static void ppc_rtas_process_sensor(struct seq_file
*m
,
249 struct individual_sensor
*s
, int state
, int error
, const char *loc
);
250 static char *ppc_rtas_process_error(int error
);
251 static void get_location_code(struct seq_file
*m
,
252 struct individual_sensor
*s
, const char *loc
);
253 static void check_location_string(struct seq_file
*m
, const char *c
);
254 static void check_location(struct seq_file
*m
, const char *c
);
256 static int __init
proc_rtas_init(void)
258 if (!machine_is(pseries
))
261 rtas_node
= of_find_node_by_name(NULL
, "rtas");
262 if (rtas_node
== NULL
)
265 proc_create("powerpc/rtas/progress", S_IRUGO
|S_IWUSR
, NULL
,
266 &ppc_rtas_progress_operations
);
267 proc_create("powerpc/rtas/clock", S_IRUGO
|S_IWUSR
, NULL
,
268 &ppc_rtas_clock_operations
);
269 proc_create("powerpc/rtas/poweron", S_IWUSR
|S_IRUGO
, NULL
,
270 &ppc_rtas_poweron_operations
);
271 proc_create("powerpc/rtas/sensors", S_IRUGO
, NULL
,
272 &ppc_rtas_sensors_operations
);
273 proc_create("powerpc/rtas/frequency", S_IWUSR
|S_IRUGO
, NULL
,
274 &ppc_rtas_tone_freq_operations
);
275 proc_create("powerpc/rtas/volume", S_IWUSR
|S_IRUGO
, NULL
,
276 &ppc_rtas_tone_volume_operations
);
277 proc_create("powerpc/rtas/rmo_buffer", S_IRUSR
, NULL
,
278 &ppc_rtas_rmo_buf_ops
);
282 __initcall(proc_rtas_init
);
284 static int parse_number(const char __user
*p
, size_t count
, unsigned long *val
)
292 if (copy_from_user(buf
, p
, count
))
297 *val
= simple_strtoul(buf
, &end
, 10);
298 if (*end
&& *end
!= '\n')
304 /* ****************************************************************** */
306 /* ****************************************************************** */
307 static ssize_t
ppc_rtas_poweron_write(struct file
*file
,
308 const char __user
*buf
, size_t count
, loff_t
*ppos
)
311 unsigned long nowtime
;
312 int error
= parse_number(buf
, count
, &nowtime
);
316 power_on_time
= nowtime
; /* save the time */
320 error
= rtas_call(rtas_token("set-time-for-power-on"), 7, 1, NULL
,
321 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
322 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, 0 /* nano */);
324 printk(KERN_WARNING
"error: setting poweron time returned: %s\n",
325 ppc_rtas_process_error(error
));
328 /* ****************************************************************** */
329 static int ppc_rtas_poweron_show(struct seq_file
*m
, void *v
)
331 if (power_on_time
== 0)
332 seq_printf(m
, "Power on time not set\n");
334 seq_printf(m
, "%lu\n",power_on_time
);
338 /* ****************************************************************** */
340 /* ****************************************************************** */
341 static ssize_t
ppc_rtas_progress_write(struct file
*file
,
342 const char __user
*buf
, size_t count
, loff_t
*ppos
)
346 if (count
>= MAX_LINELENGTH
)
347 count
= MAX_LINELENGTH
-1;
348 if (copy_from_user(progress_led
, buf
, count
)) { /* save the string */
351 progress_led
[count
] = 0;
353 /* Lets see if the user passed hexdigits */
354 hex
= simple_strtoul(progress_led
, NULL
, 10);
356 rtas_progress ((char *)progress_led
, hex
);
360 /* rtas_progress(" ", 0xffff);*/
362 /* ****************************************************************** */
363 static int ppc_rtas_progress_show(struct seq_file
*m
, void *v
)
366 seq_printf(m
, "%s\n", progress_led
);
370 /* ****************************************************************** */
372 /* ****************************************************************** */
373 static ssize_t
ppc_rtas_clock_write(struct file
*file
,
374 const char __user
*buf
, size_t count
, loff_t
*ppos
)
377 unsigned long nowtime
;
378 int error
= parse_number(buf
, count
, &nowtime
);
383 error
= rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL
,
384 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
385 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, 0);
387 printk(KERN_WARNING
"error: setting the clock returned: %s\n",
388 ppc_rtas_process_error(error
));
391 /* ****************************************************************** */
392 static int ppc_rtas_clock_show(struct seq_file
*m
, void *v
)
395 int error
= rtas_call(rtas_token("get-time-of-day"), 0, 8, ret
);
398 printk(KERN_WARNING
"error: reading the clock returned: %s\n",
399 ppc_rtas_process_error(error
));
402 unsigned int year
, mon
, day
, hour
, min
, sec
;
403 year
= ret
[0]; mon
= ret
[1]; day
= ret
[2];
404 hour
= ret
[3]; min
= ret
[4]; sec
= ret
[5];
405 seq_printf(m
, "%lu\n",
406 mktime(year
, mon
, day
, hour
, min
, sec
));
411 /* ****************************************************************** */
413 /* ****************************************************************** */
414 static int ppc_rtas_sensors_show(struct seq_file
*m
, void *v
)
418 int get_sensor_state
= rtas_token("get-sensor-state");
420 seq_printf(m
, "RTAS (RunTime Abstraction Services) Sensor Information\n");
421 seq_printf(m
, "Sensor\t\tValue\t\tCondition\tLocation\n");
422 seq_printf(m
, "********************************************************\n");
424 if (ppc_rtas_find_all_sensors() != 0) {
425 seq_printf(m
, "\nNo sensors are available\n");
429 for (i
=0; i
<sensors
.quant
; i
++) {
430 struct individual_sensor
*p
= &sensors
.sensor
[i
];
435 sprintf (rstr
, SENSOR_PREFIX
"%04d", p
->token
);
436 loc
= of_get_property(rtas_node
, rstr
, &llen
);
438 /* A sensor may have multiple instances */
439 for (j
= 0, offs
= 0; j
<= p
->quant
; j
++) {
440 error
= rtas_call(get_sensor_state
, 2, 2, &state
,
443 ppc_rtas_process_sensor(m
, p
, state
, error
, loc
);
446 offs
+= strlen(loc
) + 1;
447 loc
+= strlen(loc
) + 1;
456 /* ****************************************************************** */
458 static int ppc_rtas_find_all_sensors(void)
460 const unsigned int *utmp
;
463 utmp
= of_get_property(rtas_node
, "rtas-sensors", &len
);
465 printk (KERN_ERR
"error: could not get rtas-sensors\n");
469 sensors
.quant
= len
/ 8; /* int + int */
471 for (i
=0; i
<sensors
.quant
; i
++) {
472 sensors
.sensor
[i
].token
= *utmp
++;
473 sensors
.sensor
[i
].quant
= *utmp
++;
478 /* ****************************************************************** */
480 * Builds a string of what rtas returned
482 static char *ppc_rtas_process_error(int error
)
485 case SENSOR_CRITICAL_HIGH
:
486 return "(critical high)";
487 case SENSOR_WARNING_HIGH
:
488 return "(warning high)";
491 case SENSOR_WARNING_LOW
:
492 return "(warning low)";
493 case SENSOR_CRITICAL_LOW
:
494 return "(critical low)";
497 case SENSOR_HW_ERROR
:
498 return "(hardware error)";
501 case SENSOR_NOT_EXIST
:
502 return "(non existent)";
503 case SENSOR_DR_ENTITY
:
504 return "(dr entity removed)";
510 /* ****************************************************************** */
512 * Builds a string out of what the sensor said
515 static void ppc_rtas_process_sensor(struct seq_file
*m
,
516 struct individual_sensor
*s
, int state
, int error
, const char *loc
)
518 /* Defined return vales */
519 const char * key_switch
[] = { "Off\t", "Normal\t", "Secure\t",
521 const char * enclosure_switch
[] = { "Closed", "Open" };
522 const char * lid_status
[] = { " ", "Open", "Closed" };
523 const char * power_source
[] = { "AC\t", "Battery",
525 const char * battery_remaining
[] = { "Very Low", "Low", "Mid", "High" };
526 const char * epow_sensor
[] = {
527 "EPOW Reset", "Cooling warning", "Power warning",
528 "System shutdown", "System halt", "EPOW main enclosure",
530 const char * battery_cyclestate
[] = { "None", "In progress",
532 const char * battery_charging
[] = { "Charging", "Discharching",
534 const char * ibm_drconnector
[] = { "Empty", "Present", "Unusable",
537 int have_strings
= 0;
542 /* What kind of sensor do we have here? */
546 seq_printf(m
, "Key switch:\t");
547 num_states
= sizeof(key_switch
) / sizeof(char *);
548 if (state
< num_states
) {
549 seq_printf(m
, "%s\t", key_switch
[state
]);
553 case ENCLOSURE_SWITCH
:
554 seq_printf(m
, "Enclosure switch:\t");
555 num_states
= sizeof(enclosure_switch
) / sizeof(char *);
556 if (state
< num_states
) {
557 seq_printf(m
, "%s\t",
558 enclosure_switch
[state
]);
563 seq_printf(m
, "Temp. (C/F):\t");
567 seq_printf(m
, "Lid status:\t");
568 num_states
= sizeof(lid_status
) / sizeof(char *);
569 if (state
< num_states
) {
570 seq_printf(m
, "%s\t", lid_status
[state
]);
575 seq_printf(m
, "Power source:\t");
576 num_states
= sizeof(power_source
) / sizeof(char *);
577 if (state
< num_states
) {
578 seq_printf(m
, "%s\t",
579 power_source
[state
]);
583 case BATTERY_VOLTAGE
:
584 seq_printf(m
, "Battery voltage:\t");
586 case BATTERY_REMAINING
:
587 seq_printf(m
, "Battery remaining:\t");
588 num_states
= sizeof(battery_remaining
) / sizeof(char *);
589 if (state
< num_states
)
591 seq_printf(m
, "%s\t",
592 battery_remaining
[state
]);
596 case BATTERY_PERCENTAGE
:
597 seq_printf(m
, "Battery percentage:\t");
600 seq_printf(m
, "EPOW Sensor:\t");
601 num_states
= sizeof(epow_sensor
) / sizeof(char *);
602 if (state
< num_states
) {
603 seq_printf(m
, "%s\t", epow_sensor
[state
]);
607 case BATTERY_CYCLESTATE
:
608 seq_printf(m
, "Battery cyclestate:\t");
609 num_states
= sizeof(battery_cyclestate
) /
611 if (state
< num_states
) {
612 seq_printf(m
, "%s\t",
613 battery_cyclestate
[state
]);
617 case BATTERY_CHARGING
:
618 seq_printf(m
, "Battery Charging:\t");
619 num_states
= sizeof(battery_charging
) / sizeof(char *);
620 if (state
< num_states
) {
621 seq_printf(m
, "%s\t",
622 battery_charging
[state
]);
626 case IBM_SURVEILLANCE
:
627 seq_printf(m
, "Surveillance:\t");
630 seq_printf(m
, "Fan (rpm):\t");
633 seq_printf(m
, "Voltage (mv):\t");
635 case IBM_DRCONNECTOR
:
636 seq_printf(m
, "DR connector:\t");
637 num_states
= sizeof(ibm_drconnector
) / sizeof(char *);
638 if (state
< num_states
) {
639 seq_printf(m
, "%s\t",
640 ibm_drconnector
[state
]);
644 case IBM_POWERSUPPLY
:
645 seq_printf(m
, "Powersupply:\t");
648 seq_printf(m
, "Unknown sensor (type %d), ignoring it\n",
654 if (have_strings
== 0) {
656 seq_printf(m
, "%4d /%4d\t", state
, cel_to_fahr(state
));
658 seq_printf(m
, "%10d\t", state
);
661 seq_printf(m
, "%s\t", ppc_rtas_process_error(error
));
662 get_location_code(m
, s
, loc
);
666 /* ****************************************************************** */
668 static void check_location(struct seq_file
*m
, const char *c
)
672 seq_printf(m
, "Planar #%c", c
[1]);
675 seq_printf(m
, "CPU #%c", c
[1]);
678 seq_printf(m
, "Fan #%c", c
[1]);
680 case LOC_RACKMOUNTED
:
681 seq_printf(m
, "Rack #%c", c
[1]);
684 seq_printf(m
, "Voltage #%c", c
[1]);
687 seq_printf(m
, "LCD #%c", c
[1]);
690 seq_printf(m
, "- %c", c
[1]);
693 seq_printf(m
, "Unknown location");
699 /* ****************************************************************** */
702 * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
703 * the '.' may be an abbrevation
705 static void check_location_string(struct seq_file
*m
, const char *c
)
708 if (isalpha(*c
) || *c
== '.')
709 check_location(m
, c
);
710 else if (*c
== '/' || *c
== '-')
711 seq_printf(m
, " at ");
717 /* ****************************************************************** */
719 static void get_location_code(struct seq_file
*m
, struct individual_sensor
*s
,
723 seq_printf(m
, "---");/* does not have a location */
725 check_location_string(m
, loc
);
729 /* ****************************************************************** */
730 /* INDICATORS - Tone Frequency */
731 /* ****************************************************************** */
732 static ssize_t
ppc_rtas_tone_freq_write(struct file
*file
,
733 const char __user
*buf
, size_t count
, loff_t
*ppos
)
736 int error
= parse_number(buf
, count
, &freq
);
740 rtas_tone_frequency
= freq
; /* save it for later */
741 error
= rtas_call(rtas_token("set-indicator"), 3, 1, NULL
,
742 TONE_FREQUENCY
, 0, freq
);
744 printk(KERN_WARNING
"error: setting tone frequency returned: %s\n",
745 ppc_rtas_process_error(error
));
748 /* ****************************************************************** */
749 static int ppc_rtas_tone_freq_show(struct seq_file
*m
, void *v
)
751 seq_printf(m
, "%lu\n", rtas_tone_frequency
);
754 /* ****************************************************************** */
755 /* INDICATORS - Tone Volume */
756 /* ****************************************************************** */
757 static ssize_t
ppc_rtas_tone_volume_write(struct file
*file
,
758 const char __user
*buf
, size_t count
, loff_t
*ppos
)
760 unsigned long volume
;
761 int error
= parse_number(buf
, count
, &volume
);
768 rtas_tone_volume
= volume
; /* save it for later */
769 error
= rtas_call(rtas_token("set-indicator"), 3, 1, NULL
,
770 TONE_VOLUME
, 0, volume
);
772 printk(KERN_WARNING
"error: setting tone volume returned: %s\n",
773 ppc_rtas_process_error(error
));
776 /* ****************************************************************** */
777 static int ppc_rtas_tone_volume_show(struct seq_file
*m
, void *v
)
779 seq_printf(m
, "%lu\n", rtas_tone_volume
);
783 #define RMO_READ_BUF_MAX 30
785 /* RTAS Userspace access */
786 static int ppc_rtas_rmo_buf_show(struct seq_file
*m
, void *v
)
788 seq_printf(m
, "%016lx %x\n", rtas_rmo_buf
, RTAS_RMOBUF_MAX
);