xref: /freebsd/sys/dev/vt/hw/fb/vt_fb.c (revision 6d732c66bca5da4d261577aad2c8ea84519b0bea)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Aleksandr Rybalko under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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 AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
39 #include <sys/fbio.h>
40 #include <dev/vt/vt.h>
41 #include <dev/vt/hw/fb/vt_fb.h>
42 #include <dev/vt/colors/vt_termcolors.h>
43 
44 static int vt_fb_ioctl(struct vt_device *vd, u_long cmd, caddr_t data,
45     struct thread *td);
46 static int vt_fb_mmap(struct vt_device *vd, vm_ooffset_t offset,
47     vm_paddr_t *paddr, int prot, vm_memattr_t *memattr);
48 
49 static struct vt_driver vt_fb_driver = {
50 	.vd_init = vt_fb_init,
51 	.vd_blank = vt_fb_blank,
52 	.vd_bitbltchr = vt_fb_bitbltchr,
53 	.vd_postswitch = vt_fb_postswitch,
54 	.vd_priority = VD_PRIORITY_GENERIC+10,
55 	.vd_fb_ioctl = vt_fb_ioctl,
56 	.vd_fb_mmap = vt_fb_mmap,
57 };
58 
59 static int
60 vt_fb_ioctl(struct vt_device *vd, u_long cmd, caddr_t data, struct thread *td)
61 {
62 	struct fb_info *info;
63 
64 	info = vd->vd_softc;
65 
66 	if (info->fb_ioctl == NULL)
67 		return (-1);
68 
69 	return (info->fb_ioctl(info->fb_cdev, cmd, data, 0, td));
70 }
71 
72 static int vt_fb_mmap(struct vt_device *vd, vm_ooffset_t offset,
73     vm_paddr_t *paddr, int prot, vm_memattr_t *memattr)
74 {
75 	struct fb_info *info;
76 
77 	info = vd->vd_softc;
78 
79 	if (info->fb_ioctl == NULL)
80 		return (ENXIO);
81 
82 	return (info->fb_mmap(info->fb_cdev, offset, paddr, prot, memattr));
83 }
84 
85 void
86 vt_fb_blank(struct vt_device *vd, term_color_t color)
87 {
88 	struct fb_info *info;
89 	uint32_t c;
90 	u_int o;
91 
92 	info = vd->vd_softc;
93 	c = info->fb_cmap[color];
94 
95 	switch (FBTYPE_GET_BYTESPP(info)) {
96 	case 1:
97 		for (o = 0; o < info->fb_stride; o++)
98 			info->wr1(info, o, c);
99 		break;
100 	case 2:
101 		for (o = 0; o < info->fb_stride; o += 2)
102 			info->wr2(info, o, c);
103 		break;
104 	case 3:
105 		/* line 0 */
106 		for (o = 0; o < info->fb_stride; o += 3) {
107 			info->wr1(info, o, (c >> 16) & 0xff);
108 			info->wr1(info, o + 1, (c >> 8) & 0xff);
109 			info->wr1(info, o + 2, c & 0xff);
110 		}
111 		break;
112 	case 4:
113 		for (o = 0; o < info->fb_stride; o += 4)
114 			info->wr4(info, o, c);
115 		break;
116 	default:
117 		/* panic? */
118 		return;
119 	}
120 	/* Copy line0 to all other lines. */
121 	/* XXX will copy with borders. */
122 	for (o = info->fb_stride; o < info->fb_size; o += info->fb_stride) {
123 		info->copy(info, o, 0, info->fb_stride);
124 	}
125 }
126 
127 void
128 vt_fb_bitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask,
129     int bpl, vt_axis_t top, vt_axis_t left, unsigned int width,
130     unsigned int height, term_color_t fg, term_color_t bg)
131 {
132 	struct fb_info *info;
133 	uint32_t fgc, bgc, cc, o;
134 	int c, l, bpp;
135 	u_long line;
136 	uint8_t b, m;
137 	const uint8_t *ch;
138 
139 	info = vd->vd_softc;
140 	bpp = FBTYPE_GET_BYTESPP(info);
141 	fgc = info->fb_cmap[fg];
142 	bgc = info->fb_cmap[bg];
143 	b = m = 0;
144 	if (bpl == 0)
145 		bpl = (width + 7) >> 3; /* Bytes per sorce line. */
146 
147 	/* Don't try to put off screen pixels */
148 	if (((left + width) > info->fb_width) || ((top + height) >
149 	    info->fb_height))
150 		return;
151 
152 	line = (info->fb_stride * top) + (left * bpp);
153 	for (l = 0; l < height; l++) {
154 		ch = src;
155 		for (c = 0; c < width; c++) {
156 			if (c % 8 == 0)
157 				b = *ch++;
158 			else
159 				b <<= 1;
160 			if (mask != NULL) {
161 				if (c % 8 == 0)
162 					m = *mask++;
163 				else
164 					m <<= 1;
165 				/* Skip pixel write, if mask has no bit set. */
166 				if ((m & 0x80) == 0)
167 					continue;
168 			}
169 			o = line + (c * bpp);
170 			cc = b & 0x80 ? fgc : bgc;
171 
172 			switch(bpp) {
173 			case 1:
174 				info->wr1(info, o, cc);
175 				break;
176 			case 2:
177 				info->wr2(info, o, cc);
178 				break;
179 			case 3:
180 				/* Packed mode, so unaligned. Byte access. */
181 				info->wr1(info, o, (cc >> 16) & 0xff);
182 				info->wr1(info, o + 1, (cc >> 8) & 0xff);
183 				info->wr1(info, o + 2, cc & 0xff);
184 				break;
185 			case 4:
186 				info->wr4(info, o, cc);
187 				break;
188 			default:
189 				/* panic? */
190 				break;
191 			}
192 		}
193 		line += info->fb_stride;
194 		src += bpl;
195 	}
196 }
197 
198 void
199 vt_fb_postswitch(struct vt_device *vd)
200 {
201 	struct fb_info *info;
202 
203 	info = vd->vd_softc;
204 
205 	if (info->enter != NULL)
206 		info->enter(info->fb_priv);
207 }
208 
209 static int
210 vt_fb_init_cmap(uint32_t *cmap, int depth)
211 {
212 
213 	switch (depth) {
214 	case 8:
215 		return (vt_generate_vga_palette(cmap, COLOR_FORMAT_RGB,
216 		    0x7, 5, 0x7, 2, 0x3, 0));
217 	case 15:
218 		return (vt_generate_vga_palette(cmap, COLOR_FORMAT_RGB,
219 		    0x1f, 10, 0x1f, 5, 0x1f, 0));
220 	case 16:
221 		return (vt_generate_vga_palette(cmap, COLOR_FORMAT_RGB,
222 		    0x1f, 11, 0x3f, 5, 0x1f, 0));
223 	case 24:
224 	case 32: /* Ignore alpha. */
225 		return (vt_generate_vga_palette(cmap, COLOR_FORMAT_RGB,
226 		    0xff, 0, 0xff, 8, 0xff, 16));
227 	default:
228 		return (1);
229 	}
230 }
231 
232 int
233 vt_fb_init(struct vt_device *vd)
234 {
235 	struct fb_info *info;
236 	int err;
237 
238 	info = vd->vd_softc;
239 	vd->vd_height = info->fb_height;
240 	vd->vd_width = info->fb_width;
241 
242 	if (info->fb_cmsize <= 0) {
243 		err = vt_fb_init_cmap(info->fb_cmap, FBTYPE_GET_BPP(info));
244 		if (err)
245 			return (CN_DEAD);
246 		info->fb_cmsize = 16;
247 	}
248 
249 	/* Clear the screen. */
250 	vt_fb_blank(vd, TC_BLACK);
251 
252 	/* Wakeup screen. KMS need this. */
253 	vt_fb_postswitch(vd);
254 
255 	return (CN_INTERNAL);
256 }
257 
258 int
259 vt_fb_attach(struct fb_info *info)
260 {
261 
262 	vt_allocate(&vt_fb_driver, info);
263 
264 	return (0);
265 }
266 
267 void
268 vt_fb_resume(void)
269 {
270 
271 	vt_resume();
272 }
273 
274 void
275 vt_fb_suspend(void)
276 {
277 
278 	vt_suspend();
279 }
280