tools/firewire: nosy-dump: remove unused code
[linux-3.10.git] / tools / firewire / nosy-dump.c
index 3d7e1a6..f93b776 100644 (file)
@@ -20,6 +20,7 @@
 #include <byteswap.h>
 #include <endian.h>
 #include <fcntl.h>
+#include <linux/firewire-constants.h>
 #include <poll.h>
 #include <popt.h>
 #include <signal.h>
@@ -133,7 +134,7 @@ static const struct poptOption options[] = {
 };
 
 /* Allow all ^C except the first to interrupt the program in the usual way. */
-void
+static void
 sigint_handler(int signal_num)
 {
        if (run == 1) {
@@ -142,7 +143,7 @@ sigint_handler(int signal_num)
        }
 }
 
-struct subaction *
+static struct subaction *
 subaction_create(uint32_t *data, size_t length)
 {
        struct subaction *sa;
@@ -156,17 +157,17 @@ subaction_create(uint32_t *data, size_t length)
        return sa;
 }
 
-void
+static void
 subaction_destroy(struct subaction *sa)
 {
        free(sa);
 }
 
-struct list pending_transaction_list = {
+static struct list pending_transaction_list = {
        &pending_transaction_list, &pending_transaction_list
 };
 
-struct link_transaction *
+static struct link_transaction *
 link_transaction_lookup(int request_node, int response_node, int tlabel)
 {
        struct link_transaction *t;
@@ -190,7 +191,7 @@ link_transaction_lookup(int request_node, int response_node, int tlabel)
        return t;
 }
 
-void
+static void
 link_transaction_destroy(struct link_transaction *t)
 {
        struct subaction *sa;
@@ -213,11 +214,11 @@ struct protocol_decoder {
        int (*decode)(struct link_transaction *t);
 };
 
-static struct protocol_decoder protocol_decoders[] = {
+static const struct protocol_decoder protocol_decoders[] = {
        { "FCP", decode_fcp }
 };
 
-void
+static void
 handle_transaction(struct link_transaction *t)
 {
        struct subaction *sa;
@@ -254,7 +255,7 @@ handle_transaction(struct link_transaction *t)
        link_transaction_destroy(t);
 }
 
-void
+static void
 clear_pending_transaction_list(void)
 {
        struct link_transaction *t;
@@ -313,7 +314,7 @@ struct packet_info {
        const char *name;
        int type;
        int response_tcode;
-       struct packet_field *fields;
+       const struct packet_field *fields;
        int field_count;
 };
 
@@ -344,20 +345,20 @@ struct packet_field {
        { "src", 32, 16 },                                              \
        { "rcode", 48, 4, PACKET_FIELD_TRANSACTION, rcode_names }
 
-struct packet_field read_quadlet_request_fields[] = {
+static const struct packet_field read_quadlet_request_fields[] = {
        COMMON_REQUEST_FIELDS,
        { "crc", 96, 32, PACKET_FIELD_DETAIL },
        { "ack", 156, 4, 0, ack_names },
 };
 
-struct packet_field read_quadlet_response_fields[] = {
+static const struct packet_field read_quadlet_response_fields[] = {
        COMMON_RESPONSE_FIELDS,
        { "data", 96, 32, PACKET_FIELD_TRANSACTION },
        { "crc", 128, 32, PACKET_FIELD_DETAIL },
        { "ack", 188, 4, 0, ack_names },
 };
 
-struct packet_field read_block_request_fields[] = {
+static const struct packet_field read_block_request_fields[] = {
        COMMON_REQUEST_FIELDS,
        { "data_length", 96, 16, PACKET_FIELD_TRANSACTION },
        { "extended_tcode", 112, 16 },
@@ -365,7 +366,7 @@ struct packet_field read_block_request_fields[] = {
        { "ack", 188, 4, 0, ack_names },
 };
 
-struct packet_field block_response_fields[] = {
+static const struct packet_field block_response_fields[] = {
        COMMON_RESPONSE_FIELDS,
        { "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH },
        { "extended_tcode", 112, 16 },
@@ -375,13 +376,13 @@ struct packet_field block_response_fields[] = {
        { "ack", -4, 4, 0, ack_names },
 };
 
-struct packet_field write_quadlet_request_fields[] = {
+static const struct packet_field write_quadlet_request_fields[] = {
        COMMON_REQUEST_FIELDS,
        { "data", 96, 32, PACKET_FIELD_TRANSACTION },
        { "ack", -4, 4, 0, ack_names },
 };
 
-struct packet_field block_request_fields[] = {
+static const struct packet_field block_request_fields[] = {
        COMMON_REQUEST_FIELDS,
        { "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH | PACKET_FIELD_TRANSACTION },
        { "extended_tcode", 112, 16, PACKET_FIELD_TRANSACTION },
@@ -391,13 +392,13 @@ struct packet_field block_request_fields[] = {
        { "ack", -4, 4, 0, ack_names },
 };
 
-struct packet_field write_response_fields[] = {
+static const struct packet_field write_response_fields[] = {
        COMMON_RESPONSE_FIELDS,
        { "reserved", 64, 32, PACKET_FIELD_DETAIL },
        { "ack", -4, 4, 0, ack_names },
 };
 
-struct packet_field iso_data_fields[] = {
+static const struct packet_field iso_data_fields[] = {
        { "data_length", 0, 16, PACKET_FIELD_DATA_LENGTH },
        { "tag", 16, 2 },
        { "channel", 18, 6 },
@@ -409,7 +410,7 @@ struct packet_field iso_data_fields[] = {
        { "ack", -4, 4, 0, ack_names },
 };
 
-static struct packet_info packet_info[] = {
+static const struct packet_info packet_info[] = {
        {
                .name           = "write_quadlet_request",
                .type           = PACKET_REQUEST,
@@ -486,139 +487,158 @@ static struct packet_info packet_info[] = {
        },
 };
 
-int
-handle_packet(uint32_t *data, size_t length)
+static int
+handle_request_packet(uint32_t *data, size_t length)
 {
-       if (length == 0) {
-               printf("bus reset\r\n");
-               clear_pending_transaction_list();
-       } else if (length > sizeof(struct phy_packet)) {
-               struct link_packet *p = (struct link_packet *) data;
-               struct subaction *sa, *prev;
-               struct link_transaction *t;
+       struct link_packet *p = (struct link_packet *) data;
+       struct subaction *sa, *prev;
+       struct link_transaction *t;
 
-               switch (packet_info[p->common.tcode].type) {
-               case PACKET_REQUEST:
-                       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).
-                                        */
-                               }
+       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. */
-                               }
-                       }
+               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:
-                               if (p->common.tcode != TCODE_WRITE_QUADLET &&
-                                   p->common.tcode != TCODE_WRITE_BLOCK)
-                                       /* 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;
-                       }
-                       break;
+       list_append(&t->request_list, &sa->link);
 
-               case PACKET_RESPONSE:
-                       t = link_transaction_lookup(p->common.destination, p->common.source,
-                                       p->common.tlabel);
-                       if (list_empty(&t->request_list)) {
-                               /* unsolicited response */
-                       }
+       switch (sa->ack) {
+       case ACK_COMPLETE:
+               if (p->common.tcode != TCODE_WRITE_QUADLET_REQUEST &&
+                   p->common.tcode != TCODE_WRITE_BLOCK_REQUEST)
+                       /* error, unified transactions only allowed for write */;
+               list_remove(&t->link);
+               handle_transaction(t);
+               break;
 
-                       sa = subaction_create(data, length);
-                       t->response = sa;
+       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;
+       }
 
-                       if (!list_empty(&t->response_list)) {
-                               prev = list_tail(&t->response_list, struct subaction, link);
+       return 1;
+}
 
-                               if (!ACK_BUSY(prev->ack)) {
-                                       /*
-                                        * error, we should only see ack_busy_* before the
-                                        * ack_pending/ack_complete
-                                        */
-                               }
+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;
 
-                               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.
-                                        */
-                               }
+       t = link_transaction_lookup(p->common.destination, p->common.source,
+                       p->common.tlabel);
+       if (list_empty(&t->request_list)) {
+               /* unsolicited response */
+       }
 
-                               if (packet_info[prev->packet.common.tcode].response_tcode !=
-                                   sa->packet.common.tcode) {
-                                       /* error, tcode mismatch */
-                               }
-                       }
+       sa = subaction_create(data, length);
+       t->response = sa;
 
-                       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;
-                       }
+       if (!list_empty(&t->response_list)) {
+               prev = list_tail(&t->response_list, struct subaction, link);
 
-                       break;
+               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
+handle_packet(uint32_t *data, size_t length)
+{
+       if (length == 0) {
+               printf("bus reset\r\n");
+               clear_pending_transaction_list();
+       } else if (length > sizeof(struct phy_packet)) {
+               struct link_packet *p = (struct link_packet *) data;
+
+               switch (packet_info[p->common.tcode].type) {
+               case PACKET_REQUEST:
+                       return handle_request_packet(data, length);
+
+               case PACKET_RESPONSE:
+                       return handle_response_packet(data, length);
 
                case PACKET_OTHER:
                case PACKET_RESERVED:
@@ -629,7 +649,8 @@ handle_packet(uint32_t *data, size_t length)
        return 1;
 }
 
-unsigned int get_bits(struct link_packet *packet, int offset, int width)
+static unsigned int
+get_bits(struct link_packet *packet, int offset, int width)
 {
        uint32_t *data = (uint32_t *) packet;
        uint32_t index, shift, mask;
@@ -649,7 +670,8 @@ unsigned int get_bits(struct link_packet *packet, int offset, int width)
 #error unsupported byte order.
 #endif
 
-void dump_data(unsigned char *data, int length)
+static void
+dump_data(unsigned char *data, int length)
 {
        int i, print_length;
 
@@ -671,14 +693,14 @@ static void
 decode_link_packet(struct link_packet *packet, size_t length,
                   int include_flags, int exclude_flags)
 {
-       struct packet_info *pi;
+       const struct packet_info *pi;
        int data_length = 0;
        int i;
 
        pi = &packet_info[packet->common.tcode];
 
        for (i = 0; i < pi->field_count; i++) {
-               struct packet_field *f = &pi->fields[i];
+               const struct packet_field *f = &pi->fields[i];
                int offset;
 
                if (f->flags & exclude_flags)
@@ -848,15 +870,15 @@ print_stats(uint32_t *data, size_t length)
        printf(SHOW_CURSOR "\n");
 }
 
-struct termios saved_attributes;
+static struct termios saved_attributes;
 
-void
+static void
 reset_input_mode(void)
 {
        tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
 }
 
-void
+static void
 set_input_mode(void)
 {
        struct termios tattr;
@@ -881,11 +903,12 @@ set_input_mode(void)
 
 int main(int argc, const char *argv[])
 {
+       uint32_t buf[128 * 1024];
+       uint32_t filter;
+       int length, retval, view;
        int fd = -1;
        FILE *output = NULL, *input = NULL;
        poptContext con;
-       int retval;
-       int view;
        char c;
        struct pollfd pollfds[2];
 
@@ -939,70 +962,62 @@ int main(int argc, const char *argv[])
 
        setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
 
-       if (1) {
-               uint32_t buf[128 * 1024];
-               uint32_t filter;
-               int length;
-
-               filter = ~0;
-               if (!option_iso)
-                       filter &= ~(1 << TCODE_ISO_DATA);
-               if (!option_cycle_start)
-                       filter &= ~(1 << TCODE_CYCLE_START);
-               if (view == VIEW_STATS)
-                       filter = ~(1 << TCODE_CYCLE_START);
+       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);
 
-               ioctl(fd, NOSY_IOC_FILTER, filter);
+       ioctl(fd, NOSY_IOC_FILTER, filter);
 
-               ioctl(fd, NOSY_IOC_START);
+       ioctl(fd, NOSY_IOC_START);
 
-               pollfds[0].fd = fd;
-               pollfds[0].events = POLLIN;
-               pollfds[1].fd = STDIN_FILENO;
-               pollfds[1].events = POLLIN;
+       pollfds[0].fd = fd;
+       pollfds[0].events = POLLIN;
+       pollfds[1].fd = STDIN_FILENO;
+       pollfds[1].events = POLLIN;
 
-               while (run) {
-                       if (input != NULL) {
-                               if (fread(&length, sizeof length, 1, input) != 1)
+       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);
                                        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);
-                                               return 0;
-                                       }
                                }
-
-                               if (pollfds[0].revents)
-                                       length = read(fd, buf, sizeof buf);
-                               else
-                                       continue;
                        }
 
-                       if (output != NULL) {
-                               fwrite(&length, sizeof length, 1, output);
-                               fwrite(buf, 1, length, output);
-                       }
+                       if (pollfds[0].revents)
+                               length = read(fd, buf, sizeof buf);
+                       else
+                               continue;
+               }
 
-                       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;
-                       }
+               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;
                }
-       } else {
-               poptPrintUsage(con, stdout, 0);
        }
 
        if (output != NULL)