Importing CMI8738-20072011
[AROS.git] / workbench / devs / AHI / Drivers / CMI8738 / driver-init.c
blobc2c3dad7eb0fc3f4c31ce3bf32fe2399bdd4dc3a
1 /*
2 The contents of this file are subject to the AROS Public License Version 1.1 (the "License");
3 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
5 Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
6 ANY KIND, either express or implied. See the License for the specific language governing rights and
7 limitations under the License.
9 The Original Code is written by Davy Wentzler.
12 #include <config.h>
14 #include <exec/memory.h>
16 #undef __USE_INLINE__
17 #include <proto/expansion.h>
19 #include <proto/exec.h>
20 #include <proto/dos.h>
21 #include <proto/fakedma.h>
23 #include "library_card.h"
24 #include "version.h"
25 #include "misc.h"
26 #include "regs.h"
27 #include "DriverData.h"
29 struct DriverBase* AHIsubBase;
31 struct Library* ExpansionBase = NULL;
32 struct ExpansionIFace* IExpansion = NULL;
33 //struct DOSIFace* IDOS = NULL;
34 struct UtilityIFace* IUtility = NULL;
35 struct AHIsubIFace* IAHIsub = NULL;
36 struct PCIIFace* IPCI = NULL;
37 struct MMUIFace* IMMU = NULL;
38 struct Library *FakeDMABase = NULL;
39 struct FakeDMAIFace *IFakeDMA = NULL;
42 #define VENDOR_ID 0x13F6
43 #define DEVICE_ID 0x0111
44 #define CARD_STRING "CMI8738"
47 /******************************************************************************
48 ** Custom driver init *********************************************************
49 ******************************************************************************/
51 BOOL
52 DriverInit( struct DriverBase* ahisubbase )
54 struct CardBase* CardBase = (struct CardBase*) ahisubbase;
55 struct PCIDevice *dev;
56 int card_no;
58 //IExec->DebugPrintF("DRIVERINIT\n");
60 CardBase->driverdatas = 0;
61 CardBase->cards_found = 0;
62 AHIsubBase = ahisubbase;
64 DOSBase = IExec->OpenLibrary( DOSNAME, 37 );
66 if( DOSBase == NULL )
68 Req( "Unable to open 'dos.library' version 37.\n" );
69 return FALSE;
72 if ((IDOS = (struct DOSIFace *) IExec->GetInterface((struct Library *) DOSBase, "main", 1, NULL)) == NULL)
74 Req("Couldn't open IDOS interface!\n");
75 return FALSE;
78 ExpansionBase = IExec->OpenLibrary( "expansion.library", 1 );
79 if( ExpansionBase == NULL )
81 Req( "Unable to open 'expansion.library' version 1.\n" );
82 return FALSE;
84 if ((IExpansion = (struct ExpansionIFace *) IExec->GetInterface((struct Library *) ExpansionBase, "main", 1, NULL)) == NULL)
86 Req("Couldn't open IExpansion interface!\n");
87 return FALSE;
90 if ((IPCI = (struct PCIIFace *) IExec->GetInterface((struct Library *) ExpansionBase, "pci", 1, NULL)) == NULL)
92 Req("Couldn't open IPCI interface!\n");
93 return FALSE;
96 if ((IAHIsub = (struct AHIsubIFace *) IExec->GetInterface((struct Library *) AHIsubBase, "main", 1, NULL)) == NULL)
98 Req("Couldn't open IAHIsub interface!\n");
99 return FALSE;
102 if ((IUtility = (struct UtilityIFace *) IExec->GetInterface((struct Library *) UtilityBase, "main", 1, NULL)) == NULL)
104 Req("Couldn't open IUtility interface!\n");
105 return FALSE;
108 if ((IMMU = (struct MMUIFace *) IExec->GetInterface((struct Library *) SysBase, "mmu", 1, NULL)) == NULL)
110 Req("Couldn't open IMMU interface!\n");
111 return FALSE;
114 if(!( FakeDMABase = (struct Library *)IExec->OpenLibrary( "fakedma.library", 52L )))
116 IExec->DebugPrintF("No fakemda.library found. Using PIO\n");
118 else
120 if(!( IFakeDMA = (struct FakeDMAIFace *)IExec->GetInterface( FakeDMABase, "main", 1L, NULL )))
122 IExec->CloseLibrary( FakeDMABase );
126 IExec->InitSemaphore( &CardBase->semaphore );
129 /*** Count cards ***********************************************************/
131 CardBase->cards_found = 0;
132 dev = NULL;
134 if ( (dev = IPCI->FindDeviceTags( FDT_VendorID, VENDOR_ID, // while
135 FDT_DeviceID, DEVICE_ID, //FDT_INDEX, CardBase->cards_found,
136 TAG_DONE ) ) != NULL )
138 ++CardBase->cards_found;
139 // IExec->DebugPrintF("%s found! :-)\n", CARD_STRING);
142 // Fail if no hardware is present (prevents the audio modes from being added to
143 // the database if the driver cannot be used).
145 if(CardBase->cards_found == 0 )
147 IExec->DebugPrintF("No %s found! :-(\n", CARD_STRING);
148 Req( "No card present.\n" );
149 return FALSE;
152 if (dev->Lock(EXCLUSIVE_LOCK) == FALSE)
154 IExec->DebugPrintF("CMI8738: Couldn't lock the device\n");
155 return FALSE;
158 /*** CAMD ******************************************************************/
159 #if 0
160 IExec->InitSemaphore( &CardBase->camd.Semaphore );
161 CardBase->camd.Semaphore.ss_Link.ln_Pri = 0;
163 CardBase->camd.Semaphore.ss_Link.ln_Name = Card_CAMD_SEMAPHORE;
164 IExec->AddSemaphore( &CardBase->camd.Semaphore );
166 CardBase->camd.Cards = CardBase->cards_found;
167 CardBase->camd.Version = VERSION;
168 CardBase->camd.Revision = REVISION;
171 CardBase->camd.OpenPortFunc.h_Entry = OpenCAMDPort;
172 CardBase->camd.OpenPortFunc.h_SubEntry = NULL;
173 CardBase->camd.OpenPortFunc.h_Data = NULL;
175 CardBase->camd.ClosePortFunc.h_Entry = (HOOKFUNC) CloseCAMDPort;
176 CardBase->camd.ClosePortFunc.h_SubEntry = NULL;
177 CardBase->camd.ClosePortFunc.h_Data = NULL;
179 CardBase->camd.ActivateXmitFunc.h_Entry = (HOOKFUNC) ActivateCAMDXmit;
180 CardBase->camd.ActivateXmitFunc.h_SubEntry = NULL;
181 CardBase->camd.ActivateXmitFunc.h_Data = NULL;
182 #endif
185 /*** Allocate and init all cards *******************************************/
187 CardBase->driverdatas = IExec->AllocVec( sizeof( *CardBase->driverdatas ) *
188 CardBase->cards_found,
189 MEMF_PUBLIC );
191 if( CardBase->driverdatas == NULL )
193 Req( "Out of memory." );
194 return FALSE;
197 card_no = 0;
199 if( ( dev = IPCI->FindDeviceTags( FDT_VendorID, VENDOR_ID, // while
200 FDT_DeviceID, DEVICE_ID, //FDT_INDEX, CardBase->cards_found,
201 TAG_DONE ) ) != NULL )
203 CardBase->driverdatas[ card_no ] = AllocDriverData( dev, AHIsubBase );
204 ++card_no;
207 //IExec->DebugPrintF("exit init\n");
208 return TRUE;
212 /******************************************************************************
213 ** Custom driver clean-up *****************************************************
214 ******************************************************************************/
216 VOID
217 DriverCleanup( struct DriverBase* AHIsubBase )
219 struct CardBase* CardBase = (struct CardBase*) AHIsubBase;
220 int i;
223 #if 0
224 if( CardBase->camd.Semaphore.ss_Link.ln_Name != NULL )
226 IExec->ObtainSemaphore( &CardBase->camd.Semaphore );
227 IExec->RemSemaphore( &CardBase->camd.Semaphore );
228 IExec->ReleaseSemaphore( &CardBase->camd.Semaphore );
230 #endif
231 for( i = 0; i < CardBase->cards_found; ++i )
233 if (CardBase->driverdatas)
235 if (CardBase->driverdatas)
237 CardBase->driverdatas[i]->pci_dev->Unlock();
238 FreeDriverData( CardBase->driverdatas[ i ], AHIsubBase );
243 if (CardBase->driverdatas)
244 IExec->FreeVec( CardBase->driverdatas );
246 if (IFakeDMA)
247 IExec->DropInterface( (struct Interface *)IFakeDMA );
249 if (FakeDMABase)
250 IExec->CloseLibrary( FakeDMABase );
252 if (IUtility)
253 IExec->DropInterface( (struct Interface *) IUtility);
255 if (IExpansion)
256 IExec->DropInterface( (struct Interface *) IExpansion);
258 if (IPCI)
259 IExec->DropInterface( (struct Interface *) IPCI);
261 if (IAHIsub)
262 IExec->DropInterface( (struct Interface *) IAHIsub);
264 if (IDOS)
265 IExec->DropInterface( (struct Interface *) IDOS);
267 if (ExpansionBase)
268 IExec->CloseLibrary( (struct Library*) ExpansionBase);
270 if (UtilityBase)
271 IExec->CloseLibrary( (struct Library*) UtilityBase);
273 if (DOSBase)
274 IExec->CloseLibrary( (struct Library*) DOSBase);