summaryrefslogtreecommitdiffstats
path: root/carl9170fw/carlfw/src/cam.c
blob: 7c0e61c5dac0cba347cc6b04ee97b71c41f29516 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * carl9170 firmware - used by the ar9170 wireless device
 *
 * Security Engine
 *
 * Copyright (c) 2000-2005 ZyDAS Technology Corporation
 * Copyright (c) 2007-2009 Atheros Communications, Inc.
 * Copyright	2009	Johannes Berg <johannes@sipsolutions.net>
 * 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.
 */

#include "carl9170.h"
#include "cam.h"

#ifdef CONFIG_CARL9170FW_SECURITY_ENGINE
static void disable_cam_user(const uint16_t userId)
{
	if (userId <= 31)
		andl(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, (~((uint32_t) 1 << userId)));
	else if (userId <= 63)
		andl(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, (~((uint32_t) 1 << (userId - 32))));
}

static void enable_cam_user(const uint16_t userId)
{
	if (userId <= 31)
		orl(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, (((uint32_t) 1) << userId));
	else if (userId <= 63)
		orl(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, (((uint32_t) 1) << (userId - 32)));
}

static void wait_for_cam_read_ready(void)
{
	while ((get(AR9170_MAC_REG_CAM_STATE) & AR9170_MAC_CAM_STATE_READ_PENDING) == 0) {
		/*
		 * wait
		 */
	}
}

static void wait_for_cam_write_ready(void)
{
	while ((get(AR9170_MAC_REG_CAM_STATE) & AR9170_MAC_CAM_STATE_WRITE_PENDING) == 0) {
		/*
		 * wait some more
		 */
	}
}

static void HW_CAM_Avail(void)
{
	uint32_t tmpValue;

	do {
		tmpValue = get(AR9170_MAC_REG_CAM_MODE);
	} while (tmpValue & AR9170_MAC_CAM_HOST_PENDING);
}

static void HW_CAM_Write128(const uint32_t address, const uint32_t *data)
{
	HW_CAM_Avail();

	set(AR9170_MAC_REG_CAM_DATA0, data[0]);
	set(AR9170_MAC_REG_CAM_DATA1, data[1]);
	set(AR9170_MAC_REG_CAM_DATA2, data[2]);
	set(AR9170_MAC_REG_CAM_DATA3, data[3]);

	set(AR9170_MAC_REG_CAM_ADDR, address | AR9170_MAC_CAM_ADDR_WRITE);

	wait_for_cam_write_ready();
}

static void HW_CAM_Read128(const uint32_t address, uint32_t *data)
{

	HW_CAM_Avail();
	set(AR9170_MAC_REG_CAM_ADDR, address);

	wait_for_cam_read_ready();
	HW_CAM_Avail();
	data[0] = get(AR9170_MAC_REG_CAM_DATA0);
	data[1] = get(AR9170_MAC_REG_CAM_DATA1);
	data[2] = get(AR9170_MAC_REG_CAM_DATA2);
	data[3] = get(AR9170_MAC_REG_CAM_DATA3);
}

void set_key(const struct carl9170_set_key_cmd *key)
{
	uint32_t data[4];
	uint16_t row, wordId, nibbleId, i;

	if (key->user > (AR9170_CAM_MAX_USER + 3))
		return ;

	if (key->keyId > 1)
		return ;

	/* Disable Key */
	disable_cam_user(key->user);

	/* Set encrypt type */
	if (key->user >= AR9170_CAM_MAX_USER) {
		/* default */
		row = DEFAULT_ENCRY_TYPE;
		wordId = 0;
		nibbleId = (key->user - AR9170_CAM_MAX_USER) & 0x7;
	} else {
		row = ENCRY_TYPE_START_ADDR + (key->user >> 5);
		wordId = (key->user >> 3) & 0x3;
		nibbleId = key->user & 0x7;
	}

	HW_CAM_Read128(row, data);
	data[wordId] &= (~(0xf << ((uint32_t) nibbleId * 4)));
	data[wordId] |= (key->type << ((uint32_t) nibbleId * 4));
	HW_CAM_Write128(row, data);

	/* Set MAC address */
	if (key->user < AR9170_CAM_MAX_USER) {
		uint16_t byteId;
		wordId = (key->user >> 2) & 0x3;
		byteId = key->user & 0x3;
		row = (key->user >> 4) * 6;

		for (i = 0; i < 6; i++) {
			HW_CAM_Read128(row + i, data);
			data[wordId] &= (~(0xff << ((uint32_t) byteId * 8)));
			data[wordId] |= (key->macAddr[i] << ((uint32_t) byteId * 8));
			HW_CAM_Write128(row + i, data);
		}
	}

	/* Set key */
	row = KEY_START_ADDR + (key->user * 2) + key->keyId;

	HW_CAM_Write128(row, key->key);

	/* Enable Key */
	enable_cam_user(key->user);
}

void disable_key(const struct carl9170_disable_key_cmd *key)
{
	disable_cam_user(key->user);
}

#endif /* CONFIG_CARL9170FW_SECURITY_ENGINE */