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