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
32#if TARGET_OS_EMBEDDED
33    #include "IOHIDEventServiceClass.h"
34#else
35    #include "IOHIDUPSClass.h"
36#endif
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#else
50    else if (CFEqual(typeID, kIOUPSPlugInTypeID))
51        return (void *) IOHIDUPSClass::alloc();
52#endif
53    else
54        return NULL;
55}
56
57void IOHIDIUnknown::factoryAddRef()
58{
59    if (0 == factoryRefCount++) {
60        CFUUIDRef factoryId = kIOHIDDeviceFactoryID;
61
62        CFRetain(factoryId);
63        CFPlugInAddInstanceForFactory(factoryId);
64    }
65}
66
67void IOHIDIUnknown::factoryRelease()
68{
69    if (1 == factoryRefCount--) {
70        CFUUIDRef factoryId = kIOHIDDeviceFactoryID;
71
72        CFPlugInRemoveInstanceForFactory(factoryId);
73        CFRelease(factoryId);
74    }
75    else if (factoryRefCount < 0)
76        factoryRefCount = 0;
77}
78
79IOHIDIUnknown::IOHIDIUnknown(void *unknownVTable)
80: refCount(1)
81{
82    iunknown.pseudoVTable = (IUnknownVTbl *) unknownVTable;
83    iunknown.obj = this;
84
85    factoryAddRef();
86};
87
88IOHIDIUnknown::~IOHIDIUnknown()
89{
90    factoryRelease();
91}
92
93UInt32 IOHIDIUnknown::addRef()
94{
95    refCount += 1;
96    return refCount;
97}
98
99UInt32 IOHIDIUnknown::release()
100{
101    UInt32 retVal = refCount - 1;
102
103    if (retVal > 0)
104        refCount = retVal;
105    else if (retVal == 0) {
106        refCount = retVal;
107        delete this;
108    }
109    else
110        retVal = 0;
111
112    return retVal;
113}
114
115HRESULT IOHIDIUnknown::
116genericQueryInterface(void *self, REFIID iid, void **ppv)
117{
118    IOHIDIUnknown *me = ((InterfaceMap *) self)->obj;
119    return me->queryInterface(iid, ppv);
120}
121
122UInt32 IOHIDIUnknown::genericAddRef(void *self)
123{
124    IOHIDIUnknown *me = ((InterfaceMap *) self)->obj;
125    return me->addRef();
126}
127
128UInt32 IOHIDIUnknown::genericRelease(void *self)
129{
130    IOHIDIUnknown *me = ((InterfaceMap *) self)->obj;
131    return me->release();
132}
133