svn commit: r301537 - head/usr.sbin/bluetooth/ath3kfw

Adrian Chadd adrian at FreeBSD.org
Tue Jun 7 04:22:20 UTC 2016


Author: adrian
Date: Tue Jun  7 04:22:18 2016
New Revision: 301537
URL: https://svnweb.freebsd.org/changeset/base/301537

Log:
  [ath3k] add a replacement ath3k firmware loading tool.
  
  This is influenced by the ath3k driver from linux (circa 2013, this is
  how long I've been sitting on this.)
  
  It handles loading in firmware using the newer model, where it assembles
  the right set of firmware blobs and board configuration based on the
  device list and querying the device.
  
  The older utility could only load in a single image - which sometimes
  was ath3k-1.fw and sometimes was ath3k-2.fw.  However, the ath3k maintainers
  didn't want to keep adding in binaries that were just derivatives with a
  separate board config, so they deleted ath3k-2.fw from the Linux firmware
  repository and instead, well, did this.
  
  Now, this has been tested against AR3011 and AR3012 NICs from the AR9285+BT
  combo up through to the QCA9565+BT combo.  It doesn't yet work with the
  QCAFN222 NIC as that is some newer chip.
  
  The firmware can be grabbed from https://github.com/erikarn/ath3kfw/ in
  the share/firmware/ath3k directory.  I'll update this utility over time
  to support the newer firmware drops (newer than mid-2013) which should
  pull in the QCNFA222 and subsequent chips.
  
  Tested:
  
  * AR9285 + BT
  * AR9287 + BT
  * AR9485 + BT
  * AR9462 + BT
  * QCA9565 + BT

Added:
  head/usr.sbin/bluetooth/ath3kfw/ath3k_dbg.h   (contents, props changed)
  head/usr.sbin/bluetooth/ath3kfw/ath3k_fw.c   (contents, props changed)
  head/usr.sbin/bluetooth/ath3kfw/ath3k_fw.h   (contents, props changed)
  head/usr.sbin/bluetooth/ath3kfw/ath3k_hw.c   (contents, props changed)
  head/usr.sbin/bluetooth/ath3kfw/ath3k_hw.h   (contents, props changed)
  head/usr.sbin/bluetooth/ath3kfw/main.c   (contents, props changed)
Deleted:
  head/usr.sbin/bluetooth/ath3kfw/ath3kfw.c
Modified:
  head/usr.sbin/bluetooth/ath3kfw/Makefile
  head/usr.sbin/bluetooth/ath3kfw/ath3kfw.8

Modified: head/usr.sbin/bluetooth/ath3kfw/Makefile
==============================================================================
--- head/usr.sbin/bluetooth/ath3kfw/Makefile	Tue Jun  7 01:00:08 2016	(r301536)
+++ head/usr.sbin/bluetooth/ath3kfw/Makefile	Tue Jun  7 04:22:18 2016	(r301537)
@@ -3,5 +3,6 @@
 PROG=		ath3kfw
 MAN=		ath3kfw.8
 LIBADD+=	usb
+SRCS=		main.c ath3k_fw.c ath3k_hw.c
 
 .include <bsd.prog.mk>

Added: head/usr.sbin/bluetooth/ath3kfw/ath3k_dbg.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bluetooth/ath3kfw/ath3k_dbg.h	Tue Jun  7 04:22:18 2016	(r301537)
@@ -0,0 +1,41 @@
+/*-
+ * Copyright (c) 2013 Adrian Chadd <adrian at 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$
+ */
+#ifndef	__ATH3K_DEBUG_H__
+#define	__ATH3K_DEBUG_H__
+
+extern	int ath3k_do_debug;
+extern	int ath3k_do_info;
+
+#define	ath3k_debug(...)	if (ath3k_do_debug) fprintf(stderr, __VA_ARGS__)
+#define	ath3k_err(...)		fprintf(stderr, __VA_ARGS__)
+#define	ath3k_info(...)		if (ath3k_do_info) fprintf(stdout, __VA_ARGS__)
+
+#endif

Added: head/usr.sbin/bluetooth/ath3kfw/ath3k_fw.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bluetooth/ath3kfw/ath3k_fw.c	Tue Jun  7 04:22:18 2016	(r301537)
@@ -0,0 +1,114 @@
+/*-
+ * Copyright (c) 2013 Adrian Chadd <adrian at 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 <sys/types.h>
+#include <sys/stat.h>
+
+#include "ath3k_fw.h"
+#include "ath3k_dbg.h"
+
+int
+ath3k_fw_read(struct ath3k_firmware *fw, const char *fwname)
+{
+	int fd;
+	struct stat sb;
+	unsigned char *buf;
+	ssize_t r;
+	int i;
+
+	fd = open(fwname, O_RDONLY);
+	if (fd < 0) {
+		warn("%s: open: %s", __func__, fwname);
+		return (0);
+	}
+
+	if (fstat(fd, &sb) != 0) {
+		warn("%s: stat: %s", __func__, fwname);
+		close(fd);
+		return (0);
+	}
+	
+	buf = calloc(1, sb.st_size);
+	if (buf == NULL) {
+		warn("%s: calloc", __func__);
+		close(fd);
+		return (0);
+	}
+
+	i = 0;
+	/* XXX handle partial reads */
+	r = read(fd, buf, sb.st_size);
+	if (r < 0) {
+		warn("%s: read", __func__);
+		free(buf);
+		close(fd);
+		return (0);
+	}
+
+	if (r != sb.st_size) {
+		fprintf(stderr, "%s: read len %d != file size %d\n",
+		    __func__,
+		    (int) r,
+		    (int) sb.st_size);
+		free(buf);
+		close(fd);
+		return (0);
+	}
+
+	/* We have everything, so! */
+
+	bzero(fw, sizeof(*fw));
+
+	fw->fwname = strdup(fwname);
+	fw->len = sb.st_size;
+	fw->size = sb.st_size;
+	fw->buf = buf;
+
+	close(fd);
+	return (1);
+}
+
+void
+ath3k_fw_free(struct ath3k_firmware *fw)
+{
+	if (fw->fwname)
+		free(fw->fwname);
+	if (fw->buf)
+		free(fw->buf);
+	bzero(fw, sizeof(*fw));
+}

