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