Project

General

Profile

Download (4.52 KB) Statistics
| Branch: | Tag: | Revision:
1 dd0638d2 (no author)
/* Generic Philips CL RC632 Routines
2 32985a29 laforge
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
3 dd0638d2 (no author)
 *
4
 *  This program is free software; you can redistribute it and/or modify
5 32985a29 laforge
 *  it under the terms of the GNU General Public License as published by 
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8 dd0638d2 (no author)
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program; if not, write to the Free Software
16
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 32985a29 laforge
 *
18 caf50003 (no author)
 */
19
20 9e6368f1 laforge
#ifdef DEBUG
21
#undef DEBUG
22
#endif
23 32985a29 laforge
24 5ab655eb (no author)
#include <sys/types.h>
25
#include <string.h>
26
#include <errno.h>
27
#include <cl_rc632.h>
28
#include "rc632.h"
29 520784c7 (no author)
#include <os/dbgu.h>
30 9d0d7022 (no author)
#include <librfid/rfid_layer2_iso14443a.h>
31
#include <librfid/rfid_protocol_mifare_classic.h>
32 5ab655eb (no author)
33
/* initially we use the same values as cm5121 */
34
#define OPENPCD_CW_CONDUCTANCE		0x3f
35
#define OPENPCD_MOD_CONDUCTANCE		0x3f
36
#define OPENPCD_14443A_BITPHASE		0xa9
37
#define OPENPCD_14443A_THRESHOLD	0xff
38
#define OPENPCD_14443B_BITPHASE		0xad
39
#define OPENPCD_14443B_THRESHOLD	0xff
40
41 dd0638d2 (no author)
#define RC632_TMO_AUTH1	14000
42
43 1bf580a0 (no author)
#define RC632_TIMEOUT_FUZZ_FACTOR	10
44
45
#define USE_IRQ
46
47 dd0638d2 (no author)
#define ENTER()		DEBUGPCRF("entering")
48
struct rfid_asic rc632;
49
50
static int 
51
rc632_set_bit_mask(struct rfid_asic_handle *handle, 
52 373c172a Harald Welte
		   uint8_t reg, uint8_t mask, uint8_t val)
53 dd0638d2 (no author)
{
54
	int ret;
55 373c172a Harald Welte
	uint8_t tmp;
56 dd0638d2 (no author)
57 28eb4a57 laforge
	ret = opcd_rc632_reg_read(handle, reg, &tmp);
58 dd0638d2 (no author)
	if (ret < 0)
59
		return ret;
60
61
	/* if bits are already like we want them, abort */
62
	if ((tmp & mask) == val)
63
		return 0;
64
65 28eb4a57 laforge
	return opcd_rc632_reg_write(handle, reg, (tmp & ~mask)|(val & mask));
66 dd0638d2 (no author)
}
67
68 952042d2 (no author)
int 
69 dd0638d2 (no author)
rc632_turn_on_rf(struct rfid_asic_handle *handle)
70
{
71
	ENTER();
72 28eb4a57 laforge
	return opcd_rc632_set_bits(handle, RC632_REG_TX_CONTROL, 0x03);
73 dd0638d2 (no author)
}
74
75 952042d2 (no author)
int 
76 dd0638d2 (no author)
rc632_turn_off_rf(struct rfid_asic_handle *handle)
77
{
78
	ENTER();
79 28eb4a57 laforge
	return opcd_rc632_clear_bits(handle, RC632_REG_TX_CONTROL, 0x03);
80 dd0638d2 (no author)
}
81
82
static int
83
rc632_power_up(struct rfid_asic_handle *handle)
84
{
85
	ENTER();
86 28eb4a57 laforge
	return opcd_rc632_clear_bits(handle, RC632_REG_CONTROL, 
87 dd0638d2 (no author)
				RC632_CONTROL_POWERDOWN);
88
}
89
90
static int
91
rc632_power_down(struct rfid_asic_handle *handle)
92
{
93 28eb4a57 laforge
	return opcd_rc632_set_bits(handle, RC632_REG_CONTROL,
94 dd0638d2 (no author)
			      RC632_CONTROL_POWERDOWN);
95 f44e839d laforge
}
96
97
#define MAX_WRITE_LEN	16	/* see Sec. 18.6.1.2 of RC632 Spec Rev. 3.2. */
98
99
int
100
rc632_write_eeprom(struct rfid_asic_handle *handle, 
101 373c172a Harald Welte
		   uint16_t addr, uint8_t len, uint8_t *data)
