summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bluetooth/ath3kfw/main.c
blob: 6ca5206b1507b521282c49eaeb499d73d716184a (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*-
 * Copyright (c) 2013 Adrian Chadd <adrian@freebsd.org>
 * 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,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES.
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <err.h>
#include <fcntl.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/param.h>

#include <libusb.h>

#include "ath3k_fw.h"
#include "ath3k_hw.h"
#include "ath3k_dbg.h"

#define	_DEFAULT_ATH3K_FIRMWARE_PATH	"/usr/share/firmware/ath3k/"

int	ath3k_do_debug = 0;
int	ath3k_do_info = 0;

struct ath3k_devid {
	uint16_t product_id;
	uint16_t vendor_id;
	int is_3012;
};

static struct ath3k_devid ath3k_list[] = {

	/* Atheros AR3012 with sflash firmware*/
	{ .vendor_id = 0x0489, .product_id = 0xe04e, .is_3012 = 1 },
	{ .vendor_id = 0x0489, .product_id = 0xe04d, .is_3012 = 1 },
	{ .vendor_id = 0x0489, .product_id = 0xe056, .is_3012 = 1 },
	{ .vendor_id = 0x0489, .product_id = 0xe057, .is_3012 = 1 },
	{ .vendor_id = 0x0489, .product_id = 0xe05f, .is_3012 = 1 },
	{ .vendor_id = 0x04c5, .product_id = 0x1330, .is_3012 = 1 },
	{ .vendor_id = 0x04ca, .product_id = 0x3004, .is_3012 = 1 },
	{ .vendor_id = 0x04ca, .product_id = 0x3005, .is_3012 = 1 },
	{ .vendor_id = 0x04ca, .product_id = 0x3006, .is_3012 = 1 },
	{ .vendor_id = 0x04ca, .product_id = 0x3008, .is_3012 = 1 },
	{ .vendor_id = 0x04ca, .product_id = 0x300b, .is_3012 = 1 },
	{ .vendor_id = 0x0930, .product_id = 0x0219, .is_3012 = 1 },
	{ .vendor_id = 0x0930, .product_id = 0x0220, .is_3012 = 1 },
	{ .vendor_id = 0x0b05, .product_id = 0x17d0, .is_3012 = 1 },
	{ .vendor_id = 0x0CF3, .product_id = 0x0036, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x3004, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x3005, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x3008, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x311D, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x311E, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x311F, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0x3121, .is_3012 = 1 },
	{ .vendor_id = 0x0CF3, .product_id = 0x817a, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0xe004, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0xe005, .is_3012 = 1 },
	{ .vendor_id = 0x0cf3, .product_id = 0xe003, .is_3012 = 1 },
	{ .vendor_id = 0x13d3, .product_id = 0x3362, .is_3012 = 1 },
	{ .vendor_id = 0x13d3, .product_id = 0x3375, .is_3012 = 1 },
	{ .vendor_id = 0x13d3, .product_id = 0x3393, .is_3012 = 1 },
	{ .vendor_id = 0x13d3, .product_id = 0x3402, .is_3012 = 1 },

	/* Atheros AR5BBU22 with sflash firmware */
	{ .vendor_id = 0x0489, .product_id = 0xE036, .is_3012 = 1 },
	{ .vendor_id = 0x0489, .product_id = 0xE03C, .is_3012 = 1 },
};

static int
ath3k_is_3012(struct libusb_device_descriptor *d)
{
	int i;

	/* Search looking for whether it's an AR3012 */
	for (i = 0; i < (int) nitems(ath3k_list); i++) {
		if ((ath3k_list[i].product_id == d->idProduct) &&
		    (ath3k_list[i].vendor_id == d->idVendor)) {
			fprintf(stderr, "%s: found AR3012\n", __func__);
			return (ath3k_list[i].is_3012);
		}
	}

	/* Not found */
	return (0);
}

static libusb_device *
ath3k_find_device(libusb_context *ctx, int bus_id, int dev_id)
{
	libusb_device **list, *dev = NULL, *found = NULL;
	ssize_t cnt, i;

	cnt = libusb_get_device_list(ctx, &list);
	if (cnt < 0) {
		ath3k_err("%s: libusb_get_device_list() failed: code %lld\n",
		    __func__,
		    (long long int) cnt);
		return (NULL);
	}

	/*
	 * XXX TODO: match on the vendor/product id too!
	 */
	for (i = 0; i < cnt; i++) {
		dev = list[i];
		if (bus_id == libusb_get_bus_number(dev) &&
		    dev_id == libusb_get_device_address(dev)) {
			/*
			 * Take a reference so it's not freed later on.
			 */
			found = libusb_ref_device(dev);
			break;
		}
	}

	libusb_free_device_list(list, 1);
	return (found);
}

static int
ath3k_init_ar3012(libusb_device_handle *hdl, const char *fw_path)
{
	int ret;

	ret = ath3k_load_patch(hdl, fw_path);
	if (ret < 0) {
		ath3k_err("Loading patch file failed\n");
	return (ret);
	}

	ret = ath3k_load_syscfg(hdl, fw_path);
	if (ret < 0) {
		ath3k_err("Loading sysconfig file failed\n");
		return (ret);
	}

	ret = ath3k_set_normal_mode(hdl);
	if (ret < 0) {
		ath3k_err("Set normal mode failed\n");
		return (ret);
	}

	ath3k_switch_pid(hdl);
	return (0);
}

static int
ath3k_init_firmware(libusb_device_handle *hdl, const char *file_prefix)
{
	struct ath3k_firmware fw;
	char fwname[FILENAME_MAX];
	int ret;

	/* XXX path info? */
	snprintf(fwname, FILENAME_MAX, "%s/ath3k-1.fw", file_prefix);

	ath3k_debug("%s: loading ath3k-1.fw\n", __func__);

	/* Read in the firmware */
	if (ath3k_fw_read(&fw, fwname) <= 0) {
		fprintf(stderr, "%s: ath3k_fw_read() failed\n",
		    __func__);
		return (-1);
	}

	/* Load in the firmware */
	ret = ath3k_load_fwfile(hdl, &fw);

	/* free it */
	ath3k_fw_free(&fw);

	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);
}

