source: GPL/branches/uniaud32-next/drv32/rmhelp.c

Last change on this file was 677, checked in by Paul Smedley, 4 years ago

More code cleanups from AlexT from #os2russian

File size: 12.1 KB
Line 
1/*
2 * Resource manager helper functions
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 */
10
11#define INCL_NOPMAPI
12#define INCL_DOSINFOSEG
13#include <os2.h>
14
15#include <devtype.h>
16#include <stacktoflat.h>
17#include <version.h>
18#include <dbgos2.h>
19#include <osspci.h>
20#include <ossidc32.h>
21#include <string.h>
22#include "rmbase.h" // Resource manager definitions.
23#include "rmcalls.h"
24#include "pciids.h"
25
26#define MAX_RESHANDLES 16
27
28static HDRIVER DriverHandle = (HDRIVER)-1;
29static HADAPTER AdapterHandle = 0;
30
31typedef struct
32{
33 ULONG NumResource;
34 HRESOURCE hResource[MAX_RESHANDLES];
35} RESOURCES;
36
37static RESOURCES Resource_stt = {0};
38
39//******************************************************************************
40//******************************************************************************
41VOID RMCreateDriverU32(VOID)
42{
43 HDRIVER hDriver;
44 DRIVERSTRUCT DriverStruct;
45 char DriverName[sizeof(RM_DRIVER_NAME)];
46 char VendorName[sizeof(RM_DRIVER_VENDORNAME)];
47 char DriverDesc[sizeof(RM_DRIVER_DESCRIPTION)];
48
49 //copy strings to stack, because we need to give RM 16:16 pointers
50 //(which can only be (easily) generated from 32 bits stack addresses)
51 strcpy(DriverName, RM_DRIVER_NAME);
52 strcpy(VendorName, RM_DRIVER_VENDORNAME);
53 strcpy(DriverDesc, RM_DRIVER_DESCRIPTION);
54
55 DriverStruct.DrvrName = FlatToSel((ULONG)DriverName);
56 DriverStruct.DrvrDescript = FlatToSel((ULONG)DriverDesc);
57 DriverStruct.VendorName = FlatToSel((ULONG)VendorName);
58 DriverStruct.MajorVer = RM_VMAJOR;
59 DriverStruct.MinorVer = RM_VMINOR;
60 DriverStruct.Date.Year = RM_DRIVER_BUILDYEAR;
61 DriverStruct.Date.Month = RM_DRIVER_BUILDMONTH;
62 DriverStruct.Date.Day = RM_DRIVER_BUILDDAY;
63 DriverStruct.DrvrFlags = 0;
64 DriverStruct.DrvrType = DRT_AUDIO;
65 DriverStruct.DrvrSubType = 0;
66 DriverStruct.DrvrCallback = 0;
67
68 APIRET rc = RMCreateDriver( FlatToSel((ULONG)&DriverStruct),
69 FlatToSel((ULONG)&hDriver) );
70
71 if( rc == RMRC_SUCCESS )
72 {
73 DriverHandle = hDriver;
74 }
75 dprintf(("RMCreateDriver rc=%d DriverHandle=%x", rc, DriverHandle));
76}
77
78//******************************************************************************
79//******************************************************************************
80VOID RMCreateAdapterU32(ULONG DevID, ULONG *phAdapter, USHORT BusDevFunc, ULONG CardNum)
81{
82 APIRET rc;
83 char szAdapterName[128];
84 char szMixerName[64];
85
86 szAdapterName[0] = szMixerName[0] = '\0';
87
88 if(OSS32_QueryNames(CardNum, szAdapterName, sizeof(szAdapterName),
89 szMixerName, sizeof(szMixerName), FALSE) != OSSERR_SUCCESS )
90 {
91 return; // error
92 }
93
94 switch(DevID)
95 {
96 case PCIID_VIA_686A:
97 case PCIID_VIA_8233:
98 case PCIID_SI_7012:
99 case PCIID_INTEL_82801:
100 case PCIID_INTEL_82901:
101 case PCIID_INTEL_92801BA:
102 case PCIID_INTEL_440MX:
103 case PCIID_INTEL_ICH3:
104 case PCIID_INTEL_ICH4:
105 case PCIID_INTEL_ICH5:
106 case PCIID_INTEL_ICH6:
107 case PCIID_INTEL_ICH7:
108 case PCIID_NVIDIA_MCP_AUDIO:
109 case PCIID_NVIDIA_MCP2_AUDIO:
110 case PCIID_NVIDIA_MCP3_AUDIO:
111 case PCIID_NVIDIA_CK8S_AUDIO:
112 case PCIID_NVIDIA_CK8_AUDIO:
113 strcat(szAdapterName, " with ");
114 strcat(szAdapterName, szMixerName);
115 break;
116/*
117 case PCIID_CREATIVELABS_SBLIVE:
118 case PCIID_ALS4000:
119 case PCIID_CMEDIA_CM8338A:
120 case PCIID_CMEDIA_CM8338B:
121 case PCIID_CMEDIA_CM8738:
122 case PCIID_CMEDIA_CM8738B:
123 case PCIID_CIRRUS_4281:
124 case PCIID_CIRRUS_4280:
125 case PCIID_CIRRUS_4612:
126 case PCIID_CIRRUS_4615:
127 case PCIID_ESS_ALLEGRO_1:
128 case PCIID_ESS_ALLEGRO:
129 case PCIID_ESS_MAESTRO3:
130 case PCIID_ESS_MAESTRO3_1:
131 case PCIID_ESS_MAESTRO3_HW:
132 case PCIID_ESS_MAESTRO3_2:
133 case PCIID_ESS_CANYON3D_2LE:
134 case PCIID_ESS_CANYON3D_2:
135 case PCIID_ESS_ES1938:
136 case PCIID_AUREAL_VORTEX:
137 case PCIID_AUREAL_VORTEX2:
138 case PCIID_AUREAL_ADVANTAGE:
139 case PCIID_ENSONIQ_CT5880:
140 case PCIID_ENSONIQ_ES1371:
141 case PCIID_YAMAHA_YMF724:
142 case PCIID_YAMAHA_YMF724F:
143 case PCIID_YAMAHA_YMF740:
144 case PCIID_YAMAHA_YMF740C:
145 case PCIID_YAMAHA_YMF744:
146 case PCIID_YAMAHA_YMF754:
147 case PCIID_ESS_M2E:
148 case PCIID_ESS_M2:
149 case PCIID_ESS_M1:
150 case PCIID_ALI_5451:
151 case PCIID_TRIDENT_4DWAVE_DX:
152 case PCIID_TRIDENT_4DWAVE_NX:
153 case PCIID_SI_7018:
154 case PCIID_FM801:
155 case PCIID_ATIIXP_SB200:
156 case PCIID_ATIIXP_SB300:
157 case PCIID_ATIIXP_SB400:
158 case PCIID_AUDIGYLS:
159 case PCIID_AUDIGYLS1:
160 case PCIID_AUDIGYLS2:
161 break;
162*/
163 }
164
165 if( !szAdapterName[0] )
166 {
167 strcpy(szAdapterName, "Unknown");
168 }
169
170 //copy to stack, because we need to give RM 16:16 pointers
171 //(which can only be (easily) generated from 32 bits stack addresses)
172 RESOURCES Resource_loc;
173 // is any resoures detected and registered in RM ?
174 if( Resource_stt.NumResource )
175 {
176 memcpy( &Resource_loc, &Resource_stt, sizeof(Resource_loc) );
177 }
178 else
179 {
180 dprintf(("No resources allocated !!!"));
181 Resource_loc.NumResource = 0;
182 }
183
184 ADJUNCT adjBusDevFunc;
185 adjBusDevFunc.pNextAdj = NULL;
186 adjBusDevFunc.AdjLength = sizeof(adjBusDevFunc);
187 adjBusDevFunc.AdjType = ADJ_PCI_DEVFUNC;
188 adjBusDevFunc.PCI_DevFunc = BusDevFunc;
189
190 ADAPTERSTRUCT AdapterStruct;
191 AdapterStruct.AdaptDescriptName = FlatToSel((ULONG)szAdapterName); /* ### IHV */
192 AdapterStruct.AdaptFlags = AS_16MB_ADDRESS_LIMIT; // AdaptFlags /* ### IHV */
193 AdapterStruct.BaseType = AS_BASE_MMEDIA; // BaseType
194 AdapterStruct.SubType = AS_SUB_MM_AUDIO; // SubType
195 AdapterStruct.InterfaceType = AS_INTF_GENERIC; // InterfaceType
196 AdapterStruct.HostBusType = AS_HOSTBUS_PCI; // HostBusType /* ### IHV */
197 AdapterStruct.HostBusWidth = AS_BUSWIDTH_32BIT; // HostBusWidth /* ### IHV */
198 AdapterStruct.pAdjunctList = FlatToSel((ULONG)&adjBusDevFunc);// pAdjunctList /* ### IHV */
199 AdapterStruct.Reserved = 0;
200
201 //--- Register adapter. We'll record any error code, but won't fail
202 // the driver initialization and won't return resources.
203 //NOTE: hAdapter must be used as FlatToSel only works for stack variables
204 HADAPTER hAdapter;
205 rc = RMCreateAdapter(DriverHandle, // Handle to driver
206 FlatToSel((ULONG)&hAdapter), // (OUT) Handle to adapter
207 FlatToSel((ULONG)&AdapterStruct), // Adapter structure
208 0, // Parent device (defaults OK)
209 Resource_loc.NumResource ? (FlatToSel((ULONG)&Resource_loc)) : 0); // Allocated resources.
210
211 dprintf(("RMCreateAdapter rc=%d", rc));
212
213 if( rc == 0 )
214 {
215 AdapterHandle = hAdapter;
216 *phAdapter = hAdapter;
217 Resource_stt.NumResource = 0; // no resource handles to be freed
218 }
219 else
220 {
221 RMDeallocRes();
222 }
223}
224
225//******************************************************************************
226//******************************************************************************
227BOOL RMRequestIO(ULONG ulIOBase, ULONG ulIOLength)
228{
229 RESOURCESTRUCT Resource;
230 HRESOURCE hres;
231 APIRET rc;
232
233 Resource.ResourceType = RS_TYPE_IO;
234 Resource.IOResource.BaseIOPort = (USHORT)ulIOBase;
235 Resource.IOResource.NumIOPorts = (USHORT)ulIOLength;
236 Resource.IOResource.IOFlags = RS_IO_EXCLUSIVE;
237 Resource.IOResource.IOAddressLines = ( ulIOBase > 0x3ff ) ? 16 : 10;
238
239 rc = RMAllocResource(DriverHandle, // Handle to driver.
240 FlatToSel((ULONG)&hres), // OUT: "allocated" resource node handle
241 FlatToSel((ULONG)&Resource)); // Resource to allocate.
242
243 if( rc == 0 && Resource_stt.NumResource < MAX_RESHANDLES )
244 {
245 Resource_stt.hResource[Resource_stt.NumResource++] = hres; return TRUE;
246 }
247
248 dprintf(("RMAllocResource[%d] IO rc = %d", Resource_stt.NumResource, rc));
249
250 return FALSE;
251}
252
253
254//******************************************************************************
255//******************************************************************************
256BOOL RMRequestMem(ULONG ulMemBase, ULONG ulMemLength)
257{
258 RESOURCESTRUCT Resource;
259 HRESOURCE hres;
260 APIRET rc;
261
262 Resource.ResourceType = RS_TYPE_MEM;
263 Resource.MEMResource.MemBase = ulMemBase;
264 Resource.MEMResource.MemSize = ulMemLength;
265 Resource.MEMResource.MemFlags = RS_MEM_EXCLUSIVE;
266
267 rc = RMAllocResource(DriverHandle, // Handle to driver.
268 FlatToSel((ULONG)&hres), // OUT: "allocated" resource node handle
269 FlatToSel((ULONG)&Resource)); // Resource to allocate.
270
271 if( rc == 0 && Resource_stt.NumResource < MAX_RESHANDLES )
272 {
273 Resource_stt.hResource[Resource_stt.NumResource++] = hres; return TRUE;
274 }
275
276 dprintf(("RMAllocResource[%d] MEM rc = %d", Resource_stt.NumResource, rc));
277
278 return rc == 0;
279}
280
281
282//******************************************************************************
283//******************************************************************************
284BOOL RMRequestIRQ(ULONG ulIrq, BOOL fShared, PHRESOURCE phRes)
285{
286 RESOURCESTRUCT Resource;
287 HRESOURCE hRes;
288 APIRET rc;
289
290 memset( &Resource, 0, sizeof(Resource) );
291 Resource.ResourceType = RS_TYPE_IRQ;
292 Resource.IRQResource.IRQLevel = (USHORT)ulIrq & 0xff;
293 Resource.IRQResource.PCIIrqPin = 0;
294 Resource.IRQResource.IRQFlags = ( fShared ) ? RS_IRQ_SHARED : RS_IRQ_EXCLUSIVE;
295
296 rc = RMAllocResource(DriverHandle, // Handle to driver.
297 FlatToSel((ULONG)&hRes), // OUT: "allocated" resource node handle
298 FlatToSel((ULONG)&Resource)); // Resource to allocate.
299
300 if (rc == 0)
301 {
302 *phRes = hRes;
303 if (AdapterHandle)
304 {
305 dprintf(("RMRequestIRQ: DriverHandle=%x AdapterHandle=%x hRes=%x", DriverHandle, AdapterHandle, hRes));
306 rc = RMModifyResources(DriverHandle, AdapterHandle, RM_MODIFY_ADD, hRes);
307 }
308 else
309 {
310 if (Resource_stt.NumResource < MAX_RESHANDLES )
311 {
312 Resource_stt.hResource[Resource_stt.NumResource++] = hRes;
313 }
314 }
315 }
316 dprintf(("RMAllocResource[%d] IRQ=%d rc=%d", Resource_stt.NumResource, ulIrq, rc));
317 return rc == 0;
318}
319
320//******************************************************************************
321//******************************************************************************
322BOOL RMDeallocateIRQ(HRESOURCE hRes)
323{
324 APIRET rc;
325
326 dprintf(("RMDeallocateIRQ: DriverHandle=%x AdapterHandle=%x hRes=%x", DriverHandle, AdapterHandle, hRes));
327 rc = RMModifyResources(DriverHandle, AdapterHandle, RM_MODIFY_DELETE, hRes);
328
329 return rc == 0;
330}
331
332//******************************************************************************
333//******************************************************************************
334VOID RMDeallocRes(VOID)
335{
336 // free resource handles
337 while( Resource_stt.NumResource )
338 {
339 RMDeallocResource(DriverHandle, Resource_stt.hResource[--Resource_stt.NumResource]);
340 }
341}
342
343/* DAZ - dirty hack so that resource manager is updated correctly
344 * when using APIC and multiple adapters */
345VOID RMSetHandles(HADAPTER hAdapter)
346{
347 AdapterHandle = hAdapter;
348}
Note: See TracBrowser for help on using the repository browser.