Debian Codegraph @

tools/testlibraw.c

raw
1 /* -*- c-basic-offset: 8 -*-
2 *
3 * libraw1394 - library for raw access to the 1394 bus with the Linux subsystem.
4 *
5 * Copyright (C) 1999,2000 Andreas Bombe
6 *
7 * This library is licensed under the GNU Lesser General Public License (LGPL),
8 * version 2.1 or later. See the file COPYING.LIB in the distribution for
9 * details.
10 */
11
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <sys/poll.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include <arpa/inet.h>
20
21 #include "../src/raw1394.h"
22 #include "../src/csr.h"
23
24
25 #define TESTADDR (CSR_REGISTER_BASE + CSR_CONFIG_ROM)
26
27 const char not_compatible[] = "\
28 This libraw1394 does not work with your version of Linux. You need a different\n\
29 version that matches your kernel (see kernel help text for the raw1394 option to\n\
30 find out which is the correct version).\n";
31
32 const char not_loaded[] = "\
33 This probably means that you don't have raw1394 support in the kernel or that\n\
34 you haven't loaded the raw1394 module.\n";
35
36 quadlet_t buffer;
37
38 int my_tag_handler(raw1394handle_t handle, unsigned long tag,
39 raw1394_errcode_t errcode)
40 {
41 int err = raw1394_errcode_to_errno(errcode);
42
43 if (err) {
44 printf("failed with error: %s\n", strerror(err));
45 } else {
46 printf("completed with value 0x%08x\n", buffer);
47 }
48
49 return 0;
50 }
51
52 static const unsigned char fcp_data[] =
53 { 0x1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
54
55 int my_fcp_handler(raw1394handle_t handle, nodeid_t nodeid, int response,
56 size_t length, unsigned char *data)
57 {
58 printf(" got fcp %s from node %d of %d bytes:",
59 (response ? "response" : "command"), nodeid & 0x3f, (int)length);
60
61 if (memcmp(fcp_data, data, sizeof fcp_data) != 0)
62 printf("ERROR: fcp payload not correct\n");
63
64 while (length) {
65 printf(" %02x", *data);
66 data++;
67 length--;
68 }
69
70 printf("\n");
71
72 return 0;
73 }
74
75 static void
76 test_fcp(raw1394handle_t handle)
77 {
78 printf("\n - testing FCP monitoring on local node\n");
79 raw1394_set_fcp_handler(handle, my_fcp_handler);
80 raw1394_start_fcp_listen(handle);
81 raw1394_write(handle, raw1394_get_local_id(handle),
82 CSR_REGISTER_BASE + CSR_FCP_COMMAND, sizeof(fcp_data),
83 (quadlet_t *)fcp_data);
84 raw1394_write(handle, raw1394_get_local_id(handle),
85 CSR_REGISTER_BASE + CSR_FCP_RESPONSE, sizeof(fcp_data),
86 (quadlet_t *)fcp_data);
87 }
88
89 static void
90 read_topology_map(raw1394handle_t handle)
91 {
92 quadlet_t map[70];
93 nodeid_t local_id;
94 int node_count, self_id_count, i, retval;
95
96 local_id = raw1394_get_local_id(handle) | 0xffc0;
97
98 retval = raw1394_read(handle, local_id,
99 CSR_REGISTER_BASE + CSR_TOPOLOGY_MAP, 12, &map[0]);
100 if (retval < 0) {
101 perror("\n - topology map: raw1394_read failed with error");
102 return;
103 }
104
105 self_id_count = ntohl(map[2]) & 0xffff;
106 node_count = ntohl(map[2]) >> 16;
107 retval = raw1394_read(handle, local_id,
108 CSR_REGISTER_BASE + CSR_TOPOLOGY_MAP + 12,
109 self_id_count * sizeof map[0], &map[3]);
110 if (retval < 0) {
111 perror("\n - topology map: raw1394_read failed with error");
112 return;
113 }
114
115 printf("\n - topology map: %d nodes, %d self ids, generation %d\n",
116 node_count, self_id_count, ntohl(map[1]));
117 for (i = 0; i < self_id_count; i++)
118 printf(" 0x%08x\n", ntohl(map[3 + i]));
119 }
120
121 static const quadlet_t unit_directory_data[] = {
122 0x00060000, /* directory_length (CRC left blank) */
123 0x1258595a, /* a bogus unit_specifier_id: XYZ */
124 0x13616263, /* unit_sw_version: abc */
125 0x036c7277, /* a bogus vendor OUI: lrw */
126 0x81000003, /* textual descriptor offset */
127 0x17000001, /* model: 1 */
128 0x81000007, /* textual descriptor offset */
129
130 0x00050000, /* descriptor_length (CRC left blank) */
131 0x00000000, /* descriptor_type: text */
132 0x00000000, /* minimal ASCII, English */
133 0x6c696272, /* "libr" */
134 0x61773133, /* "aw13" */
135 0x39340000, /* "94" */
136
137 0x00050000, /* descriptor_length (CRC left blank) */
138 0x00000000, /* descriptor_type: text */
139 0x00000000, /* minimal ASCII, English */
140 0x74657374, /* "test" */
141 0x6c696272, /* "libr" */
142 0x61770000, /* "aw" */
143 };
144 #define IEEE1212_KEY_UNIT_DIRECTORY 0xd1000000
145
146 static void
147 test_config_rom(raw1394handle_t handle)
148 {
149 quadlet_t rom[0x100] = { 0, };
150 u_int32_t token;
151 size_t rom_size;
152 unsigned char rom_version;
153 int i, retval;
154
155 printf("\n - testing config rom\n");
156 retval = raw1394_get_config_rom(handle, rom, 0x100,
157 &rom_size, &rom_version);
158 printf(" get_config_rom returned %d, romsize %d, rom_version %d\n",
159 retval, (int)rom_size, rom_version);
160 printf(" here are the first 10 quadlets:\n");
161 for (i = 0; i < 10; i++)
162 printf(" 0x%08x\n", rom[i]);
163
164 retval = raw1394_update_config_rom(handle, rom, rom_size, rom_version);
165 perror(" raw1394_update_config_rom failed with error");
166
167 retval = raw1394_add_config_rom_descriptor(handle, &token,
168 0, IEEE1212_KEY_UNIT_DIRECTORY,
169 unit_directory_data, sizeof(unit_directory_data));
170 if (retval) {
171 printf(" raw1394_add_config_rom_descriptor failed with error");
172 return;
173 }
174
175 printf(" added unit '0x58595a:0x616263', reverting in 5 seconds\n");
176 sleep(5);
177 retval = raw1394_remove_config_rom_descriptor(handle, token);
178 if (retval)
179 printf(" raw1394_remove_config_rom_descriptor failed with error");
180 else
181 printf(" unit '0x58595a:0x616263' removed\n");
182 }
183
184 static void
185 read_cycle_timer(raw1394handle_t handle)
186 {
187 u_int32_t ct;
188 u_int64_t local_time;
189 time_t seconds;
190 int retval;
191
192 retval = raw1394_read_cycle_timer(handle, &ct, &local_time);
193 if (retval < 0) {
194 perror("\n - raw1394_read_cycle_timer failed with error");
195 return;
196 }
197 printf("\n - cycle timer: %d seconds, %d cycles, %d sub-cycles\n",
198 ct >> 25, (ct >> 12) & 0x1fff, ct & 0xfff);
199 seconds = local_time / 1000000;
200 printf(" local time from CLOCK_REALTIME: %lld us = %s",
201 (unsigned long long)local_time, ctime(&seconds));
202
203 retval = raw1394_read_cycle_timer_and_clock(handle, &ct, &local_time,
204 CLOCK_MONOTONIC);
205 if (retval < 0) {
206 perror("\n raw1394_read_cycle_timer_and_clock failed with error");
207 return;
208 }
209 printf(" cycle timer: %d seconds, %d cycles, %d sub-cycles\n",
210 ct >> 25, (ct >> 12) & 0x1fff, ct & 0xfff);
211 printf(" local time from CLOCK_MONOTONIC: %lld us\n",
212 (unsigned long long)local_time);
213
214 #if defined(CLOCK_MONOTONIC_RAW)
215 retval = raw1394_read_cycle_timer_and_clock(handle, &ct, &local_time,
216 CLOCK_MONOTONIC_RAW);
217 if (retval < 0) {
218 perror("\n raw1394_read_cycle_timer_and_clock failed with error");
219 return;
220 }
221 printf(" cycle timer: %d seconds, %d cycles, %d sub-cycles\n",
222 ct >> 25, (ct >> 12) & 0x1fff, ct & 0xfff);
223 printf(" local time from CLOCK_MONOTONIC_RAW: %lld us\n",
224 (unsigned long long)local_time);
225 #endif
226 }
227
228 int test_card(int card)
229 {
230 raw1394handle_t handle;
231 struct raw1394_portinfo *portinfo;
232 tag_handler_t std_handler;
233 struct pollfd pfd;
234 int i, l, n, numcards, retval, s;
235
236 portinfo = malloc(sizeof(*portinfo) * (card + 1));
237 if (!portinfo)
238 return -1;
239
240 handle = raw1394_new_handle();
241
242 if (!handle) {
243 if (!errno) {
244 printf(not_compatible);
245 } else {
246 perror("couldn't get handle");
247 printf(not_loaded);
248 }
249 free(portinfo);
250 return -1;
251 }
252
253 if (card == 0) {
254 printf("successfully got handle\n");
255 printf("current generation number: %d\n",
256 raw1394_get_generation(handle));
257 }
258
259 numcards = raw1394_get_port_info(handle, portinfo, card + 1);
260 if (numcards < card)
261 perror("couldn't get card info");
262 else if (card == 0)
263 printf("%d card%s found\n",
264 numcards, numcards == 1 ? "" : "s");
265
266 if (numcards <= card)
267 goto out;
268
269 printf("\ncard %d, name: %s\n", card, portinfo[card].name);
270
271 if (raw1394_set_port(handle, card) < 0) {
272 perror("couldn't set port");
273 goto out;
274 }
275
276 n = raw1394_get_nodecount(handle);
277 l = raw1394_get_local_id(handle) & 0x3f;
278 i = raw1394_get_irm_id(handle) & 0x3f;
279 printf("%d nodes on bus, local ID is %d, IRM is %d\n", n, l, i);
280
281 if (n > 0)
282 printf("\n - getting speeds between between nodes and local node\n");
283 for (i = 0; i < n; i++) {
284 printf(" node %d: ", i);
285 fflush(stdout);
286 s = raw1394_get_speed(handle, 0xffc0 | i);
287 if (s >= 0)
288 printf("S%d00%s\n", 1 << s,
289 i == l ? " (local node)" : "");
290 else
291 perror("unknown");
292 }
293
294 if (n > 0) {
295 printf("\n - doing transactions with custom tag handler\n");
296 std_handler = raw1394_set_tag_handler(handle, my_tag_handler);
297 }
298 for (i = 0; i < n; i++) {
299 printf(" read from node %d... ", i);
300 fflush(stdout);
301 buffer = 0;
302
303 if (raw1394_start_read(handle, 0xffc0 | i, TESTADDR, 4,
304 &buffer, 0) < 0) {
305 perror("failed");
306 continue;
307 }
308 if (raw1394_loop_iterate(handle))
309 perror("failed");
310 }
311
312 if (n > 0) {
313 printf("\n - using standard tag handler and synchronous calls\n");
314 raw1394_set_tag_handler(handle, std_handler);
315 }
316 for (i = 0; i < n; i++) {
317 printf(" read from node %d... ", i);
318 fflush(stdout);
319 buffer = 0;
320
321 retval = raw1394_read(handle, 0xffc0 | i, TESTADDR, 4, &buffer);
322 if (retval < 0)
323 perror("failed with error");
324 else
325 printf("completed with value 0x%08x\n", buffer);
326 }
327
328 test_fcp(handle);
329 read_topology_map(handle);
330 test_config_rom(handle);
331 read_cycle_timer(handle);
332
333 printf("\n - posting 0xdeadbeef as an echo request\n");
334 raw1394_echo_request(handle, 0xdeadbeef);
335
336 printf(" polling for leftover messages\n");
337 pfd.fd = raw1394_get_fd(handle);
338 pfd.events = POLLIN;
339 pfd.revents = 0;
340 while (1) {
341 retval = poll(&pfd, 1, 10);
342 if (retval < 1)
343 break;
344 retval = raw1394_loop_iterate(handle);
345 if (retval != 0)
346 printf(" raw1394_loop_iterate() returned 0x%08x\n",
347 retval);
348 }
349
350 if (retval < 0)
351 perror("poll failed");
352 out:
353 raw1394_destroy_handle(handle);
354 free(portinfo);
355 return numcards;
356 }
357
358 int main(int argc, char **argv)
359 {
360 int card = 0, numcards;
361
362 do
363 numcards = test_card(card);
364 while (++card < numcards);
365
366 return numcards < 0;
367 }