Blame view

tools/firewire/nosy-dump.c 25.1 KB
9f6d3c4b7   Stefan Richter   tools/firewire: a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   * nosy-dump - Interface to snoop mode driver for TI PCILynx 1394 controllers
   * Copyright (C) 2002-2006 Kristian Høgsberg
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   */
92c16f7e9   Stefan Richter   tools/firewire: n...
19
20
21
  #include <byteswap.h>
  #include <endian.h>
  #include <fcntl.h>
83ef7c759   Stefan Richter   tools/firewire: n...
22
  #include <linux/firewire-constants.h>
92c16f7e9   Stefan Richter   tools/firewire: n...
23
24
25
  #include <poll.h>
  #include <popt.h>
  #include <signal.h>
9f6d3c4b7   Stefan Richter   tools/firewire: a...
26
  #include <stdio.h>
92c16f7e9   Stefan Richter   tools/firewire: n...
27
  #include <stdlib.h>
9f6d3c4b7   Stefan Richter   tools/firewire: a...
28
  #include <string.h>
9f6d3c4b7   Stefan Richter   tools/firewire: a...
29
30
  #include <sys/ioctl.h>
  #include <sys/time.h>
9f6d3c4b7   Stefan Richter   tools/firewire: a...
31
  #include <termios.h>
92c16f7e9   Stefan Richter   tools/firewire: n...
32
  #include <unistd.h>
9f6d3c4b7   Stefan Richter   tools/firewire: a...
33
34
  
  #include "list.h"
9f6d3c4b7   Stefan Richter   tools/firewire: a...
35
  #include "nosy-dump.h"
92c16f7e9   Stefan Richter   tools/firewire: n...
36
  #include "nosy-user.h"
9f6d3c4b7   Stefan Richter   tools/firewire: a...
37
38
  
  enum {
92c16f7e9   Stefan Richter   tools/firewire: n...
39
40
41
42
  	PACKET_FIELD_DETAIL		= 0x01,
  	PACKET_FIELD_DATA_LENGTH	= 0x02,
  	/* Marks the fields we print in transaction view. */
  	PACKET_FIELD_TRANSACTION	= 0x04,
9f6d3c4b7   Stefan Richter   tools/firewire: a...
43
  };
92c16f7e9   Stefan Richter   tools/firewire: n...
44
45
46
47
  static void print_packet(uint32_t *data, size_t length);
  static void decode_link_packet(struct link_packet *packet, size_t length,
  			       int include_flags, int exclude_flags);
  static int run = 1;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
48
49
50
51
  sig_t sys_sigint_handler;
  
  static char *option_nosy_device = "/dev/nosy";
  static char *option_view = "packet";
92c16f7e9   Stefan Richter   tools/firewire: n...
52
53
54
55
56
57
58
  static char *option_output;
  static char *option_input;
  static int option_hex;
  static int option_iso;
  static int option_cycle_start;
  static int option_version;
  static int option_verbose;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
59
60
  
  enum {
92c16f7e9   Stefan Richter   tools/firewire: n...
61
62
63
  	VIEW_TRANSACTION,
  	VIEW_PACKET,
  	VIEW_STATS,
9f6d3c4b7   Stefan Richter   tools/firewire: a...
64
65
66
  };
  
  static const struct poptOption options[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  	{
  		.longName	= "device",
  		.shortName	= 'd',
  		.argInfo	= POPT_ARG_STRING,
  		.arg		= &option_nosy_device,
  		.descrip	= "Path to nosy device.",
  		.argDescrip	= "DEVICE"
  	},
  	{
  		.longName	= "view",
  		.argInfo	= POPT_ARG_STRING,
  		.arg		= &option_view,
  		.descrip	= "Specify view of bus traffic: packet, transaction or stats.",
  		.argDescrip	= "VIEW"
  	},
  	{
  		.longName	= "hex",
  		.shortName	= 'x',
  		.argInfo	= POPT_ARG_NONE,
  		.arg		= &option_hex,
  		.descrip	= "Print each packet in hex.",
  	},
  	{
  		.longName	= "iso",
  		.argInfo	= POPT_ARG_NONE,
  		.arg		= &option_iso,
  		.descrip	= "Print iso packets.",
  	},
  	{
  		.longName	= "cycle-start",
  		.argInfo	= POPT_ARG_NONE,
  		.arg		= &option_cycle_start,
  		.descrip	= "Print cycle start packets.",
  	},
  	{
  		.longName	= "verbose",
  		.shortName	= 'v',
  		.argInfo	= POPT_ARG_NONE,
  		.arg		= &option_verbose,
  		.descrip	= "Verbose packet view.",
  	},
  	{
  		.longName	= "output",
  		.shortName	= 'o',
  		.argInfo	= POPT_ARG_STRING,
  		.arg		= &option_output,
  		.descrip	= "Log to output file.",
  		.argDescrip	= "FILENAME"
  	},
  	{
  		.longName	= "input",
  		.shortName	= 'i',
  		.argInfo	= POPT_ARG_STRING,
  		.arg		= &option_input,
  		.descrip	= "Decode log from file.",
  		.argDescrip	= "FILENAME"
  	},
  	{
  		.longName	= "version",
  		.argInfo	= POPT_ARG_NONE,
  		.arg		= &option_version,
  		.descrip	= "Specify print version info.",
  	},
  	POPT_AUTOHELP
  	POPT_TABLEEND
9f6d3c4b7   Stefan Richter   tools/firewire: a...
132
  };
