Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2000 Tilmann Bitterberg
4 * (tilmann@bitterberg.de)
5 *
6 * RTAS (Runtime Abstraction Services) stuff
7 * Intention is to provide a clean user interface
8 * to use the RTAS.
9 *
10 * TODO:
11 * Split off a header file and maybe move it to a different
12 * location. Write Documentation on what the /proc/rtas/ entries
13 * actually do.
14 */
15
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>
27
28#include <linux/uaccess.h>
29#include <asm/processor.h>
30#include <asm/io.h>
31#include <asm/prom.h>
32#include <asm/rtas.h>
33#include <asm/machdep.h> /* for ppc_md */
34#include <asm/time.h>
35
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
48
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 */
55
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
67
68/* Location Codes */
69#define LOC_SCSI_DEV_ADDR 'A'
70#define LOC_SCSI_DEV_LOC 'B'
71#define LOC_CPU 'C'
72#define LOC_DISKETTE 'D'
73#define LOC_ETHERNET 'E'
74#define LOC_FAN 'F'
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'
80#define LOC_LCD 'L'
81#define LOC_MEMORY 'M'
82#define LOC_NV_MEMORY 'N'
83#define LOC_MOUSE 'O'
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'
92#define LOC_OTHER 'X'
93#define LOC_FIRMWARE 'Y'
94#define LOC_SCSI 'Z'
95
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 */
110
111/* other */
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)
116
117struct individual_sensor {
118 unsigned int token;
119 unsigned int quant;
120};
121
122struct rtas_sensors {
123 struct individual_sensor sensor[MAX_SENSORS];
124 unsigned int quant;
125};
126
127/* Globals */
128static struct rtas_sensors sensors;
129static struct device_node *rtas_node = NULL;
130static unsigned long power_on_time = 0; /* Save the time the user set */
131static char progress_led[MAX_LINELENGTH];
132
133static unsigned long rtas_tone_frequency = 1000;
134static unsigned long rtas_tone_volume = 0;
135
136/* ****************************************************************** */
137/* Declarations */
138static int ppc_rtas_sensors_show(struct seq_file *m, void *v);
139static int ppc_rtas_clock_show(struct seq_file *m, void *v);
140static ssize_t ppc_rtas_clock_write(struct file *file,
141 const char __user *buf, size_t count, loff_t *ppos);
142static int ppc_rtas_progress_show(struct seq_file *m, void *v);
143static ssize_t ppc_rtas_progress_write(struct file *file,
144 const char __user *buf, size_t count, loff_t *ppos);
145static int ppc_rtas_poweron_show(struct seq_file *m, void *v);
146static ssize_t ppc_rtas_poweron_write(struct file *file,
147 const char __user *buf, size_t count, loff_t *ppos);
148
149static ssize_t ppc_rtas_tone_freq_write(struct file *file,
150 const char __user *buf, size_t count, loff_t *ppos);
151static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v);
152static ssize_t ppc_rtas_tone_volume_write(struct file *file,
153 const char __user *buf, size_t count, loff_t *ppos);
154static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v);
155static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v);
156
157static int sensors_open(struct inode *inode, struct file *file)
158{
159 return single_open(file, ppc_rtas_sensors_show, NULL);
160}
161
162static const struct file_operations ppc_rtas_sensors_operations = {
163 .open = sensors_open,
164 .read = seq_read,
165 .llseek = seq_lseek,
166 .release = single_release,
167};
168
169static int poweron_open(struct inode *inode, struct file *file)
170{
171 return single_open(file, ppc_rtas_poweron_show, NULL);
172}
173
174static const struct file_operations ppc_rtas_poweron_operations = {
175 .open = poweron_open,
176 .read = seq_read,
177 .llseek = seq_lseek,
178 .write = ppc_rtas_poweron_write,
179 .release = single_release,
180};
181
182static int progress_open(struct inode *inode, struct file *file)
183{
184 return single_open(file, ppc_rtas_progress_show, NULL);
185}
186
187static const struct file_operations ppc_rtas_progress_operations = {
188 .open = progress_open,
189 .read = seq_read,
190 .llseek = seq_lseek,
191 .write = ppc_rtas_progress_write,
192 .release = single_release,
193};
194
195static int clock_open(struct inode *inode, struct file *file)
196{
197 return single_open(file, ppc_rtas_clock_show, NULL);
198}
199
200static const struct file_operations ppc_rtas_clock_operations = {
201 .open = clock_open,
202 .read = seq_read,
203 .llseek = seq_lseek,
204 .write = ppc_rtas_clock_write,
205 .release = single_release,
206};
207
208static int tone_freq_open(struct inode *inode, struct file *file)
209{
210 return single_open(file, ppc_rtas_tone_freq_show, NULL);
211}
212
213static const struct file_operations ppc_rtas_tone_freq_operations = {
214 .open = tone_freq_open,
215 .read = seq_read,
216 .llseek = seq_lseek,
217 .write = ppc_rtas_tone_freq_write,
218 .release = single_release,
219};
220
221static int tone_volume_open(struct inode *inode, struct file *file)
222{
223 return single_open(file, ppc_rtas_tone_volume_show, NULL);
224}
225
226static const struct file_operations ppc_rtas_tone_volume_operations = {
227 .open = tone_volume_open,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .write = ppc_rtas_tone_volume_write,
231 .release = single_release,
232};
233
234static int rmo_buf_open(struct inode *inode, struct file *file)
235{
236 return single_open(file, ppc_rtas_rmo_buf_show, NULL);
237}
238
239static const struct file_operations ppc_rtas_rmo_buf_ops = {
240 .open = rmo_buf_open,
241 .read = seq_read,
242 .llseek = seq_lseek,
243 .release = single_release,
244};
245
246static int ppc_rtas_find_all_sensors(void);
247static void ppc_rtas_process_sensor(struct seq_file *m,
248 struct individual_sensor *s, int state, int error, const char *loc);
249static char *ppc_rtas_process_error(int error);
250static void get_location_code(struct seq_file *m,
251 struct individual_sensor *s, const char *loc);
252static void check_location_string(struct seq_file *m, const char *c);
253static void check_location(struct seq_file *m, const char *c);
254
255static int __init proc_rtas_init(void)
256{
257 if (!machine_is(pseries))
258 return -ENODEV;
259
260 rtas_node = of_find_node_by_name(NULL, "rtas");
261 if (rtas_node == NULL)
262 return -ENODEV;
263
264 proc_create("powerpc/rtas/progress", 0644, NULL,
265 &ppc_rtas_progress_operations);
266 proc_create("powerpc/rtas/clock", 0644, NULL,
267 &ppc_rtas_clock_operations);
268 proc_create("powerpc/rtas/poweron", 0644, NULL,
269 &ppc_rtas_poweron_operations);
270 proc_create("powerpc/rtas/sensors", 0444, NULL,
271 &ppc_rtas_sensors_operations);
272 proc_create("powerpc/rtas/frequency", 0644, NULL,
273 &ppc_rtas_tone_freq_operations);
274 proc_create("powerpc/rtas/volume", 0644, NULL,
275 &ppc_rtas_tone_volume_operations);
276 proc_create("powerpc/rtas/rmo_buffer", 0400, NULL,
277 &ppc_rtas_rmo_buf_ops);
278 return 0;
279}
280
281__initcall(proc_rtas_init);
282
283static int parse_number(const char __user *p, size_t count, unsigned long *val)
284{
285 char buf[40];
286 char *end;
287
288 if (count > 39)
289 return -EINVAL;
290
291 if (copy_from_user(buf, p, count))
292 return -EFAULT;
293
294 buf[count] = 0;
295
296 *val = simple_strtoul(buf, &end, 10);
297 if (*end && *end != '\n')
298 return -EINVAL;
299
300 return 0;
301}
302
303/* ****************************************************************** */
304/* POWER-ON-TIME */
305/* ****************************************************************** */
306static ssize_t ppc_rtas_poweron_write(struct file *file,
307 const char __user *buf, size_t count, loff_t *ppos)
308{
309 struct rtc_time tm;
310 unsigned long nowtime;
311 int error = parse_number(buf, count, &nowtime);
312 if (error)
313 return error;
314
315 power_on_time = nowtime; /* save the time */
316
317 to_tm(nowtime, &tm);
318
319 error = rtas_call(rtas_token("set-time-for-power-on"), 7, 1, NULL,
320 tm.tm_year, tm.tm_mon, tm.tm_mday,
321 tm.tm_hour, tm.tm_min, tm.tm_sec, 0 /* nano */);
322 if (error)
323 printk(KERN_WARNING "error: setting poweron time returned: %s\n",
324 ppc_rtas_process_error(error));
325 return count;
326}
327/* ****************************************************************** */
328static int ppc_rtas_poweron_show(struct seq_file *m, void *v)
329{
330 if (power_on_time == 0)
331 seq_printf(m, "Power on time not set\n");
332 else
333 seq_printf(m, "%lu\n",power_on_time);
334 return 0;
335}
336
337/* ****************************************************************** */
338/* PROGRESS */
339/* ****************************************************************** */
340static ssize_t ppc_rtas_progress_write(struct file *file,
341 const char __user *buf, size_t count, loff_t *ppos)
342{
343 unsigned long hex;
344
345 if (count >= MAX_LINELENGTH)
346 count = MAX_LINELENGTH -1;
347 if (copy_from_user(progress_led, buf, count)) { /* save the string */
348 return -EFAULT;
349 }
350 progress_led[count] = 0;
351
352 /* Lets see if the user passed hexdigits */
353 hex = simple_strtoul(progress_led, NULL, 10);
354
355 rtas_progress ((char *)progress_led, hex);
356 return count;
357
358 /* clear the line */
359 /* rtas_progress(" ", 0xffff);*/
360}
361/* ****************************************************************** */
362static int ppc_rtas_progress_show(struct seq_file *m, void *v)
363{
364 if (progress_led[0])
365 seq_printf(m, "%s\n", progress_led);
366 return 0;
367}
368
369/* ****************************************************************** */
370/* CLOCK */
371/* ****************************************************************** */
372static ssize_t ppc_rtas_clock_write(struct file *file,
373 const char __user *buf, size_t count, loff_t *ppos)
374{
375 struct rtc_time tm;
376 unsigned long nowtime;
377 int error = parse_number(buf, count, &nowtime);
378 if (error)
379 return error;
380
381 to_tm(nowtime, &tm);
382 error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
383 tm.tm_year, tm.tm_mon, tm.tm_mday,
384 tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
385 if (error)
386 printk(KERN_WARNING "error: setting the clock returned: %s\n",
387 ppc_rtas_process_error(error));
388 return count;
389}
390/* ****************************************************************** */
391static int ppc_rtas_clock_show(struct seq_file *m, void *v)
392{
393 int ret[8];
394 int error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
395
396 if (error) {
397 printk(KERN_WARNING "error: reading the clock returned: %s\n",
398 ppc_rtas_process_error(error));
399 seq_printf(m, "0");
400 } else {
401 unsigned int year, mon, day, hour, min, sec;
402 year = ret[0]; mon = ret[1]; day = ret[2];
403 hour = ret[3]; min = ret[4]; sec = ret[5];
404 seq_printf(m, "%lu\n",
405 mktime(year, mon, day, hour, min, sec));
406 }
407 return 0;
408}
409
410/* ****************************************************************** */
411/* SENSOR STUFF */
412/* ****************************************************************** */
413static int ppc_rtas_sensors_show(struct seq_file *m, void *v)
414{
415 int i,j;
416 int state, error;
417 int get_sensor_state = rtas_token("get-sensor-state");
418
419 seq_printf(m, "RTAS (RunTime Abstraction Services) Sensor Information\n");
420 seq_printf(m, "Sensor\t\tValue\t\tCondition\tLocation\n");
421 seq_printf(m, "********************************************************\n");
422
423 if (ppc_rtas_find_all_sensors() != 0) {
424 seq_printf(m, "\nNo sensors are available\n");
425 return 0;
426 }
427
428 for (i=0; i<sensors.quant; i++) {
429 struct individual_sensor *p = &sensors.sensor[i];
430 char rstr[64];
431 const char *loc;
432 int llen, offs;
433
434 sprintf (rstr, SENSOR_PREFIX"%04d", p->token);
435 loc = of_get_property(rtas_node, rstr, &llen);
436
437 /* A sensor may have multiple instances */
438 for (j = 0, offs = 0; j <= p->quant; j++) {
439 error = rtas_call(get_sensor_state, 2, 2, &state,
440 p->token, j);
441
442 ppc_rtas_process_sensor(m, p, state, error, loc);
443 seq_putc(m, '\n');
444 if (loc) {
445 offs += strlen(loc) + 1;
446 loc += strlen(loc) + 1;
447 if (offs >= llen)
448 loc = NULL;
449 }
450 }
451 }
452 return 0;
453}
454
455/* ****************************************************************** */
456
457static int ppc_rtas_find_all_sensors(void)
458{
459 const unsigned int *utmp;
460 int len, i;
461
462 utmp = of_get_property(rtas_node, "rtas-sensors", &len);
463 if (utmp == NULL) {
464 printk (KERN_ERR "error: could not get rtas-sensors\n");
465 return 1;
466 }
467
468 sensors.quant = len / 8; /* int + int */
469
470 for (i=0; i<sensors.quant; i++) {
471 sensors.sensor[i].token = *utmp++;
472 sensors.sensor[i].quant = *utmp++;
473 }
474 return 0;
475}
476
477/* ****************************************************************** */
478/*
479 * Builds a string of what rtas returned
480 */
481static char *ppc_rtas_process_error(int error)
482{
483 switch (error) {
484 case SENSOR_CRITICAL_HIGH:
485 return "(critical high)";
486 case SENSOR_WARNING_HIGH:
487 return "(warning high)";
488 case SENSOR_NORMAL:
489 return "(normal)";
490 case SENSOR_WARNING_LOW:
491 return "(warning low)";
492 case SENSOR_CRITICAL_LOW:
493 return "(critical low)";
494 case SENSOR_SUCCESS:
495 return "(read ok)";
496 case SENSOR_HW_ERROR:
497 return "(hardware error)";
498 case SENSOR_BUSY:
499 return "(busy)";
500 case SENSOR_NOT_EXIST:
501 return "(non existent)";
502 case SENSOR_DR_ENTITY:
503 return "(dr entity removed)";
504 default:
505 return "(UNKNOWN)";
506 }
507}
508
509/* ****************************************************************** */
510/*
511 * Builds a string out of what the sensor said
512 */
513
514static void ppc_rtas_process_sensor(struct seq_file *m,
515 struct individual_sensor *s, int state, int error, const char *loc)
516{
517 /* Defined return vales */
518 const char * key_switch[] = { "Off\t", "Normal\t", "Secure\t",
519 "Maintenance" };
520 const char * enclosure_switch[] = { "Closed", "Open" };
521 const char * lid_status[] = { " ", "Open", "Closed" };
522 const char * power_source[] = { "AC\t", "Battery",
523 "AC & Battery" };
524 const char * battery_remaining[] = { "Very Low", "Low", "Mid", "High" };
525 const char * epow_sensor[] = {
526 "EPOW Reset", "Cooling warning", "Power warning",
527 "System shutdown", "System halt", "EPOW main enclosure",
528 "EPOW power off" };
529 const char * battery_cyclestate[] = { "None", "In progress",
530 "Requested" };
531 const char * battery_charging[] = { "Charging", "Discharching",
532 "No current flow" };
533 const char * ibm_drconnector[] = { "Empty", "Present", "Unusable",
534 "Exchange" };
535
536 int have_strings = 0;
537 int num_states = 0;
538 int temperature = 0;
539 int unknown = 0;
540
541 /* What kind of sensor do we have here? */
542
543 switch (s->token) {
544 case KEY_SWITCH:
545 seq_printf(m, "Key switch:\t");
546 num_states = sizeof(key_switch) / sizeof(char *);
547 if (state < num_states) {
548 seq_printf(m, "%s\t", key_switch[state]);
549 have_strings = 1;
550 }
551 break;
552 case ENCLOSURE_SWITCH:
553 seq_printf(m, "Enclosure switch:\t");
554 num_states = sizeof(enclosure_switch) / sizeof(char *);
555 if (state < num_states) {
556 seq_printf(m, "%s\t",
557 enclosure_switch[state]);
558 have_strings = 1;
559 }
560 break;
561 case THERMAL_SENSOR:
562 seq_printf(m, "Temp. (C/F):\t");
563 temperature = 1;
564 break;
565 case LID_STATUS:
566 seq_printf(m, "Lid status:\t");
567 num_states = sizeof(lid_status) / sizeof(char *);
568 if (state < num_states) {
569 seq_printf(m, "%s\t", lid_status[state]);
570 have_strings = 1;
571 }
572 break;
573 case POWER_SOURCE:
574 seq_printf(m, "Power source:\t");
575 num_states = sizeof(power_source) / sizeof(char *);
576 if (state < num_states) {
577 seq_printf(m, "%s\t",
578 power_source[state]);
579 have_strings = 1;
580 }
581 break;
582 case BATTERY_VOLTAGE:
583 seq_printf(m, "Battery voltage:\t");
584 break;
585 case BATTERY_REMAINING:
586 seq_printf(m, "Battery remaining:\t");
587 num_states = sizeof(battery_remaining) / sizeof(char *);
588 if (state < num_states)
589 {
590 seq_printf(m, "%s\t",
591 battery_remaining[state]);
592 have_strings = 1;
593 }
594 break;
595 case BATTERY_PERCENTAGE:
596 seq_printf(m, "Battery percentage:\t");
597 break;
598 case EPOW_SENSOR:
599 seq_printf(m, "EPOW Sensor:\t");
600 num_states = sizeof(epow_sensor) / sizeof(char *);
601 if (state < num_states) {
602 seq_printf(m, "%s\t", epow_sensor[state]);
603 have_strings = 1;
604 }
605 break;
606 case BATTERY_CYCLESTATE:
607 seq_printf(m, "Battery cyclestate:\t");
608 num_states = sizeof(battery_cyclestate) /
609 sizeof(char *);
610 if (state < num_states) {
611 seq_printf(m, "%s\t",
612 battery_cyclestate[state]);
613 have_strings = 1;
614 }
615 break;
616 case BATTERY_CHARGING:
617 seq_printf(m, "Battery Charging:\t");
618 num_states = sizeof(battery_charging) / sizeof(char *);
619 if (state < num_states) {
620 seq_printf(m, "%s\t",
621 battery_charging[state]);
622 have_strings = 1;
623 }
624 break;
625 case IBM_SURVEILLANCE:
626 seq_printf(m, "Surveillance:\t");
627 break;
628 case IBM_FANRPM:
629 seq_printf(m, "Fan (rpm):\t");
630 break;
631 case IBM_VOLTAGE:
632 seq_printf(m, "Voltage (mv):\t");
633 break;
634 case IBM_DRCONNECTOR:
635 seq_printf(m, "DR connector:\t");
636 num_states = sizeof(ibm_drconnector) / sizeof(char *);
637 if (state < num_states) {
638 seq_printf(m, "%s\t",
639 ibm_drconnector[state]);
640 have_strings = 1;
641 }
642 break;
643 case IBM_POWERSUPPLY:
644 seq_printf(m, "Powersupply:\t");
645 break;
646 default:
647 seq_printf(m, "Unknown sensor (type %d), ignoring it\n",
648 s->token);
649 unknown = 1;
650 have_strings = 1;
651 break;
652 }
653 if (have_strings == 0) {
654 if (temperature) {
655 seq_printf(m, "%4d /%4d\t", state, cel_to_fahr(state));
656 } else
657 seq_printf(m, "%10d\t", state);
658 }
659 if (unknown == 0) {
660 seq_printf(m, "%s\t", ppc_rtas_process_error(error));
661 get_location_code(m, s, loc);
662 }
663}
664
665/* ****************************************************************** */
666
667static void check_location(struct seq_file *m, const char *c)
668{
669 switch (c[0]) {
670 case LOC_PLANAR:
671 seq_printf(m, "Planar #%c", c[1]);
672 break;
673 case LOC_CPU:
674 seq_printf(m, "CPU #%c", c[1]);
675 break;
676 case LOC_FAN:
677 seq_printf(m, "Fan #%c", c[1]);
678 break;
679 case LOC_RACKMOUNTED:
680 seq_printf(m, "Rack #%c", c[1]);
681 break;
682 case LOC_VOLTAGE:
683 seq_printf(m, "Voltage #%c", c[1]);
684 break;
685 case LOC_LCD:
686 seq_printf(m, "LCD #%c", c[1]);
687 break;
688 case '.':
689 seq_printf(m, "- %c", c[1]);
690 break;
691 default:
692 seq_printf(m, "Unknown location");
693 break;
694 }
695}
696
697
698/* ****************************************************************** */
699/*
700 * Format:
701 * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
702 * the '.' may be an abbreviation
703 */
704static void check_location_string(struct seq_file *m, const char *c)
705{
706 while (*c) {
707 if (isalpha(*c) || *c == '.')
708 check_location(m, c);
709 else if (*c == '/' || *c == '-')
710 seq_printf(m, " at ");
711 c++;
712 }
713}
714
715
716/* ****************************************************************** */
717
718static void get_location_code(struct seq_file *m, struct individual_sensor *s,
719 const char *loc)
720{
721 if (!loc || !*loc) {
722 seq_printf(m, "---");/* does not have a location */
723 } else {
724 check_location_string(m, loc);
725 }
726 seq_putc(m, ' ');
727}
728/* ****************************************************************** */
729/* INDICATORS - Tone Frequency */
730/* ****************************************************************** */
731static ssize_t ppc_rtas_tone_freq_write(struct file *file,
732 const char __user *buf, size_t count, loff_t *ppos)
733{
734 unsigned long freq;
735 int error = parse_number(buf, count, &freq);
736 if (error)
737 return error;
738
739 rtas_tone_frequency = freq; /* save it for later */
740 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
741 TONE_FREQUENCY, 0, freq);
742 if (error)
743 printk(KERN_WARNING "error: setting tone frequency returned: %s\n",
744 ppc_rtas_process_error(error));
745 return count;
746}
747/* ****************************************************************** */
748static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v)
749{
750 seq_printf(m, "%lu\n", rtas_tone_frequency);
751 return 0;
752}
753/* ****************************************************************** */
754/* INDICATORS - Tone Volume */
755/* ****************************************************************** */
756static ssize_t ppc_rtas_tone_volume_write(struct file *file,
757 const char __user *buf, size_t count, loff_t *ppos)
758{
759 unsigned long volume;
760 int error = parse_number(buf, count, &volume);
761 if (error)
762 return error;
763
764 if (volume > 100)
765 volume = 100;
766
767 rtas_tone_volume = volume; /* save it for later */
768 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
769 TONE_VOLUME, 0, volume);
770 if (error)
771 printk(KERN_WARNING "error: setting tone volume returned: %s\n",
772 ppc_rtas_process_error(error));
773 return count;
774}
775/* ****************************************************************** */
776static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)
777{
778 seq_printf(m, "%lu\n", rtas_tone_volume);
779 return 0;
780}
781
782#define RMO_READ_BUF_MAX 30
783
784/* RTAS Userspace access */
785static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v)
786{
787 seq_printf(m, "%016lx %x\n", rtas_rmo_buf, RTAS_RMOBUF_MAX);
788 return 0;
789}
1/*
2 * Copyright (C) 2000 Tilmann Bitterberg
3 * (tilmann@bitterberg.de)
4 *
5 * RTAS (Runtime Abstraction Services) stuff
6 * Intention is to provide a clean user interface
7 * to use the RTAS.
8 *
9 * TODO:
10 * Split off a header file and maybe move it to a different
11 * location. Write Documentation on what the /proc/rtas/ entries
12 * actually do.
13 */
14
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>
26
27#include <asm/uaccess.h>
28#include <asm/processor.h>
29#include <asm/io.h>
30#include <asm/prom.h>
31#include <asm/rtas.h>
32#include <asm/machdep.h> /* for ppc_md */
33#include <asm/time.h>
34
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
47
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 */
54
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
66
67/* Location Codes */
68#define LOC_SCSI_DEV_ADDR 'A'
69#define LOC_SCSI_DEV_LOC 'B'
70#define LOC_CPU 'C'
71#define LOC_DISKETTE 'D'
72#define LOC_ETHERNET 'E'
73#define LOC_FAN 'F'
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'
79#define LOC_LCD 'L'
80#define LOC_MEMORY 'M'
81#define LOC_NV_MEMORY 'N'
82#define LOC_MOUSE 'O'
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'
91#define LOC_OTHER 'X'
92#define LOC_FIRMWARE 'Y'
93#define LOC_SCSI 'Z'
94
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 */
109
110/* other */
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)
115
116struct individual_sensor {
117 unsigned int token;
118 unsigned int quant;
119};
120
121struct rtas_sensors {
122 struct individual_sensor sensor[MAX_SENSORS];
123 unsigned int quant;
124};
125
126/* Globals */
127static struct rtas_sensors sensors;
128static struct device_node *rtas_node = NULL;
129static unsigned long power_on_time = 0; /* Save the time the user set */
130static char progress_led[MAX_LINELENGTH];
131
132static unsigned long rtas_tone_frequency = 1000;
133static unsigned long rtas_tone_volume = 0;
134
135/* ****************************************************************** */
136/* Declarations */
137static int ppc_rtas_sensors_show(struct seq_file *m, void *v);
138static int ppc_rtas_clock_show(struct seq_file *m, void *v);
139static ssize_t ppc_rtas_clock_write(struct file *file,
140 const char __user *buf, size_t count, loff_t *ppos);
141static int ppc_rtas_progress_show(struct seq_file *m, void *v);
142static ssize_t ppc_rtas_progress_write(struct file *file,
143 const char __user *buf, size_t count, loff_t *ppos);
144static int ppc_rtas_poweron_show(struct seq_file *m, void *v);
145static ssize_t ppc_rtas_poweron_write(struct file *file,
146 const char __user *buf, size_t count, loff_t *ppos);
147
148static ssize_t ppc_rtas_tone_freq_write(struct file *file,
149 const char __user *buf, size_t count, loff_t *ppos);
150static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v);
151static ssize_t ppc_rtas_tone_volume_write(struct file *file,
152 const char __user *buf, size_t count, loff_t *ppos);
153static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v);
154static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v);
155
156static int sensors_open(struct inode *inode, struct file *file)
157{
158 return single_open(file, ppc_rtas_sensors_show, NULL);
159}
160
161static const struct file_operations ppc_rtas_sensors_operations = {
162 .open = sensors_open,
163 .read = seq_read,
164 .llseek = seq_lseek,
165 .release = single_release,
166};
167
168static int poweron_open(struct inode *inode, struct file *file)
169{
170 return single_open(file, ppc_rtas_poweron_show, NULL);
171}
172
173static const struct file_operations ppc_rtas_poweron_operations = {
174 .open = poweron_open,
175 .read = seq_read,
176 .llseek = seq_lseek,
177 .write = ppc_rtas_poweron_write,
178 .release = single_release,
179};
180
181static int progress_open(struct inode *inode, struct file *file)
182{
183 return single_open(file, ppc_rtas_progress_show, NULL);
184}
185
186static const struct file_operations ppc_rtas_progress_operations = {
187 .open = progress_open,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .write = ppc_rtas_progress_write,
191 .release = single_release,
192};
193
194static int clock_open(struct inode *inode, struct file *file)
195{
196 return single_open(file, ppc_rtas_clock_show, NULL);
197}
198
199static const struct file_operations ppc_rtas_clock_operations = {
200 .open = clock_open,
201 .read = seq_read,
202 .llseek = seq_lseek,
203 .write = ppc_rtas_clock_write,
204 .release = single_release,
205};
206
207static int tone_freq_open(struct inode *inode, struct file *file)
208{
209 return single_open(file, ppc_rtas_tone_freq_show, NULL);
210}
211
212static const struct file_operations ppc_rtas_tone_freq_operations = {
213 .open = tone_freq_open,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .write = ppc_rtas_tone_freq_write,
217 .release = single_release,
218};
219
220static int tone_volume_open(struct inode *inode, struct file *file)
221{
222 return single_open(file, ppc_rtas_tone_volume_show, NULL);
223}
224
225static const struct file_operations ppc_rtas_tone_volume_operations = {
226 .open = tone_volume_open,
227 .read = seq_read,
228 .llseek = seq_lseek,
229 .write = ppc_rtas_tone_volume_write,
230 .release = single_release,
231};
232
233static int rmo_buf_open(struct inode *inode, struct file *file)
234{
235 return single_open(file, ppc_rtas_rmo_buf_show, NULL);
236}
237
238static const struct file_operations ppc_rtas_rmo_buf_ops = {
239 .open = rmo_buf_open,
240 .read = seq_read,
241 .llseek = seq_lseek,
242 .release = single_release,
243};
244
245static int ppc_rtas_find_all_sensors(void);
246static void ppc_rtas_process_sensor(struct seq_file *m,
247 struct individual_sensor *s, int state, int error, const char *loc);
248static char *ppc_rtas_process_error(int error);
249static void get_location_code(struct seq_file *m,
250 struct individual_sensor *s, const char *loc);
251static void check_location_string(struct seq_file *m, const char *c);
252static void check_location(struct seq_file *m, const char *c);
253
254static int __init proc_rtas_init(void)
255{
256 if (!machine_is(pseries))
257 return -ENODEV;
258
259 rtas_node = of_find_node_by_name(NULL, "rtas");
260 if (rtas_node == NULL)
261 return -ENODEV;
262
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);
277 return 0;
278}
279
280__initcall(proc_rtas_init);
281
282static int parse_number(const char __user *p, size_t count, unsigned long *val)
283{
284 char buf[40];
285 char *end;
286
287 if (count > 39)
288 return -EINVAL;
289
290 if (copy_from_user(buf, p, count))
291 return -EFAULT;
292
293 buf[count] = 0;
294
295 *val = simple_strtoul(buf, &end, 10);
296 if (*end && *end != '\n')
297 return -EINVAL;
298
299 return 0;
300}
301
302/* ****************************************************************** */
303/* POWER-ON-TIME */
304/* ****************************************************************** */
305static ssize_t ppc_rtas_poweron_write(struct file *file,
306 const char __user *buf, size_t count, loff_t *ppos)
307{
308 struct rtc_time tm;
309 unsigned long nowtime;
310 int error = parse_number(buf, count, &nowtime);
311 if (error)
312 return error;
313
314 power_on_time = nowtime; /* save the time */
315
316 to_tm(nowtime, &tm);
317
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 */);
321 if (error)
322 printk(KERN_WARNING "error: setting poweron time returned: %s\n",
323 ppc_rtas_process_error(error));
324 return count;
325}
326/* ****************************************************************** */
327static int ppc_rtas_poweron_show(struct seq_file *m, void *v)
328{
329 if (power_on_time == 0)
330 seq_printf(m, "Power on time not set\n");
331 else
332 seq_printf(m, "%lu\n",power_on_time);
333 return 0;
334}
335
336/* ****************************************************************** */
337/* PROGRESS */
338/* ****************************************************************** */
339static ssize_t ppc_rtas_progress_write(struct file *file,
340 const char __user *buf, size_t count, loff_t *ppos)
341{
342 unsigned long hex;
343
344 if (count >= MAX_LINELENGTH)
345 count = MAX_LINELENGTH -1;
346 if (copy_from_user(progress_led, buf, count)) { /* save the string */
347 return -EFAULT;
348 }
349 progress_led[count] = 0;
350
351 /* Lets see if the user passed hexdigits */
352 hex = simple_strtoul(progress_led, NULL, 10);
353
354 rtas_progress ((char *)progress_led, hex);
355 return count;
356
357 /* clear the line */
358 /* rtas_progress(" ", 0xffff);*/
359}
360/* ****************************************************************** */
361static int ppc_rtas_progress_show(struct seq_file *m, void *v)
362{
363 if (progress_led[0])
364 seq_printf(m, "%s\n", progress_led);
365 return 0;
366}
367
368/* ****************************************************************** */
369/* CLOCK */
370/* ****************************************************************** */
371static ssize_t ppc_rtas_clock_write(struct file *file,
372 const char __user *buf, size_t count, loff_t *ppos)
373{
374 struct rtc_time tm;
375 unsigned long nowtime;
376 int error = parse_number(buf, count, &nowtime);
377 if (error)
378 return error;
379
380 to_tm(nowtime, &tm);
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);
384 if (error)
385 printk(KERN_WARNING "error: setting the clock returned: %s\n",
386 ppc_rtas_process_error(error));
387 return count;
388}
389/* ****************************************************************** */
390static int ppc_rtas_clock_show(struct seq_file *m, void *v)
391{
392 int ret[8];
393 int error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
394
395 if (error) {
396 printk(KERN_WARNING "error: reading the clock returned: %s\n",
397 ppc_rtas_process_error(error));
398 seq_printf(m, "0");
399 } else {
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));
405 }
406 return 0;
407}
408
409/* ****************************************************************** */
410/* SENSOR STUFF */
411/* ****************************************************************** */
412static int ppc_rtas_sensors_show(struct seq_file *m, void *v)
413{
414 int i,j;
415 int state, error;
416 int get_sensor_state = rtas_token("get-sensor-state");
417
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");
421
422 if (ppc_rtas_find_all_sensors() != 0) {
423 seq_printf(m, "\nNo sensors are available\n");
424 return 0;
425 }
426
427 for (i=0; i<sensors.quant; i++) {
428 struct individual_sensor *p = &sensors.sensor[i];
429 char rstr[64];
430 const char *loc;
431 int llen, offs;
432
433 sprintf (rstr, SENSOR_PREFIX"%04d", p->token);
434 loc = of_get_property(rtas_node, rstr, &llen);
435
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,
439 p->token, j);
440
441 ppc_rtas_process_sensor(m, p, state, error, loc);
442 seq_putc(m, '\n');
443 if (loc) {
444 offs += strlen(loc) + 1;
445 loc += strlen(loc) + 1;
446 if (offs >= llen)
447 loc = NULL;
448 }
449 }
450 }
451 return 0;
452}
453
454/* ****************************************************************** */
455
456static int ppc_rtas_find_all_sensors(void)
457{
458 const unsigned int *utmp;
459 int len, i;
460
461 utmp = of_get_property(rtas_node, "rtas-sensors", &len);
462 if (utmp == NULL) {
463 printk (KERN_ERR "error: could not get rtas-sensors\n");
464 return 1;
465 }
466
467 sensors.quant = len / 8; /* int + int */
468
469 for (i=0; i<sensors.quant; i++) {
470 sensors.sensor[i].token = *utmp++;
471 sensors.sensor[i].quant = *utmp++;
472 }
473 return 0;
474}
475
476/* ****************************************************************** */
477/*
478 * Builds a string of what rtas returned
479 */
480static char *ppc_rtas_process_error(int error)
481{
482 switch (error) {
483 case SENSOR_CRITICAL_HIGH:
484 return "(critical high)";
485 case SENSOR_WARNING_HIGH:
486 return "(warning high)";
487 case SENSOR_NORMAL:
488 return "(normal)";
489 case SENSOR_WARNING_LOW:
490 return "(warning low)";
491 case SENSOR_CRITICAL_LOW:
492 return "(critical low)";
493 case SENSOR_SUCCESS:
494 return "(read ok)";
495 case SENSOR_HW_ERROR:
496 return "(hardware error)";
497 case SENSOR_BUSY:
498 return "(busy)";
499 case SENSOR_NOT_EXIST:
500 return "(non existent)";
501 case SENSOR_DR_ENTITY:
502 return "(dr entity removed)";
503 default:
504 return "(UNKNOWN)";
505 }
506}
507
508/* ****************************************************************** */
509/*
510 * Builds a string out of what the sensor said
511 */
512
513static void ppc_rtas_process_sensor(struct seq_file *m,
514 struct individual_sensor *s, int state, int error, const char *loc)
515{
516 /* Defined return vales */
517 const char * key_switch[] = { "Off\t", "Normal\t", "Secure\t",
518 "Maintenance" };
519 const char * enclosure_switch[] = { "Closed", "Open" };
520 const char * lid_status[] = { " ", "Open", "Closed" };
521 const char * power_source[] = { "AC\t", "Battery",
522 "AC & 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",
527 "EPOW power off" };
528 const char * battery_cyclestate[] = { "None", "In progress",
529 "Requested" };
530 const char * battery_charging[] = { "Charging", "Discharching",
531 "No current flow" };
532 const char * ibm_drconnector[] = { "Empty", "Present", "Unusable",
533 "Exchange" };
534
535 int have_strings = 0;
536 int num_states = 0;
537 int temperature = 0;
538 int unknown = 0;
539
540 /* What kind of sensor do we have here? */
541
542 switch (s->token) {
543 case KEY_SWITCH:
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]);
548 have_strings = 1;
549 }
550 break;
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]);
557 have_strings = 1;
558 }
559 break;
560 case THERMAL_SENSOR:
561 seq_printf(m, "Temp. (C/F):\t");
562 temperature = 1;
563 break;
564 case LID_STATUS:
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]);
569 have_strings = 1;
570 }
571 break;
572 case POWER_SOURCE:
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]);
578 have_strings = 1;
579 }
580 break;
581 case BATTERY_VOLTAGE:
582 seq_printf(m, "Battery voltage:\t");
583 break;
584 case BATTERY_REMAINING:
585 seq_printf(m, "Battery remaining:\t");
586 num_states = sizeof(battery_remaining) / sizeof(char *);
587 if (state < num_states)
588 {
589 seq_printf(m, "%s\t",
590 battery_remaining[state]);
591 have_strings = 1;
592 }
593 break;
594 case BATTERY_PERCENTAGE:
595 seq_printf(m, "Battery percentage:\t");
596 break;
597 case EPOW_SENSOR:
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]);
602 have_strings = 1;
603 }
604 break;
605 case BATTERY_CYCLESTATE:
606 seq_printf(m, "Battery cyclestate:\t");
607 num_states = sizeof(battery_cyclestate) /
608 sizeof(char *);
609 if (state < num_states) {
610 seq_printf(m, "%s\t",
611 battery_cyclestate[state]);
612 have_strings = 1;
613 }
614 break;
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]);
621 have_strings = 1;
622 }
623 break;
624 case IBM_SURVEILLANCE:
625 seq_printf(m, "Surveillance:\t");
626 break;
627 case IBM_FANRPM:
628 seq_printf(m, "Fan (rpm):\t");
629 break;
630 case IBM_VOLTAGE:
631 seq_printf(m, "Voltage (mv):\t");
632 break;
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]);
639 have_strings = 1;
640 }
641 break;
642 case IBM_POWERSUPPLY:
643 seq_printf(m, "Powersupply:\t");
644 break;
645 default:
646 seq_printf(m, "Unknown sensor (type %d), ignoring it\n",
647 s->token);
648 unknown = 1;
649 have_strings = 1;
650 break;
651 }
652 if (have_strings == 0) {
653 if (temperature) {
654 seq_printf(m, "%4d /%4d\t", state, cel_to_fahr(state));
655 } else
656 seq_printf(m, "%10d\t", state);
657 }
658 if (unknown == 0) {
659 seq_printf(m, "%s\t", ppc_rtas_process_error(error));
660 get_location_code(m, s, loc);
661 }
662}
663
664/* ****************************************************************** */
665
666static void check_location(struct seq_file *m, const char *c)
667{
668 switch (c[0]) {
669 case LOC_PLANAR:
670 seq_printf(m, "Planar #%c", c[1]);
671 break;
672 case LOC_CPU:
673 seq_printf(m, "CPU #%c", c[1]);
674 break;
675 case LOC_FAN:
676 seq_printf(m, "Fan #%c", c[1]);
677 break;
678 case LOC_RACKMOUNTED:
679 seq_printf(m, "Rack #%c", c[1]);
680 break;
681 case LOC_VOLTAGE:
682 seq_printf(m, "Voltage #%c", c[1]);
683 break;
684 case LOC_LCD:
685 seq_printf(m, "LCD #%c", c[1]);
686 break;
687 case '.':
688 seq_printf(m, "- %c", c[1]);
689 break;
690 default:
691 seq_printf(m, "Unknown location");
692 break;
693 }
694}
695
696
697/* ****************************************************************** */
698/*
699 * Format:
700 * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
701 * the '.' may be an abbrevation
702 */
703static void check_location_string(struct seq_file *m, const char *c)
704{
705 while (*c) {
706 if (isalpha(*c) || *c == '.')
707 check_location(m, c);
708 else if (*c == '/' || *c == '-')
709 seq_printf(m, " at ");
710 c++;
711 }
712}
713
714
715/* ****************************************************************** */
716
717static void get_location_code(struct seq_file *m, struct individual_sensor *s,
718 const char *loc)
719{
720 if (!loc || !*loc) {
721 seq_printf(m, "---");/* does not have a location */
722 } else {
723 check_location_string(m, loc);
724 }
725 seq_putc(m, ' ');
726}
727/* ****************************************************************** */
728/* INDICATORS - Tone Frequency */
729/* ****************************************************************** */
730static ssize_t ppc_rtas_tone_freq_write(struct file *file,
731 const char __user *buf, size_t count, loff_t *ppos)
732{
733 unsigned long freq;
734 int error = parse_number(buf, count, &freq);
735 if (error)
736 return error;
737
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);
741 if (error)
742 printk(KERN_WARNING "error: setting tone frequency returned: %s\n",
743 ppc_rtas_process_error(error));
744 return count;
745}
746/* ****************************************************************** */
747static int ppc_rtas_tone_freq_show(struct seq_file *m, void *v)
748{
749 seq_printf(m, "%lu\n", rtas_tone_frequency);
750 return 0;
751}
752/* ****************************************************************** */
753/* INDICATORS - Tone Volume */
754/* ****************************************************************** */
755static ssize_t ppc_rtas_tone_volume_write(struct file *file,
756 const char __user *buf, size_t count, loff_t *ppos)
757{
758 unsigned long volume;
759 int error = parse_number(buf, count, &volume);
760 if (error)
761 return error;
762
763 if (volume > 100)
764 volume = 100;
765
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);
769 if (error)
770 printk(KERN_WARNING "error: setting tone volume returned: %s\n",
771 ppc_rtas_process_error(error));
772 return count;
773}
774/* ****************************************************************** */
775static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)
776{
777 seq_printf(m, "%lu\n", rtas_tone_volume);
778 return 0;
779}
780
781#define RMO_READ_BUF_MAX 30
782
783/* RTAS Userspace access */
784static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v)
785{
786 seq_printf(m, "%016lx %x\n", rtas_rmo_buf, RTAS_RMOBUF_MAX);
787 return 0;
788}