Files
mop3/usb/usb.c
kamkow1 c8fb575bdd
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 2m7s
Build documentation / build-and-deploy (push) Successful in 39s
Change formatting rules
2026-04-24 01:54:48 +02:00

90 lines
2.2 KiB
C

#include <device_info.h>
#include <devices.h>
#include <malloc.h>
#include <mprintf.h>
#include <process.h>
#include <status.h>
#include <str_status.h>
#include <string.h>
#include <system.h>
#include <volume_info.h>
static void usb_poll(void) {
struct device_info* infos = malloc(sizeof(struct device_info) * 1024);
memset(infos, 0, sizeof(struct device_info) * 1024);
int device_count = get_device_info(infos, 1024);
struct device_info info;
bool found = false;
for (int dev = 0; dev < device_count; dev++) {
info = infos[dev];
if (info.type == DEVICE_TYPE_USB_CTRL) {
found = true;
break;
}
}
if (found) {
mprintf("Polling device %s\n", info.key);
for (;;) {
device_do(info.key, XUSBCTRL_POLL_DRIVER, NULL, NULL, NULL, NULL);
for (volatile int i = 0; i < 1000; i++) {
sched();
#if defined(__x86_64__)
__asm__ volatile("pause" ::: "memory");
#endif
}
}
}
free(infos);
}
static void usb_eject(const char* dev_key) {
struct volume_info* volume_infos = malloc(sizeof(*volume_infos) * 100);
memset(volume_infos, 0, sizeof(*volume_infos) * 100);
size_t count = get_volume_info(volume_infos, 100);
for (size_t i = 0; i < count; i++) {
struct volume_info* info = &volume_infos[i];
if (strcmp(info->device_key, dev_key) == 0) {
int ret = volume_delete(info->volume_name);
mprintf("Deleted volume %s: %s\n", info->volume_name, str_status[ret < 0 ? -ret : ret]);
break;
}
}
free(volume_infos);
}
void app_main(void) {
char commandbuf[32];
memset(commandbuf, 0, sizeof(commandbuf));
char devnamebuf[32];
memset(devnamebuf, 0, sizeof(devnamebuf));
if (env_get(process_get_pgid(), "C", (void*)commandbuf, sizeof(commandbuf)) != ST_OK) {
mprintf("ERROR C=???. No command provided\n");
return;
}
if (strcmp(commandbuf, "poll") == 0) {
usb_poll();
} else if (strcmp(commandbuf, "eject") == 0) {
if (env_get(process_get_pgid(), "dev", (void*)devnamebuf, sizeof(devnamebuf)) != ST_OK) {
mprintf("ERROR No device provided\n");
return;
}
usb_eject(devnamebuf);
} else {
mprintf("ERROR unknown command %s\n", commandbuf);
}
}