92c16f7e9   Stefan Richter   tools/firewire: n...
133
  /* Allow all ^C except the first to interrupt the program in the usual way. */
468066f77   Stefan Richter   tools/firewire: n...
134
  static void
9f6d3c4b7   Stefan Richter   tools/firewire: a...
135
136
  sigint_handler(int signal_num)
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
137
138
139
140
  	if (run == 1) {
  		run = 0;
  		signal(SIGINT, SIG_DFL);
  	}
9f6d3c4b7   Stefan Richter   tools/firewire: a...
141
  }
468066f77   Stefan Richter   tools/firewire: n...
142
  static struct subaction *
1bcc69fb6   Stefan Richter   tools/firewire: n...
143
  subaction_create(uint32_t *data, size_t length)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
144
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
145
  	struct subaction *sa;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
146

92c16f7e9   Stefan Richter   tools/firewire: n...
147
148
149
150
151
  	/* we put the ack in the subaction struct for easy access. */
  	sa = malloc(sizeof *sa - sizeof sa->packet + length);
  	sa->ack = data[length / 4 - 1];
  	sa->length = length;
  	memcpy(&sa->packet, data, length);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
152

92c16f7e9   Stefan Richter   tools/firewire: n...
153
  	return sa;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
154
  }
468066f77   Stefan Richter   tools/firewire: n...
155
  static void
9f6d3c4b7   Stefan Richter   tools/firewire: a...
156
157
  subaction_destroy(struct subaction *sa)
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
158
  	free(sa);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
159
  }
468066f77   Stefan Richter   tools/firewire: n...
160
  static struct list pending_transaction_list = {
92c16f7e9   Stefan Richter   tools/firewire: n...
161
162
  	&pending_transaction_list, &pending_transaction_list
  };
9f6d3c4b7   Stefan Richter   tools/firewire: a...
163

468066f77   Stefan Richter   tools/firewire: n...
164
  static struct link_transaction *
9f6d3c4b7   Stefan Richter   tools/firewire: a...
165
166
  link_transaction_lookup(int request_node, int response_node, int tlabel)
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  	struct link_transaction *t;
  
  	list_for_each_entry(t, &pending_transaction_list, link) {
  		if (t->request_node == request_node &&
  		    t->response_node == response_node &&
  		    t->tlabel == tlabel)
  			return t;
  	}
  
  	t = malloc(sizeof *t);
  	t->request_node = request_node;
  	t->response_node = response_node;
  	t->tlabel = tlabel;
  	list_init(&t->request_list);
  	list_init(&t->response_list);
  
  	list_append(&pending_transaction_list, &t->link);
  
  	return t;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
186
  }
468066f77   Stefan Richter   tools/firewire: n...
187
  static void
9f6d3c4b7   Stefan Richter   tools/firewire: a...
188
189
  link_transaction_destroy(struct link_transaction *t)
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
190
191
192
193
194
195
196
197
198
199
200
201
202
  	struct subaction *sa;
  
  	while (!list_empty(&t->request_list)) {
  		sa = list_head(&t->request_list, struct subaction, link);
  		list_remove(&sa->link);
  		subaction_destroy(sa);
  	}
  	while (!list_empty(&t->response_list)) {
  		sa = list_head(&t->response_list, struct subaction, link);
  		list_remove(&sa->link);
  		subaction_destroy(sa);
  	}
  	free(t);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
203
204
205
  }
  
  struct protocol_decoder {
92c16f7e9   Stefan Richter   tools/firewire: n...
206
207
  	const char *name;
  	int (*decode)(struct link_transaction *t);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
208
  };
468066f77   Stefan Richter   tools/firewire: n...
209
  static const struct protocol_decoder protocol_decoders[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
210
  	{ "FCP", decode_fcp }
9f6d3c4b7   Stefan Richter   tools/firewire: a...
211
  };
468066f77   Stefan Richter   tools/firewire: n...
212
  static void
