1 /*
2  * ath3kfw.c
3  */
4 
5 /*-
6  * Copyright (c) 2010 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD: stable/10/usr.sbin/bluetooth/ath3kfw/ath3kfw.c 249179 2013-04-05 23:42:22Z adrian $
31  */
32 
33 #include <sys/types.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <libusb20_desc.h>
37 #include <libusb20.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
44 
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
52 
53 static int	parse_ugen_name		(char const *ugen, uint8_t *bus,
54 					 uint8_t *addr);
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);
61 
62 static int			vendor_id = ATH3KFW_VENDOR_ID;
63 static int			product_id = ATH3KFW_PRODUCT_ID;
64 
65 /*
66  * Firmware downloader for Atheros AR3011 based USB Bluetooth devices
67  */
68 
69 int
main(int argc,char ** argv)70 main(int argc, char **argv)
71 {
72 	uint8_t			bus, addr;
73 	char const		*firmware;
74 	struct libusb20_backend	*be;
75 	struct libusb20_device	*dev;
76 	int			n;
77 
78 	openlog(ATH3KFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
79 
80 	bus = 0;
81 	addr = 0;
82 	firmware = ATH3KFW_FW;
83 
84 	while ((n = getopt(argc, argv, "d:f:hp:v:")) != -1) {
85 		switch (n) {
86 		case 'd': /* ugen device name */
87 			if (parse_ugen_name(optarg, &bus, &addr) < 0)
88 				usage();
89 			break;
90 
91 		case 'f': /* firmware file */
92 			firmware = optarg;
93 			break;
94 		case 'p': /* product id */
95 			product_id = strtol(optarg, NULL, 0);
96 			break;
97 		case 'v': /* vendor id */
98 			vendor_id = strtol(optarg, NULL, 0);
99 			break;
100 		case 'h':
101 		default:
102 			usage();
103 			break;
104 			/* NOT REACHED */
105 		}
106 	}
107 
108 	be = libusb20_be_alloc_default();
109 	if (be == NULL) {
110 		syslog(LOG_ERR, "libusb20_be_alloc_default() failed");
111 		return (-1);
112 	}
113 
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);
118 		return (-1);
119 	}
120 
121 	if (download_firmware(dev, firmware) < 0) {
122 		syslog(LOG_ERR, "could not download %s firmare to ugen%d.%d",
123 			firmware, bus, addr);
124 		return (-1);
125 	}
126 
127 	libusb20_be_free(be);
128 	closelog();
129 
130 	return (0);
131 }
132 
133 /*
134  * Parse ugen name and extract device's bus and address
135  */
136 
137 static int
parse_ugen_name(char const * ugen,uint8_t * bus,uint8_t * addr)138 parse_ugen_name(char const *ugen, uint8_t *bus, uint8_t *addr)
139 {
140 	char	*ep;
141 
142 	if (strncmp(ugen, "ugen", 4) != 0)
143 		return (-1);
144 
145 	*bus = (uint8_t) strtoul(ugen + 4, &ep, 10);
146 	if (*ep != '.')
147 		return (-1);
148 
149 	*addr = (uint8_t) strtoul(ep + 1, &ep, 10);
150 	if (*ep != '\0')
151 		return (-1);
152 
153 	return (0);
154 }
155 
156 /*
157  * Find USB device
158  */
159 
160 static int
find_device(struct libusb20_backend * be,uint8_t bus,uint8_t addr,struct libusb20_device ** dev)161 find_device(struct libusb20_backend *be, uint8_t bus, uint8_t addr,
162 		struct libusb20_device **dev)
163 {
164 	struct LIBUSB20_DEVICE_DESC_DECODED	*desc;
165 
166 	*dev = NULL;
167 
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)
171 			continue;
172 
173 		desc = libusb20_dev_get_device_desc(*dev);
174 		if (desc == NULL)
175 			continue;
176 
177 		if (desc->idVendor != vendor_id ||
178 		    desc->idProduct != product_id)
179 			continue;
180 
181 		break;
182 	}
183 
184 	return ((*dev == NULL)? -1 : 0);
185 }
186 
187 /*
188  * Download firmware
189  */
190 
191 static int
download_firmware(struct libusb20_device * dev,char const * firmware)192 download_firmware(struct libusb20_device *dev, char const *firmware)
193 {
194 	struct libusb20_transfer		*bulk;
195 	struct LIBUSB20_CONTROL_SETUP_DECODED	req;
196 	int					fd, n, error;
197 	uint8_t					buf[ATH3KFW_MAX_BSIZE];
198 
199 	error = -1;
200 
201 	if (libusb20_dev_open(dev, 1) != 0) {
202 		syslog(LOG_ERR, "libusb20_dev_open() failed");
203 		return (error);
204 	}
205 
206 	if ((bulk = libusb20_tr_get_pointer(dev, 0)) == NULL) {
207 		syslog(LOG_ERR, "libusb20_tr_get_pointer() failed");
208 		goto out;
209 	}
210 
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);
214 		goto out;
215 	}
216 
217 	if ((fd = open(firmware, O_RDONLY)) < 0) {
218 		syslog(LOG_ERR, "open(%s) failed. %s",
219 			firmware, strerror(errno));
220 		goto out1;
221 	}
222 
223 	n = read(fd, buf, 20);
224 	if (n != 20) {
225 		syslog(LOG_ERR, "read(%s, 20) failed. %s",
226 			firmware, strerror(errno));
227 		goto out2;
228 	}
229 
230 	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
231 	req.bmRequestType = LIBUSB20_REQUEST_TYPE_VENDOR;
232 	req.bRequest = ATH3KFW_REQ_DFU_DNLOAD;
233 	req.wLength = 20;
234 
235 	if (libusb20_dev_request_sync(dev, &req, buf, NULL, 5000, 0) != 0) {
236 		syslog(LOG_ERR, "libusb20_dev_request_sync() failed");
237 		goto out2;
238 	}
239 
240 	for (;;) {
241 		n = read(fd, buf, sizeof(buf));
242 		if (n < 0) {
243 			syslog(LOG_ERR, "read(%s, %d) failed. %s",
244 				firmware, (int) sizeof(buf), strerror(errno));
245 			goto out2;
246 		}
247 		if (n == 0)
248 			break;
249 
250 		libusb20_tr_setup_bulk(bulk, buf, n, 3000);
251 		libusb20_tr_start(bulk);
252 
253 		while (libusb20_dev_process(dev) == 0) {
254 			if (libusb20_tr_pending(bulk) == 0)
255 				break;
256 
257 			libusb20_dev_wait_process(dev, -1);
258 		}
259 
260 		if (libusb20_tr_get_status(bulk) != 0) {
261 			syslog(LOG_ERR, "bulk transfer failed with status %d",
262 				libusb20_tr_get_status(bulk));
263 			goto out2;
264 		}
265 	}
266 
267 	error = 0;
268 out2:
269 	close(fd);
270 out1:
271 	libusb20_tr_close(bulk);
272 out:
273 	libusb20_dev_close(dev);
274 
275 	return (error);
276 }
277 
278 /*
279  * Display usage and exit
280  */
281 
282 static void
usage(void)283 usage(void)
284 {
285 	printf(
286 "Usage: %s -d ugenX.Y -f firmware_file\n"
287 "Usage: %s -h\n" \
288 "Where:\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);
294 
295         exit(255);
296 }
297 
298