Debian Codegraph @

examples/plugreport.c

raw
1 /* plugreport.c v0.2
2 * A program to read all MPR/PCR registers from all devices and report them.
3 * Copyright 2002-2004 Dan Dennedy
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /* standard system includes */
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <netinet/in.h>
26
27 /* linux1394 includes */
28 #include <libraw1394/raw1394.h>
29 #include <libraw1394/csr.h>
30
31 #include "../src/iec61883.h"
32
33 /************ DECLARE PRIVATE INTERFACE BITS OF LIBIEC61883!! **************/
34
35 /* standard CSR offsets for plugs */
36 #define CSR_O_MPR 0x900
37 #define CSR_O_PCR_0 0x904
38 #define CSR_O_PCR_1 0x908
39 #define CSR_O_PCR_2 0x90C
40 #define CSR_O_PCR_3 0x910
41
42 #define CSR_I_MPR 0x980
43 #define CSR_I_PCR_0 0x984
44 #define CSR_I_PCR_1 0x988
45 #define CSR_I_PCR_2 0x98C
46 #define CSR_I_PCR_3 0x990
47
48 #if ( __BYTE_ORDER == __BIG_ENDIAN )
49
50 struct iec61883_oMPR {
51 unsigned int data_rate:2;
52 unsigned int bcast_channel:6;
53 unsigned int non_persist_ext:8;
54 unsigned int persist_ext:8;
55 unsigned int reserved:3;
56 unsigned int n_plugs:5;
57 };
58
59 struct iec61883_iMPR {
60 unsigned int data_rate:2;
61 unsigned int reserved:6;
62 unsigned int non_persist_ext:8;
63 unsigned int persist_ext:8;
64 unsigned int reserved2:3;
65 unsigned int n_plugs:5;
66 };
67
68 struct iec61883_oPCR {
69 unsigned int online:1;
70 unsigned int bcast_connection:1;
71 unsigned int n_p2p_connections:6;
72 unsigned int reserved:2;
73 unsigned int channel:6;
74 unsigned int data_rate:2;
75 unsigned int overhead_id:4;
76 unsigned int payload:10;
77 };
78
79 struct iec61883_iPCR {
80 unsigned int online:1;
81 unsigned int bcast_connection:1;
82 unsigned int n_p2p_connections:6;
83 unsigned int reserved:2;
84 unsigned int channel:6;
85 unsigned int reserved2:16;
86 };
87
88 #else
89
90 struct iec61883_oMPR {
91 unsigned int n_plugs:5;
92 unsigned int reserved:3;
93 unsigned int persist_ext:8;
94 unsigned int non_persist_ext:8;
95 unsigned int bcast_channel:6;
96 unsigned int data_rate:2;
97 };
98
99 struct iec61883_iMPR {
100 unsigned int n_plugs:5;
101 unsigned int reserved2:3;
102 unsigned int persist_ext:8;
103 unsigned int non_persist_ext:8;
104 unsigned int reserved:6;
105 unsigned int data_rate:2;
106 };
107
108 struct iec61883_oPCR {
109 unsigned int payload:10;
110 unsigned int overhead_id:4;
111 unsigned int data_rate:2;
112 unsigned int channel:6;
113 unsigned int reserved:2;
114 unsigned int n_p2p_connections:6;
115 unsigned int bcast_connection:1;
116 unsigned int online:1;
117 };
118
119 struct iec61883_iPCR {
120 unsigned int reserved2:16;
121 unsigned int channel:6;
122 unsigned int reserved:2;
123 unsigned int n_p2p_connections:6;
124 unsigned int bcast_connection:1;
125 unsigned int online:1;
126 };
127
128 #endif
129
130 /**
131 * iec61883_plug_get - Read a node's plug register.
132 * @h: A raw1394 handle.
133 * @n: The node id of the node to read
134 * @a: The CSR offset address (relative to base) of the register to read.
135 * @value: A pointer to a quadlet where the plug register's value will be stored.
136 *
137 * This function handles bus to host endian conversion. It returns 0 for
138 * suceess or -1 for error (errno available).
139 **/
140 extern int
141 iec61883_plug_get(raw1394handle_t h, nodeid_t n, nodeaddr_t a, quadlet_t *value);
142
143
144 /**
145 * iec61883_plug_set - Write a node's plug register.
146 * @h: A raw1394 handle.
147 * @n: The node id of the node to read
148 * @a: The CSR offset address (relative to CSR base) of the register to write.
149 * @value: A quadlet containing the new register value.
150 *
151 * This uses a compare/swap lock operation to safely write the
152 * new register value, as required by IEC 61883-1.
153 * This function handles host to bus endian conversion. It returns 0 for success
154 * or -1 for error (errno available).
155 **/
156 extern int
157 iec61883_plug_set(raw1394handle_t h, nodeid_t n, nodeaddr_t a, quadlet_t value);
158
159 /**
160 * High level plug access macros
161 */
162
163 #define iec61883_get_oMPR(h,n,v) iec61883_plug_get((h), (n), CSR_O_MPR, (quadlet_t *)(v))
164 #define iec61883_set_oMPR(h,n,v) iec61883_plug_set((h), (n), CSR_O_MPR, *((quadlet_t *)&(v)))
165 #define iec61883_get_oPCR0(h,n,v) iec61883_plug_get((h), (n), CSR_O_PCR_0, (quadlet_t *)(v))
166 #define iec61883_set_oPCR0(h,n,v) iec61883_plug_set((h), (n), CSR_O_PCR_0, *((quadlet_t *)&(v)))
167 #define iec61883_get_oPCRX(h,n,v,x) iec61883_plug_get((h), (n), CSR_O_PCR_0+(4*(x)), (quadlet_t *)(v))
168 #define iec61883_set_oPCRX(h,n,v,x) iec61883_plug_set((h), (n), CSR_O_PCR_0+(4*(x)), *((quadlet_t *)&(v)))
169 #define iec61883_get_iMPR(h,n,v) iec61883_plug_get((h), (n), CSR_I_MPR, (quadlet_t *)(v))
170 #define iec61883_set_iMPR(h,n,v) iec61883_plug_set((h), (n), CSR_I_MPR, *((quadlet_t *)&(v)))
171 #define iec61883_get_iPCR0(h,n,v) iec61883_plug_get((h), (n), CSR_I_PCR_0, (quadlet_t *)(v))
172 #define iec61883_set_iPCR0(h,n,v) iec61883_plug_set((h), (n), CSR_I_PCR_0, *((quadlet_t *)&(v)))
173 #define iec61883_get_iPCRX(h,n,v,x) iec61883_plug_get((h), (n), CSR_I_PCR_0+(4*(x)), (quadlet_t *)(v))
174 #define iec61883_set_iPCRX(h,n,v,x) iec61883_plug_set((h), (n), CSR_I_PCR_0+(4*(x)), *((quadlet_t *)&(v)))
175
176 /************ END PRIVATE INTERFACE **************/
177
178 #define FAIL(s) {fprintf(stderr, "libiec61883 error: %s\n", s);}
179
180 #define PLUGREPORT_GUID_HI 0x0C
181 #define PLUGREPORT_GUID_LO 0x10
182
183 static octlet_t get_guid(raw1394handle_t handle, nodeid_t node)
184 {
185 quadlet_t quadlet;
186 octlet_t offset;
187 octlet_t guid = 0;
188
189 offset = CSR_REGISTER_BASE + CSR_CONFIG_ROM + PLUGREPORT_GUID_HI;
190 raw1394_read(handle, node, offset, sizeof(quadlet_t), &quadlet);
191 quadlet = htonl(quadlet);
192 guid = quadlet;
193 guid <<= 32;
194 offset = CSR_REGISTER_BASE + CSR_CONFIG_ROM + PLUGREPORT_GUID_LO;
195 raw1394_read(handle, node, offset, sizeof(quadlet_t), &quadlet);
196 quadlet = htonl(quadlet);
197 guid += quadlet;
198
199 return guid;
200 }
201
202
203 int main(int argc, const char** argv)
204 {
205 raw1394handle_t handle;
206 int device;
207 struct iec61883_oMPR o_mpr;
208 struct iec61883_oPCR o_pcr;
209 struct iec61883_iMPR i_mpr;
210 struct iec61883_iPCR i_pcr;
211 int numcards, port, i;
212 struct raw1394_portinfo pinf[16];
213
214 if (!(handle = raw1394_new_handle())) {
215 perror("raw1394 - couldn't get handle");
216 printf("This error usually means that the raw1394 driver is not loaded or that /dev/raw1394 does not exist.\n");
217 exit( -1);
218 }
219
220 if ((numcards = raw1394_get_port_info(handle, pinf, 16)) < 0) {
221 perror("raw1394 - couldn't get card info");
222 exit( -1);
223 }
224
225 for (port = 0; port < numcards; port++)
226 {
227 if (raw1394_set_port(handle, port) < 0) {
228 perror("raw1394 - couldn't set port");
229 exit( -1);
230 }
231
232 printf( "Host Adapter %d\n==============\n", port);
233
234 for (device = 0; device < raw1394_get_nodecount(handle); device++ )
235 {
236 octlet_t guid = get_guid(handle, 0xffc0 | device);
237
238 printf( "\nNode %d GUID 0x%08x%08x\n------------------------------\n",
239 device, (quadlet_t) (guid>>32), (quadlet_t) (guid & 0xffffffff));
240
241 if (iec61883_get_oMPR(handle, 0xffc0 | device, &o_mpr) < 0)
242 FAIL("error reading oMPR")
243 else
244 {
245 printf( "oMPR n_plugs=%d, data_rate=%d, bcast_channel=%d\n", o_mpr.n_plugs, o_mpr.data_rate, o_mpr.bcast_channel);
246 for (i = 0; i < o_mpr.n_plugs; i++)
247 {
248 if (iec61883_get_oPCRX( handle, 0xffc0 | device, &o_pcr, i) < 0)
249 FAIL("error reading oPCR")
250 else
251 {
252 printf( "oPCR[%d] online=%d, bcast_connection=%d, n_p2p_connections=%d\n", i,
253 o_pcr.online, o_pcr.bcast_connection, o_pcr.n_p2p_connections);
254 printf( "\tchannel=%d, data_rate=%d, overhead_id=%d, payload=%d\n",
255 o_pcr.channel, o_pcr.data_rate, o_pcr.overhead_id, o_pcr.payload);
256 }
257 }
258 }
259
260 if (iec61883_get_iMPR(handle, 0xffc0 | device, &i_mpr) < 0)
261 FAIL("error reading iMPR")
262 else
263 {
264 printf( "iMPR n_plugs=%d, data_rate=%d\n", i_mpr.n_plugs, i_mpr.data_rate);
265 for (i = 0; i < i_mpr.n_plugs; i++)
266 {
267 if (iec61883_get_iPCRX( handle, 0xffc0 | device, &i_pcr, i) < 0)
268 FAIL("error reading iPCR")
269 else
270 {
271 printf( "iPCR[%d] online=%d, bcast_connection=%d, n_p2p_connections=%d\n", i,
272 i_pcr.online, i_pcr.bcast_connection, i_pcr.n_p2p_connections);
273 printf( "\tchannel=%d\n",
274 i_pcr.channel);
275 }
276 }
277 }
278 }
279 printf("\n");
280
281 raw1394_destroy_handle(handle);
282
283 if (!(handle = raw1394_new_handle())) {
284 perror("raw1394 - couldn't get handle");
285 printf("This error usually means that the raw1394 driver is not loaded or that /dev/raw1394 does not exist.\n");
286 exit( -1);
287 }
288
289 if ((numcards = raw1394_get_port_info(handle, pinf, 16)) < 0) {
290 perror("raw1394 - couldn't get card info");
291 exit( -1);
292 }
293
294 }
295
296 return 0;
297 }