9f6d3c4b7   Stefan Richter   tools/firewire: a...
213
214
  handle_transaction(struct link_transaction *t)
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  	struct subaction *sa;
  	int i;
  
  	if (!t->request) {
  		printf("BUG in handle_transaction
  ");
  		return;
  	}
  
  	for (i = 0; i < array_length(protocol_decoders); i++)
  		if (protocol_decoders[i].decode(t))
  			break;
  
  	/* HACK: decode only fcp right now. */
  	return;
  
  	decode_link_packet(&t->request->packet, t->request->length,
  			   PACKET_FIELD_TRANSACTION, 0);
  	if (t->response)
  		decode_link_packet(&t->response->packet, t->request->length,
  				   PACKET_FIELD_TRANSACTION, 0);
  	else
  		printf("[no response]");
  
  	if (option_verbose) {
  		list_for_each_entry(sa, &t->request_list, link)
  			print_packet((uint32_t *) &sa->packet, sa->length);
  		list_for_each_entry(sa, &t->response_list, link)
  			print_packet((uint32_t *) &sa->packet, sa->length);
  	}
  	printf("\r
  ");
  
  	link_transaction_destroy(t);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
249
  }
468066f77   Stefan Richter   tools/firewire: n...
250
  static void
9f6d3c4b7   Stefan Richter   tools/firewire: a...
251
252
  clear_pending_transaction_list(void)
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
253
254
255
256
257
258
259
260
261
  	struct link_transaction *t;
  
  	while (!list_empty(&pending_transaction_list)) {
  		t = list_head(&pending_transaction_list,
  			      struct link_transaction, link);
  		list_remove(&t->link);
  		link_transaction_destroy(t);
  		/* print unfinished transactions */
  	}
9f6d3c4b7   Stefan Richter   tools/firewire: a...
262
263
264
  }
  
  static const char * const tcode_names[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
265
266
267
268
269
270
  	[0x0] = "write_quadlet_request",	[0x6] = "read_quadlet_response",
  	[0x1] = "write_block_request",		[0x7] = "read_block_response",
  	[0x2] = "write_response",		[0x8] = "cycle_start",
  	[0x3] = "reserved",			[0x9] = "lock_request",
  	[0x4] = "read_quadlet_request",		[0xa] = "iso_data",
  	[0x5] = "read_block_request",		[0xb] = "lock_response",
9f6d3c4b7   Stefan Richter   tools/firewire: a...
271
272
273
  };
  
  static const char * const ack_names[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
274
275
276
277
278
279
280
281
  	[0x0] = "no ack",			[0x8] = "reserved (0x08)",
  	[0x1] = "ack_complete",			[0x9] = "reserved (0x09)",
  	[0x2] = "ack_pending",			[0xa] = "reserved (0x0a)",
  	[0x3] = "reserved (0x03)",		[0xb] = "reserved (0x0b)",
  	[0x4] = "ack_busy_x",			[0xc] = "reserved (0x0c)",
  	[0x5] = "ack_busy_a",			[0xd] = "ack_data_error",
  	[0x6] = "ack_busy_b",			[0xe] = "ack_type_error",
  	[0x7] = "reserved (0x07)",		[0xf] = "reserved (0x0f)",
9f6d3c4b7   Stefan Richter   tools/firewire: a...
282
283
284
  };
  
  static const char * const rcode_names[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
285
286
287
288
  	[0x0] = "complete",			[0x4] = "conflict_error",
  	[0x1] = "reserved (0x01)",		[0x5] = "data_error",
  	[0x2] = "reserved (0x02)",		[0x6] = "type_error",
  	[0x3] = "reserved (0x03)",		[0x7] = "address_error",
9f6d3c4b7   Stefan Richter   tools/firewire: a...
289
290
291
  };
  
  static const char * const retry_names[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
292
293
294
295
  	[0x0] = "retry_1",
  	[0x1] = "retry_x",
  	[0x2] = "retry_a",
  	[0x3] = "retry_b",
9f6d3c4b7   Stefan Richter   tools/firewire: a...
296
297
298
  };
  
  enum {
92c16f7e9   Stefan Richter   tools/firewire: n...
299
300
301
302
  	PACKET_RESERVED,
  	PACKET_REQUEST,
  	PACKET_RESPONSE,
  	PACKET_OTHER,
9f6d3c4b7   Stefan Richter   tools/firewire: a...
303
304
305
  };
  
  struct packet_info {
92c16f7e9   Stefan Richter   tools/firewire: n...
306
307
308
  	const char *name;
  	int type;
  	int response_tcode;
468066f77   Stefan Richter   tools/firewire: n...
309
  	const struct packet_field *fields;
92c16f7e9   Stefan Richter   tools/firewire: n...
310
  	int field_count;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
311
312
313
  };
  
  struct packet_field {
92c16f7e9   Stefan Richter   tools/firewire: n...
314
315
316
317
318
319
  	const char *name; /* Short name for field. */
  	int offset;	/* Location of field, specified in bits; */
  			/* negative means from end of packet.    */
  	int width;	/* Width of field, 0 means use data_length. */
  	int flags;	/* Show options. */
  	const char * const *value_names;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
320
  };
92c16f7e9   Stefan Richter   tools/firewire: n...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  #define COMMON_REQUEST_FIELDS						\
  	{ "dest", 0, 16, PACKET_FIELD_TRANSACTION },			\
  	{ "tl", 16, 6 },						\
  	{ "rt", 22, 2, PACKET_FIELD_DETAIL, retry_names },		\
  	{ "tcode", 24, 4, PACKET_FIELD_TRANSACTION, tcode_names },	\
  	{ "pri", 28, 4, PACKET_FIELD_DETAIL },				\
  	{ "src", 32, 16, PACKET_FIELD_TRANSACTION },			\
  	{ "offs", 48, 48, PACKET_FIELD_TRANSACTION }
  
  #define COMMON_RESPONSE_FIELDS						\
  	{ "dest", 0, 16 },						\
  	{ "tl", 16, 6 },						\
  	{ "rt", 22, 2, PACKET_FIELD_DETAIL, retry_names },		\
  	{ "tcode", 24, 4, 0, tcode_names },				\
  	{ "pri", 28, 4, PACKET_FIELD_DETAIL },				\
  	{ "src", 32, 16 },						\
  	{ "rcode", 48, 4, PACKET_FIELD_TRANSACTION, rcode_names }
9f6d3c4b7   Stefan Richter   tools/firewire: a...
338

468066f77   Stefan Richter   tools/firewire: n...
339
  static const struct packet_field read_quadlet_request_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
340
341
342
  	COMMON_REQUEST_FIELDS,
  	{ "crc", 96, 32, PACKET_FIELD_DETAIL },
  	{ "ack", 156, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
343
  };
468066f77   Stefan Richter   tools/firewire: n...
344
  static const struct packet_field read_quadlet_response_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
345
346
347
348
  	COMMON_RESPONSE_FIELDS,
  	{ "data", 96, 32, PACKET_FIELD_TRANSACTION },
  	{ "crc", 128, 32, PACKET_FIELD_DETAIL },
  	{ "ack", 188, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
349
  };
