Mercurial > hg > minimon
diff minimon.c @ 1:111d4bbce605
improving
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Sat, 07 May 2011 19:00:43 +0200 |
parents | 71b9540bdd23 |
children | bac8ed8d6eb9 |
line wrap: on
line diff
--- a/minimon.c Sat May 07 17:29:58 2011 +0200 +++ b/minimon.c Sat May 07 19:00:43 2011 +0200 @@ -7,7 +7,50 @@ static const char *progname = "minimon"; -struct usb_device *find_dev() { +static int need_switch = 0; +static int have_idx = -1; + +typedef struct { + int mass_id; + int custom_id; + const char *name; + int width; + int height; +} id_info_t; + +static id_info_t ids[] = { + {0x2027, 0x2028, }, + {0xffff, 0xffff, "SPF-75H", 800, 480}, + {0xffff, 0xffff, "SPF-83H", 800, 600}, // 85P?? + {0xffff, 0xffff, "SPF-85H", 800, 600}, // 85P?? + {0x2033, 0x2034, "SPF-87H", 800, 480}, + {0x2035, 0x2036, "SPF-107H", 1024, 600}, + {0, 0, } // end-of-list +}; + +static int in_list(int id, id_info_t *list) { + if (!list) + return 0; + + int idx = 0; + while (list->mass_id || list->custom_id) { + if (id == list->mass_id) { + // still in mass-storage mode, need to switch + need_switch = 1; + return idx; + } + else if (id == list->custom_id) { + need_switch = 0; + return idx; + } + idx++; + list++; + } + + return -1; +} + +static struct usb_device *find_dev() { struct usb_bus *bus; struct usb_device *dev; @@ -17,9 +60,13 @@ for (bus = usb_busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { - if (dev->descriptor.idVendor == 0x04e8 && - dev->descriptor.idProduct == 0x2028) { - return dev; + if (dev->descriptor.idVendor == 0x04e8) { + // found a Samsung device, good + int idx = -1; + if ((idx = in_list(dev->descriptor.idProduct, ids)) >= 0) { + have_idx = idx; + return dev; + } } } } @@ -27,7 +74,7 @@ return NULL; } -usb_dev_handle *dev_open(struct usb_device *dev) { +static usb_dev_handle *dev_open(struct usb_device *dev) { int res = -1; char buf[256]; usb_dev_handle *udev; @@ -52,14 +99,14 @@ strcpy(buf, "** no string **"); res = usb_get_string_simple(udev, dev->descriptor.iManufacturer, buf, sizeof(buf)); - fprintf(stderr, "usb_get_string_simple => %d, %s\n", res, buf); + // fprintf(stderr, "usb_get_string_simple => %d, %s\n", res, buf); { int eplist[] = { 0x2, 0x81, 0x83 }; int eplength = sizeof(eplist)/sizeof(eplist[0]); int *endpoint = eplist; int i; - for (i=0; i<eplength; i++) { + for (i = 0; i < eplength; i++) { res = usb_resetep(udev, *endpoint); res = usb_clear_halt(udev, *endpoint); endpoint++; @@ -69,8 +116,7 @@ return udev; } - -void send_jpeg(FILE *f, usb_dev_handle *udev) { +static void send_jpeg(FILE *f, usb_dev_handle *udev) { fseek(f, 0, SEEK_END); int sz = ftell(f); fseek(f, 0, SEEK_SET); @@ -103,15 +149,53 @@ int main(int argc, char *argv[]) { if (argc != 2) { - fprintf(stderr, "Usage: %s FILE", progname); + fprintf(stderr, "Usage: %s <.jpg file>\n", progname); return EXIT_FAILURE; } struct usb_device *dev = find_dev(index); - assert(dev != NULL); + if (!dev) { + fprintf(stderr, "%s: no photo frame device found, exit.\n", progname); + exit(EXIT_FAILURE); + } + + if (need_switch) { + fprintf(stderr, "%s: found %s, trying to switch to custom product mode...\n", + ids[have_idx].name, progname); + + usb_dev_handle *udev; + + udev = usb_open(dev); + if (!udev) { + fprintf(stderr, "%s: failed to open device, exit.\n", progname); + exit(EXIT_FAILURE); + } + + char buf[254]; + memset(buf, 0, 254); + + int res = usb_control_msg(udev, USB_TYPE_STANDARD | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, + 0xfe, 0xfe, buf, 0xfe, 1000); + fprintf(stderr, "%s: usb_control_msg() = %d\n", progname, res); + + usb_close(udev); + } + + dev = find_dev(index); + if (!dev || need_switch) { + fprintf(stderr, "%s: no photo frame device found, exit.\n", progname); + exit(EXIT_FAILURE); + } + + fprintf(stderr, "%s: found %s (%d x %d)\n", + progname, ids[have_idx].name, ids[have_idx].width, ids[have_idx].height); usb_dev_handle *udev = dev_open(dev); +// int res = usb_control_msg(udev, USB_TYPE_STANDARD | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, +// 0xfe, 0x0, buf, 0x0, 1000); +// fprintf(stderr, "%s: usb_control_msg() = %d\n", progname, res); + FILE *f = fopen(argv[1], "rb"); if (f == NULL) { fprintf(stderr, "%s: failed to open file '%s', exit.\n", progname, argv[1]);