diff --git a/src/userland/hid/Makefile b/src/userland/hid/Makefile new file mode 100644 index 0000000..462034d --- /dev/null +++ b/src/userland/hid/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = hid + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/hid/Makefile b/src/userland/hid/Makefile new file mode 100644 index 0000000..462034d --- /dev/null +++ b/src/userland/hid/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = hid + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/hid/main.c b/src/userland/hid/main.c new file mode 100644 index 0000000..bbf449d --- /dev/null +++ b/src/userland/hid/main.c @@ -0,0 +1,56 @@ +#define ALLOC_MAIN +#include + +REQUEST(checkFocus, "ioManager", "checkFocus"); +REQUEST(moveRelative, "mouse", "moveRelative"); +REQUEST(updateButtons, "mouse", "updateButtons"); + +ListElement *hidDevices = NULL; + +typedef struct { + uint32_t serviceId; + uint32_t deviceId; + uint32_t normalFunction; + void *buffer; +} HIDDevice; + +typedef struct { + uint8_t buttons; + int8_t x, y; +} __attribute__((packed)) MouseReport; + +void hidListening(HIDDevice *device) { + while (1) { + request(device->serviceId, device->normalFunction, device->deviceId, getPhysicalAddress(device->buffer)); + MouseReport *report = device->buffer; + moveRelative((int32_t) report->x, (int32_t)report->y); + updateButtons(report->buttons, 0); + // todo: sleep for at least endpoint->interval? + // todo: start this loop in a fork? + sleep(10); + } +} + +uint32_t registerHID(uint32_t usbDevice, uint32_t _, uint32_t serviceName, uint32_t serviceId) { + HIDDevice *device = malloc(sizeof(HIDDevice)); + device->serviceId = serviceId; + device->deviceId = usbDevice; // USB calls this a interface, others may differ + device->buffer = malloc(0x1000); + device->normalFunction = getFunction(serviceId, "hid_normal"); + fork(hidListening, device, 0, 0); +} + +void initialize() { + createFunction("registerHID", (void *)registerHID); +} + +int32_t main() { + static bool initialized = false; + if (!initialized) { + initialized = true; + initialize(); + } + if (!checkFocus(0, 0)) { + return 0; + } +} diff --git a/src/userland/hid/Makefile b/src/userland/hid/Makefile new file mode 100644 index 0000000..462034d --- /dev/null +++ b/src/userland/hid/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = hid + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/hid/main.c b/src/userland/hid/main.c new file mode 100644 index 0000000..bbf449d --- /dev/null +++ b/src/userland/hid/main.c @@ -0,0 +1,56 @@ +#define ALLOC_MAIN +#include + +REQUEST(checkFocus, "ioManager", "checkFocus"); +REQUEST(moveRelative, "mouse", "moveRelative"); +REQUEST(updateButtons, "mouse", "updateButtons"); + +ListElement *hidDevices = NULL; + +typedef struct { + uint32_t serviceId; + uint32_t deviceId; + uint32_t normalFunction; + void *buffer; +} HIDDevice; + +typedef struct { + uint8_t buttons; + int8_t x, y; +} __attribute__((packed)) MouseReport; + +void hidListening(HIDDevice *device) { + while (1) { + request(device->serviceId, device->normalFunction, device->deviceId, getPhysicalAddress(device->buffer)); + MouseReport *report = device->buffer; + moveRelative((int32_t) report->x, (int32_t)report->y); + updateButtons(report->buttons, 0); + // todo: sleep for at least endpoint->interval? + // todo: start this loop in a fork? + sleep(10); + } +} + +uint32_t registerHID(uint32_t usbDevice, uint32_t _, uint32_t serviceName, uint32_t serviceId) { + HIDDevice *device = malloc(sizeof(HIDDevice)); + device->serviceId = serviceId; + device->deviceId = usbDevice; // USB calls this a interface, others may differ + device->buffer = malloc(0x1000); + device->normalFunction = getFunction(serviceId, "hid_normal"); + fork(hidListening, device, 0, 0); +} + +void initialize() { + createFunction("registerHID", (void *)registerHID); +} + +int32_t main() { + static bool initialized = false; + if (!initialized) { + initialized = true; + initialize(); + } + if (!checkFocus(0, 0)) { + return 0; + } +} diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 96e72b4..a565a40 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -65,11 +65,6 @@ } UsbHostControllerInterface; typedef struct { - uint8_t buttons; - int8_t x, y; -} __attribute__((packed)) MouseReport; - -typedef struct { void *data; ListElement *slots; } UsbHostController; @@ -78,6 +73,7 @@ void *data; UsbHostControllerInterface *interface; uint32_t portIndex; + uint32_t id; } UsbSlot; extern uint32_t serviceId; diff --git a/src/userland/hid/Makefile b/src/userland/hid/Makefile new file mode 100644 index 0000000..462034d --- /dev/null +++ b/src/userland/hid/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = hid + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/hid/main.c b/src/userland/hid/main.c new file mode 100644 index 0000000..bbf449d --- /dev/null +++ b/src/userland/hid/main.c @@ -0,0 +1,56 @@ +#define ALLOC_MAIN +#include + +REQUEST(checkFocus, "ioManager", "checkFocus"); +REQUEST(moveRelative, "mouse", "moveRelative"); +REQUEST(updateButtons, "mouse", "updateButtons"); + +ListElement *hidDevices = NULL; + +typedef struct { + uint32_t serviceId; + uint32_t deviceId; + uint32_t normalFunction; + void *buffer; +} HIDDevice; + +typedef struct { + uint8_t buttons; + int8_t x, y; +} __attribute__((packed)) MouseReport; + +void hidListening(HIDDevice *device) { + while (1) { + request(device->serviceId, device->normalFunction, device->deviceId, getPhysicalAddress(device->buffer)); + MouseReport *report = device->buffer; + moveRelative((int32_t) report->x, (int32_t)report->y); + updateButtons(report->buttons, 0); + // todo: sleep for at least endpoint->interval? + // todo: start this loop in a fork? + sleep(10); + } +} + +uint32_t registerHID(uint32_t usbDevice, uint32_t _, uint32_t serviceName, uint32_t serviceId) { + HIDDevice *device = malloc(sizeof(HIDDevice)); + device->serviceId = serviceId; + device->deviceId = usbDevice; // USB calls this a interface, others may differ + device->buffer = malloc(0x1000); + device->normalFunction = getFunction(serviceId, "hid_normal"); + fork(hidListening, device, 0, 0); +} + +void initialize() { + createFunction("registerHID", (void *)registerHID); +} + +int32_t main() { + static bool initialized = false; + if (!initialized) { + initialized = true; + initialize(); + } + if (!checkFocus(0, 0)) { + return 0; + } +} diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 96e72b4..a565a40 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -65,11 +65,6 @@ } UsbHostControllerInterface; typedef struct { - uint8_t buttons; - int8_t x, y; -} __attribute__((packed)) MouseReport; - -typedef struct { void *data; ListElement *slots; } UsbHostController; @@ -78,6 +73,7 @@ void *data; UsbHostControllerInterface *interface; uint32_t portIndex; + uint32_t id; } UsbSlot; extern uint32_t serviceId; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 5a31845..3667260 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -24,6 +24,11 @@ return string; } +REQUEST(registerHID, "hid", "registerHID"); +#include "xhci/xhci.h" +extern void setProtocol(SlotXHCI *slot); +extern void setIdle(SlotXHCI *slot); + void setupInterfaces(UsbSlot *slot, void *start, uint32_t configurationValue) { UsbInterfaceDescriptor *interface = start; // only doing blank interface descriptors for now, there are @@ -61,13 +66,16 @@ uint8_t endpointNumber = endpoint->address & 0xF; // never 0 uint8_t direction = endpoint->address >> 7; uint8_t endpointIndex = (endpointNumber)*2 - 1 + direction; - void *buffer = requestMemory(1, 0, 0); - void *bufferPhysical = getPhysicalAddress(buffer); - fork(slot->interface->setupHID, slot->data, endpointIndex, buffer); + printf("endpoint index: %i\n", endpointIndex); + setProtocol(slot->data); + setIdle(slot->data); + registerHID(slot->id, 0); }) // clear list } +ListElement *usbSlots = NULL; + void resetPort(UsbSlot *slot) { printf("--------\n"); void *buffer = requestMemory(1, 0, 0); @@ -98,7 +106,8 @@ printf("port %i: %i interfaces, configuration %s, %i bytes\n", slot->portIndex, configuration->interfaceCount, configurationString, configuration->totalLength); - + slot->id = listCount(usbSlots); + listAdd(&usbSlots, slot); setupInterfaces(slot, (void *)configuration + configuration->size, configuration->configurationValue); } @@ -129,10 +138,36 @@ } } -int32_t main() { +bool initialized = false; + +void hid_normal(uint32_t slotId, void *bufferPhysical) { + UsbSlot *usbSlot = listGet(usbSlots, slotId); + SlotXHCI *slot = usbSlot->data; + uint32_t endpointIndex = 2; // TODO + XHCINormalTRB normal = {0}; + normal.type = 1; + normal.inDirection = 1; + normal.interrupterTarget = 0; + normal.interruptOnCompletion = 1; + normal.interruptOnShortPacket = 1; + normal.dataBuffer[0] = U32(bufferPhysical); + normal.dataBuffer[1] = 0; + normal.transferSize = 4; + uint32_t commandAddress = U32(enqueueCommand( + slot->endpointRings[endpointIndex], (void *)&normal)); + slot->controller->doorbells[slot->slotIndex] = endpointIndex + 1; + printf("xhci normal %i, %x, %x", slotId, bufferPhysical, commandAddress); + awaitCode(serviceId, xhciEvent, commandAddress); + // data is returned to buffer +} + +void initialize() { serviceId = getServiceId(); - // xhciEvent will carry data corresponding to the data in the xhci event xhciEvent = createEvent("xhciEvent"); + loadFromInitrd("hid"); + createFunction("hid_normal", (void *)hid_normal); + // xhciEvent will carry data corresponding to the data in the xhci event + // code will be used to identify an event for (uint32_t i = 0; i < 100; i++) { uint32_t class = getDeviceClass(i, 0); if (!class) { @@ -141,3 +176,10 @@ checkDevice(i, class); } } + +int32_t main() { + if (!initialized) { + initialize(); + initialized = true; + } +} diff --git a/src/userland/hid/Makefile b/src/userland/hid/Makefile new file mode 100644 index 0000000..462034d --- /dev/null +++ b/src/userland/hid/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = hid + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/hid/main.c b/src/userland/hid/main.c new file mode 100644 index 0000000..bbf449d --- /dev/null +++ b/src/userland/hid/main.c @@ -0,0 +1,56 @@ +#define ALLOC_MAIN +#include + +REQUEST(checkFocus, "ioManager", "checkFocus"); +REQUEST(moveRelative, "mouse", "moveRelative"); +REQUEST(updateButtons, "mouse", "updateButtons"); + +ListElement *hidDevices = NULL; + +typedef struct { + uint32_t serviceId; + uint32_t deviceId; + uint32_t normalFunction; + void *buffer; +} HIDDevice; + +typedef struct { + uint8_t buttons; + int8_t x, y; +} __attribute__((packed)) MouseReport; + +void hidListening(HIDDevice *device) { + while (1) { + request(device->serviceId, device->normalFunction, device->deviceId, getPhysicalAddress(device->buffer)); + MouseReport *report = device->buffer; + moveRelative((int32_t) report->x, (int32_t)report->y); + updateButtons(report->buttons, 0); + // todo: sleep for at least endpoint->interval? + // todo: start this loop in a fork? + sleep(10); + } +} + +uint32_t registerHID(uint32_t usbDevice, uint32_t _, uint32_t serviceName, uint32_t serviceId) { + HIDDevice *device = malloc(sizeof(HIDDevice)); + device->serviceId = serviceId; + device->deviceId = usbDevice; // USB calls this a interface, others may differ + device->buffer = malloc(0x1000); + device->normalFunction = getFunction(serviceId, "hid_normal"); + fork(hidListening, device, 0, 0); +} + +void initialize() { + createFunction("registerHID", (void *)registerHID); +} + +int32_t main() { + static bool initialized = false; + if (!initialized) { + initialized = true; + initialize(); + } + if (!checkFocus(0, 0)) { + return 0; + } +} diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 96e72b4..a565a40 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -65,11 +65,6 @@ } UsbHostControllerInterface; typedef struct { - uint8_t buttons; - int8_t x, y; -} __attribute__((packed)) MouseReport; - -typedef struct { void *data; ListElement *slots; } UsbHostController; @@ -78,6 +73,7 @@ void *data; UsbHostControllerInterface *interface; uint32_t portIndex; + uint32_t id; } UsbSlot; extern uint32_t serviceId; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 5a31845..3667260 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -24,6 +24,11 @@ return string; } +REQUEST(registerHID, "hid", "registerHID"); +#include "xhci/xhci.h" +extern void setProtocol(SlotXHCI *slot); +extern void setIdle(SlotXHCI *slot); + void setupInterfaces(UsbSlot *slot, void *start, uint32_t configurationValue) { UsbInterfaceDescriptor *interface = start; // only doing blank interface descriptors for now, there are @@ -61,13 +66,16 @@ uint8_t endpointNumber = endpoint->address & 0xF; // never 0 uint8_t direction = endpoint->address >> 7; uint8_t endpointIndex = (endpointNumber)*2 - 1 + direction; - void *buffer = requestMemory(1, 0, 0); - void *bufferPhysical = getPhysicalAddress(buffer); - fork(slot->interface->setupHID, slot->data, endpointIndex, buffer); + printf("endpoint index: %i\n", endpointIndex); + setProtocol(slot->data); + setIdle(slot->data); + registerHID(slot->id, 0); }) // clear list } +ListElement *usbSlots = NULL; + void resetPort(UsbSlot *slot) { printf("--------\n"); void *buffer = requestMemory(1, 0, 0); @@ -98,7 +106,8 @@ printf("port %i: %i interfaces, configuration %s, %i bytes\n", slot->portIndex, configuration->interfaceCount, configurationString, configuration->totalLength); - + slot->id = listCount(usbSlots); + listAdd(&usbSlots, slot); setupInterfaces(slot, (void *)configuration + configuration->size, configuration->configurationValue); } @@ -129,10 +138,36 @@ } } -int32_t main() { +bool initialized = false; + +void hid_normal(uint32_t slotId, void *bufferPhysical) { + UsbSlot *usbSlot = listGet(usbSlots, slotId); + SlotXHCI *slot = usbSlot->data; + uint32_t endpointIndex = 2; // TODO + XHCINormalTRB normal = {0}; + normal.type = 1; + normal.inDirection = 1; + normal.interrupterTarget = 0; + normal.interruptOnCompletion = 1; + normal.interruptOnShortPacket = 1; + normal.dataBuffer[0] = U32(bufferPhysical); + normal.dataBuffer[1] = 0; + normal.transferSize = 4; + uint32_t commandAddress = U32(enqueueCommand( + slot->endpointRings[endpointIndex], (void *)&normal)); + slot->controller->doorbells[slot->slotIndex] = endpointIndex + 1; + printf("xhci normal %i, %x, %x", slotId, bufferPhysical, commandAddress); + awaitCode(serviceId, xhciEvent, commandAddress); + // data is returned to buffer +} + +void initialize() { serviceId = getServiceId(); - // xhciEvent will carry data corresponding to the data in the xhci event xhciEvent = createEvent("xhciEvent"); + loadFromInitrd("hid"); + createFunction("hid_normal", (void *)hid_normal); + // xhciEvent will carry data corresponding to the data in the xhci event + // code will be used to identify an event for (uint32_t i = 0; i < 100; i++) { uint32_t class = getDeviceClass(i, 0); if (!class) { @@ -141,3 +176,10 @@ checkDevice(i, class); } } + +int32_t main() { + if (!initialized) { + initialize(); + initialized = true; + } +} diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 14c9a0a..247fd01 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -142,10 +142,10 @@ U32(&globalController->events .physical[globalController->events.dequeue]) | (1 << 3); - // printf("event %i [%x %x %x %x]: %i\n", - // globalController->events.dequeue, trb->dataLow, - // trb->dataHigh, trb->status, trb->control, - // trb->control >> 10 & 0x3F); + printf("event %i [%x %x %x %x]: %i\n", + globalController->events.dequeue, trb->dataLow, + trb->dataHigh, trb->status, trb->control, + trb->control >> 10 & 0x3F); fireEventCode(xhciEvent, U32(trb), trb->dataLow); } } diff --git a/src/userland/hid/Makefile b/src/userland/hid/Makefile new file mode 100644 index 0000000..462034d --- /dev/null +++ b/src/userland/hid/Makefile @@ -0,0 +1,31 @@ +CC = i686-elf-gcc +CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I ../../include -I include -Wno-discarded-qualifiers -fms-extensions -Wno-shift-count-overflow -O0 +LD = i686-elf-ld +LD_FLAGS = -z max-page-size=0x1000 -T ../link.ld +AS = nasm +ASFlAGS = -felf32 + +BUILD_FOLDER = build + +SOURCE_FILES := $(shell find . -name *.c -or -name *.asm -or -name *.s) +OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) + +NAME = hid + +../../../initrd/$(NAME): $(OBJS) ../../../build/hlib.o + @echo "linking user program $(NAME)" + @$(LD) $(LD_FLAGS) -o ../../../initrd/$(NAME) $(OBJS) + +$(BUILD_FOLDER)/%.asm.o: %.asm + @echo "asembling $<" + @mkdir -p $(dir $@) + @$(AS) $(ASFlAGS) $< -o $@ + +$(BUILD_FOLDER)/%.c.o: %.c + @echo "compiling $<" + @mkdir -p $(dir $@) + @$(CC) $(CCFLAGS) -r $< -o $@ + +clean: + @echo "clearing build folder" + @rm -r $(BUILD_FOLDER) diff --git a/src/userland/hid/main.c b/src/userland/hid/main.c new file mode 100644 index 0000000..bbf449d --- /dev/null +++ b/src/userland/hid/main.c @@ -0,0 +1,56 @@ +#define ALLOC_MAIN +#include + +REQUEST(checkFocus, "ioManager", "checkFocus"); +REQUEST(moveRelative, "mouse", "moveRelative"); +REQUEST(updateButtons, "mouse", "updateButtons"); + +ListElement *hidDevices = NULL; + +typedef struct { + uint32_t serviceId; + uint32_t deviceId; + uint32_t normalFunction; + void *buffer; +} HIDDevice; + +typedef struct { + uint8_t buttons; + int8_t x, y; +} __attribute__((packed)) MouseReport; + +void hidListening(HIDDevice *device) { + while (1) { + request(device->serviceId, device->normalFunction, device->deviceId, getPhysicalAddress(device->buffer)); + MouseReport *report = device->buffer; + moveRelative((int32_t) report->x, (int32_t)report->y); + updateButtons(report->buttons, 0); + // todo: sleep for at least endpoint->interval? + // todo: start this loop in a fork? + sleep(10); + } +} + +uint32_t registerHID(uint32_t usbDevice, uint32_t _, uint32_t serviceName, uint32_t serviceId) { + HIDDevice *device = malloc(sizeof(HIDDevice)); + device->serviceId = serviceId; + device->deviceId = usbDevice; // USB calls this a interface, others may differ + device->buffer = malloc(0x1000); + device->normalFunction = getFunction(serviceId, "hid_normal"); + fork(hidListening, device, 0, 0); +} + +void initialize() { + createFunction("registerHID", (void *)registerHID); +} + +int32_t main() { + static bool initialized = false; + if (!initialized) { + initialized = true; + initialize(); + } + if (!checkFocus(0, 0)) { + return 0; + } +} diff --git a/src/userland/usb/include/usb.h b/src/userland/usb/include/usb.h index 96e72b4..a565a40 100644 --- a/src/userland/usb/include/usb.h +++ b/src/userland/usb/include/usb.h @@ -65,11 +65,6 @@ } UsbHostControllerInterface; typedef struct { - uint8_t buttons; - int8_t x, y; -} __attribute__((packed)) MouseReport; - -typedef struct { void *data; ListElement *slots; } UsbHostController; @@ -78,6 +73,7 @@ void *data; UsbHostControllerInterface *interface; uint32_t portIndex; + uint32_t id; } UsbSlot; extern uint32_t serviceId; diff --git a/src/userland/usb/main.c b/src/userland/usb/main.c index 5a31845..3667260 100644 --- a/src/userland/usb/main.c +++ b/src/userland/usb/main.c @@ -24,6 +24,11 @@ return string; } +REQUEST(registerHID, "hid", "registerHID"); +#include "xhci/xhci.h" +extern void setProtocol(SlotXHCI *slot); +extern void setIdle(SlotXHCI *slot); + void setupInterfaces(UsbSlot *slot, void *start, uint32_t configurationValue) { UsbInterfaceDescriptor *interface = start; // only doing blank interface descriptors for now, there are @@ -61,13 +66,16 @@ uint8_t endpointNumber = endpoint->address & 0xF; // never 0 uint8_t direction = endpoint->address >> 7; uint8_t endpointIndex = (endpointNumber)*2 - 1 + direction; - void *buffer = requestMemory(1, 0, 0); - void *bufferPhysical = getPhysicalAddress(buffer); - fork(slot->interface->setupHID, slot->data, endpointIndex, buffer); + printf("endpoint index: %i\n", endpointIndex); + setProtocol(slot->data); + setIdle(slot->data); + registerHID(slot->id, 0); }) // clear list } +ListElement *usbSlots = NULL; + void resetPort(UsbSlot *slot) { printf("--------\n"); void *buffer = requestMemory(1, 0, 0); @@ -98,7 +106,8 @@ printf("port %i: %i interfaces, configuration %s, %i bytes\n", slot->portIndex, configuration->interfaceCount, configurationString, configuration->totalLength); - + slot->id = listCount(usbSlots); + listAdd(&usbSlots, slot); setupInterfaces(slot, (void *)configuration + configuration->size, configuration->configurationValue); } @@ -129,10 +138,36 @@ } } -int32_t main() { +bool initialized = false; + +void hid_normal(uint32_t slotId, void *bufferPhysical) { + UsbSlot *usbSlot = listGet(usbSlots, slotId); + SlotXHCI *slot = usbSlot->data; + uint32_t endpointIndex = 2; // TODO + XHCINormalTRB normal = {0}; + normal.type = 1; + normal.inDirection = 1; + normal.interrupterTarget = 0; + normal.interruptOnCompletion = 1; + normal.interruptOnShortPacket = 1; + normal.dataBuffer[0] = U32(bufferPhysical); + normal.dataBuffer[1] = 0; + normal.transferSize = 4; + uint32_t commandAddress = U32(enqueueCommand( + slot->endpointRings[endpointIndex], (void *)&normal)); + slot->controller->doorbells[slot->slotIndex] = endpointIndex + 1; + printf("xhci normal %i, %x, %x", slotId, bufferPhysical, commandAddress); + awaitCode(serviceId, xhciEvent, commandAddress); + // data is returned to buffer +} + +void initialize() { serviceId = getServiceId(); - // xhciEvent will carry data corresponding to the data in the xhci event xhciEvent = createEvent("xhciEvent"); + loadFromInitrd("hid"); + createFunction("hid_normal", (void *)hid_normal); + // xhciEvent will carry data corresponding to the data in the xhci event + // code will be used to identify an event for (uint32_t i = 0; i < 100; i++) { uint32_t class = getDeviceClass(i, 0); if (!class) { @@ -141,3 +176,10 @@ checkDevice(i, class); } } + +int32_t main() { + if (!initialized) { + initialize(); + initialized = true; + } +} diff --git a/src/userland/usb/xhci/controller.c b/src/userland/usb/xhci/controller.c index 14c9a0a..247fd01 100644 --- a/src/userland/usb/xhci/controller.c +++ b/src/userland/usb/xhci/controller.c @@ -142,10 +142,10 @@ U32(&globalController->events .physical[globalController->events.dequeue]) | (1 << 3); - // printf("event %i [%x %x %x %x]: %i\n", - // globalController->events.dequeue, trb->dataLow, - // trb->dataHigh, trb->status, trb->control, - // trb->control >> 10 & 0x3F); + printf("event %i [%x %x %x %x]: %i\n", + globalController->events.dequeue, trb->dataLow, + trb->dataHigh, trb->status, trb->control, + trb->control >> 10 & 0x3F); fireEventCode(xhciEvent, U32(trb), trb->dataLow); } } diff --git a/src/userland/usb/xhci/xhci.c b/src/userland/usb/xhci/xhci.c index d3f7486..5ab5167 100644 --- a/src/userland/usb/xhci/xhci.c +++ b/src/userland/usb/xhci/xhci.c @@ -185,41 +185,10 @@ awaitCode(serviceId, xhciEvent, commandAddress); } -REQUEST(moveRelative, "mouse", "moveRelative"); -REQUEST(updateButtons, "mouse", "updateButtons"); - -void setupHID(SlotXHCI *slot, uint32_t endpointIndex, void *buffer) { - setProtocol(slot); - setIdle(slot); - - XHCINormalTRB normal = {0}; - normal.type = 1; - normal.inDirection = 1; - normal.interrupterTarget = 0; - normal.interruptOnCompletion = 1; - normal.interruptOnShortPacket = 1; - normal.dataBuffer[0] = U32(getPhysicalAddress(buffer)); - normal.transferSize = 4; - - while (1) { - uint32_t commandAddress = U32(enqueueCommand( - slot->endpointRings[endpointIndex], (void *)&normal)); - slot->controller->doorbells[slot->slotIndex] = endpointIndex + 1; - awaitCode(serviceId, xhciEvent, commandAddress); - MouseReport *report = buffer; - moveRelative((int32_t) report->x, (int32_t)report->y); - updateButtons(report->buttons, 0); - // todo: sleep for at least endpoint->interval? - // todo: start this loop in a fork? - sleep(10); - } -} - - UsbHostControllerInterface xhci = { .initialize = init, .getDeviceDescriptor = (void *)usbGetDeviceDescriptor, .setupEndpoints = (void *)xhciSetupEndpoints, - .setupHID = (void *)setupHID, + //.setupHID = (void *)setupHID, .pciClass = 0x0C0330, };