diff --git a/Kernel/Bus/USB/UHCI/UHCIController.cpp b/Kernel/Bus/USB/UHCI/UHCIController.cpp index 97f24ac59c..64a0583e16 100644 --- a/Kernel/Bus/USB/UHCI/UHCIController.cpp +++ b/Kernel/Bus/USB/UHCI/UHCIController.cpp @@ -64,15 +64,9 @@ static constexpr u16 UHCI_NUMBER_OF_FRAMES = 1024; KResultOr> UHCIController::try_to_initialize(PCI::Address address) { // NOTE: This assumes that address is pointing to a valid UHCI controller. - auto controller = adopt_ref_if_nonnull(new (nothrow) UHCIController(address)); - if (!controller) - return ENOMEM; - - auto init_result = controller->initialize(); - if (init_result.is_error()) - return init_result; - - return controller.release_nonnull(); + auto controller = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UHCIController(address))); + TRY(controller->initialize()); + return controller; } KResult UHCIController::initialize() @@ -83,12 +77,8 @@ KResult UHCIController::initialize() spawn_port_proc(); - auto reset_result = reset(); - if (reset_result.is_error()) - return reset_result; - - auto start_result = start(); - return start_result; + TRY(reset()); + return start(); } UNMAP_AFTER_INIT UHCIController::UHCIController(PCI::Address address) @@ -104,8 +94,7 @@ UNMAP_AFTER_INIT UHCIController::~UHCIController() KResult UHCIController::reset() { - if (auto stop_result = stop(); stop_result.is_error()) - return stop_result; + TRY(stop()); write_usbcmd(UHCI_USBCMD_HOST_CONTROLLER_RESET); @@ -117,17 +106,14 @@ KResult UHCIController::reset() } // Let's allocate the physical page for the Frame List (which is 4KiB aligned) - auto maybe_framelist_vmobj = Memory::AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE); - if (maybe_framelist_vmobj.is_error()) - return maybe_framelist_vmobj.error(); + auto vmobject = TRY(Memory::AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE)); - m_framelist = MM.allocate_kernel_region_with_vmobject(maybe_framelist_vmobj.release_value(), PAGE_SIZE, "UHCI Framelist", Memory::Region::Access::Write); + m_framelist = MM.allocate_kernel_region_with_vmobject(move(vmobject), PAGE_SIZE, "UHCI Framelist", Memory::Region::Access::Write); dbgln("UHCI: Allocated framelist at physical address {}", m_framelist->physical_page(0)->paddr()); dbgln("UHCI: Framelist is at virtual address {}", m_framelist->vaddr()); write_sofmod(64); // 1mS frame time - if (auto result = create_structures(); result.is_error()) - return result; + TRY(create_structures()); setup_schedule(); @@ -158,9 +144,7 @@ UNMAP_AFTER_INIT KResult UHCIController::create_structures() m_dummy_qh = allocate_queue_head(); // Now the Transfer Descriptor pool - auto maybe_td_pool_vmobject = Memory::AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE); - if (maybe_td_pool_vmobject.is_error()) - return maybe_td_pool_vmobject.error(); + auto td_pool_vmobject = TRY(Memory::AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE)); m_transfer_descriptor_pool = UHCIDescriptorPool::try_create("Transfer Descriptor Pool"); if (!m_transfer_descriptor_pool) { @@ -168,7 +152,7 @@ UNMAP_AFTER_INIT KResult UHCIController::create_structures() return ENOMEM; } - m_isochronous_transfer_pool = MM.allocate_kernel_region_with_vmobject(*maybe_td_pool_vmobject.release_value(), PAGE_SIZE, "UHCI Isochronous Descriptor Pool", Memory::Region::Access::ReadWrite); + m_isochronous_transfer_pool = MM.allocate_kernel_region_with_vmobject(move(td_pool_vmobject), PAGE_SIZE, "UHCI Isochronous Descriptor Pool", Memory::Region::Access::ReadWrite); if (!m_isochronous_transfer_pool) { dmesgln("UHCI: Failed to allocated Isochronous Descriptor Pool!"); return ENOMEM; @@ -293,15 +277,8 @@ KResult UHCIController::start() } dbgln("UHCI: Started"); - auto root_hub_or_error = UHCIRootHub::try_create(*this); - if (root_hub_or_error.is_error()) - return root_hub_or_error.error(); - - m_root_hub = root_hub_or_error.release_value(); - auto result = m_root_hub->setup({}); - if (result.is_error()) - return result; - + m_root_hub = TRY(UHCIRootHub::try_create(*this)); + TRY(m_root_hub->setup({})); return KSuccess; } @@ -416,8 +393,7 @@ KResultOr UHCIController::submit_control_transfer(Transfer& transfer) TransferDescriptor* last_data_descriptor; TransferDescriptor* data_descriptor_chain; auto buffer_address = Ptr32(transfer.buffer_physical().as_ptr() + sizeof(USBRequestData)); - if (auto result = create_chain(pipe, direction_in ? PacketID::IN : PacketID::OUT, buffer_address, pipe.max_packet_size(), transfer.transfer_data_size(), &data_descriptor_chain, &last_data_descriptor); result.is_error()) - return result; + TRY(create_chain(pipe, direction_in ? PacketID::IN : PacketID::OUT, buffer_address, pipe.max_packet_size(), transfer.transfer_data_size(), &data_descriptor_chain, &last_data_descriptor)); // Status TD always has toggle set to 1 pipe.set_toggle(true); diff --git a/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp b/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp index 00eac03269..4140815650 100644 --- a/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp +++ b/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp @@ -99,22 +99,14 @@ UHCIRootHub::UHCIRootHub(NonnullRefPtr uhci_controller) KResult UHCIRootHub::setup(Badge) { - auto hub_or_error = Hub::try_create_root_hub(m_uhci_controller, Device::DeviceSpeed::FullSpeed); - if (hub_or_error.is_error()) - return hub_or_error.error(); - - m_hub = hub_or_error.release_value(); + m_hub = TRY(Hub::try_create_root_hub(m_uhci_controller, Device::DeviceSpeed::FullSpeed)); // NOTE: The root hub will be on the default address at this point. // The root hub must be the first device to be created, otherwise the HCD will intercept all default address transfers as though they're targeted at the root hub. - auto result = m_hub->enumerate_device(); - if (result.is_error()) - return result; + TRY(m_hub->enumerate_device()); // NOTE: The root hub is no longer on the default address. - result = m_hub->enumerate_and_power_on_hub(); - if (result.is_error()) - return result; + TRY(m_hub->enumerate_and_power_on_hub()); return KSuccess; } @@ -219,9 +211,7 @@ KResultOr UHCIRootHub::handle_control_transfer(Transfer& transfer) return EINVAL; auto feature_selector = (HubFeatureSelector)request.value; - auto result = m_uhci_controller->set_port_feature({}, port - 1, feature_selector); - if (result.is_error()) - return result.error(); + TRY(m_uhci_controller->set_port_feature({}, port - 1, feature_selector)); break; } case HubRequest::CLEAR_FEATURE: { @@ -246,9 +236,7 @@ KResultOr UHCIRootHub::handle_control_transfer(Transfer& transfer) return EINVAL; auto feature_selector = (HubFeatureSelector)request.value; - auto result = m_uhci_controller->clear_port_feature({}, port - 1, feature_selector); - if (result.is_error()) - return result.error(); + TRY(m_uhci_controller->clear_port_feature({}, port - 1, feature_selector)); break; } default: diff --git a/Kernel/Bus/USB/USBDevice.cpp b/Kernel/Bus/USB/USBDevice.cpp index 0f48152d98..4ad6d78608 100644 --- a/Kernel/Bus/USB/USBDevice.cpp +++ b/Kernel/Bus/USB/USBDevice.cpp @@ -18,17 +18,13 @@ namespace Kernel::USB { KResultOr> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed) { - auto pipe_or_error = Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0); - if (pipe_or_error.is_error()) - return pipe_or_error.error(); + auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0)); - auto device = try_make_ref_counted(controller, port, speed, pipe_or_error.release_value()); + auto device = try_make_ref_counted(controller, port, speed, move(pipe)); if (!device) return ENOMEM; - auto enumerate_result = device->enumerate_device(); - if (enumerate_result.is_error()) - return enumerate_result; + TRY(device->enumerate_device()); return device.release_nonnull(); } @@ -71,12 +67,7 @@ KResult Device::enumerate_device() // Send 8-bytes to get at least the `max_packet_size` from the device constexpr u8 short_device_descriptor_length = 8; - auto transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, short_device_descriptor_length, &dev_descriptor); - - if (transfer_length_or_error.is_error()) - return transfer_length_or_error.error(); - - auto transfer_length = transfer_length_or_error.release_value(); + auto transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, short_device_descriptor_length, &dev_descriptor)); // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected. if (transfer_length < short_device_descriptor_length) { @@ -99,12 +90,7 @@ KResult Device::enumerate_device() VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE); m_default_pipe->set_max_packet_size(dev_descriptor.max_packet_size); - transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, sizeof(USBDeviceDescriptor), &dev_descriptor); - - if (transfer_length_or_error.is_error()) - return transfer_length_or_error.error(); - - transfer_length = transfer_length_or_error.release_value(); + transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, sizeof(USBDeviceDescriptor), &dev_descriptor)); // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected. if (transfer_length < sizeof(USBDeviceDescriptor)) { @@ -127,10 +113,7 @@ KResult Device::enumerate_device() auto new_address = m_controller->allocate_address(); // Attempt to set devices address on the bus - transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, new_address, 0, 0, nullptr); - - if (transfer_length_or_error.is_error()) - return transfer_length_or_error.error(); + transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, new_address, 0, 0, nullptr)); // This has to be set after we send out the "Set Address" request because it might be sent to the root hub. // The root hub uses the address to intercept requests to itself. diff --git a/Kernel/Bus/USB/USBHub.cpp b/Kernel/Bus/USB/USBHub.cpp index d0c032f44c..7acea2d8da 100644 --- a/Kernel/Bus/USB/USBHub.cpp +++ b/Kernel/Bus/USB/USBHub.cpp @@ -15,11 +15,8 @@ namespace Kernel::USB { KResultOr> Hub::try_create_root_hub(NonnullRefPtr controller, DeviceSpeed device_speed) { - auto pipe_or_error = Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0); - if (pipe_or_error.is_error()) - return pipe_or_error.error(); - - auto hub = try_make_ref_counted(controller, device_speed, pipe_or_error.release_value()); + auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0)); + auto hub = try_make_ref_counted(controller, device_speed, move(pipe)); if (!hub) return ENOMEM; @@ -30,18 +27,11 @@ KResultOr> Hub::try_create_root_hub(NonnullRefPtr> Hub::try_create_from_device(Device const& device) { - auto pipe_or_error = Pipe::try_create_pipe(device.controller(), Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, device.device_descriptor().max_packet_size, device.address()); - if (pipe_or_error.is_error()) - return pipe_or_error.error(); - - auto hub = try_make_ref_counted(device, pipe_or_error.release_value()); + auto pipe = TRY(Pipe::try_create_pipe(device.controller(), Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, device.device_descriptor().max_packet_size, device.address())); + auto hub = try_make_ref_counted(device, move(pipe)); if (!hub) return ENOMEM; - - auto result = hub->enumerate_and_power_on_hub(); - if (result.is_error()) - return result; - + TRY(hub->enumerate_and_power_on_hub()); return hub.release_nonnull(); } @@ -70,13 +60,11 @@ KResult Hub::enumerate_and_power_on_hub() USBHubDescriptor descriptor {}; // Get the first hub descriptor. All hubs are required to have a hub descriptor at index 0. USB 2.0 Specification Section 11.24.2.5. - auto transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, HubRequest::GET_DESCRIPTOR, (DESCRIPTOR_TYPE_HUB << 8), 0, sizeof(USBHubDescriptor), &descriptor); - if (transfer_length_or_error.is_error()) - return transfer_length_or_error.error(); + auto transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, HubRequest::GET_DESCRIPTOR, (DESCRIPTOR_TYPE_HUB << 8), 0, sizeof(USBHubDescriptor), &descriptor)); // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected. - if (transfer_length_or_error.value() < sizeof(USBHubDescriptor)) { - dbgln("USB Hub: Unexpected hub descriptor size. Expected {}, got {}", sizeof(USBHubDescriptor), transfer_length_or_error.value()); + if (transfer_length < sizeof(USBHubDescriptor)) { + dbgln("USB Hub: Unexpected hub descriptor size. Expected {}, got {}", sizeof(USBHubDescriptor), transfer_length); return EIO; } @@ -112,13 +100,11 @@ KResult Hub::get_port_status(u8 port, HubStatus& hub_status) if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports) return EINVAL; - auto transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::GET_STATUS, 0, port, sizeof(HubStatus), &hub_status); - if (transfer_length_or_error.is_error()) - return transfer_length_or_error.error(); + auto transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::GET_STATUS, 0, port, sizeof(HubStatus), &hub_status)); // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected. - if (transfer_length_or_error.value() < sizeof(HubStatus)) { - dbgln("USB Hub: Unexpected hub status size. Expected {}, got {}.", sizeof(HubStatus), transfer_length_or_error.value()); + if (transfer_length < sizeof(HubStatus)) { + dbgln("USB Hub: Unexpected hub status size. Expected {}, got {}.", sizeof(HubStatus), transfer_length); return EIO; } @@ -132,10 +118,7 @@ KResult Hub::clear_port_feature(u8 port, HubFeatureSelector feature_selector) if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports) return EINVAL; - auto result = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::CLEAR_FEATURE, feature_selector, port, 0, nullptr); - if (result.is_error()) - return result.error(); - + TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::CLEAR_FEATURE, feature_selector, port, 0, nullptr)); return KSuccess; } @@ -146,10 +129,7 @@ KResult Hub::set_port_feature(u8 port, HubFeatureSelector feature_selector) if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports) return EINVAL; - auto result = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::SET_FEATURE, feature_selector, port, 0, nullptr); - if (result.is_error()) - return result.error(); - + TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::SET_FEATURE, feature_selector, port, 0, nullptr)); return KSuccess; } diff --git a/Kernel/Bus/USB/USBPipe.cpp b/Kernel/Bus/USB/USBPipe.cpp index b374ba80a5..db404c8446 100644 --- a/Kernel/Bus/USB/USBPipe.cpp +++ b/Kernel/Bus/USB/USBPipe.cpp @@ -69,12 +69,7 @@ KResultOr Pipe::control_transfer(u8 request_type, u8 request, u16 value, transfer->set_setup_packet(usb_request); dbgln_if(USB_DEBUG, "Pipe: Transfer allocated @ {}", transfer->buffer_physical()); - auto transfer_len_or_error = m_controller->submit_control_transfer(*transfer); - - if (transfer_len_or_error.is_error()) - return transfer_len_or_error.error(); - - auto transfer_length = transfer_len_or_error.release_value(); + auto transfer_length = TRY(m_controller->submit_control_transfer(*transfer)); // TODO: Check transfer for completion and copy data from transfer buffer into data if (length > 0)