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