xref: /haiku/src/add-ons/kernel/drivers/graphics/intel_extreme/driver.cpp (revision f09ba8ea46ba2f4e482d7cd03e8eb77f37a60663)
1 /*
2  * Copyright 2006-2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include "driver.h"
11 #include "device.h"
12 #include "lock.h"
13 
14 #include <OS.h>
15 #include <KernelExport.h>
16 #include <SupportDefs.h>
17 #include <PCI.h>
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <malloc.h>
23 
24 #define TRACE_DRIVER
25 #ifdef TRACE_DRIVER
26 #	define TRACE(x) dprintf x
27 #else
28 #	define TRACE(x) ;
29 #endif
30 
31 #define MAX_CARDS 4
32 
33 // list of supported devices
34 const struct supported_device {
35 	uint32		device_id;
36 	int32		type;
37 	const char	*name;
38 } kSupportedDevices[] = {
39 	{0x3577, INTEL_TYPE_8xx | INTEL_TYPE_83x, "i830GM"},
40 	{0x2562, INTEL_TYPE_8xx | INTEL_TYPE_83x, "i845G"},
41 
42 	{0x2572, INTEL_TYPE_8xx | INTEL_TYPE_85x, "i865G"},
43 	{0x3582, INTEL_TYPE_8xx | INTEL_TYPE_85x, "i855G"},
44 
45 #if 1
46 	{0x2792, INTEL_TYPE_9xx, "i910"},
47 	{0x258a, INTEL_TYPE_9xx, "i915"},
48 	{0x2582, INTEL_TYPE_9xx, "i915G"},
49 	{0x2592, INTEL_TYPE_9xx, "i915GM"},
50 	{0x2772, INTEL_TYPE_9xx, "i945G"},
51 	{0x27a2, INTEL_TYPE_9xx, "i945GM"},
52 	{0x29a2, INTEL_TYPE_9xx | INTEL_TYPE_965, "i965G"}
53 #endif
54 };
55 
56 int32 api_version = B_CUR_DRIVER_API_VERSION;
57 
58 char *gDeviceNames[MAX_CARDS + 1];
59 intel_info *gDeviceInfo[MAX_CARDS];
60 pci_module_info *gPCI;
61 lock gLock;
62 
63 
64 static status_t
65 get_next_intel_extreme(int32 *_cookie, pci_info &info, uint32 &type)
66 {
67 	int32 index = *_cookie;
68 
69 	// find devices
70 
71 	for (; gPCI->get_nth_pci_info(index, &info) == B_OK; index++) {
72 		// check vendor
73 		if (info.vendor_id != VENDOR_ID_INTEL
74 			|| info.class_base != PCI_display
75 			|| info.class_sub != 0)
76 			continue;
77 
78 		// check device
79 		for (uint32 i = 0; i < sizeof(kSupportedDevices)
80 				/ sizeof(kSupportedDevices[0]); i++) {
81 			if (info.device_id == kSupportedDevices[i].device_id) {
82 				type = i;
83 				*_cookie = index + 1;
84 				return B_OK;
85 			}
86 		}
87 	}
88 
89 	return B_ENTRY_NOT_FOUND;
90 }
91 
92 
93 extern "C" const char **
94 publish_devices(void)
95 {
96 	TRACE((DEVICE_NAME ": publish_devices()\n"));
97 	return (const char **)gDeviceNames;
98 }
99 
100 
101 extern "C" status_t
102 init_hardware(void)
103 {
104 	TRACE((DEVICE_NAME ": init_hardware()\n"));
105 
106 	status_t status = get_module(B_PCI_MODULE_NAME,(module_info **)&gPCI);
107 	if (status != B_OK) {
108 		TRACE((DEVICE_NAME ": pci module unavailable\n"));
109 		return status;
110 	}
111 
112 	int32 cookie = 0;
113 	uint32 type;
114 	pci_info info;
115 	status = get_next_intel_extreme(&cookie, info, type);
116 
117 	put_module(B_PCI_MODULE_NAME);
118 	return status;
119 }
120 
121 
122 extern "C" status_t
123 init_driver(void)
124 {
125 	TRACE((DEVICE_NAME ": init_driver()\n"));
126 
127 	status_t status = get_module(B_PCI_MODULE_NAME,(module_info **)&gPCI);
128 	if (status != B_OK) {
129 		TRACE((DEVICE_NAME ": pci module unavailable\n"));
130 		return status;
131 	}
132 
133 	status = init_lock(&gLock, "intel extreme ksync");
134 	if (status < B_OK)
135 		return status;
136 
137 	// find devices
138 
139 	int32 found = 0;
140 
141 	for (int32 cookie = 0; found < MAX_CARDS;) {
142 		pci_info *info = (pci_info *)malloc(sizeof(pci_info));
143 		if (info == NULL)
144 			break;
145 
146 		uint32 type;
147 		status = get_next_intel_extreme(&cookie, *info, type);
148 		if (status < B_OK) {
149 			free(info);
150 			break;
151 		}
152 
153 		// create device names & allocate device info structure
154 
155 		char name[64];
156 		sprintf(name, "graphics/intel_extreme_%02x%02x%02x",
157 			 info->bus, info->device,
158 			 info->function);
159 
160 		gDeviceNames[found] = strdup(name);
161 		if (gDeviceNames[found] == NULL)
162 			break;
163 
164 		gDeviceInfo[found] = (intel_info *)malloc(sizeof(intel_info));
165 		if (gDeviceInfo[found] == NULL) {
166 			free(gDeviceNames[found]);
167 			break;
168 		}
169 
170 		// initialize the structure for later use
171 
172 		memset(gDeviceInfo[found], 0, sizeof(intel_info));
173 		gDeviceInfo[found]->init_status = B_NO_INIT;
174 		gDeviceInfo[found]->id = found;
175 		gDeviceInfo[found]->pci = info;
176 		gDeviceInfo[found]->registers = (uint8 *)info->u.h0.base_registers[0];
177 		gDeviceInfo[found]->device_identifier = kSupportedDevices[type].name;
178 		gDeviceInfo[found]->device_type = kSupportedDevices[type].type;
179 
180 		dprintf(DEVICE_NAME ": (%ld) %s, revision = 0x%x\n", found,
181 			kSupportedDevices[type].name, info->revision);
182 		found++;
183 	}
184 
185 	gDeviceNames[found] = NULL;
186 
187 	if (found == 0) {
188 		uninit_lock(&gLock);
189 		put_module(B_PCI_MODULE_NAME);
190 		return ENODEV;
191 	}
192 
193 	return B_OK;
194 }
195 
196 
197 extern "C" void
198 uninit_driver(void)
199 {
200 	TRACE((DEVICE_NAME ": uninit_driver()\n"));
201 
202 	uninit_lock(&gLock);
203 
204 	// free device related structures
205 	char *name;
206 	for (int32 index = 0; (name = gDeviceNames[index]) != NULL; index++) {
207 		free(gDeviceInfo[index]);
208 		free(name);
209 	}
210 
211 	put_module(B_PCI_MODULE_NAME);
212 }
213 
214 
215 extern "C" device_hooks *
216 find_device(const char *name)
217 {
218 	int index;
219 
220 	TRACE((DEVICE_NAME ": find_device()\n"));
221 
222 	for (index = 0; gDeviceNames[index] != NULL; index++) {
223 		if (!strcmp(name, gDeviceNames[index]))
224 			return &gDeviceHooks;
225 	}
226 
227 	return NULL;
228 }
229 
230 /*
231 extern "C" void
232 wake_driver(void)
233 {
234 	// for compatibility with Dano, only
235 }
236 
237 
238 extern "C" void
239 suspend_driver(void)
240 {
241 	// for compatibility with Dano, only
242 }
243 */
244