Added: head/usr.sbin/bluetooth/ath3kfw/ath3k_fw.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bluetooth/ath3kfw/ath3k_fw.h	Tue Jun  7 04:22:18 2016	(r301537)
@@ -0,0 +1,56 @@
+/*-
+ * Copyright (c) 2013 Adrian Chadd <adrian at 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$
+ */
+#ifndef	__ATH3K_FW_H__
+#define	__ATH3K_FW_H__
+
+/*
+ * XXX TODO: ensure that the endian-ness of this stuff is
+ * correct!
+ */
+struct ath3k_version {
+	unsigned int	rom_version;
+	unsigned int	build_version;
+	unsigned int	ram_version;
+	unsigned char	ref_clock;
+	unsigned char	reserved[0x07];
+};
+
+struct ath3k_firmware {
+	char *fwname;
+	int len;		/* firmware length */
+	int size;		/* buffer size */
+	unsigned char *buf;
+};
+
+extern	int ath3k_fw_read(struct ath3k_firmware *fw, const char *fwname);
+extern	void ath3k_fw_free(struct ath3k_firmware *fw);
+
+#endif

Added: head/usr.sbin/bluetooth/ath3kfw/ath3k_hw.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bluetooth/ath3kfw/ath3k_hw.c	Tue Jun  7 04:22:18 2016	(r301537)
@@ -0,0 +1,358 @@
+/*-
+ * Copyright (c) 2013 Adrian Chadd <adrian at 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 <sys/endian.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <libusb.h>
+
+#include "ath3k_fw.h"
+#include "ath3k_hw.h"
+#include "ath3k_dbg.h"
+
+#define	XMIN(x, y)	((x) < (y) ? (x) : (y))
+
+int
+ath3k_load_fwfile(struct libusb_device_handle *hdl,
+    const struct ath3k_firmware *fw)
+{
+	int size, count, sent = 0;
+	int ret, r;
+
+	count = fw->len;
+
+	size = XMIN(count, FW_HDR_SIZE);
+
+	ath3k_debug("%s: file=%s, size=%d\n",
+	    __func__, fw->fwname, count);
+
+	/*
+	 * Flip the device over to configuration mode.
+	 */
+	ret = libusb_control_transfer(hdl,
+	    LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
+	    ATH3K_DNLOAD,
+	    0,
+	    0,
+	    fw->buf + sent,
+	    size,
+	    1000);	/* XXX timeout */
+
+	if (ret != size) {
+		fprintf(stderr, "Can't switch to config mode; ret=%d\n",
+		    ret);
+		return (-1);
+	}
+
+	sent += size;
+	count -= size;
+
+	/* Load in the rest of the data */
+	while (count) {
+		size = XMIN(count, BULK_SIZE);
+		ath3k_debug("%s: transferring %d bytes, offset %d\n",
+		    __func__,
+		    sent,
+		    size);
+
+		ret = libusb_bulk_transfer(hdl,
+		    0x2,
+		    fw->buf + sent,
+		    size,
+		    &r,
+		    1000);
+
+		if (ret < 0 || r != size) {
+			fprintf(stderr, "Can't load firmware: err=%s, size=%d\n",
+			    libusb_strerror(ret),
+			    size);
+			return (-1);
+		}
+		sent  += size;
+		count -= size;
+	}
+	return (0);
+}
+
+int
+ath3k_get_state(struct libusb_device_handle *hdl, unsigned char *state)
+{
+	int ret;
+
+	ret = libusb_control_transfer(hdl,
+	    LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
+	    ATH3K_GETSTATE,
+	    0,
+	    0,
+	    state,
+	    1,
+	    1000);	/* XXX timeout */
+
+	if (ret < 0) {
+		fprintf(stderr,
+		    "%s: libusb_control_transfer() failed: code=%d\n",
+		    __func__,
+		    ret);
+		return (0);
+	}
+
+	return (ret == 1);
+}
+
+int
+ath3k_get_version(struct libusb_device_handle *hdl,
+    struct ath3k_version *version)
+{
+	int ret;
+
+	ret = libusb_control_transfer(hdl,
+	    LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
+	    ATH3K_GETVERSION,
+	    0,
+	    0,
+	    (unsigned char *) version,
+	    sizeof(struct ath3k_version),
+	    1000);	/* XXX timeout */
+
+	if (ret < 0) {
+		fprintf(stderr,
+		    "%s: libusb_control_transfer() failed: code=%d\n",
+		    __func__,
+		    ret);
+		return (0);
+	}
+
+	/* XXX endian fix! */
+
+	return (ret == sizeof(struct ath3k_version));
+}
+
+int
+ath3k_load_patch(libusb_device_handle *hdl, const char *fw_path)
+{
+	int ret;
+	unsigned char fw_state;
+	struct ath3k_version fw_ver, pt_ver;
+	char fwname[FILENAME_MAX];
+	struct ath3k_firmware fw;
+	uint32_t tmp;
+
+	ret = ath3k_get_state(hdl, &fw_state);
+	if (ret < 0) {
+		ath3k_err("%s: Can't get state\n", __func__);
+		return (ret);
+	}
+
+	if (fw_state & ATH3K_PATCH_UPDATE) {
+		ath3k_info("%s: Patch already downloaded\n",
+		    __func__);
+		return (0);
+	}
+
+	ret = ath3k_get_version(hdl, &fw_ver);
+	if (ret < 0) {
+		ath3k_debug("%s: Can't get version\n", __func__);
+		return (ret);
+	}
+
+	/* XXX path info? */
+	snprintf(fwname, FILENAME_MAX, "%s/ar3k/AthrBT_0x%08x.dfu",
+	    fw_path,
+	    fw_ver.rom_version);
+
+	/* Read in the firmware */
+	if (ath3k_fw_read(&fw, fwname) <= 0) {
+		ath3k_debug("%s: ath3k_fw_read() failed\n",
+		    __func__);
+		return (-1);
+	}
+
+	/*
+	 * Extract the ROM/build version from the patch file.
+	 */
+	memcpy(&tmp, fw.buf + fw.len - 8, sizeof(tmp));
+	pt_ver.rom_version = le32toh(tmp);
+	memcpy(&tmp, fw.buf + fw.len - 4, sizeof(tmp));
+	pt_ver.build_version = le32toh(tmp);
+
+	ath3k_info("%s: file %s: rom_ver=%d, build_ver=%d\n",
+	    __func__,
+	    fwname,
+	    (int) pt_ver.rom_version,
+	    (int) pt_ver.build_version);
+
+	/* Check the ROM/build version against the firmware */
+	if ((pt_ver.rom_version != fw_ver.rom_version) ||
+	    (pt_ver.build_version <= fw_ver.build_version)) {
+		ath3k_debug("Patch file version mismatch!\n");
+		ath3k_fw_free(&fw);
+		return (-1);
+	}
+
+	/* Load in the firmware */
+	ret = ath3k_load_fwfile(hdl, &fw);
+
+	/* free it */
+	ath3k_fw_free(&fw);
+
+	return (ret);
+}
+
+int
+ath3k_load_syscfg(libusb_device_handle *hdl, const char *fw_path)
+{
+	unsigned char fw_state;
+	char filename[FILENAME_MAX];
+	struct ath3k_firmware fw;
+	struct ath3k_version fw_ver;
+	int clk_value, ret;
+
+	ret = ath3k_get_state(hdl, &fw_state);
+	if (ret < 0) {
+		ath3k_err("Can't get state to change to load configuration err");
+		return (-EBUSY);
+	}
+
+	ret = ath3k_get_version(hdl, &fw_ver);
+	if (ret < 0) {
+		ath3k_err("Can't get version to change to load ram patch err");
+		return (ret);
+	}
+
+	switch (fw_ver.ref_clock) {
+	case ATH3K_XTAL_FREQ_26M:
+		clk_value = 26;
+		break;
+	case ATH3K_XTAL_FREQ_40M:
+		clk_value = 40;
+		break;
+	case ATH3K_XTAL_FREQ_19P2:
+		clk_value = 19;
+		break;
+	default:
+		clk_value = 0;
+		break;
+}
+
+	snprintf(filename, FILENAME_MAX, "%s/ar3k/ramps_0x%08x_%d%s",
+	    fw_path,
+	    fw_ver.rom_version,
+	    clk_value,
+	    ".dfu");
+
+	ath3k_info("%s: syscfg file = %s\n",
+	    __func__,
+	    filename);
+
+	/* Read in the firmware */
+	if (ath3k_fw_read(&fw, filename) <= 0) {
+		ath3k_err("%s: ath3k_fw_read() failed\n",
+		    __func__);
+		return (-1);
+	}
+
+	ret = ath3k_load_fwfile(hdl, &fw);
+
+	ath3k_fw_free(&fw);
+	return (ret);
+}
+
+int
+ath3k_set_normal_mode(libusb_device_handle *hdl)
+{
+	int ret;
+	unsigned char fw_state;
+
+	ret = ath3k_get_state(hdl, &fw_state);
+	if (ret < 0) {
+		ath3k_err("%s: can't get state\n", __func__);
+		return (ret);
+	}
+
+	/*
+	 * This isn't a fatal error - the device may have detached
+	 * already.
+	 */
+	if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
+		ath3k_debug("%s: firmware is already in normal mode\n",
+		    __func__);
+		return (0);
+	}
+
+	ret = libusb_control_transfer(hdl,
+	    LIBUSB_REQUEST_TYPE_VENDOR,		/* XXX out direction? */
+	    ATH3K_SET_NORMAL_MODE,
+	    0,
+	    0,
+	    NULL,
+	    0,
+	    1000);	/* XXX timeout */
+
+	if (ret < 0) {
+		ath3k_err("%s: libusb_control_transfer() failed: code=%d\n",
+		    __func__,
+		    ret);
+		return (0);
+	}
+
+	return (ret == 0);
+}
+
+int
+ath3k_switch_pid(libusb_device_handle *hdl)
+{
+	int ret;
+	ret = libusb_control_transfer(hdl,
+	    LIBUSB_REQUEST_TYPE_VENDOR,		/* XXX set an out flag? */
+	    USB_REG_SWITCH_VID_PID,
+	    0,
+	    0,
+	    NULL,
+	    0,
+	    1000);	/* XXX timeout */
+
+	if (ret < 0) {
+		ath3k_debug("%s: libusb_control_transfer() failed: code=%d\n",
+		    __func__,
+		    ret);
+		return (0);
+	}
+
+	return (ret == 0);
+}

