-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrf2500.c
228 lines (186 loc) · 5.35 KB
/
rf2500.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include "rf2500.h"
#include "util.h"
#include "usbutil.h"
#include "output.h"
struct rf2500_transport {
struct transport base;
int int_number;
struct usb_dev_handle *handle;
uint8_t buf[64];
int len;
int offset;
};
/*********************************************************************
* USB transport
*
* These functions handle the details of slicing data over USB
* transfers. The interface presented is a continuous byte stream with
* no slicing codes.
*
* Writes are unbuffered -- a single write translates to at least
* one transfer.
*/
#define USB_FET_VENDOR 0x0451
#define USB_FET_PRODUCT 0xf432
#define USB_FET_INTERFACE_CLASS 3
#define USB_FET_IN_EP 0x81
#define USB_FET_OUT_EP 0x01
static int open_interface(struct rf2500_transport *tr,
struct usb_device *dev, int ino)
{
printc("Trying to open interface %d on %s\n", ino, dev->filename);
tr->int_number = ino;
tr->handle = usb_open(dev);
if (!tr->handle) {
pr_error("rf2500: can't open device");
return -1;
}
#if defined(__linux__)
if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0)
pr_error("rf2500: warning: can't "
"detach kernel driver");
#endif
if (usb_claim_interface(tr->handle, tr->int_number) < 0) {
pr_error("rf2500: can't claim interface");
usb_close(tr->handle);
return -1;
}
return 0;
}
static int open_device(struct rf2500_transport *tr,
struct usb_device *dev)
{
struct usb_config_descriptor *c = &dev->config[0];
int i;
for (i = 0; i < c->bNumInterfaces; i++) {
struct usb_interface *intf = &c->interface[i];
struct usb_interface_descriptor *desc = &intf->altsetting[0];
if (desc->bInterfaceClass == USB_FET_INTERFACE_CLASS &&
!open_interface(tr, dev, desc->bInterfaceNumber))
return 0;
}
return -1;
}
static int usbtr_send(transport_t tr_base, const uint8_t *data, int len)
{
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
while (len) {
uint8_t pbuf[256];
int plen = len > 255 ? 255 : len;
int txlen = plen + 1;
memcpy(pbuf + 1, data, plen);
/* This padding is needed to work around an apparent bug in
* the RF2500 FET. Without this, the device hangs.
*/
if (txlen > 32 && (txlen & 0x3f))
while (txlen < 255 && (txlen & 0x3f))
pbuf[txlen++] = 0xff;
else if (txlen > 16 && (txlen & 0xf))
while (txlen < 255 && (txlen & 0xf) != 1)
pbuf[txlen++] = 0xff;
pbuf[0] = txlen - 1;
#ifdef DEBUG_USBTR
debug_hexdump("USB transfer out", pbuf, txlen);
#endif
if (usb_bulk_write(tr->handle, USB_FET_OUT_EP,
(char *)pbuf, txlen, 10000) < 0) {
pr_error("rf2500: can't send data");
return -1;
}
data += plen;
len -= plen;
}
return 0;
}
static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
{
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
int rlen;
if (tr->offset >= tr->len) {
if (usb_bulk_read(tr->handle, USB_FET_IN_EP,
(char *)tr->buf, sizeof(tr->buf),
10000) < 0) {
pr_error("rf2500: can't receive data");
return -1;
}
#ifdef DEBUG_USBTR
debug_hexdump("USB transfer in", tr->buf, 64);
#endif
tr->len = tr->buf[1] + 2;
if (tr->len > sizeof(tr->buf))
tr->len = sizeof(tr->buf);
tr->offset = 2;
}
rlen = tr->len - tr->offset;
if (rlen > max_len)
rlen = max_len;
memcpy(databuf, tr->buf + tr->offset, rlen);
tr->offset += rlen;
return rlen;
}
static void usbtr_destroy(transport_t tr_base)
{
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
usb_release_interface(tr->handle, tr->int_number);
usb_close(tr->handle);
free(tr);
}
transport_t rf2500_open(const char *devpath)
{
struct rf2500_transport *tr = malloc(sizeof(*tr));
struct usb_device *dev;
char buf[64];
if (!tr) {
pr_error("rf2500: can't allocate memory");
return NULL;
}
tr->base.destroy = usbtr_destroy;
tr->base.send = usbtr_send;
tr->base.recv = usbtr_recv;
usb_init();
usb_find_busses();
usb_find_devices();
if (devpath)
dev = usbutil_find_by_loc(devpath);
else
dev = usbutil_find_by_id(USB_FET_VENDOR, USB_FET_PRODUCT);
if (!dev) {
free(tr);
return NULL;
}
if (open_device(tr, dev) < 0) {
printc_err("rf2500: failed to open RF2500 device\n");
return NULL;
}
/* Flush out lingering data.
*
* The timeout apparently doesn't work on OS/X, and this loop
* just hangs once the endpoint buffer empties.
*/
#ifndef __APPLE__
while (usb_bulk_read(tr->handle, USB_FET_IN_EP,
buf, sizeof(buf),
100) >= 0);
#endif
return (transport_t)tr;
}