Ticket #2910: sa3250ch.c

File sa3250ch.c, 6.4 KB (added by anonymous, 17 years ago)
Line 
1/*
2 * sa3250ch - an external channel changer for SA3250HD Tuner
3 * Based off 6200ch.c by Stacey D. Son
4 *
5 * Copyright 2004,2005 by Stacey D. Son <mythdev@son.org>
6 * Copyright 2005 Matt Porter <mporter@kernel.crashing.org>
7 * Portions Copyright 2006 Chris Ingrassia <chris@spicecoffee.org> (SA4200 and Single-digit command mode)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <libavc1394/rom1394.h>
25#include <libavc1394/avc1394.h>
26#include <libraw1394/raw1394.h>
27#include <sys/types.h>
28#include <stdio.h>
29#include <errno.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34/* SA3250HD IDs */
35#define SA3250HD_VENDOR_ID1     0x000011e6
36#define SA3250HD_VENDOR_ID2     0x000014f8
37#define SA3250HD_VENDOR_ID3     0x00001692
38#define SA3250HD_VENDOR_ID4     0x00001947
39#define SA3250HD_MODEL_ID1      0x00000be0
40#define SA4200HD_VENDOR_ID1     0x000014f8
41#define SA4200HD_VENDOR_ID2     0x00001692
42#define SA4200HD_MODEL_ID1      0x00001072
43
44#define AVC1394_SA3250_COMMAND_CHANNEL 0x000007c00   /* subunit command */
45#define AVC1394_SA3250_OPERAND_KEY_PRESS 0xe7
46#define AVC1394_SA3250_OPERAND_KEY_RELEASE 0x67
47
48#define CTL_CMD0 AVC1394_CTYPE_CONTROL | AVC1394_SUBUNIT_TYPE_PANEL | \
49        AVC1394_SUBUNIT_ID_0 | AVC1394_SA3250_COMMAND_CHANNEL
50#define CTL_CMD1 (0x04 << 24)
51#define CTL_CMD2 0xff000000
52
53#define STARTING_NODE 1
54
55void usage()
56{
57   fprintf(stderr, "Usage: sa3250ch [-v] [-s] <channel_num>\n");
58   fprintf(stderr, "  -v : Verbose Mode\n");
59   fprintf(stderr, "  -s : Send command as single digit "
60           "(for SA4200 and some SA3250s and SA4200HD's)\n");
61   fprintf(stderr, "-n NODE   node to start device scanning on (default:%i)\n",
62           STARTING_NODE);
63   exit(1);
64}
65
66int main (int argc, char *argv[])
67{
68   rom1394_directory dir;
69   int device = -1;
70   int single = 0;
71   int i;
72   int verbose = 0;
73   quadlet_t cmd[3];
74   int c;
75   int dig[3];
76   int chn = 708;
77
78   /* some people experience crashes when starting on node 1 */
79   int starting_node = STARTING_NODE;
80
81   if (argc < 2)
82      usage();
83
84   while ((c = getopt(argc, argv, "vsn:")) != -1)
85      {
86       switch (c) {
87            case 'v':
88                verbose = 1;
89                break;
90            case 's':
91                single = 1;
92                break;
93            case 'n':
94                starting_node = atoi(optarg);
95                break;
96            default:
97                fprintf(stderr, "WARNING: Unknown option \'%c\', ignoring", argv[i][1]);
98        }
99      }
100
101   /* print out usage message if not enough arguments */
102   if (optind != argc-1) {
103       usage();
104   }
105
106   /* the last argument is the channel number */
107   chn = atoi(argv[optind]);
108
109#ifdef RAW1394_V_0_8
110   raw1394handle_t handle = raw1394_get_handle();
111#else
112   raw1394handle_t handle = raw1394_new_handle();
113#endif
114
115   if (!handle) {
116      if (!errno) {
117         fprintf(stderr, "Not Compatible!\n");
118      } else {
119         perror("Couldn't get 1394 handle");
120         fprintf(stderr, "Is ieee1394, driver, and raw1394 loaded?\n");
121      }
122      exit(1);
123   }
124
125   if (raw1394_set_port(handle, 0) < 0) {
126      perror("couldn't set port");
127      raw1394_destroy_handle(handle);
128      exit(1);
129   }
130
131   int nc = raw1394_get_nodecount(handle);
132   for (i=starting_node; i < nc; ++i) {
133      if (rom1394_get_directory(handle, i, &dir) < 0) {
134         fprintf(stderr,"error reading config rom directory for node %d\n", i);
135         raw1394_destroy_handle(handle);
136         exit(1);
137      }
138
139      if (verbose)
140         printf("node %d: vendor_id = 0x%08x model_id = 0x%08x\n",
141                 i, dir.vendor_id, dir.model_id);
142               
143      if ((((dir.vendor_id == SA4200HD_VENDOR_ID1) ||
144            (dir.vendor_id == SA4200HD_VENDOR_ID2)) &&
145            (dir.model_id == SA4200HD_MODEL_ID1))  ||
146          (((dir.vendor_id == SA3250HD_VENDOR_ID1) ||
147            (dir.vendor_id == SA3250HD_VENDOR_ID2) ||
148            (dir.vendor_id == SA3250HD_VENDOR_ID3) ||
149            (dir.vendor_id == SA3250HD_VENDOR_ID4)) &&
150            (dir.model_id == SA3250HD_MODEL_ID1))) {
151            device = i;
152            break;
153      }
154   }
155   
156   if (device == -1)
157   {
158        fprintf(stderr, "Could not find SA3250HD or SA4200HD "
159                "on the IEEE 1394 bus.\n");
160
161        raw1394_destroy_handle(handle);
162        exit(1);
163   }
164
165   if (single) {
166       /* Send channel as single number for SA4200 and some SA3250s */
167       if (verbose)
168        printf("Using single number channel change command method\n");
169       
170       cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS;
171       cmd[1] = CTL_CMD1 | (chn << 8);
172       cmd[2] = 0x0;
173
174       if (verbose)
175            printf("AV/C Command: cmd0=0x%08x cmd1=0x%08x cmd2=0x%08x\n",
176                   cmd[0], cmd[1], cmd[2]);
177       avc1394_transaction_block(handle, 0, cmd, 3, 1);       
178   } else {
179       /* Default method sending three seperate digits */
180       dig[2] = 0x30 | (chn % 10);
181       dig[1] = 0x30 | ((chn % 100)  / 10);
182       dig[0] = 0x30 | ((chn % 1000) / 100);
183
184       cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS;
185       cmd[1] = CTL_CMD1 | (dig[2] << 16) | (dig[1] << 8) | dig[0];
186       cmd[2] = CTL_CMD2;
187
188       if (verbose)
189          printf("AV/C Command: %d%d%d = cmd0=0x%08x cmd2=0x%08x cmd3=0x%08x\n",
190                dig[0] & 0xf, dig[1] & 0xf, dig[2] & 0xf, cmd[0], cmd[1], cmd[2]);
191
192       avc1394_transaction_block(handle, 0, cmd, 3, 1);
193       cmd[0] = CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_RELEASE;
194       cmd[1] = CTL_CMD1 | (dig[0] << 16) | (dig[1] << 8) | dig[2];
195       cmd[2] = CTL_CMD2;
196
197       if (verbose)
198          printf("AV/C Command: %d%d%d = cmd0=0x%08x cmd2=0x%08x cmd3=0x%08x\n",
199                dig[0] & 0xf, dig[1] & 0xf, dig[2] & 0xf, cmd[0], cmd[1], cmd[2]);
200
201       avc1394_transaction_block(handle, 0, cmd, 3, 1);
202   }
203
204   raw1394_destroy_handle(handle);
205
206   exit(0);
207}