4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: bcmfw.c,v 1.4 2003/04/27 19:28:09 max Exp $
33 * Based on Linux BlueZ BlueFW-0.9 package
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usb_ioctl.h>
52 #define BCMFW_INTR_EP 1
53 #define BCMFW_BULK_EP 2
54 #define BCMFW_BSIZE 4096
56 #define USB_VENDOR_BROADCOM 0x0a5c
57 #define USB_PRODUCT_BROADCOM_BCM2033 0x2033
59 static int bcmfw_check_device
61 static int bcmfw_load_firmware
62 (char const *name, char const *md, char const *fw);
63 static void bcmfw_usage
71 main(int argc, char *argv[])
73 char *name = NULL, *md = NULL, *fw = NULL;
76 while ((x = getopt(argc, argv, "f:hn:m:")) != -1) {
78 case 'f': /* firmware file */
86 case 'm': /* Mini-driver */
97 if (name == NULL || md == NULL || fw == NULL)
101 openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
103 if (bcmfw_check_device(name) < 0)
106 if (bcmfw_load_firmware(name, md, fw) < 0)
115 * Check device VendorID/ProductID
119 bcmfw_check_device(char const *name)
121 usb_device_descriptor_t desc;
122 char path[BCMFW_BSIZE];
123 int fd = -1, error = -1;
125 snprintf(path, sizeof(path), "/dev/%s", name);
127 if ((fd = open(path, O_WRONLY)) < 0) {
128 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
129 path, strerror(errno), errno);
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);
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));
153 } /* bcmfw_check_device */
156 * Download minidriver and firmware
160 bcmfw_load_firmware(char const *name, char const *md, char const *fw)
162 char buf[BCMFW_BSIZE];
163 int intr = -1, bulk = -1, fd = -1, error = -1, len;
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);
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);
185 if ((fd = open(md, O_RDONLY)) < 0) {
186 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
187 md, strerror(errno), errno);
192 len = read(fd, buf, sizeof(buf));
194 syslog(LOG_ERR, "Could not read(%s). %s (%d)",
195 md, strerror(errno), errno);
201 len = write(bulk, buf, len);
203 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
204 name, BCMFW_BULK_EP, strerror(errno),
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);
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);
232 syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]);
240 if ((fd = open(fw, O_RDONLY)) < 0) {
241 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
242 fw, strerror(errno), errno);
247 len = read(fd, buf, sizeof(buf));
249 syslog(LOG_ERR, "Could not read(%s). %s (%d)",
250 fw, strerror(errno), errno);
256 len = write(bulk, buf, len);
258 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
259 name, BCMFW_BULK_EP, strerror(errno),
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);
275 syslog(LOG_ERR, "%s: Could not load firmware (%c)",
291 } /* bcmfw_load_firmware */
294 * Display usage message and quit
301 "Usage: %s -n name -m md_file -f fw_file\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);