468066f77   Stefan Richter   tools/firewire: n...
350
  static const struct packet_field read_block_request_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
351
352
353
354
355
  	COMMON_REQUEST_FIELDS,
  	{ "data_length", 96, 16, PACKET_FIELD_TRANSACTION },
  	{ "extended_tcode", 112, 16 },
  	{ "crc", 128, 32, PACKET_FIELD_DETAIL },
  	{ "ack", 188, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
356
  };
468066f77   Stefan Richter   tools/firewire: n...
357
  static const struct packet_field block_response_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
358
359
360
361
362
363
364
  	COMMON_RESPONSE_FIELDS,
  	{ "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH },
  	{ "extended_tcode", 112, 16 },
  	{ "crc", 128, 32, PACKET_FIELD_DETAIL },
  	{ "data", 160, 0, PACKET_FIELD_TRANSACTION },
  	{ "crc", -64, 32, PACKET_FIELD_DETAIL },
  	{ "ack", -4, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
365
  };
468066f77   Stefan Richter   tools/firewire: n...
366
  static const struct packet_field write_quadlet_request_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
367
368
369
  	COMMON_REQUEST_FIELDS,
  	{ "data", 96, 32, PACKET_FIELD_TRANSACTION },
  	{ "ack", -4, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
370
  };
468066f77   Stefan Richter   tools/firewire: n...
371
  static const struct packet_field block_request_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
372
373
374
375
376
377
378
  	COMMON_REQUEST_FIELDS,
  	{ "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH | PACKET_FIELD_TRANSACTION },
  	{ "extended_tcode", 112, 16, PACKET_FIELD_TRANSACTION },
  	{ "crc", 128, 32, PACKET_FIELD_DETAIL },
  	{ "data", 160, 0, PACKET_FIELD_TRANSACTION },
  	{ "crc", -64, 32, PACKET_FIELD_DETAIL },
  	{ "ack", -4, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
379
  };
468066f77   Stefan Richter   tools/firewire: n...
380
  static const struct packet_field write_response_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
381
382
383
  	COMMON_RESPONSE_FIELDS,
  	{ "reserved", 64, 32, PACKET_FIELD_DETAIL },
  	{ "ack", -4, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
384
  };
468066f77   Stefan Richter   tools/firewire: n...
385
  static const struct packet_field iso_data_fields[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
386
387
388
389
390
391
392
393
394
  	{ "data_length", 0, 16, PACKET_FIELD_DATA_LENGTH },
  	{ "tag", 16, 2 },
  	{ "channel", 18, 6 },
  	{ "tcode", 24, 4, 0, tcode_names },
  	{ "sy", 28, 4 },
  	{ "crc", 32, 32, PACKET_FIELD_DETAIL },
  	{ "data", 64, 0 },
  	{ "crc", -64, 32, PACKET_FIELD_DETAIL },
  	{ "ack", -4, 4, 0, ack_names },
9f6d3c4b7   Stefan Richter   tools/firewire: a...
395
  };
468066f77   Stefan Richter   tools/firewire: n...
396
  static const struct packet_info packet_info[] = {
92c16f7e9   Stefan Richter   tools/firewire: n...
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
  	{
  		.name		= "write_quadlet_request",
  		.type		= PACKET_REQUEST,
  		.response_tcode	= TCODE_WRITE_RESPONSE,
  		.fields		= write_quadlet_request_fields,
  		.field_count	= array_length(write_quadlet_request_fields)
  	},
  	{
  		.name		= "write_block_request",
  		.type		= PACKET_REQUEST,
  		.response_tcode	= TCODE_WRITE_RESPONSE,
  		.fields		= block_request_fields,
  		.field_count	= array_length(block_request_fields)
  	},
  	{
  		.name		= "write_response",
  		.type		= PACKET_RESPONSE,
  		.fields		= write_response_fields,
  		.field_count	= array_length(write_response_fields)
  	},
  	{
  		.name		= "reserved",
  		.type		= PACKET_RESERVED,
  	},
  	{
  		.name		= "read_quadlet_request",
  		.type		= PACKET_REQUEST,
  		.response_tcode	= TCODE_READ_QUADLET_RESPONSE,
  		.fields		= read_quadlet_request_fields,
  		.field_count	= array_length(read_quadlet_request_fields)
  	},
  	{
  		.name		= "read_block_request",
  		.type		= PACKET_REQUEST,
  		.response_tcode	= TCODE_READ_BLOCK_RESPONSE,
  		.fields		= read_block_request_fields,
  		.field_count	= array_length(read_block_request_fields)
  	},
  	{
  		.name		= "read_quadlet_response",
  		.type		= PACKET_RESPONSE,
  		.fields		= read_quadlet_response_fields,
  		.field_count	= array_length(read_quadlet_response_fields)
  	},
  	{
  		.name		= "read_block_response",
  		.type		= PACKET_RESPONSE,
  		.fields		= block_response_fields,
  		.field_count	= array_length(block_response_fields)
  	},
  	{
  		.name		= "cycle_start",
  		.type		= PACKET_OTHER,
  		.fields		= write_quadlet_request_fields,
  		.field_count	= array_length(write_quadlet_request_fields)
  	},
  	{
  		.name		= "lock_request",
  		.type		= PACKET_REQUEST,
  		.fields		= block_request_fields,
  		.field_count	= array_length(block_request_fields)
  	},
  	{
  		.name		= "iso_data",
  		.type		= PACKET_OTHER,
  		.fields		= iso_data_fields,
  		.field_count	= array_length(iso_data_fields)
  	},
  	{
  		.name		= "lock_response",
  		.type		= PACKET_RESPONSE,
  		.fields		= block_response_fields,
  		.field_count	= array_length(block_response_fields)
  	},
9f6d3c4b7   Stefan Richter   tools/firewire: a...
471
  };
468066f77   Stefan Richter   tools/firewire: n...
472
  static int
269fe1023   Stefan Richter   tools/firewire: n...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  handle_request_packet(uint32_t *data, size_t length)
  {
  	struct link_packet *p = (struct link_packet *) data;
  	struct subaction *sa, *prev;
  	struct link_transaction *t;
  
  	t = link_transaction_lookup(p->common.source, p->common.destination,
  			p->common.tlabel);
  	sa = subaction_create(data, length);
  	t->request = sa;
  
  	if (!list_empty(&t->request_list)) {
  		prev = list_tail(&t->request_list,
  				 struct subaction, link);
  
  		if (!ACK_BUSY(prev->ack)) {
  			/*
  			 * error, we should only see ack_busy_* before the
  			 * ack_pending/ack_complete -- this is an ack_pending
  			 * instead (ack_complete would have finished the
  			 * transaction).
  			 */
  		}
  
  		if (prev->packet.common.tcode != sa->packet.common.tcode ||
  		    prev->packet.common.tlabel != sa->packet.common.tlabel) {
  			/* memcmp() ? */
  			/* error, these should match for retries. */
  		}
  	}
  
  	list_append(&t->request_list, &sa->link);
  
  	switch (sa->ack) {
  	case ACK_COMPLETE:
83ef7c759   Stefan Richter   tools/firewire: n...
508
509
  		if (p->common.tcode != TCODE_WRITE_QUADLET_REQUEST &&
  		    p->common.tcode != TCODE_WRITE_BLOCK_REQUEST)
269fe1023   Stefan Richter   tools/firewire: n...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
  			/* error, unified transactions only allowed for write */;
  		list_remove(&t->link);
  		handle_transaction(t);
  		break;
  
  	case ACK_NO_ACK:
  	case ACK_DATA_ERROR:
  	case ACK_TYPE_ERROR:
  		list_remove(&t->link);
  		handle_transaction(t);
  		break;
  
  	case ACK_PENDING:
  		/* request subaction phase over, wait for response. */
  		break;
  
  	case ACK_BUSY_X:
  	case ACK_BUSY_A:
  	case ACK_BUSY_B:
  		/* ok, wait for retry. */
  		/* check that retry protocol is respected. */
  		break;
  	}
  
  	return 1;
  }
  
  static int
  handle_response_packet(uint32_t *data, size_t length)
  {
  	struct link_packet *p = (struct link_packet *) data;
  	struct subaction *sa, *prev;
  	struct link_transaction *t;
  
  	t = link_transaction_lookup(p->common.destination, p->common.source,
  			p->common.tlabel);
  	if (list_empty(&t->request_list)) {
  		/* unsolicited response */
  	}
  
  	sa = subaction_create(data, length);
  	t->response = sa;
  
  	if (!list_empty(&t->response_list)) {
  		prev = list_tail(&t->response_list, struct subaction, link);
  
  		if (!ACK_BUSY(prev->ack)) {
  			/*
  			 * error, we should only see ack_busy_* before the
  			 * ack_pending/ack_complete
  			 */
  		}
  
  		if (prev->packet.common.tcode != sa->packet.common.tcode ||
  		    prev->packet.common.tlabel != sa->packet.common.tlabel) {
  			/* use memcmp() instead? */
  			/* error, these should match for retries. */
  		}
  	} else {
  		prev = list_tail(&t->request_list, struct subaction, link);
  		if (prev->ack != ACK_PENDING) {
  			/*
  			 * error, should not get response unless last request got
  			 * ack_pending.
  			 */
  		}
  
  		if (packet_info[prev->packet.common.tcode].response_tcode !=
  		    sa->packet.common.tcode) {
  			/* error, tcode mismatch */
  		}
  	}
  
  	list_append(&t->response_list, &sa->link);
  
  	switch (sa->ack) {
  	case ACK_COMPLETE:
  	case ACK_NO_ACK:
  	case ACK_DATA_ERROR:
  	case ACK_TYPE_ERROR:
  		list_remove(&t->link);
  		handle_transaction(t);
  		/* transaction complete, remove t from pending list. */
  		break;
  
  	case ACK_PENDING:
  		/* error for responses. */
  		break;
  
  	case ACK_BUSY_X:
  	case ACK_BUSY_A:
  	case ACK_BUSY_B:
  		/* no problem, wait for next retry */
  		break;
  	}
  
  	return 1;
  }
  
  static int
1bcc69fb6   Stefan Richter   tools/firewire: n...
610
  handle_packet(uint32_t *data, size_t length)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
611
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
612
613
614
615
616
617
  	if (length == 0) {
  		printf("bus reset\r
  ");
  		clear_pending_transaction_list();
  	} else if (length > sizeof(struct phy_packet)) {
  		struct link_packet *p = (struct link_packet *) data;
92c16f7e9   Stefan Richter   tools/firewire: n...
618
619
620
  
  		switch (packet_info[p->common.tcode].type) {
  		case PACKET_REQUEST:
269fe1023   Stefan Richter   tools/firewire: n...
621
  			return handle_request_packet(data, length);
92c16f7e9   Stefan Richter   tools/firewire: n...
622
623
  
  		case PACKET_RESPONSE:
269fe1023   Stefan Richter   tools/firewire: n...
624
  			return handle_response_packet(data, length);
92c16f7e9   Stefan Richter   tools/firewire: n...
625
626
627
628
629
  
  		case PACKET_OTHER:
  		case PACKET_RESERVED:
  			return 0;
  		}
9f6d3c4b7   Stefan Richter   tools/firewire: a...
630
  	}
92c16f7e9   Stefan Richter   tools/firewire: n...
631
632
  
  	return 1;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
633
  }
468066f77   Stefan Richter   tools/firewire: n...
634
635
  static unsigned int
  get_bits(struct link_packet *packet, int offset, int width)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
636
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
637
638
  	uint32_t *data = (uint32_t *) packet;
  	uint32_t index, shift, mask;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
639

92c16f7e9   Stefan Richter   tools/firewire: n...
640
641
642
  	index = offset / 32 + 1;
  	shift = 32 - (offset & 31) - width;
  	mask = width == 32 ? ~0 : (1 << width) - 1;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
643

92c16f7e9   Stefan Richter   tools/firewire: n...
644
  	return (data[index] >> shift) & mask;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
645
646
647
648
649
650
651
652
653
  }
  
  #if __BYTE_ORDER == __LITTLE_ENDIAN
  #define byte_index(i) ((i) ^ 3)
  #elif __BYTE_ORDER == __BIG_ENDIAN
  #define byte_index(i) (i)
  #else
  #error unsupported byte order.
  #endif
468066f77   Stefan Richter   tools/firewire: n...
654
655
  static void
  dump_data(unsigned char *data, int length)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
656
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
657
  	int i, print_length;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
658

92c16f7e9   Stefan Richter   tools/firewire: n...
659
660
661
662
  	if (length > 128)
  		print_length = 128;
  	else
  		print_length = length;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
663

92c16f7e9   Stefan Richter   tools/firewire: n...
664
665
666
667
  	for (i = 0; i < print_length; i++)
  		printf("%s%02hhx",
  		       (i % 4 == 0 && i != 0) ? " " : "",
  		       data[byte_index(i)]);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
668

92c16f7e9   Stefan Richter   tools/firewire: n...
669
670
  	if (print_length < length)
  		printf(" (%d more bytes)", length - print_length);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
671
672
673
674
675
676
  }
  
  static void
  decode_link_packet(struct link_packet *packet, size_t length,
  		   int include_flags, int exclude_flags)
  {
468066f77   Stefan Richter   tools/firewire: n...
677
  	const struct packet_info *pi;
92c16f7e9   Stefan Richter   tools/firewire: n...
678
679
680
681
682
683
  	int data_length = 0;
  	int i;
  
  	pi = &packet_info[packet->common.tcode];
  
  	for (i = 0; i < pi->field_count; i++) {
468066f77   Stefan Richter   tools/firewire: n...
684
  		const struct packet_field *f = &pi->fields[i];
92c16f7e9   Stefan Richter   tools/firewire: n...
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
  		int offset;
  
  		if (f->flags & exclude_flags)
  			continue;
  		if (include_flags && !(f->flags & include_flags))
  			continue;
  
  		if (f->offset < 0)
  			offset = length * 8 + f->offset - 32;
  		else
  			offset = f->offset;
  
  		if (f->value_names != NULL) {
  			uint32_t bits;
  
  			bits = get_bits(packet, offset, f->width);
  			printf("%s", f->value_names[bits]);
  		} else if (f->width == 0) {
  			printf("%s=[", f->name);
  			dump_data((unsigned char *) packet + (offset / 8 + 4), data_length);
  			printf("]");
  		} else {
  			unsigned long long bits;
  			int high_width, low_width;
  
  			if ((offset & ~31) != ((offset + f->width - 1) & ~31)) {
  				/* Bit field spans quadlet boundary. */
  				high_width = ((offset + 31) & ~31) - offset;
  				low_width = f->width - high_width;
  
  				bits = get_bits(packet, offset, high_width);
  				bits = (bits << low_width) |
  					get_bits(packet, offset + high_width, low_width);
  			} else {
  				bits = get_bits(packet, offset, f->width);
  			}
  
  			printf("%s=0x%0*llx", f->name, (f->width + 3) / 4, bits);
  
  			if (f->flags & PACKET_FIELD_DATA_LENGTH)
  				data_length = bits;
  		}
  
  		if (i < pi->field_count - 1)
  			printf(", ");
  	}
9f6d3c4b7   Stefan Richter   tools/firewire: a...
731
732
733
  }
  
  static void
