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.
39 #include <sys/endian.h>
40 #include <sys/types.h>
47 #include "ath3k_dbg.h"
49 #define XMIN(x, y) ((x) < (y) ? (x) : (y))
52 ath3k_load_fwfile(struct libusb_device_handle *hdl,
53 const struct ath3k_firmware *fw)
55 int size, count, sent = 0;
60 size = XMIN(count, FW_HDR_SIZE);
62 ath3k_debug("%s: file=%s, size=%d\n",
63 __func__, fw->fwname, count);
66 * Flip the device over to configuration mode.
68 ret = libusb_control_transfer(hdl,
69 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
75 1000); /* XXX timeout */
78 fprintf(stderr, "Can't switch to config mode; ret=%d\n",
86 /* Load in the rest of the data */
88 size = XMIN(count, BULK_SIZE);
89 ath3k_debug("%s: transferring %d bytes, offset %d\n",
94 ret = libusb_bulk_transfer(hdl,
101 if (ret < 0 || r != size) {
102 fprintf(stderr, "Can't load firmware: err=%s, size=%d\n",
103 libusb_strerror(ret),
114 ath3k_get_state(struct libusb_device_handle *hdl, unsigned char *state)
118 ret = libusb_control_transfer(hdl,
119 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
125 1000); /* XXX timeout */
129 "%s: libusb_control_transfer() failed: code=%d\n",
139 ath3k_get_version(struct libusb_device_handle *hdl,
140 struct ath3k_version *version)
144 ret = libusb_control_transfer(hdl,
145 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
149 (unsigned char *) version,
150 sizeof(struct ath3k_version),
151 1000); /* XXX timeout */
155 "%s: libusb_control_transfer() failed: code=%d\n",
161 /* XXX endian fix! */
163 return (ret == sizeof(struct ath3k_version));
167 ath3k_load_patch(libusb_device_handle *hdl, const char *fw_path)
170 unsigned char fw_state;
171 struct ath3k_version fw_ver, pt_ver;
172 char fwname[FILENAME_MAX];
173 struct ath3k_firmware fw;
176 ret = ath3k_get_state(hdl, &fw_state);
178 ath3k_err("%s: Can't get state\n", __func__);
182 if (fw_state & ATH3K_PATCH_UPDATE) {
183 ath3k_info("%s: Patch already downloaded\n",
188 ret = ath3k_get_version(hdl, &fw_ver);
190 ath3k_debug("%s: Can't get version\n", __func__);
195 snprintf(fwname, FILENAME_MAX, "%s/ar3k/AthrBT_0x%08x.dfu",
199 /* Read in the firmware */
200 if (ath3k_fw_read(&fw, fwname) <= 0) {
201 ath3k_debug("%s: ath3k_fw_read() failed\n",
207 * Extract the ROM/build version from the patch file.
209 memcpy(&tmp, fw.buf + fw.len - 8, sizeof(tmp));
210 pt_ver.rom_version = le32toh(tmp);
211 memcpy(&tmp, fw.buf + fw.len - 4, sizeof(tmp));
212 pt_ver.build_version = le32toh(tmp);
214 ath3k_info("%s: file %s: rom_ver=%d, build_ver=%d\n",
217 (int) pt_ver.rom_version,
218 (int) pt_ver.build_version);
220 /* Check the ROM/build version against the firmware */
221 if ((pt_ver.rom_version != fw_ver.rom_version) ||
222 (pt_ver.build_version <= fw_ver.build_version)) {
223 ath3k_debug("Patch file version mismatch!\n");
228 /* Load in the firmware */
229 ret = ath3k_load_fwfile(hdl, &fw);
238 ath3k_load_syscfg(libusb_device_handle *hdl, const char *fw_path)
240 unsigned char fw_state;
241 char filename[FILENAME_MAX];
242 struct ath3k_firmware fw;
243 struct ath3k_version fw_ver;
246 ret = ath3k_get_state(hdl, &fw_state);
248 ath3k_err("Can't get state to change to load configuration err");
252 ret = ath3k_get_version(hdl, &fw_ver);
254 ath3k_err("Can't get version to change to load ram patch err");
258 switch (fw_ver.ref_clock) {
259 case ATH3K_XTAL_FREQ_26M:
262 case ATH3K_XTAL_FREQ_40M:
265 case ATH3K_XTAL_FREQ_19P2:
273 snprintf(filename, FILENAME_MAX, "%s/ar3k/ramps_0x%08x_%d%s",
279 ath3k_info("%s: syscfg file = %s\n",
283 /* Read in the firmware */
284 if (ath3k_fw_read(&fw, filename) <= 0) {
285 ath3k_err("%s: ath3k_fw_read() failed\n",
290 ret = ath3k_load_fwfile(hdl, &fw);
297 ath3k_set_normal_mode(libusb_device_handle *hdl)
300 unsigned char fw_state;
302 ret = ath3k_get_state(hdl, &fw_state);
304 ath3k_err("%s: can't get state\n", __func__);
309 * This isn't a fatal error - the device may have detached
312 if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
313 ath3k_debug("%s: firmware is already in normal mode\n",
318 ret = libusb_control_transfer(hdl,
319 LIBUSB_REQUEST_TYPE_VENDOR, /* XXX out direction? */
320 ATH3K_SET_NORMAL_MODE,
325 1000); /* XXX timeout */
328 ath3k_err("%s: libusb_control_transfer() failed: code=%d\n",
338 ath3k_switch_pid(libusb_device_handle *hdl)
341 ret = libusb_control_transfer(hdl,
342 LIBUSB_REQUEST_TYPE_VENDOR, /* XXX set an out flag? */
343 USB_REG_SWITCH_VID_PID,
348 1000); /* XXX timeout */
351 ath3k_debug("%s: libusb_control_transfer() failed: code=%d\n",