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