xref: /haiku/src/add-ons/kernel/drivers/graphics/matrox/driver.c (revision 36be333ed8783bc08f9f94c710fbc769633f3ef5)
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 	Modified to work with the Matrox G400 - Mark Watson
6 */
7 
8 /* standard kernel driver stuff */
9 #include <KernelExport.h>
10 #include <PCI.h>
11 #include <OS.h>
12 #include <driver_settings.h>
13 #include <malloc.h>
14 #include <stdlib.h> // for strtoXX
15 
16 /* this is for the standardized portion of the driver API */
17 /* currently only one operation is defined: B_GET_ACCELERANT_SIGNATURE */
18 #include <graphic_driver.h>
19 
20 /* this is for sprintf() */
21 #include <stdio.h>
22 
23 /* this is for string compares */
24 #include <string.h>
25 
26 /* The private interface between the accelerant and the kernel driver. */
27 #include "DriverInterface.h"
28 #include "mga_macros.h"
29 
30 #define get_pci(o, s) (*pci_bus->read_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s))
31 #define set_pci(o, s, v) (*pci_bus->write_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s), (v))
32 
33 #define MAX_DEVICES	  8
34 
35 #define DEVICE_FORMAT "%04X_%04X_%02X%02X%02X" // apsed
36 
37 /* Tell the kernel what revision of the driver API we support */
38 int32	api_version = B_CUR_DRIVER_API_VERSION; // apsed, was 2, is 2 in R5
39 
40 /* these structures are private to the kernel driver */
41 typedef struct device_info device_info;
42 
43 typedef struct {
44 	timer		te;				/* timer entry for add_timer() */
45 	device_info	*di;			/* pointer to the owning device */
46 	bigtime_t	when_target;	/* when we're supposed to wake up */
47 } timer_info;
48 
49 struct device_info {
50 	uint32		is_open;			/* a count of how many times the devices has been opened */
51 	area_id		shared_area;		/* the area shared between the driver and all of the accelerants */
52 	shared_info	*si;				/* a pointer to the shared area, for convenience */
53 	vuint32		*regs;				/* kernel's pointer to memory mapped registers */
54 	pci_info	pcii;					/* a convenience copy of the pci info for this device */
55 	char		name[B_OS_NAME_LENGTH];	/* where we keep the name of the device for publishing and comparing */
56 };
57 
58 typedef struct {
59 	uint32		count;				/* number of devices actually found */
60 	benaphore	kernel;				/* for serializing opens/closes */
61 	char		*device_names[MAX_DEVICES+1];	/* device name pointer storage */
62 	device_info	di[MAX_DEVICES];	/* device specific stuff */
63 } DeviceData;
64 
65 /* prototypes for our private functions */
66 static status_t open_hook (const char* name, uint32 flags, void** cookie);
67 static status_t close_hook (void* dev);
68 static status_t free_hook (void* dev);
69 static status_t read_hook (void* dev, off_t pos, void* buf, size_t* len);
70 static status_t write_hook (void* dev, off_t pos, const void* buf, size_t* len);
71 static status_t control_hook (void* dev, uint32 msg, void *buf, size_t len);
72 static status_t map_device(device_info *di);
73 static void unmap_device(device_info *di);
74 static void probe_devices(void);
75 static int32 gx00_interrupt(void *data);
76 
77 static DeviceData		*pd;
78 static pci_module_info	*pci_bus;
79 static device_hooks graphics_device_hooks = {
80 	open_hook,
81 	close_hook,
82 	free_hook,
83 	control_hook,
84 	read_hook,
85 	write_hook,
86 	NULL,
87 	NULL,
88 	NULL,
89 	NULL
90 };
91 
92 #define VENDOR_ID			0x102b	/* Matrox graphics inc. */
93 
94 static uint16 gx00_device_list[] = {
95 	0x2527,/*G550AGP*/
96 	0x0525,/*G400AGP*/
97 	0x0520,/*G200PCI*/
98 	0x0521,/*G200AGP*/
99 	0x1000,/*G100PCI*/
100 	0x1001,/*G100AGP*/
101 	0x051F,/*MGA-2164 AGP Millennium 2*/
102 	0x051B,/*MGA-2164 PCI Millennium 2*/
103 	0x051A,/*MGA-1054 PCI Mystic*/
104 	0x0519,/*MGA-2064 PCI Millennium*/
105 	0
106 };
107 
108 static struct {
109 	uint16	vendor;
110 	uint16	*devices;
111 } SupportedDevices[] = {
112 	{VENDOR_ID, gx00_device_list},
113 	{0x0000, NULL}
114 };
115 
116 static settings current_settings = { // see comments in mga.settings
117 	// for driver
118 	DRIVER_PREFIX ".accelerant",
119 	false,      // dumprom
120 	// for accelerant
121 	0x00000000, // logmask
122 	0,          // memory
123 	false,      // usebios
124 	false,      // hardcursor
125 };
126 
127 static void dumprom (void *rom, size_t size)
128 {
129 	int fd = open ("/boot/home/" DRIVER_PREFIX ".rom", O_WRONLY | O_CREAT, 0666);
130 	if (fd < 0) return;
131 	write (fd, rom, size);
132 	close (fd);
133 }
134 
135 /*return 1, is interrupt has occured*/
136 int caused_vbi(vuint32 * regs)
137 {
138 	return (ACCR(STATUS)&0x20);
139 }
140 
141 /*clear the interrupt*/
142 void clear_vbi(vuint32 * regs)
143 {
144 	ACCW(ICLEAR,0x20);
145 }
146 
147 void enable_vbi(vuint32 * regs)
148 {
149 	ACCW(IEN,ACCR(IEN)|0x20);
150 }
151 
152 void disable_vbi(vuint32 * regs)
153 {
154 	ACCW(IEN,(ACCR(IEN)&~0x20));
155 	ACCW(ICLEAR,0x20);
156 }
157 
158 
159 /*
160 	init_hardware() - Returns B_OK if one is
161 	found, otherwise returns B_ERROR so the driver will be unloaded.
162 */
163 status_t
164 init_hardware(void) {
165 	long		pci_index = 0;
166 	pci_info	pcii;
167 	bool		found_one = FALSE;
168 
169 	/* choke if we can't find the PCI bus */
170 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
171 		return B_ERROR;
172 
173 	/* while there are more pci devices */
174 	while ((*pci_bus->get_nth_pci_info)(pci_index, &pcii) == B_NO_ERROR) {
175 		int vendor = 0;
176 
177 		/* if we match a supported vendor */
178 		while (SupportedDevices[vendor].vendor) {
179 			if (SupportedDevices[vendor].vendor == pcii.vendor_id) {
180 				uint16 *devices = SupportedDevices[vendor].devices;
181 				/* while there are more supported devices */
182 				while (*devices) {
183 					/* if we match a supported device */
184 					if (*devices == pcii.device_id ) {
185 
186 						found_one = TRUE;
187 						goto done;
188 					}
189 					/* next supported device */
190 					devices++;
191 				}
192 			}
193 			vendor++;
194 		}
195 		/* next pci_info struct, please */
196 		pci_index++;
197 	}
198 
199 done:
200 	/* put away the module manager */
201 	put_module(B_PCI_MODULE_NAME);
202 	return (found_one ? B_OK : B_ERROR);
203 }
204 
205 status_t
206 init_driver(void) {
207 	void *settings_handle;
208 
209 	// get driver/accelerant settings, apsed
210 	settings_handle  = load_driver_settings (DRIVER_PREFIX ".settings");
211 	if (settings_handle != NULL) {
212 		const char *item;
213 		char       *end;
214 		uint32      value;
215 
216 		// for driver
217 		item = get_driver_parameter (settings_handle, "accelerant", "", "");
218 		if ((strlen (item) > 0) && (strlen (item) < sizeof (current_settings.accelerant) - 1)) {
219 			strcpy (current_settings.accelerant, item);
220 		}
221 		current_settings.dumprom = get_driver_boolean_parameter (settings_handle, "dumprom", false, false);
222 
223 		// for accelerant
224 		item = get_driver_parameter (settings_handle, "logmask", "0x00000000", "0x00000000");
225 		value = strtoul (item, &end, 0);
226 		if (*end == '\0') current_settings.logmask = value;
227 
228 		item = get_driver_parameter (settings_handle, "memory", "0", "0");
229 		value = strtoul (item, &end, 0);
230 		if (*end == '\0') current_settings.memory = value;
231 
232 		current_settings.hardcursor = get_driver_boolean_parameter (settings_handle, "hardcursor", false, false);
233 		current_settings.usebios = get_driver_boolean_parameter (settings_handle, "usebios", false, false);
234 
235 		unload_driver_settings (settings_handle);
236 	}
237 
238 	/* get a handle for the pci bus */
239 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
240 		return B_ERROR;
241 
242 	/* driver private data */
243 	pd = (DeviceData *)calloc(1, sizeof(DeviceData));
244 	if (!pd) {
245 		put_module(B_PCI_MODULE_NAME);
246 		return B_ERROR;
247 	}
248 	/* initialize the benaphore */
249 	INIT_BEN(pd->kernel);
250 	/* find all of our supported devices */
251 	probe_devices();
252 	return B_OK;
253 }
254 
255 const char **
256 publish_devices(void) {
257 	/* return the list of supported devices */
258 	return (const char **)pd->device_names;
259 }
260 
261 device_hooks *
262 find_device(const char *name) {
263 	int index = 0;
264 	while (pd->device_names[index]) {
265 		if (strcmp(name, pd->device_names[index]) == 0)
266 			return &graphics_device_hooks;
267 		index++;
268 	}
269 	return NULL;
270 
271 }
272 
273 void uninit_driver(void) {
274 
275 	/* free the driver data */
276 	DELETE_BEN(pd->kernel);
277 	free(pd);
278 	pd = NULL;
279 
280 	/* put the pci module away */
281 	put_module(B_PCI_MODULE_NAME);
282 }
283 
284 static status_t map_device(device_info *di) {
285 	/* frame buffer in [0], control regs in [1], pseudo_dma in [2] */
286 	int frame_buffer = 0;
287 	int registers = 1;
288 	int pseudo_dma = 2;
289 
290 	char buffer[B_OS_NAME_LENGTH]; /*memory for device name*/
291 	shared_info *si = di->si;
292 	uint32	tmpUlong;
293 	pci_info *pcii = &(di->pcii);
294 	system_info sysinfo;
295 
296 	/*storage for the physical to virtual table (used for dma buffer)*/
297 //	physical_entry physical_memory[2];
298 //	#define G400_DMA_BUFFER_SIZE 1024*1024
299 
300 	/*variables for making copy of ROM*/
301 	int i;
302 	char * rom_temp;
303 	area_id rom_area;
304 
305 	/* enable memory mapped IO, disable VGA I/O - this is standard*/
306 	tmpUlong = get_pci(PCI_command, 4);
307 	tmpUlong |= 0x00000002;
308 	tmpUlong &= 0xfffffffe;
309 	set_pci(PCI_command, 4, tmpUlong);
310 
311  	/*work out which version of BeOS is running*/
312  	get_system_info(&sysinfo);
313  	if (sysinfo.kernel_build_date[0]=='J')/*FIXME - better ID version*/
314  	{
315  		si->use_clone_bugfix = 1;
316  	}
317  	else
318  	{
319  		si->use_clone_bugfix = 0;
320  	}
321 
322 	/* work out a name for the register mapping */
323 	sprintf(buffer, DEVICE_FORMAT " regs",
324 		di->pcii.vendor_id, di->pcii.device_id,
325 		di->pcii.bus, di->pcii.device, di->pcii.function);
326 
327 	/* get a virtual memory address for the registers*/
328 	si->regs_area = map_physical_memory(
329 		buffer,
330 		(void *) di->pcii.u.h0.base_registers[registers],
331 		di->pcii.u.h0.base_register_sizes[registers],
332 		B_ANY_KERNEL_ADDRESS,
333  		(si->use_clone_bugfix ? B_READ_AREA|B_WRITE_AREA : 0),
334 		(void **)&(di->regs));
335  	si->clone_bugfix_regs = (uint32 *) di->regs;
336 
337 	/* if mapping registers to vmem failed then pass on error */
338 	if (si->regs_area < 0) return si->regs_area;
339 
340 	/* work out a name for the ROM mapping*/
341 	sprintf(buffer, DEVICE_FORMAT " rom",
342 		di->pcii.vendor_id, di->pcii.device_id,
343 		di->pcii.bus, di->pcii.device, di->pcii.function);
344 
345 	/*place ROM over the fbspace (this is definately safe)*/
346 	tmpUlong = di->pcii.u.h0.base_registers[frame_buffer];
347 	tmpUlong |= 0x00000001;
348 	set_pci(PCI_rom_base, 4, tmpUlong);
349 
350 	rom_area = map_physical_memory(
351 		buffer,
352 		(void *)di->pcii.u.h0.base_registers[frame_buffer],
353 		32768,
354 		B_ANY_KERNEL_ADDRESS,
355 		B_READ_AREA,
356 		(void **)&(rom_temp)
357 	);
358 
359 	/* if mapping ROM to vmem failed then clean up and pass on error */
360 	if (rom_area < 0) {
361 		delete_area(si->regs_area);
362 		si->regs_area = -1;
363 		return rom_area;
364 	}
365 
366 	/* make a copy of ROM for future reference*/
367 	memcpy (si->rom_mirror, rom_temp, 32768);
368 	if (current_settings.dumprom) dumprom (rom_temp, 32768);
369 
370 	/*disable ROM and delete the area*/
371 	set_pci(PCI_rom_base,4,0);
372 	delete_area(rom_area);
373 
374 	/* work out a name for the pseudo dma mapping*/
375 	sprintf(buffer, DEVICE_FORMAT " pseudodma",
376 		di->pcii.vendor_id, di->pcii.device_id,
377 		di->pcii.bus, di->pcii.device, di->pcii.function);
378 
379 	/* map the pseudo dma into vmem (write-only)*/
380 	si->pseudo_dma_area = map_physical_memory(
381 		buffer,
382 		(void *) di->pcii.u.h0.base_registers[pseudo_dma],
383 		di->pcii.u.h0.base_register_sizes[pseudo_dma],
384 		B_ANY_KERNEL_ADDRESS,
385 		B_WRITE_AREA,
386 		&(si->pseudo_dma));
387 
388 	/* if there was an error, delete our other areas and pass on error*/
389 	if (si->pseudo_dma_area < 0) {
390 		delete_area(si->regs_area);
391 		si->regs_area = -1;
392 		return si->pseudo_dma_area;
393 	}
394 
395 	/* work out a name for the a dma buffer*/
396 //	sprintf(buffer, DEVICE_FORMAT " dmabuffer",
397 //		di->pcii.vendor_id, di->pcii.device_id,
398 //		di->pcii.bus, di->pcii.device, di->pcii.function);
399 
400 	/* create an area for the dma buffer*/
401 //	si->dma_buffer_area = create_area(
402 //		buffer,
403 //		&si->dma_buffer,
404 //		B_ANY_ADDRESS,
405 //		G400_DMA_BUFFER_SIZE,
406 //		B_FULL_LOCK|B_CONTIGUOUS,
407 //		B_READ_AREA|B_WRITE_AREA);
408 
409 	/* if there was an error, delete our other areas and pass on error*/
410 //	if (si->dma_buffer_area < 0) {
411 //		delete_area(si->pseudo_dma_area);
412 //		si->pseudo_dma_area = -1;
413 //		delete_area(si->regs_area);
414 //		si->regs_area = -1;
415 //		return si->dma_buffer_area;
416 //	}
417 
418 	/*find where it is in real memory*/
419 //	get_memory_map(si->dma_buffer,4,physical_memory,1);
420 //	si->dma_buffer_pci = physical_memory[0].address; /*addr from PCI space*/
421 
422 	/* work out a name for the framebuffer mapping*/
423 	sprintf(buffer, DEVICE_FORMAT " framebuffer",
424 		di->pcii.vendor_id, di->pcii.device_id,
425 		di->pcii.bus, di->pcii.device, di->pcii.function);
426 
427 	/* map the framebuffer into vmem, using Write Combining*/
428 	si->fb_area = map_physical_memory(
429 		buffer,
430 		(void *) di->pcii.u.h0.base_registers[frame_buffer],
431 		di->pcii.u.h0.base_register_sizes[frame_buffer],
432 		B_ANY_KERNEL_BLOCK_ADDRESS | B_MTR_WC,
433 		B_READ_AREA + B_WRITE_AREA,
434 		&(si->framebuffer));
435 
436 	/*if failed with write combining try again without*/
437 	if (si->fb_area < 0) {
438 		si->fb_area = map_physical_memory(
439 			buffer,
440 			(void *) di->pcii.u.h0.base_registers[frame_buffer],
441 			di->pcii.u.h0.base_register_sizes[frame_buffer],
442 			B_ANY_KERNEL_BLOCK_ADDRESS,
443 			B_READ_AREA + B_WRITE_AREA,
444 			&(si->framebuffer));
445 	}
446 
447 	/* if there was an error, delete our other areas and pass on error*/
448 	if (si->fb_area < 0) {
449 		delete_area(si->dma_buffer_area);
450 		si->dma_buffer_area = -1;
451 		delete_area(si->pseudo_dma_area);
452 		si->pseudo_dma_area = -1;
453 		delete_area(si->regs_area);
454 		si->regs_area = -1;
455 		return si->fb_area;
456 	}
457 	/* remember the DMA address of the frame buffer for BDirectWindow?? purposes */
458 	si->framebuffer_pci = (void *) di->pcii.u.h0.base_registers_pci[frame_buffer];
459 
460 	// remember settings for use here and in accelerant
461 	si->settings = current_settings;
462 
463 	/* in any case, return the result */
464 	return si->fb_area;
465 }
466 
467 static void unmap_device(device_info *di) {
468 	shared_info *si = di->si;
469 	uint32	tmpUlong;
470 	pci_info *pcii = &(di->pcii);
471 
472 	/* disable memory mapped IO */
473 	tmpUlong = get_pci(PCI_command, 4);
474 	tmpUlong &= 0xfffffffc;
475 	set_pci(PCI_command, 4, tmpUlong);
476 	/* delete the areas */
477 	if (si->regs_area >= 0) delete_area(si->regs_area);
478 	if (si->fb_area >= 0) delete_area(si->fb_area);
479 	si->regs_area = si->fb_area = -1;
480 	si->framebuffer = NULL;
481 	di->regs = NULL;
482 }
483 
484 static void probe_devices(void) {
485 	uint32 pci_index = 0;
486 	uint32 count = 0;
487 	device_info *di = pd->di;
488 
489 	/* while there are more pci devices */
490 	while ((count < MAX_DEVICES) && ((*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) == B_NO_ERROR)) {
491 		int vendor = 0;
492 
493 		/* if we match a supported vendor */
494 		while (SupportedDevices[vendor].vendor) {
495 			if (SupportedDevices[vendor].vendor == di->pcii.vendor_id) {
496 				uint16 *devices = SupportedDevices[vendor].devices;
497 				/* while there are more supported devices */
498 				while (*devices) {
499 					/* if we match a supported device */
500 					if (*devices == di->pcii.device_id ) {
501 						/* publish the device name */
502 						sprintf(di->name, "graphics/" DEVICE_FORMAT,
503 							di->pcii.vendor_id, di->pcii.device_id,
504 							di->pcii.bus, di->pcii.device, di->pcii.function);
505 
506 						/* remember the name */
507 						pd->device_names[count] = di->name;
508 						/* mark the driver as available for R/W open */
509 						di->is_open = 0;
510 						/* mark areas as not yet created */
511 						di->shared_area = -1;
512 						/* mark pointer to shared data as invalid */
513 						di->si = NULL;
514 						/* inc pointer to device info */
515 						di++;
516 						/* inc count */
517 						count++;
518 						/* break out of these while loops */
519 						goto next_device;
520 					}
521 					/* next supported device */
522 					devices++;
523 				}
524 			}
525 			vendor++;
526 		}
527 next_device:
528 		/* next pci_info struct, please */
529 		pci_index++;
530 	}
531 	/* propagate count */
532 	pd->count = count;
533 	/* terminate list of device names with a null pointer */
534 	pd->device_names[pd->count] = NULL;
535 }
536 
537 static uint32 thread_interrupt_work(int32 *flags, vuint32 *regs, shared_info *si) {
538 	uint32 handled = B_HANDLED_INTERRUPT;
539 	/* release the vblank semaphore */
540 	if (si->vblank >= 0) {
541 		int32 blocked;
542 		if ((get_sem_count(si->vblank, &blocked) == B_OK) && (blocked < 0)) {
543 			release_sem_etc(si->vblank, -blocked, B_DO_NOT_RESCHEDULE);
544 			handled = B_INVOKE_SCHEDULER;
545 		}
546 	}
547 	return handled;
548 }
549 
550 static int32
551 gx00_interrupt(void *data)
552 {
553 	int32 handled = B_UNHANDLED_INTERRUPT;
554 	device_info *di = (device_info *)data;
555 	shared_info *si = di->si;
556 	int32 *flags = &(si->flags);
557 	vuint32 *regs;
558 
559 	/* is someone already handling an interrupt for this device? */
560 	if (atomic_or(flags, SKD_HANDLER_INSTALLED) & SKD_HANDLER_INSTALLED) {
561 		goto exit0;
562 	}
563 	/* get regs */
564 	regs = di->regs;
565 
566 	/* was it a VBI? */
567 	if (caused_vbi(regs)) {
568 		/*clear the interrupt*/
569 		clear_vbi(regs);
570 		/*release the semaphore*/
571 		handled = thread_interrupt_work(flags, regs, si);
572 	}
573 
574 	/* note that we're not in the handler any more */
575 	atomic_and(flags, ~SKD_HANDLER_INSTALLED);
576 
577 exit0:
578 	return handled;
579 }
580 
581 static status_t open_hook (const char* name, uint32 flags, void** cookie) {
582 	int32 index = 0;
583 	device_info *di;
584 	shared_info *si;
585 	thread_id	thid;
586 	thread_info	thinfo;
587 	status_t	result = B_OK;
588 	vuint32		*regs;
589 	char shared_name[B_OS_NAME_LENGTH];
590 
591 	/* find the device name in the list of devices */
592 	/* we're never passed a name we didn't publish */
593 	while (pd->device_names[index] && (strcmp(name, pd->device_names[index]) != 0)) index++;
594 
595 	/* for convienience */
596 	di = &(pd->di[index]);
597 
598 	/* make sure no one else has write access to the common data */
599 	AQUIRE_BEN(pd->kernel);
600 
601 	/* if it's already open for writing */
602 	if (di->is_open) {
603 		/* mark it open another time */
604 		goto mark_as_open;
605 	}
606 	/* create the shared area */
607 	sprintf(shared_name, DEVICE_FORMAT " shared",
608 		di->pcii.vendor_id, di->pcii.device_id,
609 		di->pcii.bus, di->pcii.device, di->pcii.function);
610 	/* create this area with NO user-space read or write permissions, to prevent accidental dammage */
611 	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);
612 	if (di->shared_area < 0) {
613 		/* return the error */
614 		result = di->shared_area;
615 		goto done;
616 	}
617 
618 	/* save a few dereferences */
619 	si = di->si;
620 
621 	/* save the vendor and device IDs */
622 	si->vendor_id = di->pcii.vendor_id;
623 	si->device_id = di->pcii.device_id;
624 	si->revision = di->pcii.revision;
625 
626 	/* map the device */
627 	result = map_device(di);
628 	if (result < 0) goto free_shared;
629 	result = B_OK;
630 
631 	/* create a semaphore for vertical blank management */
632 	si->vblank = create_sem(0, di->name);
633 	if (si->vblank < 0) {
634 		result = si->vblank;
635 		goto unmap;
636 	}
637 
638 	/* change the owner of the semaphores to the opener's team */
639 	/* this is required because apps can't aquire kernel semaphores */
640 	thid = find_thread(NULL);
641 	get_thread_info(thid, &thinfo);
642 	set_sem_owner(si->vblank, thinfo.team);
643 
644 	/* assign local regs pointer for SAMPLExx() macros */
645 	regs = di->regs;
646 
647 	/* disable and clear any pending interrupts */
648 	disable_vbi(regs);
649 
650 	/* If there is an interrupt line then set up interrupts*/
651 	if ((di->pcii.u.h0.interrupt_pin == 0x00) || (di->pcii.u.h0.interrupt_line == 0xff)){
652 		/*interrupt does not exist so DIE*/
653 		goto delete_the_sem;
654 	} else {
655 		/* otherwise install our interrupt handler */
656 		result = install_io_interrupt_handler(di->pcii.u.h0.interrupt_line, gx00_interrupt, (void *)di, 0);
657 		/* bail if we couldn't install the handler */
658 		if (result != B_OK) goto delete_the_sem;
659 	}
660 
661 mark_as_open:
662 	/* mark the device open */
663 	di->is_open++;
664 
665 	/* send the cookie to the opener */
666 	*cookie = di;
667 
668 	goto done;
669 
670 
671 delete_the_sem:
672 	delete_sem(si->vblank);
673 
674 unmap:
675 	unmap_device(di);
676 
677 free_shared:
678 	/* clean up our shared area */
679 	delete_area(di->shared_area);
680 	di->shared_area = -1;
681 	di->si = NULL;
682 
683 done:
684 	/* end of critical section */
685 	RELEASE_BEN(pd->kernel);
686 
687 	/* all done, return the status */
688 	return result;
689 }
690 
691 /* ----------
692 	read_hook - does nothing, gracefully
693 ----- */
694 static status_t
695 read_hook (void* dev, off_t pos, void* buf, size_t* len)
696 {
697 	*len = 0;
698 	return B_NOT_ALLOWED;
699 }
700 
701 
702 /* ----------
703 	write_hook - does nothing, gracefully
704 ----- */
705 static status_t
706 write_hook (void* dev, off_t pos, const void* buf, size_t* len)
707 {
708 	*len = 0;
709 	return B_NOT_ALLOWED;
710 }
711 
712 /* ----------
713 	close_hook - does nothing, gracefully
714 ----- */
715 static status_t
716 close_hook (void* dev)
717 {
718 	/* we don't do anything on close: there might be dup'd fd */
719 	return B_NO_ERROR;
720 }
721 
722 /* -----------
723 	free_hook - close down the device
724 ----------- */
725 static status_t
726 free_hook (void* dev) {
727 	device_info *di = (device_info *)dev;
728 	shared_info	*si = di->si;
729 	vuint32 *regs = di->regs;
730 
731 	/* lock the driver */
732 	AQUIRE_BEN(pd->kernel);
733 
734 	/* if opened multiple times, decrement the open count and exit */
735 	if (di->is_open > 1)
736 		goto unlock_and_exit;
737 
738 	/* disable and clear any pending interrupts */
739 	disable_vbi(regs);
740 
741 	/* remove interrupt handler */
742 	remove_io_interrupt_handler(di->pcii.u.h0.interrupt_line, gx00_interrupt, di);
743 
744 	/* delete the semaphores, ignoring any errors ('cause the owning team may have died on us) */
745 	delete_sem(si->vblank);
746 	si->vblank = -1;
747 
748 	/* free regs and framebuffer areas */
749 	unmap_device(di);
750 
751 	/* clean up our shared area */
752 	delete_area(di->shared_area);
753 	di->shared_area = -1;
754 	di->si = NULL;
755 
756 unlock_and_exit:
757 	/* mark the device available */
758 	di->is_open--;
759 	/* unlock the driver */
760 	RELEASE_BEN(pd->kernel);
761 	/* all done */
762 	return B_OK;
763 }
764 
765 /* -----------
766 	control_hook - where the real work is done
767 ----------- */
768 static status_t
769 control_hook (void* dev, uint32 msg, void *buf, size_t len) {
770 	device_info *di = (device_info *)dev;
771 	status_t result = B_DEV_INVALID_IOCTL;
772 
773 	switch (msg) {
774 		/* the only PUBLIC ioctl */
775 		case B_GET_ACCELERANT_SIGNATURE: {
776 			char *sig = (char *)buf;
777 			strcpy(sig, current_settings.accelerant);
778 			result = B_OK;
779 		} break;
780 
781 		/* PRIVATE ioctl from here on */
782 		case GX00_GET_PRIVATE_DATA: {
783 			gx00_get_private_data *gpd = (gx00_get_private_data *)buf;
784 			if (gpd->magic == GX00_PRIVATE_DATA_MAGIC) {
785 				gpd->shared_info_area = di->shared_area;
786 				result = B_OK;
787 			}
788 		} break;
789 		case GX00_GET_PCI: {
790 			gx00_get_set_pci *gsp = (gx00_get_set_pci *)buf;
791 			if (gsp->magic == GX00_PRIVATE_DATA_MAGIC) {
792 				pci_info *pcii = &(di->pcii);
793 				gsp->value = get_pci(gsp->offset, gsp->size);
794 				result = B_OK;
795 			}
796 		} break;
797 		case GX00_SET_PCI: {
798 			gx00_get_set_pci *gsp = (gx00_get_set_pci *)buf;
799 			if (gsp->magic == GX00_PRIVATE_DATA_MAGIC) {
800 				pci_info *pcii = &(di->pcii);
801 				set_pci(gsp->offset, gsp->size, gsp->value);
802 				result = B_OK;
803 			}
804 		} break;
805 		case GX00_DEVICE_NAME: { // apsed
806 			gx00_device_name *dn = (gx00_device_name *)buf;
807 			if (dn->magic == GX00_PRIVATE_DATA_MAGIC) {
808 				strcpy(dn->name, di->name);
809 				result = B_OK;
810 			}
811 		} break;
812 		case GX00_RUN_INTERRUPTS: {
813 			gx00_set_bool_state *ri = (gx00_set_bool_state *)buf;
814 			if (ri->magic == GX00_PRIVATE_DATA_MAGIC) {
815 				vuint32 *regs = di->regs;
816 				if (ri->do_it) {
817 					enable_vbi(regs);
818 				} else {
819 					disable_vbi(regs);
820 				}
821 				result = B_OK;
822 			}
823 		} break;
824 	}
825 	return result;
826 }
827 
828