1 /*- 2 * Copyright (c) 2006 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * Atheros AR5523 USB Station Firmware downloader. 34 * 35 * uathload -d ugen-device [firmware-file] 36 * 37 * Intended to be called from devd on device discovery. 38 */ 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <sys/endian.h> 42 #include <sys/mman.h> 43 44 #include <sys/ioctl.h> 45 #include <dev/usb/usb.h> 46 #include <dev/usb/usb_ioctl.h> 47 48 #include <err.h> 49 #include <fcntl.h> 50 #include <libgen.h> 51 #include <paths.h> 52 #include <stdio.h> 53 #include <string.h> 54 #include <strings.h> 55 #include <unistd.h> 56 57 /* all fields are big endian */ 58 struct uath_fwmsg { 59 uint32_t flags; 60 #define UATH_WRITE_BLOCK (1 << 4) 61 62 uint32_t len; 63 #define UATH_MAX_FWBLOCK_SIZE 2048 64 65 uint32_t total; 66 uint32_t remain; 67 uint32_t rxtotal; 68 uint32_t pad[123]; 69 } __packed; 70 71 #define UATH_DATA_TIMEOUT 10000 72 #define UATH_CMD_TIMEOUT 1000 73 74 #define VERBOSE(_fmt, ...) do { \ 75 if (verbose) { \ 76 printf(_fmt, __VA_ARGS__); \ 77 fflush(stdout); \ 78 } \ 79 } while (0) 80 81 extern uint8_t _binary_ar5523_bin_start; 82 extern uint8_t _binary_ar5523_bin_end; 83 84 static int 85 getdevname(const char *devname, char *msgdev, char *datadev) 86 { 87 char *bn, *dn; 88 89 dn = dirname(devname); 90 if (dn == NULL) 91 return (-1); 92 bn = basename(devname); 93 if (bn == NULL || strncmp(bn, "ugen", 4)) 94 return (-1); 95 bn += 4; 96 97 /* NB: pipes are hardcoded */ 98 snprintf(msgdev, 256, "%s/usb/%s.1", dn, bn); 99 snprintf(datadev, 256, "%s/usb/%s.2", dn, bn); 100 return (0); 101 } 102 103 static void 104 usage(void) 105 { 106 errx(-1, "usage: uathload [-v] -d devname [firmware]"); 107 } 108 109 int 110 main(int argc, char *argv[]) 111 { 112 const char *fwname, *devname; 113 char msgdev[256], datadev[256]; 114 struct uath_fwmsg txmsg, rxmsg; 115 char *txdata; 116 struct stat sb; 117 int msg, data, fw, timeout, b, c; 118 int bufsize = 512, verbose = 0; 119 ssize_t len; 120 121 devname = NULL; 122 while ((c = getopt(argc, argv, "d:v")) != -1) { 123 switch (c) { 124 case 'd': 125 devname = optarg; 126 break; 127 case 'v': 128 verbose = 1; 129 break; 130 default: 131 usage(); 132 /*NOTREACHED*/ 133 } 134 } 135 argc -= optind; 136 argv += optind; 137 138 if (devname == NULL) 139 errx(-1, "No device name; use -d to specify the ugen device"); 140 if (argc > 1) 141 usage(); 142 143 if (argc == 1) 144 fwname = argv[0]; 145 else 146 fwname = "/usr/share/firmware/ar5523.bin"; 147 fw = open(fwname, O_RDONLY, 0); 148 if (fw < 0) 149 err(-1, "open(%s)", fwname); 150 if (fstat(fw, &sb) < 0) 151 err(-1, "fstat(%s)", fwname); 152 txdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fw, 0); 153 if (txdata == MAP_FAILED) 154 err(-1, "mmap(%s)", fwname); 155 len = sb.st_size; 156 /* XXX verify device is an AR5005 part */ 157 if (getdevname(devname, msgdev, datadev)) 158 err(-1, "getdevname error"); 159 160 msg = open(msgdev, O_RDWR, 0); 161 if (msg < 0) 162 err(-1, "open(%s)", msgdev); 163 timeout = UATH_DATA_TIMEOUT; 164 if (ioctl(msg, USB_SET_RX_TIMEOUT, &timeout) < 0) 165 err(-1, "%s: USB_SET_RX_TIMEOUT(%u)", msgdev, UATH_DATA_TIMEOUT); 166 if (ioctl(msg, USB_SET_RX_BUFFER_SIZE, &bufsize) < 0) 167 err(-1, "%s: USB_SET_RX_BUFFER_SIZE(%u)", msgdev, bufsize); 168 169 data = open(datadev, O_WRONLY, 0); 170 if (data < 0) 171 err(-1, "open(%s)", datadev); 172 timeout = UATH_DATA_TIMEOUT; 173 if (ioctl(data, USB_SET_TX_TIMEOUT, &timeout) < 0) 174 err(-1, "%s: USB_SET_TX_TIMEOUT(%u)", datadev, 175 UATH_DATA_TIMEOUT); 176 177 VERBOSE("Load firmware %s to %s\n", fwname, devname); 178 179 bzero(&txmsg, sizeof (struct uath_fwmsg)); 180 txmsg.flags = htobe32(UATH_WRITE_BLOCK); 181 txmsg.total = htobe32(len); 182 183 b = 0; 184 while (len > 0) { 185 int mlen; 186 187 mlen = len; 188 if (mlen > UATH_MAX_FWBLOCK_SIZE) 189 mlen = UATH_MAX_FWBLOCK_SIZE; 190 txmsg.remain = htobe32(len - mlen); 191 txmsg.len = htobe32(mlen); 192 193 /* send firmware block meta-data */ 194 VERBOSE("send block %2u: %zd bytes remaining", b, len - mlen); 195 if (write(msg, &txmsg, sizeof(txmsg)) != sizeof(txmsg)) { 196 VERBOSE("%s", "\n"); 197 err(-1, "error sending msg (%s)", msgdev); 198 break; 199 } 200 201 /* send firmware block data */ 202 VERBOSE("%s", "\n : data..."); 203 if (write(data, txdata, mlen) != mlen) { 204 VERBOSE("%s", "\n"); 205 err(-1, "error sending data (%s)", datadev); 206 break; 207 } 208 209 /* wait for ack from firmware */ 210 VERBOSE("%s", "\n : wait for ack..."); 211 bzero(&rxmsg, sizeof(rxmsg)); 212 if (read(msg, &rxmsg, sizeof(rxmsg)) != sizeof(rxmsg)) { 213 VERBOSE("%s", "\n"); 214 err(-1, "error reading msg (%s)", msgdev); 215 break; 216 } 217 218 VERBOSE("flags=0x%x total=%d\n", 219 be32toh(rxmsg.flags), be32toh(rxmsg.rxtotal)); 220 len -= mlen; 221 txdata += mlen; 222 b++; 223 } 224 sleep(1); 225 close(fw); 226 close(msg); 227 close(data); 228 return 0; 229 } 230