2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Aleksandr Rybalko under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
40 #include <dev/vt/vt.h>
41 #include <dev/vt/hw/fb/vt_fb.h>
42 #include <dev/vt/colors/vt_termcolors.h>
44 void vt_fb_drawrect(struct vt_device *vd, int x1, int y1, int x2, int y2,
45 int fill, term_color_t color);
46 void vt_fb_setpixel(struct vt_device *vd, int x, int y, term_color_t color);
48 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_maskbitbltchr = vt_fb_maskbitbltchr,
54 .vd_drawrect = vt_fb_drawrect,
55 .vd_setpixel = vt_fb_setpixel,
56 .vd_postswitch = vt_fb_postswitch,
57 .vd_priority = VD_PRIORITY_GENERIC+10,
58 .vd_fb_ioctl = vt_fb_ioctl,
59 .vd_fb_mmap = vt_fb_mmap,
62 VT_DRIVER_DECLARE(vt_fb, vt_fb_driver);
65 vt_fb_ioctl(struct vt_device *vd, u_long cmd, caddr_t data, struct thread *td)
74 bcopy(info, (struct fbtype *)data, sizeof(struct fbtype));
77 case FBIO_GETWINORG: /* get frame buffer window origin */
81 case FBIO_GETDISPSTART: /* get display start address */
82 ((video_display_start_t *)data)->x = 0;
83 ((video_display_start_t *)data)->y = 0;
86 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
87 *(u_int *)data = info->fb_stride;
90 case FBIO_BLANK: /* blank display */
91 if (vd->vd_driver->vd_blank == NULL)
93 vd->vd_driver->vd_blank(vd, TC_BLACK);
105 vt_fb_mmap(struct vt_device *vd, vm_ooffset_t offset, vm_paddr_t *paddr,
106 int prot, vm_memattr_t *memattr)
108 struct fb_info *info;
112 if (info->fb_flags & FB_FLAG_NOMMAP)
115 if (offset >= 0 && offset < info->fb_size) {
116 *paddr = info->fb_pbase + offset;
117 #ifdef VM_MEMATTR_WRITE_COMBINING
118 *memattr = VM_MEMATTR_WRITE_COMBINING;
127 vt_fb_setpixel(struct vt_device *vd, int x, int y, term_color_t color)
129 struct fb_info *info;
134 c = info->fb_cmap[color];
135 o = info->fb_stride * y + x * FBTYPE_GET_BYTESPP(info);
137 switch (FBTYPE_GET_BYTESPP(info)) {
139 info->wr1(info, o, c);
142 info->wr2(info, o, c);
145 info->wr1(info, o, (c >> 16) & 0xff);
146 info->wr1(info, o + 1, (c >> 8) & 0xff);
147 info->wr1(info, o + 2, c & 0xff);
150 info->wr4(info, o, c);
160 vt_fb_drawrect(struct vt_device *vd, int x1, int y1, int x2, int y2, int fill,
165 for (y = y1; y <= y2; y++) {
166 if (fill || (y == y1) || (y == y2)) {
167 for (x = x1; x <= x2; x++)
168 vt_fb_setpixel(vd, x, y, color);
170 vt_fb_setpixel(vd, x1, y, color);
171 vt_fb_setpixel(vd, x2, y, color);
177 vt_fb_blank(struct vt_device *vd, term_color_t color)
179 struct fb_info *info;
184 c = info->fb_cmap[color];
186 switch (FBTYPE_GET_BYTESPP(info)) {
188 for (h = 0; h < info->fb_height; h++)
189 for (o = 0; o < info->fb_stride; o++)
190 info->wr1(info, h*info->fb_stride + o, c);
193 for (h = 0; h < info->fb_height; h++)
194 for (o = 0; o < info->fb_stride; o += 2)
195 info->wr2(info, h*info->fb_stride + o, c);
198 for (h = 0; h < info->fb_height; h++)
199 for (o = 0; o < info->fb_stride; o += 3) {
200 info->wr1(info, h*info->fb_stride + o,
202 info->wr1(info, h*info->fb_stride + o + 1,
204 info->wr1(info, h*info->fb_stride + o + 2,
209 for (h = 0; h < info->fb_height; h++)
210 for (o = 0; o < info->fb_stride; o += 4)
211 info->wr4(info, h*info->fb_stride + o, c);
220 vt_fb_bitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask,
221 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width,
222 unsigned int height, term_color_t fg, term_color_t bg)
224 struct fb_info *info;
225 uint32_t fgc, bgc, cc, o;
232 bpp = FBTYPE_GET_BYTESPP(info);
233 fgc = info->fb_cmap[fg];
234 bgc = info->fb_cmap[bg];
237 bpl = (width + 7) >> 3; /* Bytes per sorce line. */
239 /* Don't try to put off screen pixels */
240 if (((left + width) > info->fb_width) || ((top + height) >
244 line = (info->fb_stride * top) + (left * bpp);
245 for (l = 0; l < height; l++) {
247 for (c = 0; c < width; c++) {
252 o = line + (c * bpp);
253 cc = b & 0x80 ? fgc : bgc;
257 info->wr1(info, o, cc);
260 info->wr2(info, o, cc);
263 /* Packed mode, so unaligned. Byte access. */
264 info->wr1(info, o, (cc >> 16) & 0xff);
265 info->wr1(info, o + 1, (cc >> 8) & 0xff);
266 info->wr1(info, o + 2, cc & 0xff);
269 info->wr4(info, o, cc);
276 line += info->fb_stride;
282 vt_fb_maskbitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask,
283 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width,
284 unsigned int height, term_color_t fg, term_color_t bg)
286 struct fb_info *info;
287 uint32_t fgc, bgc, cc, o;
294 bpp = FBTYPE_GET_BYTESPP(info);
295 fgc = info->fb_cmap[fg];
296 bgc = info->fb_cmap[bg];
299 bpl = (width + 7) >> 3; /* Bytes per sorce line. */
301 /* Don't try to put off screen pixels */
302 if (((left + width) > info->fb_width) || ((top + height) >
306 line = (info->fb_stride * top) + (left * bpp);
307 for (l = 0; l < height; l++) {
309 for (c = 0; c < width; c++) {
319 /* Skip pixel write, if mask has no bit set. */
323 o = line + (c * bpp);
324 cc = b & 0x80 ? fgc : bgc;
328 info->wr1(info, o, cc);
331 info->wr2(info, o, cc);
334 /* Packed mode, so unaligned. Byte access. */
335 info->wr1(info, o, (cc >> 16) & 0xff);
336 info->wr1(info, o + 1, (cc >> 8) & 0xff);
337 info->wr1(info, o + 2, cc & 0xff);
340 info->wr4(info, o, cc);
347 line += info->fb_stride;
353 vt_fb_postswitch(struct vt_device *vd)
355 struct fb_info *info;
359 if (info->enter != NULL)
360 info->enter(info->fb_priv);
364 vt_fb_init_cmap(uint32_t *cmap, int depth)
369 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
370 0x7, 5, 0x7, 2, 0x3, 0));
372 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
373 0x1f, 10, 0x1f, 5, 0x1f, 0));
375 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
376 0x1f, 11, 0x3f, 5, 0x1f, 0));
378 case 32: /* Ignore alpha. */
379 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
380 0xff, 16, 0xff, 8, 0xff, 0));
387 vt_fb_init(struct vt_device *vd)
389 struct fb_info *info;
393 vd->vd_height = info->fb_height;
394 vd->vd_width = info->fb_width;
396 if (info->fb_cmsize <= 0) {
397 err = vt_fb_init_cmap(info->fb_cmap, FBTYPE_GET_BPP(info));
400 info->fb_cmsize = 16;
403 /* Clear the screen. */
404 vt_fb_blank(vd, TC_BLACK);
406 /* Wakeup screen. KMS need this. */
407 vt_fb_postswitch(vd);
409 return (CN_INTERNAL);
413 vt_fb_attach(struct fb_info *info)
416 vt_allocate(&vt_fb_driver, info);