Index: sa3250ch.c
===================================================================
--- sa3250ch.c	(revision 10227)
+++ sa3250ch.c	(working copy)
@@ -2,6 +2,9 @@
  * sa3250ch - an external channel changer for SA3250HD Tuner 
  * Based off 6200ch.c by Stacey D. Son
  * 
+ * Additional enhancements and alternate SA3250 and SA4200 channel change
+ * method added for the MythTV project by Chris Ingrassia <chris@spicecoffee.org>
+ *
  * Copyright 2004,2005 by Stacey D. Son <mythdev@son.org> 
  * Copyright 2005 Matt Porter <mporter@kernel.crashing.org>
  * 
@@ -20,17 +23,30 @@
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-#include <libavc1394/rom1394.h>
-#include <libavc1394/avc1394.h>
-#include <libraw1394/raw1394.h>
+#include <unistd.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
 
+#include <libavc1394/rom1394.h>
+#include <libavc1394/avc1394.h>
+#include <libraw1394/raw1394.h>
+
+
+int verbose = 0;
+
+#ifdef RAW1394_V_0_8
+#   define  GET_1394HANDLE(h)   (h = raw1394_get_handle())
+#else
+#   define  GET_1394HANDLE(h)   (h = raw1394_new_handle())
+#endif
+
+#define MAX_PORTS   4   /* Maximum number of 1394 ports to check for devices */
+
 /* SA3250HD IDs */
 #define SA3250HD_VENDOR_ID	0x000011e6
-#define SA3250HD_VENDOR_ID2     0x000014f8
+#define SA3250HD_VENDOR_ID2     0x00001692
 #define SA3250HD_MODEL_ID	0x00000be0
 
 #define AVC1394_SA3250_COMMAND_CHANNEL 0x000007c00   /* subunit command */
@@ -40,41 +56,77 @@
 #define CTL_CMD0 AVC1394_CTYPE_CONTROL | AVC1394_SUBUNIT_TYPE_PANEL | \
         AVC1394_SUBUNIT_ID_0 | AVC1394_SA3250_COMMAND_CHANNEL
 #define CTL_CMD1 (0x04 << 24)
