diff options
Diffstat (limited to 'drv_dpf.c')
-rw-r--r-- | drv_dpf.c | 478 |
1 files changed, 240 insertions, 238 deletions
@@ -122,11 +122,11 @@ static char Name[] = "DPF"; * Dpf status */ static struct { - unsigned char *lcdBuf; // Display data buffer + unsigned char *lcdBuf; // Display data buffer unsigned char *xferBuf; // USB transfer buffer - DPFAXHANDLE dpfh; // Handle for dpf access - int pwidth; // Physical display width - int pheight; // Physical display height + DPFAXHANDLE dpfh; // Handle for dpf access + int pwidth; // Physical display width + int pheight; // Physical display height // Flags to translate logical to physical orientation int isPortrait; @@ -160,7 +160,7 @@ static struct { static void drv_set_pixel(int x, int y, RGBA pix) { int changed = 0; - + int sx = DCOLS; int sy = DROWS; int lx = x % sx; @@ -168,39 +168,41 @@ static void drv_set_pixel(int x, int y, RGBA pix) if (dpf.flip) { // upside down orientation - lx = DCOLS - 1 - lx; - ly = DROWS - 1 - ly; + lx = DCOLS - 1 - lx; + ly = DROWS - 1 - ly; } if (dpf.rotate90) { - // wrong Orientation, rotate - int i = ly; - ly = dpf.pheight - 1 - lx; - lx = i; + // wrong Orientation, rotate + int i = ly; + ly = dpf.pheight - 1 - lx; + lx = i; } - if (lx < 0 || lx >= (int) dpf.pwidth || ly < 0 || ly >= (int) dpf.pheight) - { - error("dpf: x/y out of bounds (x=%d, y=%d, rot=%d, flip=%d, lx=%d, ly=%d)\n", x, y, dpf.rotate90, dpf.flip, lx, ly); - return; + if (lx < 0 || lx >= (int) dpf.pwidth || ly < 0 || ly >= (int) dpf.pheight) { + error("dpf: x/y out of bounds (x=%d, y=%d, rot=%d, flip=%d, lx=%d, ly=%d)\n", x, y, dpf.rotate90, dpf.flip, lx, + ly); + return; } unsigned char c1 = _RGB565_0(pix); unsigned char c2 = _RGB565_1(pix); unsigned int i = (ly * dpf.pwidth + lx) * DPF_BPP; - if (dpf.lcdBuf[i] != c1 || dpf.lcdBuf[i+1] != c2) - { - dpf.lcdBuf[i] = c1; - dpf.lcdBuf[i+1] = c2; + if (dpf.lcdBuf[i] != c1 || dpf.lcdBuf[i + 1] != c2) { + dpf.lcdBuf[i] = c1; + dpf.lcdBuf[i + 1] = c2; changed = 1; } - if (changed) - { - if (lx < dpf.minx) dpf.minx = lx; - if (lx > dpf.maxx) dpf.maxx = lx; - if (ly < dpf.miny) dpf.miny = ly; - if (ly > dpf.maxy) dpf.maxy = ly; + if (changed) { + if (lx < dpf.minx) + dpf.minx = lx; + if (lx > dpf.maxx) + dpf.maxx = lx; + if (ly < dpf.miny) + dpf.miny = ly; + if (ly > dpf.maxy) + dpf.maxy = ly; } } @@ -259,78 +261,74 @@ static int drv_dpf_start(const char *section) // Get the device dev = cfg_get(section, "Port", NULL); if (dev == NULL || *dev == '\0') { - error("dpf: no '%s.Port' entry from %s", section, cfg_source()); - return -1; + error("dpf: no '%s.Port' entry from %s", section, cfg_source()); + return -1; } - // Get font s = cfg_get(section, "Font", "6x8"); if (s == NULL || *s == '\0') { - error("%s: no '%s.Font' entry from %s", Name, section, cfg_source()); - return -1; + error("%s: no '%s.Font' entry from %s", Name, section, cfg_source()); + return -1; } XRES = -1; YRES = -1; if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) { - error("%s: bad Font '%s' from %s", Name, s, cfg_source()); - return -1; + error("%s: bad Font '%s' from %s", Name, s, cfg_source()); + return -1; } /* we dont want fonts below 6 width */ if (XRES < 6) { - error("%s: bad Font '%s' width '%d' using minimum of 6)", Name, s, XRES); - XRES = 6; + error("%s: bad Font '%s' width '%d' using minimum of 6)", Name, s, XRES); + XRES = 6; } /* we dont want fonts below 8 height */ if (YRES < 8) { - error("%s: bad Font '%s' height '%d' using minimum of 8)", Name, s, YRES); - YRES = 8; + error("%s: bad Font '%s' height '%d' using minimum of 8)", Name, s, YRES); + YRES = 8; } - // Get the logical orientation (0 = landscape, 1 = portrait, 2 = reverse landscape, 3 = reverse portrait) if (cfg_number(section, "Orientation", 0, 0, 3, &i) > 0) - dpf.orientation = i; + dpf.orientation = i; else - dpf.orientation = 0; + dpf.orientation = 0; // Get the backlight value (0 = off, 7 = max brightness) if (cfg_number(section, "Backlight", 0, 0, 7, &i) > 0) - dpf.backlight = i; + dpf.backlight = i; else - dpf.backlight = 7; + dpf.backlight = 7; /* open communication with the display */ dpf.dpfh = dpf_ax_open(dev); if (dpf.dpfh == NULL) { - error("dpf: cannot open dpf device %s", dev); - return -1; + error("dpf: cannot open dpf device %s", dev); + return -1; } - // Get dpfs physical dimensions dpf.pwidth = dpf_ax_getwidth(dpf.dpfh); dpf.pheight = dpf_ax_getheight(dpf.dpfh); - + // See, if we have to rotate the display dpf.isPortrait = dpf.pwidth < dpf.pheight; if (dpf.isPortrait) { if (dpf.orientation == 0 || dpf.orientation == 2) dpf.rotate90 = 1; - } - else - if (dpf.orientation == 1 || dpf.orientation == 3) - dpf.rotate90 = 1; - dpf.flip = (!dpf.isPortrait && dpf.rotate90); // adjust to make rotate por/land = physical por/land - if (dpf.orientation > 1) dpf.flip = !dpf.flip; + } else if (dpf.orientation == 1 || dpf.orientation == 3) + dpf.rotate90 = 1; + dpf.flip = (!dpf.isPortrait && dpf.rotate90); // adjust to make rotate por/land = physical por/land + if (dpf.orientation > 1) + dpf.flip = !dpf.flip; // allocate display buffer + temp transfer buffer dpf.lcdBuf = malloc(dpf.pwidth * dpf.pheight * DPF_BPP); dpf.xferBuf = malloc(dpf.pwidth * dpf.pheight * DPF_BPP); // clear display buffer + set it to "dirty" - memset(dpf.lcdBuf, 0, dpf.pwidth * dpf.pheight * DPF_BPP); //Black + memset(dpf.lcdBuf, 0, dpf.pwidth * dpf.pheight * DPF_BPP); //Black dpf.minx = 0; dpf.maxx = dpf.pwidth - 1; dpf.miny = 0; @@ -342,7 +340,7 @@ static int drv_dpf_start(const char *section) // Set backlight (brightness) dpf_ax_setbacklight(dpf.dpfh, dpf.backlight); - + return 0; } @@ -354,9 +352,11 @@ static int drv_dpf_start(const char *section) static void plugin_backlight(RESULT * result, RESULT * arg1) { int b = R2N(arg1); - if (b < 0) b = 0; - if (b > 7) b = 7; - + if (b < 0) + b = 0; + if (b > 7) + b = 7; + dpf_ax_setbacklight(dpf.dpfh, b); SetResult(&result, R_NUMBER, &b); } @@ -443,11 +443,11 @@ DRIVER drv_DPF = { #include <usb.h> -#define AX206_VID 0x1908 // Hacked frames USB Vendor ID -#define AX206_PID 0x0102 // Hacked frames USB Product ID +#define AX206_VID 0x1908 // Hacked frames USB Vendor ID +#define AX206_PID 0x0102 // Hacked frames USB Product ID #define USBCMD_SETPROPERTY 0x01 // USB command: Set property -#define USBCMD_BLIT 0x12 // USB command: Blit to screen +#define USBCMD_BLIT 0x12 // USB command: Blit to screen /* Generic SCSI device stuff */ @@ -456,14 +456,14 @@ DRIVER drv_DPF = { /* The DPF context structure */ typedef -struct dpf_context { - usb_dev_handle *udev; - unsigned int width; - unsigned int height; + struct dpf_context { + usb_dev_handle *udev; + unsigned int width; + unsigned int height; } DPFContext; -static int wrap_scsi(DPFContext *h, unsigned char *cmd, int cmdlen, char out, - unsigned char *data, unsigned long block_len); +static int wrap_scsi(DPFContext * h, unsigned char *cmd, int cmdlen, char out, + unsigned char *data, unsigned long block_len); /** * Open DPF device. @@ -477,89 +477,88 @@ static int wrap_scsi(DPFContext *h, unsigned char *cmd, int cmdlen, char out, */ DPFAXHANDLE dpf_ax_open(const char *dev) { - DPFContext *dpf; - int index = -1; - usb_dev_handle *u; - - if (dev && strlen(dev) == 4 && (strncmp(dev, "usb", 3) == 0 || strncmp(dev, "dpf", 3) == 0)) - index = dev[3] - '0'; - - if (index < 0 || index > 9) { - fprintf(stderr, "dpf_ax_open: wrong device '%s'. Please specify a string like 'usb0'\n", dev); - return NULL; - } + DPFContext *dpf; + int index = -1; + usb_dev_handle *u; - usb_init(); - usb_find_busses(); - usb_find_devices(); - - struct usb_bus *b = usb_get_busses(); - struct usb_device *d = NULL; - int enumeration = 0; - int found = 0; - - while (b && !found) { - d = b->devices; - while (d) { - if ((d->descriptor.idVendor == AX206_VID) && (d->descriptor.idProduct == AX206_PID)) { - fprintf(stderr, "dpf_ax_open: found AX206 #%d\n", enumeration+1); - if (enumeration == index) { - found = 1; - break; - } - else enumeration++; - } - d = d->next; - } - b = b->next; - } + if (dev && strlen(dev) == 4 && (strncmp(dev, "usb", 3) == 0 || strncmp(dev, "dpf", 3) == 0)) + index = dev[3] - '0'; - if (!d) { - fprintf(stderr,"dpf_ax_open: no matching USB device '%s' found!\n", dev); - return NULL; - } + if (index < 0 || index > 9) { + fprintf(stderr, "dpf_ax_open: wrong device '%s'. Please specify a string like 'usb0'\n", dev); + return NULL; + } - dpf = (DPFContext *) malloc(sizeof(DPFContext)); - if (!dpf) { - fprintf(stderr, "dpf_ax_open: error allocation memory.\n"); - return NULL; + usb_init(); + usb_find_busses(); + usb_find_devices(); + + struct usb_bus *b = usb_get_busses(); + struct usb_device *d = NULL; + int enumeration = 0; + int found = 0; + + while (b && !found) { + d = b->devices; + while (d) { + if ((d->descriptor.idVendor == AX206_VID) && (d->descriptor.idProduct == AX206_PID)) { + fprintf(stderr, "dpf_ax_open: found AX206 #%d\n", enumeration + 1); + if (enumeration == index) { + found = 1; + break; + } else + enumeration++; + } + d = d->next; } + b = b->next; + } - u = usb_open(d); - if (u == NULL) { - fprintf(stderr,"dpf_ax_open: failed to open usb device '%s'!\n", dev); - free(dpf); - return NULL; - } + if (!d) { + fprintf(stderr, "dpf_ax_open: no matching USB device '%s' found!\n", dev); + return NULL; + } - if (usb_claim_interface(u, 0) < 0) { - fprintf(stderr,"dpf_ax_open: failed to claim usb device!\n"); - usb_close(u); - free(dpf); - return NULL; - } + dpf = (DPFContext *) malloc(sizeof(DPFContext)); + if (!dpf) { + fprintf(stderr, "dpf_ax_open: error allocation memory.\n"); + return NULL; + } - dpf->udev = u; - - static unsigned char buf[5]; - static unsigned char cmd[16] = { - 0xcd, 0, 0, 0, - 0, 2, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - }; - cmd[5] = 2; // get LCD parameters - if (wrap_scsi(dpf, cmd, sizeof(cmd), DIR_IN, buf, 5) == 0) { - dpf->width = (buf[0]) | (buf[1] << 8); - dpf->height = (buf[2]) | (buf[3] << 8); - fprintf(stderr, "dpf_ax_open: got LCD dimensions: %dx%d\n", dpf->width, dpf->height); - } - else { - fprintf(stderr, "dpf_ax_open: error reading LCD dimensions!\n"); - dpf_ax_close(dpf); - return NULL; - } - return (DPFAXHANDLE) dpf; + u = usb_open(d); + if (u == NULL) { + fprintf(stderr, "dpf_ax_open: failed to open usb device '%s'!\n", dev); + free(dpf); + return NULL; + } + + if (usb_claim_interface(u, 0) < 0) { + fprintf(stderr, "dpf_ax_open: failed to claim usb device!\n"); + usb_close(u); + free(dpf); + return NULL; + } + + dpf->udev = u; + + static unsigned char buf[5]; + static unsigned char cmd[16] = { + 0xcd, 0, 0, 0, + 0, 2, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 + }; + cmd[5] = 2; // get LCD parameters + if (wrap_scsi(dpf, cmd, sizeof(cmd), DIR_IN, buf, 5) == 0) { + dpf->width = (buf[0]) | (buf[1] << 8); + dpf->height = (buf[2]) | (buf[3] << 8); + fprintf(stderr, "dpf_ax_open: got LCD dimensions: %dx%d\n", dpf->width, dpf->height); + } else { + fprintf(stderr, "dpf_ax_open: error reading LCD dimensions!\n"); + dpf_ax_close(dpf); + return NULL; + } + return (DPFAXHANDLE) dpf; } /** @@ -568,11 +567,11 @@ DPFAXHANDLE dpf_ax_open(const char *dev) void dpf_ax_close(DPFAXHANDLE h) { - DPFContext *dpf = (DPFContext *) h; - - usb_release_interface(dpf->udev, 0); - usb_close(dpf->udev); - free(dpf); + DPFContext *dpf = (DPFContext *) h; + + usb_release_interface(dpf->udev, 0); + usb_close(dpf->udev); + free(dpf); } /** Get screen width. @@ -581,7 +580,7 @@ void dpf_ax_close(DPFAXHANDLE h) */ int dpf_ax_getwidth(DPFAXHANDLE h) { - return ((DPFContext *) h)->width; + return ((DPFContext *) h)->width; } /** Get screen height. @@ -590,15 +589,15 @@ int dpf_ax_getwidth(DPFAXHANDLE h) */ int dpf_ax_getheight(DPFAXHANDLE h) { - return ((DPFContext *) h)->height; + return ((DPFContext *) h)->height; } static unsigned char g_excmd[16] = { - 0xcd, 0, 0, 0, - 0, 6, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 + 0xcd, 0, 0, 0, + 0, 6, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 }; /** Blit data to screen. @@ -608,22 +607,22 @@ unsigned char g_excmd[16] = { */ void dpf_ax_screen_blit(DPFAXHANDLE h, const unsigned char *buf, short rect[4]) { - unsigned long len = (rect[2] - rect[0]) * (rect[3] - rect[1]); - len <<= 1; - unsigned char *cmd = g_excmd; - - cmd[6] = USBCMD_BLIT; - cmd[7] = rect[0]; - cmd[8] = rect[0] >> 8; - cmd[9] = rect[1]; - cmd[10] = rect[1] >> 8; - cmd[11] = rect[2] - 1; - cmd[12] = (rect[2] - 1) >> 8; - cmd[13] = rect[3] - 1; - cmd[14] = (rect[3] - 1) >> 8; - cmd[15] = 0; - - wrap_scsi((DPFContext *) h, cmd, sizeof(g_excmd), DIR_OUT, (unsigned char*) buf, len); + unsigned long len = (rect[2] - rect[0]) * (rect[3] - rect[1]); + len <<= 1; + unsigned char *cmd = g_excmd; + + cmd[6] = USBCMD_BLIT; + cmd[7] = rect[0]; + cmd[8] = rect[0] >> 8; + cmd[9] = rect[1]; + cmd[10] = rect[1] >> 8; + cmd[11] = rect[2] - 1; + cmd[12] = (rect[2] - 1) >> 8; + cmd[13] = rect[3] - 1; + cmd[14] = (rect[3] - 1) >> 8; + cmd[15] = 0; + + wrap_scsi((DPFContext *) h, cmd, sizeof(g_excmd), DIR_OUT, (unsigned char *) buf, len); } /** Set backlight @@ -632,91 +631,94 @@ void dpf_ax_screen_blit(DPFAXHANDLE h, const unsigned char *buf, short rect[4]) */ void dpf_ax_setbacklight(DPFAXHANDLE h, int b) { - unsigned char *cmd = g_excmd; + unsigned char *cmd = g_excmd; - if (b < 0) b = 0; - if (b > 7) b = 7; + if (b < 0) + b = 0; + if (b > 7) + b = 7; - cmd[6] = USBCMD_SETPROPERTY; - cmd[7] = 0x01; // PROPERTY_BRIGHTNESS - cmd[8] = 0x00; //PROPERTY_BRIGHTNESS >> 8; - cmd[9] = b; - cmd[10] = b >> 8; + cmd[6] = USBCMD_SETPROPERTY; + cmd[7] = 0x01; // PROPERTY_BRIGHTNESS + cmd[8] = 0x00; //PROPERTY_BRIGHTNESS >> 8; + cmd[9] = b; + cmd[10] = b >> 8; - wrap_scsi((DPFContext *) h, cmd, sizeof(g_excmd), DIR_OUT, NULL, 0); + wrap_scsi((DPFContext *) h, cmd, sizeof(g_excmd), DIR_OUT, NULL, 0); } static unsigned char g_buf[] = { - 0x55, 0x53, 0x42, 0x43, // dCBWSignature - 0xde, 0xad, 0xbe, 0xef, // dCBWTag - 0x00, 0x80, 0x00, 0x00, // dCBWLength - 0x00, // bmCBWFlags: 0x80: data in (dev to host), 0x00: Data out - 0x00, // bCBWLUN - 0x10, // bCBWCBLength - - // SCSI cmd: - 0xcd, 0x00, 0x00, 0x00, - 0x00, 0x06, 0x11, 0xf8, - 0x70, 0x00, 0x40, 0x00, - 0x00, 0x00, 0x00, 0x00, + 0x55, 0x53, 0x42, 0x43, // dCBWSignature + 0xde, 0xad, 0xbe, 0xef, // dCBWTag + 0x00, 0x80, 0x00, 0x00, // dCBWLength + 0x00, // bmCBWFlags: 0x80: data in (dev to host), 0x00: Data out + 0x00, // bCBWLUN + 0x10, // bCBWCBLength + + // SCSI cmd: + 0xcd, 0x00, 0x00, 0x00, + 0x00, 0x06, 0x11, 0xf8, + 0x70, 0x00, 0x40, 0x00, + 0x00, 0x00, 0x00, 0x00, }; #define ENDPT_OUT 1 #define ENDPT_IN 0x81 -static int wrap_scsi(DPFContext *h, unsigned char *cmd, int cmdlen, char out, - unsigned char *data, unsigned long block_len) +static int wrap_scsi(DPFContext * h, unsigned char *cmd, int cmdlen, char out, + unsigned char *data, unsigned long block_len) { - int len; - int ret; - static unsigned char ansbuf[13]; // Do not change size. - - g_buf[14] = cmdlen; - memcpy(&g_buf[15], cmd, cmdlen); - - g_buf[8] = block_len; - g_buf[9] = block_len >> 8; - g_buf[10] = block_len >> 16; - g_buf[11] = block_len >> 24; - - ret = usb_bulk_write(h->udev, ENDPT_OUT, (const char *)g_buf, sizeof(g_buf), 1000); - if (ret < 0) return ret; - - if (out == DIR_OUT) { - if (data) { - ret = usb_bulk_write(h->udev, ENDPT_OUT, (const char* )data, block_len, 3000); - if (ret != (int) block_len) { - fprintf(stderr, "dpf_ax ERROR: bulk write.\n"); - return ret; - } - } - } else if (data) { - ret = usb_bulk_read(h->udev, ENDPT_IN, (char *) data, block_len, 4000); - if (ret != (int) block_len) { - fprintf(stderr, "dpf_ax ERROR: bulk read.\n"); - return ret; - } + int len; + int ret; + static unsigned char ansbuf[13]; // Do not change size. + + g_buf[14] = cmdlen; + memcpy(&g_buf[15], cmd, cmdlen); + + g_buf[8] = block_len; + g_buf[9] = block_len >> 8; + g_buf[10] = block_len >> 16; + g_buf[11] = block_len >> 24; + + ret = usb_bulk_write(h->udev, ENDPT_OUT, (const char *) g_buf, sizeof(g_buf), 1000); + if (ret < 0) + return ret; + + if (out == DIR_OUT) { + if (data) { + ret = usb_bulk_write(h->udev, ENDPT_OUT, (const char *) data, block_len, 3000); + if (ret != (int) block_len) { + fprintf(stderr, "dpf_ax ERROR: bulk write.\n"); + return ret; + } } - // get ACK: - len = sizeof(ansbuf); - int retry = 0; - int timeout = 0; - do { - timeout = 0; - ret = usb_bulk_read(h->udev, ENDPT_IN, (char *) ansbuf, len, 5000); - if (ret != len) { - fprintf(stderr, "dpf_ax ERROR: bulk ACK read.\n"); - timeout = 1; - } - retry++; - } while (timeout && retry < 5); - if (strncmp((char *) ansbuf, "USBS", 4)) { - fprintf(stderr, "dpf_ax ERROR: got invalid reply\n."); - return -1; + } else if (data) { + ret = usb_bulk_read(h->udev, ENDPT_IN, (char *) data, block_len, 4000); + if (ret != (int) block_len) { + fprintf(stderr, "dpf_ax ERROR: bulk read.\n"); + return ret; } - // pass back return code set by peer: - return ansbuf[12]; + } + // get ACK: + len = sizeof(ansbuf); + int retry = 0; + int timeout = 0; + do { + timeout = 0; + ret = usb_bulk_read(h->udev, ENDPT_IN, (char *) ansbuf, len, 5000); + if (ret != len) { + fprintf(stderr, "dpf_ax ERROR: bulk ACK read.\n"); + timeout = 1; + } + retry++; + } while (timeout && retry < 5); + if (strncmp((char *) ansbuf, "USBS", 4)) { + fprintf(stderr, "dpf_ax ERROR: got invalid reply\n."); + return -1; + } + // pass back return code set by peer: + return ansbuf[12]; } //################################################################### |