xref: /haiku/src/add-ons/kernel/drivers/graphics/skeleton/driver.c (revision 239222b2369c39dc52df52b0a7cdd6cc0a91bc92)
1 /*
2 	Copyright 1999, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 
5 	Other authors:
6 	Mark Watson;
7 	Rudolf Cornelissen 3/2002-4/2006.
8 */
9 
10 /* standard kernel driver stuff */
11 #include <KernelExport.h>
12 #include <ISA.h>
13 #include <PCI.h>
14 #include <OS.h>
15 #include <driver_settings.h>
16 #include <malloc.h>
17 #include <stdlib.h> // for strtoXX
18 #include "AGP.h"
19 
20 /* this is for the standardized portion of the driver API */
21 /* currently only one operation is defined: B_GET_ACCELERANT_SIGNATURE */
22 #include <graphic_driver.h>
23 
24 /* this is for sprintf() */
25 #include <stdio.h>
26 
27 /* this is for string compares */
28 #include <string.h>
29 
30 /* The private interface between the accelerant and the kernel driver. */
31 #include "DriverInterface.h"
32 #include "macros.h"
33 
34 #define get_pci(o, s) (*pci_bus->read_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s))
35 #define set_pci(o, s, v) (*pci_bus->write_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s), (v))
36 
37 #define MAX_DEVICES	  8
38 
39 #define DEVICE_FORMAT "%04x_%04x_%02x%02x%02x" // apsed
40 
41 /* Tell the kernel what revision of the driver API we support */
42 int32	api_version = B_CUR_DRIVER_API_VERSION; // apsed, was 2, is 2 in R5
43 
44 /* these structures are private to the kernel driver */
45 typedef struct device_info device_info;
46 
47 typedef struct {
48 	timer		te;				/* timer entry for add_timer() */
49 	device_info	*di;			/* pointer to the owning device */
50 	bigtime_t	when_target;	/* when we're supposed to wake up */
51 } timer_info;
52 
53 struct device_info {
54 	uint32		is_open;			/* a count of how many times the devices has been opened */
55 	area_id		shared_area;		/* the area shared between the driver and all of the accelerants */
56 	shared_info	*si;				/* a pointer to the shared area, for convenience */
57 	vuint32		*regs;				/* kernel's pointer to memory mapped registers */
58 	pci_info	pcii;					/* a convenience copy of the pci info for this device */
59 	char		name[B_OS_NAME_LENGTH];	/* where we keep the name of the device for publishing and comparing */
60 };
61 
62 typedef struct {
63 	uint32		count;				/* number of devices actually found */
64 	benaphore	kernel;				/* for serializing opens/closes */
65 	char		*device_names[MAX_DEVICES+1];	/* device name pointer storage */
66 	device_info	di[MAX_DEVICES];	/* device specific stuff */
67 } DeviceData;
68 
69 /* prototypes for our private functions */
70 static status_t open_hook (const char* name, uint32 flags, void** cookie);
71 static status_t close_hook (void* dev);
72 static status_t free_hook (void* dev);
73 static status_t read_hook (void* dev, off_t pos, void* buf, size_t* len);
74 static status_t write_hook (void* dev, off_t pos, const void* buf, size_t* len);
75 static status_t control_hook (void* dev, uint32 msg, void *buf, size_t len);
76 static status_t map_device(device_info *di);
77 static void unmap_device(device_info *di);
78 static void probe_devices(void);
79 static int32 eng_interrupt(void *data);
80 
81 static DeviceData		*pd;
82 static isa_module_info	*isa_bus = NULL;
83 static pci_module_info	*pci_bus = NULL;
84 static agp_module_info	*agp_bus = NULL;
85 static device_hooks graphics_device_hooks = {
86 	open_hook,
87 	close_hook,
88 	free_hook,
89 	control_hook,
90 	read_hook,
91 	write_hook,
92 	NULL,
93 	NULL,
94 	NULL,
95 	NULL
96 };
97 
98 #define VENDOR_ID_NVIDIA	0x1106 /* Via */
99 
100 static uint16 nvidia_device_list[] = {
101 	0x3122, /*  */
102 	0
103 };
104 
105 static struct {
106 	uint16	vendor;
107 	uint16	*devices;
108 } SupportedDevices[] = {
109 //	{VENDOR_ID_NVIDIA, nvidia_device_list},
110 	{0x0000, NULL}
111 };
112 
113 static settings current_settings = { // see comments in skel.settings
114 	// for driver
115 	DRIVER_PREFIX ".accelerant",
116 	false,      // dumprom
117 	// for accelerant
118 	0x00000000, // logmask
119 	0,          // memory
120 	true,       // usebios
121 	true,       // hardcursor
122 	false,		// switchhead
123 	false,		// force_pci
124 	false,		// unhide_fw
125 	true,		// pgm_panel
126 };
127 
128 static void dumprom (void *rom, uint32 size)
129 {
130 	int fd;
131 	uint32 cnt;
132 
133 	fd = open ("/boot/home/" DRIVER_PREFIX ".rom", O_WRONLY | O_CREAT, 0666);
134 	if (fd < 0) return;
135 
136 	/* apparantly max. 32kb may be written at once;
137 	 * the ROM size is a multiple of that anyway. */
138 	for (cnt = 0; (cnt < size); cnt += 32768)
139 		write (fd, ((void *)(((uint8 *)rom) + cnt)), 32768);
140 	close (fd);
141 }
142 
143 /* return 1 if vblank interrupt has occured */
144 static int caused_vbi(vuint32 * regs)
145 {
146 //	return (ENG_RG32(RG32_CRTC_INTS) & 0x00000001);
147 return 0;
148 }
149 
150 /* clear the vblank interrupt */
151 static void clear_vbi(vuint32 * regs)
152 {
153 //	ENG_RG32(RG32_CRTC_INTS) = 0x00000001;
154 }
155 
156 static void enable_vbi(vuint32 * regs)
157 {
158 	/* clear the vblank interrupt */
159 //	ENG_RG32(RG32_CRTC_INTS) = 0x00000001;
160 	/* enable nVidia interrupt source vblank */
161 //	ENG_RG32(RG32_CRTC_INTE) |= 0x00000001;
162 	/* enable nVidia interrupt system hardware (b0-1) */
163 //	ENG_RG32(RG32_MAIN_INTE) = 0x00000001;
164 }
165 
166 static void disable_vbi(vuint32 * regs)
167 {
168 	/* disable nVidia interrupt source vblank */
169 //	ENG_RG32(RG32_CRTC_INTE) &= 0xfffffffe;
170 	/* clear the vblank interrupt */
171 //	ENG_RG32(RG32_CRTC_INTS) = 0x00000001;
172 	/* disable nVidia interrupt system hardware (b0-1) */
173 //	ENG_RG32(RG32_MAIN_INTE) = 0x00000000;
174 }
175 
176 /*
177 	init_hardware() - Returns B_OK if one is
178 	found, otherwise returns B_ERROR so the driver will be unloaded.
179 */
180 status_t
181 init_hardware(void) {
182 	long		pci_index = 0;
183 	pci_info	pcii;
184 	bool		found_one = false;
185 
186 	/* choke if we can't find the PCI bus */
187 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
188 		return B_ERROR;
189 
190 	/* choke if we can't find the ISA bus */
191 	if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
192 	{
193 		put_module(B_PCI_MODULE_NAME);
194 		return B_ERROR;
195 	}
196 
197 	/* while there are more pci devices */
198 	while ((*pci_bus->get_nth_pci_info)(pci_index, &pcii) == B_NO_ERROR) {
199 		int vendor = 0;
200 
201 		/* if we match a supported vendor */
202 		while (SupportedDevices[vendor].vendor) {
203 			if (SupportedDevices[vendor].vendor == pcii.vendor_id) {
204 				uint16 *devices = SupportedDevices[vendor].devices;
205 				/* while there are more supported devices */
206 				while (*devices) {
207 					/* if we match a supported device */
208 					if (*devices == pcii.device_id ) {
209 
210 						found_one = true;
211 						goto done;
212 					}
213 					/* next supported device */
214 					devices++;
215 				}
216 			}
217 			vendor++;
218 		}
219 		/* next pci_info struct, please */
220 		pci_index++;
221 	}
222 
223 done:
224 	/* put away the module manager */
225 	put_module(B_PCI_MODULE_NAME);
226 	return (found_one ? B_OK : B_ERROR);
227 }
228 
229 status_t
230 init_driver(void) {
231 	void *settings_handle;
232 
233 	// get driver/accelerant settings, apsed
234 	settings_handle  = load_driver_settings (DRIVER_PREFIX ".settings");
235 	if (settings_handle != NULL) {
236 		const char *item;
237 		char       *end;
238 		uint32      value;
239 
240 		// for driver
241 		item = get_driver_parameter (settings_handle, "accelerant", "", "");
242 		if ((strlen (item) > 0) && (strlen (item) < sizeof (current_settings.accelerant) - 1)) {
243 			strcpy (current_settings.accelerant, item);
244 		}
245 		current_settings.dumprom = get_driver_boolean_parameter (settings_handle, "dumprom", false, false);
246 
247 		// for accelerant
248 		item = get_driver_parameter (settings_handle, "logmask", "0x00000000", "0x00000000");
249 		value = strtoul (item, &end, 0);
250 		if (*end == '\0') current_settings.logmask = value;
251 
252 		item = get_driver_parameter (settings_handle, "memory", "0", "0");
253 		value = strtoul (item, &end, 0);
254 		if (*end == '\0') current_settings.memory = value;
255 
256 		current_settings.hardcursor = get_driver_boolean_parameter (settings_handle, "hardcursor", false, false);
257 		current_settings.usebios = get_driver_boolean_parameter (settings_handle, "usebios", false, false);
258 		current_settings.switchhead = get_driver_boolean_parameter (settings_handle, "switchhead", false, false);
259 		current_settings.force_pci = get_driver_boolean_parameter (settings_handle, "force_pci", false, false);
260 		current_settings.unhide_fw = get_driver_boolean_parameter (settings_handle, "unhide_fw", false, false);
261 		current_settings.pgm_panel = get_driver_boolean_parameter (settings_handle, "pgm_panel", false, false);
262 
263 		unload_driver_settings (settings_handle);
264 	}
265 
266 	/* get a handle for the pci bus */
267 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
268 		return B_ERROR;
269 
270 	/* get a handle for the isa bus */
271 	if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
272 	{
273 		put_module(B_PCI_MODULE_NAME);
274 		return B_ERROR;
275 	}
276 
277 	/* get a handle for the agp bus if it exists */
278 	get_module(B_AGP_MODULE_NAME, (module_info **)&agp_bus);
279 
280 	/* driver private data */
281 	pd = (DeviceData *)calloc(1, sizeof(DeviceData));
282 	if (!pd) {
283 		put_module(B_PCI_MODULE_NAME);
284 		return B_ERROR;
285 	}
286 	/* initialize the benaphore */
287 	INIT_BEN(pd->kernel);
288 	/* find all of our supported devices */
289 	probe_devices();
290 	return B_OK;
291 }
292 
293 const char **
294 publish_devices(void) {
295 	/* return the list of supported devices */
296 	return (const char **)pd->device_names;
297 }
298 
299 device_hooks *
300 find_device(const char *name) {
301 	int index = 0;
302 	while (pd->device_names[index]) {
303 		if (strcmp(name, pd->device_names[index]) == 0)
304 			return &graphics_device_hooks;
305 		index++;
306 	}
307 	return NULL;
308 
309 }
310 
311 void uninit_driver(void) {
312 
313 	/* free the driver data */
314 	DELETE_BEN(pd->kernel);
315 	free(pd);
316 	pd = NULL;
317 
318 	/* put the pci module away */
319 	put_module(B_PCI_MODULE_NAME);
320 	put_module(B_ISA_MODULE_NAME);
321 
322 	/* put the agp module away if it's there */
323 	if (agp_bus) put_module(B_AGP_MODULE_NAME);
324 }
325 
326 static status_t map_device(device_info *di)
327 {
328 	char buffer[B_OS_NAME_LENGTH]; /*memory for device name*/
329 	shared_info *si = di->si;
330 	uint32	tmpUlong;
331 	pci_info *pcii = &(di->pcii);
332 	system_info sysinfo;
333 
334 	/*storage for the physical to virtual table (used for dma buffer)*/
335 //	physical_entry physical_memory[2];
336 //	#define G400_DMA_BUFFER_SIZE 1024*1024
337 
338 	/* variables for making copy of ROM */
339 	uint8* rom_temp;
340 	area_id rom_area;
341 
342 	/* Nvidia cards have registers in [0] and framebuffer in [1] */
343 	int registers = 1;
344 	int frame_buffer = 0;
345 //	int pseudo_dma = 2;
346 
347 	/* enable memory mapped IO, disable VGA I/O - this is defined in the PCI standard */
348 	tmpUlong = get_pci(PCI_command, 2);
349 	/* enable PCI access */
350 	tmpUlong |= PCI_command_memory;
351 	/* enable busmastering */
352 	tmpUlong |= PCI_command_master;
353 	/* disable ISA I/O access */
354 	tmpUlong &= ~PCI_command_io;
355 	set_pci(PCI_command, 2, tmpUlong);
356 
357  	/*work out which version of BeOS is running*/
358  	get_system_info(&sysinfo);
359  	if (0)//sysinfo.kernel_build_date[0]=='J')/*FIXME - better ID version*/
360  	{
361  		si->use_clone_bugfix = 1;
362  	}
363  	else
364  	{
365  		si->use_clone_bugfix = 0;
366  	}
367 
368 	/* work out a name for the register mapping */
369 	sprintf(buffer, DEVICE_FORMAT " regs",
370 		di->pcii.vendor_id, di->pcii.device_id,
371 		di->pcii.bus, di->pcii.device, di->pcii.function);
372 
373 	/* get a virtual memory address for the registers*/
374 	si->regs_area = map_physical_memory(
375 		buffer,
376 		/* WARNING: Nvidia needs to map regs as viewed from PCI space! */
377 		(void *) di->pcii.u.h0.base_registers_pci[registers],
378 		di->pcii.u.h0.base_register_sizes[registers],
379 		B_ANY_KERNEL_ADDRESS,
380  		(si->use_clone_bugfix ? B_READ_AREA|B_WRITE_AREA : 0),
381 		(void **)&(di->regs));
382  	si->clone_bugfix_regs = (uint32 *) di->regs;
383 
384 	/* if mapping registers to vmem failed then pass on error */
385 	if (si->regs_area < 0) return si->regs_area;
386 
387 	/* work out a name for the ROM mapping*/
388 	sprintf(buffer, DEVICE_FORMAT " rom",
389 		di->pcii.vendor_id, di->pcii.device_id,
390 		di->pcii.bus, di->pcii.device, di->pcii.function);
391 
392 	/* disable ROM shadowing, we want the guaranteed exact contents of the chip */
393 	/* warning:
394 	 * don't touch: (confirmed) NV04, NV05, NV05-M64, NV11 all shutoff otherwise.
395 	 * NV18, NV28 and NV34 keep working.
396 	 * confirmed NV28 and NV34 to use upper part of shadowed ROM for scratch purposes,
397 	 * however the actual ROM content (so the used part) is intact (confirmed). */
398 	//set_pci(ENCFG_ROMSHADOW, 4, 0);
399 
400 	/* get ROM memory mapped base adress - this is defined in the PCI standard */
401 	tmpUlong = get_pci(PCI_rom_base, 4);
402 	if (tmpUlong)
403 	{
404 		/* ROM was assigned an adress, so enable ROM decoding - see PCI standard */
405 		tmpUlong |= 0x00000001;
406 		set_pci(PCI_rom_base, 4, tmpUlong);
407 
408 		rom_area = map_physical_memory(
409 			buffer,
410 			(void *)di->pcii.u.h0.rom_base_pci,
411 			di->pcii.u.h0.rom_size,
412 			B_ANY_KERNEL_ADDRESS,
413 			B_READ_AREA,
414 			(void **)&(rom_temp)
415 		);
416 
417 		/* check if we got the BIOS signature (might fail on laptops..) */
418 		if (rom_temp[0]!=0x55 || rom_temp[1]!=0xaa)
419 		{
420 			/* apparantly no ROM is mapped here */
421 			delete_area(rom_area);
422 			rom_area = -1;
423 			/* force using ISA legacy map as fall-back */
424 			tmpUlong = 0x00000000;
425 		}
426 	}
427 
428 	if (!tmpUlong)
429 	{
430 		/* ROM was not assigned an adress, fetch it from ISA legacy memory map! */
431 		rom_area = map_physical_memory(
432 			buffer,
433 			(void *)0x000c0000,
434 			65536,
435 			B_ANY_KERNEL_ADDRESS,
436 			B_READ_AREA,
437 			(void **)&(rom_temp)
438 		);
439 	}
440 
441 	/* if mapping ROM to vmem failed then clean up and pass on error */
442 	if (rom_area < 0) {
443 		delete_area(si->regs_area);
444 		si->regs_area = -1;
445 		return rom_area;
446 	}
447 
448 	/* dump ROM to file if selected in skel.settings
449 	 * (ROM always fits in 64Kb: checked TNT1 - FX5950) */
450 	if (current_settings.dumprom) dumprom (rom_temp, 65536);
451 	/* make a copy of ROM for future reference */
452 	memcpy (si->rom_mirror, rom_temp, 65536);
453 
454 	/* disable ROM decoding - this is defined in the PCI standard, and delete the area */
455 	tmpUlong = get_pci(PCI_rom_base, 4);
456 	tmpUlong &= 0xfffffffe;
457 	set_pci(PCI_rom_base, 4, tmpUlong);
458 	delete_area(rom_area);
459 
460 	/* work out a name for the framebuffer mapping*/
461 	sprintf(buffer, DEVICE_FORMAT " framebuffer",
462 		di->pcii.vendor_id, di->pcii.device_id,
463 		di->pcii.bus, di->pcii.device, di->pcii.function);
464 
465 	/* map the framebuffer into vmem, using Write Combining*/
466 	si->fb_area = map_physical_memory(
467 		buffer,
468 		/* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */
469 		(void *) di->pcii.u.h0.base_registers_pci[frame_buffer],
470 		di->pcii.u.h0.base_register_sizes[frame_buffer],
471 		B_ANY_KERNEL_BLOCK_ADDRESS | B_MTR_WC,
472 		B_READ_AREA + B_WRITE_AREA,
473 		&(si->framebuffer));
474 
475 	/*if failed with write combining try again without*/
476 	if (si->fb_area < 0) {
477 		si->fb_area = map_physical_memory(
478 			buffer,
479 			/* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */
480 			(void *) di->pcii.u.h0.base_registers_pci[frame_buffer],
481 			di->pcii.u.h0.base_register_sizes[frame_buffer],
482 			B_ANY_KERNEL_BLOCK_ADDRESS,
483 			B_READ_AREA + B_WRITE_AREA,
484 			&(si->framebuffer));
485 	}
486 
487 	/* if there was an error, delete our other areas and pass on error*/
488 	if (si->fb_area < 0)
489 	{
490 		delete_area(si->regs_area);
491 		si->regs_area = -1;
492 		return si->fb_area;
493 	}
494 //fixme: retest for card coldstart and PCI/virt_mem mapping!!
495 	/* remember the DMA address of the frame buffer for BDirectWindow?? purposes */
496 	si->framebuffer_pci = (void *) di->pcii.u.h0.base_registers_pci[frame_buffer];
497 
498 	// remember settings for use here and in accelerant
499 	si->settings = current_settings;
500 
501 	/* in any case, return the result */
502 	return si->fb_area;
503 }
504 
505 static void unmap_device(device_info *di) {
506 	shared_info *si = di->si;
507 	uint32	tmpUlong;
508 	pci_info *pcii = &(di->pcii);
509 
510 	/* disable memory mapped IO */
511 	tmpUlong = get_pci(PCI_command, 4);
512 	tmpUlong &= 0xfffffffc;
513 	set_pci(PCI_command, 4, tmpUlong);
514 	/* delete the areas */
515 	if (si->regs_area >= 0) delete_area(si->regs_area);
516 	if (si->fb_area >= 0) delete_area(si->fb_area);
517 	si->regs_area = si->fb_area = -1;
518 	si->framebuffer = NULL;
519 	di->regs = NULL;
520 }
521 
522 static void probe_devices(void) {
523 	uint32 pci_index = 0;
524 	uint32 count = 0;
525 	device_info *di = pd->di;
526 
527 	/* while there are more pci devices */
528 	while ((count < MAX_DEVICES) && ((*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) == B_NO_ERROR)) {
529 		int vendor = 0;
530 
531 		/* if we match a supported vendor */
532 		while (SupportedDevices[vendor].vendor) {
533 			if (SupportedDevices[vendor].vendor == di->pcii.vendor_id) {
534 				uint16 *devices = SupportedDevices[vendor].devices;
535 				/* while there are more supported devices */
536 				while (*devices) {
537 					/* if we match a supported device */
538 					if (*devices == di->pcii.device_id ) {
539 						/* publish the device name */
540 						sprintf(di->name, "graphics/" DEVICE_FORMAT,
541 							di->pcii.vendor_id, di->pcii.device_id,
542 							di->pcii.bus, di->pcii.device, di->pcii.function);
543 
544 						/* remember the name */
545 						pd->device_names[count] = di->name;
546 						/* mark the driver as available for R/W open */
547 						di->is_open = 0;
548 						/* mark areas as not yet created */
549 						di->shared_area = -1;
550 						/* mark pointer to shared data as invalid */
551 						di->si = NULL;
552 						/* inc pointer to device info */
553 						di++;
554 						/* inc count */
555 						count++;
556 						/* break out of these while loops */
557 						goto next_device;
558 					}
559 					/* next supported device */
560 					devices++;
561 				}
562 			}
563 			vendor++;
564 		}
565 next_device:
566 		/* next pci_info struct, please */
567 		pci_index++;
568 	}
569 	/* propagate count */
570 	pd->count = count;
571 	/* terminate list of device names with a null pointer */
572 	pd->device_names[pd->count] = NULL;
573 }
574 
575 static uint32 thread_interrupt_work(int32 *flags, vuint32 *regs, shared_info *si) {
576 	uint32 handled = B_HANDLED_INTERRUPT;
577 	/* release the vblank semaphore */
578 	if (si->vblank >= 0) {
579 		int32 blocked;
580 		if ((get_sem_count(si->vblank, &blocked) == B_OK) && (blocked < 0)) {
581 			release_sem_etc(si->vblank, -blocked, B_DO_NOT_RESCHEDULE);
582 			handled = B_INVOKE_SCHEDULER;
583 		}
584 	}
585 	return handled;
586 }
587 
588 static int32
589 eng_interrupt(void *data)
590 {
591 	int32 handled = B_UNHANDLED_INTERRUPT;
592 	device_info *di = (device_info *)data;
593 	shared_info *si = di->si;
594 	int32 *flags = &(si->flags);
595 	vuint32 *regs;
596 
597 	/* is someone already handling an interrupt for this device? */
598 	if (atomic_or(flags, SKD_HANDLER_INSTALLED) & SKD_HANDLER_INSTALLED) {
599 		goto exit0;
600 	}
601 	/* get regs */
602 	regs = di->regs;
603 
604 	/* was it a VBI? */
605 	if (caused_vbi(regs)) {
606 		/*clear the interrupt*/
607 		clear_vbi(regs);
608 		/*release the semaphore*/
609 		handled = thread_interrupt_work(flags, regs, si);
610 	}
611 
612 	/* note that we're not in the handler any more */
613 	atomic_and(flags, ~SKD_HANDLER_INSTALLED);
614 
615 exit0:
616 	return handled;
617 }
618 
619 static status_t open_hook (const char* name, uint32 flags, void** cookie) {
620 	int32 index = 0;
621 	device_info *di;
622 	shared_info *si;
623 	thread_id	thid;
624 	thread_info	thinfo;
625 	status_t	result = B_OK;
626 	vuint32		*regs;
627 	char shared_name[B_OS_NAME_LENGTH];
628 
629 	/* find the device name in the list of devices */
630 	/* we're never passed a name we didn't publish */
631 	while (pd->device_names[index] && (strcmp(name, pd->device_names[index]) != 0)) index++;
632 
633 	/* for convienience */
634 	di = &(pd->di[index]);
635 
636 	/* make sure no one else has write access to the common data */
637 	AQUIRE_BEN(pd->kernel);
638 
639 	/* if it's already open for writing */
640 	if (di->is_open) {
641 		/* mark it open another time */
642 		goto mark_as_open;
643 	}
644 	/* create the shared area */
645 	sprintf(shared_name, DEVICE_FORMAT " shared",
646 		di->pcii.vendor_id, di->pcii.device_id,
647 		di->pcii.bus, di->pcii.device, di->pcii.function);
648 	/* create this area with NO user-space read or write permissions, to prevent accidental dammage */
649 	di->shared_area = create_area(shared_name, (void **)&(di->si), B_ANY_KERNEL_ADDRESS, ((sizeof(shared_info) + (B_PAGE_SIZE - 1)) & ~(B_PAGE_SIZE - 1)), B_FULL_LOCK, 0);
650 	if (di->shared_area < 0) {
651 		/* return the error */
652 		result = di->shared_area;
653 		goto done;
654 	}
655 
656 	/* save a few dereferences */
657 	si = di->si;
658 
659 	/* save the vendor and device IDs */
660 	si->vendor_id = di->pcii.vendor_id;
661 	si->device_id = di->pcii.device_id;
662 	si->revision = di->pcii.revision;
663 	si->bus = di->pcii.bus;
664 	si->device = di->pcii.device;
665 	si->function = di->pcii.function;
666 
667 	/* note the amount of system RAM the system BIOS assigned to the card if applicable:
668 	 * unified memory architecture (UMA) */
669 	switch ((((uint32)(si->device_id)) << 16) | si->vendor_id)
670 	{
671 	case 0x01a010de: /* Nvidia GeForce2 Integrated GPU */
672 		/* device at bus #0, device #0, function #1 holds value at byte-index 0x7C */
673 		si->ps.memory_size = 1024 * 1024 *
674 			(((((*pci_bus->read_pci_config)(0, 0, 1, 0x7c, 4)) & 0x000007c0) >> 6) + 1);
675 		/* last 64kB RAM is used for the BIOS (or something else?) */
676 		si->ps.memory_size -= (64 * 1024);
677 		break;
678 	case 0x01f010de: /* Nvidia GeForce4 MX Integrated GPU */
679 		/* device at bus #0, device #0, function #1 holds value at byte-index 0x84 */
680 		si->ps.memory_size = 1024 * 1024 *
681 			(((((*pci_bus->read_pci_config)(0, 0, 1, 0x84, 4)) & 0x000007f0) >> 4) + 1);
682 		/* last 64kB RAM is used for the BIOS (or something else?) */
683 		si->ps.memory_size -= (64 * 1024);
684 		break;
685 	default:
686 		/* all other cards have own RAM: the amount of which is determined in the
687 		 * accelerant. */
688 		break;
689 	}
690 
691 	/* map the device */
692 	result = map_device(di);
693 	if (result < 0) goto free_shared;
694 	result = B_OK;
695 
696 	/* create a semaphore for vertical blank management */
697 	si->vblank = create_sem(0, di->name);
698 	if (si->vblank < 0) {
699 		result = si->vblank;
700 		goto unmap;
701 	}
702 
703 	/* change the owner of the semaphores to the opener's team */
704 	/* this is required because apps can't aquire kernel semaphores */
705 	thid = find_thread(NULL);
706 	get_thread_info(thid, &thinfo);
707 	set_sem_owner(si->vblank, thinfo.team);
708 
709 	/* assign local regs pointer for SAMPLExx() macros */
710 	regs = di->regs;
711 
712 	/* disable and clear any pending interrupts */
713 	disable_vbi(regs);
714 
715 	/* If there is a valid interrupt line assigned then set up interrupts */
716 	if ((di->pcii.u.h0.interrupt_pin == 0x00) ||
717 	    (di->pcii.u.h0.interrupt_line == 0xff) || /* no IRQ assigned */
718 	    (di->pcii.u.h0.interrupt_line <= 0x02))   /* system IRQ assigned */
719 	{
720 		/* we are aborting! */
721 		/* Note: the R4 graphics driver kit lacks this statement!! */
722 		result = B_ERROR;
723 		/* interrupt does not exist so exit without installing our handler */
724 		goto delete_the_sem;
725 	}
726 	else
727 	{
728 		/* otherwise install our interrupt handler */
729 		result = install_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, (void *)di, 0);
730 		/* bail if we couldn't install the handler */
731 		if (result != B_OK) goto delete_the_sem;
732 	}
733 
734 mark_as_open:
735 	/* mark the device open */
736 	di->is_open++;
737 
738 	/* send the cookie to the opener */
739 	*cookie = di;
740 
741 	goto done;
742 
743 
744 delete_the_sem:
745 	delete_sem(si->vblank);
746 
747 unmap:
748 	unmap_device(di);
749 
750 free_shared:
751 	/* clean up our shared area */
752 	delete_area(di->shared_area);
753 	di->shared_area = -1;
754 	di->si = NULL;
755 
756 done:
757 	/* end of critical section */
758 	RELEASE_BEN(pd->kernel);
759 
760 	/* all done, return the status */
761 	return result;
762 }
763 
764 /* ----------
765 	read_hook - does nothing, gracefully
766 ----- */
767 static status_t
768 read_hook (void* dev, off_t pos, void* buf, size_t* len)
769 {
770 	*len = 0;
771 	return B_NOT_ALLOWED;
772 }
773 
774 /* ----------
775 	write_hook - does nothing, gracefully
776 ----- */
777 static status_t
778 write_hook (void* dev, off_t pos, const void* buf, size_t* len)
779 {
780 	*len = 0;
781 	return B_NOT_ALLOWED;
782 }
783 
784 /* ----------
785 	close_hook - does nothing, gracefully
786 ----- */
787 static status_t
788 close_hook (void* dev)
789 {
790 	/* we don't do anything on close: there might be dup'd fd */
791 	return B_NO_ERROR;
792 }
793 
794 /* -----------
795 	free_hook - close down the device
796 ----------- */
797 static status_t
798 free_hook (void* dev) {
799 	device_info *di = (device_info *)dev;
800 	shared_info	*si = di->si;
801 	vuint32 *regs = di->regs;
802 
803 	/* lock the driver */
804 	AQUIRE_BEN(pd->kernel);
805 
806 	/* if opened multiple times, decrement the open count and exit */
807 	if (di->is_open > 1)
808 		goto unlock_and_exit;
809 
810 	/* disable and clear any pending interrupts */
811 	disable_vbi(regs);
812 
813 	/* remove interrupt handler */
814 	remove_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, di);
815 
816 	/* delete the semaphores, ignoring any errors ('cause the owning team may have died on us) */
817 	delete_sem(si->vblank);
818 	si->vblank = -1;
819 
820 	/* free regs and framebuffer areas */
821 	unmap_device(di);
822 
823 	/* clean up our shared area */
824 	delete_area(di->shared_area);
825 	di->shared_area = -1;
826 	di->si = NULL;
827 
828 unlock_and_exit:
829 	/* mark the device available */
830 	di->is_open--;
831 	/* unlock the driver */
832 	RELEASE_BEN(pd->kernel);
833 	/* all done */
834 	return B_OK;
835 }
836 
837 /* -----------
838 	control_hook - where the real work is done
839 ----------- */
840 static status_t
841 control_hook (void* dev, uint32 msg, void *buf, size_t len) {
842 	device_info *di = (device_info *)dev;
843 	status_t result = B_DEV_INVALID_IOCTL;
844 	uint32 tmpUlong;
845 
846 	switch (msg) {
847 		/* the only PUBLIC ioctl */
848 		case B_GET_ACCELERANT_SIGNATURE: {
849 			char *sig = (char *)buf;
850 			strcpy(sig, current_settings.accelerant);
851 			result = B_OK;
852 		} break;
853 
854 		/* PRIVATE ioctl from here on */
855 		case ENG_GET_PRIVATE_DATA: {
856 			eng_get_private_data *gpd = (eng_get_private_data *)buf;
857 			if (gpd->magic == SKEL_PRIVATE_DATA_MAGIC) {
858 				gpd->shared_info_area = di->shared_area;
859 				result = B_OK;
860 			}
861 		} break;
862 		case ENG_GET_PCI: {
863 			eng_get_set_pci *gsp = (eng_get_set_pci *)buf;
864 			if (gsp->magic == SKEL_PRIVATE_DATA_MAGIC) {
865 				pci_info *pcii = &(di->pcii);
866 				gsp->value = get_pci(gsp->offset, gsp->size);
867 				result = B_OK;
868 			}
869 		} break;
870 		case ENG_SET_PCI: {
871 			eng_get_set_pci *gsp = (eng_get_set_pci *)buf;
872 			if (gsp->magic == SKEL_PRIVATE_DATA_MAGIC) {
873 				pci_info *pcii = &(di->pcii);
874 				set_pci(gsp->offset, gsp->size, gsp->value);
875 				result = B_OK;
876 			}
877 		} break;
878 		case ENG_DEVICE_NAME: { // apsed
879 			eng_device_name *dn = (eng_device_name *)buf;
880 			if (dn->magic == SKEL_PRIVATE_DATA_MAGIC) {
881 				strcpy(dn->name, di->name);
882 				result = B_OK;
883 			}
884 		} break;
885 		case ENG_RUN_INTERRUPTS: {
886 			eng_set_bool_state *ri = (eng_set_bool_state *)buf;
887 			if (ri->magic == SKEL_PRIVATE_DATA_MAGIC) {
888 				vuint32 *regs = di->regs;
889 				if (ri->do_it) {
890 					enable_vbi(regs);
891 				} else {
892 					disable_vbi(regs);
893 				}
894 				result = B_OK;
895 			}
896 		} break;
897 		case ENG_GET_NTH_AGP_INFO: {
898 			eng_nth_agp_info *nai = (eng_nth_agp_info *)buf;
899 			if (nai->magic == SKEL_PRIVATE_DATA_MAGIC) {
900 				nai->exist = false;
901 				nai->agp_bus = false;
902 				if (agp_bus) {
903 					nai->agp_bus = true;
904 					if ((*agp_bus->get_nth_agp_info)(nai->index, &(nai->agpi)) == B_NO_ERROR) {
905 						nai->exist = true;
906 					}
907 				}
908 				result = B_OK;
909 			}
910 		} break;
911 		case ENG_ENABLE_AGP: {
912 			eng_cmd_agp *nca = (eng_cmd_agp *)buf;
913 			if (nca->magic == SKEL_PRIVATE_DATA_MAGIC) {
914 				if (agp_bus) {
915 					nca->agp_bus = true;
916 					(*agp_bus->enable_agp)(&(nca->cmd));
917 				} else {
918 					nca->agp_bus = false;
919 					nca->cmd = 0;
920 				}
921 				result = B_OK;
922 			}
923 		} break;
924 		case ENG_ISA_OUT: {
925 			eng_in_out_isa *io_isa = (eng_in_out_isa *)buf;
926 			if (io_isa->magic == SKEL_PRIVATE_DATA_MAGIC) {
927 				pci_info *pcii = &(di->pcii);
928 
929 				/* lock the driver:
930 				 * no other graphics card may have ISA I/O enabled when we enter */
931 				AQUIRE_BEN(pd->kernel);
932 
933 				/* enable ISA I/O access */
934 				tmpUlong = get_pci(PCI_command, 2);
935 				tmpUlong |= PCI_command_io;
936 				set_pci(PCI_command, 2, tmpUlong);
937 
938 				if (io_isa->size == 1)
939   					isa_bus->write_io_8(io_isa->adress, (uint8)io_isa->data);
940    				else
941    					isa_bus->write_io_16(io_isa->adress, io_isa->data);
942   				result = B_OK;
943 
944 				/* disable ISA I/O access */
945 				tmpUlong = get_pci(PCI_command, 2);
946 				tmpUlong &= ~PCI_command_io;
947 				set_pci(PCI_command, 2, tmpUlong);
948 
949 				/* end of critical section */
950 				RELEASE_BEN(pd->kernel);
951    			}
952 		} break;
953 		case ENG_ISA_IN: {
954 			eng_in_out_isa *io_isa = (eng_in_out_isa *)buf;
955 			if (io_isa->magic == SKEL_PRIVATE_DATA_MAGIC) {
956 				pci_info *pcii = &(di->pcii);
957 
958 				/* lock the driver:
959 				 * no other graphics card may have ISA I/O enabled when we enter */
960 				AQUIRE_BEN(pd->kernel);
961 
962 				/* enable ISA I/O access */
963 				tmpUlong = get_pci(PCI_command, 2);
964 				tmpUlong |= PCI_command_io;
965 				set_pci(PCI_command, 2, tmpUlong);
966 
967 				if (io_isa->size == 1)
968 	   				io_isa->data = isa_bus->read_io_8(io_isa->adress);
969 	   			else
970 	   				io_isa->data = isa_bus->read_io_16(io_isa->adress);
971    				result = B_OK;
972 
973 				/* disable ISA I/O access */
974 				tmpUlong = get_pci(PCI_command, 2);
975 				tmpUlong &= ~PCI_command_io;
976 				set_pci(PCI_command, 2, tmpUlong);
977 
978 				/* end of critical section */
979 				RELEASE_BEN(pd->kernel);
980    			}
981 		} break;
982 	}
983 	return result;
984 }
985