The Pedigree Project  0.1
hosted/IrqManager.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 "IrqManager.h"
21 #include "pedigree/kernel/LockGuard.h"
22 #include "pedigree/kernel/Log.h"
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/debugger/Debugger.h"
25 
26 namespace __pedigree_hosted
27 {
28 }; // namespace __pedigree_hosted
29 using namespace __pedigree_hosted;
30 
31 #include <signal.h>
32 
33 static int irqToSignal[2] = {0};
34 static int signalToIrq[NSIG] = {0};
35 
36 // TODO: Needs locking
37 
39 
41 {
42 }
43 
44 bool HostedIrqManager::control(uint8_t irq, ControlCode code, size_t argument)
45 {
46  return true;
47 }
48 
50  uint8_t irq, IrqHandler *handler, bool bEdge)
51 {
52  if (UNLIKELY(irq >= 16))
53  return 0;
54 
55  // Save the IrqHandler
56  m_Handler[irq].pushBack(handler);
57 
58  return irqToSignal[irq];
59 }
60 
61 irq_id_t
63 {
64  if (UNLIKELY(!pDevice))
65  return 0;
66  irq_id_t irq = pDevice->getInterruptNumber();
67  if (UNLIKELY(irq >= 16))
68  return 0;
69 
70  // Save the IrqHandler
71  m_Handler[irq].pushBack(handler);
72 
73  return irqToSignal[irq];
74 }
75 
77 {
78 }
79 
81 {
82  size_t irq = signalToIrq[Id];
83 
84  // Remove the handler
85  for (List<IrqHandler *>::Iterator it = m_Handler[irq].begin();
86  it != m_Handler[irq].end(); it++)
87  {
88  if (*it == handler)
89  {
90  m_Handler[irq].erase(it);
91  return;
92  }
93  }
94 }
95 
97 {
98  // Register the interrupts
100  if (IntManager.registerInterruptHandler(SIGUSR1, this) == false)
101  return false;
102  if (IntManager.registerInterruptHandler(SIGUSR2, this) == false)
103  return false;
104 
105  irqToSignal[0] = SIGUSR1;
106  irqToSignal[1] = SIGUSR2;
107 
108  signalToIrq[SIGUSR1] = 0;
109  signalToIrq[SIGUSR2] = 1;
110 
111  return true;
112 }
113 
115 {
116  for (size_t i = 0; i < 2; i++)
117  {
118  m_Handler[i].clear();
119  }
120 }
121 
122 void HostedIrqManager::interrupt(size_t interruptNumber, InterruptState &state)
123 {
124  size_t irq = signalToIrq[interruptNumber];
125 
126  // Call the irq handler, if any
127  if (LIKELY(m_Handler[irq].count() != 0))
128  {
129  for (List<IrqHandler *>::Iterator it = m_Handler[irq].begin();
130  it != m_Handler[irq].end(); it++)
131  {
132  (*it)->irq(irq, state);
133  }
134  }
135  else
136  {
137  NOTICE("HostedIrqManager: unhandled irq #" << irq << " occurred");
138  }
139 }
virtual bool registerInterruptHandler(size_t nInterruptNumber, InterruptHandler *pHandler)=0
void clear()
Definition: List.h:386
HostedIrqManager() INITIALISATION_ONLY
Handles interrupts and interrupt registrations from kernel components.
static HostedIrqManager m_Instance
virtual irq_id_t registerPciIrqHandler(IrqHandler *handler, Device *pDevice)
virtual uintptr_t getInterruptNumber()
Definition: Device.h:262
Definition: Device.h:43
virtual void interrupt(size_t interruptNumber, InterruptState &state)
bool initialise() INITIALISATION_ONLY
Definition: List.h:64
#define NOTICE(text)
Definition: Log.h:74
virtual void unregisterHandler(irq_id_t Id, IrqHandler *handler)
virtual void tick()
virtual bool control(uint8_t irq, ControlCode code, size_t argument)
List< IrqHandler * > m_Handler[2]
static InterruptManager & instance()
Iterator end()
Definition: List.h:135
virtual void acknowledgeIrq(irq_id_t Id)
virtual irq_id_t registerIsaIrqHandler(uint8_t irq, IrqHandler *handler, bool bEdge=false)