[MOUHID]
[reactos.git] / reactos / drivers / hid / kbdhid / kbdhid.c
index 8366d46..f33a99d 100644 (file)
@@ -48,7 +48,7 @@ KbdHid_InsertScanCodes(
     CHAR Prefix = 0;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)Context;
+    DeviceExtension = Context;
 
     for(Index = 0; Index < Length; Index++)
     {
@@ -104,7 +104,7 @@ KbdHid_ReadCompletion(
     ULONG ButtonLength;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)Context;
+    DeviceExtension = Context;
 
     if (Irp->IoStatus.Status == STATUS_PRIVILEGE_NOT_HELD ||
         Irp->IoStatus.Status == STATUS_DEVICE_NOT_CONNECTED ||
@@ -247,7 +247,7 @@ KbdHid_Create(
     DPRINT("[KBDHID]: IRP_MJ_CREATE\n");
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* get stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
@@ -318,7 +318,7 @@ KbdHid_Close(
     PKBDHID_DEVICE_EXTENSION DeviceExtension;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     DPRINT("[KBDHID] IRP_MJ_CLOSE ReadReportActive %x\n", DeviceExtension->ReadReportActive);
 
@@ -363,65 +363,65 @@ KbdHid_InternalDeviceControl(
     DPRINT("[KBDHID] InternalDeviceControl %x\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     switch (IoStack->Parameters.DeviceIoControl.IoControlCode)
     {
         case IOCTL_KEYBOARD_QUERY_ATTRIBUTES:
-        /* verify output buffer length */
-        if (IoStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUSE_ATTRIBUTES))
-        {
-            /* invalid request */
-            DPRINT1("[MOUHID] IOCTL_MOUSE_QUERY_ATTRIBUTES Buffer too small\n");
-            Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
-            IoCompleteRequest(Irp, IO_NO_INCREMENT);
-            return STATUS_BUFFER_TOO_SMALL;
-        }
+            /* verify output buffer length */
+            if (IoStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUSE_ATTRIBUTES))
+            {
+                /* invalid request */
+                DPRINT1("[MOUHID] IOCTL_MOUSE_QUERY_ATTRIBUTES Buffer too small\n");
+                Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_BUFFER_TOO_SMALL;
+            }
 
-        /* get output buffer */
-        Attributes = (PKEYBOARD_ATTRIBUTES)Irp->AssociatedIrp.SystemBuffer;
+            /* get output buffer */
+            Attributes = Irp->AssociatedIrp.SystemBuffer;
 
-        /* copy attributes */
+            /* copy attributes */
             RtlCopyMemory(Attributes,
                           &DeviceExtension->Attributes,
                           sizeof(KEYBOARD_ATTRIBUTES));
 
-         /* complete request */
-         Irp->IoStatus.Information = sizeof(MOUSE_ATTRIBUTES);
-         Irp->IoStatus.Status = STATUS_SUCCESS;
-         IoCompleteRequest(Irp, IO_NO_INCREMENT);
-         return STATUS_SUCCESS;
+            /* complete request */
+            Irp->IoStatus.Information = sizeof(MOUSE_ATTRIBUTES);
+            Irp->IoStatus.Status = STATUS_SUCCESS;
+            IoCompleteRequest(Irp, IO_NO_INCREMENT);
+            return STATUS_SUCCESS;
 
         case IOCTL_INTERNAL_KEYBOARD_CONNECT:
-             /* verify input buffer length */
-         if (IoStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(CONNECT_DATA))
-         {
-             /* invalid request */
-             Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
-             IoCompleteRequest(Irp, IO_NO_INCREMENT);
-             return STATUS_INVALID_PARAMETER;
-         }
+            /* verify input buffer length */
+            if (IoStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(CONNECT_DATA))
+            {
+                /* invalid request */
+                Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_INVALID_PARAMETER;
+            }
 
-         /* is it already connected */
-         if (DeviceExtension->ClassService)
-         {
-             /* already connected */
-             Irp->IoStatus.Status = STATUS_SHARING_VIOLATION;
-             IoCompleteRequest(Irp, IO_NO_INCREMENT);
-             return STATUS_SHARING_VIOLATION;
-         }
+            /* is it already connected */
+            if (DeviceExtension->ClassService)
+            {
+                /* already connected */
+                Irp->IoStatus.Status = STATUS_SHARING_VIOLATION;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_SHARING_VIOLATION;
+            }
 
-         /* get connect data */
-         Data = (PCONNECT_DATA)IoStack->Parameters.DeviceIoControl.Type3InputBuffer;
+            /* get connect data */
+            Data = IoStack->Parameters.DeviceIoControl.Type3InputBuffer;
 
-         /* store connect details */
-         DeviceExtension->ClassDeviceObject = Data->ClassDeviceObject;
-         DeviceExtension->ClassService = Data->ClassService;
+            /* store connect details */
+            DeviceExtension->ClassDeviceObject = Data->ClassDeviceObject;
+            DeviceExtension->ClassService = Data->ClassService;
 
-         /* completed successfully */
-         Irp->IoStatus.Status = STATUS_SUCCESS;
-         IoCompleteRequest(Irp, IO_NO_INCREMENT);
-             return STATUS_SUCCESS;
+            /* completed successfully */
+            Irp->IoStatus.Status = STATUS_SUCCESS;
+            IoCompleteRequest(Irp, IO_NO_INCREMENT);
+            return STATUS_SUCCESS;
 
         case IOCTL_INTERNAL_KEYBOARD_DISCONNECT:
             /* not implemented */
@@ -444,11 +444,11 @@ KbdHid_InternalDeviceControl(
         case IOCTL_KEYBOARD_QUERY_INDICATORS:
             if (IoStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(KEYBOARD_INDICATOR_PARAMETERS))
             {
-             /* invalid parameter */
-             Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
-             IoCompleteRequest(Irp, IO_NO_INCREMENT);
-             return STATUS_INVALID_PARAMETER;
-        }
+                /* invalid parameter */
+                Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_INVALID_PARAMETER;
+            }
 
             /* copy indicators */
             RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer,
@@ -464,10 +464,10 @@ KbdHid_InternalDeviceControl(
         case IOCTL_KEYBOARD_QUERY_TYPEMATIC:
             if (IoStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(KEYBOARD_TYPEMATIC_PARAMETERS))
             {
-                 /* invalid parameter */
-                 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
-                 IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                 return STATUS_INVALID_PARAMETER;
+                /* invalid parameter */
+                Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_INVALID_PARAMETER;
             }
 
             /* copy indicators */
@@ -477,17 +477,17 @@ KbdHid_InternalDeviceControl(
 
             /* done */
             Irp->IoStatus.Status = STATUS_SUCCESS;
-        Irp->IoStatus.Information = sizeof(KEYBOARD_TYPEMATIC_PARAMETERS);
-        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+            Irp->IoStatus.Information = sizeof(KEYBOARD_TYPEMATIC_PARAMETERS);
+            IoCompleteRequest(Irp, IO_NO_INCREMENT);
             return STATUS_SUCCESS;
 
         case IOCTL_KEYBOARD_SET_INDICATORS:
             if (IoStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(KEYBOARD_INDICATOR_PARAMETERS))
             {
-                 /* invalid parameter */
-                 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
-                 IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                 return STATUS_INVALID_PARAMETER;
+                /* invalid parameter */
+                Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_INVALID_PARAMETER;
             }
 
             /* copy indicators */
@@ -504,10 +504,10 @@ KbdHid_InternalDeviceControl(
         case IOCTL_KEYBOARD_SET_TYPEMATIC:
             if (IoStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(KEYBOARD_TYPEMATIC_PARAMETERS))
             {
-                 /* invalid parameter */
-                 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
-                 IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                 return STATUS_INVALID_PARAMETER;
+                /* invalid parameter */
+                Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return STATUS_INVALID_PARAMETER;
             }
 
             /* copy indicators */
@@ -523,10 +523,10 @@ KbdHid_InternalDeviceControl(
 
         case IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION:
             /* not implemented */
-        DPRINT1("IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION not implemented\n");
-        Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
-        IoCompleteRequest(Irp, IO_NO_INCREMENT);
-        return STATUS_NOT_IMPLEMENTED;
+            DPRINT1("IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION not implemented\n");
+            Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
+            IoCompleteRequest(Irp, IO_NO_INCREMENT);
+            return STATUS_NOT_IMPLEMENTED;
     }
 
     /* unknown control code */
@@ -546,7 +546,7 @@ KbdHid_DeviceControl(
     PKBDHID_DEVICE_EXTENSION DeviceExtension;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* skip stack location */
     IoSkipCurrentIrpStackLocation(Irp);
@@ -598,7 +598,7 @@ KbdHid_SubmitRequest(
     IO_STATUS_BLOCK IoStatus;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* init event */
     KeInitializeEvent(&Event, NotificationEvent, FALSE);
@@ -646,7 +646,7 @@ KbdHid_StartDevice(
     PUSAGE_AND_PAGE Buffer;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* query collection information */
     Status = KbdHid_SubmitRequest(DeviceObject,
@@ -663,7 +663,7 @@ KbdHid_StartDevice(
     }
 
     /* lets allocate space for preparsed data */
-    PreparsedData = (PHIDP_PREPARSED_DATA)ExAllocatePool(NonPagedPool, Information.DescriptorSize);
+    PreparsedData = ExAllocatePoolWithTag(NonPagedPool, Information.DescriptorSize, KBDHID_TAG);
     if (!PreparsedData)
     {
         /* no memory */
@@ -682,7 +682,7 @@ KbdHid_StartDevice(
     {
         /* failed to get preparsed data */
         DPRINT1("[KBDHID] failed to obtain collection information with %x\n", Status);
-        ExFreePool(PreparsedData);
+        ExFreePoolWithTag(PreparsedData, KBDHID_TAG);
         return Status;
     }
 
@@ -692,7 +692,7 @@ KbdHid_StartDevice(
     {
         /* failed to get capabilities */
         DPRINT1("[KBDHID] failed to obtain caps with %x\n", Status);
-        ExFreePool(PreparsedData);
+        ExFreePoolWithTag(PreparsedData, KBDHID_TAG);
         return Status;
     }
 
@@ -701,7 +701,7 @@ KbdHid_StartDevice(
     /* init input report */
     DeviceExtension->ReportLength = Capabilities.InputReportByteLength;
     ASSERT(DeviceExtension->ReportLength);
-    DeviceExtension->Report = (PCHAR)ExAllocatePool(NonPagedPool, DeviceExtension->ReportLength);
+    DeviceExtension->Report = ExAllocatePoolWithTag(NonPagedPool, DeviceExtension->ReportLength, KBDHID_TAG);
     ASSERT(DeviceExtension->Report);
     RtlZeroMemory(DeviceExtension->Report, DeviceExtension->ReportLength);
 
@@ -722,13 +722,14 @@ KbdHid_StartDevice(
     ASSERT(Buttons > 0);
 
     /* now allocate an array for those buttons */
-    Buffer = (PUSAGE_AND_PAGE)ExAllocatePool(NonPagedPool, sizeof(USAGE_AND_PAGE) * 4 * Buttons);
+    Buffer = ExAllocatePoolWithTag(NonPagedPool, sizeof(USAGE_AND_PAGE) * 4 * Buttons, KBDHID_TAG);
     if (!Buffer)
     {
         /* no memory */
-        ExFreePool(PreparsedData);
+        ExFreePoolWithTag(PreparsedData, KBDHID_TAG);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
+    DeviceExtension->UsageListBuffer = Buffer;
 
     /* init usage lists */
     RtlZeroMemory(Buffer, sizeof(USAGE_AND_PAGE) * 4 * Buttons);
@@ -771,6 +772,48 @@ KbdHid_StartDeviceCompletion(
     return STATUS_MORE_PROCESSING_REQUIRED;
 }
 
+NTSTATUS
+NTAPI
+KbdHid_FreeResources(
+    IN PDEVICE_OBJECT DeviceObject)
+{
+    PKBDHID_DEVICE_EXTENSION DeviceExtension;
+
+    /* get device extension */
+    DeviceExtension = DeviceObject->DeviceExtension;
+
+    /* free resources */
+    if (DeviceExtension->PreparsedData)
+    {
+        ExFreePoolWithTag(DeviceExtension->PreparsedData, KBDHID_TAG);
+        DeviceExtension->PreparsedData = NULL;
+    }
+
+    if (DeviceExtension->UsageListBuffer)
+    {
+        ExFreePoolWithTag(DeviceExtension->UsageListBuffer, KBDHID_TAG);
+        DeviceExtension->UsageListBuffer = NULL;
+        DeviceExtension->CurrentUsageList = NULL;
+        DeviceExtension->PreviousUsageList = NULL;
+        DeviceExtension->MakeUsageList = NULL;
+        DeviceExtension->BreakUsageList = NULL;
+    }
+
+    if (DeviceExtension->ReportMDL)
+    {
+        IoFreeMdl(DeviceExtension->ReportMDL);
+        DeviceExtension->ReportMDL = NULL;
+    }
+
+    if (DeviceExtension->Report)
+    {
+        ExFreePoolWithTag(DeviceExtension->Report, KBDHID_TAG);
+        DeviceExtension->Report = NULL;
+    }
+
+    return STATUS_SUCCESS;
+}
+
 NTSTATUS
 NTAPI
 KbdHid_Flush(
@@ -781,7 +824,7 @@ KbdHid_Flush(
     PKBDHID_DEVICE_EXTENSION DeviceExtension;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* skip current stack location */
     IoSkipCurrentIrpStackLocation(Irp);
@@ -809,7 +852,7 @@ KbdHid_Pnp(
     PKBDHID_DEVICE_EXTENSION DeviceExtension;
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* get current irp stack */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
@@ -818,6 +861,10 @@ KbdHid_Pnp(
     switch (IoStack->MinorFunction)
     {
     case IRP_MN_STOP_DEVICE:
+    case IRP_MN_SURPRISE_REMOVAL:
+        /* free resources */
+        KbdHid_FreeResources(DeviceObject);
+        /* fall through */
     case IRP_MN_CANCEL_REMOVE_DEVICE:
     case IRP_MN_QUERY_STOP_DEVICE:
     case IRP_MN_CANCEL_STOP_DEVICE:
@@ -837,6 +884,9 @@ KbdHid_Pnp(
         /* cancel irp */
         IoCancelIrp(DeviceExtension->Irp);
 
+        /* free resources */
+        KbdHid_FreeResources(DeviceObject);
+
         /* indicate success */
         Irp->IoStatus.Status = STATUS_SUCCESS;
 
@@ -933,7 +983,7 @@ KbdHid_AddDevice(
     }
 
     /* get device extension */
-    DeviceExtension = (PKBDHID_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    DeviceExtension = DeviceObject->DeviceExtension;
 
     /* zero extension */
     RtlZeroMemory(DeviceExtension, sizeof(KBDHID_DEVICE_EXTENSION));
@@ -988,8 +1038,6 @@ DriverEntry(
     IN PUNICODE_STRING RegPath)
 {
     /* initialize driver object */
-    DriverObject->DriverUnload = KbdHid_Unload;
-    DriverObject->DriverExtension->AddDevice = KbdHid_AddDevice;
     DriverObject->MajorFunction[IRP_MJ_CREATE] = KbdHid_Create;
     DriverObject->MajorFunction[IRP_MJ_CLOSE] = KbdHid_Close;
     DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] = KbdHid_Flush;