change init code to support multiple cards. small fixes and corrections to code.
[AROS.git] / workbench / devs / AHI / Drivers / CMI8738 / driver-init.c
blob992ca35a9c421859d4f1e0f43793e1506b3de41f
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 #ifdef __AROS__
13 #define DEBUG 1
14 #include <aros/debug.h>
15 #define DebugPrintF bug
16 #endif
18 #include <exec/memory.h>
20 #if !defined(__AROS__)
21 #undef __USE_INLINE__
22 #include <proto/expansion.h>
23 #endif
25 #include <proto/exec.h>
26 #include <proto/dos.h>
28 #include "library.h"
29 #include "version.h"
30 #include "misc.h"
31 #include "regs.h"
33 #include "pci_wrapper.h"
35 struct DriverBase* AHIsubBase;
37 struct DosLibrary* DOSBase;
38 struct Library* ExpansionBase = NULL;
40 struct VendorDevice
42 UWORD vendor;
43 UWORD device;
46 #define VENDOR_ID 0x13F6
47 #define DEVICE_ID 0x0111
48 #define CARD_STRING "CMI8738"
49 #define MAX_DEVICE_VENDORS 512
51 struct VendorDevice *vendor_device_list = NULL;
52 static int vendor_device_list_size = 0;
54 /******************************************************************************
55 ** Custom driver init *********************************************************
56 ******************************************************************************/
58 BOOL
59 DriverInit( struct DriverBase* ahisubbase )
61 struct CMI8738Base *CMI8738Base = (struct CMI8738Base*) ahisubbase;
62 struct PCIDevice *dev;
63 int card_no, i;
64 struct List foundCards;
65 struct Node *devTmp;
67 bug("[CMI8738]: %s()\n", __PRETTY_FUNCTION__);
69 CMI8738Base->driverdatas = 0;
70 CMI8738Base->cards_found = 0;
71 AHIsubBase = ahisubbase;
73 NewList(&foundCards);
75 DOSBase = OpenLibrary( DOSNAME, 37 );
77 if( DOSBase == NULL )
79 Req( "CMI8738: Unable to open 'dos.library' version 37.\n" );
80 return FALSE;
83 ExpansionBase = OpenLibrary( "expansion.library", 1 );
84 if( ExpansionBase == NULL )
86 Req( "CMI8738: Unable to open 'expansion.library' version 1.\n" );
87 return FALSE;
90 if (!ahi_pci_init(ahisubbase))
92 return FALSE;
95 InitSemaphore( &CMI8738Base->semaphore );
97 /*** Count cards ***********************************************************/
99 vendor_device_list = (struct VendorDevice *) AllocVec(sizeof(struct VendorDevice) * MAX_DEVICE_VENDORS, MEMF_PUBLIC | MEMF_CLEAR);
101 vendor_device_list[0].vendor = VENDOR_ID;
102 vendor_device_list[0].device = DEVICE_ID;
103 vendor_device_list_size++;
105 bug("vendor_device_list_size = %ld\n", vendor_device_list_size);
107 CMI8738Base->cards_found = 0;
108 dev = NULL;
110 for (i = 0; i < vendor_device_list_size; i++)
112 dev = ahi_pci_find_device(vendor_device_list[i].vendor, vendor_device_list[i].device, dev);
114 if (dev != NULL)
116 bug("[CMI8738] %s: Found CMI8738 #%d [%4x:%4x] pci obj @ 0x%p\n", __PRETTY_FUNCTION__, i, vendor_device_list[i].vendor, vendor_device_list[i].device, dev);
117 ++CMI8738Base->cards_found;
119 devTmp = AllocVec(sizeof(struct Node), MEMF_CLEAR);
120 devTmp->ln_Name = dev;
121 AddTail(&foundCards, devTmp);
125 // Fail if no hardware is present (prevents the audio modes from being added to
126 // the database if the driver cannot be used).
128 if(CMI8738Base->cards_found == 0 )
130 DebugPrintF("No CMI8738 found! :-(\n");
131 #if defined(VERBOSE_REQ)
132 Req( "No card present.\n" );
133 #endif
134 return FALSE;
137 /*** CAMD ******************************************************************/
138 #if 0
139 InitSemaphore( &CMI8738Base->camd.Semaphore );
140 CMI8738Base->camd.Semaphore.ss_Link.ln_Pri = 0;
142 CMI8738Base->camd.Semaphore.ss_Link.ln_Name = Card_CAMD_SEMAPHORE;
143 AddSemaphore( &CMI8738Base->camd.Semaphore );
145 CMI8738Base->camd.Cards = CMI8738Base->cards_found;
146 CMI8738Base->camd.Version = VERSION;
147 CMI8738Base->camd.Revision = REVISION;
149 CMI8738Base->camd.OpenPortFunc.h_Entry = OpenCAMDPort;
150 CMI8738Base->camd.OpenPortFunc.h_SubEntry = NULL;
151 CMI8738Base->camd.OpenPortFunc.h_Data = NULL;
153 CMI8738Base->camd.ClosePortFunc.h_Entry = (HOOKFUNC) CloseCAMDPort;
154 CMI8738Base->camd.ClosePortFunc.h_SubEntry = NULL;
155 CMI8738Base->camd.ClosePortFunc.h_Data = NULL;
157 CMI8738Base->camd.ActivateXmitFunc.h_Entry = (HOOKFUNC) ActivateCAMDXmit;
158 CMI8738Base->camd.ActivateXmitFunc.h_SubEntry = NULL;
159 CMI8738Base->camd.ActivateXmitFunc.h_Data = NULL;
160 #endif
162 /*** Allocate and init all cards *******************************************/
164 CMI8738Base->driverdatas = AllocVec( sizeof( *CMI8738Base->driverdatas ) *
165 CMI8738Base->cards_found,
166 MEMF_PUBLIC | MEMF_CLEAR);
168 if( CMI8738Base->driverdatas == NULL )
170 Req( "Out of memory." );
171 return FALSE;
174 card_no = 0;
176 struct Node *scratchNode;
177 ForeachNodeSafe(&foundCards, devTmp, scratchNode)
179 Remove(devTmp);
181 dev = devTmp->ln_Name;
182 bug("[CMI8738] %s: Prepairing card #%d pci obj @ 0x%p\n", __PRETTY_FUNCTION__, card_no, dev);
183 CMI8738Base->driverdatas[ card_no ] = AllocDriverData( dev, AHIsubBase );
185 FreeVec(devTmp);
186 ++card_no;
189 bug("[CMI8738] %s: Done.\n", __PRETTY_FUNCTION__);
191 return TRUE;
195 /******************************************************************************
196 ** Custom driver clean-up *****************************************************
197 ******************************************************************************/
199 VOID
200 DriverCleanup( struct DriverBase* AHIsubBase )
202 struct CMI8738Base* CMI8738Base = (struct CMI8738Base*) AHIsubBase;
203 int i;
205 bug("[CMI8738]: %s()\n", __PRETTY_FUNCTION__);
207 for( i = 0; i < CMI8738Base->cards_found; ++i )
209 FreeDriverData( CMI8738Base->driverdatas[ i ], AHIsubBase );
212 FreeVec( CMI8738Base->driverdatas );
214 ahi_pci_exit();
216 if (ExpansionBase)
217 CloseLibrary( (struct Library*) ExpansionBase);
219 if (UtilityBase)
220 CloseLibrary( (struct Library*) UtilityBase);
222 if (DOSBase)
223 CloseLibrary( (struct Library*) DOSBase);