xref: /freebsd/usr.sbin/bluetooth/bcmfw/bcmfw.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * bcmfw.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2003 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  * $Id: bcmfw.c,v 1.4 2003/04/27 19:28:09 max Exp $
31  * $FreeBSD$
32  *
33  * Based on Linux BlueZ BlueFW-0.9 package
34  *
35  */
36 
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usb_ioctl.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <netgraph.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <unistd.h>
50 
51 #define BCMFW		"bcmfw"
52 #define BCMFW_INTR_EP	1
53 #define BCMFW_BULK_EP	2
54 #define BCMFW_BSIZE	4096
55 
56 #define USB_VENDOR_BROADCOM 0x0a5c
57 #define USB_PRODUCT_BROADCOM_BCM2033 0x2033
58 
59 static int bcmfw_check_device
60 	(char const *name);
61 static int bcmfw_load_firmware
62 	(char const *name, char const *md, char const *fw);
63 static void bcmfw_usage
64 	(void);
65 
66 /*
67  * Main
68  */
69 
70 int
71 main(int argc, char *argv[])
72 {
73 	char	*name = NULL, *md = NULL, *fw = NULL;
74 	int	 x;
75 
76 	while ((x = getopt(argc, argv, "f:hn:m:")) != -1) {
77 		switch (x) {
78 		case 'f': /* firmware file */
79 			fw = optarg;
80 			break;
81 
82 		case 'n': /* name */
83 			name = optarg;
84 			break;
85 
86 		case 'm': /* Mini-driver */
87 			md = optarg;
88 			break;
89 
90 		case 'h':
91 		default:
92 			bcmfw_usage();
93 			/* NOT REACHED */
94 		}
95 	}
96 
97 	if (name == NULL || md == NULL || fw == NULL)
98 		bcmfw_usage();
99 		/* NOT REACHED */
100 
101 	openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
102 
103 	if (bcmfw_check_device(name) < 0)
104 		exit(1);
105 
106 	if (bcmfw_load_firmware(name, md, fw) < 0)
107 		exit(1);
108 
109 	closelog();
110 
111 	return (0);
112 } /* main */
113 
114 /*
115  * Check device VendorID/ProductID
116  */
117 
118 static int
119 bcmfw_check_device(char const *name)
120 {
121 	usb_device_descriptor_t	desc;
122 	char			path[BCMFW_BSIZE];
123 	int			fd = -1, error = -1;
124 
125 	snprintf(path, sizeof(path), "/dev/%s", name);
126 
127 	if ((fd = open(path, O_WRONLY)) < 0) {
128 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
129 				path, strerror(errno), errno);
130 		goto out;
131 	}
132 
133 	if (ioctl(fd, USB_GET_DEVICE_DESC, &desc) < 0) {
134 		syslog(LOG_ERR, "Could not ioctl(%d, %ld, %p). %s (%d)",
135 				fd, USB_GET_DEVICE_DESC, &desc,
136 				strerror(errno), errno);
137 		goto out;
138 	}
139 
140 	if (UGETW(desc.idVendor) != USB_VENDOR_BROADCOM ||
141 	    UGETW(desc.idProduct) != USB_PRODUCT_BROADCOM_BCM2033) {
142 		syslog(LOG_ERR, "Unsupported device, VendorID=%#x, " \
143 				"ProductID=%#x", UGETW(desc.idVendor),
144 				UGETW(desc.idProduct));
145 		error = -1;
146 	} else
147 		error = 0;
148 out:
149 	if (fd != -1)
150 		close(fd);
151 
152 	return (error);
153 } /* bcmfw_check_device */
154 
155 /*
156  * Download minidriver and firmware
157  */
158 
159 static int
160 bcmfw_load_firmware(char const *name, char const *md, char const *fw)
161 {
162 	char	buf[BCMFW_BSIZE];
163 	int	intr = -1, bulk = -1, fd = -1, error = -1, len;
164 
165 	/* Open interrupt endpoint device */
166 	snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_INTR_EP);
167 	if ((intr = open(buf, O_RDONLY)) < 0) {
168 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
169 				buf, strerror(errno), errno);
170 		goto out;
171 	}
172 
173 	/* Open bulk endpoint device */
174 	snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_BULK_EP);
175 	if ((bulk = open(buf, O_WRONLY)) < 0) {
176 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
177 				buf, strerror(errno), errno);
178 		goto out;
179 	}
180 
181 	/*
182 	 * Load mini-driver
183 	 */
184 
185 	if ((fd = open(md, O_RDONLY)) < 0) {
186 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
187 				md, strerror(errno), errno);
188 		goto out;
189 	}
190 
191 	for (;;) {
192 		len = read(fd, buf, sizeof(buf));
193 		if (len < 0) {
194 			syslog(LOG_ERR, "Could not read(%s). %s (%d)",
195 					md, strerror(errno), errno);
196 			goto out;
197 		}
198 		if (len == 0)
199 			break;
200 
201 		len = write(bulk, buf, len);
202 		if (len < 0) {
203 			syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
204 					name, BCMFW_BULK_EP, strerror(errno),
205 					errno);
206 			goto out;
207 		}
208 	}
209 
210 	close(fd);
211 	fd = -1;
212 
213 	usleep(10);
214 
215 	/*
216 	 * Memory select
217 	 */
218 
219 	if (write(bulk, "#", 1) < 0) {
220 		syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
221 				name, BCMFW_BULK_EP, strerror(errno), errno);
222 		goto out;
223 	}
224 
225 	if (read(intr, buf, sizeof(buf)) < 0) {
226 		syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
227 				name, BCMFW_INTR_EP, strerror(errno), errno);
228 		goto out;
229 	}
230 
231 	if (buf[0] != '#') {
232 		syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]);
233 		goto out;
234 	}
235 
236 	/*
237 	 * Load firmware
238 	 */
239 
240 	if ((fd = open(fw, O_RDONLY)) < 0) {
241 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
242 				fw, strerror(errno), errno);
243 		goto out;
244 	}
245 
246 	for (;;) {
247 		len = read(fd, buf, sizeof(buf));
248 		if (len < 0) {
249 			syslog(LOG_ERR, "Could not read(%s). %s (%d)",
250 					fw, strerror(errno), errno);
251 			goto out;
252 		}
253 		if (len == 0)
254 			break;
255 
256 		len = write(bulk, buf, len);
257 		if (len < 0) {
258 			syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
259 					name, BCMFW_BULK_EP, strerror(errno),
260 					errno);
261 			goto out;
262 		}
263 	}
264 
265 	close(fd);
266 	fd = -1;
267 
268 	if (read(intr, buf, sizeof(buf)) < 0) {
269 		syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
270 				name, BCMFW_INTR_EP, strerror(errno), errno);
271 		goto out;
272 	}
273 
274 	if (buf[0] != '.') {
275 		syslog(LOG_ERR, "%s: Could not load firmware (%c)",
276 				name, buf[0]);
277 		goto out;
278 	}
279 
280 	usleep(500000);
281 	error = 0;
282 out:
283 	if (fd != -1)
284 		close(fd);
285 	if (bulk != -1)
286 		close(bulk);
287 	if (intr != -1)
288 		close(intr);
289 
290 	return (error);
291 } /* bcmfw_load_firmware */
292 
293 /*
294  * Display usage message and quit
295  */
296 
297 static void
298 bcmfw_usage(void)
299 {
300 	fprintf(stdout,
301 "Usage: %s -n name -m md_file -f fw_file\n"
302 "Where:\n" \
303 "\t-n name              device name\n" \
304 "\t-m mini-driver       image mini-driver image file name for download\n" \
305 "\t-f firmware image    firmware image file name for download\n" \
306 "\t-h                   display this message\n", BCMFW);
307 
308 	exit(255);
309 } /* bcmfw_usage */
310 
311