UHCIController.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Jesse Buhagiar <jooster669@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Platform.h>
  28. #include <Kernel/Debug.h>
  29. #include <Kernel/Devices/USB/UHCIController.h>
  30. #include <Kernel/Process.h>
  31. #include <Kernel/StdLib.h>
  32. #include <Kernel/Time/TimeManagement.h>
  33. #include <Kernel/VM/AnonymousVMObject.h>
  34. #include <Kernel/VM/MemoryManager.h>
  35. static constexpr u8 MAXIMUM_NUMBER_OF_TDS = 128; // Upper pool limit. This consumes the second page we have allocated
  36. static constexpr u8 MAXIMUM_NUMBER_OF_QHS = 64;
  37. namespace Kernel::USB {
  38. static UHCIController* s_the;
  39. static constexpr u16 UHCI_USBCMD_RUN = 0x0001;
  40. static constexpr u16 UHCI_USBCMD_HOST_CONTROLLER_RESET = 0x0002;
  41. static constexpr u16 UHCI_USBCMD_GLOBAL_RESET = 0x0004;
  42. static constexpr u16 UHCI_USBCMD_ENTER_GLOBAL_SUSPEND_MODE = 0x0008;
  43. static constexpr u16 UHCI_USBCMD_FORCE_GLOBAL_RESUME = 0x0010;
  44. static constexpr u16 UHCI_USBCMD_SOFTWARE_DEBUG = 0x0020;
  45. static constexpr u16 UHCI_USBCMD_CONFIGURE_FLAG = 0x0040;
  46. static constexpr u16 UHCI_USBCMD_MAX_PACKET = 0x0080;
  47. static constexpr u16 UHCI_USBSTS_HOST_CONTROLLER_HALTED = 0x0020;
  48. static constexpr u16 UHCI_USBSTS_HOST_CONTROLLER_PROCESS_ERROR = 0x0010;
  49. static constexpr u16 UHCI_USBSTS_PCI_BUS_ERROR = 0x0008;
  50. static constexpr u16 UHCI_USBSTS_RESUME_RECEIVED = 0x0004;
  51. static constexpr u16 UHCI_USBSTS_USB_ERROR_INTERRUPT = 0x0002;
  52. static constexpr u16 UHCI_USBSTS_USB_INTERRUPT = 0x0001;
  53. static constexpr u8 UHCI_USBINTR_TIMEOUT_CRC_ENABLE = 0x01;
  54. static constexpr u8 UHCI_USBINTR_RESUME_INTR_ENABLE = 0x02;
  55. static constexpr u8 UHCI_USBINTR_IOC_ENABLE = 0x04;
  56. static constexpr u8 UHCI_USBINTR_SHORT_PACKET_INTR_ENABLE = 0x08;
  57. static constexpr u16 UHCI_FRAMELIST_FRAME_COUNT = 1024; // Each entry is 4 bytes in our allocated page
  58. static constexpr u16 UHCI_FRAMELIST_FRAME_INVALID = 0x0001;
  59. // Port stuff
  60. static constexpr u8 UHCI_ROOT_PORT_COUNT = 2;
  61. static constexpr u16 UHCI_PORTSC_CURRRENT_CONNECT_STATUS = 0x0001;
  62. static constexpr u16 UHCI_PORTSC_CONNECT_STATUS_CHANGED = 0x0002;
  63. static constexpr u16 UHCI_PORTSC_PORT_ENABLED = 0x0004;
  64. static constexpr u16 UHCI_PORTSC_PORT_ENABLE_CHANGED = 0x0008;
  65. static constexpr u16 UHCI_PORTSC_LINE_STATUS = 0x0030;
  66. static constexpr u16 UHCI_PORTSC_RESUME_DETECT = 0x40;
  67. static constexpr u16 UHCI_PORTSC_LOW_SPEED_DEVICE = 0x0100;
  68. static constexpr u16 UHCI_PORTSC_PORT_RESET = 0x0200;
  69. static constexpr u16 UHCI_PORTSC_SUSPEND = 0x1000;
  70. // *BSD and a few other drivers seem to use this number
  71. static constexpr u8 UHCI_NUMBER_OF_ISOCHRONOUS_TDS = 128;
  72. static constexpr u16 UHCI_NUMBER_OF_FRAMES = 1024;
  73. UHCIController& UHCIController::the()
  74. {
  75. return *s_the;
  76. }
  77. UNMAP_AFTER_INIT void UHCIController::detect()
  78. {
  79. PCI::enumerate([&](const PCI::Address& address, PCI::ID id) {
  80. if (address.is_null())
  81. return;
  82. if (PCI::get_class(address) == 0xc && PCI::get_subclass(address) == 0x03 && PCI::get_programming_interface(address) == 0) {
  83. if (!s_the)
  84. s_the = new UHCIController(address, id);
  85. }
  86. });
  87. }
  88. UNMAP_AFTER_INIT UHCIController::UHCIController(PCI::Address address, PCI::ID id)
  89. : PCI::Device(address)
  90. , m_io_base(PCI::get_BAR4(pci_address()) & ~1)
  91. {
  92. dmesgln("UHCI: Controller found {} @ {}", id, address);
  93. dmesgln("UHCI: I/O base {}", m_io_base);
  94. dmesgln("UHCI: Interrupt line: {}", PCI::get_interrupt_line(pci_address()));
  95. reset();
  96. start();
  97. spawn_port_proc();
  98. }
  99. UNMAP_AFTER_INIT UHCIController::~UHCIController()
  100. {
  101. }
  102. void UHCIController::reset()
  103. {
  104. stop();
  105. write_usbcmd(UHCI_USBCMD_HOST_CONTROLLER_RESET);
  106. // FIXME: Timeout
  107. for (;;) {
  108. if (read_usbcmd() & UHCI_USBCMD_HOST_CONTROLLER_RESET)
  109. continue;
  110. break;
  111. }
  112. // Let's allocate the physical page for the Frame List (which is 4KiB aligned)
  113. auto framelist_vmobj = ContiguousVMObject::create_with_size(PAGE_SIZE);
  114. m_framelist = MemoryManager::the().allocate_kernel_region_with_vmobject(*framelist_vmobj, PAGE_SIZE, "UHCI Framelist", Region::Access::Write);
  115. dbgln("UHCI: Allocated framelist at physical address {}", m_framelist->physical_page(0)->paddr());
  116. dbgln("UHCI: Framelist is at virtual address {}", m_framelist->vaddr());
  117. write_sofmod(64); // 1mS frame time
  118. create_structures();
  119. setup_schedule();
  120. write_flbaseadd(m_framelist->physical_page(0)->paddr().get()); // Frame list (physical) address
  121. write_frnum(0); // Set the initial frame number
  122. // Enable all interrupt types
  123. write_frnum(UHCI_USBINTR_TIMEOUT_CRC_ENABLE | UHCI_USBINTR_RESUME_INTR_ENABLE | UHCI_USBINTR_IOC_ENABLE | UHCI_USBINTR_SHORT_PACKET_INTR_ENABLE);
  124. dbgln("UHCI: Reset completed");
  125. }
  126. UNMAP_AFTER_INIT void UHCIController::create_structures()
  127. {
  128. // Let's allocate memory for botht the QH and TD pools
  129. // First the QH pool and all of the Interrupt QH's
  130. auto qh_pool_vmobject = ContiguousVMObject::create_with_size(2 * PAGE_SIZE);
  131. m_qh_pool = MemoryManager::the().allocate_kernel_region_with_vmobject(*qh_pool_vmobject, 2 * PAGE_SIZE, "UHCI Queue Head Pool", Region::Access::Write);
  132. memset(m_qh_pool->vaddr().as_ptr(), 0, 2 * PAGE_SIZE); // Zero out both pages
  133. // Let's populate our free qh list (so we have some we can allocate later on)
  134. m_free_qh_pool.resize(MAXIMUM_NUMBER_OF_TDS);
  135. for (size_t i = 0; i < m_free_qh_pool.size(); i++) {
  136. auto placement_addr = reinterpret_cast<void*>(m_qh_pool->vaddr().get() + (i * sizeof(QueueHead)));
  137. auto paddr = static_cast<u32>(m_qh_pool->physical_page(0)->paddr().get() + (i * sizeof(QueueHead)));
  138. m_free_qh_pool.at(i) = new (placement_addr) QueueHead(paddr);
  139. }
  140. // Create the Full Speed, Low Speed Control and Bulk Queue Heads
  141. m_interrupt_transfer_queue = allocate_queue_head();
  142. m_lowspeed_control_qh = allocate_queue_head();
  143. m_fullspeed_control_qh = allocate_queue_head();
  144. m_bulk_qh = allocate_queue_head();
  145. m_dummy_qh = allocate_queue_head();
  146. // Now the Transfer Descriptor pool
  147. auto td_pool_vmobject = ContiguousVMObject::create_with_size(2 * PAGE_SIZE);
  148. m_td_pool = MemoryManager::the().allocate_kernel_region_with_vmobject(*td_pool_vmobject, 2 * PAGE_SIZE, "UHCI Transfer Descriptor Pool", Region::Access::Write);
  149. memset(m_td_pool->vaddr().as_ptr(), 0, 2 * PAGE_SIZE);
  150. // Set up the Isochronous Transfer Descriptor list
  151. m_iso_td_list.resize(UHCI_NUMBER_OF_ISOCHRONOUS_TDS);
  152. for (size_t i = 0; i < m_iso_td_list.size(); i++) {
  153. auto placement_addr = reinterpret_cast<void*>(m_td_pool->vaddr().get() + (i * sizeof(Kernel::USB::TransferDescriptor)));
  154. auto paddr = static_cast<u32>(m_td_pool->physical_page(0)->paddr().get() + (i * sizeof(Kernel::USB::TransferDescriptor)));
  155. // Place a new Transfer Descriptor with a 1:1 in our region
  156. // The pointer returned by `new()` lines up exactly with the value
  157. // that we store in `paddr`, meaning our member functions directly
  158. // access the raw descriptor (that we later send to the controller)
  159. m_iso_td_list.at(i) = new (placement_addr) Kernel::USB::TransferDescriptor(paddr);
  160. auto transfer_descriptor = m_iso_td_list.at(i);
  161. transfer_descriptor->set_in_use(true); // Isochronous transfers are ALWAYS marked as in use (in case we somehow get allocated one...)
  162. transfer_descriptor->set_isochronous();
  163. transfer_descriptor->link_queue_head(m_interrupt_transfer_queue->paddr());
  164. #if UHCI_VERBOSE_DEBUG
  165. transfer_descriptor->print();
  166. #endif
  167. }
  168. m_free_td_pool.resize(MAXIMUM_NUMBER_OF_TDS);
  169. for (size_t i = 0; i < m_free_td_pool.size(); i++) {
  170. auto placement_addr = reinterpret_cast<void*>(m_td_pool->vaddr().offset(PAGE_SIZE).get() + (i * sizeof(Kernel::USB::TransferDescriptor)));
  171. auto paddr = static_cast<u32>(m_td_pool->physical_page(1)->paddr().get() + (i * sizeof(Kernel::USB::TransferDescriptor)));
  172. // Place a new Transfer Descriptor with a 1:1 in our region
  173. // The pointer returned by `new()` lines up exactly with the value
  174. // that we store in `paddr`, meaning our member functions directly
  175. // access the raw descriptor (that we later send to the controller)
  176. m_free_td_pool.at(i) = new (placement_addr) Kernel::USB::TransferDescriptor(paddr);
  177. #if UHCI_VERBOSE_DEBUG
  178. auto transfer_descriptor = m_free_td_pool.at(i);
  179. transfer_descriptor->print();
  180. #endif
  181. }
  182. if constexpr (UHCI_DEBUG) {
  183. dbgln("UHCI: Pool information:");
  184. dbgln(" qh_pool: {}, length: {}", PhysicalAddress(m_qh_pool->physical_page(0)->paddr()), m_qh_pool->range().size());
  185. dbgln(" td_pool: {}, length: {}", PhysicalAddress(m_td_pool->physical_page(0)->paddr()), m_td_pool->range().size());
  186. }
  187. }
  188. UNMAP_AFTER_INIT void UHCIController::setup_schedule()
  189. {
  190. //
  191. // https://github.com/alkber/minix3-usbsubsystem/blob/master/usb/uhci-hcd.c
  192. //
  193. // This lad probably has the best explanation as to how this is actually done. I'll try and
  194. // explain it here to so that there's no need for anyone to go hunting for this shit again, because
  195. // the USB spec and Intel explain next to nothing.
  196. // According to the USB spec (and the UHCI datasheet), 90% of the bandwidth should be used for
  197. // Isochronous and """Interrupt""" related transfers, with the rest being used for control and bulk
  198. // transfers.
  199. // That is, most of the time, the schedule is going to be executing either an Isochronous transfer
  200. // in our framelist, or an Interrupt transfer. The allocation in `create_structures` reflects this.
  201. //
  202. // Each frame has it's own Isochronous transfer Transfer Descriptor(s) that point to each other
  203. // horizontally in the list. The end of these transfers then point to the Interrupt Queue Headers,
  204. // in which we can attach Transfer Descriptors (related to Interrupt Transfers). These are attached
  205. // to the Queue Head _vertically_. We need to ensure that these are executed every 8ms, so they are inserted
  206. // at different points in the schedule (TODO: How do we do this?!?!). After the Interrupt Transfer Queue Heads,
  207. // we attach the Control Queue Heads. We need two in total, one for Low Speed devices, and one for Full Speed
  208. // USB devices. Finally, we attach the Bulk Transfer Queue Head.
  209. // Not specified in the datasheet, however, is another Queue Head with an "inactive" Transfer Descriptor. This
  210. // is to circumvent a bug in the silicon of the PIIX4's UHCI controller.
  211. // https://github.com/openbsd/src/blob/master/sys/dev/usb/uhci.c#L390
  212. //
  213. m_interrupt_transfer_queue->link_next_queue_head(m_lowspeed_control_qh);
  214. m_interrupt_transfer_queue->terminate_element_link_ptr();
  215. m_lowspeed_control_qh->link_next_queue_head(m_fullspeed_control_qh);
  216. m_lowspeed_control_qh->terminate_element_link_ptr();
  217. m_fullspeed_control_qh->link_next_queue_head(m_bulk_qh);
  218. m_fullspeed_control_qh->terminate_element_link_ptr();
  219. m_bulk_qh->link_next_queue_head(m_dummy_qh);
  220. m_bulk_qh->terminate_element_link_ptr();
  221. auto piix4_td_hack = allocate_transfer_descriptor();
  222. piix4_td_hack->terminate();
  223. piix4_td_hack->set_max_len(0x7ff); // Null data packet
  224. piix4_td_hack->set_device_address(0x7f);
  225. piix4_td_hack->set_packet_id(PacketID::IN);
  226. m_dummy_qh->terminate_with_stray_descriptor(piix4_td_hack);
  227. m_dummy_qh->terminate_element_link_ptr();
  228. u32* framelist = reinterpret_cast<u32*>(m_framelist->vaddr().as_ptr());
  229. for (int frame = 0; frame < UHCI_NUMBER_OF_FRAMES; frame++) {
  230. // Each frame pointer points to iso_td % NUM_ISO_TDS
  231. framelist[frame] = m_iso_td_list.at(frame % UHCI_NUMBER_OF_ISOCHRONOUS_TDS)->paddr();
  232. }
  233. m_interrupt_transfer_queue->print();
  234. m_lowspeed_control_qh->print();
  235. m_fullspeed_control_qh->print();
  236. m_bulk_qh->print();
  237. m_dummy_qh->print();
  238. }
  239. QueueHead* UHCIController::allocate_queue_head() const
  240. {
  241. for (QueueHead* queue_head : m_free_qh_pool) {
  242. if (!queue_head->in_use()) {
  243. queue_head->set_in_use(true);
  244. dbgln_if(UHCI_DEBUG, "UHCI: Allocated a new Queue Head! Located @ {} ({})", VirtualAddress(queue_head), PhysicalAddress(queue_head->paddr()));
  245. return queue_head;
  246. }
  247. }
  248. VERIFY_NOT_REACHED(); // Let's just assert for now, this should never happen
  249. return nullptr; // Huh!? We're outta queue heads!
  250. }
  251. TransferDescriptor* UHCIController::allocate_transfer_descriptor() const
  252. {
  253. for (TransferDescriptor* transfer_descriptor : m_free_td_pool) {
  254. if (!transfer_descriptor->in_use()) {
  255. transfer_descriptor->set_in_use(true);
  256. dbgln_if(UHCI_DEBUG, "UHCI: Allocated a new Transfer Descriptor! Located @ {} ({})", VirtualAddress(transfer_descriptor), PhysicalAddress(transfer_descriptor->paddr()));
  257. return transfer_descriptor;
  258. }
  259. }
  260. VERIFY_NOT_REACHED(); // Let's just assert for now, this should never happen
  261. return nullptr; // Huh?! We're outta TDs!!
  262. }
  263. void UHCIController::stop()
  264. {
  265. write_usbcmd(read_usbcmd() & ~UHCI_USBCMD_RUN);
  266. // FIXME: Timeout
  267. for (;;) {
  268. if (read_usbsts() & UHCI_USBSTS_HOST_CONTROLLER_HALTED)
  269. break;
  270. }
  271. }
  272. void UHCIController::start()
  273. {
  274. write_usbcmd(read_usbcmd() | UHCI_USBCMD_RUN);
  275. // FIXME: Timeout
  276. for (;;) {
  277. if (!(read_usbsts() & UHCI_USBSTS_HOST_CONTROLLER_HALTED))
  278. break;
  279. }
  280. dbgln("UHCI: Started");
  281. }
  282. struct setup_packet {
  283. u8 bmRequestType;
  284. u8 bRequest;
  285. u16 wValue;
  286. u16 wIndex;
  287. u16 wLength;
  288. };
  289. void UHCIController::do_debug_transfer()
  290. {
  291. dbgln("UHCI: Attempting a dummy transfer...");
  292. // Okay, let's set up the buffer so we can write some data
  293. auto vmobj = ContiguousVMObject::create_with_size(PAGE_SIZE);
  294. m_td_buffer_region = MemoryManager::the().allocate_kernel_region_with_vmobject(*vmobj, PAGE_SIZE, "UHCI Debug Data Region", Region::Access::Write);
  295. // We need to set up THREE Transfer descriptors here
  296. // 1. The SETUP packet TD
  297. // 2. The DATA packet
  298. // 3. The ACK TD that will be filled by the device
  299. // We can use the buffer pool provided above to do this, using nasty pointer offsets!
  300. auto setup_td = allocate_transfer_descriptor();
  301. auto data_td = allocate_transfer_descriptor();
  302. auto response_td = allocate_transfer_descriptor();
  303. dbgln("BUFFER PHYSICAL ADDRESS = {}", m_td_buffer_region->physical_page(0)->paddr());
  304. setup_packet* packet = reinterpret_cast<setup_packet*>(m_td_buffer_region->vaddr().as_ptr());
  305. packet->bmRequestType = 0x81;
  306. packet->bRequest = 0x06;
  307. packet->wValue = 0x2200;
  308. packet->wIndex = 0x0;
  309. packet->wLength = 8;
  310. // Let's begin....
  311. setup_td->set_status(0x18800000);
  312. setup_td->set_token(0x00E0002D);
  313. setup_td->set_buffer_address(m_td_buffer_region->physical_page(0)->paddr().get());
  314. data_td->set_status(0x18800000);
  315. data_td->set_token(0x00E80069);
  316. data_td->set_buffer_address(m_td_buffer_region->physical_page(0)->paddr().get() + 16);
  317. response_td->set_status(0x19800000);
  318. response_td->set_token(0xFFE800E1);
  319. setup_td->insert_next_transfer_descriptor(data_td);
  320. data_td->insert_next_transfer_descriptor(response_td);
  321. response_td->terminate();
  322. setup_td->print();
  323. data_td->print();
  324. response_td->print();
  325. // Now let's (attempt) to attach to one of the queue heads
  326. m_lowspeed_control_qh->attach_transfer_descriptor_chain(setup_td);
  327. }
  328. void UHCIController::spawn_port_proc()
  329. {
  330. RefPtr<Thread> usb_hotplug_thread;
  331. Process::create_kernel_process(usb_hotplug_thread, "UHCIHotplug", [&] {
  332. for (;;) {
  333. for (int port = 0; port < UHCI_ROOT_PORT_COUNT; port++) {
  334. u16 port_data = 0;
  335. if (port == 1) {
  336. // Let's see what's happening on port 1
  337. // Current status
  338. port_data = read_portsc1();
  339. if (port_data & UHCI_PORTSC_CONNECT_STATUS_CHANGED) {
  340. if (port_data & UHCI_PORTSC_CURRRENT_CONNECT_STATUS) {
  341. dmesgln("UHCI: Device attach detected on Root Port 1!");
  342. // Reset the port
  343. port_data = read_portsc1();
  344. write_portsc1(port_data | UHCI_PORTSC_PORT_RESET);
  345. for (size_t i = 0; i < 50000; ++i)
  346. IO::in8(0x80);
  347. write_portsc1(port_data & ~UHCI_PORTSC_PORT_RESET);
  348. for (size_t i = 0; i < 100000; ++i)
  349. IO::in8(0x80);
  350. write_portsc1(port_data & (~UHCI_PORTSC_PORT_ENABLE_CHANGED | ~UHCI_PORTSC_CONNECT_STATUS_CHANGED));
  351. } else {
  352. dmesgln("UHCI: Device detach detected on Root Port 1!");
  353. }
  354. port_data = read_portsc1();
  355. write_portsc1(port_data | UHCI_PORTSC_PORT_ENABLED);
  356. dbgln("port should be enabled now: {:#04x}\n", read_portsc1());
  357. do_debug_transfer();
  358. }
  359. } else {
  360. port_data = UHCIController::the().read_portsc2();
  361. if (port_data & UHCI_PORTSC_CONNECT_STATUS_CHANGED) {
  362. if (port_data & UHCI_PORTSC_CURRRENT_CONNECT_STATUS) {
  363. dmesgln("UHCI: Device attach detected on Root Port 2!");
  364. } else {
  365. dmesgln("UHCI: Device detach detected on Root Port 2!");
  366. }
  367. UHCIController::the().write_portsc2(
  368. UHCI_PORTSC_CONNECT_STATUS_CHANGED);
  369. }
  370. }
  371. }
  372. (void)Thread::current()->sleep(Time::from_seconds(1));
  373. }
  374. });
  375. }
  376. void UHCIController::handle_irq(const RegisterState&)
  377. {
  378. // Shared IRQ. Not ours!
  379. if (!read_usbsts())
  380. return;
  381. if constexpr (UHCI_DEBUG) {
  382. dbgln("UHCI: Interrupt happened!");
  383. dbgln("Value of USBSTS: {:#04x}", read_usbsts());
  384. }
  385. }
  386. }