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 IOHIDDeviceTimeStampedDeviceInterface	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    IOHIDReportWithTimeStampCallback    fInputReportWithTimeStampCallback;
119    void *                              fInputReportRefcon;
120    uint8_t *                           fInputReportBuffer;
121    CFIndex                             fInputReportBufferSize;
122    IOOptionBits                        fInputReportOptions;
123
124    uint64_t                        fGeneration;
125
126    virtual IOReturn createSharedMemory(uint64_t generation);
127    virtual IOReturn releaseSharedMemory();
128
129    virtual Boolean isValid();
130
131    // routines to create owned classes
132    virtual HRESULT queryInterfaceQueue (CFUUIDRef uuid, void **ppv);
133    virtual HRESULT queryInterfaceTransaction (CFUUIDRef uuid, void **ppv);
134
135    IOReturn buildElements(uint32_t type, CFMutableDataRef * pDataRef, IOHIDElementStruct ** buffer, uint32_t * count );
136
137    // helper function for copyMatchingElements
138    bool getElementDictIntValue(CFDictionaryRef element, CFStringRef key, uint32_t * value);
139    void setElementDictIntValue(CFMutableDictionaryRef element, CFStringRef key,  uint32_t value);
140    void setElementDictBoolValue(CFMutableDictionaryRef  element, CFStringRef key,  bool value);
141    CFTypeRef createElement(CFDataRef data, IOHIDElementStruct * element, uint32_t index, CFTypeRef parentElement, CFMutableDictionaryRef elementCache,
142                                bool * isElementCached = NULL, IOOptionBits options = 0);
143
144    IOReturn getCurrentElementValueAndGeneration(IOHIDElementRef element, IOHIDValueRef *pEvent = 0, uint32_t * pGeneration = 0);
145
146	IOReturn finishAsyncPortSetup();
147	IOReturn finishReportHandlerQueueSetup();
148
149	virtual IOHIDQueueClass * createQueue(bool reportHandler=false);
150
151    // Call back methods
152    static void _cfmachPortCallback(CFMachPortRef cfPort, mach_msg_header_t *msg, CFIndex size, void *info);
153    static void _hidReportCallback(void *refcon, IOReturn result, uint32_t bufferSize);
154    static void _deviceNotification(void *refCon, io_service_t service, natural_t messageType, void *messageArgument );
155    static void _hidReportHandlerCallback(void * refcon, IOReturn result, void * sender);
156
157/*
158 * Routing gumf for CFPlugIn interfaces
159 */
160    static inline IOHIDDeviceClass *getThis(void *self)
161        { return (IOHIDDeviceClass *) ((InterfaceMap *) self)->obj; };
162
163    // Methods for routing the iocfplugin Interface v1r1
164    static IOReturn _probe(void *self, CFDictionaryRef propertyTable, io_service_t service, SInt32 *order);
165    static IOReturn _start(void *self, CFDictionaryRef propertyTable, io_service_t service);
166    static IOReturn _stop(void *self);	// Calls close()
167
168    // IOHIDDeviceDeviceInterface
169    static IOReturn _open(void * self, IOOptionBits options);
170    static IOReturn _close(void * self, IOOptionBits options);
171    static IOReturn _getProperty(void * self, CFStringRef key, CFTypeRef * pProperty);
172    static IOReturn _setProperty(void * self, CFStringRef key, CFTypeRef property);
173    static IOReturn _getAsyncPort(void * self, mach_port_t * port);
174    static IOReturn _getAsyncEventSource(void * self, CFTypeRef * pSource);
175    static IOReturn _copyMatchingElements(void * self, CFDictionaryRef matchingDict, CFArrayRef * pElements, IOOptionBits options);
176    static IOReturn _setInterruptReportCallback(void * self, uint8_t * report, CFIndex reportLength,
177                            IOHIDReportCallback callback, void * refcon, IOOptionBits options);
178    static IOReturn _setInterruptReportWithTimeStampCallback(void * self, uint8_t * report,
179                            CFIndex reportLength, IOHIDReportWithTimeStampCallback callback,
180                            void * refcon, IOOptionBits options);
181    static IOReturn _getReport(void * self, IOHIDReportType reportType, uint32_t reportID, uint8_t * report, CFIndex * pReportLength,
182                            uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options);
183    static IOReturn _setReport(void * self, IOHIDReportType reportType, uint32_t reportID, const uint8_t * report, CFIndex reportLength,
184                            uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options);
185    static IOReturn _getElementValue(void * self, IOHIDElementRef element, IOHIDValueRef * pEvent,
186                            uint32_t timeout, IOHIDValueCallback callback, void * refcon, IOOptionBits options);
187    static IOReturn _setElementValue(void * self, IOHIDElementRef element, IOHIDValueRef event,
188                            uint32_t timeout, IOHIDValueCallback callback, void * refcon, IOOptionBits options);
189
190public:
191    void * getInterfaceMap () { return &fHIDDevice; };
192
193    // add/remove a queue
194    HRESULT attachQueue (IOHIDQueueClass * iohidQueue, bool reportHandler = false);
195    HRESULT detachQueue (IOHIDQueueClass * iohidQueue);
196
197    // add/remove a queue
198    HRESULT attachTransaction (IOHIDTransactionClass * transaction);
199    HRESULT detachTransaction (IOHIDTransactionClass * transaction);
200
201
202    // get an element info
203    bool getElementStructPtr(IOHIDElementCookie elementCookie, IOHIDElementStruct ** ppElementStruct, uint32_t * pIndex=0, CFDataRef * pData =0);
204    bool getElementStruct(IOHIDElementCookie elementCookie, IOHIDElementStruct * pElementStruct);
205    uint32_t getElementByteSize (IOHIDElementCookie elementCookie);
206    IOHIDElementRef getElement(IOHIDElementCookie elementCookie);
207
208    // IOCFPlugin stuff
209    static IOCFPlugInInterface **alloc();
210
211    virtual HRESULT queryInterface(REFIID iid, void **ppv);
212
213    virtual IOReturn probe(CFDictionaryRef propertyTable, io_service_t service, SInt32 *order);
214    virtual IOReturn start(CFDictionaryRef propertyTable, io_service_t service);
215
216    virtual IOReturn getProperty(CFStringRef key, CFTypeRef * pProperty);
217    virtual IOReturn setProperty(CFStringRef key, CFTypeRef property);
218
219    virtual IOReturn getAsyncEventSource(CFTypeRef *source);
220    virtual IOReturn getAsyncPort(mach_port_t *port);
221
222    virtual IOReturn open(IOOptionBits options = 0);
223    virtual IOReturn close(IOOptionBits options = 0);
224
225    virtual IOReturn startAllQueues();
226    virtual IOReturn stopAllQueues();
227
228    virtual IOReturn setReport(IOHIDReportType reportType, uint32_t reportID, const uint8_t * report, CFIndex reportLength,
229                                uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options = 0);
230    virtual IOReturn getReport(IOHIDReportType reportType, uint32_t reportID, uint8_t * report, CFIndex * pReportLength,
231                                uint32_t timeout, IOHIDReportCallback callback, void * refcon, IOOptionBits options = 0);
232    virtual IOReturn copyMatchingElements(CFDictionaryRef matchingDict, CFArrayRef * elements, CFTypeRef parentElement=0, CFMutableDictionaryRef elementCache=0, IOOptionBits options=0);
233    virtual IOReturn setInterruptReportCallback(uint8_t * report, CFIndex reportLength, IOHIDReportCallback callback, IOHIDReportWithTimeStampCallback callbackWithTimeStamp, void * refcon, IOOptionBits options = 0);
234
235    virtual IOReturn getElementValue(IOHIDElementRef element,
236                                     IOHIDValueRef * pEvent,
237                                     uint32_t timeout = 0,
238                                     IOHIDValueCallback callback = 0,
239                                     void * refcon = 0,
240                                     IOOptionBits options = 0);
241
242    virtual IOReturn setElementValue(IOHIDElementRef element,
243                                     IOHIDValueRef event,
244                                     uint32_t timeout = 0,
245                                     IOHIDValueCallback callback = 0,
246                                     void * refcon = 0,
247                                     IOOptionBits options = 0);
248};
249
250
251class IOHIDObsoleteDeviceClass : public IOHIDDeviceClass
252{
253    // friends with queue class
254    friend class IOHIDObsoleteQueueClass;
255    friend class IOHIDOutputTransactionClass;
256
257    // Disable copy constructors
258    IOHIDObsoleteDeviceClass(IOHIDObsoleteDeviceClass &src);
259    void operator =(IOHIDObsoleteDeviceClass &src);
260
261    void * fInputReportContext;
262
263    static void _reportCallback(
264                                        void *                  context,
265                                        IOReturn                result,
266                                        void *                  sender,
267                                        IOHIDReportType         type,
268                                        uint32_t                reportID,
269                                        uint8_t *               report,
270                                        CFIndex                 reportLength);
271
272protected:
273
274    static IOHIDDeviceInterface122	sHIDDeviceInterfaceV122;
275
276    // routines to create owned classes
277    virtual HRESULT queryInterfaceTransaction (CFUUIDRef uuid, void **ppv);
278	virtual IOHIDQueueClass * createQueue(bool reportHandler=false);
279
280    static inline IOHIDObsoleteDeviceClass *getThis(void *self) { return (IOHIDObsoleteDeviceClass *) ((InterfaceMap *) self)->obj; };
281
282    // Methods for routing asynchronous completion plumbing.
283    static IOReturn             _createAsyncEventSource(void * self, CFRunLoopSourceRef * pSource);
284    static CFRunLoopSourceRef   _getAsyncEventSource(void *self);
285    static mach_port_t          _getAsyncPort(void *self);
286    static IOReturn             _close(void *self);
287    static IOReturn             _setRemovalCallback(void * self, IOHIDCallbackFunction callback, void * target, void * refcon);
288    static IOReturn             _getElementValue(void * self, IOHIDElementCookie elementCookie, IOHIDEventStruct * valueEvent);
289    static IOReturn             _setElementValue(void * self, IOHIDElementCookie cookie, IOHIDEventStruct * pEvent, uint32_t timeout, IOHIDElementCallbackFunction callback, void * target,  void * refcon);
290    static IOReturn             _queryElementValue(void * self, IOHIDElementCookie cookie, IOHIDEventStruct * pEvent, uint32_t timeout, IOHIDElementCallbackFunction callback, void * target, void * refcon);
291    static IOReturn             _startAllQueues(void * self);
292    static IOReturn             _stopAllQueues(void * self);
293    static IOHIDQueueInterface **               _allocQueue(void *self);
294    static IOHIDOutputTransactionInterface **   _allocOutputTransaction (void *self);
295    static IOReturn             _setReport (void * self, IOHIDReportType type, uint32_t id, void * report, uint32_t length, uint32_t timeout, IOHIDReportCallbackFunction callback, void * target, void * refcon);
296    static IOReturn             _getReport (void * self, IOHIDReportType type, uint32_t id, void * report, uint32_t * pLength, uint32_t timeout, IOHIDReportCallbackFunction callback, void * target, void * refcon);
297    static IOReturn             _copyMatchingElements(void * self, CFDictionaryRef matchingDict, CFArrayRef * elements);
298    static IOReturn             _setInterruptReportHandlerCallback(void * self, void * report, uint32_t length, IOHIDReportCallbackFunction callback, void * target, void * refcon);
299
300
301    static void                 _elementValueCallback(void * context, IOReturn result, void * sender, IOHIDValueRef value);
302
303public:
304    IOHIDObsoleteDeviceClass();
305    static IOCFPlugInInterface **alloc();
306    virtual HRESULT queryInterface(REFIID iid, void **ppv);
307
308    virtual IOReturn createAsyncEventSource(CFRunLoopSourceRef * pSource);
309    virtual IOReturn setRemovalCallback(IOHIDCallbackFunction removalCallback, void * removalTarget, void * removalRefcon);
310    virtual IOReturn setElementValue(IOHIDElementCookie cookie,
311                                     IOHIDEventStruct * pEvent,
312                                     uint32_t timeout = 0,
313                                     IOHIDElementCallbackFunction callback = NULL,
314                                     void * target = NULL,
315                                     void * refcon = NULL,
316                                     IOOptionBits options = 0);
317    virtual IOReturn setElementValue(IOHIDElementRef element,
318                                     IOHIDValueRef event,
319                                     uint32_t timeout = 0,
320                                     IOHIDValueCallback callback = 0,
321                                     void * refcon = 0,
322                                     IOOptionBits options = 0);
323    virtual IOReturn getElementValue(IOHIDElementCookie cookie,
324                                     IOHIDEventStruct * pEvent);
325    virtual IOReturn getElementValue(IOHIDElementRef element,
326                                     IOHIDValueRef * pEvent,
327                                     uint32_t timeout = 0,
328                                     IOHIDValueCallback callback = 0,
329                                     void * refcon = 0,
330                                     IOOptionBits options = 0);
331    virtual IOReturn queryElementValue(IOHIDElementCookie cookie, IOHIDEventStruct * pEvent, uint32_t timeout, IOHIDElementCallbackFunction callback, void * target, void * refcon);
332    virtual IOReturn setReport(IOHIDReportType type,
333                               uint32_t id,
334                               void * report,
335                               uint32_t length,
336                               uint32_t timeout,
337                               IOHIDReportCallbackFunction callback,
338                               void * target,
339                               void * refcon);
340    virtual IOReturn setReport(IOHIDReportType reportType,
341                               uint32_t reportID,
342                               const uint8_t * report,
343                               CFIndex reportLength,
344                               uint32_t timeout,
345                               IOHIDReportCallback callback,
346                               void * refcon,
347                               IOOptionBits options = 0);
348    virtual IOReturn getReport(IOHIDReportType type,
349                               uint32_t id,
350                               void * report,
351                               uint32_t * pLength,
352                               uint32_t timeout,
353                               IOHIDReportCallbackFunction callback,
354                               void * target,
355                               void * refcon);
356    virtual IOReturn getReport(IOHIDReportType reportType,
357                               uint32_t reportID,
358                               uint8_t * report,
359                               CFIndex * pReportLength,
360                               uint32_t timeout,
361                               IOHIDReportCallback callback,
362                               void * refcon,
363                               IOOptionBits options = 0);
364    virtual IOReturn setInterruptReportHandlerCallback(void * report, uint32_t length, IOHIDReportCallbackFunction callback, void * target, void * refcon);
365
366    virtual IOHIDQueueInterface ** allocQueue();
367    virtual IOHIDOutputTransactionInterface ** allocOutputTransaction();
368};
369
370
371#endif /* !_IOKIT_IOHIDDeviceClass_H */
372