Skip to content

Commit c1fb2cc

Browse files
committed
usb: hub: FDO: Fix USBHUB_FdoStartDevice()
Done fixes in cleanup also refactored code to be more readable and error safe. svn path=/branches/GSoC_2016/USB/; revision=72381
1 parent a504c6b commit c1fb2cc

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

drivers/usb/usbhub/fdo.c

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,8 @@ USBHUB_FdoStartDevice(
15811581
if (!Urb)
15821582
{
15831583
// no memory
1584-
return STATUS_INSUFFICIENT_RESOURCES;
1584+
Status = STATUS_INSUFFICIENT_RESOURCES;
1585+
goto cleanup;
15851586
}
15861587

15871588
// zero urb
@@ -1596,8 +1597,7 @@ USBHUB_FdoStartDevice(
15961597
{
15971598
// failed to obtain hub pdo
15981599
DPRINT1("IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO failed with %x\n", Status);
1599-
ExFreePool(Urb);
1600-
return Status;
1600+
goto cleanup;
16011601
}
16021602

16031603
// sanity checks
@@ -1614,8 +1614,7 @@ USBHUB_FdoStartDevice(
16141614
{
16151615
// failed to start pdo
16161616
DPRINT1("Failed to start the RootHub PDO\n");
1617-
ExFreePool(Urb);
1618-
return Status;
1617+
goto cleanup;
16191618
}
16201619

16211620
// Get the current number of hubs
@@ -1626,8 +1625,7 @@ USBHUB_FdoStartDevice(
16261625
{
16271626
// failed to get number of hubs
16281627
DPRINT1("IOCTL_INTERNAL_USB_GET_HUB_COUNT failed with %x\n", Status);
1629-
ExFreePool(Urb);
1630-
return Status;
1628+
goto cleanup;
16311629
}
16321630

16331631
// Get the Hub Interface
@@ -1641,8 +1639,7 @@ USBHUB_FdoStartDevice(
16411639
{
16421640
// failed to get root hub interface
16431641
DPRINT1("Failed to get HUB_GUID interface with status 0x%08lx\n", Status);
1644-
ExFreePool(Urb);
1645-
return Status;
1642+
goto cleanup;
16461643
}
16471644

16481645
HubInterfaceBusContext = HubDeviceExtension->HubInterface.BusContext;
@@ -1658,8 +1655,7 @@ USBHUB_FdoStartDevice(
16581655
{
16591656
// failed to get usbdi interface
16601657
DPRINT1("Failed to get USBDI_GUID interface with status 0x%08lx\n", Status);
1661-
ExFreePool(Urb);
1662-
return Status;
1658+
goto cleanup;
16631659
}
16641660

16651661
// Get Root Hub Device Handle
@@ -1672,8 +1668,7 @@ USBHUB_FdoStartDevice(
16721668
{
16731669
// failed
16741670
DPRINT1("IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE failed with status 0x%08lx\n", Status);
1675-
ExFreePool(Urb);
1676-
return Status;
1671+
goto cleanup;
16771672
}
16781673

16791674
//
@@ -1717,8 +1712,7 @@ USBHUB_FdoStartDevice(
17171712
{
17181713
// failed to get device descriptor of hub
17191714
DPRINT1("Failed to get HubDeviceDescriptor!\n");
1720-
ExFreePool(Urb);
1721-
return Status;
1715+
goto cleanup;
17221716
}
17231717

17241718
// build configuration request
@@ -1745,8 +1739,7 @@ USBHUB_FdoStartDevice(
17451739
{
17461740
// failed to get configuration descriptor
17471741
DPRINT1("Failed to get RootHub Configuration with status %x\n", Status);
1748-
ExFreePool(Urb);
1749-
return Status;
1742+
goto cleanup;
17501743
}
17511744

17521745
// sanity checks
@@ -1772,16 +1765,15 @@ USBHUB_FdoStartDevice(
17721765
{
17731766
// failed to get hub information
17741767
DPRINT1("Failed to extended hub information. Unable to determine the number of ports!\n");
1775-
ExFreePool(Urb);
1776-
return Status;
1768+
goto cleanup;
17771769
}
17781770

17791771
if (!HubDeviceExtension->UsbExtHubInfo.NumberOfPorts)
17801772
{
17811773
// bogus port driver
17821774
DPRINT1("Failed to retrieve the number of ports\n");
1783-
ExFreePool(Urb);
1784-
return STATUS_UNSUCCESSFUL;
1775+
Status = STATUS_UNSUCCESSFUL;
1776+
goto cleanup;
17851777
}
17861778

17871779
DPRINT("HubDeviceExtension->UsbExtHubInfo.NumberOfPorts %x\n", HubDeviceExtension->UsbExtHubInfo.NumberOfPorts);
@@ -1812,8 +1804,8 @@ USBHUB_FdoStartDevice(
18121804
if (!NT_SUCCESS(Status))
18131805
{
18141806
DPRINT1("Failed to get Hub Descriptor!\n");
1815-
ExFreePool(Urb);
1816-
return STATUS_UNSUCCESSFUL;
1807+
Status = STATUS_UNSUCCESSFUL;
1808+
goto cleanup;
18171809
}
18181810

18191811
// sanity checks
@@ -1841,15 +1833,22 @@ USBHUB_FdoStartDevice(
18411833
{
18421834
// failed to get hub status
18431835
DPRINT1("Failed to get Hub Status!\n");
1844-
ExFreePool(Urb);
1845-
return STATUS_UNSUCCESSFUL;
1836+
Status = STATUS_UNSUCCESSFUL;
1837+
goto cleanup;
18461838
}
18471839

18481840
// Allocate memory for PortStatusChange to hold 2 USHORTs for each port on hub
18491841
HubDeviceExtension->PortStatusChange = ExAllocatePoolWithTag(NonPagedPool,
18501842
sizeof(ULONG) * HubDeviceExtension->UsbExtHubInfo.NumberOfPorts,
18511843
USB_HUB_TAG);
18521844

1845+
if (!HubDeviceExtension->PortStatusChange)
1846+
{
1847+
DPRINT1("Failed to allocate pool for PortStatusChange!\n");
1848+
Status = STATUS_INSUFFICIENT_RESOURCES;
1849+
goto cleanup;
1850+
}
1851+
18531852
// Get the first Configuration Descriptor
18541853
Pid = USBD_ParseConfigurationDescriptorEx(&HubDeviceExtension->HubConfigDescriptor,
18551854
&HubDeviceExtension->HubConfigDescriptor,
@@ -1858,8 +1857,8 @@ USBHUB_FdoStartDevice(
18581857
{
18591858
// failed parse hub descriptor
18601859
DPRINT1("Failed to parse configuration descriptor\n");
1861-
ExFreePool(Urb);
1862-
return STATUS_UNSUCCESSFUL;
1860+
Status = STATUS_UNSUCCESSFUL;
1861+
goto cleanup;
18631862
}
18641863

18651864
// create configuration request
@@ -1870,8 +1869,8 @@ USBHUB_FdoStartDevice(
18701869
{
18711870
// failed to build urb
18721871
DPRINT1("Failed to allocate urb\n");
1873-
ExFreePool(Urb);
1874-
return STATUS_INSUFFICIENT_RESOURCES;
1872+
Status = STATUS_INSUFFICIENT_RESOURCES;
1873+
goto cleanup;
18751874
}
18761875

18771876
// send request
@@ -1883,9 +1882,7 @@ USBHUB_FdoStartDevice(
18831882
{
18841883
// failed to select configuration
18851884
DPRINT1("Failed to select configuration with %x\n", Status);
1886-
ExFreePool(Urb);
1887-
ExFreePool(ConfigUrb);
1888-
return Status;
1885+
goto cleanup;
18891886
}
18901887

18911888
// store configuration & pipe handle
@@ -1895,10 +1892,6 @@ USBHUB_FdoStartDevice(
18951892

18961893
FDO_QueryInterface(DeviceObject, &HubDeviceExtension->DeviceInterface);
18971894

1898-
1899-
// free urb
1900-
ExFreePool(ConfigUrb);
1901-
19021895
// check if function is available
19031896
if (HubDeviceExtension->UsbDInterface.IsDeviceHighSpeed)
19041897
{
@@ -1938,8 +1931,7 @@ USBHUB_FdoStartDevice(
19381931
if (!NT_SUCCESS(Status))
19391932
{
19401933
DPRINT1("Failed to set callback\n");
1941-
ExFreePool(Urb);
1942-
return Status;
1934+
goto cleanup;
19431935
}
19441936
}
19451937
else
@@ -1991,8 +1983,30 @@ USBHUB_FdoStartDevice(
19911983
// free urb
19921984
ExFreePool(Urb);
19931985

1986+
// free ConfigUrb
1987+
ExFreePool(ConfigUrb);
1988+
19941989
// done
19951990
return Status;
1991+
1992+
cleanup:
1993+
if (Urb)
1994+
ExFreePool(Urb);
1995+
1996+
// Dereference interfaces
1997+
if (HubDeviceExtension->HubInterface.Size)
1998+
HubDeviceExtension->HubInterface.InterfaceDereference(HubDeviceExtension->HubInterface.BusContext);
1999+
2000+
if (HubDeviceExtension->UsbDInterface.Size)
2001+
HubDeviceExtension->UsbDInterface.InterfaceDereference(HubDeviceExtension->UsbDInterface.BusContext);
2002+
2003+
if (HubDeviceExtension->PortStatusChange)
2004+
ExFreePool(HubDeviceExtension->PortStatusChange);
2005+
2006+
if (ConfigUrb)
2007+
ExFreePool(ConfigUrb);
2008+
2009+
return Status;
19962010
}
19972011

19982012
NTSTATUS

0 commit comments

Comments
 (0)