1bcc69fb6   Stefan Richter   tools/firewire: n...
734
  print_packet(uint32_t *data, size_t length)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
735
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  	int i;
  
  	printf("%6u  ", data[0]);
  
  	if (length == 4) {
  		printf("bus reset");
  	} else if (length < sizeof(struct phy_packet)) {
  		printf("short packet: ");
  		for (i = 1; i < length / 4; i++)
  			printf("%s%08x", i == 0 ? "[" : " ", data[i]);
  		printf("]");
  
  	} else if (length == sizeof(struct phy_packet) && data[1] == ~data[2]) {
  		struct phy_packet *pp = (struct phy_packet *) data;
  
  		/* phy packet are 3 quadlets: the 1 quadlet payload,
  		 * the bitwise inverse of the payload and the snoop
  		 * mode ack */
  
  		switch (pp->common.identifier) {
  		case PHY_PACKET_CONFIGURATION:
  			if (!pp->phy_config.set_root && !pp->phy_config.set_gap_count) {
  				printf("ext phy config: phy_id=%02x", pp->phy_config.root_id);
  			} else {
  				printf("phy config:");
  				if (pp->phy_config.set_root)
  					printf(" set_root_id=%02x", pp->phy_config.root_id);
  				if (pp->phy_config.set_gap_count)
  					printf(" set_gap_count=%d", pp->phy_config.gap_count);
  			}
  			break;
  
  		case PHY_PACKET_LINK_ON:
  			printf("link-on packet, phy_id=%02x", pp->link_on.phy_id);
  			break;
  
  		case PHY_PACKET_SELF_ID:
  			if (pp->self_id.extended) {
  				printf("extended self id: phy_id=%02x, seq=%d",
  				       pp->ext_self_id.phy_id, pp->ext_self_id.sequence);
  			} else {
  				static const char * const speed_names[] = {
  					"S100", "S200", "S400", "BETA"
  				};
  				printf("self id: phy_id=%02x, link %s, gap_count=%d, speed=%s%s%s",
  				       pp->self_id.phy_id,
  				       (pp->self_id.link_active ? "active" : "not active"),
  				       pp->self_id.gap_count,
  				       speed_names[pp->self_id.phy_speed],
  				       (pp->self_id.contender ? ", irm contender" : ""),
  				       (pp->self_id.initiated_reset ? ", initiator" : ""));
  			}
  			break;
  		default:
  			printf("unknown phy packet: ");
  			for (i = 1; i < length / 4; i++)
  				printf("%s%08x", i == 0 ? "[" : " ", data[i]);
  			printf("]");
  			break;
  		}
  	} else {
  		struct link_packet *packet = (struct link_packet *) data;
  
  		decode_link_packet(packet, length, 0,
  				   option_verbose ? 0 : PACKET_FIELD_DETAIL);
  	}
  
  	if (option_hex) {
  		printf("  [");
  		dump_data((unsigned char *) data + 4, length - 4);
  		printf("]");
  	}
  
  	printf("\r
  ");
9f6d3c4b7   Stefan Richter   tools/firewire: a...
811
812
813
814
815
816
817
  }
  
  #define HIDE_CURSOR	"\033[?25l"
  #define SHOW_CURSOR	"\033[?25h"
  #define CLEAR		"\033[H\033[2J"
  
  static void
