From 8c8e818c8fc401948403977a0c9543961ce766c5 Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 9 Mar 2009 20:09:49 +0000 Subject: [PATCH] starting linux code for mx device --- mx/linux/Makefile | 5 ++ mx/linux/main.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 mx/linux/Makefile create mode 100644 mx/linux/main.c diff --git a/mx/linux/Makefile b/mx/linux/Makefile new file mode 100644 index 0000000..8f0ca1d --- /dev/null +++ b/mx/linux/Makefile @@ -0,0 +1,5 @@ + +CC ?= $(CROSS)gcc + +CFLAGS += -Wall -ggdb +LDFLAGS += -lusb diff --git a/mx/linux/main.c b/mx/linux/main.c new file mode 100644 index 0000000..99748b1 --- /dev/null +++ b/mx/linux/main.c @@ -0,0 +1,142 @@ +#include +#include + + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +#define array_size(x) (sizeof(x) / sizeof(x[0])) + +static const struct { + unsigned short vendor; + unsigned short product; + const char *name; +} g_devices[] = { + { 0x03eb, 0x202a, "16MX+U Game Device" }, + { 0x03eb, 0x202b, "32MX+U Game Device" }, + { 0x03eb, 0x202c, "16MX+US Game Device" }, + { 0x03eb, 0x202d, "32MX+UF Game Device" }, +}; + +/*****************************************************************************/ + +struct { + u8 magic[4]; + u8 reserved[11]; + u8 magic2; + u8 mx_cmd; + union { + struct { + u8 which_device; + u8 boot_cmd; + } dev_info; + struct { + u8 addrb2; /* most significant */ + u8 addrb1; + u8 addrb0; + u8 num_pages; + } rom_rw; + struct { + u8 position; + } filename; + struct { + u8 times_cmd; + u8 action; + u8 cntb3; + u8 cntb2; + u8 cntb1; + u8 cntb0; + } times_write; + }; + u8 pad[8]; +} dev_cmd; + + +static usb_dev_handle *get_device(void) +{ + struct usb_dev_handle *handle; + struct usb_device *dev; + struct usb_bus *bus; + int i, ret; + + ret = usb_find_busses(); + if (ret <= 0) { + fprintf(stderr, "Can't find USB busses\n"); + return NULL; + } + + ret = usb_find_devices(); + if (ret <= 0) { + fprintf(stderr, "Can't find USB devices\n"); + return NULL; + } + + bus = usb_get_busses(); + for (; bus; bus = bus->next) + { + for (dev = bus->devices; dev; dev = dev->next) + { + for (i = 0; i < array_size(g_devices); i++) + { + if (dev->descriptor.idVendor == g_devices[i].vendor && + dev->descriptor.idProduct == g_devices[i].product) + goto found; + } + } + } + + fprintf(stderr, "device not found.\n"); + return NULL; + +found: + printf("found %s.\n", g_devices[i].name); + + handle = usb_open(dev); + if (handle == NULL) { + fprintf(stderr, "failed to open device:\n"); + fprintf(stderr, "%s\n", usb_strerror()); + return NULL; + } + + ret = usb_set_configuration(handle, 1); + if (ret != 0) { + fprintf(stderr, "couldn't set configuration for /*/bus/usb/%s/%s:\n", + bus->dirname, dev->filename); + fprintf(stderr, "%s\n", usb_strerror()); + return NULL; + } + + ret = usb_claim_interface(handle, 0); + if (ret != 0) { + fprintf(stderr, "couldn't claim /*/bus/usb/%s/%s:\n", + bus->dirname, dev->filename); + fprintf(stderr, "%s (%d)\n", usb_strerror(), ret); + return NULL; + } + + return handle; +} + +static void release_device(struct usb_dev_handle *device) +{ + usb_release_interface(device, 0); + usb_close(device); +} + +int main(int argc, char *argv[]) +{ + struct usb_dev_handle *device; + + usb_init(); + + device = get_device(); + if (device == NULL) + return 1; + + + + release_device(device); + + return 0; +} + -- 2.39.5