2 * arch/ppc64/kernel/rtas-proc.c
3 * Copyright (C) 2000 Tilmann Bitterberg
4 * (tilmann@bitterberg.de)
6 * RTAS (Runtime Abstraction Services) stuff
7 * Intention is to provide a clean user interface
11 * Split off a header file and maybe move it to a different
12 * location. Write Documentation on what the /proc/rtas/ entries
16 #include <linux/errno.h>
17 #include <linux/sched.h>
18 #include <linux/proc_fs.h>
19 #include <linux/stat.h>
20 #include <linux/ctype.h>
21 #include <linux/time.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
24 #include <linux/seq_file.h>
25 #include <linux/bitops.h>
26 #include <linux/rtc.h>
28 #include <asm/uaccess.h>
29 #include <asm/processor.h>
33 #include <asm/machdep.h> /* for ppc_md */
35 #include <asm/systemcfg.h>
37 /* Token for Sensors */
38 #define KEY_SWITCH 0x0001
39 #define ENCLOSURE_SWITCH 0x0002
40 #define THERMAL_SENSOR 0x0003
41 #define LID_STATUS 0x0004
42 #define POWER_SOURCE 0x0005
43 #define BATTERY_VOLTAGE 0x0006
44 #define BATTERY_REMAINING 0x0007
45 #define BATTERY_PERCENTAGE 0x0008
46 #define EPOW_SENSOR 0x0009
47 #define BATTERY_CYCLESTATE 0x000a
48 #define BATTERY_CHARGING 0x000b
50 /* IBM specific sensors */
51 #define IBM_SURVEILLANCE 0x2328 /* 9000 */
52 #define IBM_FANRPM 0x2329 /* 9001 */
53 #define IBM_VOLTAGE 0x232a /* 9002 */
54 #define IBM_DRCONNECTOR 0x232b /* 9003 */
55 #define IBM_POWERSUPPLY 0x232c /* 9004 */
57 /* Status return values */
58 #define SENSOR_CRITICAL_HIGH 13
59 #define SENSOR_WARNING_HIGH 12
60 #define SENSOR_NORMAL 11
61 #define SENSOR_WARNING_LOW 10
62 #define SENSOR_CRITICAL_LOW 9
63 #define SENSOR_SUCCESS 0
64 #define SENSOR_HW_ERROR -1
65 #define SENSOR_BUSY -2
66 #define SENSOR_NOT_EXIST -3
67 #define SENSOR_DR_ENTITY -9000
70 #define LOC_SCSI_DEV_ADDR 'A'
71 #define LOC_SCSI_DEV_LOC 'B'
73 #define LOC_DISKETTE 'D'
74 #define LOC_ETHERNET 'E'
76 #define LOC_GRAPHICS 'G'
77 /* reserved / not used 'H' */
78 #define LOC_IO_ADAPTER 'I'
79 /* reserved / not used 'J' */
80 #define LOC_KEYBOARD 'K'
82 #define LOC_MEMORY 'M'
83 #define LOC_NV_MEMORY 'N'
85 #define LOC_PLANAR 'P'
86 #define LOC_OTHER_IO 'Q'
87 #define LOC_PARALLEL 'R'
88 #define LOC_SERIAL 'S'
89 #define LOC_DEAD_RING 'T'
90 #define LOC_RACKMOUNTED 'U' /* for _u_nit is rack mounted */
91 #define LOC_VOLTAGE 'V'
92 #define LOC_SWITCH_ADAPTER 'W'
94 #define LOC_FIRMWARE 'Y'
97 /* Tokens for indicators */
98 #define TONE_FREQUENCY 0x0001 /* 0 - 1000 (HZ)*/
99 #define TONE_VOLUME 0x0002 /* 0 - 100 (%) */
100 #define SYSTEM_POWER_STATE 0x0003
101 #define WARNING_LIGHT 0x0004
102 #define DISK_ACTIVITY_LIGHT 0x0005
103 #define HEX_DISPLAY_UNIT 0x0006
104 #define BATTERY_WARNING_TIME 0x0007
105 #define CONDITION_CYCLE_REQUEST 0x0008
106 #define SURVEILLANCE_INDICATOR 0x2328 /* 9000 */
107 #define DR_ACTION 0x2329 /* 9001 */
108 #define DR_INDICATOR 0x232a /* 9002 */
109 /* 9003 - 9004: Vendor specific */
110 /* 9006 - 9999: Vendor specific */
113 #define MAX_SENSORS 17 /* I only know of 17 sensors */
114 #define MAX_LINELENGTH 256
115 #define SENSOR_PREFIX "ibm,sensor-"
116 #define cel_to_fahr(x) ((x*9/5)+32)
120 static struct rtas_sensors sensors;
121 static struct device_node *rtas_node = NULL;
122 static unsigned long power_on_time = 0; /* Save the time the user set */
123 static char progress_led[MAX_LINELENGTH];
125 static unsigned long rtas_tone_frequency = 1000;
126 static unsigned long rtas_tone_volume = 0;
128 /* ****************STRUCTS******************************************* */
129 struct individual_sensor {
134 struct rtas_sensors {
135 struct individual_sensor sensor[MAX_SENSORS];
139 /* ****************************************************************** */
141 static int ppc_rtas_sensors_show(struct seq_file *m, void *v);
142 static int ppc_rtas_clock_show(struct seq_file *m, void *v);
143 static ssize_t ppc_rtas_clock_write(struct file *file,
144 const char __user *buf, size_t count, loff_t *ppos);
145 static int ppc_rtas_progress_show(struct seq_file *m, void *v);
146 static ssize_t ppc_rtas_progress_write(struct file *file,
147 const char __user *buf, size_t count, loff_t *ppos);
148 static int ppc_rtas_poweron_show(struct seq_file *m, void *v);
149 static ssize_t ppc_rtas_poweron_write(struct file *file,
150 const char __user *buf, size_t count, loff_t *ppos);
152 static ssize_t ppc_rtas_tone_freq_write(struct file *file,
153 const char __user *buf, size_t count, loff_t *ppos);
154 static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v);
155 static ssize_t ppc_rtas_tone_volume_write(struct file *file,
156 const char __user *buf, size_t count, loff_t *ppos);
157 static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v);
158 static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v);
160 static int sensors_open(struct inode *inode, struct file *file)
162 return single_open(file, ppc_rtas_sensors_show, NULL);
165 struct file_operations ppc_rtas_sensors_operations = {
166 .open = sensors_open,
169 .release = single_release,
172 static int poweron_open(struct inode *inode, struct file *file)
174 return single_open(file, ppc_rtas_poweron_show, NULL);
177 struct file_operations ppc_rtas_poweron_operations = {
178 .open = poweron_open,
181 .write = ppc_rtas_poweron_write,
182 .release = single_release,
185 static int progress_open(struct inode *inode, struct file *file)
187 return single_open(file, ppc_rtas_progress_show, NULL);
190 struct file_operations ppc_rtas_progress_operations = {
191 .open = progress_open,
194 .write = ppc_rtas_progress_write,
195 .release = single_release,
198 static int clock_open(struct inode *inode, struct file *file)
200 return single_open(file, ppc_rtas_clock_show, NULL);
203 struct file_operations ppc_rtas_clock_operations = {
207 .write = ppc_rtas_clock_write,
208 .release = single_release,
211 static int tone_freq_open(struct inode *inode, struct file *file)
213 return single_open(file, ppc_rtas_tone_freq_show, NULL);
216 struct file_operations ppc_rtas_tone_freq_operations = {
217 .open = tone_freq_open,
220 .write = ppc_rtas_tone_freq_write,
221 .release = single_release,
224 static int tone_volume_open(struct inode *inode, struct file *file)
226 return single_open(file, ppc_rtas_tone_volume_show, NULL);
229 struct file_operations ppc_rtas_tone_volume_operations = {
230 .open = tone_volume_open,
233 .write = ppc_rtas_tone_volume_write,
234 .release = single_release,
237 static int rmo_buf_open(struct inode *inode, struct file *file)
239 return single_open(file, ppc_rtas_rmo_buf_show, NULL);
242 struct file_operations ppc_rtas_rmo_buf_ops = {
243 .open = rmo_buf_open,
246 .release = single_release,
249 static int ppc_rtas_find_all_sensors(void);
250 static void ppc_rtas_process_sensor(struct seq_file *m,
251 struct individual_sensor *s, int state, int error, char *loc);
252 static char *ppc_rtas_process_error(int error);
253 static void get_location_code(struct seq_file *m,
254 struct individual_sensor *s, char *loc);
255 static void check_location_string(struct seq_file *m, char *c);
256 static void check_location(struct seq_file *m, char *c);
258 static int __init proc_rtas_init(void)
260 struct proc_dir_entry *entry;
262 if (!(systemcfg->platform & PLATFORM_PSERIES))
265 rtas_node = of_find_node_by_name(NULL, "rtas");
266 if (rtas_node == NULL)
269 entry = create_proc_entry("ppc64/rtas/progress", S_IRUGO|S_IWUSR, NULL);
271 entry->proc_fops = &ppc_rtas_progress_operations;
273 entry = create_proc_entry("ppc64/rtas/clock", S_IRUGO|S_IWUSR, NULL);
275 entry->proc_fops = &ppc_rtas_clock_operations;
277 entry = create_proc_entry("ppc64/rtas/poweron", S_IWUSR|S_IRUGO, NULL);
279 entry->proc_fops = &ppc_rtas_poweron_operations;
281 entry = create_proc_entry("ppc64/rtas/sensors", S_IRUGO, NULL);
283 entry->proc_fops = &ppc_rtas_sensors_operations;
285 entry = create_proc_entry("ppc64/rtas/frequency", S_IWUSR|S_IRUGO,
288 entry->proc_fops = &ppc_rtas_tone_freq_operations;
290 entry = create_proc_entry("ppc64/rtas/volume", S_IWUSR|S_IRUGO, NULL);
292 entry->proc_fops = &ppc_rtas_tone_volume_operations;
294 entry = create_proc_entry("ppc64/rtas/rmo_buffer", S_IRUSR, NULL);
296 entry->proc_fops = &ppc_rtas_rmo_buf_ops;
301 __initcall(proc_rtas_init);
303 static int parse_number(const char __user *p, size_t count, unsigned long *val)
311 if (copy_from_user(buf, p, count))
316 *val = simple_strtoul(buf, &end, 10);
317 if (*end && *end != '\n')
323 /* ****************************************************************** */
325 /* ****************************************************************** */
326 static ssize_t ppc_rtas_poweron_write(struct file *file,
327 const char __user *buf, size_t count, loff_t *ppos)
330 unsigned long nowtime;
331 int error = parse_number(buf, count, &nowtime);
335 power_on_time = nowtime; /* save the time */
339 error = rtas_call(rtas_token("set-time-for-power-on"), 7, 1, NULL,
340 tm.tm_year, tm.tm_mon, tm.tm_mday,
341 tm.tm_hour, tm.tm_min, tm.tm_sec, 0 /* nano */);
343 printk(KERN_WARNING "error: setting poweron time returned: %s\n",
344 ppc_rtas_process_error(error));
347 /* ****************************************************************** */
348 static int ppc_rtas_poweron_show(struct seq_file *m, void *v)
350 if (power_on_time == 0)
351 seq_printf(m, "Power on time not set\n");
353 seq_printf(m, "%lu\n",power_on_time);
357 /* ****************************************************************** */
359 /* ****************************************************************** */
360 static ssize_t ppc_rtas_progress_write(struct file *file,
361 const char __user *buf, size_t count, loff_t *ppos)
365 if (count >= MAX_LINELENGTH)
366 count = MAX_LINELENGTH -1;
367 if (copy_from_user(progress_led, buf, count)) { /* save the string */
370 progress_led[count] = 0;
372 /* Lets see if the user passed hexdigits */
373 hex = simple_strtoul(progress_led, NULL, 10);
375 rtas_progress ((char *)progress_led, hex);
379 /* rtas_progress(" ", 0xffff);*/
381 /* ****************************************************************** */
382 static int ppc_rtas_progress_show(struct seq_file *m, void *v)
385 seq_printf(m, "%s\n", progress_led);
389 /* ****************************************************************** */
391 /* ****************************************************************** */
392 static ssize_t ppc_rtas_clock_write(struct file *file,
393 const char __user *buf, size_t count, loff_t *ppos)
396 unsigned long nowtime;
397 int error = parse_number(buf, count, &nowtime);
402 error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
403 tm.tm_year, tm.tm_mon, tm.tm_mday,
404 tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
406 printk(KERN_WARNING "error: setting the clock returned: %s\n",
407 ppc_rtas_process_error(error));
410 /* ****************************************************************** */
411 static int ppc_rtas_clock_show(struct seq_file *m, void *v)
414 int error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
417 printk(KERN_WARNING "error: reading the clock returned: %s\n",
418 ppc_rtas_process_error(error));
421 unsigned int year, mon, day, hour, min, sec;
422 year = ret[0]; mon = ret[1]; day = ret[2];
423 hour = ret[3]; min = ret[4]; sec = ret[5];
424 seq_printf(m, "%lu\n",
425 mktime(year, mon, day, hour, min, sec));
430 /* ****************************************************************** */
432 /* ****************************************************************** */
433 static int ppc_rtas_sensors_show(struct seq_file *m, void *v)
437 int get_sensor_state = rtas_token("get-sensor-state");
439 seq_printf(m, "RTAS (RunTime Abstraction Services) Sensor Information\n");
440 seq_printf(m, "Sensor\t\tValue\t\tCondition\tLocation\n");
441 seq_printf(m, "********************************************************\n");
443 if (ppc_rtas_find_all_sensors() != 0) {
444 seq_printf(m, "\nNo sensors are available\n");
448 for (i=0; i<sensors.quant; i++) {
449 struct individual_sensor *p = &sensors.sensor[i];
454 sprintf (rstr, SENSOR_PREFIX"%04d", p->token);
455 loc = (char *) get_property(rtas_node, rstr, &llen);
457 /* A sensor may have multiple instances */
458 for (j = 0, offs = 0; j <= p->quant; j++) {
459 error = rtas_call(get_sensor_state, 2, 2, &state,
462 ppc_rtas_process_sensor(m, p, state, error, loc);
465 offs += strlen(loc) + 1;
466 loc += strlen(loc) + 1;
475 /* ****************************************************************** */
477 static int ppc_rtas_find_all_sensors(void)
482 utmp = (unsigned int *) get_property(rtas_node, "rtas-sensors", &len);
484 printk (KERN_ERR "error: could not get rtas-sensors\n");
488 sensors.quant = len / 8; /* int + int */
490 for (i=0; i<sensors.quant; i++) {
491 sensors.sensor[i].token = *utmp++;
492 sensors.sensor[i].quant = *utmp++;
497 /* ****************************************************************** */
499 * Builds a string of what rtas returned
501 static char *ppc_rtas_process_error(int error)
504 case SENSOR_CRITICAL_HIGH:
505 return "(critical high)";
506 case SENSOR_WARNING_HIGH:
507 return "(warning high)";
510 case SENSOR_WARNING_LOW:
511 return "(warning low)";
512 case SENSOR_CRITICAL_LOW:
513 return "(critical low)";
516 case SENSOR_HW_ERROR:
517 return "(hardware error)";
520 case SENSOR_NOT_EXIST:
521 return "(non existent)";
522 case SENSOR_DR_ENTITY:
523 return "(dr entity removed)";
529 /* ****************************************************************** */
531 * Builds a string out of what the sensor said
534 static void ppc_rtas_process_sensor(struct seq_file *m,
535 struct individual_sensor *s, int state, int error, char *loc)
537 /* Defined return vales */
538 const char * key_switch[] = { "Off\t", "Normal\t", "Secure\t",
540 const char * enclosure_switch[] = { "Closed", "Open" };
541 const char * lid_status[] = { " ", "Open", "Closed" };
542 const char * power_source[] = { "AC\t", "Battery",
544 const char * battery_remaining[] = { "Very Low", "Low", "Mid", "High" };
545 const char * epow_sensor[] = {
546 "EPOW Reset", "Cooling warning", "Power warning",
547 "System shutdown", "System halt", "EPOW main enclosure",
549 const char * battery_cyclestate[] = { "None", "In progress",
551 const char * battery_charging[] = { "Charging", "Discharching",
553 const char * ibm_drconnector[] = { "Empty", "Present", "Unusable",
556 int have_strings = 0;
561 /* What kind of sensor do we have here? */
565 seq_printf(m, "Key switch:\t");
566 num_states = sizeof(key_switch) / sizeof(char *);
567 if (state < num_states) {
568 seq_printf(m, "%s\t", key_switch[state]);
572 case ENCLOSURE_SWITCH:
573 seq_printf(m, "Enclosure switch:\t");
574 num_states = sizeof(enclosure_switch) / sizeof(char *);
575 if (state < num_states) {
576 seq_printf(m, "%s\t",
577 enclosure_switch[state]);
582 seq_printf(m, "Temp. (C/F):\t");
586 seq_printf(m, "Lid status:\t");
587 num_states = sizeof(lid_status) / sizeof(char *);
588 if (state < num_states) {
589 seq_printf(m, "%s\t", lid_status[state]);
594 seq_printf(m, "Power source:\t");
595 num_states = sizeof(power_source) / sizeof(char *);
596 if (state < num_states) {
597 seq_printf(m, "%s\t",
598 power_source[state]);
602 case BATTERY_VOLTAGE:
603 seq_printf(m, "Battery voltage:\t");
605 case BATTERY_REMAINING:
606 seq_printf(m, "Battery remaining:\t");
607 num_states = sizeof(battery_remaining) / sizeof(char *);
608 if (state < num_states)
610 seq_printf(m, "%s\t",
611 battery_remaining[state]);
615 case BATTERY_PERCENTAGE:
616 seq_printf(m, "Battery percentage:\t");
619 seq_printf(m, "EPOW Sensor:\t");
620 num_states = sizeof(epow_sensor) / sizeof(char *);
621 if (state < num_states) {
622 seq_printf(m, "%s\t", epow_sensor[state]);
626 case BATTERY_CYCLESTATE:
627 seq_printf(m, "Battery cyclestate:\t");
628 num_states = sizeof(battery_cyclestate) /
630 if (state < num_states) {
631 seq_printf(m, "%s\t",
632 battery_cyclestate[state]);
636 case BATTERY_CHARGING:
637 seq_printf(m, "Battery Charging:\t");
638 num_states = sizeof(battery_charging) / sizeof(char *);
639 if (state < num_states) {
640 seq_printf(m, "%s\t",
641 battery_charging[state]);
645 case IBM_SURVEILLANCE:
646 seq_printf(m, "Surveillance:\t");
649 seq_printf(m, "Fan (rpm):\t");
652 seq_printf(m, "Voltage (mv):\t");
654 case IBM_DRCONNECTOR:
655 seq_printf(m, "DR connector:\t");
656 num_states = sizeof(ibm_drconnector) / sizeof(char *);
657 if (state < num_states) {
658 seq_printf(m, "%s\t",
659 ibm_drconnector[state]);
663 case IBM_POWERSUPPLY:
664 seq_printf(m, "Powersupply:\t");
667 seq_printf(m, "Unknown sensor (type %d), ignoring it\n",
673 if (have_strings == 0) {
675 seq_printf(m, "%4d /%4d\t", state, cel_to_fahr(state));
677 seq_printf(m, "%10d\t", state);
680 seq_printf(m, "%s\t", ppc_rtas_process_error(error));
681 get_location_code(m, s, loc);
685 /* ****************************************************************** */
687 static void check_location(struct seq_file *m, char *c)
691 seq_printf(m, "Planar #%c", c[1]);
694 seq_printf(m, "CPU #%c", c[1]);
697 seq_printf(m, "Fan #%c", c[1]);
699 case LOC_RACKMOUNTED:
700 seq_printf(m, "Rack #%c", c[1]);
703 seq_printf(m, "Voltage #%c", c[1]);
706 seq_printf(m, "LCD #%c", c[1]);
709 seq_printf(m, "- %c", c[1]);
712 seq_printf(m, "Unknown location");
718 /* ****************************************************************** */
721 * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
722 * the '.' may be an abbrevation
724 static void check_location_string(struct seq_file *m, char *c)
727 if (isalpha(*c) || *c == '.')
728 check_location(m, c);
729 else if (*c == '/' || *c == '-')
730 seq_printf(m, " at ");
736 /* ****************************************************************** */
738 static void get_location_code(struct seq_file *m, struct individual_sensor *s, char *loc)
741 seq_printf(m, "---");/* does not have a location */
743 check_location_string(m, loc);
747 /* ****************************************************************** */
748 /* INDICATORS - Tone Frequency */
749 /* ****************************************************************** */
750 static ssize_t ppc_rtas_tone_freq_write(struct file *file,
751 const char __user *buf, size_t count, loff_t *ppos)
754 int error = parse_number(buf, count, &freq);
758 rtas_tone_frequency = freq; /* save it for later */
759 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
760 TONE_FREQUENCY, 0, freq);
762 printk(KERN_WARNING "error: setting tone frequency returned: %s\n",
763 ppc_rtas_process_error(error));
766 /* ****************************************************************** */
767 static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v)
769 seq_printf(m, "%lu\n", rtas_tone_frequency);
772 /* ****************************************************************** */
773 /* INDICATORS - Tone Volume */
774 /* ****************************************************************** */
775 static ssize_t ppc_rtas_tone_volume_write(struct file *file,
776 const char __user *buf, size_t count, loff_t *ppos)
778 unsigned long volume;
779 int error = parse_number(buf, count, &volume);
786 rtas_tone_volume = volume; /* save it for later */
787 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
788 TONE_VOLUME, 0, volume);
790 printk(KERN_WARNING "error: setting tone volume returned: %s\n",
791 ppc_rtas_process_error(error));
794 /* ****************************************************************** */
795 static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)
797 seq_printf(m, "%lu\n", rtas_tone_volume);
801 #define RMO_READ_BUF_MAX 30
803 /* RTAS Userspace access */
804 static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v)
806 seq_printf(m, "%016lx %x\n", rtas_rmo_buf, RTAS_RMOBUF_MAX);