6 * Copyright (c) 2010 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
36 #include <libusb20_desc.h>
45 #define ATH3KFW "ath3kfw"
46 #define ATH3KFW_VENDOR_ID 0x0cf3
47 #define ATH3KFW_PRODUCT_ID 0x3000
48 #define ATH3KFW_FW "/usr/local/etc/ath3k-1.fw"
49 #define ATH3KFW_BULK_EP 0x02
50 #define ATH3KFW_REQ_DFU_DNLOAD 1
51 #define ATH3KFW_MAX_BSIZE 4096
53 static int parse_ugen_name (char const *ugen, uint8_t *bus,
55 static int find_device (struct libusb20_backend *be,
56 uint8_t bus, uint8_t addr,
57 struct libusb20_device **dev);
58 static int download_firmware (struct libusb20_device *dev,
59 char const *firmware);
60 static void usage (void);
63 * Firmware downloader for Atheros AR3011 based USB Bluetooth devices
67 main(int argc, char **argv)
71 struct libusb20_backend *be;
72 struct libusb20_device *dev;
75 openlog(ATH3KFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
79 firmware = ATH3KFW_FW;
81 while ((n = getopt(argc, argv, "d:f:h")) != -1) {
83 case 'd': /* ugen device name */
84 if (parse_ugen_name(optarg, &bus, &addr) < 0)
88 case 'f': /* firmware file */
100 be = libusb20_be_alloc_default();
102 syslog(LOG_ERR, "libusb20_be_alloc_default() failed");
106 if (find_device(be, bus, addr, &dev) < 0) {
107 syslog(LOG_ERR, "ugen%d.%d is not recognized as " \
108 "Atheros AR3011 based device " \
109 "(possibly caused by lack of permissions)", bus, addr);
113 if (download_firmware(dev, firmware) < 0) {
114 syslog(LOG_ERR, "could not download %s firmare to ugen%d.%d",
115 firmware, bus, addr);
119 libusb20_be_free(be);
126 * Parse ugen name and extract device's bus and address
130 parse_ugen_name(char const *ugen, uint8_t *bus, uint8_t *addr)
134 if (strncmp(ugen, "ugen", 4) != 0)
137 *bus = (uint8_t) strtoul(ugen + 4, &ep, 10);
141 *addr = (uint8_t) strtoul(ep + 1, &ep, 10);
153 find_device(struct libusb20_backend *be, uint8_t bus, uint8_t addr,
154 struct libusb20_device **dev)
156 struct LIBUSB20_DEVICE_DESC_DECODED *desc;
160 while ((*dev = libusb20_be_device_foreach(be, *dev)) != NULL) {
161 if (libusb20_dev_get_bus_number(*dev) != bus ||
162 libusb20_dev_get_address(*dev) != addr)
165 desc = libusb20_dev_get_device_desc(*dev);
169 if (desc->idVendor != ATH3KFW_VENDOR_ID ||
170 desc->idProduct != ATH3KFW_PRODUCT_ID)
176 return ((*dev == NULL)? -1 : 0);
184 download_firmware(struct libusb20_device *dev, char const *firmware)
186 struct libusb20_transfer *bulk;
187 struct LIBUSB20_CONTROL_SETUP_DECODED req;
189 uint8_t buf[ATH3KFW_MAX_BSIZE];
193 if (libusb20_dev_open(dev, 1) != 0) {
194 syslog(LOG_ERR, "libusb20_dev_open() failed");
198 if ((bulk = libusb20_tr_get_pointer(dev, 0)) == NULL) {
199 syslog(LOG_ERR, "libusb20_tr_get_pointer() failed");
203 if (libusb20_tr_open(bulk, ATH3KFW_MAX_BSIZE, 1, ATH3KFW_BULK_EP) != 0) {
204 syslog(LOG_ERR, "libusb20_tr_open(%d, 1, %d) failed",
205 ATH3KFW_MAX_BSIZE, ATH3KFW_BULK_EP);
209 if ((fd = open(firmware, O_RDONLY)) < 0) {
210 syslog(LOG_ERR, "open(%s) failed. %s",
211 firmware, strerror(errno));
215 n = read(fd, buf, 20);
217 syslog(LOG_ERR, "read(%s, 20) failed. %s",
218 firmware, strerror(errno));
222 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
223 req.bmRequestType = LIBUSB20_REQUEST_TYPE_VENDOR;
224 req.bRequest = ATH3KFW_REQ_DFU_DNLOAD;
227 if (libusb20_dev_request_sync(dev, &req, buf, NULL, 5000, 0) != 0) {
228 syslog(LOG_ERR, "libusb20_dev_request_sync() failed");
233 n = read(fd, buf, sizeof(buf));
235 syslog(LOG_ERR, "read(%s, %d) failed. %s",
236 firmware, (int) sizeof(buf), strerror(errno));
242 libusb20_tr_setup_bulk(bulk, buf, n, 3000);
243 libusb20_tr_start(bulk);
245 while (libusb20_dev_process(dev) == 0) {
246 if (libusb20_tr_pending(bulk) == 0)
249 libusb20_dev_wait_process(dev, -1);
252 if (libusb20_tr_get_status(bulk) != 0) {
253 syslog(LOG_ERR, "bulk transfer failed with status %d",
254 libusb20_tr_get_status(bulk));
263 libusb20_tr_close(bulk);
265 libusb20_dev_close(dev);
271 * Display usage and exit
278 "Usage: %s -d ugenX.Y -f firmware_file\n"
281 "\t-d ugenX.Y ugen device name\n" \
282 "\t-f firmware image firmware image file name for download\n" \
283 "\t-h display this message\n", ATH3KFW, ATH3KFW);