From b777da118a46c73f5dba3f47d302debd39bf94fa Mon Sep 17 00:00:00 2001
From: Unconfigured <Unconfigured>
Date: Sun, 19 Jul 2009 21:54:56 +0930
Subject: tweaker: add usb tuner support for Dvico DD4 rev2

---
 abs/core-testing/tweaker/PKGBUILD          |  2 +-
 abs/core-testing/tweaker/bin/twk_tuners.pl | 71 +++++++++++++++++++++++++++++-
 2 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/abs/core-testing/tweaker/PKGBUILD b/abs/core-testing/tweaker/PKGBUILD
index 84f4f9e..dbbe914 100644
--- a/abs/core-testing/tweaker/PKGBUILD
+++ b/abs/core-testing/tweaker/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=tweaker
 pkgver=1
-pkgrel=42
+pkgrel=43
 pkgdesc="applies tweaks to the baseline configuration and attempts to optimize settings for the hardware it finds"
 arch=('i686' 'x86_64')
 
diff --git a/abs/core-testing/tweaker/bin/twk_tuners.pl b/abs/core-testing/tweaker/bin/twk_tuners.pl
index f928ea7..ff62b5e 100755
--- a/abs/core-testing/tweaker/bin/twk_tuners.pl
+++ b/abs/core-testing/tweaker/bin/twk_tuners.pl
@@ -109,6 +109,11 @@ my @capture_card_patterns = (
 
 # USB patterns
 
+my @usb_capture_patterns = (
+    [ "Dvico Dual Digital 4 rev2", [".* 0fe9:db98 .*"],
+      ["DVB", ["DVB-T"]]],
+    );
+
 # "Hauppauge Nova-T 500 Dual DVB-T"
 
 #T:  Bus=08 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=480 MxCh= 0
@@ -151,6 +156,9 @@ sub implement_option {
 	# Get a list of all PCI devices for later processing
 	my @lspci = split('\n', execute_shell_command("lspci -mn"));
 
+	# Get a list of all USB devices for later processing
+	my @lsusb = split('\n', execute_shell_command("lsusb"));
+
 	# Iterate through the list of known tuner cards, then the list of known network tuners.
 	# For each known tuner, configure a safe default for it if it's found.  Set up
 	# well-named video sources for any mode supported by any tuner, and pick a default.
@@ -339,7 +347,7 @@ sub implement_option {
 	# Configure each supported tuner/capture device detected on the system.
 
 	# built-in, PCI, or PCI Express
-	$logger->debug("Processing local devices...");
+	$logger->debug("Processing built-in, PCI, or PCI Express devices...");
 	for my $pci_device (@lspci) {
 	    $logger->debug("DEVICE: $pci_device");
 	    my $match=0;
@@ -459,7 +467,66 @@ sub implement_option {
 	
 	# USB devices
 	$logger->debug("Processing USB devices...");
-	$logger->debug("\t(no USB devices supported yet)");
+	for my $usb_device (@lsusb) {
+	    $logger->debug("DEVICE: $usb_device");
+	    my $match=0;
+
+	    for my $usb_device_data (@usb_capture_patterns) {
+		$logger->debug("\tIs there a @$usb_device_data[0] at this location?");
+
+		for my $patterns (@$usb_device_data[1]) {
+		    for my $pattern (@$patterns) {
+			if ($match) {
+			    next;
+			} else {
+			    $logger->debug("\t\tPATTERN: $pattern");
+			    $match += ($usb_device =~ m/$pattern/i);
+			    if ($match) {
+				$global_device_count++; # 1-indexed
+				
+				$logger->debug("\t\tfound one!");
+				# Each device has a device type (e.g. MPEG, DVB, V4L) that it supports, and a
+				# list of sub_types (e.g. PAL, NTSC, DVB-S, DVB-T, DVB-C, ATSC, QAM).
+				# The device type is used to configure the capturecard table and the cardinput table.
+				# The sub_types are used to populate the videosource table.
+				# The device type and "best" sub_type are used to set the 'sourceid' field of the cardinput table.
+
+				for my $typeblock (@$usb_device_data[2]) {
+				    my $usb_device_type = @$typeblock[0];
+				    $logger->debug("\t\tDEVICE TYPE: $usb_device_type"); # e.g. "DVB", "MPEG", etc.
+				    $cardtypes{$usb_device_type}++;
+				    my $sourceid = -1;
+				    for my $sub_types (@$typeblock[1]) {
+					for my $sub_type (@$sub_types) {
+					    $logger->debug("\t\tSUB-TYPE: $sub_type\n"); # e.g. "DVB-S", "QAM", etc.
+					    # ensure that the videosource table has an entry for each sub_type
+					    # that this device supports; use the "best" one for the device by
+					    # default
+
+					    #
+					    # POPULATE videosource table
+					    #
+					    $sourceid = max(verify_or_make_videosource_SQL($sub_type), $sourceid);
+					}
+				    }
+				    $logger->debug("\t\t\"BEST\" SOURCE ID: $sourceid\n");
+
+				    #
+				    # POPULATE capturecard table
+				    #
+				    my $defaultinput = make_capturecard_SQL($global_device_count, $cardtypes{$usb_device_type}-1, $usb_device_type, -1);
+
+				    #
+				    # POPULATE cardinput table
+				    #
+				    make_cardinput_SQL($global_device_count, $sourceid, $defaultinput, 0);
+				}
+			    }
+			}
+		    }
+		}
+	    }
+	}
 
 	# Filesystem Tweaks for tuners
 	# This only works with the Nova-T-500 card, but it doesn't hurt any other cards
-- 
cgit v0.12