Added: head/usr.sbin/bluetooth/ath3kfw/ath3k_hw.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bluetooth/ath3kfw/ath3k_hw.h	Tue Jun  7 04:22:18 2016	(r301537)
@@ -0,0 +1,66 @@
+/*-
+ * Copyright (c) 2013 Adrian Chadd <adrian at 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$
+ */
+#ifndef	__ATH3K_HW_H__
+#define	__ATH3K_HW_H__
+
+#define	ATH3K_DNLOAD			0x01
+#define	ATH3K_GETSTATE			0x05
+#define	ATH3K_SET_NORMAL_MODE		0x07
+#define	ATH3K_GETVERSION		0x09
+#define	USB_REG_SWITCH_VID_PID		0x0a
+
+#define	ATH3K_MODE_MASK			0x3F
+#define	ATH3K_NORMAL_MODE		0x0E
+
+#define	ATH3K_PATCH_UPDATE		0x80
+#define	ATH3K_SYSCFG_UPDATE		0x40
+
+#define	ATH3K_XTAL_FREQ_26M		0x00
+#define	ATH3K_XTAL_FREQ_40M		0x01
+#define	ATH3K_XTAL_FREQ_19P2		0x02
+#define	ATH3K_NAME_LEN			0xFF
+
+#define	USB_REQ_DFU_DNLOAD		1
+#define	BULK_SIZE			4096
+#define	FW_HDR_SIZE			20
+
+extern	int ath3k_load_fwfile(struct libusb_device_handle *hdl,
+	    const struct ath3k_firmware *fw);
+extern	int ath3k_get_state(struct libusb_device_handle *hdl,
+	    unsigned char *state);
+extern	int ath3k_get_version(struct libusb_device_handle *hdl,
+	    struct ath3k_version *version);
+extern	int ath3k_load_patch(libusb_device_handle *hdl, const char *fw_path);
+extern	int ath3k_load_syscfg(libusb_device_handle *hdl, const char *fw_path);
+extern	int ath3k_set_normal_mode(libusb_device_handle *hdl);
+extern	int ath3k_switch_pid(libusb_device_handle *hdl);
+
+#endif

