1/*
2 * Copyright (c) 2019-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _IOKIT_UIOSERVICEDISPATCHSOURCE_H
30#define _IOKIT_UIOSERVICEDISPATCHSOURCE_H
31
32#include <DriverKit/IODispatchQueue.iig>
33#include <DriverKit/OSAction.iig>
34#include <DriverKit/IOService.iig>
35
36
37typedef void (^IOServiceNotificationBlock)(uint64_t type, IOService * service, uint64_t options);
38
39enum {
40	kIOServiceNotificationTypeTerminated = 0x00000000,
41	kIOServiceNotificationTypeMatched    = 0x00000001,
42	kIOServiceNotificationTypeLast       = kIOServiceNotificationTypeMatched,
43	kIOServiceNotificationTypeNone       = 0xFFFFFFFF,
44};
45
46class NATIVE KERNEL IOServiceNotificationDispatchSource : public IODispatchSource
47{
48public:
49
50    /*!
51     * @brief       Create an IOServiceNotificationDispatchSource for IOService matching and termination events.
52     * @param       matching An IOService matching dictionary.
53     * @param       options None defined, pass zero.
54     * @param       queue IODispatchQueue the source is attached to. Note that the ServiceNotificationReady
55     *              handler is invoked on the queue set for the target method
56     *              of the OSAction, not this queue.
57     * @param       source Created source with +1 retain count to be released by the caller.
58     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
59     */
60	static kern_return_t
61	Create(
62		OSDictionary	  *	matching,
63		uint64_t            options,
64	    IODispatchQueue   * queue,
65		IOServiceNotificationDispatchSource ** notification) LOCAL;
66
67	virtual bool
68	init() override;
69
70	virtual void
71	free() override;
72
73    /*!
74     * @brief       Control the enable state of the notification.
75     * @param       enable Pass true to enable the source or false to disable.
76     * @param       handler Optional block to be executed after the interrupt has been disabled and any pending
77     *              interrupt handlers completed.
78     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
79     */
80	virtual kern_return_t
81	SetEnableWithCompletion(
82		bool enable,
83		IODispatchSourceCancelHandler handler) override LOCAL;
84
85    /*!
86     * @brief       Cancel all callbacks from the event source.
87     * @discussion  After cancellation, the source can only be freed. It cannot be reactivated.
88     * @param       handler Handler block to be invoked after any callbacks have completed.
89     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
90     */
91	virtual kern_return_t
92	Cancel(IODispatchSourceCancelHandler handler) override LOCAL;
93
94    /*!
95     * @brief       Set the handler block to run when the notification has become ready.
96     * @param       action OSAction instance specifying the callback method. The OSAction object will be retained
97     *              until SetHandler is called again or the event source is cancelled.
98     *              The ServiceNotificationReady handler is invoked on the queue set for the target method of the
99     *              OSAction.
100     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
101     */
102	virtual kern_return_t
103	SetHandler(
104	OSAction * action TYPE(ServiceNotificationReady));
105
106    /*!
107     * @brief       Invoke a block for each notification available in response to ServiceNotificationReady.
108     * @discussion  The IOService object passed to the notification is only retained for the duration of the block.
109     *              It should be retained by the block code if used beyond the invocation.
110     * @param       block to be invoked with each notification
111     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
112     */
113	kern_return_t
114	DeliverNotifications(IOServiceNotificationBlock block) LOCALONLY;
115
116private:
117	virtual kern_return_t
118	CheckForWork(bool synchronous) override LOCAL;
119
120	virtual void
121	ServiceNotificationReady(
122		OSAction  * action TARGET) LOCAL = 0;
123
124	virtual kern_return_t
125	CopyNextNotification(
126		uint64_t   * type,
127		IOService ** service,
128		uint64_t   * options);
129};
130
131#endif /* ! _IOKIT_UIOSERVICEDISPATCHSOURCE_H */
132