-#define CTL_CMD2 0xff000000
+#define CTL_CMD2 0x00000000
 
 #define STARTING_NODE 0
 
 void usage()
 {
-   fprintf(stderr, "Usage: sa3250ch [-v] <channel_num>\n");
+   fprintf(stderr, "Usage: sa3250ch [-vsh] [-p <port>] [-n <node>] <channel>\n");
+   fprintf(stderr, "Available Options:\n");
+   fprintf(stderr, "\t-v:  Be verbose (useful for debugging)\n");
+   fprintf(stderr, "\t-f:  Force sending command even if compatible device is not detected\n");
+   fprintf(stderr, "\t-a:  Just attempt autodetection of device and exit\n");
+   fprintf(stderr, "\t-s:  Send channel as single command (for SA4200, and some SA3250 models)\n");
+   fprintf(stderr, "\t-p:  Use port <port> (Default: autodetect)\n");
+   fprintf(stderr, "\t-n:  Use node <node> (Default: autodetect)\n");
+   fprintf(stderr, "\t-h:  Print this usage message\n");
    exit(1);
 }
 
 int main (int argc, char *argv[])
 {
-   rom1394_directory dir;
-   int device = -1;
-   int i;
-   int verbose = 0;
-   quadlet_t cmd[3];
+   int device = -1, i = 0, chn = 708, st = EXIT_SUCCESS, arg = 0;
+   int single = 0, port = -1, node = -1, report = 0, force = 0;
    int dig[3];
-   int chn = 708;
-
+   quadlet_t cmd[3], *response = NULL;
+   raw1394handle_t handle;
+  
    if (argc < 2) 
       usage();
 
-   if (argc == 3 && argv[1][0] == '-' && argv[1][1] == 'v') {
-      verbose = 1;
-      chn = atoi(argv[2]);
-   } else {
-      chn = atoi(argv[1]);
+   while((arg = getopt(argc, argv, "vsafhp:n:")) != -1) {
+      switch(arg) {
+          case 'v':
+            verbose = 1;
+            printf("Verbose mode on\n");
+          break;
+          case 's':
+            single = 1;
+          break;
+          case 'p':
+            port = atoi(optarg);
+          break;
+          case 'n':
+            node = atoi(optarg);
+          break;
+          case 'f':
+            if(verbose) printf("Force mode on\n");
+            force = 1;
+          break;
+          case 'a':
+            report = 1;
+          break;
+          case 'h':
+            usage();
+            goto cleanup;
+          break;
+      }
    }
 
-#ifdef RAW1394_V_0_8
-   raw1394handle_t handle = raw1394_get_handle();
-#else
-   raw1394handle_t handle = raw1394_new_handle();
-#endif
+   argc -= optind;
+   argv += optind;
+  
+   if((argc != 1) && !report) {
+      if(verbose) printf("No channel number specified?\n");
+      usage();
+      st = EXIT_FAILURE;
+      goto cleanup;
+   }
+  
+   if(!report) chn = atoi(*argv);
+  
+   GET_1394HANDLE(handle);
 
    if (!handle) {
       if (!errno) {
@@ -83,65 +135,179 @@
          perror("Couldn't get 1394 handle");
          fprintf(stderr, "Is ieee1394, driver, and raw1394 loaded?\n");
       }
-      exit(1);
+      st = EXIT_FAILURE;
+      goto cleanup;
    } 
 
-   if (raw1394_set_port(handle, 0) < 0) {
-      perror("couldn't set port");
-      raw1394_destroy_handle(handle);
-      exit(1);
+   if(!locate_device(handle, &port, &node)) {
+       if(force) {
+           if(verbose) printf("Compatible AV/C device not found, continuing anyway\n");
+       } else {
+           fprintf(stderr, "Unable to locate AV/C 1394 Panel Device\n");
+           st = EXIT_FAILURE;
+           goto cleanup;
+       }
    }
 
-   int nc = raw1394_get_nodecount(handle);
-   for (i=STARTING_NODE; i < nc; ++i) {
-      if (rom1394_get_directory(handle, i, &dir) < 0) {
-         fprintf(stderr,"error reading config rom directory for node %d\n", i);
-         raw1394_destroy_handle(handle);
-    	 exit(1);
-      }
-
-      if (verbose) 
-         printf("node %d: vendor_id = 0x%08x model_id = 0x%08x\n", 
-                 i, dir.vendor_id, dir.model_id); 
-		
-      if ((dir.vendor_id == SA3250HD_VENDOR_ID ||
-           dir.vendor_id == SA3250HD_VENDOR_ID2)  &&
-          (dir.model_id == SA3250HD_MODEL_ID)) {
-            device = i;
-            break;
-      }
+   if(report) {
+       printf("Detected compatible device at port %d and node %d\n",
+              port, node);
+       st = EXIT_SUCCESS;
+       goto cleanup;
    }
-    
-   if (device == -1) {
-        fprintf(stderr, "Could not find SA3250HD on the 1394 bus.\n");
-        raw1394_destroy_handle(handle);
-        exit(1);
-   }
 
-   dig[2] = 0x30 | (chn % 10);
-   dig[1] = 0x30 | ((chn % 100)  / 10);
-   dig[0] = 0x30 | ((chn % 1000) / 100);
+   raw1394_set_port(handle, port);
+   
+   if(single) {
+       if(verbose)
+        printf("Tuning to channel %d using single command\n", chn);
 
-   cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS;
-   cmd[1] = CTL_CMD1 | (dig[2] << 16) | (dig[1] << 8) | dig[0];
-   cmd[2] = CTL_CMD2;
+       cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS;
+       cmd[1] = CTL_CMD1 | (chn << 8);
+       cmd[2] = 0x0;
 
-   if (verbose)
-      printf("AV/C Command: %d%d%d = cmd0=0x%08x cmd2=0x%08x cmd3=0x%08x\n", 
-            dig[0] & 0xf, dig[1] & 0xf, dig[2] & 0xf, cmd[0], cmd[1], cmd[2]);
+       if(verbose)
+            printf("Sending command channel change: 0x%08X 0x%08X\n",
+                   cmd[0], cmd[1]);
 
-   avc1394_transaction_block(handle, 0, cmd, 3, 1);
-   cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_RELEASE;
-   cmd[1] = CTL_CMD1 | (dig[0] << 16) | (dig[1] << 8) | dig[2];
-   cmd[2] = CTL_CMD2;
+       response = avc1394_transaction_block(handle, 0, cmd, 3, 1);
+       if(AVC1394_MASK_RESPONSE(response[0]) == AVC1394_RESPONSE_ACCEPTED) {
+           if(verbose)
+            printf("Channel change command success: Response: 0x%08X 0x%08X\n",
+                   response[0], response[1]);
+       }
+       else {
+           fprintf(stderr, "Channel change command failed: Response: 0x%08X 0x%08X\n",
+                   response[0], response[1]);
+       }
+   }
+   else {
+       /* Original sa3250ch channel change method */
+       dig[2] = 0x30 | (chn % 10);
+       dig[1] = 0x30 | ((chn % 100)  / 10);
+       dig[0] = 0x30 | ((chn % 1000) / 100);
 
-   if (verbose)
-      printf("AV/C Command: %d%d%d = cmd0=0x%08x cmd2=0x%08x cmd3=0x%08x\n", 
-            dig[0] & 0xf, dig[1] & 0xf, dig[2] & 0xf, cmd[0], cmd[1], cmd[2]);
+       if (verbose) {
+          printf("Digits : [0] : %d [1]: %d [2]: %d\n",
+                 dig[0], dig[1], dig[2]);
+       }
+       cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS;
+       cmd[1] = CTL_CMD1 | (dig[0] << 16) | (dig[1] << 8) | dig[2];
+       cmd[2] = CTL_CMD2;
+       if(verbose)
+           printf("AV/C Command: %d%d%d = cmd0=0x%08x cmd2=0x%08x cmd3=0x%08x\n", 
+                    dig[0] & 0xf, dig[1] & 0xf, dig[2] & 0xf, cmd[0], cmd[1], cmd[2]);
+       avc1394_transaction_block(handle, 0, cmd, 3, 1);
+       cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_RELEASE;
+       if(verbose)
+           printf("AV/C Command: %d%d%d = cmd0=0x%08x cmd2=0x%08x cmd3=0x%08x\n", 
+                    dig[0] & 0xf, dig[1] & 0xf, dig[2] & 0xf, cmd[0], cmd[1], cmd[2]);
+       avc1394_transaction_block(handle, 0, cmd, 3, 1);
+   }
 
-   avc1394_transaction_block(handle, 0, cmd, 3, 1);
+   cleanup:
+   if(handle) raw1394_destroy_handle(handle);
+   
+   if(verbose) {
+       printf("%s attempt %s\n",
+           report ? "Autodetection" : "Channel change",
+           st ? "failed" : "suceeded");
+   }
+   
+   exit(st);
+}
 
-   raw1394_destroy_handle(handle);
+/**
+ * locate_device - Attempt to locate a compatible AV/C panel device on the 1394
+ * bus
+ * @handle: libraw1394 handle
+ * @port: pointer to detected port number
+ * @node: pointer to detected node number
+ *
+ * Setting the value pointed to by @port or @node to a value >= 0 will limit detection
+ * to those ports, and this function will simply verify that it thinks a compatible
+ * device is at that location
+ *
+ * Returns: 1 on succesful detection, and sets the value at the locations
+ * pointed to be @port and @node to the detected ieee1394 port and node, respectively.
+ * Returns 0 on failure
+ */
+int locate_device(raw1394handle_t handle, int *port, int *node) {
+    int st = 0, nports = 0, i = 0, j = 0;
+    int found_port = -1, found_node = -1;
+    struct raw1394_portinfo pinfo[MAX_PORTS];
+    
+    if((port && (*port >= 0))) {
+        found_port = *port;
+        if(verbose) printf("Using user specified port %d\n", found_port);
+    }
+    
+    if((node && (*node >= 0))) {
+        found_node = *node;
+        if(verbose) printf("Using user specified node %d\n", found_node);
+    }
+    
+    if((found_port >= 0) && (found_node >= 0)) {
+        /* Just verify device type at this port/node combination */
+        st = verify_device(found_port, found_node);
+        goto cleanup;
+    }
+    /* XXX: I think you have to give raw1394_get_port_info() a nonzero
+     * maxports argument.  I figure the default in MAX_PORTS should be more
+     * than sufficient, and you can always specify the port manually, but
+     * I could be wrong
+     */
+    if(found_port < 0) {
+        if(!(nports = raw1394_get_port_info(handle, pinfo, MAX_PORTS))) {
+            fprintf(stderr, "Could not find any IEEE 1394 ports!\n");
+            st = 0;
+            goto cleanup;
+        }
+    } else {
+        nports = 1;
+        raw1394_set_port(handle, found_port);
+        pinfo[0].nodes = raw1394_get_nodecount(handle);
+        strncpy(pinfo[0].name, "Pre-set port", sizeof(pinfo[0].name));
+    }
+    
+    if(verbose) {
+        printf("Found %d ports on 1394 Bus\n", nports);
+    }
+    
+    for(i = 0; i < nports; ++i) {
+        if(verbose) {
+            printf("Checking port #%d - \'%s\'\n", i, pinfo[i].name);
+        }
+        for(j = 0; (j < pinfo[i].nodes) && (found_node < 0); ++j) {
+            if(verbose) printf("\t Node %d ... ", j);
+            if(verify_device(i, j)) {
+                found_node = j;
+                if(verbose) printf("AV/C Panel/Tuner Device Found\n");
+                break;                
+            }
+            if(verbose) printf("\n");
+        }
+        if(found_node >= 0) {
+            found_port = i;
+            st = 1;
+            break;
+        }
+    }
+    
+    cleanup:
+    if(found_port >= 0) *port = found_port;
+    if(found_node >= 0) *node = found_node;
+    return st;
+}
 
-   exit(0);
+int verify_device(int port, int node) {
+    raw1394handle_t handle;
+    int panel = 0, tuner = 0;
+        
+    GET_1394HANDLE(handle);
+    raw1394_set_port(handle, port);
+    panel = avc1394_check_subunit_type(handle, node, AVC1394_SUBUNIT_TYPE_PANEL);
+    tuner = avc1394_check_subunit_type(handle, node, AVC1394_SUBUNIT_TYPE_TUNER);
+    raw1394_destroy_handle(handle);
+    return (panel && tuner);
 }
Index: sa3250ch-README
===================================================================
--- sa3250ch-README	(revision 10227)
+++ sa3250ch-README	(working copy)
@@ -1,5 +1,5 @@
 sa3250ch is a small program that changes channels on a Scientific Atlanta
-SA3250HD cable box via a 1394 (aka. Firewire) connection. It is based off
+SA3250HD, and SA4200 cable boxes via a 1394 (aka. Firewire) connection. It is based off
 of 6200ch by Stacey Son (mythdev@son.org).
 
 To use this with mythtv do the following:
@@ -21,7 +21,19 @@
 program and adding to "/usr/local/bin/sa3250ch" to the "External channel
 change command" field under "Connect source to Input".
 
-I'm curious if this works for anybody on other SciAtl boxes when the proper
-model IDs are added.
+Troubleshooting:
+----------------
 
+(1) Try running the program with the "-v" (verbose) argument first to see if anything
+    obvious is wrong
+(2) If for some reason auto-detection of the device fails or you want to circumvent it,
+    manually specify the port and node with the "-p" and "-n" arguments, respectively.
+(3) If the channel changes, but is incorrect or you have problems recording or watching LiveTV
+    after a channel change, try using the alternate method of channel changing by sending
+    a single command to the box with the "-s" option.
+
+If you're still having problems, please include the output of the failed command with the "-v"
+argument specified when reporting problems.
+
 Matt Porter <mporter@kernel.crashing.org>
+Chris Ingrassia <chris@spicecoffee.org> (sa3250ch enhancements, "-s" support)
\ No newline at end of file
