1 /*	$NetBSD: wsdisplay_util.c,v 1.2 2013/01/31 10:57:31 macallan Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 Michael Lorenz
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.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* some utility functions for use with wsdisplay */
30 
31 #include <sys/param.h>
32 #include <sys/stdint.h>
33 #include <sys/systm.h>
34 #include <sys/buf.h>
35 #include <sys/device.h>
36 #include <dev/cons.h>
37 
38 #include <dev/wscons/wsdisplayvar.h>
39 #include <dev/rasops/rasops.h>
40 #include <dev/wscons/wsconsio.h>
41 
42 int
wsdisplayio_get_edid(device_t dev,struct wsdisplayio_edid_info * d)43 wsdisplayio_get_edid(device_t dev, struct wsdisplayio_edid_info *d)
44 {
45 	prop_data_t edid_data;
46 	int edid_size;
47 
48 	edid_data = prop_dictionary_get(device_properties(dev), "EDID");
49 	if (edid_data != NULL) {
50 		edid_size = prop_data_size(edid_data);
51 		/* less than 128 bytes is bogus */
52 		if (edid_size < 128)
53 			return ENODEV;
54 		d->data_size = edid_size;
55 		if (d->buffer_size < edid_size)
56 			return EAGAIN;
57 		return copyout(prop_data_data_nocopy(edid_data),
58 			       d->edid_data, edid_size);
59 	}
60 	return ENODEV;
61 }
62 
63 /* convenience function to fill in stuff from rasops_info */
64 int
wsdisplayio_get_fbinfo(struct rasops_info * ri,struct wsdisplayio_fbinfo * fbi)65 wsdisplayio_get_fbinfo(struct rasops_info *ri, struct wsdisplayio_fbinfo *fbi)
66 {
67 	fbi->fbi_width = ri->ri_width;
68 	fbi->fbi_height = ri->ri_height;
69 	fbi->fbi_stride = ri->ri_stride;
70 	fbi->fbi_bitsperpixel = ri->ri_depth;
71 	if (ri->ri_depth > 8) {
72 		fbi->fbi_pixeltype = WSFB_RGB;
73 		fbi->fbi_subtype.fbi_rgbmasks.red_offset = ri->ri_rpos;
74 		fbi->fbi_subtype.fbi_rgbmasks.red_size = ri->ri_rnum;
75 		fbi->fbi_subtype.fbi_rgbmasks.green_offset = ri->ri_gpos;
76 		fbi->fbi_subtype.fbi_rgbmasks.green_size = ri->ri_gnum;
77 		fbi->fbi_subtype.fbi_rgbmasks.blue_offset = ri->ri_bpos;
78 		fbi->fbi_subtype.fbi_rgbmasks.blue_size = ri->ri_bnum;
79 		fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
80 		fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
81 	} else {
82 		fbi->fbi_pixeltype = WSFB_CI;
83 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = 1 << ri->ri_depth;
84 	}
85 	fbi->fbi_flags = 0;
86 	fbi->fbi_fbsize = ri->ri_stride * ri->ri_height;
87 	fbi->fbi_fboffset = 0;
88 	return 0;
89 }
90