The Pedigree Project 0.1
UsbPnP.h
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#ifndef USBPNP_H
21#define USBPNP_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/Spinlock.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/process/Mutex.h"
26#include "pedigree/kernel/process/OperationBarrier.h"
27#include "pedigree/kernel/processor/types.h"
28#include "pedigree/kernel/utilities/new"
29
30#include <config.h>
31
32class Device;
33class UsbDevice;
35class UsbHub;
36
37enum UsbPnPConstants {
38 VendorIdNone = 0xFFFF,
39 ProductIdNone = 0xFFFF,
40 ClassNone = 0xFF,
41 SubclassNone = 0xFF,
42 ProtocolNone = 0xFF,
43};
44
45class EXPORTED_PUBLIC UsbPnP {
46 private:
47 struct CallbackItem;
48
50 typedef UsbDevice* (*callback_t)(UsbDevice*);
51
52 public:
64 class EXPORTED_PUBLIC Registration {
65 public:
69
70 Registration& operator=(Registration&& other);
71
72 bool reset();
73
74 explicit operator bool() const {
75 return static_cast<CallbackItem*>(m_Item) != nullptr;
76 }
77
78 private:
79 friend class UsbPnP;
80
81 void adopt(UsbPnP* owner, CallbackItem* item);
82
83 Registration(const Registration&) = delete;
84 Registration& operator=(const Registration&) = delete;
85
86 Atomic<UsbPnP*> m_Owner;
88 Atomic<bool> m_Resetting;
89 };
90
91 UsbPnP();
92 virtual ~UsbPnP();
93
95 static UsbPnP& instance() {
96 return m_Instance;
97 }
98
100 MUST_USE_RESULT bool registerCallback(uint16_t nVendorId, uint16_t nProductId,
101 callback_t callback, Registration& registration);
102
104 MUST_USE_RESULT bool registerCallback(uint8_t nClass, uint8_t nSubclass, uint8_t nProtocol,
105 callback_t callback, Registration& registration);
106
108 bool probeDevice(Device* pDeviceBase);
109
110#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
111 static bool runHostedRegistrationRegression();
112#endif
113#if PEDIGREE_CONCURRENCY_SMOKE_TESTS
114 static bool runQemuRegistrationRegression();
115#endif
116#if (HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS) || PEDIGREE_CONCURRENCY_SMOKE_TESTS
117 bool invokeCallbackForTest(size_t callbackIndex = 0);
118 size_t callbackCountForTest();
119 bool callbackStorageEmptyForTest();
120#endif
121
122 private:
123 friend class UsbHub;
124 friend class UsbDeviceContainer;
125
126 struct ActiveInvocation;
127
128 bool probeDeviceAdmitted(UsbDeviceContainer* container, OperationBarrier::Lease& lease);
129
132 Device* doProbe(Device* pDeviceBase);
133
134 bool registerCallbackItem(CallbackItem* item, Registration& registration, bool reprobe);
135 bool unregisterCallback(CallbackItem* item);
136 void retireBindings(CallbackItem* item, bool reprobe);
137 void publishBinding(CallbackItem* item, UsbDeviceContainer* container,
138 OperationBarrier::Lease& binding);
139 void detachBinding(UsbDeviceContainer* container);
140 void unlinkBindingLocked(UsbDeviceContainer* container);
141 bool acquireCallback(UsbDevice* device, size_t afterSequence, CallbackItem*& item,
142 callback_t& callback, size_t& sequence, ActiveInvocation& invocation);
143 void finishCallback(CallbackItem* item, ActiveInvocation& invocation);
144 static void* currentInvocationOwner();
145 bool isCallbackContext(void* owner) const;
146 bool inCurrentCallbackContext();
147#if (HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS) || PEDIGREE_CONCURRENCY_SMOKE_TESTS
148 static bool runRegistrationRegression();
149#endif
150
153
155 void reprobeDevices(Device* pParent);
156
157 CallbackItem* m_FirstCallback;
158 CallbackItem* m_LastCallback;
159 size_t m_CallbackCount;
160 Spinlock m_CallbackLock;
161 Mutex m_BindingLock;
162 size_t m_NextCallbackSequence;
163 ActiveInvocation* m_ActiveInvocations;
164};
165
166#endif
Definition Mutex.h:56
static UsbPnP m_Instance
Static instance.
Definition UsbPnP.h:152
static UsbPnP & instance()
Singleton design.
Definition UsbPnP.h:95