The Pedigree Project  0.1
IoPortManager.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/processor/IoPortManager.h"
21 #include "pedigree/kernel/LockGuard.h"
22 #include "pedigree/kernel/processor/IoPort.h"
23 #include "pedigree/kernel/processor/Processor.h"
24 #include "pedigree/kernel/utilities/new"
25 
26 #if !defined(KERNEL_PROCESSOR_NO_PORT_IO)
27 
29 
30 bool IoPortManager::allocate(IoPort *Port, io_port_t ioPort, size_t size)
31 {
32 #if defined(ADDITIONAL_CHECKS)
33  if (Processor::isInitialised() == 0)
35 #endif
36 
37  // Acquire the lock untill the end of the function
39 
40  // Remove the I/O ports from the list of free I/O ports
41  if (m_FreeIoPorts.allocateSpecific(ioPort, size) == false)
42  return false;
43 
44  // Add information to the list of used I/O ports
45  m_UsedIoPorts.pushBack(Port);
46  return true;
47 }
48 
50 {
51 #if defined(ADDITIONAL_CHECKS)
52  if (Processor::isInitialised() == 0)
54 #endif
55 
56  // Acquire the lock untill the end of the function
58 
59  // Remove from the used I/O ports list
62  for (; i != end; i++)
63  if ((*i) == Port)
64  {
66  break;
67  }
68 
69  // Add to the free I/O ports list
70  m_FreeIoPorts.free(Port->base(), Port->size());
71 }
72 
74 {
75  // Acquire the lock untill the end of the function
77 
78  for (size_t i = 0; i < m_UsedIoPorts.count(); i++)
79  {
80  IoPortInfo *pIoPortInfo = new IoPortInfo(
81  m_UsedIoPorts[i]->base(), m_UsedIoPorts[i]->size(),
82  m_UsedIoPorts[i]->name());
83  IoPorts.pushBack(pIoPortInfo);
84  }
85 }
86 
88 {
89  while (IoPorts.count() != 0)
90  {
91  IoPortInfo *pIoPortInfo = IoPorts.popBack();
92  delete pIoPortInfo;
93  }
94 }
95 
96 //
97 // Functions only usable in the kernel initialisation phase
98 //
99 
100 void IoPortManager::initialise(io_port_t ioPortBase, size_t size)
101 {
102  m_FreeIoPorts.free(ioPortBase, size);
103 }
104 
106 {
107 }
109 {
110 }
111 
112 #endif
void pushBack(const T &value)
Definition: Vector.h:270
T popBack()
Definition: Vector.h:286
Iterator begin()
Definition: Vector.h:148
Iterator end()
Definition: Vector.h:160
size_t count() const
Definition: Vector.h:264
io_port_t base() const
Definition: IoPort.cc:65
Vector< IoPort * > m_UsedIoPorts
static size_t isInitialised()
Definition: Processor.cc:34
A vector / dynamic array.
void free(IoPort *Port)
Spinlock m_Lock
void free(T address, T length, bool merge=true)
Definition: RangeList.h:163
virtual ~IoPortManager()
bool allocate(IoPort *Port, io_port_t ioPort, size_t size)
Manages hardware I/O port (de)allocations.
Definition: IoPortManager.h:38
static IoPortManager m_Instance
void initialise(io_port_t ioPortBase, size_t size) INITIALISATION_ONLY
I/O port range.
Definition: IoPort.h:34
bool allocateSpecific(T address, T length)
Definition: RangeList.h:280
void allocateIoPortList(Vector< IoPortInfo * > &IoPorts)
static void halt()
IoPortManager() INITIALISATION_ONLY
virtual size_t size() const
Definition: IoPort.cc:60
void erase(size_t index)
Definition: Vector.h:350
RangeList< uint32_t > m_FreeIoPorts
void freeIoPortList(Vector< IoPortInfo * > &IoPorts)