1/*
2 *
3 * @APPLE_LICENSE_HEADER_START@
4 *
5 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25#include <TargetConditionals.h>
26
27#include <IOKit/hid/IOHIDDevicePlugIn.h>
28#include <IOKit/hid/IOHIDServicePlugIn.h>
29#include "IOHIDIUnknown.h"
30#include "IOHIDDeviceClass.h"
31#include "IOHIDUPSClass.h"
32
33#if TARGET_OS_EMBEDDED
34    #include "IOHIDEventServiceClass.h"
35#endif
36
37
38int IOHIDIUnknown::factoryRefCount = 0;
39
40void *IOHIDLibFactory(CFAllocatorRef allocator __unused, CFUUIDRef typeID)
41{
42    if (CFEqual(typeID, kIOHIDDeviceUserClientTypeID))
43        return (void *) IOHIDObsoleteDeviceClass::alloc();
44    else if (CFEqual(typeID, kIOHIDDeviceTypeID))
45        return (void *) IOHIDDeviceClass::alloc();
46#if TARGET_OS_EMBEDDED
47    else if (CFEqual(typeID, kIOHIDServicePlugInTypeID))
48        return (void *) IOHIDEventServiceClass::alloc();
49#endif
50    else if (CFEqual(typeID, kIOUPSPlugInTypeID))
51        return (void *) IOHIDUPSClass::alloc();
52    else
53        return NULL;
54}
55
56void IOHIDIUnknown::factoryAddRef()
57{
58    if (0 == factoryRefCount++) {
59        CFUUIDRef factoryId = kIOHIDDeviceFactoryID;
60
61        CFRetain(factoryId);
62        CFPlugInAddInstanceForFactory(factoryId);
63    }
64}
65
66void IOHIDIUnknown::factoryRelease()
67{
68    if (1 == factoryRefCount--) {
69        CFUUIDRef factoryId = kIOHIDDeviceFactoryID;
70
71        CFPlugInRemoveInstanceForFactory(factoryId);
72        CFRelease(factoryId);
73    }
74    else if (factoryRefCount < 0)
75        factoryRefCount = 0;
76}
77
78IOHIDIUnknown::IOHIDIUnknown(void *unknownVTable)
79: refCount(1)
80{
81    iunknown.pseudoVTable = (IUnknownVTbl *) unknownVTable;
82    iunknown.obj = this;
83
84    factoryAddRef();
85};
86
87IOHIDIUnknown::~IOHIDIUnknown()
88{
89    factoryRelease();
90}
91
92UInt32 IOHIDIUnknown::addRef()
93{
94    refCount += 1;
95    return refCount;
96}
97
98UInt32 IOHIDIUnknown::release()
99{
100    UInt32 retVal = refCount - 1;
101
102    if (retVal > 0)
103        refCount = retVal;
104    else if (retVal == 0) {
105        refCount = retVal;
106        delete this;
107    }
108    else
109        retVal = 0;
110
111    return retVal;
112}
113
114HRESULT IOHIDIUnknown::
115genericQueryInterface(void *self, REFIID iid, void **ppv)
116{
117    IOHIDIUnknown *me = ((InterfaceMap *) self)->obj;
118    return me->queryInterface(iid, ppv);
119}
120
121UInt32 IOHIDIUnknown::genericAddRef(void *self)
122{
123    IOHIDIUnknown *me = ((InterfaceMap *) self)->obj;
124    return me->addRef();
125}
126
127UInt32 IOHIDIUnknown::genericRelease(void *self)
128{
129    IOHIDIUnknown *me = ((InterfaceMap *) self)->obj;
130    return me->release();
131}
132