<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

#   Copyright (C) 2010 Mauro Carvalho Chehab
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
# This small script parses register dumps generated by cx231xx driver
# with debug options enabled, generating a source code with the results
# of the dump.
#
# To use it, you may modprobe cx231xx with reg_debug=1, and do:
# dmesg | ./parse_em28xx.pl
#
# Also, there are other utilities that produce similar outputs, and it
# is not hard to parse some USB analyzers log into the expected format.
#

use strict;

my %cfg_reg_map = (
	0x0 =&gt; "BOARD_CFG_STAT",
	0x4 =&gt; "TS_MODE_REG",
	0x8 =&gt; "TS1_CFG_REG",
	0xc =&gt; "TS1_LENGTH_REG",
	0x10 =&gt; "TS2_CFG_REG",
	0x14 =&gt; "TS2_LENGTH_REG",
	0x18 =&gt; "EP_MODE_SET",
	0x1c =&gt; "CIR_PWR_PTN1",
	0x20 =&gt; "CIR_PWR_PTN2",
	0x24 =&gt; "CIR_PWR_PTN3",
	0x28 =&gt; "CIR_PWR_MASK0",
	0x2c =&gt; "CIR_PWR_MASK1",
	0x30 =&gt; "CIR_PWR_MASK2",
	0x34 =&gt; "CIR_GAIN",
	0x38 =&gt; "CIR_CAR_REG",
	0x40 =&gt; "CIR_OT_CFG1",
	0x44 =&gt; "CIR_OT_CFG2",
	0x68 =&gt; "GBULK_BIT_EN",
	0x74 =&gt; "PWR_CTL_EN",
);

sub parse_i2c($$$$$$)
{
	my $reqtype = shift;
	my $req = shift;
	my $wvalue = shift;
	my $windex = shift;
	my $wlen = shift;
	my $payload = shift;

	my $daddr = $wvalue &gt;&gt; 9;
	my $reserved = ($wvalue &gt;&gt;6 ) &amp; 0x07;
	my $period = ($wvalue &gt;&gt; 4) &amp; 0x03;
	my $addr_len = ($wvalue &gt;&gt; 2) &amp; 0x03;
	my $nostop = ($wvalue &gt;&gt;1) &amp; 0x01;
	my $sync = $wvalue &amp; 0x01;

	if ($nostop) {
		$nostop="nostop ";
	} else {
		$nostop="";
	}
	if ($sync) {
		$sync="sync ";
	} else {
		$sync="";
	}
	my $type;
	my $i2c_channel;
	if ($reqtype &gt; 128) {
		$type = "IN ";
		$i2c_channel = $req - 4;
	} else {
		$type = "OUT";
		$i2c_channel = $req;
	}
	if ($period == 0) {
		$period = "1Mbps";
	} elsif ($period == 1) {
		$period = "400kbps";
	} elsif ($period == 2) {
		$period = "100kbps";
	} else {
		$period = "???kbps";
	}
	printf("$type i2c channel#%d daddr 0x%02x %s addr_len %d %s%slen %d = ",
		$i2c_channel, $daddr, $period, $addr_len, $nostop, $sync,
		$wlen);
	if ($addr_len == 1) {
		printf("(saddr)%02x ", $windex &amp; 0xff);
	} elsif ($addr_len == 2) {
		printf("(saddr)%04x ", $windex);
	}
	printf("$payload\n");
}

sub parse_gpio($$$$$$)
{
	my $reqtype = shift;
	my $req = shift;
	my $wvalue = shift;
	my $windex = shift;
	my $wlen = shift;
	my $payload = shift;

	my $type;
	if ($req == 8) {
		$type .= "GET gpio";
	} elsif ($req == 9) {
		$type .= "SET gpio";
	} elsif ($req == 0xa) {
		$type .= "SET gpie";
	} elsif ($req == 0xb) {
		$type .= "SET gpie";
	}

	my $gpio_bit = $wvalue &lt;&lt; 16 &amp; $windex;

	printf("$type: Reqtype %3d Req %3d 0x%04x len %d val = %s\n",
		$reqtype, $req, $gpio_bit, $wlen, $payload);
}

while (&lt;&gt;) {
	tr/A-F/a-f/;
	if (m/([4c]0) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].)[\&lt;\&gt;\s]+(.*)/) {
		my $reqtype = hex($1);
		my $req = hex($2);
		my $wvalue = hex("$4$3");
		my $windex = hex("$6$5");
		my $wlen = hex("$8$7");
		my $payload = $9;

		if ($reqtype &gt; 128 &amp;&amp; (($req &gt;= 4) &amp;&amp; ($req &lt;= 6))) {
			parse_i2c($reqtype, $req, $wvalue, $windex, $wlen, $payload);
		} elsif ($req &lt; 3) {
			parse_i2c($reqtype, $req, $wvalue, $windex, $wlen, $payload);
		} elsif ($req &gt;= 8 &amp;&amp; $req &lt;= 0xb) {
			parse_gpio($reqtype, $req, $wvalue, $windex, $wlen, $payload);
		} elsif ($req == 0xc) {
			my $cfg_len;
			if ($wvalue == 1) {
				$cfg_len = 1;
			} elsif($wvalue == 3) {
				$cfg_len = 2;
			} elsif($wvalue == 7) {
				$cfg_len = 3;
			} elsif($wvalue == 0xf) {
				$cfg_len = 4;
			} else {
				printf("Invalid get len for ");
				printf("Reqtype: %3d, Req %3d, wValue: 0x%04x, wIndex 0x%04x, wlen %d: %s\n",
					$reqtype, $req, $wvalue, $windex, $wlen, $payload);
			}

			if ($cfg_len) {
				my $reg = $windex;
				$reg = $cfg_reg_map{$windex} if defined($cfg_reg_map{$windex});

				printf "cx231xx_write_ctrl_reg(dev, $reg, $payload, $cfg_len);\n";
			}
		} elsif ($req == 0xd) {
			my $cfg_len;
			if ($wvalue == 1) {
				$cfg_len = 1;
			} elsif($wvalue == 3) {
				$cfg_len = 2;
			} elsif($wvalue == 7) {
				$cfg_len = 3;
			} elsif($wvalue == 0xf) {
				$cfg_len = 4;
			} else {
				printf("Invalid get len for ");
				printf("Reqtype: %3d, Req %3d, wValue: 0x%04x, wIndex 0x%04x, wlen %d: %s\n",
					$reqtype, $req, $wvalue, $windex, $wlen, $payload);
			}
			if ($cfg_len) {
				my $reg = $windex;
				$reg = $cfg_reg_map{$windex} if defined($cfg_reg_map{$windex});

				printf "cx231xx_read_ctrl_reg(dev, $reg, $cfg_len);\t\t/* read %s */\n",
					$payload;
			}
		} else {
			printf("Reqtype: %3d, Req %3d, wValue: 0x%04x, wIndex 0x%04x, wlen %d: %s\n",
				$reqtype, $req, $wvalue, $windex, $wlen, $payload);
		}
	}
}
</pre></body></html>