comparison disp-minimon.c @ 10:078dc69945ad

working: libjpeg-turbo support, handle fb and monitor with different size
author Peter Meerwald <pmeerw@pmeerw.net>
date Sun, 15 May 2011 17:48:57 +0200
parents
children
comparison
equal deleted inserted replaced
9:c7af696b6221 10:078dc69945ad
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <assert.h>
6 #include "usb.h"
7
8 static const char *progname = "minimon";
9
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 // there are many more,
22 static id_info_t ids[] = {
23 {0x2027, 0x2028, "SPF-107H", 1024, 600},
24 {0xffff, 0xffff, "SPF-75H", 800, 480},
25 {0xffff, 0xffff, "SPF-83H", 800, 600},
26 {0xffff, 0xffff, "SPF-85H", 800, 600}, // 85P??
27 {0x2033, 0x2034, "SPF-87H", 800, 480},
28 {0x2035, 0x2036, "SPF-107H", 1024, 600},
29 {0, 0, } // end-of-list
30 };
31
32 static int in_list(int id, id_info_t *list) {
33 if (!list)
34 return 0;
35
36 int idx = 0;
37 while (list->mass_id || list->custom_id) {
38 if (id == list->mass_id) {
39 // still in mass-storage mode, need to switch
40 need_switch = 1;
41 return idx;
42 }
43 else if (id == list->custom_id) {
44 need_switch = 0;
45 return idx;
46 }
47 idx++;
48 list++;
49 }
50
51 return -1;
52 }
53
54 static struct usb_device *find_dev() {
55 struct usb_bus *bus;
56 struct usb_device *dev;
57
58 usb_init();
59 usb_find_busses();
60 usb_find_devices();
61
62 for (bus = usb_busses; bus; bus = bus->next) {
63 for (dev = bus->devices; dev; dev = dev->next) {
64 if (dev->descriptor.idVendor == 0x04e8) {
65 // found a Samsung device, good
66 int idx = -1;
67 if ((idx = in_list(dev->descriptor.idProduct, ids)) >= 0) {
68 have_idx = idx;
69 return dev;
70 }
71 }
72 }
73 }
74
75 return NULL;
76 }
77
78 static usb_dev_handle *dev_open(struct usb_device *dev) {
79 int res = -1;
80 usb_dev_handle *udev;
81 int numeps = 0;
82
83 udev = usb_open(dev);
84 if (!udev) {
85 fprintf(stderr, "%s: failed to open device, exit.\n", progname);
86 exit(EXIT_FAILURE);
87 }
88
89 // setuid(getuid());
90
91 res = usb_set_configuration(udev, 1);
92
93 usb_claim_interface(udev, 0);
94 numeps = dev->config[0].interface[0].altsetting[0].bNumEndpoints;
95 if (numeps == 0) {
96 fprintf(stderr, "%s: no endpoints, exit.\n", progname);
97 exit(EXIT_FAILURE);
98 }
99
100 {
101 int eplist[] = { 0x2, 0x81, 0x83 };
102 int eplength = sizeof(eplist)/sizeof(eplist[0]);
103 int *endpoint = eplist;
104 int i;
105 for (i = 0; i < eplength; i++) {
106 res = usb_resetep(udev, *endpoint);
107 res = usb_clear_halt(udev, *endpoint);
108 endpoint++;
109 }
110 }
111
112 return udev;
113 }
114
115 static void send_jpeg(FILE *f, usb_dev_handle *udev) {
116 fseek(f, 0, SEEK_END);
117 int sz = ftell(f);
118 fseek(f, 0, SEEK_SET);
119
120 #define URBBUF_MAX 0x20000
121 char buf[URBBUF_MAX];
122
123 #define HDR_LEN 12
124 char hdr[HDR_LEN] = {0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff, 0xff, 0xff, 0x48, 0x00, 0x00, 0x00};
125 *(int *)(hdr+4) = sz;
126
127 memcpy(buf, hdr, HDR_LEN);
128 int off = HDR_LEN;
129
130 while(!feof(f)) {
131 int nr = fread(buf+off, 1, URBBUF_MAX - off, f);
132 if (nr < 0) break;
133 // pad
134 memset(buf + off + nr, 0, URBBUF_MAX - off - nr);
135
136 // write it out chunk by chunk
137 int timeout = 1000;
138 int endpoint = 0x2;
139 int res = usb_bulk_write(udev, endpoint, buf, URBBUF_MAX, timeout);
140
141 assert(res >= 0);
142 off = 0; // no header on subsequent chunks
143 }
144 }
145
146 int main(int argc, char *argv[]) {
147 if (argc != 2) {
148 fprintf(stderr, "Usage: %s <.jpg file>\n", progname);
149 return EXIT_FAILURE;
150 }
151
152 struct usb_device *dev = find_dev(index);
153 if (!dev) {
154 fprintf(stderr, "%s: no photo frame device found, exit.\n", progname);
155 exit(EXIT_FAILURE);
156 }
157
158 if (need_switch) {
159 fprintf(stderr, "%s: found %s, trying to switch to custom product mode...\n",
160 ids[have_idx].name, progname);
161
162 usb_dev_handle *udev;
163
164 udev = usb_open(dev);
165 if (!udev) {
166 fprintf(stderr, "%s: failed to open device, exit.\n", progname);
167 exit(EXIT_FAILURE);
168 }
169
170 char buf[254];
171 memset(buf, 0, 254);
172
173 int res = usb_control_msg(udev, USB_TYPE_STANDARD | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
174 0xfe, 0xfe, buf, 0xfe, 1000);
175 fprintf(stderr, "%s: usb_control_msg() = %d\n", progname, res);
176
177 usb_close(udev);
178 usleep(500000);
179 }
180
181 dev = find_dev(index);
182 if (!dev || need_switch) {
183 fprintf(stderr, "%s: no photo frame device found, exit.\n", progname);
184 exit(EXIT_FAILURE);
185 }
186
187 fprintf(stderr, "%s: found %s (%d x %d)\n",
188 progname, ids[have_idx].name, ids[have_idx].width, ids[have_idx].height);
189
190 usb_dev_handle *udev = dev_open(dev);
191
192 FILE *f = fopen(argv[1], "rb");
193 if (f == NULL) {
194 fprintf(stderr, "%s: failed to open file '%s', exit.\n", progname, argv[1]);
195 exit(EXIT_FAILURE);
196 }
197 send_jpeg(f, udev);
198 fclose(f);
199
200 while (1) {
201 char buf[2];
202 int res = usb_control_msg(udev, USB_TYPE_VENDOR | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
203 0x0, 0x0, buf, 0x2, 1000);
204 fprintf(stderr, "%s: usb_control_msg() = %d\n", progname, res);
205
206 usleep(500000);
207 }
208
209 usb_close(udev);
210
211 return EXIT_SUCCESS;
212 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.