setos

拙OS
Log | Files | Refs | LICENSE

commit adf31829198928d5fc8aa93a0d6356167b25ea36
parent d35df0faa7faebfd81090978334c5362b167477d
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 24 Apr 2024 09:07:25 +0900

add endian converters.

need to cleanup the code to convert xhci's little endian to
the machine's one

Diffstat:
Msys/include/libc.h | 4++++
Msys/include/xhc.h | 8++++++++
Msys/src/libc/endian.c | 40++++++++++++++++++++++++++++++++++++++++
3 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/sys/include/libc.h b/sys/include/libc.h @@ -33,8 +33,12 @@ extern Memmap memmap; void *malloc(uintptr); void *memset(void *s, int c, uintptr n); +uint16 endian_little_to_native16(uint16); +uint16 endian_native_to_little16(uint16); uint32 endian_little_to_native32(uint32); uint32 endian_native_to_little32(uint32); +uint64 endian_little_to_native64(uint64); +uint64 endian_native_to_little64(uint64); /* typedef char *va_list; diff --git a/sys/include/xhc.h b/sys/include/xhc.h @@ -7,6 +7,7 @@ typedef struct xhc_doorbell_registers xhc_doorbell_registers; typedef struct xhc_device_context xhc_device_context; typedef struct xhc_slot_context xhc_slot_context; typedef struct xhc_endpoint_context xhc_endpoint_context; +typedef struct xhc_trb xhc_trb; typedef struct Xhc { uintptr base; @@ -72,3 +73,9 @@ struct xhc_device_context { xhc_slot_context slot_context; xhc_endpoint_context ep_context[31]; }; + +struct xhc_trb { + uint64 parameter; + uint32 status; + uint32 control; +}; +\ No newline at end of file diff --git a/sys/src/libc/endian.c b/sys/src/libc/endian.c @@ -1,5 +1,21 @@ #include <libc.h> +uint16 +endian_little_to_native16(uint16 n) +{ + uint8 *m = (uint8 *) &n; + return (uint16) m[0] | (uint16) m[1] << 8; +} + +uint16 +endian_native_to_little16(uint16 n) +{ + uint8 m[4]; + m[0] = (uint8) n&0x000000ff; + m[1] = (uint8) ((n&0x0000ff00) >> 8); + return *(uint16 *)m; +} + uint32 endian_little_to_native32(uint32 n) { @@ -17,3 +33,27 @@ endian_native_to_little32(uint32 n) m[3] = (uint8) ((n&0xff000000) >> 24); return *(uint32 *)m; } + +uint64 +endian_little_to_native64(uint64 n) +{ + uint8 *m = (uint8 *) &n; + return (uint64) m[0] | (uint64) m[1] << 8 | (uint64) m[2] << 16 | + (uint64) m[3] << 24 | (uint64) m[4] << 32 | (uint64) m[5] << 40 | + (uint64) m[6] << 48 | (uint64) m[7] << 56; +} + +uint64 +endian_native_to_little64(uint64 n) +{ + uint8 m[4]; + m[0] = (uint8) n&0x00000000000000ff; + m[1] = (uint8) ((n&0x000000000000ff00) >> 8); + m[2] = (uint8) ((n&0x0000000000ff0000) >> 16); + m[3] = (uint8) ((n&0x00000000ff000000) >> 24); + m[4] = (uint8) ((n&0x000000ff00000000) >> 32); + m[5] = (uint8) ((n&0x0000ff0000000000) >> 40); + m[6] = (uint8) ((n&0x00ff000000000000) >> 48); + m[7] = (uint8) ((n&0xff00000000000000) >> 56); + return *(uint64 *)m; +}