Mercurial > hg > minimon
changeset 0:71b9540bdd23
starting, importing Woo's code and adding minimon
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Sat, 07 May 2011 17:29:58 +0200 |
parents | |
children | 111d4bbce605 |
files | README.txt controlmsg.c minimon.c playusb.c |
diffstat | 4 files changed, 343 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Sat May 07 17:29:58 2011 +0200 @@ -0,0 +1,2 @@ + +based on Grace Woo's work: http://web.media.mit.edu/~gracewoo/stuff/picframe/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/controlmsg.c Sat May 07 17:29:58 2011 +0200 @@ -0,0 +1,73 @@ +/* $Id: usbreplay.c,v 1.7 2004/02/07 17:02:32 bd Exp $ */ +/* Use only with the Hauppauge WinTV PVR usb2, VID/PID 2040/2900. */ +/* No commercial affiliation, warranty, copyright, party invitation, */ +/* fitness or non-fitness, implied or otherwise, is claimed by this comment. */ +/* Compile with -lusb, then put it where capture.pl will find it. */ + +#include <usb.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <fcntl.h> + +struct usb_device *find_first_pvr() { + struct usb_bus *bus; + struct usb_device *dev; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + + for (bus = usb_busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if (dev->descriptor.idVendor == 0x04e8 && + dev->descriptor.idProduct == 0x2027) { + fprintf(stderr, "Samsung photoframe in Mass Storage mode found ...\n"); + return dev; + } + else if (dev->descriptor.idVendor == 0x04e8 && + dev->descriptor.idProduct == 0x2028){ + fprintf(stderr, "Samsung photoframe in Custom Product mode found ...\n"); + return NULL; + } + } + } + fprintf(stderr, "No Samsung device found ...\n"); + return NULL; +} + + +int replay() { + int res = -1; + char buf[256]; + usb_dev_handle *udev; + struct usb_device *dev = NULL; + int numeps = 0; + + dev = find_first_pvr(); + if (dev == NULL) { + fprintf(stderr, "Since no Samsung device in Mass Storage mode found, not going to do anything\n"); + return 0; + } + udev = usb_open(dev); + + setuid(getuid()); + + 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); + + char blah[254]; + memset(blah,0,254); + + res = usb_control_msg(udev, USB_TYPE_STANDARD | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, 0xfe, 0xfe, blah, 0xfe, 1000); + printf("usb_control_msg() = %d\n",res); + + fprintf(stderr, "Just switched to 0x2028 Custom Product mode\n"); + return 0; +} + + +int main(int argc, char *argv[]) { + return replay(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/minimon.c Sat May 07 17:29:58 2011 +0200 @@ -0,0 +1,124 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <assert.h> +#include "usb.h" + +static const char *progname = "minimon"; + +struct usb_device *find_dev() { + struct usb_bus *bus; + struct usb_device *dev; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + + 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; + } + } + } + + return NULL; +} + +usb_dev_handle *dev_open(struct usb_device *dev) { + int res = -1; + char buf[256]; + usb_dev_handle *udev; + int numeps = 0; + + udev = usb_open(dev); + if (!udev) { + fprintf(stderr, "%s: failed to open device, exit.\n", progname); + exit(EXIT_FAILURE); + } + + setuid(getuid()); + + res = usb_set_configuration(udev, 1); + + usb_claim_interface(udev, 0); + numeps = dev->config[0].interface[0].altsetting[0].bNumEndpoints; + if (numeps == 0) { + fprintf(stderr, "%s: no endpoints, exit.\n", progname); + exit(EXIT_FAILURE); + } + + 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); + + { + int eplist[] = { 0x2, 0x81, 0x83 }; + int eplength = sizeof(eplist)/sizeof(eplist[0]); + int *endpoint = eplist; + int i; + for (i=0; i<eplength; i++) { + res = usb_resetep(udev, *endpoint); + res = usb_clear_halt(udev, *endpoint); + endpoint++; + } + } + + return udev; +} + + +void send_jpeg(FILE *f, usb_dev_handle *udev) { + fseek(f, 0, SEEK_END); + int sz = ftell(f); + fseek(f, 0, SEEK_SET); + + #define URBBUF_MAX 0x20000 + char buf[URBBUF_MAX]; + + #define HDR_LEN 12 + char hdr[HDR_LEN] = {0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff, 0xff, 0xff, 0x48, 0x00, 0x00, 0x00}; + *(int *)(hdr+4) = sz; + + memcpy(buf, hdr, HDR_LEN); + int off = HDR_LEN; + + while(!feof(f)) { + int nr = fread(buf+off, 1, URBBUF_MAX - off, f); + if (nr < 0) break; + // pad + memset(buf + off + nr, 0, URBBUF_MAX - off - nr); + + // write it out chunk by chunk + int timeout = 1000; + int endpoint = 0x2; + int res = usb_bulk_write(udev, endpoint, buf, URBBUF_MAX, timeout); + + assert(res >= 0); + off = 0; // no header on subsequent chunks + } +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s FILE", progname); + return EXIT_FAILURE; + } + + struct usb_device *dev = find_dev(index); + assert(dev != NULL); + + usb_dev_handle *udev = dev_open(dev); + + FILE *f = fopen(argv[1], "rb"); + if (f == NULL) { + fprintf(stderr, "%s: failed to open file '%s', exit.\n", progname, argv[1]); + exit(EXIT_FAILURE); + } + send_jpeg(f, udev); + fclose(f); + + return EXIT_SUCCESS; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/playusb.c Sat May 07 17:29:58 2011 +0200 @@ -0,0 +1,144 @@ +/** + * based on usbreplay.c + * -- takes a list of jpeg filenames (on stdin) and sends them to the device index X + * + */ + +#include <usb.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <assert.h> + +struct usb_device *find_pvr(int index) { + struct usb_bus *bus; + struct usb_device *dev; + + usb_init(); + //usb_set_debug(999999); + usb_find_busses(); + usb_find_devices(); + + while(index >= 0) { + 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) { + if (index == 0) + return dev; + --index; + } + } + } + } + return NULL; +} + +usb_dev_handle *dev_open(struct usb_device *dev) { + int res = -1; + char buf[256]; + usb_dev_handle *udev; + int numeps = 0; + + udev = usb_open(dev); + + setuid(getuid()); + + res = usb_set_configuration(udev, 1); + + usb_claim_interface(udev, 0); + numeps = dev->config[0].interface[0].altsetting[0].bNumEndpoints; + if (numeps == 0) { + fprintf(stderr, "** Did you forget to initialize the FX2 firmware with usbreplay -i?\n"); + exit(1); + } + + 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); + + { + int eplist[] = { 0x2, 0x81, 0x83 }; + int eplength = sizeof(eplist)/sizeof(eplist[0]); + int *endpoint = eplist; + int i; + for (i=0; i<eplength; i++) { + res = usb_resetep(udev, *endpoint); + res = usb_clear_halt(udev, *endpoint); + endpoint++; + } + } + + return udev; +} + + +void send_jpeg(FILE *f, usb_dev_handle *udev) { + fseek(f, 0, SEEK_END); + int sz = ftell(f); + fseek(f, 0, SEEK_SET); + + #define URBBUF_MAX 0x20000 + char buf[URBBUF_MAX]; + + #define HDR_LEN 12 + char hdr[HDR_LEN] = {0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff, 0xff, 0xff, 0x48, 0x00, 0x00, 0x00}; + *(int *)(hdr+4) = sz; + + memcpy(buf, hdr, HDR_LEN); + int off = HDR_LEN; + + //printf("new file of size %d\n", sz); + while(!feof(f)) { + int nr = fread(buf+off, 1, URBBUF_MAX - off, f); + if (nr < 0) break; + // pad + memset(buf + off + nr, 0, URBBUF_MAX - off - nr); + + // write it out chunk by chunk + int timeout = 1000; + int endpoint = 0x2; + int res = usb_bulk_write(udev, endpoint, buf, URBBUF_MAX, timeout); + //printf("writing chunk\n"); + assert(res >= 0); + off = 0; // no header on subsequent chunks + } +} + +int main(int argc, char *argv[]) { + int index = 0; + if (argc > 1) { + if (sscanf(argv[1], "%d", &index) != 1) { + fprintf(stderr, "Usage: %s [device index] < filelist", argv[0]); + return -1; + } + } + + struct usb_device *dev = find_pvr(index); + assert (dev != NULL); + + usb_dev_handle *udev = dev_open(dev); + + while(1) { + // for each file in the list + while(!feof(stdin)) { + char fn[256]; + fgets(fn, 256, stdin); + int nn = strlen(fn); + if (nn > 0) { + if (fn[nn-1] == '\n') + fn[nn-1] = 0; + FILE *f = fopen(fn, "r"); + if (f == NULL) { + fprintf(stderr, "file %s not found\n", fn); + return 2; + } + send_jpeg(f, udev); + fclose(f); + } + } + fseek(stdin, 0, SEEK_SET); + } + return 0; +}