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#ifndef _IOKIT_IOHIDDeviceClass_H
25#define _IOKIT_IOHIDDeviceClass_H
26
27// FIXME
28#include <IOKit/hid/IOHIDLib.h>
29#include <IOKit/hid/IOHIDValue.h>
30#include <IOKit/hid/IOHIDElement.h>
31#include <IOKit/hid/IOHIDLibPrivate.h>
32
33#include "IOHIDIUnknown.h"
34
35#define HIDLog(fmt, args...) {}
36
37enum {
38    kHIDSetElementValuePendEvent    = 0x00010000,
39    kHIDGetElementValueForcePoll    = 0x00020000,
40    kHIDGetElementValuePreventPoll  = 0x00040000,
41    kHIDReportObsoleteCallback      = 0x00080000
42};
43
44class IOHIDQueueClass;
45class IOHIDTransactionClass;
46
47class IOHIDDeviceClass : public IOHIDIUnknown
48{
49    // friends with queue class
50    friend class IOHIDQueueClass;
51    friend class IOHIDTransactionClass;
52
53    // Disable copy constructors
54    IOHIDDeviceClass(IOHIDDeviceClass &src);
55    void operator =(IOHIDDeviceClass &src);
56
57protected:
58    typedef struct MyPrivateData {
59        io_object_t				notification;
60        IOHIDDeviceClass *		self;
61    } MyPrivateData;
62
63    IOHIDDeviceClass();
64    virtual ~IOHIDDeviceClass();
65
66    static IOCFPlugInInterface		sIOCFPlugInInterfaceV1;
67    static IOHIDDeviceDeviceInterface	sHIDDeviceInterfaceV2;
68
69    struct InterfaceMap             fHIDDevice;
70    io_service_t                    fService;
71    io_connect_t                    fConnection;
72    CFRunLoopRef                    fRunLoop;
73    IONotificationPortRef           fNotifyPort;
74	CFRunLoopSourceRef              fNotifyCFSource;
75    IONotificationPortRef           fAsyncPort;
76    CFMachPortRef                   fAsyncCFMachPort;
77    CFRunLoopSourceRef              fAsyncCFSource;
78    mach_port_t                     fDeviceValidPort;
79    bool                            fIsOpen;
80    bool                            fIsLUNZero;
81    bool                            fIsTerminated;
82    bool                            fAsyncPortSetupDone;
83
84	MyPrivateData *                 fAsyncPrivateDataRef;
85	MyPrivateData *                 fNotifyPrivateDataRef;
86
87    IOHIDCallbackFunction           fRemovalCallback;
88    void *                          fRemovalTarget;
89    void *                          fRemovalRefcon;
90
91    CFMutableSetRef                 fQueues;
92    CFMutableDictionaryRef          fElementCache;
93
94    CFMutableDictionaryRef          fProperties;
95
96    // ptr to shared memory for current values of elements
97#if !__LP64__
98    vm_address_t                    fCurrentValuesMappedMemory;
99    vm_size_t                       fCurrentValuesMappedMemorySize;
100#else
101    mach_vm_address_t                    fCurrentValuesMappedMemory;
102    mach_vm_size_t                       fCurrentValuesMappedMemorySize;
103#endif
104
105    // array of leaf elements (those that can be used in get value)
106    uint32_t                        fElementCount;
107    CFMutableDataRef                fElementData;
108    IOHIDElementStruct *            fElements;
109
110    // array of report handler elements (those that can be used in get value)
111    uint32_t                        fReportHandlerElementCount;
112    CFMutableDataRef                fReportHandlerElementData;
113    IOHIDElementStruct *            fReportHandlerElements;
114
115    IOHIDQueueClass *               fReportHandlerQueue;
116
117    IOHIDReportCallback             fInputReportCallback;
118    void *                          fInputReportRefcon;
119    uint8_t *                       fInputReportBuffer;
120    CFIndex                         fInputReportBufferSize;
121    IOOptionBits                    fInputReportOptions;
122
123    uint64_t                        fGeneration;
124
125    virtual IOReturn createSharedMemory(uint64_t generation);
126    virtual IOReturn releaseSharedMemory();
127
128    virtual Boolean isValid();
129
130    // routines to create owned classes
131    virtual HRESULT queryInterfaceQueue (CFUUIDRef uuid, void **ppv);
132    virtual HRESULT queryInterfaceTransaction (CFUUIDRef uuid, void **ppv);
133
134    IOReturn buildElements(uint32_t type, CFMutableDataRef * pDataRef, IOHIDElementStruct ** buffer, uint32_t * count );
135
136    // helper function for copyMatchingElements
137    bool getElementDictIntValue(CFDictionaryRef element, CFStringRef key, uint32_t * value);
138    void setElementDictIntValue(CFMutableDictionaryRef element, CFStringRef key,  uint32_t value);
139    void setElementDictBoolValue(CFMutableDictionaryRef  element, CFStringRef key,  bool value);
140    CFTypeRef createElement(CFDataRef data, IOHIDElementStruct * element, uint32_t index, CFTypeRef parentElement, CFMutableDictionaryRef elementCache,
141                                bool * isElementCached = NULL, IOOptionBits options = 0);
142
143    IOReturn getCurrentElementValueAndGeneration(IOHIDElementRef element, IOHIDValueRef *pEvent = 0, uint32_t * pGeneration = 0);
144
145	IOReturn finishAsyncPortSetup();
146	IOReturn finishReportHandlerQueueSetup();
147
148	virtual IOHIDQueueClass * createQueue(bool reportHandler=false);
149
150    // Call back methods
151    static void _cfmachPortCallback(CFMachPortRef cfPort, mach_msg_header_t *msg, CFIndex size, void *info);
152    static void _hidReportCallback(void *refcon, IOReturn result, uint32_t bufferSize);
153    static void _deviceNotification(void *refCon, io_service_t service, natural_t messageType, void *messageArgument );
154    static void _hidReportHandlerCallback(void * refcon, IOReturn result, void * sender);
155
156/*
157 * Routing gumf for CFPlugIn interfaces
158 */
159    static inline IOHIDDeviceClass *getThis(void *self)
160        { return (IOHIDDeviceClass *) ((InterfaceMap *) self)->obj; };
161
162    // Methods for routing the iocfplugin Interface v1r1
163    static IOReturn _probe(void *self, CFDictionaryRef propertyTable, io_service_t service, SInt32 *order);
164    static IOReturn _start(void *self, CFDictionaryRef propertyTable, io_service_t service);
165    static IOReturn _stop(void *self);	// Calls close()
166
167    // IOHIDDeviceDeviceInterface
168    static IOReturn _open(void * self, IOOptionBits options);
169    static IOReturn _close(void * self, IOOptionBits options);
170    static IOReturn _getProperty(void * self, CFStringRef key, CFTypeRef * pProperty);
171    static IOReturn _setProperty(void * self, CFStringRef key, CFTypeRef property);
172    static IOReturn _getAsyncPort(void * self, mach_port_t * port);
173    static IOReturn _getAsyncEventSource(void * self, CFTypeRef * pSource);
174    static IOReturn _copyMatchingElements(void * self, CFDictionaryRef matchingDict, CFArrayRef * pElements, IOOptionBits options);
175    static IOReturn _setInterruptReportCallback(void * self, uint8_t * report, CFIndex reportLength,
176                            IOHIDReportCallback callback, void * refcon, IOOptionBits options);
177    static IOReturn _getReport(void * self, IOHIDReportType reportType, uint32_t reportID, uint8_t * report, CFIndex * pReportLength,
178                            uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options);
179    static IOReturn _setReport(void * self, IOHIDReportType reportType, uint32_t reportID, const uint8_t * report, CFIndex reportLength,
180                            uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options);
181    static IOReturn _getElementValue(void * self, IOHIDElementRef element, IOHIDValueRef * pEvent,
182                            uint32_t timeout, IOHIDValueCallback callback, void * refcon, IOOptionBits options);
183    static IOReturn _setElementValue(void * self, IOHIDElementRef element, IOHIDValueRef event,
184                            uint32_t timeout, IOHIDValueCallback callback, void * refcon, IOOptionBits options);
185
186public:
187    void * getInterfaceMap () { return &fHIDDevice; };
188
189    // add/remove a queue
190    HRESULT attachQueue (IOHIDQueueClass * iohidQueue, bool reportHandler = false);
191    HRESULT detachQueue (IOHIDQueueClass * iohidQueue);
192
193    // add/remove a queue
194    HRESULT attachTransaction (IOHIDTransactionClass * transaction);
195    HRESULT detachTransaction (IOHIDTransactionClass * transaction);
196
197
198    // get an element info
199    bool getElementStructPtr(IOHIDElementCookie elementCookie, IOHIDElementStruct ** ppElementStruct, uint32_t * pIndex=0, CFDataRef * pData =0);
200    bool getElementStruct(IOHIDElementCookie elementCookie, IOHIDElementStruct * pElementStruct);
201    uint32_t getElementByteSize (IOHIDElementCookie elementCookie);
202    IOHIDElementRef getElement(IOHIDElementCookie elementCookie);
203
204    // IOCFPlugin stuff
205    static IOCFPlugInInterface **alloc();
206
207    virtual HRESULT queryInterface(REFIID iid, void **ppv);
208
209    virtual IOReturn probe(CFDictionaryRef propertyTable, io_service_t service, SInt32 *order);
210    virtual IOReturn start(CFDictionaryRef propertyTable, io_service_t service);
211
212    virtual IOReturn getProperty(CFStringRef key, CFTypeRef * pProperty);
213    virtual IOReturn setProperty(CFStringRef key, CFTypeRef property);
214
215    virtual IOReturn getAsyncEventSource(CFTypeRef *source);
216    virtual IOReturn getAsyncPort(mach_port_t *port);
217
218    virtual IOReturn open(IOOptionBits options = 0);
219    virtual IOReturn close(IOOptionBits options = 0);
220
221    virtual IOReturn startAllQueues();
222    virtual IOReturn stopAllQueues();
223
224    virtual IOReturn setReport(IOHIDReportType reportType, uint32_t reportID, const uint8_t * report, CFIndex reportLength,
225                                uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options = 0);
226    virtual IOReturn getReport(IOHIDReportType reportType, uint32_t reportID, uint8_t * report, CFIndex * pReportLength,
227                                uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options = 0);
228    virtual IOReturn copyMatchingElements(CFDictionaryRef matchingDict, CFArrayRef * elements, CFTypeRef parentElement=0, CFMutableDictionaryRef elementCache=0, IOOptionBits options=0);
229    virtual IOReturn setInterruptReportCallback(uint8_t * report, CFIndex reportLength, IOHIDReportCallback callback, void * refcon, IOOptionBits options = 0);
230
231    virtual IOReturn getElementValue(IOHIDElementRef element,
232                                     IOHIDValueRef * pEvent,
233                                     uint32_t timeout = 0,
234                                     IOHIDValueCallback callback = 0,
235                                     void * refcon = 0,
236                                     IOOptionBits options = 0);
237
238    virtual IOReturn setElementValue(IOHIDElementRef element,
239                                     IOHIDValueRef event,
240                                     uint32_t timeout = 0,
241                                     IOHIDValueCallback callback = 0,
242                                     void * refcon = 0,
243                                     IOOptionBits options = 0);
244};
245
246
247class IOHIDObsoleteDeviceClass : public IOHIDDeviceClass
248{
249    // friends with queue class
250    friend class IOHIDObsoleteQueueClass;
251    friend class IOHIDOutputTransactionClass;
252
253    // Disable copy constructors
254    IOHIDObsoleteDeviceClass(IOHIDObsoleteDeviceClass &src);
255    void operator =(IOHIDObsoleteDeviceClass &src);
256
257    void * fInputReportContext;
258
259    static void _reportCallback(
260                                        void *                  context,
261                                        IOReturn                result,
262                                        void *                  sender,
263                                        IOHIDReportType         type,
264                                        uint32_t                reportID,
265                                        uint8_t *               report,
266                                        CFIndex                 reportLength);
267
268protected:
269
270    static IOHIDDeviceInterface122	sHIDDeviceInterfaceV122;
271
272    // routines to create owned classes
273    virtual HRESULT queryInterfaceTransaction (CFUUIDRef uuid, void **ppv);
274	virtual IOHIDQueueClass * createQueue(bool reportHandler=false);
275
276    static inline IOHIDObsoleteDeviceClass *getThis(void *self) { return (IOHIDObsoleteDeviceClass *) ((InterfaceMap *) self)->obj; };
277
278    // Methods for routing asynchronous completion plumbing.
279    static IOReturn             _createAsyncEventSource(void * self, CFRunLoopSourceRef * pSource);
280    static CFRunLoopSourceRef   _getAsyncEventSource(void *self);
281    static mach_port_t          _getAsyncPort(void *self);
282    static IOReturn             _close(void *self);
283    static IOReturn             _setRemovalCallback(void * self, IOHIDCallbackFunction callback, void * target, void * refcon);
284    static IOReturn             _getElementValue(void * self, IOHIDElementCookie elementCookie, IOHIDEventStruct * valueEvent);
285    static IOReturn             _setElementValue(void * self, IOHIDElementCookie cookie, IOHIDEventStruct * pEvent, uint32_t timeout, IOHIDElementCallbackFunction callback, void * target,  void * refcon);
286    static IOReturn             _queryElementValue(void * self, IOHIDElementCookie cookie, IOHIDEventStruct * pEvent, uint32_t timeout, IOHIDElementCallbackFunction callback, void * target, void * refcon);
287    static IOReturn             _startAllQueues(void * self);
288    static IOReturn             _stopAllQueues(void * self);
289    static IOHIDQueueInterface **               _allocQueue(void *self);
290    static IOHIDOutputTransactionInterface **   _allocOutputTransaction (void *self);
291    static IOReturn             _setReport (void * self, IOHIDReportType type, uint32_t id, void * report, uint32_t length, uint32_t timeout, IOHIDReportCallbackFunction callback, void * target, void * refcon);
292    static IOReturn             _getReport (void * self, IOHIDReportType type, uint32_t id, void * report, uint32_t * pLength, uint32_t timeout, IOHIDReportCallbackFunction callback, void * target, void * refcon);
293    static IOReturn             _copyMatchingElements(void * self, CFDictionaryRef matchingDict, CFArrayRef * elements);
294    static IOReturn             _setInterruptReportHandlerCallback(void * self, void * report, uint32_t length, IOHIDReportCallbackFunction callback, void * target, void * refcon);
295
296
297    static void                 _elementValueCallback(void * context, IOReturn result, void * sender, IOHIDValueRef value);
298
299public:
300    IOHIDObsoleteDeviceClass();
301    static IOCFPlugInInterface **alloc();
302    virtual HRESULT queryInterface(REFIID iid, void **ppv);
303
304    virtual IOReturn createAsyncEventSource(CFRunLoopSourceRef * pSource);
305    virtual IOReturn setRemovalCallback(IOHIDCallbackFunction removalCallback, void * removalTarget, void * removalRefcon);
306    virtual IOReturn setElementValue(IOHIDElementCookie cookie,
307                                     IOHIDEventStruct * pEvent,
308                                     uint32_t timeout = 0,
309                                     IOHIDElementCallbackFunction callback = NULL,
310                                     void * target = NULL,
311                                     void * refcon = NULL,
312                                     IOOptionBits options = 0);
313    virtual IOReturn setElementValue(IOHIDElementRef element,
314                                     IOHIDValueRef event,
315                                     uint32_t timeout = 0,
316                                     IOHIDValueCallback callback = 0,
317                                     void * refcon = 0,
318                                     IOOptionBits options = 0);
319    virtual IOReturn getElementValue(IOHIDElementCookie cookie,
320                                     IOHIDEventStruct * pEvent);
321    virtual IOReturn getElementValue(IOHIDElementRef element,
322                                     IOHIDValueRef * pEvent,
323                                     uint32_t timeout = 0,
324                                     IOHIDValueCallback callback = 0,
325                                     void * refcon = 0,
326                                     IOOptionBits options = 0);
327    virtual IOReturn queryElementValue(IOHIDElementCookie cookie, IOHIDEventStruct * pEvent, uint32_t timeout, IOHIDElementCallbackFunction callback, void * target, void * refcon);
328    virtual IOReturn setReport(IOHIDReportType type,
329                               uint32_t id,
330                               void * report,
331                               uint32_t length,
332                               uint32_t timeout,
333                               IOHIDReportCallbackFunction callback,
334                               void * target,
335                               void * refcon);
336    virtual IOReturn setReport(IOHIDReportType reportType,
337                               uint32_t reportID,
338                               const uint8_t * report,
339                               CFIndex reportLength,
340                               uint32_t timeout,
341                               IOHIDReportCallback callback,
342                               void * refcon,
343                               IOOptionBits options = 0);
344    virtual IOReturn getReport(IOHIDReportType type,
345                               uint32_t id,
346                               void * report,
347                               uint32_t * pLength,
348                               uint32_t timeout,
349                               IOHIDReportCallbackFunction callback,
350                               void * target,
351                               void * refcon);
352    virtual IOReturn getReport(IOHIDReportType reportType,
353                               uint32_t reportID,
354                               uint8_t * report,
355                               CFIndex * pReportLength,
356                               uint32_t timeout,
357                               IOHIDReportCallback callback,
358                               void * refcon,
359                               IOOptionBits options = 0);
360    virtual IOReturn setInterruptReportHandlerCallback(void * report, uint32_t length, IOHIDReportCallbackFunction callback, void * target, void * refcon);
361
362    virtual IOHIDQueueInterface ** allocQueue();
363    virtual IOHIDOutputTransactionInterface ** allocOutputTransaction();
364};
365
366
367#endif /* !_IOKIT_IOHIDDeviceClass_H */
368