2 * Copyright (c) 2013 Adrian Chadd <adrian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
37 #include <sys/endian.h>
38 #include <sys/types.h>
45 #include "ath3k_dbg.h"
47 #define XMIN(x, y) ((x) < (y) ? (x) : (y))
50 ath3k_load_fwfile(struct libusb_device_handle *hdl,
51 const struct ath3k_firmware *fw)
53 int size, count, sent = 0;
58 size = XMIN(count, FW_HDR_SIZE);
60 ath3k_debug("%s: file=%s, size=%d\n",
61 __func__, fw->fwname, count);
64 * Flip the device over to configuration mode.
66 ret = libusb_control_transfer(hdl,
67 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
73 1000); /* XXX timeout */
76 fprintf(stderr, "Can't switch to config mode; ret=%d\n",
84 /* Load in the rest of the data */
86 size = XMIN(count, BULK_SIZE);
87 ath3k_debug("%s: transferring %d bytes, offset %d\n",
92 ret = libusb_bulk_transfer(hdl,
99 if (ret < 0 || r != size) {
100 fprintf(stderr, "Can't load firmware: err=%s, size=%d\n",
101 libusb_strerror(ret),
112 ath3k_get_state(struct libusb_device_handle *hdl, unsigned char *state)
116 ret = libusb_control_transfer(hdl,
117 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
123 1000); /* XXX timeout */
127 "%s: libusb_control_transfer() failed: code=%d\n",
137 ath3k_get_version(struct libusb_device_handle *hdl,
138 struct ath3k_version *version)
142 ret = libusb_control_transfer(hdl,
143 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
147 (unsigned char *) version,
148 sizeof(struct ath3k_version),
149 1000); /* XXX timeout */
153 "%s: libusb_control_transfer() failed: code=%d\n",
159 /* XXX endian fix! */
161 return (ret == sizeof(struct ath3k_version));
165 ath3k_load_patch(libusb_device_handle *hdl, const char *fw_path)
168 unsigned char fw_state;
169 struct ath3k_version fw_ver, pt_ver;
170 char fwname[FILENAME_MAX];
171 struct ath3k_firmware fw;
174 ret = ath3k_get_state(hdl, &fw_state);
176 ath3k_err("%s: Can't get state\n", __func__);
180 if (fw_state & ATH3K_PATCH_UPDATE) {
181 ath3k_info("%s: Patch already downloaded\n",
186 ret = ath3k_get_version(hdl, &fw_ver);
188 ath3k_debug("%s: Can't get version\n", __func__);
193 snprintf(fwname, FILENAME_MAX, "%s/ar3k/AthrBT_0x%08x.dfu",
197 /* Read in the firmware */
198 if (ath3k_fw_read(&fw, fwname) <= 0) {
199 ath3k_debug("%s: ath3k_fw_read() failed\n",
205 * Extract the ROM/build version from the patch file.
207 memcpy(&tmp, fw.buf + fw.len - 8, sizeof(tmp));
208 pt_ver.rom_version = le32toh(tmp);
209 memcpy(&tmp, fw.buf + fw.len - 4, sizeof(tmp));
210 pt_ver.build_version = le32toh(tmp);
212 ath3k_info("%s: file %s: rom_ver=%d, build_ver=%d\n",
215 (int) pt_ver.rom_version,
216 (int) pt_ver.build_version);
218 /* Check the ROM/build version against the firmware */
219 if ((pt_ver.rom_version != fw_ver.rom_version) ||
220 (pt_ver.build_version <= fw_ver.build_version)) {
221 ath3k_debug("Patch file version mismatch!\n");
226 /* Load in the firmware */
227 ret = ath3k_load_fwfile(hdl, &fw);
236 ath3k_load_syscfg(libusb_device_handle *hdl, const char *fw_path)
238 unsigned char fw_state;
239 char filename[FILENAME_MAX];
240 struct ath3k_firmware fw;
241 struct ath3k_version fw_ver;
244 ret = ath3k_get_state(hdl, &fw_state);
246 ath3k_err("Can't get state to change to load configuration err");
250 ret = ath3k_get_version(hdl, &fw_ver);
252 ath3k_err("Can't get version to change to load ram patch err");
256 switch (fw_ver.ref_clock) {
257 case ATH3K_XTAL_FREQ_26M:
260 case ATH3K_XTAL_FREQ_40M:
263 case ATH3K_XTAL_FREQ_19P2:
271 snprintf(filename, FILENAME_MAX, "%s/ar3k/ramps_0x%08x_%d%s",
277 ath3k_info("%s: syscfg file = %s\n",
281 /* Read in the firmware */
282 if (ath3k_fw_read(&fw, filename) <= 0) {
283 ath3k_err("%s: ath3k_fw_read() failed\n",
288 ret = ath3k_load_fwfile(hdl, &fw);
295 ath3k_set_normal_mode(libusb_device_handle *hdl)
298 unsigned char fw_state;
300 ret = ath3k_get_state(hdl, &fw_state);
302 ath3k_err("%s: can't get state\n", __func__);
307 * This isn't a fatal error - the device may have detached
310 if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
311 ath3k_debug("%s: firmware is already in normal mode\n",
316 ret = libusb_control_transfer(hdl,
317 LIBUSB_REQUEST_TYPE_VENDOR, /* XXX out direction? */
318 ATH3K_SET_NORMAL_MODE,
323 1000); /* XXX timeout */
326 ath3k_err("%s: libusb_control_transfer() failed: code=%d\n",
336 ath3k_switch_pid(libusb_device_handle *hdl)
339 ret = libusb_control_transfer(hdl,
340 LIBUSB_REQUEST_TYPE_VENDOR, /* XXX set an out flag? */
341 USB_REG_SWITCH_VID_PID,
346 1000); /* XXX timeout */
349 ath3k_debug("%s: libusb_control_transfer() failed: code=%d\n",