| 1 | /* |
|---|
| 2 | * Samo's SA4250HD firewire channel changer by majoridiot |
|---|
| 3 | * with node selection added by Bradley S. Corsello |
|---|
| 4 | * |
|---|
| 5 | * requires: libavc1394-dev libraw1394-dev |
|---|
| 6 | * |
|---|
| 7 | * compile with: gcc -o sa4250_ch sa4250_ch.c -lrom1394 -lavc1394 -lraw1394 |
|---|
| 8 | * |
|---|
| 9 | * based on mythtv source code and |
|---|
| 10 | * |
|---|
| 11 | * sa3250ch - an external channel changer for SA3250HD Tuner |
|---|
| 12 | * Based off 6200ch.c by Stacey D. Son |
|---|
| 13 | * |
|---|
| 14 | * Copyright 2004,2005 by Stacey D. Son <mythdev@son.org> |
|---|
| 15 | * Copyright 2005 Matt Porter <mporter@kernel.crashing.org> |
|---|
| 16 | * Portions Copyright 2006 Chris Ingrassia <chris@spicecoffee.org> (SA4200 and Single-digit command mode) |
|---|
| 17 | * |
|---|
| 18 | * This program is free software; you can redistribute it and/or modify |
|---|
| 19 | * it under the terms of the GNU General Public License as published by |
|---|
| 20 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 21 | * (at your option) any later version. |
|---|
| 22 | * |
|---|
| 23 | * This program is distributed in the hope that it will be useful, |
|---|
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 26 | * GNU General Public License for more details. |
|---|
| 27 | * |
|---|
| 28 | * You should have received a copy of the GNU General Public License |
|---|
| 29 | * along with this program; if not, write to the Free Software Foundation, |
|---|
| 30 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | #include <libavc1394/rom1394.h> |
|---|
| 34 | #include <libavc1394/avc1394.h> |
|---|
| 35 | #include <libraw1394/raw1394.h> |
|---|
| 36 | #include <sys/types.h> |
|---|
| 37 | #include <stdio.h> |
|---|
| 38 | #include <errno.h> |
|---|
| 39 | #include <stdlib.h> |
|---|
| 40 | #include <string.h> |
|---|
| 41 | |
|---|
| 42 | /* SA42xxHD IDs */ |
|---|
| 43 | #define SA4200HD_VENDOR_ID1 0x000014f8 |
|---|
| 44 | #define SA4200HD_MODEL_ID1 0x00001072 |
|---|
| 45 | #define SA4250HD_VENDOR_ID1 0x00001cea /* samo's stb */ |
|---|
| 46 | #define SA4250HD_MODEL_ID1 0x000010cc /* samo's stb */ |
|---|
| 47 | |
|---|
| 48 | /* add additional vendor and model id's here- addition needed to if statement starting @ line 134 below */ |
|---|
| 49 | |
|---|
| 50 | #define AVC1394_SA3250_COMMAND_CHANNEL 0x000007c00 |
|---|
| 51 | #define AVC1394_SA3250_OPERAND_KEY_PRESS 0xe7 |
|---|
| 52 | #define AVC1394_SA3250_OPERAND_KEY_RELEASE 0x67 |
|---|
| 53 | |
|---|
| 54 | #define CTL_CMD0 AVC1394_CTYPE_CONTROL | AVC1394_SUBUNIT_TYPE_PANEL | \ |
|---|
| 55 | AVC1394_SUBUNIT_ID_0 | AVC1394_SA3250_COMMAND_CHANNEL |
|---|
| 56 | #define CTL_CMD1 (0x04 << 24) |
|---|
| 57 | #define CTL_CMD2 0xff000000 |
|---|
| 58 | |
|---|
| 59 | #define STARTING_NODE 0 |
|---|
| 60 | |
|---|
| 61 | void usage() |
|---|
| 62 | { |
|---|
| 63 | fprintf(stderr, "Usage: sa4250_ch_new [-v] [-n <node>] <channel_num>\n"); |
|---|
| 64 | fprintf(stderr, " -v : Verbose Mode\n"); |
|---|
| 65 | fprintf(stderr, " -n : Node to change - default 0\n"); |
|---|
| 66 | exit(1); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | int main (int argc, char *argv[]) |
|---|
| 70 | { |
|---|
| 71 | rom1394_directory dir; |
|---|
| 72 | int device = -1; |
|---|
| 73 | int single = 0; |
|---|
| 74 | int i; |
|---|
| 75 | int verbose = 0; |
|---|
| 76 | int dig[3]; |
|---|
| 77 | int chn = 708; |
|---|
| 78 | int unode = 0; |
|---|
| 79 | |
|---|
| 80 | if (argc < 2) |
|---|
| 81 | usage(); |
|---|
| 82 | |
|---|
| 83 | for(i = 1; i < argc; ++i) { |
|---|
| 84 | if ((argv[i][0] == '-') && (strlen(argv[i]) > 1)) { |
|---|
| 85 | switch(argv[i][1]) { |
|---|
| 86 | case 'v': |
|---|
| 87 | verbose = 1; |
|---|
| 88 | break; |
|---|
| 89 | case 's': |
|---|
| 90 | single = 1; |
|---|
| 91 | break; |
|---|
| 92 | case 'n': |
|---|
| 93 | unode = atoi(argv[++i]); |
|---|
| 94 | break; |
|---|
| 95 | default: |
|---|
| 96 | fprintf(stderr, "WARNING: Unknown option \'%c\', ignoring", argv[i][1]); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | else { |
|---|
| 100 | chn = atoi(argv[i]); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #ifdef RAW1394_V_0_8 |
|---|
| 105 | raw1394handle_t handle = raw1394_get_handle(); |
|---|
| 106 | #else |
|---|
| 107 | raw1394handle_t handle = raw1394_new_handle(); |
|---|
| 108 | #endif |
|---|
| 109 | |
|---|
| 110 | if (!handle) { |
|---|
| 111 | if (!errno) { |
|---|
| 112 | fprintf(stderr, "Not Compatible!\n"); |
|---|
| 113 | } else { |
|---|
| 114 | perror("Couldn't get 1394 handle"); |
|---|
| 115 | fprintf(stderr, "Is ieee1394, driver, and raw1394 loaded? Are /dev/raw1394 permissions set correctly?\n"); |
|---|
| 116 | } |
|---|
| 117 | exit(1); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if (raw1394_set_port(handle, 0) < 0) { |
|---|
| 121 | perror("ERROR-- could not set port"); |
|---|
| 122 | raw1394_destroy_handle(handle); |
|---|
| 123 | exit(1); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int nc = raw1394_get_nodecount(handle); |
|---|
| 127 | if (rom1394_get_directory(handle, unode, &dir) < 0) { |
|---|
| 128 | fprintf(stderr,"ERROR reading config rom directory for node %d\n", unode); |
|---|
| 129 | raw1394_destroy_handle(handle); |
|---|
| 130 | exit(1); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (verbose) |
|---|
| 134 | printf("node %d: vendor_id = 0x%08x model_id = 0x%08x\n", |
|---|
| 135 | unode, dir.vendor_id, dir.model_id); |
|---|
| 136 | |
|---|
| 137 | /* add new vendor and model ids to if stanza below */ |
|---|
| 138 | |
|---|
| 139 | if ( ((dir.vendor_id == SA4250HD_VENDOR_ID1) && |
|---|
| 140 | (dir.model_id == SA4250HD_MODEL_ID1)) || |
|---|
| 141 | ((dir.vendor_id == SA4200HD_VENDOR_ID1) && |
|---|
| 142 | (dir.model_id == SA4200HD_MODEL_ID1))) { |
|---|
| 143 | device = unode; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | if (device == -1) { |
|---|
| 147 | fprintf(stderr, "Could not find SA42XXHD on the 1394 bus!\n"); |
|---|
| 148 | fprintf(stderr, "Try running again with -v and check source code for matching Vendor ID and Model ID-\n"); |
|---|
| 149 | fprintf(stderr, "Add if necessary and recompile.\n"); |
|---|
| 150 | raw1394_destroy_handle(handle); |
|---|
| 151 | exit(1); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (verbose) |
|---|
| 155 | printf("Device acquired on node %d\n", device); |
|---|
| 156 | printf("Changing channel %d\n", chn); |
|---|
| 157 | |
|---|
| 158 | quadlet_t cmd[3] = |
|---|
| 159 | { |
|---|
| 160 | CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS, |
|---|
| 161 | CTL_CMD1 | (chn << 8), |
|---|
| 162 | CTL_CMD2, |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | if (verbose) |
|---|
| 166 | printf("AV/C Command: cmd0=0x%08x cmd1=0x%08x cmd2=0x%08x\n", |
|---|
| 167 | cmd[0], cmd[1], cmd[2]); |
|---|
| 168 | avc1394_transaction_block(handle, device, cmd, 3, 1); |
|---|
| 169 | |
|---|
| 170 | raw1394_destroy_handle(handle); |
|---|
| 171 | |
|---|
| 172 | exit(0); |
|---|
| 173 | } |
|---|