1bcc69fb6   Stefan Richter   tools/firewire: n...
818
  print_stats(uint32_t *data, size_t length)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
819
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  	static int bus_reset_count, short_packet_count, phy_packet_count;
  	static int tcode_count[16];
  	static struct timeval last_update;
  	struct timeval now;
  	int i;
  
  	if (length == 0)
  		bus_reset_count++;
  	else if (length < sizeof(struct phy_packet))
  		short_packet_count++;
  	else if (length == sizeof(struct phy_packet) && data[1] == ~data[2])
  		phy_packet_count++;
  	else {
  		struct link_packet *packet = (struct link_packet *) data;
  		tcode_count[packet->common.tcode]++;
  	}
  
  	gettimeofday(&now, NULL);
  	if (now.tv_sec <= last_update.tv_sec &&
  	    now.tv_usec < last_update.tv_usec + 500000)
  		return;
  
  	last_update = now;
  	printf(CLEAR HIDE_CURSOR
  	       "  bus resets              : %8d
  "
  	       "  short packets           : %8d
  "
  	       "  phy packets             : %8d
  ",
  	       bus_reset_count, short_packet_count, phy_packet_count);
  
  	for (i = 0; i < array_length(packet_info); i++)
  		if (packet_info[i].type != PACKET_RESERVED)
  			printf("  %-24s: %8d
  ", packet_info[i].name, tcode_count[i]);
  	printf(SHOW_CURSOR "
  ");
9f6d3c4b7   Stefan Richter   tools/firewire: a...
858
  }
468066f77   Stefan Richter   tools/firewire: n...
859
  static struct termios saved_attributes;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
860

468066f77   Stefan Richter   tools/firewire: n...
861
  static void
92c16f7e9   Stefan Richter   tools/firewire: n...
862
  reset_input_mode(void)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
863
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
864
  	tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
865
  }
