Mercurial > hg > minimon
comparison minimon.c @ 1:111d4bbce605
improving
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Sat, 07 May 2011 19:00:43 +0200 |
parents | 71b9540bdd23 |
children | bac8ed8d6eb9 |
comparison
equal
deleted
inserted
replaced
0:71b9540bdd23 | 1:111d4bbce605 |
---|---|
5 #include <assert.h> | 5 #include <assert.h> |
6 #include "usb.h" | 6 #include "usb.h" |
7 | 7 |
8 static const char *progname = "minimon"; | 8 static const char *progname = "minimon"; |
9 | 9 |
10 struct usb_device *find_dev() { | 10 static int need_switch = 0; |
11 static int have_idx = -1; | |
12 | |
13 typedef struct { | |
14 int mass_id; | |
15 int custom_id; | |
16 const char *name; | |
17 int width; | |
18 int height; | |
19 } id_info_t; | |
20 | |
21 static id_info_t ids[] = { | |
22 {0x2027, 0x2028, }, | |
23 {0xffff, 0xffff, "SPF-75H", 800, 480}, | |
24 {0xffff, 0xffff, "SPF-83H", 800, 600}, // 85P?? | |
25 {0xffff, 0xffff, "SPF-85H", 800, 600}, // 85P?? | |
26 {0x2033, 0x2034, "SPF-87H", 800, 480}, | |
27 {0x2035, 0x2036, "SPF-107H", 1024, 600}, | |
28 {0, 0, } // end-of-list | |
29 }; | |
30 | |
31 static int in_list(int id, id_info_t *list) { | |
32 if (!list) | |
33 return 0; | |
34 | |
35 int idx = 0; | |
36 while (list->mass_id || list->custom_id) { | |
37 if (id == list->mass_id) { | |
38 // still in mass-storage mode, need to switch | |
39 need_switch = 1; | |
40 return idx; | |
41 } | |
42 else if (id == list->custom_id) { | |
43 need_switch = 0; | |
44 return idx; | |
45 } | |
46 idx++; | |
47 list++; | |
48 } | |
49 | |
50 return -1; | |
51 } | |
52 | |
53 static struct usb_device *find_dev() { | |
11 struct usb_bus *bus; | 54 struct usb_bus *bus; |
12 struct usb_device *dev; | 55 struct usb_device *dev; |
13 | 56 |
14 usb_init(); | 57 usb_init(); |
15 usb_find_busses(); | 58 usb_find_busses(); |
16 usb_find_devices(); | 59 usb_find_devices(); |
17 | 60 |
18 for (bus = usb_busses; bus; bus = bus->next) { | 61 for (bus = usb_busses; bus; bus = bus->next) { |
19 for (dev = bus->devices; dev; dev = dev->next) { | 62 for (dev = bus->devices; dev; dev = dev->next) { |
20 if (dev->descriptor.idVendor == 0x04e8 && | 63 if (dev->descriptor.idVendor == 0x04e8) { |
21 dev->descriptor.idProduct == 0x2028) { | 64 // found a Samsung device, good |
22 return dev; | 65 int idx = -1; |
66 if ((idx = in_list(dev->descriptor.idProduct, ids)) >= 0) { | |
67 have_idx = idx; | |
68 return dev; | |
69 } | |
23 } | 70 } |
24 } | 71 } |
25 } | 72 } |
26 | 73 |
27 return NULL; | 74 return NULL; |
28 } | 75 } |
29 | 76 |
30 usb_dev_handle *dev_open(struct usb_device *dev) { | 77 static usb_dev_handle *dev_open(struct usb_device *dev) { |
31 int res = -1; | 78 int res = -1; |
32 char buf[256]; | 79 char buf[256]; |
33 usb_dev_handle *udev; | 80 usb_dev_handle *udev; |
34 int numeps = 0; | 81 int numeps = 0; |
35 | 82 |
50 exit(EXIT_FAILURE); | 97 exit(EXIT_FAILURE); |
51 } | 98 } |
52 | 99 |
53 strcpy(buf, "** no string **"); | 100 strcpy(buf, "** no string **"); |
54 res = usb_get_string_simple(udev, dev->descriptor.iManufacturer, buf, sizeof(buf)); | 101 res = usb_get_string_simple(udev, dev->descriptor.iManufacturer, buf, sizeof(buf)); |
55 fprintf(stderr, "usb_get_string_simple => %d, %s\n", res, buf); | 102 // fprintf(stderr, "usb_get_string_simple => %d, %s\n", res, buf); |
56 | 103 |
57 { | 104 { |
58 int eplist[] = { 0x2, 0x81, 0x83 }; | 105 int eplist[] = { 0x2, 0x81, 0x83 }; |
59 int eplength = sizeof(eplist)/sizeof(eplist[0]); | 106 int eplength = sizeof(eplist)/sizeof(eplist[0]); |
60 int *endpoint = eplist; | 107 int *endpoint = eplist; |
61 int i; | 108 int i; |
62 for (i=0; i<eplength; i++) { | 109 for (i = 0; i < eplength; i++) { |
63 res = usb_resetep(udev, *endpoint); | 110 res = usb_resetep(udev, *endpoint); |
64 res = usb_clear_halt(udev, *endpoint); | 111 res = usb_clear_halt(udev, *endpoint); |
65 endpoint++; | 112 endpoint++; |
66 } | 113 } |
67 } | 114 } |
68 | 115 |
69 return udev; | 116 return udev; |
70 } | 117 } |
71 | 118 |
72 | 119 static void send_jpeg(FILE *f, usb_dev_handle *udev) { |
73 void send_jpeg(FILE *f, usb_dev_handle *udev) { | |
74 fseek(f, 0, SEEK_END); | 120 fseek(f, 0, SEEK_END); |
75 int sz = ftell(f); | 121 int sz = ftell(f); |
76 fseek(f, 0, SEEK_SET); | 122 fseek(f, 0, SEEK_SET); |
77 | 123 |
78 #define URBBUF_MAX 0x20000 | 124 #define URBBUF_MAX 0x20000 |
101 } | 147 } |
102 } | 148 } |
103 | 149 |
104 int main(int argc, char *argv[]) { | 150 int main(int argc, char *argv[]) { |
105 if (argc != 2) { | 151 if (argc != 2) { |
106 fprintf(stderr, "Usage: %s FILE", progname); | 152 fprintf(stderr, "Usage: %s <.jpg file>\n", progname); |
107 return EXIT_FAILURE; | 153 return EXIT_FAILURE; |
108 } | 154 } |
109 | 155 |
110 struct usb_device *dev = find_dev(index); | 156 struct usb_device *dev = find_dev(index); |
111 assert(dev != NULL); | 157 if (!dev) { |
158 fprintf(stderr, "%s: no photo frame device found, exit.\n", progname); | |
159 exit(EXIT_FAILURE); | |
160 } | |
161 | |
162 if (need_switch) { | |
163 fprintf(stderr, "%s: found %s, trying to switch to custom product mode...\n", | |
164 ids[have_idx].name, progname); | |
165 | |
166 usb_dev_handle *udev; | |
167 | |
168 udev = usb_open(dev); | |
169 if (!udev) { | |
170 fprintf(stderr, "%s: failed to open device, exit.\n", progname); | |
171 exit(EXIT_FAILURE); | |
172 } | |
173 | |
174 char buf[254]; | |
175 memset(buf, 0, 254); | |
176 | |
177 int res = usb_control_msg(udev, USB_TYPE_STANDARD | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, | |
178 0xfe, 0xfe, buf, 0xfe, 1000); | |
179 fprintf(stderr, "%s: usb_control_msg() = %d\n", progname, res); | |
180 | |
181 usb_close(udev); | |
182 } | |
183 | |
184 dev = find_dev(index); | |
185 if (!dev || need_switch) { | |
186 fprintf(stderr, "%s: no photo frame device found, exit.\n", progname); | |
187 exit(EXIT_FAILURE); | |
188 } | |
189 | |
190 fprintf(stderr, "%s: found %s (%d x %d)\n", | |
191 progname, ids[have_idx].name, ids[have_idx].width, ids[have_idx].height); | |
112 | 192 |
113 usb_dev_handle *udev = dev_open(dev); | 193 usb_dev_handle *udev = dev_open(dev); |
194 | |
195 // int res = usb_control_msg(udev, USB_TYPE_STANDARD | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, | |
196 // 0xfe, 0x0, buf, 0x0, 1000); | |
197 // fprintf(stderr, "%s: usb_control_msg() = %d\n", progname, res); | |
114 | 198 |
115 FILE *f = fopen(argv[1], "rb"); | 199 FILE *f = fopen(argv[1], "rb"); |
116 if (f == NULL) { | 200 if (f == NULL) { |
117 fprintf(stderr, "%s: failed to open file '%s', exit.\n", progname, argv[1]); | 201 fprintf(stderr, "%s: failed to open file '%s', exit.\n", progname, argv[1]); |
118 exit(EXIT_FAILURE); | 202 exit(EXIT_FAILURE); |