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 */
36 /* Token for Sensors */
37 #define KEY_SWITCH 0x0001
38 #define ENCLOSURE_SWITCH 0x0002
39 #define THERMAL_SENSOR 0x0003
40 #define LID_STATUS 0x0004
41 #define POWER_SOURCE 0x0005
42 #define BATTERY_VOLTAGE 0x0006
43 #define BATTERY_REMAINING 0x0007
44 #define BATTERY_PERCENTAGE 0x0008
45 #define EPOW_SENSOR 0x0009
46 #define BATTERY_CYCLESTATE 0x000a
47 #define BATTERY_CHARGING 0x000b
49 /* IBM specific sensors */
50 #define IBM_SURVEILLANCE 0x2328 /* 9000 */
51 #define IBM_FANRPM 0x2329 /* 9001 */
52 #define IBM_VOLTAGE 0x232a /* 9002 */
53 #define IBM_DRCONNECTOR 0x232b /* 9003 */
54 #define IBM_POWERSUPPLY 0x232c /* 9004 */
56 /* Status return values */
57 #define SENSOR_CRITICAL_HIGH 13
58 #define SENSOR_WARNING_HIGH 12
59 #define SENSOR_NORMAL 11
60 #define SENSOR_WARNING_LOW 10
61 #define SENSOR_CRITICAL_LOW 9
62 #define SENSOR_SUCCESS 0
63 #define SENSOR_HW_ERROR -1
64 #define SENSOR_BUSY -2
65 #define SENSOR_NOT_EXIST -3
66 #define SENSOR_DR_ENTITY -9000
69 #define LOC_SCSI_DEV_ADDR 'A'
70 #define LOC_SCSI_DEV_LOC 'B'
72 #define LOC_DISKETTE 'D'
73 #define LOC_ETHERNET 'E'
75 #define LOC_GRAPHICS 'G'
76 /* reserved / not used 'H' */
77 #define LOC_IO_ADAPTER 'I'
78 /* reserved / not used 'J' */
79 #define LOC_KEYBOARD 'K'
81 #define LOC_MEMORY 'M'
82 #define LOC_NV_MEMORY 'N'
84 #define LOC_PLANAR 'P'
85 #define LOC_OTHER_IO 'Q'
86 #define LOC_PARALLEL 'R'
87 #define LOC_SERIAL 'S'
88 #define LOC_DEAD_RING 'T'
89 #define LOC_RACKMOUNTED 'U' /* for _u_nit is rack mounted */
90 #define LOC_VOLTAGE 'V'
91 #define LOC_SWITCH_ADAPTER 'W'
93 #define LOC_FIRMWARE 'Y'
96 /* Tokens for indicators */
97 #define TONE_FREQUENCY 0x0001 /* 0 - 1000 (HZ)*/
98 #define TONE_VOLUME 0x0002 /* 0 - 100 (%) */
99 #define SYSTEM_POWER_STATE 0x0003
100 #define WARNING_LIGHT 0x0004
101 #define DISK_ACTIVITY_LIGHT 0x0005
102 #define HEX_DISPLAY_UNIT 0x0006
103 #define BATTERY_WARNING_TIME 0x0007
104 #define CONDITION_CYCLE_REQUEST 0x0008
105 #define SURVEILLANCE_INDICATOR 0x2328 /* 9000 */
106 #define DR_ACTION 0x2329 /* 9001 */
107 #define DR_INDICATOR 0x232a /* 9002 */
108 /* 9003 - 9004: Vendor specific */
109 /* 9006 - 9999: Vendor specific */
112 #define MAX_SENSORS 17 /* I only know of 17 sensors */
113 #define MAX_LINELENGTH 256
114 #define SENSOR_PREFIX "ibm,sensor-"
115 #define cel_to_fahr(x) ((x*9/5)+32)
119 static struct rtas_sensors sensors;
120 static struct device_node *rtas_node = NULL;
121 static unsigned long power_on_time = 0; /* Save the time the user set */
122 static char progress_led[MAX_LINELENGTH];
124 static unsigned long rtas_tone_frequency = 1000;
125 static unsigned long rtas_tone_volume = 0;
127 /* ****************STRUCTS******************************************* */
128 struct individual_sensor {
133 struct rtas_sensors {
134 struct individual_sensor sensor[MAX_SENSORS];
138 /* ****************************************************************** */
140 static int ppc_rtas_sensors_show(struct seq_file *m, void *v);
141 static int ppc_rtas_clock_show(struct seq_file *m, void *v);
142 static ssize_t ppc_rtas_clock_write(struct file *file,
143 const char __user *buf, size_t count, loff_t *ppos);
144 static int ppc_rtas_progress_show(struct seq_file *m, void *v);
145 static ssize_t ppc_rtas_progress_write(struct file *file,
146 const char __user *buf, size_t count, loff_t *ppos);
147 static int ppc_rtas_poweron_show(struct seq_file *m, void *v);
148 static ssize_t ppc_rtas_poweron_write(struct file *file,
149 const char __user *buf, size_t count, loff_t *ppos);
151 static ssize_t ppc_rtas_tone_freq_write(struct file *file,
152 const char __user *buf, size_t count, loff_t *ppos);
153 static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v);
154 static ssize_t ppc_rtas_tone_volume_write(struct file *file,
155 const char __user *buf, size_t count, loff_t *ppos);
156 static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v);
157 static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v);
159 static int sensors_open(struct inode *inode, struct file *file)
161 return single_open(file, ppc_rtas_sensors_show, NULL);
164 struct file_operations ppc_rtas_sensors_operations = {
165 .open = sensors_open,
168 .release = single_release,
171 static int poweron_open(struct inode *inode, struct file *file)
173 return single_open(file, ppc_rtas_poweron_show, NULL);
176 struct file_operations ppc_rtas_poweron_operations = {
177 .open = poweron_open,
180 .write = ppc_rtas_poweron_write,
181 .release = single_release,
184 static int progress_open(struct inode *inode, struct file *file)
186 return single_open(file, ppc_rtas_progress_show, NULL);
189 struct file_operations ppc_rtas_progress_operations = {
190 .open = progress_open,
193 .write = ppc_rtas_progress_write,
194 .release = single_release,
197 static int clock_open(struct inode *inode, struct file *file)
199 return single_open(file, ppc_rtas_clock_show, NULL);
202 struct file_operations ppc_rtas_clock_operations = {
206 .write = ppc_rtas_clock_write,
207 .release = single_release,
210 static int tone_freq_open(struct inode *inode, struct file *file)
212 return single_open(file, ppc_rtas_tone_freq_show, NULL);
215 struct file_operations ppc_rtas_tone_freq_operations = {
216 .open = tone_freq_open,
219 .write = ppc_rtas_tone_freq_write,
220 .release = single_release,
223 static int tone_volume_open(struct inode *inode, struct file *file)
225 return single_open(file, ppc_rtas_tone_volume_show, NULL);
228 struct file_operations ppc_rtas_tone_volume_operations = {
229 .open = tone_volume_open,
232 .write = ppc_rtas_tone_volume_write,
233 .release = single_release,
236 static int rmo_buf_open(struct inode *inode, struct file *file)
238 return single_open(file, ppc_rtas_rmo_buf_show, NULL);
241 struct file_operations ppc_rtas_rmo_buf_ops = {
242 .open = rmo_buf_open,
245 .release = single_release,
248 static int ppc_rtas_find_all_sensors(void);
249 static void ppc_rtas_process_sensor(struct seq_file *m,
250 struct individual_sensor *s, int state, int error, char *loc);
251 static char *ppc_rtas_process_error(int error);
252 static void get_location_code(struct seq_file *m,
253 struct individual_sensor *s, char *loc);
254 static void check_location_string(struct seq_file *m, char *c);
255 static void check_location(struct seq_file *m, char *c);
257 static int __init proc_rtas_init(void)
259 struct proc_dir_entry *entry;
261 if (_machine != PLATFORM_PSERIES && _machine != PLATFORM_PSERIES_LPAR)
264 rtas_node = of_find_node_by_name(NULL, "rtas");
265 if (rtas_node == NULL)
268 entry = create_proc_entry("ppc64/rtas/progress", S_IRUGO|S_IWUSR, NULL);
270 entry->proc_fops = &ppc_rtas_progress_operations;
272 entry = create_proc_entry("ppc64/rtas/clock", S_IRUGO|S_IWUSR, NULL);
274 entry->proc_fops = &ppc_rtas_clock_operations;
276 entry = create_proc_entry("ppc64/rtas/poweron", S_IWUSR|S_IRUGO, NULL);
278 entry->proc_fops = &ppc_rtas_poweron_operations;
280 entry = create_proc_entry("ppc64/rtas/sensors", S_IRUGO, NULL);
282 entry->proc_fops = &ppc_rtas_sensors_operations;
284 entry = create_proc_entry("ppc64/rtas/frequency", S_IWUSR|S_IRUGO,
287 entry->proc_fops = &ppc_rtas_tone_freq_operations;
289 entry = create_proc_entry("ppc64/rtas/volume", S_IWUSR|S_IRUGO, NULL);
291 entry->proc_fops = &ppc_rtas_tone_volume_operations;
293 entry = create_proc_entry("ppc64/rtas/rmo_buffer", S_IRUSR, NULL);
295 entry->proc_fops = &ppc_rtas_rmo_buf_ops;
300 __initcall(proc_rtas_init);
302 static int parse_number(const char __user *p, size_t count, unsigned long *val)
310 if (copy_from_user(buf, p, count))
315 *val = simple_strtoul(buf, &end, 10);
316 if (*end && *end != '\n')
322 /* ****************************************************************** */
324 /* ****************************************************************** */
325 static ssize_t ppc_rtas_poweron_write(struct file *file,
326 const char __user *buf, size_t count, loff_t *ppos)
329 unsigned long nowtime;
330 int error = parse_number(buf, count, &nowtime);
334 power_on_time = nowtime; /* save the time */
338 error = rtas_call(rtas_token("set-time-for-power-on"), 7, 1, NULL,
339 tm.tm_year, tm.tm_mon, tm.tm_mday,
340 tm.tm_hour, tm.tm_min, tm.tm_sec, 0 /* nano */);
342 printk(KERN_WARNING "error: setting poweron time returned: %s\n",
343 ppc_rtas_process_error(error));
346 /* ****************************************************************** */
347 static int ppc_rtas_poweron_show(struct seq_file *m, void *v)
349 if (power_on_time == 0)
350 seq_printf(m, "Power on time not set\n");
352 seq_printf(m, "%lu\n",power_on_time);
356 /* ****************************************************************** */
358 /* ****************************************************************** */
359 static ssize_t ppc_rtas_progress_write(struct file *file,
360 const char __user *buf, size_t count, loff_t *ppos)
364 if (count >= MAX_LINELENGTH)
365 count = MAX_LINELENGTH -1;
366 if (copy_from_user(progress_led, buf, count)) { /* save the string */
369 progress_led[count] = 0;
371 /* Lets see if the user passed hexdigits */
372 hex = simple_strtoul(progress_led, NULL, 10);
374 rtas_progress ((char *)progress_led, hex);
378 /* rtas_progress(" ", 0xffff);*/
380 /* ****************************************************************** */
381 static int ppc_rtas_progress_show(struct seq_file *m, void *v)
384 seq_printf(m, "%s\n", progress_led);
388 /* ****************************************************************** */
390 /* ****************************************************************** */
391 static ssize_t ppc_rtas_clock_write(struct file *file,
392 const char __user *buf, size_t count, loff_t *ppos)
395 unsigned long nowtime;
396 int error = parse_number(buf, count, &nowtime);
401 error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
402 tm.tm_year, tm.tm_mon, tm.tm_mday,
403 tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
405 printk(KERN_WARNING "error: setting the clock returned: %s\n",
406 ppc_rtas_process_error(error));
409 /* ****************************************************************** */
410 static int ppc_rtas_clock_show(struct seq_file *m, void *v)
413 int error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
416 printk(KERN_WARNING "error: reading the clock returned: %s\n",
417 ppc_rtas_process_error(error));
420 unsigned int year, mon, day, hour, min, sec;
421 year = ret[0]; mon = ret[1]; day = ret[2];
422 hour = ret[3]; min = ret[4]; sec = ret[5];
423 seq_printf(m, "%lu\n",
424 mktime(year, mon, day, hour, min, sec));
429 /* ****************************************************************** */
431 /* ****************************************************************** */
432 static int ppc_rtas_sensors_show(struct seq_file *m, void *v)
436 int get_sensor_state = rtas_token("get-sensor-state");
438 seq_printf(m, "RTAS (RunTime Abstraction Services) Sensor Information\n");
439 seq_printf(m, "Sensor\t\tValue\t\tCondition\tLocation\n");
440 seq_printf(m, "********************************************************\n");
442 if (ppc_rtas_find_all_sensors() != 0) {
443 seq_printf(m, "\nNo sensors are available\n");
447 for (i=0; i<sensors.quant; i++) {
448 struct individual_sensor *p = &sensors.sensor[i];
453 sprintf (rstr, SENSOR_PREFIX"%04d", p->token);
454 loc = (char *) get_property(rtas_node, rstr, &llen);
456 /* A sensor may have multiple instances */
457 for (j = 0, offs = 0; j <= p->quant; j++) {
458 error = rtas_call(get_sensor_state, 2, 2, &state,
461 ppc_rtas_process_sensor(m, p, state, error, loc);
464 offs += strlen(loc) + 1;
465 loc += strlen(loc) + 1;
474 /* ****************************************************************** */
476 static int ppc_rtas_find_all_sensors(void)
481 utmp = (unsigned int *) get_property(rtas_node, "rtas-sensors", &len);
483 printk (KERN_ERR "error: could not get rtas-sensors\n");
487 sensors.quant = len / 8; /* int + int */
489 for (i=0; i<sensors.quant; i++) {
490 sensors.sensor[i].token = *utmp++;
491 sensors.sensor[i].quant = *utmp++;
496 /* ****************************************************************** */
498 * Builds a string of what rtas returned
500 static char *ppc_rtas_process_error(int error)
503 case SENSOR_CRITICAL_HIGH:
504 return "(critical high)";
505 case SENSOR_WARNING_HIGH:
506 return "(warning high)";
509 case SENSOR_WARNING_LOW:
510 return "(warning low)";
511 case SENSOR_CRITICAL_LOW:
512 return "(critical low)";
515 case SENSOR_HW_ERROR:
516 return "(hardware error)";
519 case SENSOR_NOT_EXIST:
520 return "(non existent)";
521 case SENSOR_DR_ENTITY:
522 return "(dr entity removed)";
528 /* ****************************************************************** */
530 * Builds a string out of what the sensor said
533 static void ppc_rtas_process_sensor(struct seq_file *m,
534 struct individual_sensor *s, int state, int error, char *loc)
536 /* Defined return vales */
537 const char * key_switch[] = { "Off\t", "Normal\t", "Secure\t",
539 const char * enclosure_switch[] = { "Closed", "Open" };
540 const char * lid_status[] = { " ", "Open", "Closed" };
541 const char * power_source[] = { "AC\t", "Battery",
543 const char * battery_remaining[] = { "Very Low", "Low", "Mid", "High" };
544 const char * epow_sensor[] = {
545 "EPOW Reset", "Cooling warning", "Power warning",
546 "System shutdown", "System halt", "EPOW main enclosure",
548 const char * battery_cyclestate[] = { "None", "In progress",
550 const char * battery_charging[] = { "Charging", "Discharching",
552 const char * ibm_drconnector[] = { "Empty", "Present", "Unusable",
555 int have_strings = 0;
560 /* What kind of sensor do we have here? */
564 seq_printf(m, "Key switch:\t");
565 num_states = sizeof(key_switch) / sizeof(char *);
566 if (state < num_states) {
567 seq_printf(m, "%s\t", key_switch[state]);
571 case ENCLOSURE_SWITCH:
572 seq_printf(m, "Enclosure switch:\t");
573 num_states = sizeof(enclosure_switch) / sizeof(char *);
574 if (state < num_states) {
575 seq_printf(m, "%s\t",
576 enclosure_switch[state]);
581 seq_printf(m, "Temp. (C/F):\t");
585 seq_printf(m, "Lid status:\t");
586 num_states = sizeof(lid_status) / sizeof(char *);
587 if (state < num_states) {
588 seq_printf(m, "%s\t", lid_status[state]);
593 seq_printf(m, "Power source:\t");
594 num_states = sizeof(power_source) / sizeof(char *);
595 if (state < num_states) {
596 seq_printf(m, "%s\t",
597 power_source[state]);
601 case BATTERY_VOLTAGE:
602 seq_printf(m, "Battery voltage:\t");
604 case BATTERY_REMAINING:
605 seq_printf(m, "Battery remaining:\t");
606 num_states = sizeof(battery_remaining) / sizeof(char *);
607 if (state < num_states)
609 seq_printf(m, "%s\t",
610 battery_remaining[state]);
614 case BATTERY_PERCENTAGE:
615 seq_printf(m, "Battery percentage:\t");
618 seq_printf(m, "EPOW Sensor:\t");
619 num_states = sizeof(epow_sensor) / sizeof(char *);
620 if (state < num_states) {
621 seq_printf(m, "%s\t", epow_sensor[state]);
625 case BATTERY_CYCLESTATE:
626 seq_printf(m, "Battery cyclestate:\t");
627 num_states = sizeof(battery_cyclestate) /
629 if (state < num_states) {
630 seq_printf(m, "%s\t",
631 battery_cyclestate[state]);
635 case BATTERY_CHARGING:
636 seq_printf(m, "Battery Charging:\t");
637 num_states = sizeof(battery_charging) / sizeof(char *);
638 if (state < num_states) {
639 seq_printf(m, "%s\t",
640 battery_charging[state]);
644 case IBM_SURVEILLANCE:
645 seq_printf(m, "Surveillance:\t");
648 seq_printf(m, "Fan (rpm):\t");
651 seq_printf(m, "Voltage (mv):\t");
653 case IBM_DRCONNECTOR:
654 seq_printf(m, "DR connector:\t");
655 num_states = sizeof(ibm_drconnector) / sizeof(char *);
656 if (state < num_states) {
657 seq_printf(m, "%s\t",
658 ibm_drconnector[state]);
662 case IBM_POWERSUPPLY:
663 seq_printf(m, "Powersupply:\t");
666 seq_printf(m, "Unknown sensor (type %d), ignoring it\n",
672 if (have_strings == 0) {
674 seq_printf(m, "%4d /%4d\t", state, cel_to_fahr(state));
676 seq_printf(m, "%10d\t", state);
679 seq_printf(m, "%s\t", ppc_rtas_process_error(error));
680 get_location_code(m, s, loc);
684 /* ****************************************************************** */
686 static void check_location(struct seq_file *m, char *c)
690 seq_printf(m, "Planar #%c", c[1]);
693 seq_printf(m, "CPU #%c", c[1]);
696 seq_printf(m, "Fan #%c", c[1]);
698 case LOC_RACKMOUNTED:
699 seq_printf(m, "Rack #%c", c[1]);
702 seq_printf(m, "Voltage #%c", c[1]);
705 seq_printf(m, "LCD #%c", c[1]);
708 seq_printf(m, "- %c", c[1]);
711 seq_printf(m, "Unknown location");
717 /* ****************************************************************** */
720 * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
721 * the '.' may be an abbrevation
723 static void check_location_string(struct seq_file *m, char *c)
726 if (isalpha(*c) || *c == '.')
727 check_location(m, c);
728 else if (*c == '/' || *c == '-')
729 seq_printf(m, " at ");
735 /* ****************************************************************** */
737 static void get_location_code(struct seq_file *m, struct individual_sensor *s, char *loc)
740 seq_printf(m, "---");/* does not have a location */
742 check_location_string(m, loc);
746 /* ****************************************************************** */
747 /* INDICATORS - Tone Frequency */
748 /* ****************************************************************** */
749 static ssize_t ppc_rtas_tone_freq_write(struct file *file,
750 const char __user *buf, size_t count, loff_t *ppos)
753 int error = parse_number(buf, count, &freq);
757 rtas_tone_frequency = freq; /* save it for later */
758 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
759 TONE_FREQUENCY, 0, freq);
761 printk(KERN_WARNING "error: setting tone frequency returned: %s\n",
762 ppc_rtas_process_error(error));
765 /* ****************************************************************** */
766 static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v)
768 seq_printf(m, "%lu\n", rtas_tone_frequency);
771 /* ****************************************************************** */
772 /* INDICATORS - Tone Volume */
773 /* ****************************************************************** */
774 static ssize_t ppc_rtas_tone_volume_write(struct file *file,
775 const char __user *buf, size_t count, loff_t *ppos)
777 unsigned long volume;
778 int error = parse_number(buf, count, &volume);
785 rtas_tone_volume = volume; /* save it for later */
786 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
787 TONE_VOLUME, 0, volume);
789 printk(KERN_WARNING "error: setting tone volume returned: %s\n",
790 ppc_rtas_process_error(error));
793 /* ****************************************************************** */
794 static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)
796 seq_printf(m, "%lu\n", rtas_tone_volume);
800 #define RMO_READ_BUF_MAX 30
802 /* RTAS Userspace access */
803 static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v)
805 seq_printf(m, "%016lx %x\n", rtas_rmo_buf, RTAS_RMOBUF_MAX);