468066f77   Stefan Richter   tools/firewire: n...
866
  static void
92c16f7e9   Stefan Richter   tools/firewire: n...
867
  set_input_mode(void)
9f6d3c4b7   Stefan Richter   tools/firewire: a...
868
  {
92c16f7e9   Stefan Richter   tools/firewire: n...
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
  	struct termios tattr;
  
  	/* Make sure stdin is a terminal. */
  	if (!isatty(STDIN_FILENO)) {
  		fprintf(stderr, "Not a terminal.
  ");
  		exit(EXIT_FAILURE);
  	}
  
  	/* Save the terminal attributes so we can restore them later. */
  	tcgetattr(STDIN_FILENO, &saved_attributes);
  	atexit(reset_input_mode);
  
  	/* Set the funny terminal modes. */
  	tcgetattr(STDIN_FILENO, &tattr);
  	tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
  	tattr.c_cc[VMIN] = 1;
  	tattr.c_cc[VTIME] = 0;
  	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
9f6d3c4b7   Stefan Richter   tools/firewire: a...
888
889
890
891
  }
  
  int main(int argc, const char *argv[])
  {
ddbfe7495   Stefan Richter   tools/firewire: n...
892
893
894
  	uint32_t buf[128 * 1024];
  	uint32_t filter;
  	int length, retval, view;
92c16f7e9   Stefan Richter   tools/firewire: n...
895
896
897
  	int fd = -1;
  	FILE *output = NULL, *input = NULL;
  	poptContext con;
92c16f7e9   Stefan Richter   tools/firewire: n...
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
  	char c;
  	struct pollfd pollfds[2];
  
  	sys_sigint_handler = signal(SIGINT, sigint_handler);
  
  	con = poptGetContext(NULL, argc, argv, options, 0);
  	retval = poptGetNextOpt(con);
  	if (retval < -1) {
  		poptPrintUsage(con, stdout, 0);
  		return -1;
  	}
  
  	if (option_version) {
  		printf("dump tool for nosy sniffer, version %s
  ", VERSION);
  		return 0;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
914
  	}
92c16f7e9   Stefan Richter   tools/firewire: n...
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
  	if (__BYTE_ORDER != __LITTLE_ENDIAN)
  		fprintf(stderr, "warning: nosy has only been tested on little "
  			"endian machines
  ");
  
  	if (option_input != NULL) {
  		input = fopen(option_input, "r");
  		if (input == NULL) {
  			fprintf(stderr, "Could not open %s, %m
  ", option_input);
  			return -1;
  		}
  	} else {
  		fd = open(option_nosy_device, O_RDWR);
  		if (fd < 0) {
  			fprintf(stderr, "Could not open %s, %m
  ", option_nosy_device);
  			return -1;
  		}
  		set_input_mode();
  	}
  
  	if (strcmp(option_view, "transaction") == 0)
  		view = VIEW_TRANSACTION;
  	else if (strcmp(option_view, "stats") == 0)
  		view = VIEW_STATS;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
941
  	else
92c16f7e9   Stefan Richter   tools/firewire: n...
942
943
944
945
946
947
948
949
950
951
952
953
  		view = VIEW_PACKET;
  
  	if (option_output) {
  		output = fopen(option_output, "w");
  		if (output == NULL) {
  			fprintf(stderr, "Could not open %s, %m
  ", option_output);
  			return -1;
  		}
  	}
  
  	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
ddbfe7495   Stefan Richter   tools/firewire: n...
954
955
956
957
958
959
960
  	filter = ~0;
  	if (!option_iso)
  		filter &= ~(1 << TCODE_STREAM_DATA);
  	if (!option_cycle_start)
  		filter &= ~(1 << TCODE_CYCLE_START);
  	if (view == VIEW_STATS)
  		filter = ~(1 << TCODE_CYCLE_START);
92c16f7e9   Stefan Richter   tools/firewire: n...
961

ddbfe7495   Stefan Richter   tools/firewire: n...
962
  	ioctl(fd, NOSY_IOC_FILTER, filter);
92c16f7e9   Stefan Richter   tools/firewire: n...
963

ddbfe7495   Stefan Richter   tools/firewire: n...
964
  	ioctl(fd, NOSY_IOC_START);
92c16f7e9   Stefan Richter   tools/firewire: n...
965

ddbfe7495   Stefan Richter   tools/firewire: n...
966
967
968
969
  	pollfds[0].fd = fd;
  	pollfds[0].events = POLLIN;
  	pollfds[1].fd = STDIN_FILENO;
  	pollfds[1].events = POLLIN;
92c16f7e9   Stefan Richter   tools/firewire: n...
970

ddbfe7495   Stefan Richter   tools/firewire: n...
971
972
973
974
975
976
977
978
979
980
981
982
983
  	while (run) {
  		if (input != NULL) {
  			if (fread(&length, sizeof length, 1, input) != 1)
  				return 0;
  			fread(buf, 1, length, input);
  		} else {
  			poll(pollfds, 2, -1);
  			if (pollfds[1].revents) {
  				read(STDIN_FILENO, &c, sizeof c);
  				switch (c) {
  				case 'q':
  					if (output != NULL)
  						fclose(output);
92c16f7e9   Stefan Richter   tools/firewire: n...
984
  					return 0;
92c16f7e9   Stefan Richter   tools/firewire: n...
985
  				}
92c16f7e9   Stefan Richter   tools/firewire: n...
986
  			}
ddbfe7495   Stefan Richter   tools/firewire: n...
987
988
989
990
991
  			if (pollfds[0].revents)
  				length = read(fd, buf, sizeof buf);
  			else
  				continue;
  		}
92c16f7e9   Stefan Richter   tools/firewire: n...
992

ddbfe7495   Stefan Richter   tools/firewire: n...
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
  		if (output != NULL) {
  			fwrite(&length, sizeof length, 1, output);
  			fwrite(buf, 1, length, output);
  		}
  
  		switch (view) {
  		case VIEW_TRANSACTION:
  			handle_packet(buf, length);
  			break;
  		case VIEW_PACKET:
  			print_packet(buf, length);
  			break;
  		case VIEW_STATS:
  			print_stats(buf, length);
  			break;
92c16f7e9   Stefan Richter   tools/firewire: n...
1008
  		}
92c16f7e9   Stefan Richter   tools/firewire: n...
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
  	}
  
  	if (output != NULL)
  		fclose(output);
  
  	close(fd);
  
  	poptFreeContext(con);
  
  	return 0;
9f6d3c4b7   Stefan Richter   tools/firewire: a...
1019
  }