carl9170 firmware: import 1.7.0
[carl9170fw.git] / tools / carlu / src / rx.c
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * RX data processing
5  *
6  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include "libusb.h"
34
35 #include "carlu.h"
36 #include "debug.h"
37 #include "frame.h"
38 #include "ieee80211.h"
39 #include "wlan.h"
40
41 static void carlu_handle_data(struct carlu *ar, void *buf,
42                                unsigned int len)
43 {
44         if (ar->rx_cb) {
45                 ar->rx_cb(ar, buf, len);
46         } else {
47                 dbg("unhandled data:\n");
48                 print_hex_dump_bytes(VERBOSE, "DATA:", buf, len);
49         }
50 }
51
52 void carlu_handle_command(struct carlu *ar, void *buf,
53                           unsigned int len)
54 {
55         struct carl9170_rsp *cmd;
56         int ret = 0;
57
58         cmd = (void *) buf;
59
60         if ((cmd->hdr.cmd & 0xc0) != 0xc0) {
61
62                 SDL_mutexP(ar->resp_lock);
63                 if (ar->resp_buf && ar->resp_len && ar->resp_len >= (len - 4)) {
64                         memcpy(ar->resp_buf, buf + 4, len - 4);
65                         ar->resp_buf = NULL;
66                 } else {
67                         warn("spurious command response (%d / %d)\n",
68                              (int) len - 4, (int) ar->resp_len);
69                         print_hex_dump_bytes(WARNING, "RSP:", buf, len);
70                 }
71                 SDL_mutexV(ar->resp_lock);
72
73                 SDL_CondSignal(ar->resp_pend);
74                 return;
75         }
76
77         if (ar->cmd_cb)
78                 ret = ar->cmd_cb(ar, cmd, buf, len);
79
80         if (ret) {
81                 switch (cmd->hdr.cmd) {
82                 case CARL9170_RSP_TXCOMP:
83                         carlu_tx_feedback(ar, cmd);
84                         break;
85
86                 case CARL9170_RSP_TEXT:
87                         info("carl9170 FW: %.*s\n", (int)len - 4, (char *)buf + 4);
88                         break;
89
90                 case CARL9170_RSP_HEXDUMP:
91                         info("carl9170 FW: hexdump\n");
92                         print_hex_dump_bytes(INFO, "HEX:", (char *)buf + 4, len - 4);
93                         break;
94
95                 case CARL9170_RSP_WATCHDOG:
96                         err("Woof Woof! Watchdog notification.\n");
97                         break;
98
99                 case CARL9170_RSP_GPIO:
100                         info("GPIO Interrupt => GPIO state %.8x\n",
101                             le32_to_cpu(cmd->gpio.gpio));
102                         break;
103
104                 case CARL9170_RSP_RADAR:
105                         info("RADAR Interrupt");
106                         break;
107
108                 default:
109                         warn("received unhandled event 0x%x\n", cmd->hdr.cmd);
110                         print_hex_dump_bytes(WARNING, "RSP:", (char *)buf + 4, len - 4);
111                         break;
112                 }
113         }
114 }
115
116 static void __carlu_rx(struct carlu *ar, uint8_t *buf, unsigned int len)
117 {
118         unsigned int i;
119
120         i = 0;
121
122         /* weird thing, but this is the same in the original driver */
123         while (len > 2 && i < 12 && buf[0] == 0xff && buf[1] == 0xff) {
124                 i += 2;
125                 len -= 2;
126                 buf += 2;
127         }
128
129         if (i == 12) {
130                 struct carl9170_rsp *cmd;
131                 i = 0;
132
133                 while (i < len) {
134                         cmd = (void *) &buf[i];
135
136                         carlu_handle_command(ar, cmd, cmd->hdr.len + 4);
137                         i += cmd->hdr.len + 4;
138                 }
139         } else {
140                 carlu_handle_data(ar, buf, len);
141         }
142 }
143
144 static void carlu_rx_stream(struct carlu *ar, struct frame *frame)
145 {
146         void *buf = frame->data;
147         unsigned int len = frame->len;
148
149         while (len >= 4) {
150                 struct ar9170_stream *rx_stream;
151                 unsigned int resplen, elen;
152
153                 rx_stream = (void *) buf;
154                 resplen = le16_to_cpu(rx_stream->length);
155                 elen = roundup(resplen + 4, 4);
156
157                 if (rx_stream->tag != cpu_to_le16(0x4e00)) {
158                         warn("frame has no tag %p %u %x.\n",
159                               buf, (int) len, rx_stream->tag);
160                         print_hex_dump_bytes(WARNING, "FRAME:", frame->data, frame->len);
161
162                         __carlu_rx(ar, buf, len);
163                         return;
164                 }
165
166                 __carlu_rx(ar, rx_stream->payload, resplen);
167
168                 len -= elen;
169                 buf += elen;
170         }
171 }
172
173 void carlu_rx(struct carlu *ar, struct frame *frame)
174 {
175         if (ar->rx_stream)
176                 carlu_rx_stream(ar, frame);
177         else
178                 __carlu_rx(ar, frame->data, frame->len);
179 }