summaryrefslogtreecommitdiffstats
path: root/carl9170fw/tools/carlu/src/fw.c
blob: fa4b38819d3f6004befea31182c2353b03c08686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
/*
 * carlu - userspace testing utility for ar9170 devices
 *
 * Firmware parsers
 *
 * Copyright 2009-2011 Christian Lamparter <chunkeey@googlemail.com>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "libusb.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "carlu.h"
#include "usb.h"
#include "debug.h"

int carlu_fw_check(struct carlu *ar)
{
	struct carl9170fw_otus_desc *otus_desc;

	otus_desc = carlfw_find_desc(ar->fw, (uint8_t *) OTUS_MAGIC,
				     sizeof(*otus_desc),
				     CARL9170FW_OTUS_DESC_CUR_VER);

	if (!otus_desc) {
		err("No valid OTUS descriptor found.\n");
		return -EINVAL;
	}

	if (!carl9170fw_supports(otus_desc->feature_set, CARL9170FW_DUMMY_FEATURE)) {
		err("Invalid Firmware Descriptor.\n");
		return -EIO;
	}

	if (carl9170fw_supports(otus_desc->feature_set, CARL9170FW_UNUSABLE))
		dbg("Firmware is marked as unuseable.\n");

	info("Firmware Version: %d.\n", otus_desc->api_ver);

	return 0;
}

int carlusb_fw_check(struct carlu *ar)
{
	struct carl9170fw_otus_desc *otus_desc;

	otus_desc = carlfw_find_desc(ar->fw, (uint8_t *) OTUS_MAGIC,
				     sizeof(*otus_desc),
				     CARL9170FW_OTUS_DESC_CUR_VER);

	if (!otus_desc) {
		err("No valid USB descriptor found.\n");
		return -ENODATA;
	}

	if (!carl9170fw_supports(otus_desc->feature_set, CARL9170FW_DUMMY_FEATURE)) {
		err("Invalid Firmware Descriptor.\n");
		return -EINVAL;
	}

	if (!carl9170fw_supports(otus_desc->feature_set, CARL9170FW_USB_INIT_FIRMWARE)) {
		err("Firmware does not know how to initialize USB core.\n");
		return -EOPNOTSUPP;
	}

	if (carl9170fw_supports(otus_desc->feature_set, CARL9170FW_USB_DOWN_STREAM)) {
		dbg("Enabled tx stream mode.\n");
		ar->tx_stream = true;
		ar->extra_headroom = sizeof(struct ar9170_stream);
	}

	if (carl9170fw_supports(otus_desc->feature_set, CARL9170FW_USB_UP_STREAM)) {
		dbg("Enabled rx stream mode.\n");
		ar->rx_stream = true;
	}

	if (carl9170fw_supports(otus_desc->feature_set, CARL9170FW_USB_RESP_EP2))
		dbg("Firmware sends traps over EP2.\n");

	ar->dma_chunk_size = le16_to_cpu(otus_desc->tx_frag_len);
	ar->dma_chunks = otus_desc->tx_descs;
	ar->rx_max = le16_to_cpu(otus_desc->rx_max_frame_len);

	if (carl9170fw_supports(otus_desc->feature_set, CARL9170FW_MINIBOOT))
		ar->miniboot_size = le16_to_cpu(otus_desc->miniboot_size);

	return 0;
}

void carlu_fw_info(struct carlu *ar)
{
	struct carl9170fw_motd_desc *motd_desc;
	unsigned int fw_date;

	motd_desc = carlfw_find_desc(ar->fw, (uint8_t *) MOTD_MAGIC,
				     sizeof(*motd_desc),
				     CARL9170FW_MOTD_DESC_CUR_VER);

	if (motd_desc) {
		fw_date = le32_to_cpu(motd_desc->fw_year_month_day);

		info("Firmware Date: 2%.3d-%.2d-%.2d\n",
		     CARL9170FW_GET_YEAR(fw_date), CARL9170FW_GET_MONTH(fw_date),
		     CARL9170FW_GET_DAY(fw_date));
	}
}