summaryrefslogtreecommitdiff
path: root/src/firmware.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/firmware.c')
-rw-r--r--src/firmware.c401
1 files changed, 401 insertions, 0 deletions
diff --git a/src/firmware.c b/src/firmware.c
new file mode 100644
index 0000000..fb7a7cc
--- /dev/null
+++ b/src/firmware.c
@@ -0,0 +1,401 @@
+/*
+ * firmware.c - FX2 and CX23416 firmware download
+ *
+ * Part of mpegcap.
+ *
+ * FX2 firmware uses the EZ-USB standard 0xA0 control transfer (cold state
+ * only). CX23416 firmware is pushed through the FX2 command channel and
+ * then bulk-transferred to EP 0x02, 32-bit word swapped.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#if defined(_WIN32)
+#include <windows.h>
+#endif
+
+#include "pvrusb2.h"
+
+/* ================================================================== */
+/* FX2 firmware (EZ-USB renumeration) */
+/* ================================================================== */
+
+int pvr2_upload_fx2(pvr2_dev *p, const uint8_t *fw, size_t len)
+{
+ size_t off;
+ uint8_t one = 1, zero = 0;
+
+ if (len != 0x2000 && len != 0x4000) {
+ fprintf(stderr, "unexpected FX2 firmware size %zu "
+ "(expected 8192 or 16384)\n", len);
+ return -1;
+ }
+
+ /* CPU reset assert: bRequest 0xA0, wValue 0xE600 (CPUCS), data = 1 */
+ if (libusb_control_transfer(p->dev, 0x40, 0xA0, 0xE600, 0,
+ &one, 1, 1000) < 0) {
+ fprintf(stderr, "FX2 reset assert failed\n");
+ return -1;
+ }
+
+ /* Write firmware in 0x800 byte chunks, wValue = target address */
+ for (off = 0; off < len; off += 0x800) {
+ size_t chunk = (len - off) < 0x800 ? (len - off) : 0x800;
+ if (libusb_control_transfer(p->dev, 0x40, 0xA0, (uint16_t)off, 0,
+ (unsigned char *)(fw + off),
+ (uint16_t)chunk, 1000) < 0) {
+ fprintf(stderr, "FX2 download failed at 0x%zx\n", off);
+ return -1;
+ }
+ }
+
+ /* CPU reset release -> device re-enumerates */
+ if (libusb_control_transfer(p->dev, 0x40, 0xA0, 0xE600, 0,
+ &zero, 1, 1000) < 0) {
+ fprintf(stderr, "FX2 reset release failed\n");
+ return -1;
+ }
+
+ fprintf(stderr, "FX2 firmware %zu bytes downloaded, re-enumerating...\n", len);
+ return 0;
+}
+
+/* ================================================================== */
+/* CX23416 encoder firmware */
+/* ================================================================== */
+
+int pvr2_upload_firmware2(pvr2_dev *p, const uint8_t *fw, size_t len)
+{
+ uint8_t cmd;
+ size_t off;
+ int total = 0;
+ /* Linux pvrusb2 uses 0x2000 byte chunks (FIRMWARE_CHUNK_SIZE);
+ * mpegcapt.c used 0x8000. Both are accepted by the FX2, but the
+ * smaller size matches the mainline driver and is more forgiving
+ * of host scheduling jitter. */
+ const size_t CHUNK = 0x2000;
+ uint8_t *buf;
+ size_t len4;
+
+ if (len % 4) {
+ fprintf(stderr, "CX23416 firmware size must be a multiple of 4\n");
+ return -1;
+ }
+
+ /* 1) Encoder resume / power on */
+ /*
+ * ENCPWRRST=1 - cut power to the encoder first, then resume. This
+ * is the strongest software reset available when a botched register
+ * order has left the CX23416 unable to boot its firmware (a plain
+ * re-run, FX2RESET and even a USB port reset all failed to recover
+ * it on 2026-10-14).
+ */
+ if (getenv("ENCPWRRST")) {
+ struct timespec ts = { 1, 0 };
+
+ fprintf(stderr, "ENCPWRRST: cutting encoder power before resume\n");
+ (void)pvr2_simple_cmd(p, FX2CMD_POWER_OFF, NULL, 0);
+ nanosleep(&ts, NULL);
+ }
+
+ cmd = FX2CMD_POWER_ON; /* 0xde */
+ if (pvr2_simple_cmd(p, cmd, NULL, 0) < 0)
+ fprintf(stderr, "POWER_ON command failed\n");
+
+ /* FX2 keeps an IN report pending after some commands. The reference
+ * sequence (mpegcapt.c) issues GET_USB_SPEED (0x0b) and
+ * GET_EEPROM_ADDR (0xeb) and drains the response on EP 0x81 before
+ * touching the encoder. Skipping this leaves stale data queued on
+ * the control-IN endpoint and the encoder never starts. */
+ {
+ uint8_t tmp[64];
+ cmd = FX2CMD_GET_USB_SPEED; /* 0x0b */
+ pvr2_simple_cmd(p, cmd, NULL, 0);
+ bulk_cmd_in(p, tmp, sizeof(tmp));
+ cmd = FX2CMD_GET_EEPROM_ADDR; /* 0xeb */
+ pvr2_simple_cmd(p, cmd, NULL, 0);
+ bulk_cmd_in(p, tmp, sizeof(tmp));
+ }
+
+ /* The reference sequence probes the video decoder over I2C (device
+ * 0x44 = CX25843) before initialising the encoder registers. These
+ * reads are not cosmetic: the CX23416 I2C master clock has to be
+ * exercised before the encoder PLL/registers come alive. Skipping
+ * them leaves the encoder CPU halted (MBOX "go" word never clears). */
+ {
+ uint8_t idb[4];
+ (void)pvr2_i2c_read(p, CX2584X_I2C_ADDR, 0x0100, idb, 2);
+ (void)pvr2_i2c_read(p, CX2584X_I2C_ADDR, 0x040d, idb, 1);
+ (void)pvr2_i2c_read(p, CX2584X_I2C_ADDR, 0x040e, idb, 1);
+ }
+
+ /*
+ * AUDIOEARLY=1 reproduces the Windows driver's position for the whole
+ * audio/decoder setup: the USB capture shows it happens here, after
+ * the 0x0b/0xde handshake and the I2C probe but BEFORE the CX23416
+ * firmware image is transferred.
+ *
+ * Captured order (docs/xp-init-sequence.txt):
+ * 0x0159=0x23,0x21 0x015d=0x23,0x21 0x015b=0x00,0x10
+ * 0x015f=0x00,0x06 0x0134=0x01 0x0135=0x18 0x0136=0x0a
+ * 0x0137=0x00 0x013c=0x01,0x00
+ * 0x0115=0x8c 0x0116=0x07 0x0118=0x02
+ * -> 0x0c block writes (video, PLL, audio)
+ * -> firmware image
+ *
+ * This tool has always written 0x0115/0x0116 after START_CAPTURE, and
+ * 0x0115=0x8c there wedges the stream to 0 bytes -- which is why the
+ * value was recorded as "poison". Written here it is fine (measured),
+ * so the position, not the value, was the problem.
+ */
+ if (getenv("AUDIOEARLY")) {
+ static const struct { uint16_t r; uint8_t v; } seq[] = {
+ { 0x0159, 0x23 }, { 0x0159, 0x21 },
+ { 0x015d, 0x23 }, { 0x015d, 0x21 },
+ { 0x015b, 0x00 }, { 0x015b, 0x10 },
+ { 0x015f, 0x00 }, { 0x015f, 0x06 },
+ { 0x0134, 0x01 }, { 0x0135, 0x18 }, { 0x0136, 0x0a },
+ { 0x0137, 0x00 },
+ { 0x013c, 0x01 }, { 0x013c, 0x00 },
+ { 0x0115, 0x8c }, { 0x0116, 0x07 }, { 0x0118, 0x02 },
+ };
+ size_t k;
+ int rc;
+ fprintf(stderr, "AUDIOEARLY: decoder/audio setup before the "
+ "firmware image (driver order)\n");
+ for (k = 0; k < sizeof(seq) / sizeof(seq[0]); k++)
+ pvr2_i2c_write(p, CX2584X_I2C_ADDR, seq[k].r,
+ &seq[k].v, 1);
+
+ /*
+ * DEV6043=1 - two I2C writes the driver makes right here, after
+ * its 0x0108-0x0113 block and before 0x0808 (capture #47-48):
+ * dev 0x60, reg 0x0650, 3 data bytes ce 01 a0
+ * dev 0x43, reg 0x0016, 2 data bytes 10 44
+ * This tool has never addressed either device. The second one
+ * is interesting: the capture shows its completion status as
+ * **0x07, not 0x08**, i.e. the driver's own write is not
+ * acknowledged -- so whatever 0x43 is, it either is not present
+ * or does not accept that transfer. Replicated literally
+ * (reg 0x0650 with 3 data bytes reproduces the wire bytes
+ * exactly, whatever the device's real register width is).
+ */
+ if (getenv("DEV6043")) {
+ static const uint8_t d60[3] = { 0xce, 0x01, 0xa0 };
+ static const uint8_t d43[2] = { 0x10, 0x44 };
+ int a = pvr2_i2c_write(p, 0x60, 0x0650, d60, 3);
+ int b = pvr2_i2c_write(p, 0x43, 0x0016, d43, 2);
+ fprintf(stderr, "DEV6043: 0x60/0650 -> %s, 0x43/0016 -> %s\n",
+ a < 0 ? "NACK" : "ACK", b < 0 ? "NACK" : "ACK");
+ }
+ /*
+ * D0UPLOAD=1 - the driver sends a 12982-byte image on EP 0x02
+ * *between* 0xd0 and 0xd1. This tool's AUDIOFX2 path sends
+ * those two commands back to back with nothing in between, so
+ * the image has never been uploaded. Captured at
+ * /ROM/rz2-usb.pcap events 298-308 (0xd0, three EP 0x02
+ * transfers of 5120+4096+3766 bytes, 0xd1), saved as
+ * ref/d0_upload_12982.bin.
+ *
+ * The image is 8051 machine code -- it begins `02 2a 77`
+ * (LJMP 2A77h, the classic 8051 reset vector) and contains the
+ * strings "FM signal detected" / "AM signal detected". It is
+ * NOT byte-swapped, unlike the CX23416 image, so it is sent
+ * verbatim.
+ */
+ if (getenv("D0UPLOAD")) {
+ const char *ip = getenv("D0IMG");
+ FILE *fp;
+
+ if (!ip)
+ ip = "ref/d0_upload_12982.bin";
+ fp = fopen(ip, "rb");
+ if (!fp) {
+ fprintf(stderr, "D0UPLOAD: cannot open %s\n", ip);
+ } else {
+ long sz, off2;
+ uint8_t *img;
+ int bad = 0;
+
+ fseek(fp, 0, SEEK_END);
+ sz = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ img = malloc((size_t)sz);
+ if (!img || fread(img, 1, (size_t)sz, fp)
+ != (size_t)sz) {
+ fprintf(stderr, "D0UPLOAD: read failed\n");
+ bad = 1;
+ }
+ fclose(fp);
+ if (!bad) {
+ static const uint8_t d0p[5] =
+ { 0x44, 0x02, 0x08, 0x02, 0x00 };
+ uint8_t tmp[64];
+
+ (void)pvr2_simple_cmd(p, 0xd0, d0p, 5);
+ memset(tmp, 0, sizeof(tmp));
+ (void)bulk_cmd_in(p, tmp, sizeof(tmp));
+ for (off2 = 0; off2 < sz; off2 += 4096) {
+ int chunk = (int)((sz - off2) < 4096
+ ? (sz - off2) : 4096);
+ int tr = 0;
+ int r = libusb_bulk_transfer(
+ p->dev, EP_FW_OUT,
+ img + off2, chunk,
+ &tr, 5000);
+ if (r != 0 || tr != chunk) {
+ fprintf(stderr, "D0UPLOAD: "
+ "chunk at %ld failed "
+ "(r=%d tr=%d)\n",
+ off2, r, tr);
+ bad = 1;
+ break;
+ }
+ }
+ memset(tmp, 0, sizeof(tmp));
+ (void)pvr2_simple_cmd(p, 0xd1, NULL, 0);
+ (void)bulk_cmd_in(p, tmp, sizeof(tmp));
+ fprintf(stderr, "D0UPLOAD: 0xd0 -> %ld bytes"
+ " -> 0xd1 %s\n", sz,
+ bad ? "(errors)" : "ok");
+ }
+ free(img);
+ }
+ }
+ rc = pvr2_audio_block_write(p);
+ fprintf(stderr, "AUDIOEARLY: 0x0c blocks %s\n",
+ rc ? "reported errors" : "ok");
+
+ /* The HR1 pair closes here: the driver clears AUTO_INC_DIS
+ * again once the 0xd0/d1 image has been uploaded. */
+ if (getenv("HR1")) {
+ const char *e = getenv("HR1B");
+ uint8_t b = e ? (uint8_t)strtoul(e, NULL, 0) : 0x04;
+
+ pvr2_i2c_write(p, CX2584X_I2C_ADDR, 0x0000, &b, 1);
+ fprintf(stderr, "HR1: 0x0000 = 0x%02x (AUTO_INC_DIS "
+ "cleared after the image)\n", b);
+ }
+
+ /*
+ * The driver also issues two FX2 commands this tool does not
+ * implement at all, right between the 0x0800/0x0801 writes and
+ * the audio block writes:
+ * 0xd0 payload 44 02 08 02 00
+ * 0xd1 no payload
+ * Both are followed by a response read on EP 0x81. Send them
+ * here, padded to 64 bytes exactly as captured, and drain the
+ * response.
+ */
+ if (getenv("AUDIOFX2")) {
+ uint8_t buf[64];
+ uint8_t tmp[64];
+ memset(buf, 0, sizeof(buf));
+ buf[0] = 0xd0;
+ buf[1] = 0x44; buf[2] = 0x02; buf[3] = 0x08;
+ buf[4] = 0x02; buf[5] = 0x00;
+ (void)pvr2_simple_cmd(p, 0xd0, buf + 1, 5);
+ memset(tmp, 0, sizeof(tmp));
+ (void)bulk_cmd_in(p, tmp, sizeof(tmp));
+ memset(buf, 0, sizeof(buf));
+ buf[0] = 0xd1;
+ (void)pvr2_simple_cmd(p, 0xd1, NULL, 0);
+ memset(tmp, 0, sizeof(tmp));
+ (void)bulk_cmd_in(p, tmp, sizeof(tmp));
+ fprintf(stderr, "AUDIOFX2: 0xd0 / 0xd1 sent\n");
+ }
+ }
+
+ /* 2) interrupt mask + GPIO + deep reset + register init */
+ pvr2_write_reg(p, CX_REG_INTR_MASK, 0xffffffff);
+ pvr2_gpio_dir(p, 0xffffffff, 0x00000088);
+ pvr2_gpio_out(p, 0xffffffff, 0x00000008);
+
+ cmd = FX2CMD_DEEP_RESET; /* 0xdd */
+ pvr2_simple_cmd(p, cmd, NULL, 0);
+
+ pvr2_write_reg(p, CX_REG_APU_CMD, 0x00000000);
+ pvr2_gpio_dir(p, 0xffffffff, 0x00000408);
+ pvr2_gpio_out(p, 0xffffffff, 0x00000008);
+ pvr2_write_reg(p, CX_REG_VPU_CTRL, 0xffffffed);
+ pvr2_write_reg(p, CX_REG_VPU_RESET, 0xfffffffd);
+ /*
+ * SDRAM refresh. The official driver writes 0x80000640 here (capture
+ * #92); this tool has always written 0x80000800. SDRAMREF=<hex>
+ * overrides so the driver's value can be tried.
+ */
+ {
+ const char *e = getenv("SDRAMREF");
+ pvr2_write_reg(p, CX_REG_SDRAM_REFRESH,
+ e ? (uint32_t)strtoul(e, NULL, 0) : 0x80000800);
+ }
+ pvr2_write_reg(p, CX_REG_SDRAM_PRECHARGE, 0x0000001a);
+ pvr2_write_reg(p, CX_REG_I2C_CLOCK, 0x00000000);
+ pvr2_write_reg(p, 0xaa00, 0x00000000);
+ pvr2_write_reg(p, 0xaa04, 0x00057810);
+ pvr2_write_reg(p, 0xaa10, 0x00148500);
+ pvr2_write_reg(p, 0xaa18, 0x00840000);
+
+ /* 3) FWPOST1 + MEMSEL */
+ cmd = FX2CMD_FWPOST1; /* 0x52 */
+ pvr2_simple_cmd(p, cmd, NULL, 0);
+
+ /*
+ * ★ Do NOT move the 0x9054/0x9058 writes up here. A 2026-10-14
+ * attempt (env FWORDER, now removed) put them between FWPOST1 and
+ * the firmware image, believing that was the driver's order; it
+ * broke the boot outright ("encoder command 0x80 timed out",
+ * "firmware not running"). The driver's real order, read from the
+ * capture WITH the EP 0x02 transfers included, is:
+ * FWPOST1 -> transfer -> 0x9054=ffffffff -> 0x9058=ffffffe8 -> MEMSEL
+ * which is exactly what step 5 below does. The earlier mis-reading
+ * came from a parser that only tracked EP 0x01, so the firmware
+ * image was invisible and the VPU writes looked like they preceded
+ * it. scripts/xpcap.py now prints the EP 0x02 transfers.
+ */
+
+ {
+ uint8_t arg = 0x00;
+ cmd = FX2CMD_MEMSEL; /* 0x06 */
+ pvr2_simple_cmd(p, cmd, &arg, 1);
+ }
+
+ /* 4) bulk-transfer firmware to EP 0x02, 0x8000 at a time, swab32 */
+ buf = malloc(CHUNK);
+ if (!buf)
+ return -1;
+ len4 = len & ~(size_t)3;
+
+ for (off = 0; off < len4; off += CHUNK) {
+ size_t chunk = (len4 - off) < CHUNK ? (len4 - off) : CHUNK;
+ int transferred = 0;
+ int r;
+
+ memcpy(buf, fw + off, chunk);
+ pvr2_swap32_buf(buf, chunk / 4);
+
+ r = libusb_bulk_transfer(p->dev, EP_FW_OUT, buf, (int)chunk,
+ &transferred, 5000);
+ if (r != 0 || transferred != (int)chunk) {
+ fprintf(stderr, "CX23416 firmware download failed at "
+ "0x%zx (r=%d tr=%d)\n", off, r, transferred);
+ free(buf);
+ return -1;
+ }
+ total += (int)chunk;
+ }
+ free(buf);
+ fprintf(stderr, "CX23416 firmware %d bytes downloaded\n", total);
+
+ /* 5) post-processing */
+ pvr2_write_reg(p, CX_REG_VPU_RESET, 0xffffffff);
+ pvr2_write_reg(p, CX_REG_VPU_CTRL, 0xffffffe8);
+
+ {
+ uint8_t arg = 0x00;
+ pvr2_simple_cmd(p, FX2CMD_MEMSEL, &arg, 1);
+ }
+ return 0;
+}