USBTransfer.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Bus/USB/USBTransfer.h>
  7. #include <Kernel/VM/MemoryManager.h>
  8. namespace Kernel::USB {
  9. RefPtr<Transfer> Transfer::try_create(Pipe& pipe, u16 len)
  10. {
  11. auto vmobject = AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE);
  12. if (!vmobject)
  13. return nullptr;
  14. return AK::try_create<Transfer>(pipe, len, *vmobject);
  15. }
  16. Transfer::Transfer(Pipe& pipe, u16 len, AnonymousVMObject& vmobject)
  17. : m_pipe(pipe)
  18. , m_transfer_data_size(len)
  19. {
  20. // Initialize data buffer for transfer
  21. // This will definitely need to be refactored in the future, I doubt this will scale well...
  22. m_data_buffer = MemoryManager::the().allocate_kernel_region_with_vmobject(vmobject, PAGE_SIZE, "USB Transfer Buffer", Region::Access::Read | Region::Access::Write);
  23. }
  24. Transfer::~Transfer()
  25. {
  26. }
  27. void Transfer::set_setup_packet(const USBRequestData& request)
  28. {
  29. // Kind of a nasty hack... Because the kernel isn't in the business
  30. // of handing out physical pointers that we can directly write to,
  31. // we set the address of the setup packet to be the first 8 bytes of
  32. // the data buffer, which we then set to the physical address.
  33. auto* request_data = reinterpret_cast<USBRequestData*>(buffer().as_ptr());
  34. request_data->request_type = request.request_type;
  35. request_data->request = request.request;
  36. request_data->value = request.value;
  37. request_data->index = request.index;
  38. request_data->length = request.length;
  39. m_request = request;
  40. }
  41. }