1 /* $Id: cntrller.c,v 1.9 2002/09/08 10:23:24 chorns Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/cntrller.c
6 * PURPOSE: Implements the controller object
7 * PROGRAMMER: David Welch (welch@mcmail.com)
12 /* INCLUDES *****************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/pool.h>
17 #include <internal/debug.h>
19 /* GLOBALS *******************************************************************/
21 #define TAG_CQE TAG('C', 'Q', 'E', ' ')
22 #define TAG_CONTROLLER TAG('C', 'N', 'T', 'R')
23 #define TAG_CONTROLLER_EXTENSION TAG('C', 'E', 'X', 'T')
25 /* TYPES ********************************************************************/
29 * PURPOSE: A entry in the queue waiting for a controller object
32 KDEVICE_QUEUE_ENTRY Entry
;
33 PDEVICE_OBJECT DeviceObject
;
34 PDRIVER_CONTROL ExecutionRoutine
;
36 } CONTROLLER_QUEUE_ENTRY
, *PCONTROLLER_QUEUE_ENTRY
;
38 /* FUNCTIONS *****************************************************************/
42 IoAllocateController(PCONTROLLER_OBJECT ControllerObject
,
43 PDEVICE_OBJECT DeviceObject
,
44 PDRIVER_CONTROL ExecutionRoutine
,
47 * FUNCTION: Sets up a call to a driver-supplied ControllerControl routine
48 * as soon as the device controller, represented by the given controller
49 * object, is available to carry out an I/O operation for the target device,
50 * represented by the given device object.
52 * ControllerObject = Driver created controller object
53 * DeviceObject = Target device for the current irp
54 * ExecutionRoutine = Routine to be called when the device is available
55 * Context = Driver supplied context to be passed on to the above routine
56 * NOTE: Is the below implementation correct.
59 PCONTROLLER_QUEUE_ENTRY entry
;
60 IO_ALLOCATION_ACTION Result
;
62 assert(KeGetCurrentIrql() == DISPATCH_LEVEL
);
65 ExAllocatePoolWithTag(NonPagedPool
, sizeof(CONTROLLER_QUEUE_ENTRY
),
69 entry
->DeviceObject
= DeviceObject
;
70 entry
->ExecutionRoutine
= ExecutionRoutine
;
71 entry
->Context
= Context
;
73 if (KeInsertDeviceQueue(&ControllerObject
->DeviceWaitQueue
,&entry
->Entry
))
77 Result
= ExecutionRoutine(DeviceObject
,DeviceObject
->CurrentIrp
,
79 if (Result
== DeallocateObject
)
81 IoFreeController(ControllerObject
);
88 IoCreateController(ULONG Size
)
90 * FUNCTION: Allocates memory and initializes a controller object
92 * Size = Size (in bytes) to be allocated for the controller extension
93 * RETURNS: A pointer to the created object
96 PCONTROLLER_OBJECT controller
;
98 assert_irql(PASSIVE_LEVEL
);
101 ExAllocatePoolWithTag(NonPagedPool
, sizeof(CONTROLLER_OBJECT
),
103 if (controller
==NULL
)
108 controller
->ControllerExtension
=
109 ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_CONTROLLER_EXTENSION
);
110 if (controller
->ControllerExtension
==NULL
)
112 ExFreePool(controller
);
116 KeInitializeDeviceQueue(&controller
->DeviceWaitQueue
);
122 IoDeleteController(PCONTROLLER_OBJECT ControllerObject
)
124 * FUNCTION: Removes a given controller object from the system
126 * ControllerObject = Controller object to be released
129 assert_irql(PASSIVE_LEVEL
);
131 ExFreePool(ControllerObject
->ControllerExtension
);
132 ExFreePool(ControllerObject
);
137 IoFreeController(PCONTROLLER_OBJECT ControllerObject
)
139 * FUNCTION: Releases a previously allocated controller object when a
140 * device has finished an I/O request
142 * ControllerObject = Controller object to be released
145 PKDEVICE_QUEUE_ENTRY QEntry
;
146 CONTROLLER_QUEUE_ENTRY
* Entry
;
147 IO_ALLOCATION_ACTION Result
;
151 QEntry
= KeRemoveDeviceQueue(&ControllerObject
->DeviceWaitQueue
);
152 Entry
= CONTAINING_RECORD(QEntry
,CONTROLLER_QUEUE_ENTRY
,Entry
);
157 Result
= Entry
->ExecutionRoutine(Entry
->DeviceObject
,
158 Entry
->DeviceObject
->CurrentIrp
,
162 } while (Result
== DeallocateObject
);