summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bluetooth/bcmfw/bcmfw.c
blob: e21c10ca35aca796f436d4a1bdc0cd5560f54f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * bcmfw.c
 *
 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: bcmfw.c,v 1.4 2003/04/27 19:28:09 max Exp $
 * $FreeBSD$
 *
 * Based on Linux BlueZ BlueFW-0.9 package
 *
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdevs.h>
#include <errno.h>
#include <fcntl.h>
#include <netgraph.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#define BCMFW		"bcmfw"
#define BCMFW_INTR_EP	1
#define BCMFW_BULK_EP	2
#define BCMFW_BSIZE	4096

static int bcmfw_check_device
	(char const *name);
static int bcmfw_load_firmware
	(char const *name, char const *md, char const *fw);
static void bcmfw_usage
	(void);

/*
 * Main
 */

int
main(int argc, char *argv[])
{
	char	*name = NULL, *md = NULL, *fw = NULL;
	int	 x;

	while ((x = getopt(argc, argv, "f:hn:m:")) != -1) {
		switch (x) {
		case 'f': /* firmware file */
			fw = optarg;
			break;

		case 'n': /* name */
			name = optarg;
			break;

		case 'm': /* Mini-driver */
			md = optarg;
			break;

		case 'h':
		default:
			bcmfw_usage();
			/* NOT REACHED */
		}
	}

	if (name == NULL || md == NULL || fw == NULL)
		bcmfw_usage();
		/* NOT REACHED */

	openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);

	if (bcmfw_check_device(name) < 0)
		exit(1);

	if (bcmfw_load_firmware(name, md, fw) < 0)
		exit(1);

	closelog();

	return (0);
} /* main */

/*
 * Check device VendorID/ProductID
 */

static int
bcmfw_check_device(char const *name)
{
	usb_device_descriptor_t	desc;
	char			path[BCMFW_BSIZE];
	int			fd = -1, error = -1;

	snprintf(path, sizeof(path), "/dev/%s", name);

	if ((fd = open(path, O_WRONLY)) < 0) {
		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
				path, strerror(errno), errno);
		goto out;
	}

	if (ioctl(fd, USB_GET_DEVICE_DESC, &desc) < 0) {
		syslog(LOG_ERR, "Could not ioctl(%d, %ld, %p). %s (%d)",
				fd, USB_GET_DEVICE_DESC, &desc,
				strerror(errno), errno);
		goto out;
	}

	if (UGETW(desc.idVendor) != USB_VENDOR_BROADCOM ||
	    UGETW(desc.idProduct) != 0x2033) {
		syslog(LOG_ERR, "Unsupported device, VendorID=%#x, " \
				"ProductID=%#x", UGETW(desc.idVendor),
				UGETW(desc.idProduct));
		error = -1;
	} else
		error = 0;
out:
	if (fd != -1)
		close(fd);

	return (error);
} /* bcmfw_check_device */

/*
 * Download minidriver and firmware
 */

static int
bcmfw_load_firmware(char const *name, char const *md, char const *fw)
{
	char	buf[BCMFW_BSIZE];
	int	intr = -1, bulk = -1, fd = -1, error = -1, len;

	/* Open interrupt endpoint device */
	snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_INTR_EP);
	if ((intr = open(buf, O_RDONLY)) < 0) {
		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
				buf, strerror(errno), errno);
		goto out;
	}

	/* Open bulk endpoint device */
	snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_BULK_EP);
	if ((bulk = open(buf, O_WRONLY)) < 0) {
		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
				buf, strerror(errno), errno);
		goto out;
	}

	/* 
	 * Load mini-driver 
	 */

	if ((fd = open(md, O_RDONLY)) < 0) {
		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
				md, strerror(errno), errno);
		goto out;
	}

	for (;;) {
		len = read(fd, buf, sizeof(buf));
		if (len < 0) {
			syslog(LOG_ERR, "Could not read(%s). %s (%d)",
					md, strerror(errno), errno);
			goto out;
		}
		if (len == 0)
			break;

		len = write(bulk, buf, len);
		if (len < 0) {
			syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
					name, BCMFW_BULK_EP, strerror(errno),
					errno);
			goto out;
		}
	}

	close(fd);
	fd = -1;

	usleep(10);

	/* 
	 * Memory select 
	 */

	if (write(bulk, "#", 1) < 0) {
		syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
				name, BCMFW_BULK_EP, strerror(errno), errno);
		goto out;
	}

	if (read(intr, buf, sizeof(buf)) < 0) {
		syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
				name, BCMFW_INTR_EP, strerror(errno), errno);
		goto out;
	}

	if (buf[0] != '#') {
		syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]);
		goto out;
	}

	/*
	 * Load firmware
	 */

	if ((fd = open(fw, O_RDONLY)) < 0) {
		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
				fw, strerror(errno), errno);
		goto out;
	}

	for (;;) {
		len = read(fd, buf, sizeof(buf));
		if (len < 0) {
			syslog(LOG_ERR, "Could not read(%s). %s (%d)",
					fw, strerror(errno), errno);
			goto out;
		}
		if (len == 0)
			break;

		len = write(bulk, buf, len);
		if (len < 0) {
			syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
					name, BCMFW_BULK_EP, strerror(errno),
					errno);
			goto out;
		}
	}

	close(fd);
	fd = -1;

	if (read(intr, buf, sizeof(buf)) < 0) {
		syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
				name, BCMFW_INTR_EP, strerror(errno), errno);
		goto out;
	}

	if (buf[0] != '.') {
		syslog(LOG_ERR, "%s: Could not load firmware (%c)",
				name, buf[0]);
		goto out;
	}

	usleep(500000);
	error = 0;
out:
	if (fd != -1)
		close(fd);
	if (bulk != -1)
		close(bulk);
	if (intr != -1)
		close(intr);

	return (error);
} /* bcmfw_load_firmware */

/*
 * Display usage message and quit
 */

static void 
bcmfw_usage(void)
{
	fprintf(stdout,
"Usage: %s -n name -m md_file -f fw_file\n"
"Where:\n" \
"\t-n name              device name\n" \
"\t-m mini-driver       image mini-driver image file name for download\n" \
"\t-f firmware image    firmware image file name for download\n" \
"\t-h                   display this message\n", BCMFW);

	exit(255);
} /* bcmfw_usage */

OpenPOWER on IntegriCloud