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);
62 static int vendor_id = ATH3KFW_VENDOR_ID;
63 static int product_id = ATH3KFW_PRODUCT_ID;
66 * Firmware downloader for Atheros AR3011 based USB Bluetooth devices
70 main(int argc, char **argv)
74 struct libusb20_backend *be;
75 struct libusb20_device *dev;
78 openlog(ATH3KFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
82 firmware = ATH3KFW_FW;
84 while ((n = getopt(argc, argv, "d:f:hp:v:")) != -1) {
86 case 'd': /* ugen device name */
87 if (parse_ugen_name(optarg, &bus, &addr) < 0)
91 case 'f': /* firmware file */
94 case 'p': /* product id */
95 product_id = strtol(optarg, NULL, 0);
97 case 'v': /* vendor id */
98 vendor_id = strtol(optarg, NULL, 0);
108 be = libusb20_be_alloc_default();
110 syslog(LOG_ERR, "libusb20_be_alloc_default() failed");
114 if (find_device(be, bus, addr, &dev) < 0) {
115 syslog(LOG_ERR, "ugen%d.%d is not recognized as " \
116 "Atheros AR3011 based device " \
117 "(possibly caused by lack of permissions)", bus, addr);
121 if (download_firmware(dev, firmware) < 0) {
122 syslog(LOG_ERR, "could not download %s firmare to ugen%d.%d",
123 firmware, bus, addr);
127 libusb20_be_free(be);
134 * Parse ugen name and extract device's bus and address
138 parse_ugen_name(char const *ugen, uint8_t *bus, uint8_t *addr)
142 if (strncmp(ugen, "ugen", 4) != 0)
145 *bus = (uint8_t) strtoul(ugen + 4, &ep, 10);
149 *addr = (uint8_t) strtoul(ep + 1, &ep, 10);
161 find_device(struct libusb20_backend *be, uint8_t bus, uint8_t addr,
162 struct libusb20_device **dev)
164 struct LIBUSB20_DEVICE_DESC_DECODED *desc;
168 while ((*dev = libusb20_be_device_foreach(be, *dev)) != NULL) {
169 if (libusb20_dev_get_bus_number(*dev) != bus ||
170 libusb20_dev_get_address(*dev) != addr)
173 desc = libusb20_dev_get_device_desc(*dev);
177 if (desc->idVendor != vendor_id ||
178 desc->idProduct != product_id)
184 return ((*dev == NULL)? -1 : 0);
192 download_firmware(struct libusb20_device *dev, char const *firmware)
194 struct libusb20_transfer *bulk;
195 struct LIBUSB20_CONTROL_SETUP_DECODED req;
197 uint8_t buf[ATH3KFW_MAX_BSIZE];
201 if (libusb20_dev_open(dev, 1) != 0) {
202 syslog(LOG_ERR, "libusb20_dev_open() failed");
206 if ((bulk = libusb20_tr_get_pointer(dev, 0)) == NULL) {
207 syslog(LOG_ERR, "libusb20_tr_get_pointer() failed");
211 if (libusb20_tr_open(bulk, ATH3KFW_MAX_BSIZE, 1, ATH3KFW_BULK_EP) != 0) {
212 syslog(LOG_ERR, "libusb20_tr_open(%d, 1, %d) failed",
213 ATH3KFW_MAX_BSIZE, ATH3KFW_BULK_EP);
217 if ((fd = open(firmware, O_RDONLY)) < 0) {
218 syslog(LOG_ERR, "open(%s) failed. %s",
219 firmware, strerror(errno));
223 n = read(fd, buf, 20);
225 syslog(LOG_ERR, "read(%s, 20) failed. %s",
226 firmware, strerror(errno));
230 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
231 req.bmRequestType = LIBUSB20_REQUEST_TYPE_VENDOR;
232 req.bRequest = ATH3KFW_REQ_DFU_DNLOAD;
235 if (libusb20_dev_request_sync(dev, &req, buf, NULL, 5000, 0) != 0) {
236 syslog(LOG_ERR, "libusb20_dev_request_sync() failed");
241 n = read(fd, buf, sizeof(buf));
243 syslog(LOG_ERR, "read(%s, %d) failed. %s",
244 firmware, (int) sizeof(buf), strerror(errno));
250 libusb20_tr_setup_bulk(bulk, buf, n, 3000);
251 libusb20_tr_start(bulk);
253 while (libusb20_dev_process(dev) == 0) {
254 if (libusb20_tr_pending(bulk) == 0)
257 libusb20_dev_wait_process(dev, -1);
260 if (libusb20_tr_get_status(bulk) != 0) {
261 syslog(LOG_ERR, "bulk transfer failed with status %d",
262 libusb20_tr_get_status(bulk));
271 libusb20_tr_close(bulk);
273 libusb20_dev_close(dev);
279 * Display usage and exit
286 "Usage: %s -d ugenX.Y -f firmware_file\n"
289 "\t-d ugenX.Y ugen device name\n" \
290 "\t-f firmware image firmware image file name for download\n" \
291 "\t-v vendor_id vendor id\n" \
292 "\t-p vendor_id product id\n" \
293 "\t-h display this message\n", ATH3KFW, ATH3KFW);