summaryrefslogtreecommitdiff
path: root/src/device.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c583
1 files changed, 583 insertions, 0 deletions
diff --git a/src/device.c b/src/device.c
new file mode 100644
index 0000000..88c42cd
--- /dev/null
+++ b/src/device.c
@@ -0,0 +1,583 @@
+/*
+ * device.c - libusb device management, FX2 commands, I2C, MBOX
+ *
+ * Part of mpegcap: cross-platform CX23416/FX2 USB MPEG capture.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#if !defined(_WIN32)
+#include <unistd.h> /* geteuid() */
+#endif
+
+#include "pvrusb2.h"
+
+/* ================================================================== */
+/* Device management */
+/* ================================================================== */
+
+int pvr2_init(pvr2_dev *p)
+{
+ memset(p, 0, sizeof(*p));
+ if (libusb_init(&p->ctx) < 0) {
+ fprintf(stderr, "libusb_init failed\n");
+ return -1;
+ }
+ return 0;
+}
+
+void pvr2_exit(pvr2_dev *p)
+{
+ if (p->dev) {
+ libusb_release_interface(p->dev, 0);
+ libusb_close(p->dev);
+ p->dev = NULL;
+ }
+ if (p->ctx) {
+ libusb_exit(p->ctx);
+ p->ctx = NULL;
+ }
+}
+
+int pvr2_find_device(pvr2_dev *p, uint16_t vid, uint16_t pid)
+{
+ libusb_device **list = NULL;
+ ssize_t cnt;
+ int found = 0;
+ ssize_t i;
+
+ p->vid = vid;
+ p->pid = pid;
+
+ cnt = libusb_get_device_list(p->ctx, &list);
+ if (cnt < 0)
+ return -1;
+
+ for (i = 0; i < cnt; i++) {
+ struct libusb_device_descriptor desc;
+ if (libusb_get_device_descriptor(list[i], &desc) < 0)
+ continue;
+ if (desc.idVendor == vid && desc.idProduct == pid &&
+ desc.bcdDevice != 0x0100) {
+ char manuf[128] = "", prod[128] = "";
+ libusb_device_handle *h = NULL;
+ /* Try to describe the device (best-effort) */
+ if (libusb_open(list[i], &h) == 0) {
+ libusb_get_string_descriptor_ascii(h, desc.iManufacturer,
+ (unsigned char *)manuf, sizeof(manuf));
+ libusb_get_string_descriptor_ascii(h, desc.iProduct,
+ (unsigned char *)prod, sizeof(prod));
+ libusb_close(h);
+ }
+ fprintf(stderr, "device found: %04x:%04x %s %s\n",
+ desc.idVendor, desc.idProduct, manuf, prod);
+ found = 1;
+ break;
+ }
+ }
+ libusb_free_device_list(list, 1);
+
+ if (!found)
+ return -1;
+
+ p->dev = libusb_open_device_with_vid_pid(p->ctx, vid, pid);
+ if (!p->dev) {
+ fprintf(stderr, "cannot open device %04x:%04x "
+ "(permissions? kernel driver attached?)\n", vid, pid);
+ return -1;
+ }
+
+ /*
+ * A cold unit enumerates as the Cypress default 04b4:8613 until the
+ * FX2 firmware is uploaded - the INF calls that image Eagle.bix, and
+ * it is what turns the device into 04bb:0514. Without this fallback
+ * there is nothing to open on a unit that has just been plugged in
+ * after a power cycle, and the only symptom is "device not found".
+ */
+ if (p->verbose)
+ fprintf(stderr, "opened %04x:%04x\n", vid, pid);
+
+#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
+ /* Detach a kernel driver automatically if one is attached.
+ * Linux: usbfs. On FreeBSD there is no kernel driver for this
+ * device.
+ *
+ * IMPORTANT: on FreeBSD this call issues USBDEVFS_CLAIMINTERFACE /
+ * DETACHKERNELDRIVER internally, which needs root. As a normal user
+ * it returns EPERM (-99) and, worse, leaves the interface in a state
+ * where the following claim also fails with -99. So only enable it
+ * when we are root. */
+ if (geteuid() == 0)
+ libusb_set_auto_detach_kernel_driver(p->dev, 1);
+#endif
+
+ /* Claim interface 0. If it is busy (a previous run left the device
+ * in a bad state, or another process holds it), reset and retry.
+ * NOTE: libusb_reset_device() needs root on FreeBSD, so for a normal
+ * user we only retry the claim. */
+ /*
+ * USBRESET=1 - force a USB port reset before touching the device.
+ *
+ * This is the software equivalent of the "10 s replug" the notes
+ * call for when the CX23416 is left wedged (e.g. after an experiment
+ * that writes its registers in the wrong order -- writing
+ * 0x9054/0x9058 before the firmware image did exactly that on
+ * 2026-10-14). A port reset makes the FX2 re-load its firmware
+ * from EEPROM and resets everything downstream, without needing
+ * physical access to the cable.
+ */
+ if (getenv("USBRESET")) {
+ int rr = libusb_reset_device(p->dev);
+ struct timespec ts = { 3, 0 };
+
+ fprintf(stderr, "USBRESET: libusb_reset_device r=%d (%s)\n",
+ rr, libusb_error_name(rr));
+ nanosleep(&ts, NULL);
+ /*
+ * After a port reset the device re-enumerates and the old
+ * handle can go stale, so drop it and open a fresh one.
+ */
+ libusb_close(p->dev);
+ p->dev = libusb_open_device_with_vid_pid(p->ctx, vid, pid);
+ fprintf(stderr, "USBRESET: reopen -> %s\n",
+ p->dev ? "ok" : "FAILED");
+ if (!p->dev)
+ return -1;
+ }
+
+ {
+ int tries;
+ for (tries = 0; tries < 3; tries++) {
+ int cr = libusb_claim_interface(p->dev, 0);
+ if (cr == 0)
+ break;
+ if (tries == 0)
+ fprintf(stderr, "claim attempt 1 failed: %s\n",
+ libusb_error_name(cr));
+ if (tries == 0) {
+ /* Detach explicitly (in case auto-detach did not). */
+ libusb_detach_kernel_driver(p->dev, 0);
+ } else if (tries == 1 && geteuid() == 0) {
+ fprintf(stderr,
+ "interface 0 busy, resetting device...\n");
+ libusb_reset_device(p->dev);
+ }
+ {
+ struct timespec ts = { 0, 300000000L };
+ nanosleep(&ts, NULL);
+ }
+ }
+ if (tries == 3) {
+ fprintf(stderr,
+ "cannot claim interface 0 (is another process using the device?)\n");
+ libusb_close(p->dev);
+ p->dev = NULL;
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int pvr2_scan(pvr2_dev *p)
+{
+ libusb_device **list = NULL;
+ ssize_t cnt, i;
+
+ cnt = libusb_get_device_list(p->ctx, &list);
+ if (cnt < 0)
+ return -1;
+ fprintf(stderr, "USB devices:\n");
+ for (i = 0; i < cnt; i++) {
+ struct libusb_device_descriptor desc;
+ uint8_t bus, addr;
+ if (libusb_get_device_descriptor(list[i], &desc) < 0)
+ continue;
+ bus = libusb_get_bus_number(list[i]);
+ addr = libusb_get_device_address(list[i]);
+ fprintf(stderr, " %04x:%04x bus %u addr %u\n",
+ desc.idVendor, desc.idProduct, bus, addr);
+ }
+ libusb_free_device_list(list, 1);
+ return 0;
+}
+
+/* ================================================================== */
+/* Bulk helpers */
+/* ================================================================== */
+
+static int bulk_out(pvr2_dev *p, uint8_t ep, const uint8_t *buf, int len)
+{
+ int transferred = 0;
+ int r = libusb_bulk_transfer(p->dev, ep, (unsigned char *)buf, len,
+ &transferred, 5000);
+ if (r != 0 || transferred != len) {
+ if (p->verbose)
+ fprintf(stderr, "bulk_out ep 0x%02x len %d -> r=%d tr=%d (%s)\n",
+ ep, len, r, transferred, libusb_error_name(r));
+ return -1;
+ }
+ return 0;
+}
+
+static int bulk_in(pvr2_dev *p, uint8_t ep, uint8_t *buf, int len)
+{
+ int transferred = 0;
+ int r = libusb_bulk_transfer(p->dev, ep, buf, len, &transferred, 1000);
+ if (r != 0) {
+ if (p->verbose)
+ fprintf(stderr, "bulk_in ep 0x%02x len %d -> r=%d (%s)\n",
+ ep, len, r, libusb_error_name(r));
+ return -1;
+ }
+ return transferred;
+}
+
+/* Public wrappers for the command endpoints (used by the encoder layer) */
+int pvr2_raw_in(pvr2_dev *p, uint8_t ep, uint8_t *buf, int len)
+{
+ return bulk_in(p, ep, buf, len);
+}
+
+int bulk_cmd_out(pvr2_dev *p, const uint8_t *buf, int len)
+{
+ return bulk_out(p, EP_CMD_OUT, buf, len);
+}
+
+int bulk_cmd_in(pvr2_dev *p, uint8_t *buf, int len)
+{
+ return bulk_in(p, EP_CMD_IN, buf, len);
+}
+
+int pvr2_get_usb_speed(pvr2_dev *p)
+{
+ uint8_t cmd = FX2CMD_GET_USB_SPEED;
+ uint8_t buf[64];
+ int n;
+
+ if (bulk_out(p, EP_CMD_OUT, &cmd, 1) < 0)
+ return -1;
+ n = bulk_in(p, EP_CMD_IN, buf, sizeof(buf));
+ if (n < 1)
+ return -1;
+ return buf[0];
+}
+
+/* ================================================================== */
+/* FX2 simple command / register access */
+/* ================================================================== */
+
+int pvr2_simple_cmd(pvr2_dev *p, uint8_t cmd, const uint8_t *arg, int arglen)
+{
+ uint8_t buf[8];
+ buf[0] = cmd;
+ if (arglen > 0 && arg)
+ memcpy(buf + 1, arg, arglen);
+ else
+ arglen = 0;
+ return bulk_out(p, EP_CMD_OUT, buf, 1 + arglen);
+}
+
+int pvr2_write_reg(pvr2_dev *p, uint16_t reg, uint32_t val)
+{
+ uint8_t buf[8];
+ buf[0] = FX2CMD_REG_WRITE;
+ buf[1] = val & 0xff;
+ buf[2] = (val >> 8) & 0xff;
+ buf[3] = (val >> 16) & 0xff;
+ buf[4] = (val >> 24) & 0xff;
+ buf[5] = 0x00;
+ buf[6] = (reg >> 8) & 0xff;
+ buf[7] = reg & 0xff;
+ return bulk_out(p, EP_CMD_OUT, buf, 8);
+}
+
+int pvr2_read_reg(pvr2_dev *p, uint16_t reg, uint32_t *val)
+{
+ uint8_t buf[8];
+ uint8_t in[8];
+ int n;
+
+ memset(buf, 0, sizeof(buf));
+ buf[0] = FX2CMD_REG_READ;
+ buf[6] = (reg >> 8) & 0xff;
+ buf[7] = reg & 0xff;
+ if (bulk_out(p, EP_CMD_OUT, buf, 8) < 0)
+ return -1;
+ n = bulk_in(p, EP_CMD_IN, in, sizeof(in));
+ if (n < 4)
+ return -1;
+ *val = (uint32_t)in[0] | ((uint32_t)in[1] << 8) |
+ ((uint32_t)in[2] << 16) | ((uint32_t)in[3] << 24);
+ return 0;
+}
+
+int pvr2_gpio_dir(pvr2_dev *p, uint32_t mask, uint32_t val)
+{
+ uint32_t reg = val;
+
+ if (~mask) {
+ if (pvr2_read_reg(p, CX_REG_GPIO_DIR, &reg) < 0)
+ return -1;
+ reg = (reg & ~mask) | (val & mask);
+ }
+ return pvr2_write_reg(p, CX_REG_GPIO_DIR, reg);
+}
+
+int pvr2_gpio_out(pvr2_dev *p, uint32_t mask, uint32_t val)
+{
+ uint32_t reg = val;
+
+ if (~mask) {
+ if (pvr2_read_reg(p, CX_REG_GPIO_OUT, &reg) < 0)
+ return -1;
+ reg = (reg & ~mask) | (val & mask);
+ }
+ return pvr2_write_reg(p, CX_REG_GPIO_OUT, reg);
+}
+
+/* ================================================================== */
+/* I2C via FX2 */
+/* ================================================================== */
+
+int pvr2_i2c_write(pvr2_dev *p, uint8_t dev_addr, uint16_t reg,
+ const uint8_t *data, int len)
+{
+ uint8_t buf[256];
+ uint8_t in[16];
+ int n;
+
+ /* [0x08][i2c_addr][len+2][reg_hi][reg_lo][data...] */
+ buf[0] = FX2CMD_I2C_WRITE;
+ buf[1] = dev_addr;
+ buf[2] = len + 2;
+ buf[3] = (reg >> 8) & 0xff;
+ buf[4] = reg & 0xff;
+ memcpy(buf + 5, data, len);
+ if (bulk_out(p, EP_CMD_OUT, buf, len + 5) < 0)
+ return -1;
+ /* status byte (0x08 = OK) */
+ n = bulk_in(p, EP_CMD_IN, in, sizeof(in));
+ if (n < 1 || in[0] != 8) {
+ if (p->verbose)
+ fprintf(stderr, "i2c write status %d (expect 8)\n",
+ n > 0 ? in[0] : -1);
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * pvr2_i2c_write_block - write N registers to one device in a single FX2
+ * command (FX2CMD_I2C_WRITE_BLOCK).
+ *
+ * Recovered from a USB capture of the official Windows driver: every
+ * multi-register block -- the whole audio DSP block 0x0800-0x091b, the
+ * auxiliary PLL words, the 0x08d0-0x08d3 stream-on words -- is written this
+ * way, never as a run of single-register 0x08 writes. Single registers
+ * (e.g. the 0x012b DSP-start trigger) do use 0x08, so both paths exist.
+ *
+ * The captured packets are 64 bytes long on the wire (the used part is
+ * 4 + 3*count and the rest is whatever was in the driver's buffer), but the
+ * FX2 only reads count entries, so only the used part is sent here.
+ */
+int pvr2_i2c_write_block(pvr2_dev *p, uint8_t dev_addr,
+ const uint16_t *regs, const uint8_t *vals, int n)
+{
+ uint8_t buf[256];
+ uint8_t in[16];
+ int i, len = 0, r;
+
+ if (n < 1 || n > 84 || 4 + 3 * n > (int)sizeof(buf))
+ return -1;
+ buf[len++] = FX2CMD_I2C_WRITE_BLOCK;
+ buf[len++] = dev_addr;
+ buf[len++] = (uint8_t)n;
+ buf[len++] = 0x03; /* bytes per entry */
+ for (i = 0; i < n; i++) {
+ buf[len++] = (regs[i] >> 8) & 0xff;
+ buf[len++] = regs[i] & 0xff;
+ buf[len++] = vals[i];
+ }
+ if (bulk_out(p, EP_CMD_OUT, buf, len) < 0)
+ return -1;
+ r = bulk_in(p, EP_CMD_IN, in, sizeof(in));
+ if (r < 1 || in[0] != 8) {
+ if (p->verbose)
+ fprintf(stderr, "i2c block write status %d "
+ "(expect 8)\n", r > 0 ? in[0] : -1);
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * pvr2_audio_block_write - replay the audio register programming exactly as
+ * the official Windows driver sends it (captured over USB, see
+ * docs/NOTES.md and docs/xp-init-sequence.txt).
+ *
+ * The driver writes these registers as THREE FX2 block-I2C commands (0x0c),
+ * not as runs of single-register 0x08 writes, and it does so BEFORE the
+ * CX23416 firmware image is transferred. The values are identical to this
+ * repo's tbl_48000, including 0x0900=0xaa / 0x0901=0x4f, which this project
+ * had previously marked "poison" and never wrote -- the driver writes it
+ * normally, so the value was never the problem.
+ *
+ * The 0x012b DSP-start trigger stays a single-register write, as captured.
+ */
+int pvr2_audio_block_write(pvr2_dev *p)
+{
+ /* group 1: ADC / audio path */
+ static const uint16_t g1r[] = {
+ 0x0122, 0x0127, 0x0914, 0x0915, 0x0916, 0x0917,
+ 0x0918, 0x0919, 0x091a, 0x091b, 0x0808, 0x0809,
+ 0x080a, 0x080b
+ };
+ static const uint8_t g1v[] = {
+ 0x01, 0x50, 0xa0, 0x00, 0x00, 0x00,
+ 0xa0, 0x01, 0x00, 0x00, 0xf6, 0x00,
+ 0x00, 0x14
+ };
+ /* group 2: 0x08d0-0x08d3 (stream on) */
+ static const uint16_t g2r[] = { 0x08d0, 0x08d1, 0x08d2, 0x08d3 };
+ static const uint8_t g2v[] = { 0x70, 0x38, 0x06, 0x01 };
+ /* group 3: 48 kHz aux PLL + the 0x0900 block */
+ static const uint16_t g3r[] = {
+ 0x0108, 0x0109, 0x010a, 0x010b, 0x0110, 0x0111,
+ 0x0112, 0x0113, 0x0127, 0x0900, 0x0901, 0x0902,
+ 0x0903, 0x0128, 0x0129, 0x012a, 0x012c, 0x012d,
+ 0x012e
+ };
+ static const uint8_t g3v[] = {
+ 0x0f, 0x04, 0x0a, 0x10, 0xe5, 0xd6,
+ 0x98, 0x00, 0x50, 0xaa, 0x4f, 0x01,
+ 0x08, 0xf8, 0x93, 0x11, 0xff, 0x5f,
+ 0x20
+ };
+ uint8_t b = 0xa1;
+ int rc = 0;
+
+ rc |= pvr2_i2c_write_block(p, CX2584X_I2C_ADDR, g1r, g1v,
+ (int)(sizeof(g1r) / sizeof(g1r[0])));
+ rc |= pvr2_i2c_write_block(p, CX2584X_I2C_ADDR, g2r, g2v,
+ (int)(sizeof(g2r) / sizeof(g2r[0])));
+ rc |= pvr2_i2c_write_block(p, CX2584X_I2C_ADDR, g3r, g3v,
+ (int)(sizeof(g3r) / sizeof(g3r[0])));
+ pvr2_i2c_write(p, CX2584X_I2C_ADDR, 0x012b, &b, 1);
+ return rc;
+}
+
+int pvr2_i2c_read(pvr2_dev *p, uint8_t dev_addr, uint16_t reg,
+ uint8_t *data, int len)
+{
+ uint8_t buf[8];
+ uint8_t in[256];
+ int n;
+
+ /* [0x09][arglen=2][rlen][i2c_addr][reg_hi][reg_lo] */
+ buf[0] = FX2CMD_I2C_READ;
+ buf[1] = 2;
+ buf[2] = len;
+ buf[3] = dev_addr;
+ buf[4] = (reg >> 8) & 0xff;
+ buf[5] = reg & 0xff;
+ if (bulk_out(p, EP_CMD_OUT, buf, 6) < 0)
+ return -1;
+ /* response: status byte + len data bytes */
+ n = bulk_in(p, EP_CMD_IN, in, sizeof(in));
+ if (n < len + 1)
+ return -1;
+ if (in[0] != 8) {
+ if (p->verbose)
+ fprintf(stderr, "i2c read status %d (expect 8)\n", in[0]);
+ return -1;
+ }
+ memcpy(data, in + 1, len);
+ return 0;
+}
+
+/* ------------------------------------------------------------------ */
+/* WM873x (Wolfson codec / ADC) packed-register I2C access */
+/* ------------------------------------------------------------------ */
+/*
+ * The WM873x (WM8731/8737/8738 family) packs a 7-bit register number and
+ * a 9-bit data value into TWO bytes after the device address:
+ * [dev_addr][(reg<<1)|(data>>8)][data&0xff]
+ * The generic pvr2_i2c_write() treats 'reg' as a 16-bit sub-address and
+ * sends [dev][reg>>8][reg&0xff][data...] — which the WM873x misreads as
+ * "register 0 = low 9 bits", so every write before this helper landed on
+ * the wrong register and read back "rejected". Encode the packed form
+ * into the 16-bit 'reg' field and send zero data bytes to match.
+ */
+int pvr2_wm873x_write(pvr2_dev *p, uint8_t dev_addr, uint8_t reg, uint16_t data)
+{
+ uint16_t packed = (uint16_t)(((uint16_t)reg << 9) | (data & 0x1ff));
+ return pvr2_i2c_write(p, dev_addr, packed, NULL, 0);
+}
+
+int pvr2_wm873x_read(pvr2_dev *p, uint8_t dev_addr, uint8_t reg, uint16_t *out)
+{
+ uint8_t b[2];
+ /* read: send [dev][(reg<<1)|0][0x00], then read 2 bytes
+ * (B15-B9=reg, B8=data MSB, B7-B0=data LSB) */
+ if (pvr2_i2c_read(p, dev_addr, (uint16_t)((uint16_t)reg << 9), b, 2) < 0)
+ return -1;
+ *out = (uint16_t)((b[0] & 0x01) << 8) | b[1];
+ return 0;
+}
+
+/* ================================================================== */
+/* Utility */
+/* ================================================================== */
+
+uint32_t pvr2_swab32(uint32_t v)
+{
+ return ((v & 0x000000ffu) << 24) | ((v & 0x0000ff00u) << 8) |
+ ((v & 0x00ff0000u) >> 8) | ((v & 0xff000000u) >> 24);
+}
+
+void pvr2_swap32_buf(uint8_t *buf, size_t nblocks)
+{
+ size_t i;
+ for (i = 0; i < nblocks; i++) {
+ uint8_t a = buf[i * 4 + 0];
+ uint8_t b = buf[i * 4 + 1];
+ buf[i * 4 + 0] = buf[i * 4 + 3];
+ buf[i * 4 + 1] = buf[i * 4 + 2];
+ buf[i * 4 + 2] = b;
+ buf[i * 4 + 3] = a;
+ }
+}
+
+int pvr2_load_file(const char *path, uint8_t **out, size_t *len)
+{
+ FILE *f = fopen(path, "rb");
+ long sz;
+ uint8_t *buf;
+
+ if (!f) {
+ fprintf(stderr, "cannot open %s\n", path);
+ return -1;
+ }
+ fseek(f, 0, SEEK_END);
+ sz = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if (sz <= 0) {
+ fclose(f);
+ return -1;
+ }
+ buf = malloc((size_t)sz);
+ if (!buf) {
+ fclose(f);
+ return -1;
+ }
+ if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) {
+ free(buf);
+ fclose(f);
+ return -1;
+ }
+ fclose(f);
+ *out = buf;
+ *len = (size_t)sz;
+ return 0;
+}