-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff8876f
commit 5a180dc
Showing
5 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/****************************************************************************** | ||
* * | ||
* Copyright (C) 2024 MachineWare GmbH * | ||
* All Rights Reserved * | ||
* * | ||
* This is work is licensed under the terms described in the LICENSE file * | ||
* found in the root directory of this source tree. * | ||
* * | ||
******************************************************************************/ | ||
|
||
#ifndef VCML_USB_HOSTDEV_H | ||
#define VCML_USB_HOSTDEV_H | ||
|
||
#include "vcml/core/types.h" | ||
#include "vcml/core/systemc.h" | ||
#include "vcml/core/module.h" | ||
#include "vcml/core/model.h" | ||
|
||
#include "vcml/protocols/usb.h" | ||
#include "vcml/models/usb/device.h" | ||
|
||
struct libusb_context; | ||
struct libusb_device; | ||
struct libusb_device_handle; | ||
|
||
namespace vcml { | ||
namespace usb { | ||
|
||
class hostdev : public device | ||
{ | ||
private: | ||
struct libusb_device* m_device; | ||
struct libusb_device_handle* m_handle; | ||
|
||
public: | ||
property<u32> hostbus; | ||
property<u32> hostaddr; | ||
|
||
usb_target_socket usb_in; | ||
|
||
hostdev(const sc_module_name& nm); | ||
virtual ~hostdev(); | ||
VCML_KIND(usb::hostdev); | ||
|
||
protected: | ||
virtual usb_result get_data(u32 ep, u8* data, size_t len) override; | ||
virtual usb_result set_data(u32 ep, const u8* data, size_t len) override; | ||
|
||
virtual usb_result handle_control(u16 req, u16 val, u16 idx, u8* data, | ||
size_t length) override; | ||
|
||
virtual void usb_reset_device() override; | ||
}; | ||
|
||
} // namespace usb | ||
} // namespace vcml | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/****************************************************************************** | ||
* * | ||
* Copyright (C) 2024 MachineWare GmbH * | ||
* All Rights Reserved * | ||
* * | ||
* This is work is licensed under the terms described in the LICENSE file * | ||
* found in the root directory of this source tree. * | ||
* * | ||
******************************************************************************/ | ||
|
||
#include "vcml/models/usb/hostdev.h" | ||
#include "vcml/core/version.h" | ||
|
||
#include <libusb.h> | ||
|
||
namespace vcml { | ||
namespace usb { | ||
|
||
struct libusb { | ||
libusb_context* context; | ||
libusb(): context(nullptr) { | ||
int r = libusb_init(&context); | ||
VCML_ERROR_ON(r < 0, "failed to initialize libusb"); | ||
} | ||
|
||
~libusb() { | ||
if (context) | ||
libusb_exit(context); | ||
} | ||
|
||
static libusb& instance() { | ||
static libusb singleton; | ||
return singleton; | ||
} | ||
|
||
libusb_device* find_device(u32 bus, u32 addr) { | ||
libusb_device* found = nullptr; | ||
libusb_device** list = nullptr; | ||
size_t n = libusb_get_device_list(context, &list); | ||
for (size_t i = 0; i < n; i++) { | ||
if (libusb_get_bus_number(list[i]) == bus && | ||
libusb_get_device_address(list[i]) == addr) { | ||
found = libusb_ref_device(list[i]); | ||
break; | ||
} | ||
} | ||
|
||
libusb_free_device_list(list, true); | ||
return found; | ||
} | ||
}; | ||
|
||
hostdev::hostdev(const sc_module_name& nm): | ||
device(nm, device_desc()), | ||
m_device(), | ||
m_handle(), | ||
hostbus("hostbus", 0), | ||
hostaddr("hostaddr", 0), | ||
usb_in("usb_in") { | ||
m_device = libusb::instance().find_device(hostbus, hostaddr); | ||
VCML_ERROR_ON(!m_device, "no usb device on bus %u at address %u", | ||
hostbus.get(), hostaddr.get()); | ||
int r = libusb_open(m_device, &m_handle); | ||
VCML_ERROR_ON(r, "Failed to open USB device: %s", libusb_strerror(r)); | ||
} | ||
|
||
hostdev::~hostdev() { | ||
if (m_device) | ||
libusb_unref_device(m_device); | ||
} | ||
|
||
usb_result hostdev::get_data(u32 ep, u8* data, size_t len) { | ||
return USB_RESULT_NACK; | ||
} | ||
|
||
usb_result hostdev::set_data(u32 ep, const u8* data, size_t len) { | ||
return USB_RESULT_NACK; | ||
} | ||
|
||
usb_result hostdev::handle_control(u16 req, u16 val, u16 idx, u8* data, | ||
size_t length) { | ||
switch (req) { | ||
default: | ||
return device::handle_control(req, val, idx, data, length); | ||
} | ||
} | ||
|
||
void hostdev::usb_reset_device() { | ||
device::usb_reset_device(); | ||
} | ||
|
||
VCML_EXPORT_MODEL(vcml::usb::hostdev, name, args) { | ||
return new hostdev(name); | ||
} | ||
|
||
} // namespace usb | ||
} // namespace vcml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/****************************************************************************** | ||
* * | ||
* Copyright (C) 2024 MachineWare GmbH * | ||
* All Rights Reserved * | ||
* * | ||
* This is work is licensed under the terms described in the LICENSE file * | ||
* found in the root directory of this source tree. * | ||
* * | ||
******************************************************************************/ | ||
|
||
#include "vcml/models/usb/hostdev.h" | ||
#include "vcml/core/version.h" | ||
|
||
#include <libusb.h> | ||
|
||
namespace vcml { | ||
namespace usb { | ||
|
||
hostdev::hostdev(const sc_module_name& nm): | ||
device(nm, device_desc()), | ||
m_device(), | ||
m_handle(), | ||
hostbus("hostbus", 0), | ||
hostaddr("hostaddr", 0), | ||
usb_in("usb_in") { | ||
} | ||
|
||
hostdev::~hostdev() { | ||
// nothing to do | ||
} | ||
|
||
usb_result hostdev::get_data(u32 ep, u8* data, size_t len) { | ||
return USB_RESULT_NACK; | ||
} | ||
|
||
usb_result hostdev::set_data(u32 ep, const u8* data, size_t len) { | ||
return USB_RESULT_NACK; | ||
} | ||
|
||
usb_result hostdev::handle_control(u16 req, u16 val, u16 idx, u8* data, | ||
size_t length) { | ||
switch (req) { | ||
default: | ||
return device::handle_control(req, val, idx, data, length); | ||
} | ||
} | ||
|
||
void hostdev::usb_reset_device() { | ||
device::usb_reset_device(); | ||
} | ||
|
||
VCML_EXPORT_MODEL(vcml::usb::hostdev, name, args) { | ||
return new hostdev(name); | ||
} | ||
|
||
} // namespace usb | ||
} // namespace vcml |