4 * SPDX-License-Identifier: BSD-2-Clause
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 $
32 * Based on Linux BlueZ BlueFW-0.9 package
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <dev/usb/usb.h>
39 #include <dev/usb/usb_ioctl.h>
51 #define BCMFW_INTR_EP 1
52 #define BCMFW_BULK_EP 2
53 #define BCMFW_BSIZE 4096
55 #define USB_VENDOR_BROADCOM 0x0a5c
56 #define USB_PRODUCT_BROADCOM_BCM2033 0x2033
58 static int bcmfw_check_device
60 static int bcmfw_load_firmware
61 (char const *name, char const *md, char const *fw);
62 static void bcmfw_usage
70 main(int argc, char *argv[])
72 char *name = NULL, *md = NULL, *fw = NULL;
75 while ((x = getopt(argc, argv, "f:hn:m:")) != -1) {
77 case 'f': /* firmware file */
85 case 'm': /* Mini-driver */
96 if (name == NULL || md == NULL || fw == NULL)
100 openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
102 if (bcmfw_check_device(name) < 0)
105 if (bcmfw_load_firmware(name, md, fw) < 0)
114 * Check device VendorID/ProductID
118 bcmfw_check_device(char const *name)
120 usb_device_descriptor_t desc;
121 char path[BCMFW_BSIZE];
122 int fd = -1, error = -1;
124 snprintf(path, sizeof(path), "/dev/%s", name);
126 if ((fd = open(path, O_WRONLY)) < 0) {
127 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
128 path, strerror(errno), errno);
132 if (ioctl(fd, USB_GET_DEVICE_DESC, &desc) < 0) {
133 syslog(LOG_ERR, "Could not ioctl(%d, %ld, %p). %s (%d)",
134 fd, USB_GET_DEVICE_DESC, &desc,
135 strerror(errno), errno);
139 if (UGETW(desc.idVendor) != USB_VENDOR_BROADCOM ||
140 UGETW(desc.idProduct) != USB_PRODUCT_BROADCOM_BCM2033) {
141 syslog(LOG_ERR, "Unsupported device, VendorID=%#x, " \
142 "ProductID=%#x", UGETW(desc.idVendor),
143 UGETW(desc.idProduct));
152 } /* bcmfw_check_device */
155 * Download minidriver and firmware
159 bcmfw_load_firmware(char const *name, char const *md, char const *fw)
161 char buf[BCMFW_BSIZE];
162 int intr = -1, bulk = -1, fd = -1, error = -1, len;
164 /* Open interrupt endpoint device */
165 snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_INTR_EP);
166 if ((intr = open(buf, O_RDONLY)) < 0) {
167 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
168 buf, strerror(errno), errno);
172 /* Open bulk endpoint device */
173 snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_BULK_EP);
174 if ((bulk = open(buf, O_WRONLY)) < 0) {
175 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
176 buf, strerror(errno), errno);
184 if ((fd = open(md, O_RDONLY)) < 0) {
185 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
186 md, strerror(errno), errno);
191 len = read(fd, buf, sizeof(buf));
193 syslog(LOG_ERR, "Could not read(%s). %s (%d)",
194 md, strerror(errno), errno);
200 len = write(bulk, buf, len);
202 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
203 name, BCMFW_BULK_EP, strerror(errno),
218 if (write(bulk, "#", 1) < 0) {
219 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
220 name, BCMFW_BULK_EP, strerror(errno), errno);
224 if (read(intr, buf, sizeof(buf)) < 0) {
225 syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
226 name, BCMFW_INTR_EP, strerror(errno), errno);
231 syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]);
239 if ((fd = open(fw, O_RDONLY)) < 0) {
240 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
241 fw, strerror(errno), errno);
246 len = read(fd, buf, sizeof(buf));
248 syslog(LOG_ERR, "Could not read(%s). %s (%d)",
249 fw, strerror(errno), errno);
255 len = write(bulk, buf, len);
257 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
258 name, BCMFW_BULK_EP, strerror(errno),
267 if (read(intr, buf, sizeof(buf)) < 0) {
268 syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
269 name, BCMFW_INTR_EP, strerror(errno), errno);
274 syslog(LOG_ERR, "%s: Could not load firmware (%c)",
290 } /* bcmfw_load_firmware */
293 * Display usage message and quit
300 "Usage: %s -n name -m md_file -f fw_file\n"
302 "\t-n name device name\n" \
303 "\t-m mini-driver image mini-driver image file name for download\n" \
304 "\t-f firmware image firmware image file name for download\n" \
305 "\t-h display this message\n", BCMFW);