Merge ../linux-2.6
[sfrench/cifs-2.6.git] / drivers / sbus / char / envctrl.c
1 /* $Id: envctrl.c,v 1.25 2002/01/15 09:01:26 davem Exp $
2  * envctrl.c: Temperature and Fan monitoring on Machines providing it.
3  *
4  * Copyright (C) 1998  Eddie C. Dost  (ecd@skynet.be)
5  * Copyright (C) 2000  Vinh Truong    (vinh.truong@eng.sun.com)
6  * VT - The implementation is to support Sun Microelectronics (SME) platform
7  *      environment monitoring.  SME platforms use pcf8584 as the i2c bus 
8  *      controller to access pcf8591 (8-bit A/D and D/A converter) and 
9  *      pcf8571 (256 x 8-bit static low-voltage RAM with I2C-bus interface).
10  *      At board level, it follows SME Firmware I2C Specification. Reference:
11  *      http://www-eu2.semiconductors.com/pip/PCF8584P
12  *      http://www-eu2.semiconductors.com/pip/PCF8574AP
13  *      http://www-eu2.semiconductors.com/pip/PCF8591P
14  *
15  * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
16  *              Eric Brower <ebrower@usa.net>
17  *
18  * DB - Audit every copy_to_user in envctrl_read.
19  *              Daniele Bellucci <bellucda@tiscali.it>
20  */
21
22 #define __KERNEL_SYSCALLS__
23 static int errno;
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/kthread.h>
29 #include <linux/errno.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/init.h>
33 #include <linux/miscdevice.h>
34 #include <linux/mm.h>
35 #include <linux/slab.h>
36 #include <linux/kernel.h>
37
38 #include <asm/ebus.h>
39 #include <asm/uaccess.h>
40 #include <asm/envctrl.h>
41
42 #define ENVCTRL_MINOR   162
43
44 #define PCF8584_ADDRESS 0x55
45
46 #define CONTROL_PIN     0x80
47 #define CONTROL_ES0     0x40
48 #define CONTROL_ES1     0x20
49 #define CONTROL_ES2     0x10
50 #define CONTROL_ENI     0x08
51 #define CONTROL_STA     0x04
52 #define CONTROL_STO     0x02
53 #define CONTROL_ACK     0x01
54
55 #define STATUS_PIN      0x80
56 #define STATUS_STS      0x20
57 #define STATUS_BER      0x10
58 #define STATUS_LRB      0x08
59 #define STATUS_AD0      0x08
60 #define STATUS_AAB      0x04
61 #define STATUS_LAB      0x02
62 #define STATUS_BB       0x01
63
64 /*
65  * CLK Mode Register.
66  */
67 #define BUS_CLK_90      0x00
68 #define BUS_CLK_45      0x01
69 #define BUS_CLK_11      0x02
70 #define BUS_CLK_1_5     0x03
71
72 #define CLK_3           0x00
73 #define CLK_4_43        0x10
74 #define CLK_6           0x14
75 #define CLK_8           0x18
76 #define CLK_12          0x1c
77
78 #define OBD_SEND_START  0xc5    /* value to generate I2c_bus START condition */
79 #define OBD_SEND_STOP   0xc3    /* value to generate I2c_bus STOP condition */
80
81 /* Monitor type of i2c child device.
82  * Firmware definitions.
83  */
84 #define PCF8584_MAX_CHANNELS            8
85 #define PCF8584_GLOBALADDR_TYPE                 6  /* global address monitor */
86 #define PCF8584_FANSTAT_TYPE            3  /* fan status monitor */
87 #define PCF8584_VOLTAGE_TYPE            2  /* voltage monitor    */
88 #define PCF8584_TEMP_TYPE                       1  /* temperature monitor*/
89
90 /* Monitor type of i2c child device.
91  * Driver definitions.
92  */
93 #define ENVCTRL_NOMON                           0
94 #define ENVCTRL_CPUTEMP_MON                     1    /* cpu temperature monitor */
95 #define ENVCTRL_CPUVOLTAGE_MON          2    /* voltage monitor         */
96 #define ENVCTRL_FANSTAT_MON             3    /* fan status monitor      */
97 #define ENVCTRL_ETHERTEMP_MON           4    /* ethernet temperarture */
98                                              /* monitor                     */
99 #define ENVCTRL_VOLTAGESTAT_MON         5    /* voltage status monitor  */
100 #define ENVCTRL_MTHRBDTEMP_MON          6    /* motherboard temperature */
101 #define ENVCTRL_SCSITEMP_MON            7    /* scsi temperarture */
102 #define ENVCTRL_GLOBALADDR_MON          8    /* global address */
103
104 /* Child device type.
105  * Driver definitions.
106  */
107 #define I2C_ADC                         0    /* pcf8591 */
108 #define I2C_GPIO                        1    /* pcf8571 */
109
110 /* Data read from child device may need to decode
111  * through a data table and a scale.
112  * Translation type as defined by firmware.
113  */
114 #define ENVCTRL_TRANSLATE_NO            0
115 #define ENVCTRL_TRANSLATE_PARTIAL       1
116 #define ENVCTRL_TRANSLATE_COMBINED      2
117 #define ENVCTRL_TRANSLATE_FULL          3     /* table[data] */
118 #define ENVCTRL_TRANSLATE_SCALE         4     /* table[data]/scale */
119
120 /* Driver miscellaneous definitions. */
121 #define ENVCTRL_MAX_CPU                 4
122 #define CHANNEL_DESC_SZ                 256
123
124 /* Mask values for combined GlobalAddress/PowerStatus node */
125 #define ENVCTRL_GLOBALADDR_ADDR_MASK    0x1F
126 #define ENVCTRL_GLOBALADDR_PSTAT_MASK   0x60
127
128 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms 
129  * (see envctrl_init_i2c_child)
130  */
131 #define ENVCTRL_CPCI_IGNORED_NODE               0x70
132
133 #define PCF8584_DATA    0x00
134 #define PCF8584_CSR     0x01
135
136 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
137  * Property of a port or channel as defined by the firmware.
138  */
139 struct pcf8584_channel {
140         unsigned char chnl_no;
141         unsigned char io_direction;
142         unsigned char type;
143         unsigned char last;
144 };
145
146 /* Each child device may have one or more tables of bytes to help decode
147  * data. Table property as defined by the firmware.
148  */ 
149 struct pcf8584_tblprop {
150         unsigned int type;
151         unsigned int scale;  
152         unsigned int offset; /* offset from the beginning of the table */
153         unsigned int size;
154 };
155
156 /* i2c child */
157 struct i2c_child_t {
158         /* Either ADC or GPIO. */
159         unsigned char i2ctype;
160         unsigned long addr;    
161         struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
162
163         /* Channel info. */ 
164         unsigned int total_chnls;       /* Number of monitor channels. */
165         unsigned char fan_mask;         /* Byte mask for fan status channels. */
166         unsigned char voltage_mask;     /* Byte mask for voltage status channels. */
167         struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
168
169         /* Properties of all monitor channels. */
170         unsigned int total_tbls;        /* Number of monitor tables. */
171         char *tables;                   /* Pointer to table(s). */
172         char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
173         char mon_type[PCF8584_MAX_CHANNELS];
174 };
175
176 static void __iomem *i2c;
177 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
178 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
179 static unsigned int warning_temperature = 0;
180 static unsigned int shutdown_temperature = 0;
181 static char read_cpu;
182
183 /* Forward declarations. */
184 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
185
186 /* Function Description: Test the PIN bit (Pending Interrupt Not) 
187  *                       to test when serial transmission is completed .
188  * Return : None.
189  */
190 static void envtrl_i2c_test_pin(void)
191 {
192         int limit = 1000000;
193
194         while (--limit > 0) {
195                 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN)) 
196                         break;
197                 udelay(1);
198         } 
199
200         if (limit <= 0)
201                 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
202 }
203
204 /* Function Description: Test busy bit.
205  * Return : None.
206  */
207 static void envctrl_i2c_test_bb(void)
208 {
209         int limit = 1000000;
210
211         while (--limit > 0) {
212                 /* Busy bit 0 means busy. */
213                 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
214                         break;
215                 udelay(1);
216         } 
217
218         if (limit <= 0)
219                 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
220 }
221
222 /* Function Description: Send the address for a read access.
223  * Return : 0 if not acknowledged, otherwise acknowledged.
224  */
225 static int envctrl_i2c_read_addr(unsigned char addr)
226 {
227         envctrl_i2c_test_bb();
228
229         /* Load address. */
230         writeb(addr + 1, i2c + PCF8584_DATA);
231
232         envctrl_i2c_test_bb();
233
234         writeb(OBD_SEND_START, i2c + PCF8584_CSR);
235
236         /* Wait for PIN. */
237         envtrl_i2c_test_pin();
238
239         /* CSR 0 means acknowledged. */
240         if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
241                 return readb(i2c + PCF8584_DATA);
242         } else {
243                 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
244                 return 0;
245         }
246 }
247
248 /* Function Description: Send the address for write mode.  
249  * Return : None.
250  */
251 static void envctrl_i2c_write_addr(unsigned char addr)
252 {
253         envctrl_i2c_test_bb();
254         writeb(addr, i2c + PCF8584_DATA);
255
256         /* Generate Start condition. */
257         writeb(OBD_SEND_START, i2c + PCF8584_CSR);
258 }
259
260 /* Function Description: Read 1 byte of data from addr 
261  *                       set by envctrl_i2c_read_addr() 
262  * Return : Data from address set by envctrl_i2c_read_addr().
263  */
264 static unsigned char envctrl_i2c_read_data(void)
265 {
266         envtrl_i2c_test_pin();
267         writeb(CONTROL_ES0, i2c + PCF8584_CSR);  /* Send neg ack. */
268         return readb(i2c + PCF8584_DATA);
269 }
270
271 /* Function Description: Instruct the device which port to read data from.  
272  * Return : None.
273  */
274 static void envctrl_i2c_write_data(unsigned char port)
275 {
276         envtrl_i2c_test_pin();
277         writeb(port, i2c + PCF8584_DATA);
278 }
279
280 /* Function Description: Generate Stop condition after last byte is sent.
281  * Return : None.
282  */
283 static void envctrl_i2c_stop(void)
284 {
285         envtrl_i2c_test_pin();
286         writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
287 }
288
289 /* Function Description: Read adc device.
290  * Return : Data at address and port.
291  */
292 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
293 {
294         /* Send address. */
295         envctrl_i2c_write_addr(addr);
296
297         /* Setup port to read. */
298         envctrl_i2c_write_data(port);
299         envctrl_i2c_stop();
300
301         /* Read port. */
302         envctrl_i2c_read_addr(addr);
303
304         /* Do a single byte read and send stop. */
305         envctrl_i2c_read_data();
306         envctrl_i2c_stop();
307
308         return readb(i2c + PCF8584_DATA);
309 }
310
311 /* Function Description: Read gpio device.
312  * Return : Data at address.
313  */
314 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
315 {
316         unsigned char rd;
317
318         envctrl_i2c_read_addr(addr);
319
320         /* Do a single byte read and send stop. */
321         rd = envctrl_i2c_read_data();
322         envctrl_i2c_stop();
323         return rd;
324 }
325
326 /* Function Description: Decode data read from an adc device using firmware
327  *                       table.
328  * Return: Number of read bytes. Data is stored in bufdata in ascii format.
329  */
330 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
331                                       int scale, char *tbl, char *bufdata)
332 {
333         int len = 0;
334
335         switch (translate_type) {
336         case ENVCTRL_TRANSLATE_NO:
337                 /* No decode necessary. */
338                 len = 1;
339                 bufdata[0] = data;
340                 break;
341
342         case ENVCTRL_TRANSLATE_FULL:
343                 /* Decode this way: data = table[data]. */
344                 len = 1;
345                 bufdata[0] = tbl[data];
346                 break;
347
348         case ENVCTRL_TRANSLATE_SCALE:
349                 /* Decode this way: data = table[data]/scale */
350                 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
351                 len = strlen(bufdata);
352                 bufdata[len - 1] = bufdata[len - 2];
353                 bufdata[len - 2] = '.';
354                 break;
355
356         default:
357                 break;
358         };
359
360         return len;
361 }
362
363 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
364  * Return: Number of read bytes. Data is stored in bufdata in ascii format.
365  */
366 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
367                                  char mon_type, unsigned char *bufdata)
368 {
369         unsigned char data;
370         int i;
371         char *tbl, j = -1;
372
373         /* Find the right monitor type and channel. */
374         for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
375                 if (pchild->mon_type[i] == mon_type) {
376                         if (++j == cpu) {
377                                 break;
378                         }
379                 }
380         }
381
382         if (j != cpu)
383                 return 0;
384
385         /* Read data from address and port. */
386         data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
387                                      (unsigned char)pchild->chnl_array[i].chnl_no);
388
389         /* Find decoding table. */
390         tbl = pchild->tables + pchild->tblprop_array[i].offset;
391
392         return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
393                                           pchild->tblprop_array[i].scale,
394                                           tbl, bufdata);
395 }
396
397 /* Function Description: Read noncpu-related data such as motherboard 
398  *                       temperature.
399  * Return: Number of read bytes. Data is stored in bufdata in ascii format.
400  */
401 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
402                                     char mon_type, unsigned char *bufdata)
403 {
404         unsigned char data;
405         int i;
406         char *tbl = NULL;
407
408         for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
409                 if (pchild->mon_type[i] == mon_type)
410                         break;
411         }
412
413         if (i >= PCF8584_MAX_CHANNELS)
414                 return 0;
415
416         /* Read data from address and port. */
417         data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
418                                      (unsigned char)pchild->chnl_array[i].chnl_no);
419
420         /* Find decoding table. */
421         tbl = pchild->tables + pchild->tblprop_array[i].offset;
422
423         return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
424                                           pchild->tblprop_array[i].scale,
425                                           tbl, bufdata);
426 }
427
428 /* Function Description: Read fan status.
429  * Return : Always 1 byte. Status stored in bufdata.
430  */
431 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
432                                   unsigned char data,
433                                   char *bufdata)
434 {
435         unsigned char tmp, ret = 0;
436         int i, j = 0;
437
438         tmp = data & pchild->fan_mask;
439
440         if (tmp == pchild->fan_mask) {
441                 /* All bits are on. All fans are functioning. */
442                 ret = ENVCTRL_ALL_FANS_GOOD;
443         } else if (tmp == 0) {
444                 /* No bits are on. No fans are functioning. */
445                 ret = ENVCTRL_ALL_FANS_BAD;
446         } else {
447                 /* Go through all channels, mark 'on' the matched bits.
448                  * Notice that fan_mask may have discontiguous bits but
449                  * return mask are always contiguous. For example if we
450                  * monitor 4 fans at channels 0,1,2,4, the return mask
451                  * should be 00010000 if only fan at channel 4 is working.
452                  */
453                 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
454                         if (pchild->fan_mask & chnls_mask[i]) {
455                                 if (!(chnls_mask[i] & tmp))
456                                         ret |= chnls_mask[j];
457
458                                 j++;
459                         }
460                 }
461         }
462
463         bufdata[0] = ret;
464         return 1;
465 }
466
467 /* Function Description: Read global addressing line.
468  * Return : Always 1 byte. Status stored in bufdata.
469  */
470 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
471                                   unsigned char data,
472                                   char *bufdata)
473 {
474         /* Translatation table is not necessary, as global
475          * addr is the integer value of the GA# bits.
476          *
477          * NOTE: MSB is documented as zero, but I see it as '1' always....
478          *
479          * -----------------------------------------------
480          * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
481          * -----------------------------------------------
482          * GA0 - GA4    integer value of Global Address (backplane slot#)
483          * DEG                  0 = cPCI Power supply output is starting to degrade
484          *                              1 = cPCI Power supply output is OK
485          * FAL                  0 = cPCI Power supply has failed
486          *                              1 = cPCI Power supply output is OK
487          */
488         bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
489         return 1;
490 }
491
492 /* Function Description: Read standard voltage and power supply status.
493  * Return : Always 1 byte. Status stored in bufdata.
494  */
495 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
496                                                 unsigned char data,
497                                                 char *bufdata)
498 {
499         unsigned char tmp, ret = 0;
500         int i, j = 0;
501
502         tmp = data & pchild->voltage_mask;
503
504         /* Two channels are used to monitor voltage and power supply. */
505         if (tmp == pchild->voltage_mask) {
506                 /* All bits are on. Voltage and power supply are okay. */
507                 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
508         } else if (tmp == 0) {
509                 /* All bits are off. Voltage and power supply are bad */
510                 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
511         } else {
512                 /* Either voltage or power supply has problem. */
513                 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
514                         if (pchild->voltage_mask & chnls_mask[i]) {
515                                 j++;
516
517                                 /* Break out when there is a mismatch. */
518                                 if (!(chnls_mask[i] & tmp))
519                                         break; 
520                         }
521                 }
522
523                 /* Make a wish that hardware will always use the
524                  * first channel for voltage and the second for
525                  * power supply.
526                  */
527                 if (j == 1)
528                         ret = ENVCTRL_VOLTAGE_BAD;
529                 else
530                         ret = ENVCTRL_POWERSUPPLY_BAD;
531         }
532
533         bufdata[0] = ret;
534         return 1;
535 }
536
537 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
538  * Return: Number of read bytes. 0 for error.
539  */
540 static ssize_t
541 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
542 {
543         struct i2c_child_t *pchild;
544         unsigned char data[10];
545         int ret = 0;
546
547         /* Get the type of read as decided in ioctl() call.
548          * Find the appropriate i2c child.
549          * Get the data and put back to the user buffer.
550          */
551
552         switch ((int)(long)file->private_data) {
553         case ENVCTRL_RD_WARNING_TEMPERATURE:
554                 if (warning_temperature == 0)
555                         return 0;
556
557                 data[0] = (unsigned char)(warning_temperature);
558                 ret = 1;
559                 if (copy_to_user(buf, data, ret))
560                         ret = -EFAULT;
561                 break;
562
563         case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
564                 if (shutdown_temperature == 0)
565                         return 0;
566
567                 data[0] = (unsigned char)(shutdown_temperature);
568                 ret = 1;
569                 if (copy_to_user(buf, data, ret))
570                         ret = -EFAULT;
571                 break;
572
573         case ENVCTRL_RD_MTHRBD_TEMPERATURE:
574                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
575                         return 0;
576                 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
577                 if (copy_to_user(buf, data, ret))
578                         ret = -EFAULT;
579                 break;
580
581         case ENVCTRL_RD_CPU_TEMPERATURE:
582                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
583                         return 0;
584                 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
585
586                 /* Reset cpu to the default cpu0. */
587                 if (copy_to_user(buf, data, ret))
588                         ret = -EFAULT;
589                 break;
590
591         case ENVCTRL_RD_CPU_VOLTAGE:
592                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
593                         return 0;
594                 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
595
596                 /* Reset cpu to the default cpu0. */
597                 if (copy_to_user(buf, data, ret))
598                         ret = -EFAULT;
599                 break;
600
601         case ENVCTRL_RD_SCSI_TEMPERATURE:
602                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
603                         return 0;
604                 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
605                 if (copy_to_user(buf, data, ret))
606                         ret = -EFAULT;
607                 break;
608
609         case ENVCTRL_RD_ETHERNET_TEMPERATURE:
610                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
611                         return 0;
612                 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
613                 if (copy_to_user(buf, data, ret))
614                         ret = -EFAULT;
615                 break;
616
617         case ENVCTRL_RD_FAN_STATUS:
618                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
619                         return 0;
620                 data[0] = envctrl_i2c_read_8574(pchild->addr);
621                 ret = envctrl_i2c_fan_status(pchild,data[0], data);
622                 if (copy_to_user(buf, data, ret))
623                         ret = -EFAULT;
624                 break;
625         
626         case ENVCTRL_RD_GLOBALADDRESS:
627                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
628                         return 0;
629                 data[0] = envctrl_i2c_read_8574(pchild->addr);
630                 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
631                 if (copy_to_user(buf, data, ret))
632                         ret = -EFAULT;
633                 break;
634
635         case ENVCTRL_RD_VOLTAGE_STATUS:
636                 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
637                         /* If voltage monitor not present, check for CPCI equivalent */
638                         if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
639                                 return 0;
640                 data[0] = envctrl_i2c_read_8574(pchild->addr);
641                 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
642                 if (copy_to_user(buf, data, ret))
643                         ret = -EFAULT;
644                 break;
645
646         default:
647                 break;
648
649         };
650
651         return ret;
652 }
653
654 /* Function Description: Command what to read.  Mapped to user ioctl().
655  * Return: Gives 0 for implemented commands, -EINVAL otherwise.
656  */
657 static int
658 envctrl_ioctl(struct inode *inode, struct file *file,
659               unsigned int cmd, unsigned long arg)
660 {
661         char __user *infobuf;
662
663         switch (cmd) {
664         case ENVCTRL_RD_WARNING_TEMPERATURE:
665         case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
666         case ENVCTRL_RD_MTHRBD_TEMPERATURE:
667         case ENVCTRL_RD_FAN_STATUS:
668         case ENVCTRL_RD_VOLTAGE_STATUS:
669         case ENVCTRL_RD_ETHERNET_TEMPERATURE:
670         case ENVCTRL_RD_SCSI_TEMPERATURE:
671         case ENVCTRL_RD_GLOBALADDRESS:
672                 file->private_data = (void *)(long)cmd;
673                 break;
674
675         case ENVCTRL_RD_CPU_TEMPERATURE:
676         case ENVCTRL_RD_CPU_VOLTAGE:
677                 /* Check to see if application passes in any cpu number,
678                  * the default is cpu0.
679                  */
680                 infobuf = (char __user *) arg;
681                 if (infobuf == NULL) {
682                         read_cpu = 0;
683                 }else {
684                         get_user(read_cpu, infobuf);
685                 }
686
687                 /* Save the command for use when reading. */
688                 file->private_data = (void *)(long)cmd;
689                 break;
690
691         default:
692                 return -EINVAL;
693         };
694
695         return 0;
696 }
697
698 /* Function Description: open device. Mapped to user open().
699  * Return: Always 0.
700  */
701 static int
702 envctrl_open(struct inode *inode, struct file *file)
703 {
704         file->private_data = NULL;
705         return 0;
706 }
707
708 /* Function Description: Open device. Mapped to user close().
709  * Return: Always 0.
710  */
711 static int
712 envctrl_release(struct inode *inode, struct file *file)
713 {
714         return 0;
715 }
716
717 static struct file_operations envctrl_fops = {
718         .owner =        THIS_MODULE,
719         .read =         envctrl_read,
720         .ioctl =        envctrl_ioctl,
721         .open =         envctrl_open,
722         .release =      envctrl_release,
723 };      
724
725 static struct miscdevice envctrl_dev = {
726         ENVCTRL_MINOR,
727         "envctrl",
728         &envctrl_fops
729 };
730
731 /* Function Description: Set monitor type based on firmware description.
732  * Return: None.
733  */
734 static void envctrl_set_mon(struct i2c_child_t *pchild,
735                             char *chnl_desc,
736                             int chnl_no)
737 {
738         /* Firmware only has temperature type.  It does not distinguish
739          * different kinds of temperatures.  We use channel description
740          * to disinguish them.
741          */
742         if (!(strcmp(chnl_desc,"temp,cpu")) ||
743             !(strcmp(chnl_desc,"temp,cpu0")) ||
744             !(strcmp(chnl_desc,"temp,cpu1")) ||
745             !(strcmp(chnl_desc,"temp,cpu2")) ||
746             !(strcmp(chnl_desc,"temp,cpu3")))
747                 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
748
749         if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
750             !(strcmp(chnl_desc,"vddcore,cpu1")) ||
751             !(strcmp(chnl_desc,"vddcore,cpu2")) ||
752             !(strcmp(chnl_desc,"vddcore,cpu3")))
753                 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
754
755         if (!(strcmp(chnl_desc,"temp,motherboard")))
756                 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
757
758         if (!(strcmp(chnl_desc,"temp,scsi")))
759                 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
760
761         if (!(strcmp(chnl_desc,"temp,ethernet")))
762                 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
763 }
764
765 /* Function Description: Initialize monitor channel with channel desc,
766  *                       decoding tables, monitor type, optional properties.
767  * Return: None.
768  */
769 static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
770 {
771         char chnls_desc[CHANNEL_DESC_SZ];
772         int i = 0, len;
773         char *pos = chnls_desc;
774
775         /* Firmware describe channels into a stream separated by a '\0'. */
776         len = prom_getproperty(node, "channels-description", chnls_desc,
777                                CHANNEL_DESC_SZ);
778         chnls_desc[CHANNEL_DESC_SZ - 1] = '\0';
779
780         while (len > 0) {
781                 int l = strlen(pos) + 1;
782                 envctrl_set_mon(pchild, pos, i++);
783                 len -= l;
784                 pos += l;
785         }
786
787         /* Get optional properties. */
788         len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
789                                sizeof(warning_temperature));
790         len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
791                                sizeof(shutdown_temperature));
792 }
793
794 /* Function Description: Initialize child device monitoring fan status.
795  * Return: None.
796  */
797 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
798 {
799         int i;
800
801         /* Go through all channels and set up the mask. */
802         for (i = 0; i < pchild->total_chnls; i++)
803                 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
804
805         /* We only need to know if this child has fan status monitored.
806          * We don't care which channels since we have the mask already.
807          */
808         pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
809 }
810
811 /* Function Description: Initialize child device for global addressing line.
812  * Return: None.
813  */
814 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
815 {
816         int i;
817
818         /* Voltage/PowerSupply monitoring is piggybacked 
819          * with Global Address on CompactPCI.  See comments
820          * within envctrl_i2c_globaladdr for bit assignments.
821          *
822          * The mask is created here by assigning mask bits to each
823          * bit position that represents PCF8584_VOLTAGE_TYPE data.
824          * Channel numbers are not consecutive within the globaladdr
825          * node (why?), so we use the actual counter value as chnls_mask
826          * index instead of the chnl_array[x].chnl_no value.
827          *
828          * NOTE: This loop could be replaced with a constant representing
829          * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
830          */
831         for (i = 0; i < pchild->total_chnls; i++) {
832                 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
833                         pchild->voltage_mask |= chnls_mask[i];
834                 }
835         }
836
837         /* We only need to know if this child has global addressing 
838          * line monitored.  We don't care which channels since we know 
839          * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
840          */
841         pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
842 }
843
844 /* Initialize child device monitoring voltage status. */
845 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
846 {
847         int i;
848
849         /* Go through all channels and set up the mask. */
850         for (i = 0; i < pchild->total_chnls; i++)
851                 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
852
853         /* We only need to know if this child has voltage status monitored.
854          * We don't care which channels since we have the mask already.
855          */
856         pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
857 }
858
859 /* Function Description: Initialize i2c child device.
860  * Return: None.
861  */
862 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
863                                    struct i2c_child_t *pchild)
864 {
865         int node, len, i, tbls_size = 0;
866
867         node = edev_child->prom_node;
868
869         /* Get device address. */
870         len = prom_getproperty(node, "reg",
871                                (char *) &(pchild->addr),
872                                sizeof(pchild->addr));
873
874         /* Get tables property.  Read firmware temperature tables. */
875         len = prom_getproperty(node, "translation",
876                                (char *) pchild->tblprop_array,
877                                (PCF8584_MAX_CHANNELS *
878                                 sizeof(struct pcf8584_tblprop)));
879         if (len > 0) {
880                 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
881                 for (i = 0; i < pchild->total_tbls; i++) {
882                         if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
883                                 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
884                         }
885                 }
886
887                 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
888                 if (pchild->tables == NULL){
889                         printk("envctrl: Failed to allocate table.\n");
890                         return;
891                 }
892                 len = prom_getproperty(node, "tables",
893                                        (char *) pchild->tables, tbls_size);
894                 if (len <= 0) {
895                         printk("envctrl: Failed to get table.\n");
896                         return;
897                 }
898         }
899
900         /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
901          * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
902          * "For Factory Use Only."
903          *
904          * We ignore the node on these platforms by assigning the
905          * 'NULL' monitor type.
906          */
907         if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
908                 int len;
909                 char prop[56];
910
911                 len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
912                 if (0 < len && (0 == strncmp(prop, "SUNW,UltraSPARC-IIi-cEngine", len)))
913                 {
914                         for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
915                                 pchild->mon_type[len] = ENVCTRL_NOMON;
916                         }
917                         return;
918                 }
919         }
920
921         /* Get the monitor channels. */
922         len = prom_getproperty(node, "channels-in-use",
923                                (char *) pchild->chnl_array,
924                                (PCF8584_MAX_CHANNELS *
925                                 sizeof(struct pcf8584_channel)));
926         pchild->total_chnls = len / sizeof(struct pcf8584_channel);
927
928         for (i = 0; i < pchild->total_chnls; i++) {
929                 switch (pchild->chnl_array[i].type) {
930                 case PCF8584_TEMP_TYPE:
931                         envctrl_init_adc(pchild, node);
932                         break;
933
934                 case PCF8584_GLOBALADDR_TYPE:
935                         envctrl_init_globaladdr(pchild);
936                         i = pchild->total_chnls;
937                         break;
938
939                 case PCF8584_FANSTAT_TYPE:
940                         envctrl_init_fanstat(pchild);
941                         i = pchild->total_chnls;
942                         break;
943
944                 case PCF8584_VOLTAGE_TYPE:
945                         if (pchild->i2ctype == I2C_ADC) {
946                                 envctrl_init_adc(pchild,node);
947                         } else {
948                                 envctrl_init_voltage_status(pchild);
949                         }
950                         i = pchild->total_chnls;
951                         break;
952
953                 default:
954                         break;
955                 };
956         }
957 }
958
959 /* Function Description: Search the child device list for a device.
960  * Return : The i2c child if found. NULL otherwise.
961  */
962 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
963 {
964         int i, j;
965
966         for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
967                 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
968                         if (i2c_childlist[i].mon_type[j] == mon_type) {
969                                 return (struct i2c_child_t *)(&(i2c_childlist[i]));
970                         }
971                 }
972         }
973         return NULL;
974 }
975
976 static void envctrl_do_shutdown(void)
977 {
978         static int inprog = 0;
979         static char *envp[] = { 
980                 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
981         char *argv[] = { 
982                 "/sbin/shutdown", "-h", "now", NULL };  
983
984         if (inprog != 0)
985                 return;
986
987         inprog = 1;
988         printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
989         if (0 > execve("/sbin/shutdown", argv, envp)) {
990                 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n"); 
991                 inprog = 0;  /* unlikely to succeed, but we could try again */
992         }
993 }
994
995 static struct task_struct *kenvctrld_task;
996
997 static int kenvctrld(void *__unused)
998 {
999         int poll_interval;
1000         int whichcpu;
1001         char tempbuf[10];
1002         struct i2c_child_t *cputemp;
1003
1004         if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
1005                 printk(KERN_ERR 
1006                        "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1007                 return -ENODEV;
1008         }
1009
1010         poll_interval = 5000; /* TODO env_mon_interval */
1011
1012         printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1013         for (;;) {
1014                 msleep_interruptible(poll_interval);
1015
1016                 if (kthread_should_stop())
1017                         break;
1018                 
1019                 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1020                         if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1021                                                       ENVCTRL_CPUTEMP_MON,
1022                                                       tempbuf)) {
1023                                 if (tempbuf[0] >= shutdown_temperature) {
1024                                         printk(KERN_CRIT 
1025                                                 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1026                                                 "shutdown threshold %i C\n", 
1027                                                 current->comm, whichcpu, 
1028                                                 tempbuf[0], shutdown_temperature);
1029                                         envctrl_do_shutdown();
1030                                 }
1031                         }
1032                 }
1033         }
1034         printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1035         return 0;
1036 }
1037
1038 static int __init envctrl_init(void)
1039 {
1040         struct linux_ebus *ebus = NULL;
1041         struct linux_ebus_device *edev = NULL;
1042         struct linux_ebus_child *edev_child = NULL;
1043         int err, i = 0;
1044
1045         for_each_ebus(ebus) {
1046                 for_each_ebusdev(edev, ebus) {
1047                         if (!strcmp(edev->prom_name, "bbc")) {
1048                                 /* If we find a boot-bus controller node,
1049                                  * then this envctrl driver is not for us.
1050                                  */
1051                                 return -ENODEV;
1052                         }
1053                 }
1054         }
1055
1056         /* Traverse through ebus and ebus device list for i2c device and
1057          * adc and gpio nodes.
1058          */
1059         for_each_ebus(ebus) {
1060                 for_each_ebusdev(edev, ebus) {
1061                         if (!strcmp(edev->prom_name, "i2c")) {
1062                                 i2c = ioremap(edev->resource[0].start, 0x2);
1063                                 for_each_edevchild(edev, edev_child) {
1064                                         if (!strcmp("gpio", edev_child->prom_name)) {
1065                                                 i2c_childlist[i].i2ctype = I2C_GPIO;
1066                                                 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1067                                         }
1068                                         if (!strcmp("adc", edev_child->prom_name)) {
1069                                                 i2c_childlist[i].i2ctype = I2C_ADC;
1070                                                 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1071                                         }
1072                                 }
1073                                 goto done;
1074                         }
1075                 }
1076         }
1077
1078 done:
1079         if (!edev) {
1080                 printk("envctrl: I2C device not found.\n");
1081                 return -ENODEV;
1082         }
1083
1084         /* Set device address. */
1085         writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1086         writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1087
1088         /* Set system clock and SCL frequencies. */ 
1089         writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1090         writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1091
1092         /* Enable serial interface. */
1093         writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1094         udelay(200);
1095
1096         /* Register the device as a minor miscellaneous device. */
1097         err = misc_register(&envctrl_dev);
1098         if (err) {
1099                 printk("envctrl: Unable to get misc minor %d\n",
1100                        envctrl_dev.minor);
1101                 goto out_iounmap;
1102         }
1103
1104         /* Note above traversal routine post-incremented 'i' to accommodate 
1105          * a next child device, so we decrement before reverse-traversal of
1106          * child devices.
1107          */
1108         printk("envctrl: initialized ");
1109         for (--i; i >= 0; --i) {
1110                 printk("[%s 0x%lx]%s", 
1111                         (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") : 
1112                         ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")), 
1113                         i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1114         }
1115
1116         kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1117         if (IS_ERR(kenvctrld_task)) {
1118                 err = PTR_ERR(kenvctrld_task);
1119                 goto out_deregister;
1120         }
1121
1122         return 0;
1123
1124 out_deregister:
1125         misc_deregister(&envctrl_dev);
1126 out_iounmap:
1127         iounmap(i2c);
1128         for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1129                 kfree(i2c_childlist[i].tables);
1130
1131         return err;
1132 }
1133
1134 static void __exit envctrl_cleanup(void)
1135 {
1136         int i;
1137
1138         kthread_stop(kenvctrld_task);
1139
1140         iounmap(i2c);
1141         misc_deregister(&envctrl_dev);
1142
1143         for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1144                 kfree(i2c_childlist[i].tables);
1145 }
1146
1147 module_init(envctrl_init);
1148 module_exit(envctrl_cleanup);
1149 MODULE_LICENSE("GPL");