xref: /freebsd/sys/dev/fb/splash.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/linker.h>
33 #include <sys/fbio.h>
34 #include <sys/kernel.h>
35 
36 #include <dev/fb/fbreg.h>
37 #include <dev/fb/splashreg.h>
38 
39 MODULE_VERSION(splash, 1);
40 
41 /* video adapter and image decoder */
42 static video_adapter_t	*splash_adp;
43 static splash_decoder_t	*splash_decoder;
44 
45 /* decoder candidates */
46 static int		decoders;
47 static splash_decoder_t **decoder_set;
48 #define DECODER_ARRAY_DELTA 4
49 
50 /* console driver callback */
51 static int		(*splash_callback)(int, void *);
52 static void		*splash_arg;
53 
54 static int
55 splash_find_data(splash_decoder_t *decoder)
56 {
57         caddr_t image_module;
58 	caddr_t p;
59 
60 	if (decoder->data_type == NULL)
61 		return 0;
62 	image_module = preload_search_by_type(decoder->data_type);
63 	if (image_module == NULL)
64 		return ENOENT;
65 	p = preload_search_info(image_module, MODINFO_ADDR);
66 	if (p == NULL)
67 		return ENOENT;
68 	decoder->data = *(void **)p;
69 	p = preload_search_info(image_module, MODINFO_SIZE);
70 	if (p == NULL)
71 		return ENOENT;
72 	decoder->data_size = *(size_t *)p;
73 	if (bootverbose)
74 		printf("splash: image@%p, size:%lu\n",
75 		       (void *)decoder->data, (long)decoder->data_size);
76 	return 0;
77 }
78 
79 static int
80 splash_test(splash_decoder_t *decoder)
81 {
82 	if (splash_find_data(decoder))
83 		return ENOENT;	/* XXX */
84 	if (*decoder->init && (*decoder->init)(splash_adp)) {
85 		decoder->data = NULL;
86 		decoder->data_size = 0;
87 		return ENODEV;	/* XXX */
88 	}
89 	if (bootverbose)
90 		printf("splash: image decoder found: %s\n", decoder->name);
91 	return 0;
92 }
93 
94 static void
95 splash_new(splash_decoder_t *decoder)
96 {
97 	splash_decoder = decoder;
98 	if (splash_callback != NULL)
99 		(*splash_callback)(SPLASH_INIT, splash_arg);
100 }
101 
102 int
103 splash_register(splash_decoder_t *decoder)
104 {
105 	splash_decoder_t **p;
106 	int error;
107 	int i;
108 
109 	if (splash_adp != NULL) {
110 		/*
111 		 * If the video card has aleady been initialized, test
112 		 * this decoder immediately.
113 		 */
114 		error = splash_test(decoder);
115 		if (error == 0) {
116 			/* replace the current decoder with new one */
117 			if (splash_decoder != NULL)
118 				error = splash_term(splash_adp);
119 			if (error == 0)
120 				splash_new(decoder);
121 		}
122 		return error;
123 	} else {
124 		/* register the decoder for later use */
125 		for (i = 0; i < decoders; ++i) {
126 			if (decoder_set[i] == NULL)
127 				break;
128 		}
129 		if ((i >= decoders) && (decoders % DECODER_ARRAY_DELTA) == 0) {
130 			p = malloc(sizeof(*p)*(decoders + DECODER_ARRAY_DELTA),
131 			   	M_DEVBUF, M_NOWAIT);
132 			if (p == NULL)
133 				return ENOMEM;
134 			if (decoder_set != NULL) {
135 				bcopy(decoder_set, p, sizeof(*p)*decoders);
136 				free(decoder_set, M_DEVBUF);
137 			}
138 			decoder_set = p;
139 			i = decoders++;
140 		}
141 		decoder_set[i] = decoder;
142 	}
143 
144 	return 0;
145 }
146 
147 int
148 splash_unregister(splash_decoder_t *decoder)
149 {
150 	int error;
151 
152 	if (splash_decoder == decoder) {
153 		if ((error = splash_term(splash_adp)) != 0)
154 			return error;
155 	}
156 	return 0;
157 }
158 
159 int
160 splash_init(video_adapter_t *adp, int (*callback)(int, void *), void *arg)
161 {
162 	int i;
163 
164 	splash_adp = adp;
165 	splash_callback = callback;
166 	splash_arg = arg;
167 
168 	splash_decoder = NULL;
169 	for (i = 0; i < decoders; ++i) {
170 		if (decoder_set[i] == NULL)
171 			continue;
172 		if (splash_test(decoder_set[i]) == 0) {
173 			splash_new(decoder_set[i]);
174 			break;
175 		}
176 		decoder_set[i] = NULL;
177 	}
178 	for (++i; i < decoders; ++i) {
179 		decoder_set[i] = NULL;
180 	}
181 	return 0;
182 }
183 
184 int
185 splash_term(video_adapter_t *adp)
186 {
187 	int error = 0;
188 
189 	if (splash_adp != adp)
190 		return EINVAL;
191 	if (splash_decoder != NULL) {
192 		if (splash_callback != NULL)
193 			error = (*splash_callback)(SPLASH_TERM, splash_arg);
194 		if (error == 0 && splash_decoder->term)
195 			error = (*splash_decoder->term)(adp);
196 		if (error == 0)
197 			splash_decoder = NULL;
198 	}
199 	return error;
200 }
201 
202 int
203 splash(video_adapter_t *adp, int on)
204 {
205 	if (splash_decoder != NULL)
206 		return (*splash_decoder->splash)(adp, on);
207 	return ENODEV;
208 }
209