4 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $Id: bcmfw.c,v 1.4 2003/04/27 19:28:09 max Exp $
31 * Based on Linux BlueZ BlueFW-0.9 package
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usb_ioctl.h>
50 #define BCMFW_INTR_EP 1
51 #define BCMFW_BULK_EP 2
52 #define BCMFW_BSIZE 4096
54 #define USB_VENDOR_BROADCOM 0x0a5c
55 #define USB_PRODUCT_BROADCOM_BCM2033 0x2033
57 static int bcmfw_check_device
59 static int bcmfw_load_firmware
60 (char const *name, char const *md, char const *fw);
61 static void bcmfw_usage
69 main(int argc, char *argv[])
71 char *name = NULL, *md = NULL, *fw = NULL;
74 while ((x = getopt(argc, argv, "f:hn:m:")) != -1) {
76 case 'f': /* firmware file */
84 case 'm': /* Mini-driver */
95 if (name == NULL || md == NULL || fw == NULL)
99 openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER);
101 if (bcmfw_check_device(name) < 0)
104 if (bcmfw_load_firmware(name, md, fw) < 0)
113 * Check device VendorID/ProductID
117 bcmfw_check_device(char const *name)
119 usb_device_descriptor_t desc;
120 char path[BCMFW_BSIZE];
121 int fd = -1, error = -1;
123 snprintf(path, sizeof(path), "/dev/%s", name);
125 if ((fd = open(path, O_WRONLY)) < 0) {
126 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
127 path, strerror(errno), errno);
131 if (ioctl(fd, USB_GET_DEVICE_DESC, &desc) < 0) {
132 syslog(LOG_ERR, "Could not ioctl(%d, %ld, %p). %s (%d)",
133 fd, USB_GET_DEVICE_DESC, &desc,
134 strerror(errno), errno);
138 if (UGETW(desc.idVendor) != USB_VENDOR_BROADCOM ||
139 UGETW(desc.idProduct) != USB_PRODUCT_BROADCOM_BCM2033) {
140 syslog(LOG_ERR, "Unsupported device, VendorID=%#x, " \
141 "ProductID=%#x", UGETW(desc.idVendor),
142 UGETW(desc.idProduct));
151 } /* bcmfw_check_device */
154 * Download minidriver and firmware
158 bcmfw_load_firmware(char const *name, char const *md, char const *fw)
160 char buf[BCMFW_BSIZE];
161 int intr = -1, bulk = -1, fd = -1, error = -1, len;
163 /* Open interrupt endpoint device */
164 snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_INTR_EP);
165 if ((intr = open(buf, O_RDONLY)) < 0) {
166 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
167 buf, strerror(errno), errno);
171 /* Open bulk endpoint device */
172 snprintf(buf, sizeof(buf), "/dev/%s.%d", name, BCMFW_BULK_EP);
173 if ((bulk = open(buf, O_WRONLY)) < 0) {
174 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
175 buf, strerror(errno), errno);
183 if ((fd = open(md, O_RDONLY)) < 0) {
184 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
185 md, strerror(errno), errno);
190 len = read(fd, buf, sizeof(buf));
192 syslog(LOG_ERR, "Could not read(%s). %s (%d)",
193 md, strerror(errno), errno);
199 len = write(bulk, buf, len);
201 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
202 name, BCMFW_BULK_EP, strerror(errno),
217 if (write(bulk, "#", 1) < 0) {
218 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
219 name, BCMFW_BULK_EP, strerror(errno), errno);
223 if (read(intr, buf, sizeof(buf)) < 0) {
224 syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
225 name, BCMFW_INTR_EP, strerror(errno), errno);
230 syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]);
238 if ((fd = open(fw, O_RDONLY)) < 0) {
239 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
240 fw, strerror(errno), errno);
245 len = read(fd, buf, sizeof(buf));
247 syslog(LOG_ERR, "Could not read(%s). %s (%d)",
248 fw, strerror(errno), errno);
254 len = write(bulk, buf, len);
256 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)",
257 name, BCMFW_BULK_EP, strerror(errno),
266 if (read(intr, buf, sizeof(buf)) < 0) {
267 syslog(LOG_ERR, "Could not read(/dev/%s.%d). %s (%d)",
268 name, BCMFW_INTR_EP, strerror(errno), errno);
273 syslog(LOG_ERR, "%s: Could not load firmware (%c)",
289 } /* bcmfw_load_firmware */
292 * Display usage message and quit
299 "Usage: %s -n name -m md_file -f fw_file\n"
301 "\t-n name device name\n" \
302 "\t-m mini-driver image mini-driver image file name for download\n" \
303 "\t-f firmware image firmware image file name for download\n" \
304 "\t-h display this message\n", BCMFW);