xref: /freebsd/usr.sbin/bluetooth/bcmfw/bcmfw.c (revision 4eb513daccd08c08864dcac2966b7ef919c4248a)
1 /*
2  * bcmfw.c
3  *
4  * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: bcmfw.c,v 1.4 2003/04/27 19:28:09 max Exp $
29  * $FreeBSD$
30  *
31  * Based on Linux BlueZ BlueFW-0.9 package
32  *
33  */
34 
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbdevs.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <netgraph.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
48 
49 #define BCMFW		"bcmfw"
50 #define BCMFW_INTR_EP	1
51 #define BCMFW_BULK_EP	2
52 #define BCMFW_BSIZE	4096
53 
54 static int bcmfw_check_device
55 	(char const *name);
56 static int bcmfw_load_firmware
57 	(char const *name, char const *md, char const *fw);
58 static void bcmfw_usage
59 	(void);
60 
61 /*
62  * Main
63  */
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	char	*name = NULL, *md = NULL, *fw = NULL;
69 	int	 x;
70 
71 	while ((x = getopt(argc, argv, "f:hn:m:")) != -1) {
72 		switch (x) {
73 		case 'f': /* firmware file */
74 			fw = optarg;
75 			break;
76 
77 		case 'n': /* name */
78 			name = optarg;
79 			break;
80 
81 		case 'm': /* Mini-driver */
82 			md = optarg;
83 			break;
84 
85 		case 'h':
86 		default:
87 			bcmfw_usage();
88 			/* NOT REACHED */
89 		}
90 	}
91 
92 	if (name == NULL || md == NULL || fw == NULL)
93 		bcmfw_usage();
94 		/* NOT REACHED */
95 
96 	openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
97 
98 	if (bcmfw_check_device(name) < 0)
99 		exit(1);
100 
101 	if (bcmfw_load_firmware(name, md, fw) < 0)
102 		exit(1);
103 
104 	closelog();
105 
106 	return (0);
107 } /* main */
108 
109 /*
110  * Check device VendorID/ProductID
111  */
112 
113 static int
114 bcmfw_check_device(char const *name)
115 {
116 	usb_device_descriptor_t	desc;
117 	char			path[BCMFW_BSIZE];
118 	int			fd = -1, error = -1;
119 
120 	snprintf(path, sizeof(path), "/dev/%s", name);
121 
122 	if ((fd = open(path, O_WRONLY)) < 0) {
123 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
124 				path, strerror(errno), errno);
125 		goto out;
126 	}
127 
128 	if (ioctl(fd, USB_GET_DEVICE_DESC, &desc) < 0) {
129 		syslog(LOG_ERR, "Could not ioctl(%d, %ld, %p). %s (%d)",
130 				fd, USB_GET_DEVICE_DESC, &desc,
131 				strerror(errno), errno);
132 		goto out;
133 	}
134 
135 	if (UGETW(desc.idVendor) != USB_VENDOR_BROADCOM ||
136 	    UGETW(desc.idProduct) != 0x2033) {
137 		syslog(LOG_ERR, "Unsupported device, VendorID=%#x, " \
138 				"ProductID=%#x", UGETW(desc.idVendor),
139 				UGETW(desc.idProduct));
140 		error = -1;
141 	} else
142 		error = 0;
143 out:
144 	if (fd != -1)
145 		close(fd);
146 
147 	return (error);
148 } /* bcmfw_check_device */
149 
150 /*
151  * Download minidriver and firmware
152  */
153 
154 static int
155 bcmfw_load_firmware(char const *name, char const *md, char const *fw)
156 {
157 	char	buf[BCMFW_BSIZE];
158 	int	intr = -1, bulk = -1, fd = -1, error = -1, len;
159 
160 	/* Open interrupt endpoint device */
161 	snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_INTR_EP);
162 	if ((intr = open(buf, O_RDONLY)) < 0) {
163 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
164 				buf, strerror(errno), errno);
165 		goto out;
166 	}
167 
168 	/* Open bulk endpoint device */
169 	snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_BULK_EP);
170 	if ((bulk = open(buf, O_WRONLY)) < 0) {
171 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
172 				buf, strerror(errno), errno);
173 		goto out;
174 	}
175 
176 	/*
177 	 * Load mini-driver
178 	 */
179 
180 	if ((fd = open(md, O_RDONLY)) < 0) {
181 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
182 				md, strerror(errno), errno);
183 		goto out;
184 	}
185 
186 	for (;;) {
187 		len = read(fd, buf, sizeof(buf));
188 		if (len < 0) {
189 			syslog(LOG_ERR, "Could not read(%s). %s (%d)",
190 					md, strerror(errno), errno);
191 			goto out;
192 		}
193 		if (len == 0)
194 			break;
195 
196 		len = write(bulk, buf, len);
197 		if (len < 0) {
198 			syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
199 					name, BCMFW_BULK_EP, strerror(errno),
200 					errno);
201 			goto out;
202 		}
203 	}
204 
205 	close(fd);
206 	fd = -1;
207 
208 	usleep(10);
209 
210 	/*
211 	 * Memory select
212 	 */
213 
214 	if (write(bulk, "#", 1) < 0) {
215 		syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
216 				name, BCMFW_BULK_EP, strerror(errno), errno);
217 		goto out;
218 	}
219 
220 	if (read(intr, buf, sizeof(buf)) < 0) {
221 		syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
222 				name, BCMFW_INTR_EP, strerror(errno), errno);
223 		goto out;
224 	}
225 
226 	if (buf[0] != '#') {
227 		syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]);
228 		goto out;
229 	}
230 
231 	/*
232 	 * Load firmware
233 	 */
234 
235 	if ((fd = open(fw, O_RDONLY)) < 0) {
236 		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
237 				fw, strerror(errno), errno);
238 		goto out;
239 	}
240 
241 	for (;;) {
242 		len = read(fd, buf, sizeof(buf));
243 		if (len < 0) {
244 			syslog(LOG_ERR, "Could not read(%s). %s (%d)",
245 					fw, strerror(errno), errno);
246 			goto out;
247 		}
248 		if (len == 0)
249 			break;
250 
251 		len = write(bulk, buf, len);
252 		if (len < 0) {
253 			syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
254 					name, BCMFW_BULK_EP, strerror(errno),
255 					errno);
256 			goto out;
257 		}
258 	}
259 
260 	close(fd);
261 	fd = -1;
262 
263 	if (read(intr, buf, sizeof(buf)) < 0) {
264 		syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
265 				name, BCMFW_INTR_EP, strerror(errno), errno);
266 		goto out;
267 	}
268 
269 	if (buf[0] != '.') {
270 		syslog(LOG_ERR, "%s: Could not load firmware (%c)",
271 				name, buf[0]);
272 		goto out;
273 	}
274 
275 	usleep(500000);
276 	error = 0;
277 out:
278 	if (fd != -1)
279 		close(fd);
280 	if (bulk != -1)
281 		close(bulk);
282 	if (intr != -1)
283 		close(intr);
284 
285 	return (error);
286 } /* bcmfw_load_firmware */
287 
288 /*
289  * Display usage message and quit
290  */
291 
292 static void
293 bcmfw_usage(void)
294 {
295 	fprintf(stdout,
296 "Usage: %s -n name -m md_file -f fw_file\n"
297 "Where:\n" \
298 "\t-n name              device name\n" \
299 "\t-m mini-driver       image mini-driver image file name for download\n" \
300 "\t-f firmware image    firmware image file name for download\n" \
301 "\t-h                   display this message\n", BCMFW);
302 
303 	exit(255);
304 } /* bcmfw_usage */
305 
306