xref: /netbsd/sys/arch/sun2/sun2/leds.c (revision 6550d01e)
1 /*	$NetBSD: leds.c,v 1.7 2008/04/28 20:23:37 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross and der Mouse.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Functions to flash the LEDs with some pattern.
34  * All other Sun2 machines have an 8-position LED
35  * array in which some pattern is animated.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: leds.c,v 1.7 2008/04/28 20:23:37 martin Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/conf.h>
45 #include <sys/buf.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 
49 #include <machine/autoconf.h>
50 #include <machine/idprom.h>
51 #include <machine/leds.h>
52 
53 #include <sun2/sun2/machdep.h>
54 #include <sun2/sun2/control.h>
55 
56 static u_char led_countdown = 0;
57 static u_char led_px = 0;
58 
59 /*
60  * Initial value is the default pattern set.
61  */
62 static struct led_patterns ledpat = {
63 	8,	/* divisor */
64 	8,	/* patlen */
65 	{	/* patterns */
66 		0x0F, 0x1E, 0x3C, 0x78,
67 		0xF0, 0xE1, 0xC3, 0x87,
68 	}
69 };
70 
71 /*
72  * This is called early during startup to find the
73  * diag register (LEDs) and turn on the light(s).
74  */
75 void
76 leds_init(void)
77 {
78 
79 	/* Turn on some lights. */
80 	leds_intr();
81 }
82 
83 /*
84  * This is called by the clock interrupt.
85  */
86 void
87 leds_intr(void)
88 {
89 	u_char i;
90 
91 	if (led_countdown) {
92 		led_countdown--;
93 		return;
94 	}
95 
96 	led_countdown = ledpat.divisor - 1;
97 	i = led_px;
98 
99 	set_control_byte(DIAG_REG, ledpat.pat[i]);
100 
101 	i = i+1;
102 	if (i == ledpat.patlen)
103 		i = 0;
104 	led_px = i;
105 }
106 
107 /*
108  * This is called by mem.c to handle /dev/leds
109  */
110 int
111 leds_uio(struct uio *uio)
112 {
113 	int cnt, error;
114 	int off;	/* NOT off_t */
115 	void *va;
116 
117 	off = uio->uio_offset;
118 	if ((off < 0) || (off > sizeof(ledpat)))
119 		return (EIO);
120 
121 	cnt = min(uio->uio_resid, (sizeof(ledpat) - off));
122 	if (cnt == 0)
123 		return (0); /* EOF */
124 
125 	va = ((char*)(&ledpat)) + off;
126 	error = uiomove(va, cnt, uio);
127 
128 	return (error);
129 }
130