bcmfw.c revision 188945
1279377Simp/* 2279377Simp * bcmfw.c 3279377Simp * 4279377Simp * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5279377Simp * All rights reserved. 6279377Simp * 7279377Simp * Redistribution and use in source and binary forms, with or without 8279377Simp * modification, are permitted provided that the following conditions 9279377Simp * are met: 10279377Simp * 1. Redistributions of source code must retain the above copyright 11279377Simp * notice, this list of conditions and the following disclaimer. 12279377Simp * 2. Redistributions in binary form must reproduce the above copyright 13279377Simp * notice, this list of conditions and the following disclaimer in the 14279377Simp * documentation and/or other materials provided with the distribution. 15279377Simp * 16279377Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19279377Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26279377Simp * SUCH DAMAGE. 27279377Simp * 28279377Simp * $Id: bcmfw.c,v 1.4 2003/04/27 19:28:09 max Exp $ 29279377Simp * $FreeBSD: head/usr.sbin/bluetooth/bcmfw/bcmfw.c 188945 2009-02-23 18:36:54Z thompsa $ 30279377Simp * 31279377Simp * Based on Linux BlueZ BlueFW-0.9 package 32279377Simp * 33279377Simp */ 34279377Simp 35279377Simp#include <sys/types.h> 36279377Simp#include <sys/ioctl.h> 37279377Simp#include <dev/usb/usb.h> 38279377Simp#include <dev/usb/usb_ioctl.h> 39279377Simp#include <errno.h> 40279377Simp#include <fcntl.h> 41279377Simp#include <netgraph.h> 42279377Simp#include <stdarg.h> 43279377Simp#include <stdio.h> 44279377Simp#include <stdlib.h> 45279377Simp#include <string.h> 46279377Simp#include <syslog.h> 47279377Simp#include <unistd.h> 48279377Simp 49279377Simp#define BCMFW "bcmfw" 50279377Simp#define BCMFW_INTR_EP 1 51279377Simp#define BCMFW_BULK_EP 2 52279377Simp#define BCMFW_BSIZE 4096 53279377Simp 54279377Simp#define USB_VENDOR_BROADCOM 0x0a5c 55279377Simp#define USB_PRODUCT_BROADCOM_BCM2033 0x2033 56279377Simp 57279377Simpstatic int bcmfw_check_device 58279377Simp (char const *name); 59279377Simpstatic int bcmfw_load_firmware 60279377Simp (char const *name, char const *md, char const *fw); 61279377Simpstatic void bcmfw_usage 62279377Simp (void); 63279377Simp 64279377Simp/* 65279377Simp * Main 66279377Simp */ 67279377Simp 68279377Simpint 69279377Simpmain(int argc, char *argv[]) 70279377Simp{ 71279377Simp char *name = NULL, *md = NULL, *fw = NULL; 72279377Simp int x; 73279377Simp 74279377Simp while ((x = getopt(argc, argv, "f:hn:m:")) != -1) { 75279377Simp switch (x) { 76279377Simp case 'f': /* firmware file */ 77279377Simp fw = optarg; 78279377Simp break; 79279377Simp 80279377Simp case 'n': /* name */ 81279377Simp name = optarg; 82279377Simp break; 83279377Simp 84279377Simp case 'm': /* Mini-driver */ 85279377Simp md = optarg; 86279377Simp break; 87279377Simp 88279377Simp case 'h': 89279377Simp default: 90279377Simp bcmfw_usage(); 91279377Simp /* NOT REACHED */ 92279377Simp } 93279377Simp } 94279377Simp 95279377Simp if (name == NULL || md == NULL || fw == NULL) 96279377Simp bcmfw_usage(); 97279377Simp /* NOT REACHED */ 98279377Simp 99279377Simp openlog(BCMFW, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_USER); 100279377Simp 101279377Simp if (bcmfw_check_device(name) < 0) 102279377Simp exit(1); 103279377Simp 104279377Simp if (bcmfw_load_firmware(name, md, fw) < 0) 105279377Simp exit(1); 106 107 closelog(); 108 109 return (0); 110} /* main */ 111 112/* 113 * Check device VendorID/ProductID 114 */ 115 116static int 117bcmfw_check_device(char const *name) 118{ 119 usb_device_descriptor_t desc; 120 char path[BCMFW_BSIZE]; 121 int fd = -1, error = -1; 122 123 snprintf(path, sizeof(path), "/dev/%s", name); 124 125 if ((fd = open(path, O_WRONLY)) < 0) { 126 syslog(LOG_ERR, "Could not open(%s). %s (%d)", 127 path, strerror(errno), errno); 128 goto out; 129 } 130 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); 135 goto out; 136 } 137 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)); 143 error = -1; 144 } else 145 error = 0; 146out: 147 if (fd != -1) 148 close(fd); 149 150 return (error); 151} /* bcmfw_check_device */ 152 153/* 154 * Download minidriver and firmware 155 */ 156 157static int 158bcmfw_load_firmware(char const *name, char const *md, char const *fw) 159{ 160 char buf[BCMFW_BSIZE]; 161 int intr = -1, bulk = -1, fd = -1, error = -1, len; 162 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); 168 goto out; 169 } 170 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); 176 goto out; 177 } 178 179 /* 180 * Load mini-driver 181 */ 182 183 if ((fd = open(md, O_RDONLY)) < 0) { 184 syslog(LOG_ERR, "Could not open(%s). %s (%d)", 185 md, strerror(errno), errno); 186 goto out; 187 } 188 189 for (;;) { 190 len = read(fd, buf, sizeof(buf)); 191 if (len < 0) { 192 syslog(LOG_ERR, "Could not read(%s). %s (%d)", 193 md, strerror(errno), errno); 194 goto out; 195 } 196 if (len == 0) 197 break; 198 199 len = write(bulk, buf, len); 200 if (len < 0) { 201 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)", 202 name, BCMFW_BULK_EP, strerror(errno), 203 errno); 204 goto out; 205 } 206 } 207 208 close(fd); 209 fd = -1; 210 211 usleep(10); 212 213 /* 214 * Memory select 215 */ 216 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); 220 goto out; 221 } 222 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); 226 goto out; 227 } 228 229 if (buf[0] != '#') { 230 syslog(LOG_ERR, "%s: Memory select failed (%c)", name, buf[0]); 231 goto out; 232 } 233 234 /* 235 * Load firmware 236 */ 237 238 if ((fd = open(fw, O_RDONLY)) < 0) { 239 syslog(LOG_ERR, "Could not open(%s). %s (%d)", 240 fw, strerror(errno), errno); 241 goto out; 242 } 243 244 for (;;) { 245 len = read(fd, buf, sizeof(buf)); 246 if (len < 0) { 247 syslog(LOG_ERR, "Could not read(%s). %s (%d)", 248 fw, strerror(errno), errno); 249 goto out; 250 } 251 if (len == 0) 252 break; 253 254 len = write(bulk, buf, len); 255 if (len < 0) { 256 syslog(LOG_ERR, "Could not write(/dev/%s.%d). %s (%d)", 257 name, BCMFW_BULK_EP, strerror(errno), 258 errno); 259 goto out; 260 } 261 } 262 263 close(fd); 264 fd = -1; 265 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); 269 goto out; 270 } 271 272 if (buf[0] != '.') { 273 syslog(LOG_ERR, "%s: Could not load firmware (%c)", 274 name, buf[0]); 275 goto out; 276 } 277 278 usleep(500000); 279 error = 0; 280out: 281 if (fd != -1) 282 close(fd); 283 if (bulk != -1) 284 close(bulk); 285 if (intr != -1) 286 close(intr); 287 288 return (error); 289} /* bcmfw_load_firmware */ 290 291/* 292 * Display usage message and quit 293 */ 294 295static void 296bcmfw_usage(void) 297{ 298 fprintf(stdout, 299"Usage: %s -n name -m md_file -f fw_file\n" 300"Where:\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); 305 306 exit(255); 307} /* bcmfw_usage */ 308 309