102 f44e839d laforge
{
103 373c172a Harald Welte
	uint8_t sndbuf[MAX_WRITE_LEN + 2];
104
	uint8_t reg;
105 f44e839d laforge
	int ret;
106
107
	if (len > MAX_WRITE_LEN)
108
		return -EINVAL;
109
	if (addr < 0x10)
110
		return -EPERM;
111
	if (addr > 0x1ff)
112
		return -EINVAL;
113
114
	sndbuf[0] = addr & 0x00ff;	/* LSB */
115
	sndbuf[1] = addr >> 8;		/* MSB */
116
	memcpy(&sndbuf[2], data, len);
117
118
	ret = opcd_rc632_fifo_write(handle, len + 2, sndbuf, 0x03);
119
	if (ret < 0)
120
		return ret;
121
122
	ret = opcd_rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_WRITE_E2);
123
	if (ret < 0)
124
		return ret;
125
	
126
	ret = opcd_rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &reg);
127
	if (ret < 0)
128
		return ret;
129
130
	if (reg & RC632_ERR_FLAG_ACCESS_ERR)
131
		return -EPERM;
132
133
	while (1) {
134
		ret = opcd_rc632_reg_read(handle, RC632_REG_SECONDARY_STATUS, &reg);
135
		if (ret < 0)
136
			return ret;
137
138
		if (reg & RC632_SEC_ST_E2_READY) {
139
			/* the E2Write command must be terminated, See sec. 18.6.1.3 */
140
			ret = opcd_rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_IDLE);
141
			break;
142
		}
143
	}
144
	
145
	return ret;
146 dd0638d2 (no author)
}
147
148 83f2dd8a (no author)
int
149 373c172a Harald Welte
rc632_read_eeprom(struct rfid_asic_handle *handle, uint16_t addr, uint8_t len,
150
		  uint8_t *recvbuf)
151 dd0638d2 (no author)
{
152 373c172a Harald Welte
	uint8_t sndbuf[3];
153
	uint8_t err;
154 dd0638d2 (no author)
	int ret;
155
156 83f2dd8a (no author)
	sndbuf[0] = (addr & 0xff);
157
	sndbuf[1] = addr >> 8;
158
	sndbuf[2] = len;
159 dd0638d2 (no author)
160 28eb4a57 laforge
	ret = opcd_rc632_fifo_write(handle, 3, sndbuf, 0x03);
161 dd0638d2 (no author)
	if (ret < 0)
162
		return ret;
163
164 28eb4a57 laforge
	ret = opcd_rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
165 dd0638d2 (no author)
	if (ret < 0)
166
		return ret;
167
168 83f2dd8a (no author)
	/* usleep(20000); */
169 dd0638d2 (no author)
170 28eb4a57 laforge
	ret = opcd_rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &err);
171 83f2dd8a (no author)
	if (err & RC632_ERR_FLAG_ACCESS_ERR)
172
		return -EPERM;
173
	
174 28eb4a57 laforge
	ret = opcd_rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &err);
175 83f2dd8a (no author)
	if (err < len)
176
		len = err;
177
178 28eb4a57 laforge
	ret = opcd_rc632_fifo_read(handle, len, recvbuf);
179 dd0638d2 (no author)
	if (ret < 0)
180
		return ret;
181
182 83f2dd8a (no author)
	return len;
183 dd0638d2 (no author)
}
184
185 9e6368f1 laforge
#define RC632_E2_PRODUCT_TYPE	0
186
#define RC632_E2_PRODUCT_SERIAL	8
187
#define RC632_E2_RS_MAX_P	14
188
189
int rc632_get_serial(struct rfid_asic_handle *handle,
190 373c172a Harald Welte
		     uint32_t *serial)
191 9e6368f1 laforge
{
192
	return rc632_read_eeprom(handle, RC632_E2_PRODUCT_SERIAL, 
193 373c172a Harald Welte
				 4, (uint8_t *)serial);
194 9e6368f1 laforge
}
Add picture from clipboard (Maximum size: 48.8 MB)