xref: /haiku/src/add-ons/kernel/drivers/graphics/vesa/vesa.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <boot_item.h>
8 #include <frame_buffer_console.h>
9 
10 #include "vesa_info.h"
11 #include "driver.h"
12 #include "utility.h"
13 
14 #include "util/kernel_cpp.h"
15 
16 #include <string.h>
17 
18 
19 class PhysicalMemoryMapper {
20 	public:
21 		PhysicalMemoryMapper();
22 		~PhysicalMemoryMapper();
23 
24 		area_id Map(const char *name, void *physicalAddress, size_t numBytes,
25 			uint32 spec, uint32 protection, void **virtualAddress);
26 		status_t InitCheck() { return fArea < B_OK ? (status_t)fArea : B_OK; }
27 		void Keep();
28 
29 	private:
30 		area_id	fArea;
31 };
32 
33 
34 PhysicalMemoryMapper::PhysicalMemoryMapper()
35 	:
36 	fArea(-1)
37 {
38 }
39 
40 
41 PhysicalMemoryMapper::~PhysicalMemoryMapper()
42 {
43 	if (fArea >= B_OK)
44 		delete_area(fArea);
45 }
46 
47 
48 area_id
49 PhysicalMemoryMapper::Map(const char *name, void *physicalAddress,
50 	size_t numBytes, uint32 spec, uint32 protection, void **virtualAddress)
51 {
52 	fArea = map_physical_memory(name, physicalAddress, numBytes, spec,
53 		protection, virtualAddress);
54 	return fArea;
55 }
56 
57 
58 void
59 PhysicalMemoryMapper::Keep()
60 {
61 	fArea = -1;
62 }
63 
64 
65 //	#pragma mark -
66 
67 
68 status_t
69 vesa_init(vesa_info &info)
70 {
71 	frame_buffer_boot_info *bufferInfo
72 		= (frame_buffer_boot_info *)get_boot_item(FRAME_BUFFER_BOOT_INFO);
73 	if (bufferInfo == NULL)
74 		return B_ERROR;
75 
76 	info.shared_area = create_area("vesa shared info", (void **)&info.shared_info,
77 			B_ANY_KERNEL_ADDRESS, ROUND_TO_PAGE_SIZE(sizeof(vesa_shared_info)),
78 			B_FULL_LOCK,
79 			B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_USER_CLONEABLE_AREA);
80 	if (info.shared_area < B_OK)
81 		return info.shared_area;
82 
83 	memset((void *)info.shared_info, 0, sizeof(vesa_shared_info));
84 
85 	info.shared_info->frame_buffer_area = bufferInfo->area;
86 	info.shared_info->frame_buffer = (uint8 *)bufferInfo->frame_buffer;
87 
88 	info.shared_info->current_mode.virtual_width = bufferInfo->width;
89 	info.shared_info->current_mode.virtual_height = bufferInfo->height;
90 	switch (bufferInfo->depth) {
91 		case 4:
92 			info.shared_info->current_mode.space = B_GRAY8;
93 				// the app_server is smart enough to translate this to VGA mode
94 			break;
95 		case 8:
96 			info.shared_info->current_mode.space = B_CMAP8;
97 			break;
98 		case 15:
99 			info.shared_info->current_mode.space = B_RGB15;
100 			break;
101 		case 16:
102 			info.shared_info->current_mode.space = B_RGB16;
103 			break;
104 		case 24:
105 			info.shared_info->current_mode.space = B_RGB24;
106 			break;
107 		case 32:
108 			info.shared_info->current_mode.space = B_RGB32;
109 			break;
110 	}
111 	info.shared_info->bytes_per_row = bufferInfo->bytes_per_row;
112 
113 	physical_entry mapping;
114 	get_memory_map((void *)info.shared_info->frame_buffer, B_PAGE_SIZE,
115 		&mapping, 1);
116 	info.shared_info->physical_frame_buffer = (uint8 *)mapping.address;
117 
118 	dprintf(DEVICE_NAME ": vesa_init() completed successfully!\n");
119 	return B_OK;
120 }
121 
122 
123 void
124 vesa_uninit(vesa_info &info)
125 {
126 	dprintf(DEVICE_NAME": vesa_uninit()\n");
127 
128 	delete_area(info.shared_info->frame_buffer_area);
129 	delete_area(info.shared_area);
130 }
131 
132