Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / arch / ppc64 / kernel / rtas-proc.c
1 /*
2  *   arch/ppc64/kernel/rtas-proc.c
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
25 #include <asm/uaccess.h>
26 #include <asm/bitops.h>
27 #include <asm/processor.h>
28 #include <asm/io.h>
29 #include <asm/prom.h>
30 #include <asm/rtas.h>
31 #include <asm/machdep.h> /* for ppc_md */
32 #include <asm/time.h>
33
34 /* Token for Sensors */
35 #define KEY_SWITCH              0x0001
36 #define ENCLOSURE_SWITCH        0x0002
37 #define THERMAL_SENSOR          0x0003
38 #define LID_STATUS              0x0004
39 #define POWER_SOURCE            0x0005
40 #define BATTERY_VOLTAGE         0x0006
41 #define BATTERY_REMAINING       0x0007
42 #define BATTERY_PERCENTAGE      0x0008
43 #define EPOW_SENSOR             0x0009
44 #define BATTERY_CYCLESTATE      0x000a
45 #define BATTERY_CHARGING        0x000b
46
47 /* IBM specific sensors */
48 #define IBM_SURVEILLANCE        0x2328 /* 9000 */
49 #define IBM_FANRPM              0x2329 /* 9001 */
50 #define IBM_VOLTAGE             0x232a /* 9002 */
51 #define IBM_DRCONNECTOR         0x232b /* 9003 */
52 #define IBM_POWERSUPPLY         0x232c /* 9004 */
53 #define IBM_INTQUEUE            0x232d /* 9005 */
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 #define GLOBAL_INTERRUPT_QUEUE  0x232d /* 9005 */
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
117
118 /* Globals */
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];
123
124 static unsigned long rtas_tone_frequency = 1000;
125 static unsigned long rtas_tone_volume = 0;
126
127 /* ****************STRUCTS******************************************* */
128 struct individual_sensor {
129         unsigned int token;
130         unsigned int quant;
131 };
132
133 struct rtas_sensors {
134         struct individual_sensor sensor[MAX_SENSORS];
135         unsigned int quant;
136 };
137
138 /* ****************************************************************** */
139 /* Declarations */
140 static int ppc_rtas_sensor_read(char * buf, char ** start, off_t off,
141                 int count, int *eof, void *data);
142 static ssize_t ppc_rtas_clock_read(struct file * file, char * buf, 
143                 size_t count, loff_t *ppos);
144 static ssize_t ppc_rtas_clock_write(struct file * file, const char * buf, 
145                 size_t count, loff_t *ppos);
146 static ssize_t ppc_rtas_progress_read(struct file * file, char * buf,
147                 size_t count, loff_t *ppos);
148 static ssize_t ppc_rtas_progress_write(struct file * file, const char * buf,
149                 size_t count, loff_t *ppos);
150 static ssize_t ppc_rtas_poweron_read(struct file * file, char * buf,
151                 size_t count, loff_t *ppos);
152 static ssize_t ppc_rtas_poweron_write(struct file * file, const char * buf,
153                 size_t count, loff_t *ppos);
154
155 static ssize_t ppc_rtas_tone_freq_write(struct file * file, const char * buf,
156                 size_t count, loff_t *ppos);
157 static ssize_t ppc_rtas_tone_freq_read(struct file * file, char * buf,
158                 size_t count, loff_t *ppos);
159 static ssize_t ppc_rtas_tone_volume_write(struct file * file, const char * buf,
160                 size_t count, loff_t *ppos);
161 static ssize_t ppc_rtas_tone_volume_read(struct file * file, char * buf,
162                 size_t count, loff_t *ppos);
163 static ssize_t ppc_rtas_rmo_buf_read(struct file *file, char *buf,
164                                     size_t count, loff_t *ppos);
165
166 struct file_operations ppc_rtas_poweron_operations = {
167         .read =         ppc_rtas_poweron_read,
168         .write =        ppc_rtas_poweron_write
169 };
170 struct file_operations ppc_rtas_progress_operations = {
171         .read =         ppc_rtas_progress_read,
172         .write =        ppc_rtas_progress_write
173 };
174
175 struct file_operations ppc_rtas_clock_operations = {
176         .read =         ppc_rtas_clock_read,
177         .write =        ppc_rtas_clock_write
178 };
179
180 struct file_operations ppc_rtas_tone_freq_operations = {
181         .read =         ppc_rtas_tone_freq_read,
182         .write =        ppc_rtas_tone_freq_write
183 };
184 struct file_operations ppc_rtas_tone_volume_operations = {
185         .read =         ppc_rtas_tone_volume_read,
186         .write =        ppc_rtas_tone_volume_write
187 };
188
189 static struct file_operations ppc_rtas_rmo_buf_ops = {
190         .read =         ppc_rtas_rmo_buf_read,
191 };
192
193 int ppc_rtas_find_all_sensors (void);
194 int ppc_rtas_process_sensor(struct individual_sensor s, int state, 
195                 int error, char * buf);
196 char * ppc_rtas_process_error(int error);
197 int get_location_code(struct individual_sensor s, char * buf);
198 int check_location_string (char *c, char * buf);
199 int check_location (char *c, int idx, char * buf);
200
201 static int __init proc_rtas_init(void)
202 {
203         struct proc_dir_entry *entry;
204
205         if (!(systemcfg->platform & PLATFORM_PSERIES))
206                 return 1;
207
208         rtas_node = of_find_node_by_name(NULL, "rtas");
209         if (rtas_node == NULL)
210                 return 1;
211
212         entry = create_proc_entry("ppc64/rtas/progress", S_IRUGO|S_IWUSR, NULL);
213         if (entry)
214                 entry->proc_fops = &ppc_rtas_progress_operations;
215
216         entry = create_proc_entry("ppc64/rtas/clock", S_IRUGO|S_IWUSR, NULL);
217         if (entry)
218                 entry->proc_fops = &ppc_rtas_clock_operations;
219
220         entry = create_proc_entry("ppc64/rtas/poweron", S_IWUSR|S_IRUGO, NULL);
221         if (entry)
222                 entry->proc_fops = &ppc_rtas_poweron_operations;
223
224         create_proc_read_entry("ppc64/rtas/sensors", S_IRUGO, NULL,
225                                ppc_rtas_sensor_read, NULL);
226
227         entry = create_proc_entry("ppc64/rtas/frequency", S_IWUSR|S_IRUGO,
228                                   NULL);
229         if (entry)
230                 entry->proc_fops = &ppc_rtas_tone_freq_operations;
231
232         entry = create_proc_entry("ppc64/rtas/volume", S_IWUSR|S_IRUGO, NULL);
233         if (entry)
234                 entry->proc_fops = &ppc_rtas_tone_volume_operations;
235
236         entry = create_proc_entry("ppc64/rtas/rmo_buffer", S_IRUSR, NULL);
237         if (entry)
238                 entry->proc_fops = &ppc_rtas_rmo_buf_ops;
239
240         return 0;
241 }
242
243 __initcall(proc_rtas_init);
244
245 /* ****************************************************************** */
246 /* POWER-ON-TIME                                                      */
247 /* ****************************************************************** */
248 static ssize_t ppc_rtas_poweron_write(struct file * file, const char * buf,
249                 size_t count, loff_t *ppos)
250 {
251         char stkbuf[40];  /* its small, its on stack */
252         struct rtc_time tm;
253         unsigned long nowtime;
254         char *dest;
255         int error;
256
257         if (39 < count) count = 39;
258         if (copy_from_user (stkbuf, buf, count)) {
259                 return -EFAULT;
260         }
261         stkbuf[count] = 0;
262         nowtime = simple_strtoul(stkbuf, &dest, 10);
263         if (*dest != '\0' && *dest != '\n') {
264                 printk("ppc_rtas_poweron_write: Invalid time\n");
265                 return count;
266         }
267         power_on_time = nowtime; /* save the time */
268
269         to_tm(nowtime, &tm);
270
271         error = rtas_call(rtas_token("set-time-for-power-on"), 7, 1, NULL, 
272                         tm.tm_year, tm.tm_mon, tm.tm_mday, 
273                         tm.tm_hour, tm.tm_min, tm.tm_sec, 0 /* nano */);
274         if (error != 0)
275                 printk(KERN_WARNING "error: setting poweron time returned: %s\n", 
276                                 ppc_rtas_process_error(error));
277         return count;
278 }
279 /* ****************************************************************** */
280 static ssize_t ppc_rtas_poweron_read(struct file * file, char * buf,
281                 size_t count, loff_t *ppos)
282 {
283         char stkbuf[40];  /* its small, its on stack */
284         int n, sn;
285         if (power_on_time == 0)
286                 n = scnprintf(stkbuf,sizeof(stkbuf),"Power on time not set\n");
287         else
288                 n = scnprintf(stkbuf,sizeof(stkbuf),"%lu\n",power_on_time);
289
290         sn = strlen (stkbuf) +1;
291         if (*ppos >= sn)
292                 return 0;
293         if (n > sn - *ppos)
294                 n = sn - *ppos;
295         if (n > count)
296                 n = count;
297         if (copy_to_user (buf, stkbuf + (*ppos), n)) {
298                 return -EFAULT;
299         }
300         *ppos += n;
301         return n;
302 }
303
304 /* ****************************************************************** */
305 /* PROGRESS                                                           */
306 /* ****************************************************************** */
307 static ssize_t ppc_rtas_progress_write(struct file * file, const char * buf,
308                 size_t count, loff_t *ppos)
309 {
310         unsigned long hex;
311
312         if (count >= MAX_LINELENGTH) count = MAX_LINELENGTH -1;
313         if (copy_from_user (progress_led, buf, count)) { /* save the string */
314                 return -EFAULT;
315         }
316         progress_led[count] = 0;
317
318         /* Lets see if the user passed hexdigits */
319         hex = simple_strtoul(progress_led, NULL, 10);
320
321         ppc_md.progress ((char *)progress_led, hex);
322         return count;
323
324         /* clear the line */ /* ppc_md.progress("                   ", 0xffff);*/
325 }
326 /* ****************************************************************** */
327 static ssize_t ppc_rtas_progress_read(struct file * file, char * buf,
328                 size_t count, loff_t *ppos)
329 {
330         int sn, n = 0;
331         char *tmpbuf;
332
333         if (progress_led == NULL) return 0;
334
335         tmpbuf = kmalloc (MAX_LINELENGTH, GFP_KERNEL);
336         if (!tmpbuf) {
337                 printk(KERN_ERR "error: kmalloc failed\n");
338                 return -ENOMEM;
339         }
340         n = sprintf (tmpbuf, "%s\n", progress_led);
341
342         sn = strlen (tmpbuf) +1;
343         if (*ppos >= sn) {
344                 kfree (tmpbuf);
345                 return 0;
346         }
347         if (n > sn - *ppos)
348                 n = sn - *ppos;
349         if (n > count)
350                 n = count;
351         if (copy_to_user (buf, tmpbuf + (*ppos), n)) {
352                 kfree (tmpbuf);
353                 return -EFAULT;
354         }
355         kfree (tmpbuf);
356         *ppos += n;
357         return n;
358 }
359
360 /* ****************************************************************** */
361 /* CLOCK                                                              */
362 /* ****************************************************************** */
363 static ssize_t ppc_rtas_clock_write(struct file * file, const char * buf, 
364                 size_t count, loff_t *ppos)
365 {
366         char stkbuf[40];  /* its small, its on stack */
367         struct rtc_time tm;
368         unsigned long nowtime;
369         char *dest;
370         int error;
371
372         if (39 < count) count = 39;
373         if (copy_from_user (stkbuf, buf, count)) {
374                 return -EFAULT;
375         }
376         stkbuf[count] = 0;
377         nowtime = simple_strtoul(stkbuf, &dest, 10);
378         if (*dest != '\0' && *dest != '\n') {
379                 printk("ppc_rtas_clock_write: Invalid time\n");
380                 return count;
381         }
382
383         to_tm(nowtime, &tm);
384         error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL, 
385                         tm.tm_year, tm.tm_mon, tm.tm_mday, 
386                         tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
387         if (error != 0)
388                 printk(KERN_WARNING "error: setting the clock returned: %s\n", 
389                                 ppc_rtas_process_error(error));
390         return count;
391 }
392 /* ****************************************************************** */
393 static ssize_t ppc_rtas_clock_read(struct file * file, char * buf, 
394                 size_t count, loff_t *ppos)
395 {
396         unsigned int year, mon, day, hour, min, sec;
397         int ret[8];
398         int n, sn, error;
399         char stkbuf[40];  /* its small, its on stack */
400
401         error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
402         
403         year = ret[0]; mon  = ret[1]; day  = ret[2];
404         hour = ret[3]; min  = ret[4]; sec  = ret[5];
405
406         if (error != 0){
407                 printk(KERN_WARNING "error: reading the clock returned: %s\n", 
408                                 ppc_rtas_process_error(error));
409                 n = scnprintf (stkbuf, sizeof(stkbuf), "0");
410         } else { 
411                 n = scnprintf (stkbuf, sizeof(stkbuf), "%lu\n",
412                                 mktime(year, mon, day, hour, min, sec));
413         }
414
415         sn = strlen (stkbuf) +1;
416         if (*ppos >= sn)
417                 return 0;
418         if (n > sn - *ppos)
419                 n = sn - *ppos;
420         if (n > count)
421                 n = count;
422         if (copy_to_user (buf, stkbuf + (*ppos), n)) {
423                 return -EFAULT;
424         }
425         *ppos += n;
426         return n;
427 }
428
429 /* ****************************************************************** */
430 /* SENSOR STUFF                                                       */
431 /* ****************************************************************** */
432 static int ppc_rtas_sensor_read(char * buf, char ** start, off_t off,
433                 int count, int *eof, void *data)
434 {
435         int i,j,n;
436         int state, error;
437         char *buffer;
438         int get_sensor_state = rtas_token("get-sensor-state");
439
440         if (count < 0)
441                 return -EINVAL;
442
443         /* May not be enough */
444         buffer = kmalloc(MAX_LINELENGTH*MAX_SENSORS, GFP_KERNEL);
445
446         if (!buffer)
447                 return -ENOMEM;
448
449         memset(buffer, 0, MAX_LINELENGTH*MAX_SENSORS);
450
451         n  = sprintf ( buffer  , "RTAS (RunTime Abstraction Services) Sensor Information\n");
452         n += sprintf ( buffer+n, "Sensor\t\tValue\t\tCondition\tLocation\n");
453         n += sprintf ( buffer+n, "********************************************************\n");
454
455         if (ppc_rtas_find_all_sensors() != 0) {
456                 n += sprintf ( buffer+n, "\nNo sensors are available\n");
457                 goto return_string;
458         }
459
460         for (i=0; i<sensors.quant; i++) {
461                 j = sensors.sensor[i].quant;
462                 /* A sensor may have multiple instances */
463                 while (j >= 0) {
464
465                         error = rtas_call(get_sensor_state, 2, 2, &state, 
466                                           sensors.sensor[i].token, 
467                                           sensors.sensor[i].quant - j);
468
469                         n += ppc_rtas_process_sensor(sensors.sensor[i], state, 
470                                                      error, buffer+n );
471                         n += sprintf (buffer+n, "\n");
472                         j--;
473                 } /* while */
474         } /* for */
475
476 return_string:
477         if (off >= strlen(buffer)) {
478                 *eof = 1;
479                 kfree(buffer);
480                 return 0;
481         }
482         if (n > strlen(buffer) - off)
483                 n = strlen(buffer) - off;
484         if (n > count)
485                 n = count;
486         else
487                 *eof = 1;
488
489         memcpy(buf, buffer + off, n);
490         *start = buf;
491         kfree(buffer);
492         return n;
493 }
494
495 /* ****************************************************************** */
496
497 int ppc_rtas_find_all_sensors (void)
498 {
499         unsigned int *utmp;
500         int len, i;
501
502         utmp = (unsigned int *) get_property(rtas_node, "rtas-sensors", &len);
503         if (utmp == NULL) {
504                 printk (KERN_ERR "error: could not get rtas-sensors\n");
505                 return 1;
506         }
507
508         sensors.quant = len / 8;      /* int + int */
509
510         for (i=0; i<sensors.quant; i++) {
511                 sensors.sensor[i].token = *utmp++;
512                 sensors.sensor[i].quant = *utmp++;
513         }
514         return 0;
515 }
516
517 /* ****************************************************************** */
518 /*
519  * Builds a string of what rtas returned
520  */
521 char * ppc_rtas_process_error(int error)
522 {
523         switch (error) {
524                 case SENSOR_CRITICAL_HIGH:
525                         return "(critical high)";
526                 case SENSOR_WARNING_HIGH:
527                         return "(warning high)";
528                 case SENSOR_NORMAL:
529                         return "(normal)";
530                 case SENSOR_WARNING_LOW:
531                         return "(warning low)";
532                 case SENSOR_CRITICAL_LOW:
533                         return "(critical low)";
534                 case SENSOR_SUCCESS:
535                         return "(read ok)";
536                 case SENSOR_HW_ERROR:
537                         return "(hardware error)";
538                 case SENSOR_BUSY:
539                         return "(busy)";
540                 case SENSOR_NOT_EXIST:
541                         return "(non existent)";
542                 case SENSOR_DR_ENTITY:
543                         return "(dr entity removed)";
544                 default:
545                         return "(UNKNOWN)";
546         }
547 }
548
549 /* ****************************************************************** */
550 /*
551  * Builds a string out of what the sensor said
552  */
553
554 int ppc_rtas_process_sensor(struct individual_sensor s, int state, 
555                 int error, char * buf) 
556 {
557         /* Defined return vales */
558         const char * key_switch[]        = { "Off\t", "Normal\t", "Secure\t", 
559                                                 "Maintenance" };
560         const char * enclosure_switch[]  = { "Closed", "Open" };
561         const char * lid_status[]        = { " ", "Open", "Closed" };
562         const char * power_source[]      = { "AC\t", "Battery", 
563                                                 "AC & Battery" };
564         const char * battery_remaining[] = { "Very Low", "Low", "Mid", "High" };
565         const char * epow_sensor[]       = { 
566                 "EPOW Reset", "Cooling warning", "Power warning",
567                 "System shutdown", "System halt", "EPOW main enclosure",
568                 "EPOW power off" };
569         const char * battery_cyclestate[]  = { "None", "In progress", 
570                                                 "Requested" };
571         const char * battery_charging[]    = { "Charging", "Discharching", 
572                                                 "No current flow" };
573         const char * ibm_drconnector[]     = { "Empty", "Present", "Unusable", 
574                                                 "Exchange" };
575         const char * ibm_intqueue[]        = { "Disabled", "Enabled" };
576
577         int have_strings = 0;
578         int num_states = 0;
579         int temperature = 0;
580         int unknown = 0;
581         int n = 0;
582
583         /* What kind of sensor do we have here? */
584         
585         switch (s.token) {
586                 case KEY_SWITCH:
587                         n += sprintf(buf+n, "Key switch:\t");
588                         num_states = sizeof(key_switch) / sizeof(char *);
589                         if (state < num_states) {
590                                 n += sprintf(buf+n, "%s\t", key_switch[state]);
591                                 have_strings = 1;
592                         }
593                         break;
594                 case ENCLOSURE_SWITCH:
595                         n += sprintf(buf+n, "Enclosure switch:\t");
596                         num_states = sizeof(enclosure_switch) / sizeof(char *);
597                         if (state < num_states) {
598                                 n += sprintf(buf+n, "%s\t", 
599                                                 enclosure_switch[state]);
600                                 have_strings = 1;
601                         }
602                         break;
603                 case THERMAL_SENSOR:
604                         n += sprintf(buf+n, "Temp. (°C/°F):\t");
605                         temperature = 1;
606                         break;
607                 case LID_STATUS:
608                         n += sprintf(buf+n, "Lid status:\t");
609                         num_states = sizeof(lid_status) / sizeof(char *);
610                         if (state < num_states) {
611                                 n += sprintf(buf+n, "%s\t", lid_status[state]);
612                                 have_strings = 1;
613                         }
614                         break;
615                 case POWER_SOURCE:
616                         n += sprintf(buf+n, "Power source:\t");
617                         num_states = sizeof(power_source) / sizeof(char *);
618                         if (state < num_states) {
619                                 n += sprintf(buf+n, "%s\t", 
620                                                 power_source[state]);
621                                 have_strings = 1;
622                         }
623                         break;
624                 case BATTERY_VOLTAGE:
625                         n += sprintf(buf+n, "Battery voltage:\t");
626                         break;
627                 case BATTERY_REMAINING:
628                         n += sprintf(buf+n, "Battery remaining:\t");
629                         num_states = sizeof(battery_remaining) / sizeof(char *);
630                         if (state < num_states)
631                         {
632                                 n += sprintf(buf+n, "%s\t", 
633                                                 battery_remaining[state]);
634                                 have_strings = 1;
635                         }
636                         break;
637                 case BATTERY_PERCENTAGE:
638                         n += sprintf(buf+n, "Battery percentage:\t");
639                         break;
640                 case EPOW_SENSOR:
641                         n += sprintf(buf+n, "EPOW Sensor:\t");
642                         num_states = sizeof(epow_sensor) / sizeof(char *);
643                         if (state < num_states) {
644                                 n += sprintf(buf+n, "%s\t", epow_sensor[state]);
645                                 have_strings = 1;
646                         }
647                         break;
648                 case BATTERY_CYCLESTATE:
649                         n += sprintf(buf+n, "Battery cyclestate:\t");
650                         num_states = sizeof(battery_cyclestate) / 
651                                         sizeof(char *);
652                         if (state < num_states) {
653                                 n += sprintf(buf+n, "%s\t", 
654                                                 battery_cyclestate[state]);
655                                 have_strings = 1;
656                         }
657                         break;
658                 case BATTERY_CHARGING:
659                         n += sprintf(buf+n, "Battery Charging:\t");
660                         num_states = sizeof(battery_charging) / sizeof(char *);
661                         if (state < num_states) {
662                                 n += sprintf(buf+n, "%s\t", 
663                                                 battery_charging[state]);
664                                 have_strings = 1;
665                         }
666                         break;
667                 case IBM_SURVEILLANCE:
668                         n += sprintf(buf+n, "Surveillance:\t");
669                         break;
670                 case IBM_FANRPM:
671                         n += sprintf(buf+n, "Fan (rpm):\t");
672                         break;
673                 case IBM_VOLTAGE:
674                         n += sprintf(buf+n, "Voltage (mv):\t");
675                         break;
676                 case IBM_DRCONNECTOR:
677                         n += sprintf(buf+n, "DR connector:\t");
678                         num_states = sizeof(ibm_drconnector) / sizeof(char *);
679                         if (state < num_states) {
680                                 n += sprintf(buf+n, "%s\t", 
681                                                 ibm_drconnector[state]);
682                                 have_strings = 1;
683                         }
684                         break;
685                 case IBM_POWERSUPPLY:
686                         n += sprintf(buf+n, "Powersupply:\t");
687                         break;
688                 case IBM_INTQUEUE:
689                         n += sprintf(buf+n, "Interrupt queue:\t");
690                         num_states = sizeof(ibm_intqueue) / sizeof(char *);
691                         if (state < num_states) {
692                                 n += sprintf(buf+n, "%s\t", 
693                                                 ibm_intqueue[state]);
694                                 have_strings = 1;
695                         }
696                         break;
697                 default:
698                         n += sprintf(buf+n,  "Unknown sensor (type %d), ignoring it\n",
699                                         s.token);
700                         unknown = 1;
701                         have_strings = 1;
702                         break;
703         }
704         if (have_strings == 0) {
705                 if (temperature) {
706                         n += sprintf(buf+n, "%4d /%4d\t", state, cel_to_fahr(state));
707                 } else
708                         n += sprintf(buf+n, "%10d\t", state);
709         }
710         if (unknown == 0) {
711                 n += sprintf ( buf+n, "%s\t", ppc_rtas_process_error(error));
712                 n += get_location_code(s, buf+n);
713         }
714         return n;
715 }
716
717 /* ****************************************************************** */
718
719 int check_location (char *c, int idx, char * buf)
720 {
721         int n = 0;
722
723         switch (*(c+idx)) {
724                 case LOC_PLANAR:
725                         n += sprintf ( buf, "Planar #%c", *(c+idx+1));
726                         break;
727                 case LOC_CPU:
728                         n += sprintf ( buf, "CPU #%c", *(c+idx+1));
729                         break;
730                 case LOC_FAN:
731                         n += sprintf ( buf, "Fan #%c", *(c+idx+1));
732                         break;
733                 case LOC_RACKMOUNTED:
734                         n += sprintf ( buf, "Rack #%c", *(c+idx+1));
735                         break;
736                 case LOC_VOLTAGE:
737                         n += sprintf ( buf, "Voltage #%c", *(c+idx+1));
738                         break;
739                 case LOC_LCD:
740                         n += sprintf ( buf, "LCD #%c", *(c+idx+1));
741                         break;
742                 case '.':
743                         n += sprintf ( buf, "- %c", *(c+idx+1));
744                 default:
745                         n += sprintf ( buf, "Unknown location");
746                         break;
747         }
748         return n;
749 }
750
751
752 /* ****************************************************************** */
753 /* 
754  * Format: 
755  * ${LETTER}${NUMBER}[[-/]${LETTER}${NUMBER} [ ... ] ]
756  * the '.' may be an abbrevation
757  */
758 int check_location_string (char *c, char *buf)
759 {
760         int n=0,i=0;
761
762         while (c[i]) {
763                 if (isalpha(c[i]) || c[i] == '.') {
764                          n += check_location(c, i, buf+n);
765                 }
766                 else if (c[i] == '/' || c[i] == '-')
767                         n += sprintf(buf+n, " at ");
768                 i++;
769         }
770         return n;
771 }
772
773
774 /* ****************************************************************** */
775
776 int get_location_code(struct individual_sensor s, char * buffer)
777 {
778         char rstr[512], tmp[10], tmp2[10];
779         int n=0, i=0, llen, len;
780         /* char *buf = kmalloc(MAX_LINELENGTH, GFP_KERNEL); */
781         char *ret;
782
783         static int pos = 0; /* remember position where buffer was */
784
785         /* construct the sensor number like 0003 */
786         /* fill with zeros */
787         n = sprintf(tmp, "%d", s.token);
788         len = strlen(tmp);
789         while (strlen(tmp) < 4)
790                 n += sprintf (tmp+n, "0");
791         
792         /* invert the string */
793         while (tmp[i]) {
794                 if (i<len)
795                         tmp2[4-len+i] = tmp[i];
796                 else
797                         tmp2[3-i] = tmp[i];
798                 i++;
799         }
800         tmp2[4] = '\0';
801
802         sprintf (rstr, SENSOR_PREFIX"%s", tmp2);
803
804         ret = (char *) get_property(rtas_node, rstr, &llen);
805
806         n=0;
807         if (ret == NULL || ret[0] == '\0') {
808                 n += sprintf ( buffer+n, "--- ");/* does not have a location */
809         } else {
810                 char t[50];
811                 ret += pos;
812
813                 n += check_location_string(ret, buffer + n);
814                 n += sprintf ( buffer+n, " ");
815                 /* see how many characters we have printed */
816                 scnprintf(t, sizeof(t), "%s ", ret);
817
818                 pos += strlen(t);
819                 if (pos >= llen) pos=0;
820         }
821         return n;
822 }
823 /* ****************************************************************** */
824 /* INDICATORS - Tone Frequency                                        */
825 /* ****************************************************************** */
826 static ssize_t ppc_rtas_tone_freq_write(struct file * file, const char * buf,
827                 size_t count, loff_t *ppos)
828 {
829         char stkbuf[40];  /* its small, its on stack */
830         unsigned long freq;
831         char *dest;
832         int error;
833
834         if (39 < count) count = 39;
835         if (copy_from_user (stkbuf, buf, count)) {
836                 return -EFAULT;
837         }
838         stkbuf[count] = 0;
839         freq = simple_strtoul(stkbuf, &dest, 10);
840         if (*dest != '\0' && *dest != '\n') {
841                 printk("ppc_rtas_tone_freq_write: Invalid tone freqency\n");
842                 return count;
843         }
844         if (freq < 0) freq = 0;
845         rtas_tone_frequency = freq; /* save it for later */
846         error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
847                         TONE_FREQUENCY, 0, freq);
848         if (error != 0)
849                 printk(KERN_WARNING "error: setting tone frequency returned: %s\n", 
850                                 ppc_rtas_process_error(error));
851         return count;
852 }
853 /* ****************************************************************** */
854 static ssize_t ppc_rtas_tone_freq_read(struct file * file, char * buf,
855                 size_t count, loff_t *ppos)
856 {
857         int n, sn;
858         char stkbuf[40];  /* its small, its on stack */
859
860         n = scnprintf(stkbuf, 40, "%lu\n", rtas_tone_frequency);
861
862         sn = strlen (stkbuf) +1;
863         if (*ppos >= sn)
864                 return 0;
865         if (n > sn - *ppos)
866                 n = sn - *ppos;
867         if (n > count)
868                 n = count;
869         if (copy_to_user (buf, stkbuf + (*ppos), n)) {
870                 return -EFAULT;
871         }
872         *ppos += n;
873         return n;
874 }
875 /* ****************************************************************** */
876 /* INDICATORS - Tone Volume                                           */
877 /* ****************************************************************** */
878 static ssize_t ppc_rtas_tone_volume_write(struct file * file, const char * buf,
879                 size_t count, loff_t *ppos)
880 {
881         char stkbuf[40];  /* its small, its on stack */
882         unsigned long volume;
883         char *dest;
884         int error;
885
886         if (39 < count) count = 39;
887         if (copy_from_user (stkbuf, buf, count)) {
888                 return -EFAULT;
889         }
890         stkbuf[count] = 0;
891         volume = simple_strtoul(stkbuf, &dest, 10);
892         if (*dest != '\0' && *dest != '\n') {
893                 printk("ppc_rtas_tone_volume_write: Invalid tone volume\n");
894                 return count;
895         }
896         if (volume < 0) volume = 0;
897         if (volume > 100) volume = 100;
898         
899         rtas_tone_volume = volume; /* save it for later */
900         error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
901                         TONE_VOLUME, 0, volume);
902         if (error != 0)
903                 printk(KERN_WARNING "error: setting tone volume returned: %s\n", 
904                                 ppc_rtas_process_error(error));
905         return count;
906 }
907 /* ****************************************************************** */
908 static ssize_t ppc_rtas_tone_volume_read(struct file * file, char * buf,
909                 size_t count, loff_t *ppos)
910 {
911         int n, sn;
912         char stkbuf[40];  /* its small, its on stack */
913
914         n = scnprintf(stkbuf, 40, "%lu\n", rtas_tone_volume);
915
916         sn = strlen (stkbuf) +1;
917         if (*ppos >= sn)
918                 return 0;
919         if (n > sn - *ppos)
920                 n = sn - *ppos;
921         if (n > count)
922                 n = count;
923         if (copy_to_user (buf, stkbuf + (*ppos), n)) {
924                 return -EFAULT;
925         }
926         *ppos += n;
927         return n;
928 }
929
930 #define RMO_READ_BUF_MAX 30
931
932 /* RTAS Userspace access */
933 static ssize_t ppc_rtas_rmo_buf_read(struct file *file, char __user *buf,
934                                     size_t count, loff_t *ppos)
935 {
936         char kbuf[RMO_READ_BUF_MAX];
937         int n;
938
939         n = sprintf(kbuf, "%016lx %x\n", rtas_rmo_buf, RTAS_RMOBUF_MAX);
940         if (n > count)
941                 n = count;
942
943         if (ppos && *ppos != 0)
944                 return 0;
945
946         if (copy_to_user(buf, kbuf, n))
947                 return -EFAULT;
948
949         if (ppos)
950                 *ppos = n;
951         
952         return n;
953 }