summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bluetooth/ath3kfw/ath3kfw.c
blob: 37191d1aac4df5f51dc0583e61cf15c22621c969 (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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * ath3kfw.c
 */

/*-
 * Copyright (c) 2010 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <libusb20_desc.h>
#include <libusb20.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#define ATH3KFW			"ath3kfw"
#define ATH3KFW_VENDOR_ID	0x0cf3
#define ATH3KFW_PRODUCT_ID	0x3000
#define ATH3KFW_FW		"/usr/local/etc/ath3k-1.fw"
#define ATH3KFW_BULK_EP		0x02
#define	ATH3KFW_REQ_DFU_DNLOAD	1
#define	ATH3KFW_MAX_BSIZE	4096

static int	parse_ugen_name		(char const *ugen, uint8_t *bus,
					 uint8_t *addr);
static int	find_device		(struct libusb20_backend *be,
					 uint8_t bus, uint8_t addr,
					 struct libusb20_device **dev);
static int	download_firmware	(struct libusb20_device *dev,
					 char const *firmware);
static void	usage			(void);

static int			vendor_id = ATH3KFW_VENDOR_ID;
static int			product_id = ATH3KFW_PRODUCT_ID;

/*
 * Firmware downloader for Atheros AR3011 based USB Bluetooth devices
 */

int
main(int argc, char **argv)
{
	uint8_t			bus, addr;
	char const		*firmware;
	struct libusb20_backend	*be;
	struct libusb20_device	*dev;
	int			n;

	openlog(ATH3KFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);

	bus = 0;
	addr = 0;
	firmware = ATH3KFW_FW;

	while ((n = getopt(argc, argv, "d:f:hp:v:")) != -1) {
		switch (n) {
		case 'd': /* ugen device name */
			if (parse_ugen_name(optarg, &bus, &addr) < 0)
				usage();
			break;

		case 'f': /* firmware file */
			firmware = optarg;
			break;
		case 'p': /* product id */
			product_id = strtol(optarg, NULL, 0);
			break;
		case 'v': /* vendor id */
			vendor_id = strtol(optarg, NULL, 0);
			break;
		case 'h':
		default:
			usage();
			break;
			/* NOT REACHED */
		}
	}

	be = libusb20_be_alloc_default();
	if (be == NULL) {
		syslog(LOG_ERR, "libusb20_be_alloc_default() failed");
		return (-1);
	}

	if (find_device(be, bus, addr, &dev) < 0) {
		syslog(LOG_ERR, "ugen%d.%d is not recognized as " \
			"Atheros AR3011 based device " \
			"(possibly caused by lack of permissions)", bus, addr);
		return (-1);
	}

	if (download_firmware(dev, firmware) < 0) {
		syslog(LOG_ERR, "could not download %s firmare to ugen%d.%d",
			firmware, bus, addr);
		return (-1);
	}

	libusb20_be_free(be);
	closelog();
	
	return (0);
}

/*
 * Parse ugen name and extract device's bus and address
 */

static int
parse_ugen_name(char const *ugen, uint8_t *bus, uint8_t *addr)
{
	char	*ep;

	if (strncmp(ugen, "ugen", 4) != 0)
		return (-1);

	*bus = (uint8_t) strtoul(ugen + 4, &ep, 10);
	if (*ep != '.')
		return (-1);

	*addr = (uint8_t) strtoul(ep + 1, &ep, 10);
	if (*ep != '\0')
		return (-1);

	return (0);
}

/*
 * Find USB device
 */

static int
find_device(struct libusb20_backend *be, uint8_t bus, uint8_t addr,
		struct libusb20_device **dev)
{
	struct LIBUSB20_DEVICE_DESC_DECODED	*desc;

	*dev = NULL;

	while ((*dev = libusb20_be_device_foreach(be, *dev)) != NULL) {
		if (libusb20_dev_get_bus_number(*dev) != bus ||
		    libusb20_dev_get_address(*dev) != addr)
			continue;

		desc = libusb20_dev_get_device_desc(*dev);
		if (desc == NULL)
			continue;

		if (desc->idVendor != vendor_id ||
		    desc->idProduct != product_id)
			continue;

		break;
	}

	return ((*dev == NULL)? -1 : 0);
}

/*
 * Download firmware
 */

static int
download_firmware(struct libusb20_device *dev, char const *firmware)
{
	struct libusb20_transfer		*bulk;
	struct LIBUSB20_CONTROL_SETUP_DECODED	req;
	int					fd, n, error;
	uint8_t					buf[ATH3KFW_MAX_BSIZE];

	error = -1;

	if (libusb20_dev_open(dev, 1) != 0) {
		syslog(LOG_ERR, "libusb20_dev_open() failed");
		return (error);
	}

	if ((bulk = libusb20_tr_get_pointer(dev, 0)) == NULL) {
		syslog(LOG_ERR, "libusb20_tr_get_pointer() failed");
		goto out;
	}

	if (libusb20_tr_open(bulk, ATH3KFW_MAX_BSIZE, 1, ATH3KFW_BULK_EP) != 0) {
		syslog(LOG_ERR, "libusb20_tr_open(%d, 1, %d) failed",
			ATH3KFW_MAX_BSIZE, ATH3KFW_BULK_EP);
		goto out;
	}

	if ((fd = open(firmware, O_RDONLY)) < 0) {
		syslog(LOG_ERR, "open(%s) failed. %s",
			firmware, strerror(errno));
		goto out1;
	}

	n = read(fd, buf, 20);
	if (n != 20) {
		syslog(LOG_ERR, "read(%s, 20) failed. %s",
			firmware, strerror(errno));
		goto out2;
	}

	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
	req.bmRequestType = LIBUSB20_REQUEST_TYPE_VENDOR;
	req.bRequest = ATH3KFW_REQ_DFU_DNLOAD;
	req.wLength = 20;

	if (libusb20_dev_request_sync(dev, &req, buf, NULL, 5000, 0) != 0) {
		syslog(LOG_ERR, "libusb20_dev_request_sync() failed");
		goto out2;
	}

	for (;;) {
		n = read(fd, buf, sizeof(buf));
		if (n < 0) {
			syslog(LOG_ERR, "read(%s, %d) failed. %s",
				firmware, (int) sizeof(buf), strerror(errno));
			goto out2;
		}
		if (n == 0)
			break;

		libusb20_tr_setup_bulk(bulk, buf, n, 3000);
		libusb20_tr_start(bulk);

		while (libusb20_dev_process(dev) == 0) {
			if (libusb20_tr_pending(bulk) == 0)
				break;

			libusb20_dev_wait_process(dev, -1);
		}

		if (libusb20_tr_get_status(bulk) != 0) {
			syslog(LOG_ERR, "bulk transfer failed with status %d",
				libusb20_tr_get_status(bulk));
			goto out2;
		}
	}

	error = 0;
out2:
	close(fd);
out1:
	libusb20_tr_close(bulk);
out:
	libusb20_dev_close(dev);

	return (error);
}

/*
 * Display usage and exit
 */

static void
usage(void)
{
	printf(
"Usage: %s -d ugenX.Y -f firmware_file\n"
"Usage: %s -h\n" \
"Where:\n" \
"\t-d ugenX.Y           ugen device name\n" \
"\t-f firmware image    firmware image file name for download\n" \
"\t-v vendor_id         vendor id\n" \
"\t-p vendor_id         product id\n" \
"\t-h                   display this message\n", ATH3KFW, ATH3KFW);

        exit(255);
}

OpenPOWER on IntegriCloud