Fix crash from uninitialized list and other errors. Driver loads but makes no noise...
[AROS.git] / workbench / devs / AHI / Drivers / SB128 / driver-init.c
blobe73f87ac1ce11162b6c36fe1596cfd0ae2519ff2
1 /*
3 The contents of this file are subject to the AROS Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
4 http://www.aros.org/license.html
6 Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
7 ANY KIND, either express or implied. See the License for the specific language governing rights and
8 limitations under the License.
10 The Original Code is (C) Copyright 2004-2011 Ross Vumbaca.
12 The Initial Developer of the Original Code is Ross Vumbaca.
14 All Rights Reserved.
18 #define DEBUG 1
19 #include <aros/debug.h>
20 #define DebugPrintF bug
22 #include <exec/memory.h>
24 #include <proto/exec.h>
25 #include <proto/dos.h>
27 #include "library.h"
28 #include "version.h"
29 #include "misc.h"
30 #include "regs.h"
32 #include "pci_wrapper.h"
34 struct DosLibrary* DOSBase;
35 struct DriverBase* AHIsubBase;
36 struct Library* ExpansionBase = NULL;
38 struct VendorDevice
40 UWORD vendor;
41 UWORD device;
44 #define CARD_STRING "SB128"
45 #define MAX_DEVICE_VENDORS 512
47 struct VendorDevice *vendor_device_list = NULL;
48 static int vendor_device_list_size = 0;
50 /******************************************************************************
51 ** Custom driver init *********************************************************
52 ******************************************************************************/
54 BOOL
55 DriverInit( struct DriverBase* ahisubbase )
57 struct SB128Base* SB128Base = (struct SB128Base*) ahisubbase;
58 struct PCIDevice *dev;
59 int card_no, i;
60 struct List foundCards;
61 struct Node *devTmp;
63 bug("[SB128]: %s()\n", __PRETTY_FUNCTION__);
65 SB128Base->cards_found = 0;
66 SB128Base->driverdatas = 0;
67 AHIsubBase = ahisubbase;
69 NewList(&foundCards);
71 DOSBase = OpenLibrary( DOSNAME, 37 );
73 if( DOSBase == NULL )
75 Req( "Unable to open 'dos.library' version 37.\n" );
76 return FALSE;
79 ExpansionBase = OpenLibrary( "expansion.library", 1 );
80 if( ExpansionBase == NULL )
82 Req( "Unable to open 'expansion.library' version 1.\n" );
83 return FALSE;
86 if (!ahi_pci_init(ahisubbase))
88 return FALSE;
91 InitSemaphore( &SB128Base->semaphore );
93 /*** Count cards ***********************************************************/
95 vendor_device_list = (struct VendorDevice *) AllocVec(sizeof(struct VendorDevice) * MAX_DEVICE_VENDORS, MEMF_PUBLIC | MEMF_CLEAR);
97 vendor_device_list[0].vendor = 0x1274;
98 vendor_device_list[0].device = 0x5000;
99 vendor_device_list_size++;
101 vendor_device_list[1].vendor = 0x1274;
102 vendor_device_list[1].device = 0x1371;
103 vendor_device_list_size++;
105 vendor_device_list[2].vendor = 0x1274;
106 vendor_device_list[2].device = 0x5880;
107 vendor_device_list_size++;
109 vendor_device_list[3].vendor = 0x1102;
110 vendor_device_list[3].device = 0x8938;
111 vendor_device_list_size++;
113 bug("vendor_device_list_size = %ld\n", vendor_device_list_size);
115 SB128Base->cards_found = 0;
116 dev = NULL;
118 for (i = 0; i < vendor_device_list_size; i++)
120 dev = ahi_pci_find_device(vendor_device_list[i].vendor, vendor_device_list[i].device, dev);
122 if (dev != NULL)
124 bug("[SB128] %s: Found SB128 #%d [%4x:%4x] pci obj @ 0x%p\n", __PRETTY_FUNCTION__, i, vendor_device_list[i].vendor, vendor_device_list[i].device, dev);
125 ++SB128Base->cards_found;
127 devTmp = AllocVec(sizeof(struct Node), MEMF_CLEAR);
128 devTmp->ln_Name = dev;
129 AddTail(&foundCards, devTmp);
133 // Fail if no hardware is present (prevents the audio modes from being added to
134 // the database if the driver cannot be used).
136 if(SB128Base->cards_found == 0 )
138 DebugPrintF("No SB128 found! :-(\n");
139 #if defined(VERBOSE_REQ)
140 Req( "No card present.\n" );
141 #endif
142 return FALSE;
145 /*** CAMD ******************************************************************/
146 #if 0
147 InitSemaphore( &SB128Base->camd.Semaphore );
148 SB128Base->camd.Semaphore.ss_Link.ln_Pri = 0;
150 SB128Base->camd.Semaphore.ss_Link.ln_Name = Card_CAMD_SEMAPHORE;
151 AddSemaphore( &SB128Base->camd.Semaphore );
153 SB128Base->camd.Cards = SB128Base->cards_found;
154 SB128Base->camd.Version = VERSION;
155 SB128Base->camd.Revision = REVISION;
158 SB128Base->camd.OpenPortFunc.h_Entry = OpenCAMDPort;
159 SB128Base->camd.OpenPortFunc.h_SubEntry = NULL;
160 SB128Base->camd.OpenPortFunc.h_Data = NULL;
162 SB128Base->camd.ClosePortFunc.h_Entry = (HOOKFUNC) CloseCAMDPort;
163 SB128Base->camd.ClosePortFunc.h_SubEntry = NULL;
164 SB128Base->camd.ClosePortFunc.h_Data = NULL;
166 SB128Base->camd.ActivateXmitFunc.h_Entry = (HOOKFUNC) ActivateCAMDXmit;
167 SB128Base->camd.ActivateXmitFunc.h_SubEntry = NULL;
168 SB128Base->camd.ActivateXmitFunc.h_Data = NULL;
169 #endif
172 /*** Allocate and init all cards *******************************************/
174 SB128Base->driverdatas = AllocVec( sizeof( *SB128Base->driverdatas ) *
175 SB128Base->cards_found,
176 MEMF_PUBLIC );
178 if( SB128Base->driverdatas == NULL )
180 Req( "Out of memory." );
181 return FALSE;
184 card_no = 0;
186 struct Node *scratchNode;
187 ForeachNodeSafe(&foundCards, devTmp, scratchNode)
189 Remove(devTmp);
191 dev = devTmp->ln_Name;
192 bug("[SB128] %s: Prepairing card #%d pci obj @ 0x%p\n", __PRETTY_FUNCTION__, card_no, dev);
193 SB128Base->driverdatas[ card_no ] = AllocDriverData( dev, AHIsubBase );
195 FreeVec(devTmp);
196 ++card_no;
199 bug("[SB128] %s: Done.\n", __PRETTY_FUNCTION__);
202 return TRUE;
206 /******************************************************************************
207 ** Custom driver clean-up *****************************************************
208 ******************************************************************************/
210 VOID
211 DriverCleanup( struct DriverBase* AHIsubBase )
213 struct SB128Base* SB128Base = (struct SB128Base*) AHIsubBase;
214 int i;
215 #if 0
216 if( SB128Base->camd.Semaphore.ss_Link.ln_Name != NULL )
218 ObtainSemaphore( &SB128Base->camd.Semaphore );
219 RemSemaphore( &SB128Base->camd.Semaphore );
220 ReleaseSemaphore( &SB128Base->camd.Semaphore );
222 #endif
224 for( i = 0; i < SB128Base->cards_found; ++i )
226 if (SB128Base->driverdatas)
228 pci_outl( 0, SB128_SCON, SB128Base->driverdatas[i] );
229 FreeDriverData( SB128Base->driverdatas[ i ], AHIsubBase );
233 if (SB128Base->driverdatas)
234 FreeVec( SB128Base->driverdatas );
236 /*if (TimerIO)
238 CloseDevice((struct IORequest *)TimerIO);
239 DeleteIORequest((struct IORequest *)TimerIO);
242 /*if (replymp)
243 DeletePort(replymp);*/
245 ahi_pci_exit();
247 if (ExpansionBase)
248 CloseLibrary( (struct Library*) ExpansionBase );
250 if (UtilityBase)
251 CloseLibrary( (struct Library*) UtilityBase );
253 if (DOSBase)
254 CloseLibrary( (struct Library*) DOSBase );