99748b197cdfca3789a84f8fabcd24be223cab53
[megadrive.git] / mx / linux / main.c
1 #include <stdio.h>
2 #include <usb.h>
3
4
5 typedef unsigned char  u8;
6 typedef unsigned short u16;
7 typedef unsigned int   u32;
8 #define array_size(x) (sizeof(x) / sizeof(x[0]))
9
10 static const struct {
11         unsigned short vendor;
12         unsigned short product;
13         const char *name;
14 } g_devices[] = {
15         { 0x03eb, 0x202a, "16MX+U Game Device" },
16         { 0x03eb, 0x202b, "32MX+U Game Device" },
17         { 0x03eb, 0x202c, "16MX+US Game Device" },
18         { 0x03eb, 0x202d, "32MX+UF Game Device" },
19 };
20
21 /*****************************************************************************/
22
23 struct {
24         u8 magic[4];
25         u8 reserved[11];
26         u8 magic2;
27         u8 mx_cmd;
28         union {
29                 struct {
30                         u8 which_device;
31                         u8 boot_cmd;
32                 } dev_info;
33                 struct {
34                         u8 addrb2;      /* most significant */
35                         u8 addrb1;
36                         u8 addrb0;
37                         u8 num_pages;
38                 } rom_rw;
39                 struct {
40                         u8 position;
41                 } filename;
42                 struct {
43                         u8 times_cmd;
44                         u8 action;
45                         u8 cntb3;
46                         u8 cntb2;
47                         u8 cntb1;
48                         u8 cntb0;
49                 } times_write;
50         };
51         u8 pad[8];
52 } dev_cmd;
53
54
55 static usb_dev_handle *get_device(void)
56 {
57         struct usb_dev_handle *handle;
58         struct usb_device *dev;
59         struct usb_bus *bus;
60         int i, ret;
61
62         ret = usb_find_busses();
63         if (ret <= 0) {
64                 fprintf(stderr, "Can't find USB busses\n");
65                 return NULL;
66         }
67
68         ret = usb_find_devices();
69         if (ret <= 0) {
70                 fprintf(stderr, "Can't find USB devices\n");
71                 return NULL;
72         }
73
74         bus = usb_get_busses();
75         for (; bus; bus = bus->next)
76         {
77                 for (dev = bus->devices; dev; dev = dev->next)
78                 {
79                         for (i = 0; i < array_size(g_devices); i++)
80                         {
81                                 if (dev->descriptor.idVendor == g_devices[i].vendor &&
82                                                 dev->descriptor.idProduct == g_devices[i].product)
83                                         goto found;
84                         }
85                 }
86         }
87
88         fprintf(stderr, "device not found.\n");
89         return NULL;
90
91 found:
92         printf("found %s.\n", g_devices[i].name);
93
94         handle = usb_open(dev);
95         if (handle == NULL) {
96                 fprintf(stderr, "failed to open device:\n");
97                 fprintf(stderr, "%s\n", usb_strerror());
98                 return NULL;
99         }
100
101         ret = usb_set_configuration(handle, 1);
102         if (ret != 0) {
103                 fprintf(stderr, "couldn't set configuration for /*/bus/usb/%s/%s:\n",
104                         bus->dirname, dev->filename);
105                 fprintf(stderr, "%s\n", usb_strerror());
106                 return NULL;
107         }
108
109         ret = usb_claim_interface(handle, 0);
110         if (ret != 0) {
111                 fprintf(stderr, "couldn't claim /*/bus/usb/%s/%s:\n",
112                         bus->dirname, dev->filename);
113                 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
114                 return NULL;
115         }
116
117         return handle;
118 }
119
120 static void release_device(struct usb_dev_handle *device)
121 {
122         usb_release_interface(device, 0);
123         usb_close(device);
124 }
125
126 int main(int argc, char *argv[])
127 {
128         struct usb_dev_handle *device;
129
130         usb_init();
131
132         device = get_device();
133         if (device == NULL)
134                 return 1;
135
136
137
138         release_device(device);
139
140         return 0;
141 }
142