FreeBSD/Linux Kernel Cross Reference
sys/i386/xbox/xboxfb.c
1 /*-
2 * Copyright (c) 2005, 2006 Rink Springer <rink@il.fontys.nl>
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.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30 /*
31 * This is the syscon(4)-ized version of the Xbox Frame Buffer driver. It
32 * supports about all features required, such as mouse support.
33 *
34 * A lot of functions that are not useful to us have not been implemented.
35 * It appears that some functions are never called, but these implementations
36 * are here nevertheless.
37 */
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <vm/vm_param.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/cons.h>
44 #include <sys/module.h>
45 #include <sys/conf.h>
46 #include <sys/consio.h>
47 #include <sys/limits.h>
48 #include <sys/tty.h>
49 #include <sys/kbio.h>
50 #include <sys/fbio.h>
51 #include <dev/kbd/kbdreg.h>
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <machine/bus.h>
55 #include <machine/xbox.h>
56 #include <machine/legacyvar.h>
57 #include <dev/fb/fbreg.h>
58 #include <dev/fb/gfb.h>
59 #include <dev/syscons/syscons.h>
60
61 struct xboxfb_softc {
62 video_adapter_t sc_va;
63
64 /* screen height (pixels) */
65 uint32_t sc_height;
66
67 /* screen width (pixels) */
68 uint32_t sc_width;
69
70 /* pointer to the actual XBOX video memory */
71 char* sc_framebuffer;
72
73 /* pointer to the font used */
74 struct gfb_font* sc_font;
75 };
76
77 #define SCREEN_WIDTH 640
78 #define SCREEN_HEIGHT 480
79
80 #define XBOXFB_DRIVER_NAME "xboxsc"
81
82 extern struct gfb_font bold8x16;
83
84 static vi_probe_t xboxfb_probe;
85 static vi_init_t xboxfb_init;
86 static vi_get_info_t xboxfb_get_info;
87 static vi_query_mode_t xboxfb_query_mode;
88 static vi_set_mode_t xboxfb_set_mode;
89 static vi_save_font_t xboxfb_save_font;
90 static vi_load_font_t xboxfb_load_font;
91 static vi_show_font_t xboxfb_show_font;
92 static vi_save_palette_t xboxfb_save_palette;
93 static vi_load_palette_t xboxfb_load_palette;
94 static vi_set_border_t xboxfb_set_border;
95 static vi_save_state_t xboxfb_save_state;
96 static vi_load_state_t xboxfb_load_state;
97 static vi_set_win_org_t xboxfb_set_win_org;
98 static vi_read_hw_cursor_t xboxfb_read_hw_cursor;
99 static vi_set_hw_cursor_t xboxfb_set_hw_cursor;
100 static vi_set_hw_cursor_shape_t xboxfb_set_hw_cursor_shape;
101 static vi_blank_display_t xboxfb_blank_display;
102 static vi_mmap_t xboxfb_mmap;
103 static vi_ioctl_t xboxfb_ioctl;
104 static vi_clear_t xboxfb_clear;
105 static vi_fill_rect_t xboxfb_fill_rect;
106 static vi_bitblt_t xboxfb_bitblt;
107 static vi_diag_t xboxfb_diag;
108 static vi_save_cursor_palette_t xboxfb_save_cursor_palette;
109 static vi_load_cursor_palette_t xboxfb_load_cursor_palette;
110 static vi_copy_t xboxfb_copy;
111 static vi_putp_t xboxfb_putp;
112 static vi_putc_t xboxfb_putc;
113 static vi_puts_t xboxfb_puts;
114 static vi_putm_t xboxfb_putm;
115
116 static video_switch_t xboxvidsw = {
117 .probe = xboxfb_probe,
118 .init = xboxfb_init,
119 .get_info = xboxfb_get_info,
120 .query_mode = xboxfb_query_mode,
121 .set_mode = xboxfb_set_mode,
122 .save_font = xboxfb_save_font,
123 .load_font = xboxfb_load_font,
124 .show_font = xboxfb_show_font,
125 .save_palette = xboxfb_save_palette,
126 .load_palette = xboxfb_load_palette,
127 .set_border = xboxfb_set_border,
128 .save_state = xboxfb_save_state,
129 .load_state = xboxfb_load_state,
130 .set_win_org = xboxfb_set_win_org,
131 .read_hw_cursor = xboxfb_read_hw_cursor,
132 .set_hw_cursor = xboxfb_set_hw_cursor,
133 .set_hw_cursor_shape = xboxfb_set_hw_cursor_shape,
134 .blank_display = xboxfb_blank_display,
135 .mmap = xboxfb_mmap,
136 .ioctl = xboxfb_ioctl,
137 .clear = xboxfb_clear,
138 .fill_rect = xboxfb_fill_rect,
139 .bitblt = xboxfb_bitblt,
140 NULL,
141 NULL,
142 .diag = xboxfb_diag,
143 .save_cursor_palette = xboxfb_save_cursor_palette,
144 .load_cursor_palette = xboxfb_load_cursor_palette,
145 .copy = xboxfb_copy,
146 .putp = xboxfb_putp,
147 .putc = xboxfb_putc,
148 .puts = xboxfb_puts,
149 .putm = xboxfb_putm
150 };
151
152 static int xboxfb_configure(int flags);
153 VIDEO_DRIVER(xboxsc, xboxvidsw, xboxfb_configure);
154
155 static vr_init_t xbr_init;
156 static vr_clear_t xbr_clear;
157 static vr_draw_border_t xbr_draw_border;
158 static vr_draw_t xbr_draw;
159 static vr_set_cursor_t xbr_set_cursor;
160 static vr_draw_cursor_t xbr_draw_cursor;
161 static vr_blink_cursor_t xbr_blink_cursor;
162 static vr_set_mouse_t xbr_set_mouse;
163 static vr_draw_mouse_t xbr_draw_mouse;
164
165 /*
166 * We use our own renderer; this is because we must emulate a hardware
167 * cursor.
168 */
169 static sc_rndr_sw_t xboxrend = {
170 xbr_init,
171 xbr_clear,
172 xbr_draw_border,
173 xbr_draw,
174 xbr_set_cursor,
175 xbr_draw_cursor,
176 xbr_blink_cursor,
177 xbr_set_mouse,
178 xbr_draw_mouse
179 };
180 RENDERER(xboxsc, 0, xboxrend, gfb_set);
181
182 static struct xboxfb_softc xboxfb_sc;
183
184 /* color mappings, from dev/fb/creator.c */
185 static const uint32_t cmap[] = {
186 0x00000000, /* black */
187 0x000000ff, /* blue */
188 0x0000ff00, /* green */
189 0x0000c0c0, /* cyan */
190 0x00ff0000, /* red */
191 0x00c000c0, /* magenta */
192 0x00c0c000, /* brown */
193 0x00c0c0c0, /* light grey */
194 0x00808080, /* dark grey */
195 0x008080ff, /* light blue */
196 0x0080ff80, /* light green */
197 0x0080ffff, /* light cyan */
198 0x00ff8080, /* light red */
199 0x00ff80ff, /* light magenta */
200 0x00ffff80, /* yellow */
201 0x00ffffff /* white */
202 };
203
204 /* mouse pointer from dev/syscons/scgfbrndr.c */
205 static u_char mouse_pointer[16] = {
206 0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
207 0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
208 };
209
210 static int
211 xboxfb_init(int unit, video_adapter_t* adp, int flags)
212 {
213 struct xboxfb_softc* sc = &xboxfb_sc;
214 video_info_t* vi;
215 int i;
216 int* iptr;
217
218 vi = &adp->va_info;
219
220 vid_init_struct (adp, XBOXFB_DRIVER_NAME, -1, unit);
221 sc->sc_height = SCREEN_HEIGHT;
222 sc->sc_width = SCREEN_WIDTH;
223 sc->sc_font = &bold8x16;
224 if (!(adp->va_flags & V_ADP_INITIALIZED)) {
225 /*
226 * We must make a mapping from video framebuffer memory
227 * to real. This is very crude: we map the entire
228 * videomemory to PAGE_SIZE! Since our kernel lives at
229 * it's relocated address range (0xc0xxxxxx), it won't
230 * care.
231 *
232 * We use address PAGE_SIZE and up so we can still trap
233 * NULL pointers. Once the real init is called, the
234 * mapping will be done via the OS and stored in a more
235 * sensible location ... but since we're not fully
236 * initialized, this is our only way to go :-(
237 */
238 for (i = 0; i < (XBOX_FB_SIZE / PAGE_SIZE); i++) {
239 pmap_kenter (((i + 1) * PAGE_SIZE), XBOX_FB_START + (i * PAGE_SIZE));
240 }
241 pmap_kenter ((i + 1) * PAGE_SIZE, XBOX_FB_START_PTR - XBOX_FB_START_PTR % PAGE_SIZE);
242 sc->sc_framebuffer = (char*)PAGE_SIZE;
243
244 /* ensure the framebuffer is where we want it to be */
245 *(uint32_t*)((i + 1) * PAGE_SIZE + XBOX_FB_START_PTR % PAGE_SIZE) = XBOX_FB_START;
246
247 /* clear the screen */
248 iptr = (uint32_t*)sc->sc_framebuffer;
249 for (i = 0; i < sc->sc_height * sc->sc_width; i++)
250 *iptr++ = cmap[0];
251
252 /* don't ever do this again! */
253 adp->va_flags |= V_ADP_INITIALIZED;
254 }
255
256 vi->vi_mode = M_TEXT_80x25;
257 vi->vi_cwidth = sc->sc_font->width;
258 vi->vi_cheight = sc->sc_font->height;
259 vi->vi_height = (sc->sc_height / vi->vi_cheight);
260 vi->vi_width = (sc->sc_width / vi->vi_cwidth);
261 vi->vi_flags = V_INFO_COLOR | V_INFO_LINEAR;
262 vi->vi_mem_model = V_INFO_MM_DIRECT;
263
264 adp->va_flags |= V_ADP_COLOR;
265
266 if (vid_register(adp) < 0)
267 return (ENXIO);
268
269 adp->va_flags |= V_ADP_REGISTERED;
270
271 return 0;
272 }
273
274 static int
275 xboxfb_probe(int unit, video_adapter_t** adp, void* arg, int flags)
276 {
277 return 0;
278 }
279
280 static int
281 xboxfb_configure(int flags)
282 {
283 struct xboxfb_softc* sc = &xboxfb_sc;
284
285 /* Don't init the framebuffer on non-XBOX-es */
286 if (!arch_i386_is_xbox)
287 return 0;
288
289 /*
290 * If we do only a probe, we are in such an early boot stadium
291 * that we cannot yet do a 'clean' initialization.
292 */
293 if (flags & VIO_PROBE_ONLY) {
294 xboxfb_init(0, &sc->sc_va, 0);
295 return 1;
296 }
297
298 /* Do a clean mapping of the framebuffer memory */
299 sc->sc_framebuffer = pmap_mapdev (XBOX_FB_START, XBOX_FB_SIZE);
300 return 1;
301 }
302
303 static void
304 sc_identify(driver_t* driver, device_t parent)
305 {
306 BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
307 }
308
309 static int
310 sc_probe(device_t dev)
311 {
312 device_set_desc(dev, "XBox System console");
313 return (sc_probe_unit(device_get_unit(dev), device_get_flags(dev) | SC_AUTODETECT_KBD));
314 }
315
316 static int sc_attach(device_t dev)
317 {
318 return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) | SC_AUTODETECT_KBD));
319 }
320
321 static device_method_t sc_methods[] = {
322 /* Device interface */
323 DEVMETHOD(device_identify, sc_identify),
324 DEVMETHOD(device_probe, sc_probe),
325 DEVMETHOD(device_attach, sc_attach),
326 { 0, 0 }
327 };
328
329 static driver_t xboxfb_sc_driver = {
330 SC_DRIVER_NAME,
331 sc_methods,
332 sizeof(sc_softc_t)
333 };
334
335 static devclass_t sc_devclass;
336
337 DRIVER_MODULE(sc, legacy, xboxfb_sc_driver, sc_devclass, 0, 0);
338
339 static void
340 xbr_init(scr_stat* scp)
341 {
342 }
343
344 static void
345 xbr_clear(scr_stat* scp, int c, int attr)
346 {
347 }
348
349 static void
350 xbr_draw_border(scr_stat* scp, int color)
351 {
352 }
353
354 static void
355 xbr_draw(scr_stat* scp, int from, int count, int flip)
356 {
357 video_adapter_t* adp = scp->sc->adp;
358 int i, c, a;
359
360 if (!flip) {
361 /* Normal printing */
362 (*vidsw[scp->sc->adapter]->puts)(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
363 } else {
364 /* This is for selections and such: invert the color attribute */
365 for (i = count; i-- > 0; ++from) {
366 c = sc_vtb_getc(&scp->vtb, from);
367 a = sc_vtb_geta(&scp->vtb, from) >> 8;
368 (*vidsw[scp->sc->adapter]->putc)(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
369 }
370 }
371 }
372
373 static void
374 xbr_set_cursor(scr_stat* scp, int base, int height, int blink)
375 {
376 }
377
378 static void
379 xbr_draw_cursor(scr_stat* scp, int at, int blink, int on, int flip)
380 {
381 struct xboxfb_softc* sc = &xboxfb_sc;
382 video_adapter_t* adp = scp->sc->adp;
383 uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
384 int row, col, i, j;
385
386 if (scp->curs_attr.height <= 0)
387 return;
388
389 /* calculate the coordinates in the video buffer */
390 row = (at / adp->va_info.vi_width) * adp->va_info.vi_cheight;
391 col = (at % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
392 ptri += (row * sc->sc_width) + col;
393
394 /* our cursor consists of simply inverting the char under it */
395 for (i = 0; i < adp->va_info.vi_cheight; i++) {
396 for (j = 0; j < adp->va_info.vi_cwidth; j++) {
397 *ptri++ ^= 0x00FFFFFF;
398 }
399 ptri += (sc->sc_width - adp->va_info.vi_cwidth);
400 }
401 }
402
403 static void
404 xbr_blink_cursor(scr_stat* scp, int at, int flip)
405 {
406 }
407
408 static void
409 xbr_set_mouse(scr_stat* scp)
410 {
411 }
412
413 static void
414 xbr_draw_mouse(scr_stat* scp, int x, int y, int on)
415 {
416 (*vidsw[scp->sc->adapter]->putm)(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
417
418 }
419
420 static int
421 xboxfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
422 {
423 bcopy(&adp->va_info, info, sizeof(*info));
424 return (0);
425 }
426
427 static int
428 xboxfb_query_mode(video_adapter_t *adp, video_info_t *info)
429 {
430 return (ENODEV);
431 }
432
433 static int
434 xboxfb_set_mode(video_adapter_t *adp, int mode)
435 {
436 return (0);
437 }
438
439 static int
440 xboxfb_save_font(video_adapter_t *adp, int page, int size, int width,
441 u_char *data, int c, int count)
442 {
443 return (ENODEV);
444 }
445
446 static int
447 xboxfb_load_font(video_adapter_t *adp, int page, int size, int width,
448 u_char *data, int c, int count)
449 {
450 return (ENODEV);
451 }
452
453 static int
454 xboxfb_show_font(video_adapter_t *adp, int page)
455 {
456 return (ENODEV);
457 }
458
459 static int
460 xboxfb_save_palette(video_adapter_t *adp, u_char *palette)
461 {
462 return (ENODEV);
463 }
464
465 static int
466 xboxfb_load_palette(video_adapter_t *adp, u_char *palette)
467 {
468 return (ENODEV);
469 }
470
471 static int
472 xboxfb_set_border(video_adapter_t *adp, int border)
473 {
474 return (0);
475 }
476
477 static int
478 xboxfb_save_state(video_adapter_t *adp, void *p, size_t size)
479 {
480 return (ENODEV);
481 }
482
483 static int
484 xboxfb_load_state(video_adapter_t *adp, void *p)
485 {
486 return (ENODEV);
487 }
488
489 static int
490 xboxfb_set_win_org(video_adapter_t *adp, off_t offset)
491 {
492 return (ENODEV);
493 }
494
495 static int
496 xboxfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
497 {
498 *col = 0;
499 *row = 0;
500 return (0);
501 }
502
503 static int
504 xboxfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
505 {
506 return (ENODEV);
507 }
508
509 static int
510 xboxfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
511 int celsize, int blink)
512 {
513 return (ENODEV);
514 }
515
516 static int
517 xboxfb_blank_display(video_adapter_t *adp, int mode)
518 {
519 return (0);
520 }
521
522 static int
523 xboxfb_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
524 int prot)
525 {
526 return (EINVAL);
527 }
528
529 static int
530 xboxfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
531 {
532 return (fb_commonioctl(adp, cmd, data));
533 }
534
535 static int
536 xboxfb_clear(video_adapter_t *adp)
537 {
538 return (0);
539 }
540
541 static int
542 xboxfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
543 {
544 return (0);
545 }
546
547 static int
548 xboxfb_bitblt(video_adapter_t *adp, ...)
549 {
550 return (ENODEV);
551 }
552
553 static int
554 xboxfb_diag(video_adapter_t *adp, int level)
555 {
556 video_info_t info;
557
558 fb_dump_adp_info(adp->va_name, adp, level);
559 xboxfb_get_info(adp, 0, &info);
560 fb_dump_mode_info(adp->va_name, adp, &info, level);
561 return (0);
562 }
563
564 static int
565 xboxfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
566 {
567 return (ENODEV);
568 }
569
570 static int
571 xboxfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
572 {
573 return (ENODEV);
574 }
575
576 static int
577 xboxfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
578 {
579 return (ENODEV);
580 }
581
582 static int
583 xboxfb_putp(video_adapter_t *adp, vm_offset_t off, u_int32_t p, u_int32_t a,
584 int size, int bpp, int bit_ltor, int byte_ltor)
585 {
586 return (ENODEV);
587 }
588
589 static int
590 xboxfb_putc(video_adapter_t *adp, vm_offset_t off, u_int8_t c, u_int8_t a)
591 {
592 int row, col;
593 int i, j;
594 struct xboxfb_softc* sc = &xboxfb_sc;
595 uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
596 uint8_t* fontdata;
597 uint32_t clr;
598 uint8_t mask;
599
600 /* calculate the position in the frame buffer */
601 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
602 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
603 fontdata = &sc->sc_font->data[c * adp->va_info.vi_cheight];
604 ptri += (row * sc->sc_width) + col;
605
606 /* Place the character on the screen, pixel by pixel */
607 for (j = 0; j < adp->va_info.vi_cheight; j++) {
608 mask = 0x80;
609 for (i = 0; i < adp->va_info.vi_cwidth; i++) {
610 clr = (*fontdata & mask) ? cmap[a & 0xf] : cmap[(a >> 4) & 0xf];
611 *ptri++ = clr;
612 mask >>= 1;
613 }
614 ptri += (sc->sc_width - adp->va_info.vi_cwidth);
615 fontdata++;
616 }
617 return (0);
618 }
619
620 static int
621 xboxfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
622 {
623 int i;
624
625 for (i = 0; i < len; i++) {
626 (*vidsw[adp->va_index]->putc)(adp, off + i, s[i] & 0xff,
627 (s[i] & 0xff00) >> 8);
628 }
629 return (0);
630 }
631
632 static int
633 xboxfb_putm(video_adapter_t *adp, int x, int y, u_int8_t *pixel_image,
634 u_int32_t pixel_mask, int size, int width)
635 {
636 struct xboxfb_softc* sc = &xboxfb_sc;
637 uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
638 int i, j;
639
640 if (x < 0 || y < 0 || x + width > sc->sc_width || y + (2 * size) > sc->sc_height)
641 return 0;
642
643 ptri += (y * sc->sc_width) + x;
644
645 /* plot the mousecursor wherever the user wants it */
646 for (j = 0; j < size; j++) {
647 for (i = width; i > 0; i--) {
648 if (pixel_image[j] & (1 << i))
649 *ptri = cmap[0xf];
650 ptri++;
651 }
652 ptri += (sc->sc_width - width);
653 }
654 return (0);
655 }
Cache object: b801c2509264f6b20c6735b93ae2275c
|