Fork for kernel 5.18 API change.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

137 lines
3.7 KiB

#include "api.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/poll.h>
#include <pthread.h>
/*************************************************************************************************/
void UltraSoundDevice::sanityCheck(uint32_t address, uint32_t bar)
{
if(bar >= BAR_NUM)
{
throw(myexception("Wrong bar number"));
}
if(address >= bars[bar].size)
{
throw(myexception("Bar overflow"));
}
}
/*************************************************************************************************/
UltraSoundDevice::UltraSoundDevice()
{
#if 0
bars[0].size = 64 * 1024;
bars[1].size = 128 * 1024 * 1024;
bars[2].size = 8 * 1024;
#else
bars[0].size = 32 * 1024 * 1024;
bars[1].size = 128 * 1024 * 1024;
bars[2].size = 16 * 1024;
#endif
for(int i = 0; i < BAR_NUM; i++)
{
char str[64];
sprintf(str, "%s_%d", "/proc/usd_reg", i);
bars[i].file = open(str, O_RDWR, 0);
bars[i].ptr = mmap(NULL, bars[i].size, PROT_READ | PROT_WRITE, MAP_SHARED, bars[i].file , 0);
}
}
/*************************************************************************************************/
UltraSoundDevice::~UltraSoundDevice()
{
for(int i = 0; i < BAR_NUM; i++)
{
munmap(bars[i].ptr, bars[i].size);
close(bars[i].file);
}
}
/*************************************************************************************************/
uint8_t UltraSoundDevice::readByte(uint32_t address, uint32_t bar)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint8_t*>(bars[bar].ptr);
return ptr[address];
}
/*************************************************************************************************/
uint16_t UltraSoundDevice::readShort(uint32_t address, uint32_t bar)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint16_t*>(bars[bar].ptr);
return ptr[address];
}
/*************************************************************************************************/
uint32_t UltraSoundDevice::readWord(uint32_t address, uint32_t bar)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint32_t*>(bars[bar].ptr);
return ptr[address / 4];
}
/*************************************************************************************************/
uint64_t UltraSoundDevice::readLong(uint32_t address, uint32_t bar)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint64_t*>(bars[bar].ptr);
return ptr[address];
}
/*************************************************************************************************/
void UltraSoundDevice::writeByte(uint32_t address, uint32_t bar, uint8_t data)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint8_t*>(bars[bar].ptr);
ptr[address] = data;
}
/*************************************************************************************************/
void UltraSoundDevice::writeShort(uint32_t address, uint32_t bar, uint16_t data)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint16_t*>(bars[bar].ptr);
ptr[address / 2] = data;
}
/*************************************************************************************************/
void UltraSoundDevice::writeWord(uint32_t address, uint32_t bar, uint32_t data)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint32_t*>(bars[bar].ptr);
ptr[address / 4] = data;
}
/*************************************************************************************************/
void UltraSoundDevice::writeLong(uint32_t address, uint32_t bar, uint64_t data)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint64_t*>(bars[bar].ptr);
ptr[address / 8] = data;
}