carl9170 firmware: add cmake helpers
[carl9170fw.git] / tools / carlu / src / rx.c
1 /*
2  * carlu - userspace testing utility for ar9170 devices
3  *
4  * RX data processing
5  *
6  * Copyright 2009-2011 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 & CARL9170_RSP_FLAG) != CARL9170_RSP_FLAG) {
61                 if ((cmd->hdr.cmd & CARL9170_CMD_ASYNC_FLAG))
62                         return;
63
64                 SDL_mutexP(ar->resp_lock);
65                 if (ar->resp_buf && ar->resp_len && ar->resp_len >= (len - 4)) {
66                         memcpy(ar->resp_buf, buf + 4, len - 4);
67                         ar->resp_buf = NULL;
68                 } else {
69                         warn("spurious command response (%d / %d)\n",
70                              (int) len - 4, (int) ar->resp_len);
71                         print_hex_dump_bytes(WARNING, "RSP:", buf, len);
72                 }
73                 SDL_mutexV(ar->resp_lock);
74
75                 SDL_CondSignal(ar->resp_pend);
76                 return;
77         }
78
79         if (ar->cmd_cb)
80                 ret = ar->cmd_cb(ar, cmd, buf, len);
81
82         if (ret) {
83                 switch (cmd->hdr.cmd) {
84                 case CARL9170_RSP_TXCOMP:
85                         carlu_tx_feedback(ar, cmd);
86                         break;
87
88                 case CARL9170_RSP_TEXT:
89                         info("carl9170 FW: %.*s\n", (int)len - 4, (char *)buf + 4);
90                         break;
91
92                 case CARL9170_RSP_HEXDUMP:
93                         info("carl9170 FW: hexdump\n");
94                         print_hex_dump_bytes(INFO, "HEX:", (char *)buf + 4, len - 4);
95                         break;
96
97                 case CARL9170_RSP_WATCHDOG:
98                         err("Woof Woof! Watchdog notification.\n");
99                         break;
100
101                 case CARL9170_RSP_GPIO:
102                         info("GPIO Interrupt => GPIO state %.8x\n",
103                             le32_to_cpu(cmd->gpio.gpio));
104                         break;
105
106                 case CARL9170_RSP_RADAR:
107                         info("RADAR Interrupt");
108                         break;
109
110                 default:
111                         warn("received unhandled event 0x%x\n", cmd->hdr.cmd);
112                         print_hex_dump_bytes(WARNING, "RSP:", (char *)buf + 4, len - 4);
113                         break;
114                 }
115         }
116 }
117
118 static void __carlu_rx(struct carlu *ar, uint8_t *buf, unsigned int len)
119 {
120         unsigned int i;
121
122         i = 0;
123
124         /* weird thing, but this is the same in the original driver */
125         while (len > 2 && i < 12 && buf[0] == 0xff && buf[1] == 0xff) {
126                 i += 2;
127                 len -= 2;
128                 buf += 2;
129         }
130
131         if (i == 12) {
132                 struct carl9170_rsp *cmd;
133                 i = 0;
134
135                 while (i < len) {
136                         cmd = (void *) &buf[i];
137
138                         carlu_handle_command(ar, cmd, cmd->hdr.len + 4);
139                         i += cmd->hdr.len + 4;
140                 }
141         } else {
142                 carlu_handle_data(ar, buf, len);
143         }
144 }
145
146 static void carlu_rx_stream(struct carlu *ar, struct frame *frame)
147 {
148         void *buf = frame->data;
149         unsigned int len = frame->len;
150
151         while (len >= 4) {
152                 struct ar9170_stream *rx_stream;
153                 unsigned int resplen, elen;
154
155                 rx_stream = (void *) buf;
156                 resplen = le16_to_cpu(rx_stream->length);
157                 elen = roundup(resplen + 4, 4);
158
159                 if (rx_stream->tag != cpu_to_le16(0x4e00)) {
160                         warn("frame has no tag %p %u %x.\n",
161                               buf, (int) len, rx_stream->tag);
162                         print_hex_dump_bytes(WARNING, "FRAME:", frame->data, frame->len);
163
164                         __carlu_rx(ar, buf, len);
165                         return;
166                 }
167
168                 __carlu_rx(ar, rx_stream->payload, resplen);
169
170                 len -= elen;
171                 buf += elen;
172         }
173 }
174
175 void carlu_rx(struct carlu *ar, struct frame *frame)
176 {
177         if (ar->rx_stream)
178                 carlu_rx_stream(ar, frame);
179         else
180                 __carlu_rx(ar, frame->data, frame->len);
181 }