BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / audio / echo / generic / OsSupportBeOS.cpp
blob783da6a868b082e127bfe8bfb68b12306c1f110e
1 // ****************************************************************************
2 //
3 // OsSupportBeOS.cpp
4 //
5 // Implementation file for BeOS support services to the CEchoGals
6 // generic driver class
7 // Set editor tabs to 3 for your viewing pleasure.
8 //
9 // ----------------------------------------------------------------------------
11 // This file is part of Echo Digital Audio's generic driver library.
12 // Copyright Echo Digital Audio Corporation (c) 1998 - 2005
13 // All rights reserved
14 // www.echoaudio.com
16 // This library is free software; you can redistribute it and/or
17 // modify it under the terms of the GNU Lesser General Public
18 // License as published by the Free Software Foundation; either
19 // version 2.1 of the License, or (at your option) any later version.
21 // This library is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 // Lesser General Public License for more details.
26 // You should have received a copy of the GNU Lesser General Public
27 // License along with this library; if not, write to the Free Software
28 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 // ****************************************************************************
32 #include "OsSupportBeOS.h"
34 #include "EchoGalsXface.h"
36 #include <KernelExport.h>
37 #include "util.h"
40 // Version information.
41 // In NT, we want to get this from a resource
43 BYTE OsGetVersion()
45 // Use EngFindResource, for now hard code
46 return( 1 );
47 } // BYTE OsGetVersion()
49 BYTE OsGetRevision()
51 // Use EngFindResource, for now hard code
52 return( 0 );
53 } // BYTE OsGetRevision()
55 BYTE OsGetRelease()
57 // Use EngFindResource, for now hard code
58 return( 0 );
59 } // BYTE OsGetRelease()
62 // Global Memory Management Functions
64 DWORD gAllocNonPagedCount = 0;
66 LIST_HEAD(mems, _echo_mem) mems;
69 static echo_mem *
70 echo_mem_new(size_t size)
72 echo_mem *mem = NULL;
74 if ((mem = (echo_mem*)malloc(sizeof(*mem))) == NULL)
75 return (NULL);
77 mem->area = alloc_mem(&mem->phy_base, &mem->log_base, size, "echo buffer");
78 mem->size = size;
79 if (mem->area < B_OK) {
80 free(mem);
81 return NULL;
83 return mem;
86 static void
87 echo_mem_delete(echo_mem *mem)
89 if(mem->area > B_OK)
90 delete_area(mem->area);
91 free(mem);
94 echo_mem *
95 echo_mem_alloc(size_t size)
97 echo_mem *mem = NULL;
99 mem = echo_mem_new(size);
100 if (mem == NULL)
101 return (NULL);
103 LIST_INSERT_HEAD(&mems, mem, next);
105 return mem;
108 void
109 echo_mem_free(void *ptr)
111 echo_mem *mem = NULL;
113 LIST_FOREACH(mem, &mems, next) {
114 if (mem->log_base != ptr)
115 continue;
116 LIST_REMOVE(mem, next);
118 echo_mem_delete(mem);
119 break;
123 void OsAllocateInit()
125 gAllocNonPagedCount = 0;
127 /* Init mems list */
128 LIST_INIT(&mems);
131 // ***********************************************************************
133 // Allocate locked, non-pageable block of memory. Does not have to be
134 // physically contiguous. Primarily used to implement the overloaded
135 // new operator.
137 // ***********************************************************************
139 ECHOSTATUS OsAllocateNonPaged
141 DWORD dwByteCt, // Block size in bytes
142 PPVOID ppMemAddr // Where to return memory ptr
147 echo_mem * mem = echo_mem_alloc( dwByteCt );
148 if(mem)
149 *ppMemAddr = mem->log_base;
151 if ( NULL == *ppMemAddr )
153 ECHO_DEBUGPRINTF( ("OsAllocateNonPaged : Failed on %ld bytes\n",
154 dwByteCt) );
155 ECHO_DEBUGBREAK();
156 return ECHOSTATUS_NO_MEM;
159 OsZeroMemory( *ppMemAddr, dwByteCt );
161 gAllocNonPagedCount++;
162 ECHO_DEBUGPRINTF(("gAllocNonPagedCount %ld\n",gAllocNonPagedCount));
164 return ECHOSTATUS_OK;
166 } // ECHOSTATUS OsAllocateNonPaged
169 // ***********************************************************************
171 // Unlock and free, non-pageable block of memory.
173 // ***********************************************************************
174 ECHOSTATUS OsFreeNonPaged
176 PVOID pMemAddr
179 echo_mem_free( pMemAddr );
181 gAllocNonPagedCount--;
182 ECHO_DEBUGPRINTF(("gAllocNonPagedCount %ld\n",gAllocNonPagedCount));
184 return ECHOSTATUS_OK;
186 } // ECHOSTATUS OsFreeNonPaged
190 // ***********************************************************************
192 // This class is optional and uniquely defined for each OS. It provides
193 // information that other components may require.
194 // For example, in Windows NT it contains a device object used by various
195 // memory management methods.
196 // Since static variables are used in place of globals, an instance must
197 // be constructed and initialized by the OS Interface object prior to
198 // constructing the CEchoGals derived object. The CEchoGals and
199 // CDspCommObject classes must have access to it during their respective
200 // construction times.
202 // ***********************************************************************
204 COsSupport::COsSupport
206 WORD wDeviceId, // PCI bus device ID
207 WORD wCardRev // Card revision number
210 ECHO_DEBUGPRINTF(("COsSupport::COsSupport born, device id = 0x%x.\n", wDeviceId));
212 m_wDeviceId = wDeviceId;
213 m_wCardRev = wCardRev;
216 COsSupport::~COsSupport()
218 ECHO_DEBUGPRINTF(("COsSupport is all gone - m_dwPageBlockCount %ld\n",m_dwPageBlockCount));
222 // Timer Methods
225 ECHOSTATUS COsSupport::OsGetSystemTime
227 PULONGLONG pullTime // Where to return system time
230 *pullTime = ULONGLONG(system_time());
232 return ECHOSTATUS_OK;
234 } // ECHOSTATUS COsSupport::OsGetSystemTime
237 ECHOSTATUS COsSupport::OsSnooze
239 DWORD dwTime // Duration in micro seconds
242 status_t status;
243 status = snooze(bigtime_t(dwTime));
244 switch (status) {
245 case B_OK:
246 return ECHOSTATUS_OK;
247 break;
248 case B_INTERRUPTED:
249 return ECHOSTATUS_OPERATION_CANCELED; // maybe not appropriate, but anyway
250 break;
251 default:
252 return ECHOSTATUS_NOT_SUPPORTED; // no generic error?
253 break;
259 // Memory Management Methods
262 //---------------------------------------------------------------------------
264 // Allocate a physical page block that can be used for DSP bus mastering.
266 //---------------------------------------------------------------------------
268 ECHOSTATUS COsSupport::AllocPhysPageBlock
270 DWORD dwBytes,
271 PPAGE_BLOCK &pPageBlock
274 DWORD dwRoundedBytes;
277 // Allocate
279 dwRoundedBytes = (dwBytes + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
280 ECHO_DEBUGPRINTF(("COsSupport::AllocPhysPageBlock - dwBytes %ld dwRoundedBytes %ld\n",
281 dwBytes,dwRoundedBytes));
283 pPageBlock = echo_mem_alloc ( dwRoundedBytes );
285 if (NULL == pPageBlock)
287 ECHO_DEBUGPRINTF(("AllocPhysPageBlock failed for %ld bytes\n",dwBytes));
289 pPageBlock = NULL;
290 return ECHOSTATUS_NO_MEM;
293 ECHO_DEBUGPRINTF(("\tIOBufferMemoryDescriptor is OK\n"));
295 OsZeroMemory( pPageBlock->log_base, dwRoundedBytes );
297 #ifdef _DEBUG
298 m_dwPageBlockCount++;
299 ECHO_DEBUGPRINTF(("\tm_dwPageBlockCount %ld\n",m_dwPageBlockCount));
300 #endif
302 return ECHOSTATUS_OK;
304 } // AllocPageBlock
307 //---------------------------------------------------------------------------
309 // Free a physical page block
311 //---------------------------------------------------------------------------
313 ECHOSTATUS COsSupport::FreePhysPageBlock
315 DWORD dwBytes,
316 PPAGE_BLOCK pPageBlock
319 echo_mem_free(pPageBlock->log_base);
321 #ifdef _DEBUG
322 m_dwPageBlockCount--;
323 ECHO_DEBUGPRINTF(("\tm_dwPageBlockCount %ld\n",m_dwPageBlockCount));
324 #endif
326 return ECHOSTATUS_OK;
328 } // FreePageBlock
331 //---------------------------------------------------------------------------
333 // Get the virtual address for the buffer corresponding to the MDL
335 //---------------------------------------------------------------------------
337 PVOID COsSupport::GetPageBlockVirtAddress
339 PPAGE_BLOCK pPageBlock
343 return pPageBlock->log_base;
345 } // GetPageBlockVirtAddress
348 //---------------------------------------------------------------------------
350 // Get the physical address for part of the buffer corresponding to the MDL
352 //---------------------------------------------------------------------------
354 ECHOSTATUS COsSupport::GetPageBlockPhysSegment
356 PPAGE_BLOCK pPageBlock, // pass in a previously allocated block
357 DWORD dwOffset, // pass in the offset into the block
358 PHYS_ADDR &PhysAddr, // returns the physical address
359 DWORD &dwSegmentSize // and the length of the segment
363 PhysAddr = ((PHYS_ADDR)pPageBlock->phy_base + dwOffset);
365 return ECHOSTATUS_OK;
367 } // GetPageBlockPhysSegment
371 // Add additional methods here
375 // Display an error message w/title
377 void COsSupport::EchoErrorMsg(PCHAR pszMsg, PCHAR pszTitle)
381 PVOID COsSupport::operator new(size_t size)
383 PVOID pMemory;
385 pMemory = malloc(size);
387 if ( NULL == pMemory )
389 ECHO_DEBUGPRINTF(("COsSupport::operator new - memory allocation failed\n"));
391 pMemory = NULL;
393 else
395 memset( pMemory, 0, size );
398 return pMemory;
401 VOID COsSupport::operator delete(PVOID memory)
403 free(memory);