-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
beep.c
411 lines (357 loc) · 11.4 KB
/
beep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL - See COPYING in the top level directory
* FILE: drivers/base/beep/beep.c
* PURPOSE: Beep Device Driver
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
* Eric Kohl
*/
/* INCLUDES ******************************************************************/
#include <ntddk.h>
#include <ntddbeep.h>
#ifndef NDEBUG
#define NDEBUG
#endif
#include <debug.h>
/* TYPES *********************************************************************/
typedef struct _BEEP_DEVICE_EXTENSION
{
LONG ReferenceCount;
FAST_MUTEX Mutex;
KTIMER Timer;
LONG TimerActive;
PVOID SectionHandle;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
/* FUNCTIONS *****************************************************************/
VOID
NTAPI
BeepDPC(IN PKDPC Dpc,
IN PDEVICE_OBJECT DeviceObject,
IN PVOID SystemArgument1,
IN PVOID SystemArgument2)
{
PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
UNREFERENCED_PARAMETER(Dpc);
UNREFERENCED_PARAMETER(SystemArgument1);
UNREFERENCED_PARAMETER(SystemArgument2);
/* Stop the beep */
HalMakeBeep(0);
/* Disable the timer */
InterlockedDecrement(&DeviceExtension->TimerActive);
}
DRIVER_DISPATCH BeepCreate;
NTSTATUS
NTAPI
BeepCreate(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
/* Acquire the mutex and increase reference count */
ExAcquireFastMutex(&DeviceExtension->Mutex);
if (++DeviceExtension->ReferenceCount == 1)
{
/* First reference, lock the data section */
DeviceExtension->SectionHandle = MmLockPagableDataSection(BeepCreate);
}
/* Release it */
ExReleaseFastMutex(&DeviceExtension->Mutex);
/* Complete the request */
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
DRIVER_DISPATCH BeepClose;
NTSTATUS
NTAPI
BeepClose(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
/* Acquire the mutex and decrease reference count */
ExAcquireFastMutex(&DeviceExtension->Mutex);
if (!(--DeviceExtension->ReferenceCount))
{
/* Check for active timer */
if (DeviceExtension->TimerActive)
{
/* Cancel it */
if (KeCancelTimer(&DeviceExtension->Timer))
{
/* Mark it as cancelled */
InterlockedDecrement(&DeviceExtension->TimerActive);
}
}
/* Page the driver */
MmUnlockPagableImageSection(DeviceExtension->SectionHandle);
}
/* Release the lock */
ExReleaseFastMutex(&DeviceExtension->Mutex);
/* Complete the request */
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
DRIVER_CANCEL BeepCancel;
VOID
NTAPI
BeepCancel(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
/* Check if this is the current request */
if (Irp == DeviceObject->CurrentIrp)
{
/* Clear it */
DeviceObject->CurrentIrp = NULL;
/* Release the cancel lock and start the next packet */
IoReleaseCancelSpinLock(Irp->CancelIrql);
IoStartNextPacket(DeviceObject, TRUE);
}
else
{
/* Otherwise, remove the packet from the queue and release the lock */
KeRemoveEntryDeviceQueue(&DeviceObject->DeviceQueue,
&Irp->Tail.Overlay.DeviceQueueEntry);
IoReleaseCancelSpinLock(Irp->CancelIrql);
}
/* Complete the request */
Irp->IoStatus.Status = STATUS_CANCELLED;
Irp->IoStatus.Information = 0;
IoCompleteRequest (Irp, IO_NO_INCREMENT);
}
DRIVER_DISPATCH BeepCleanup;
NTSTATUS
NTAPI
BeepCleanup(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
KIRQL OldIrql, CancelIrql;
PKDEVICE_QUEUE_ENTRY Packet;
PIRP CurrentIrp;
/* Raise IRQL and acquire the cancel lock */
KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
IoAcquireCancelSpinLock(&CancelIrql);
/* Get the current IRP */
CurrentIrp = DeviceObject->CurrentIrp;
DeviceObject->CurrentIrp = NULL;
while (CurrentIrp)
{
/* Clear its cancel routine */
(VOID)IoSetCancelRoutine(CurrentIrp, NULL);
/* Cancel the IRP */
CurrentIrp->IoStatus.Status = STATUS_CANCELLED;
CurrentIrp->IoStatus.Information = 0;
/* Release the cancel lock and complete it */
IoReleaseCancelSpinLock(CancelIrql);
IoCompleteRequest(CurrentIrp, IO_NO_INCREMENT);
/* Reacquire the lock and get the next queue packet */
IoAcquireCancelSpinLock(&CancelIrql);
Packet = KeRemoveDeviceQueue(&DeviceObject->DeviceQueue);
if (Packet)
{
/* Get the IRP */
CurrentIrp = CONTAINING_RECORD(Packet,
IRP,
Tail.Overlay.DeviceQueueEntry);
}
else
{
/* No more IRPs */
CurrentIrp = NULL;
}
}
/* Release lock and go back to low IRQL */
IoReleaseCancelSpinLock(CancelIrql);
KeLowerIrql(OldIrql);
/* Complete the IRP */
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
/* Stop and beep and return */
HalMakeBeep(0);
return STATUS_SUCCESS;
}
DRIVER_DISPATCH BeepDeviceControl;
NTSTATUS
NTAPI
BeepDeviceControl(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
PIO_STACK_LOCATION Stack;
PBEEP_SET_PARAMETERS BeepParam;
NTSTATUS Status;
/* Get the stack location and parameters */
Stack = IoGetCurrentIrpStackLocation(Irp);
BeepParam = (PBEEP_SET_PARAMETERS)Irp->AssociatedIrp.SystemBuffer;
/* We only support one IOCTL */
if (Stack->Parameters.DeviceIoControl.IoControlCode != IOCTL_BEEP_SET)
{
/* Unsupported command */
Status = STATUS_NOT_IMPLEMENTED;
}
else
{
/* Validate the input buffer length */
if (Stack->Parameters.DeviceIoControl.InputBufferLength <
sizeof(BEEP_SET_PARAMETERS))
{
/* Invalid buffer */
Status = STATUS_INVALID_PARAMETER;
}
else if ((BeepParam->Frequency != 0) && !(BeepParam->Duration))
{
/* No duration, return immediately */
Status = STATUS_SUCCESS;
}
else
{
/* We'll queue this request */
Status = STATUS_PENDING;
}
}
/* Set packet information */
Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;
/* Check if we're completing or queuing a packet */
if (Status == STATUS_PENDING)
{
/* Start the queue */
IoMarkIrpPending(Irp);
IoStartPacket(DeviceObject, Irp, NULL, BeepCancel);
}
else
{
/* Complete the request */
IoCompleteRequest(Irp, IO_NO_INCREMENT);
}
/* Return */
return Status;
}
DRIVER_UNLOAD BeepUnload;
VOID
NTAPI
BeepUnload(IN PDRIVER_OBJECT DriverObject)
{
PDEVICE_EXTENSION DeviceExtension;
PDEVICE_OBJECT DeviceObject;
/* Get DO and DE */
DeviceObject = DriverObject->DeviceObject;
DeviceExtension = DeviceObject->DeviceExtension;
/* Check if the timer is active */
if (DeviceExtension->TimerActive)
{
/* Cancel it */
if (KeCancelTimer(&DeviceExtension->Timer))
{
/* All done */
InterlockedDecrement(&DeviceExtension->TimerActive);
}
}
/* Delete the object */
IoDeleteDevice(DeviceObject);
}
DRIVER_STARTIO BeepStartIo;
VOID
NTAPI
BeepStartIo(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
KIRQL CancelIrql;
PIO_STACK_LOCATION IoStack;
PBEEP_SET_PARAMETERS BeepParam;
LARGE_INTEGER DueTime;
NTSTATUS Status;
/* Acquire the cancel lock and make sure the IRP is valid */
IoAcquireCancelSpinLock(&CancelIrql);
if (!Irp)
{
/* It's not, release the lock and quit */
IoReleaseCancelSpinLock(CancelIrql);
return;
}
/* Remove the cancel routine and release the lock */
(VOID)IoSetCancelRoutine(Irp, NULL);
IoReleaseCancelSpinLock(CancelIrql);
/* Get the I/O Stack and make sure the request is valid */
BeepParam = (PBEEP_SET_PARAMETERS)Irp->AssociatedIrp.SystemBuffer;
IoStack = IoGetCurrentIrpStackLocation(Irp);
if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_BEEP_SET)
{
/* Check if we have an active timer */
if (DeviceExtension->TimerActive)
{
/* Cancel it */
if (KeCancelTimer(&DeviceExtension->Timer))
{
/* Set the state */
InterlockedDecrement(&DeviceExtension->TimerActive);
}
}
/* Make the beep */
if (HalMakeBeep(BeepParam->Frequency))
{
/* Beep successful, queue a DPC to stop it */
Status = STATUS_SUCCESS;
DueTime.QuadPart = BeepParam->Duration * -10000LL;
InterlockedIncrement(&DeviceExtension->TimerActive);
KeSetTimer(&DeviceExtension->Timer, DueTime, &DeviceObject->Dpc);
}
else
{
/* Beep has failed */
Status = STATUS_INVALID_PARAMETER;
}
}
else
{
/* Invalid request */
Status = STATUS_INVALID_PARAMETER;
}
/* Complete the request and start the next packet */
Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;
IoStartNextPacket(DeviceObject, TRUE);
IoCompleteRequest(Irp, IO_NO_INCREMENT);
}
NTSTATUS
NTAPI
DriverEntry(IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath)
{
PDEVICE_EXTENSION DeviceExtension;
PDEVICE_OBJECT DeviceObject;
UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\Beep");
NTSTATUS Status;
UNREFERENCED_PARAMETER(RegistryPath);
/* Create the device */
Status = IoCreateDevice(DriverObject,
sizeof(DEVICE_EXTENSION),
&DeviceName,
FILE_DEVICE_BEEP,
0,
FALSE,
&DeviceObject);
if (!NT_SUCCESS(Status)) return Status;
/* Make it use buffered I/O */
DeviceObject->Flags |= DO_BUFFERED_IO;
/* Setup the Driver Object */
DriverObject->MajorFunction[IRP_MJ_CREATE] = BeepCreate;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = BeepClose;
DriverObject->MajorFunction[IRP_MJ_CLEANUP] = BeepCleanup;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = BeepDeviceControl;
DriverObject->DriverUnload = BeepUnload;
DriverObject->DriverStartIo = BeepStartIo;
/* Set up device extension */
DeviceExtension = DeviceObject->DeviceExtension;
DeviceExtension->ReferenceCount = 0;
DeviceExtension->TimerActive = FALSE;
IoInitializeDpcRequest(DeviceObject, (PIO_DPC_ROUTINE)BeepDPC);
KeInitializeTimer(&DeviceExtension->Timer);
ExInitializeFastMutex(&DeviceExtension->Mutex);
/* Page the entire driver */
MmPageEntireDriver(DriverEntry);
return STATUS_SUCCESS;
}
/* EOF */