Modified: head/usr.sbin/bluetooth/ath3kfw/ath3kfw.8
==============================================================================
--- head/usr.sbin/bluetooth/ath3kfw/ath3kfw.8	Tue Jun  7 01:00:08 2016	(r301536)
+++ head/usr.sbin/bluetooth/ath3kfw/ath3kfw.8	Tue Jun  7 04:22:18 2016	(r301537)
@@ -1,4 +1,5 @@
 .\" Copyright (c) 2010 Maksim Yevmenkin <m_evmenkin at yahoo.com>
+.\" Copyright (c) 2013, 2016 Adrian Chadd <adrian at freebsd.org>
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -24,16 +25,16 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd November 9, 2010
+.Dd June 4, 2016
 .Dt ATH3KFW 8
 .Os
 .Sh NAME
 .Nm ath3kfw
-.Nd firmware download utility for Atheros AR3011 chip based Bluetooth USB devices
+.Nd firmware download utility for Atheros AR3011/AR3012 chip based Bluetooth USB devices
 .Sh SYNOPSIS
 .Nm
 .Fl d Ar device_name
-.Fl f Ar firmware_file_name
+.Fl f Ar firmware_path
 .Nm
 .Fl h
 .Sh DESCRIPTION
@@ -45,23 +46,31 @@ device.
 .Pp
 This utility will
 .Em only