static void
usage(void)
{
	fprintf(stderr,
	    "Usage: ath3kfw (-D) -d ugenX.Y (-f firmware path) (-I)\n");
	fprintf(stderr, "    -D: enable debugging\n");
	fprintf(stderr, "    -d: device to operate upon\n");
	fprintf(stderr, "    -f: firmware path, if not default\n");
	fprintf(stderr, "    -I: enable informational output\n");
	exit(127);
}

int
main(int argc, char *argv[])
{
	struct libusb_device_descriptor d;
	libusb_context *ctx;
	libusb_device *dev;
	libusb_device_handle *hdl;
	unsigned char state;
	struct ath3k_version ver;
	int r;
	uint8_t bus_id = 0, dev_id = 0;
	int devid_set = 0;
	int n;
	char *firmware_path = NULL;
	int is_3012 = 0;

	/* libusb setup */
	r = libusb_init(&ctx);
	if (r != 0) {
		ath3k_err("%s: libusb_init failed: code %d\n",
		    argv[0],
		    r);
		exit(127);
	}

	/* Enable debugging, just because */
	libusb_set_debug(ctx, 3);

	/* Parse command line arguments */
	while ((n = getopt(argc, argv, "Dd:f:hIm:p:v:")) != -1) {
		switch (n) {
		case 'd': /* ugen device name */
			devid_set = 1;
			if (parse_ugen_name(optarg, &bus_id, &dev_id) < 0)
				usage();
			break;
		case 'D':
			ath3k_do_debug = 1;
			break;
		case 'f': /* firmware path */
			if (firmware_path)
				free(firmware_path);
			firmware_path = strdup(optarg);
			break;
		case 'I':
			ath3k_do_info = 1;
			break;
		case 'h':
		default:
			usage();
			break;
			/* NOT REACHED */
		}
	}

	/* Ensure the devid was given! */
	if (devid_set == 0) {
		usage();
		/* NOTREACHED */
	}

	ath3k_debug("%s: opening dev %d.%d\n",
	    basename(argv[0]),
	    (int) bus_id,
	    (int) dev_id);

	/* Find a device based on the bus/dev id */
	dev = ath3k_find_device(ctx, bus_id, dev_id);
	if (dev == NULL) {
		ath3k_err("%s: device not found\n", __func__);
		/* XXX cleanup? */
		exit(1);
	}

	/* Get the device descriptor for this device entry */
	r = libusb_get_device_descriptor(dev, &d);
	if (r != 0) {
		warn("%s: libusb_get_device_descriptor: %s\n",
		    __func__,
		    libusb_strerror(r));
		exit(1);
	}

	/* See if its an AR3012 */
	if (ath3k_is_3012(&d)) {
		is_3012 = 1;

		/* If it's bcdDevice > 1, don't attach */
		if (d.bcdDevice > 0x0001) {
			ath3k_debug("%s: AR3012; bcdDevice=%d, exiting\n",
			    __func__,
			    d.bcdDevice);
			exit(0);
		}
	}

	/* XXX enforce that bInterfaceNumber is 0 */

	/* XXX enforce the device/product id if they're non-zero */

	/* Grab device handle */
	r = libusb_open(dev, &hdl);
	if (r != 0) {
		ath3k_err("%s: libusb_open() failed: code %d\n", __func__, r);
		/* XXX cleanup? */
		exit(1);
	}

	/*
	 * Get the initial NIC state.
	 */
	r = ath3k_get_state(hdl, &state);
	if (r == 0) {
		ath3k_err("%s: ath3k_get_state() failed!\n", __func__);
		/* XXX cleanup? */
		exit(1);
	}
	ath3k_debug("%s: state=0x%02x\n",
	    __func__,
	    (int) state);

	/* And the version */
	r = ath3k_get_version(hdl, &ver);
	if (r == 0) {
		ath3k_err("%s: ath3k_get_version() failed!\n", __func__);
		/* XXX cleanup? */
		exit(1);
	}
	ath3k_info("ROM version: %d, build version: %d, ram version: %d, "
	    "ref clock=%d\n",
	    ver.rom_version,
	    ver.build_version,
	    ver.ram_version,
	    ver.ref_clock);

	/* Default the firmware path */
	if (firmware_path == NULL)
		firmware_path = strdup(_DEFAULT_ATH3K_FIRMWARE_PATH);

	if (is_3012) {
		(void) ath3k_init_ar3012(hdl, firmware_path);
	} else {
		(void) ath3k_init_firmware(hdl, firmware_path);
	}

	/* Shutdown */
	libusb_close(hdl);
	hdl = NULL;

	libusb_unref_device(dev);
	dev = NULL;

	libusb_exit(ctx);
	ctx = NULL;
}
OpenPOWER on IntegriCloud