-work with Atheros AR3011 chip based Bluetooth USB devices.
+work with Atheros AR3011 and AR3012 chip based Bluetooth USB devices.
 The identification is currently based on USB vendor ID/product ID pair.
 The vendor ID should be 0x0cf3
 .Pq Dv USB_VENDOR_ATHEROS2
-and the product ID should be 0x3000.
+and the product ID should be one of the supported devices.
 .Pp
-Firmware files ath3k-1.fw and ath3k-2.fw can be obtained from the
-linux-firmware RPM.
+Firmware files are available in the linux-firmware RPM.
+.Pp
+The
+.Nm
+utility will query the device to determine which firmware image and board
+configuration to load in at runtime.
 .Pp
 The options are as follows:
 .Bl -tag -width indent
+.It Fl D
+Enable verbose debugging.
 .It Fl d Ar device_name
 Specify
 .Xr ugen 4
 device name.
-.It Fl f Ar firmware_file_name
-Specify firmware file name for download.
+.It I
+Enable informational debugging.
+.It Fl f Ar firmware_path
+Specify the directory containing the firmware files to search and upload.
 .It Fl h
 Display usage message and exit.
 .El
@@ -72,7 +81,10 @@ Display usage message and exit.
 .Xr ugen 4 ,
 .Xr devd 8
 .Sh AUTHORS
-.An Maksim Yevmenkin Aq Mt m_evmenkin at yahoo.com
+The original utility was written by
+.An Maksim Yevmenkin Aq Mt m_evmenkin at yahoo.com .
+This was written based on Linux ath3k by
+.An Adrian Chadd Aq Mt adrian at freebsd.org .
 .Sh BUGS
 Most likely.
 Please report if found.

Added: head/usr.sbin/bluetooth/ath3kfw/main.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bluetooth/ath3kfw/main.c	Tue Jun  7 04:22:18 2016	(r301537)
@@ -0,0 +1,394 @@
+/*-
+ * Copyright (c) 2013 Adrian Chadd <adrian at 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],

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-head mailing list