2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Sam Leffler, Errno Consulting
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 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
35 * Atheros AR5523 USB Station Firmware downloader.
37 * uathload -d ugen-device [firmware-file]
39 * Intended to be called from devd on device discovery.
41 #include <sys/types.h>
43 #include <sys/endian.h>
46 #include <sys/ioctl.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usb_ioctl.h>
60 /* all fields are big endian */
63 #define UATH_WRITE_BLOCK (1 << 4)
66 #define UATH_MAX_FWBLOCK_SIZE 2048
74 #define UATH_DATA_TIMEOUT 10000
75 #define UATH_CMD_TIMEOUT 1000
77 #define VERBOSE(_fmt, ...) do { \
79 printf(_fmt, __VA_ARGS__); \
84 extern uint8_t _binary_ar5523_bin_start;
85 extern uint8_t _binary_ar5523_bin_end;
88 getdevname(const char *udevname, char *msgdev, char *datadev)
90 char *bn, *bnbuf, *dn, *dnbuf;
92 dnbuf = strdup(udevname);
96 bnbuf = strdup(udevname);
101 bn = basename(bnbuf);
102 if (strncmp(bn, "ugen", 4) != 0) {
109 /* NB: pipes are hardcoded */
110 snprintf(msgdev, 256, "%s/usb/%s.1", dn, bn);
111 snprintf(datadev, 256, "%s/usb/%s.2", dn, bn);
120 errx(-1, "usage: uathload [-v] -d devname [firmware]");
124 main(int argc, char *argv[])
126 const char *fwname, *udevname;
127 char msgdev[256], datadev[256];
128 struct uath_fwmsg txmsg, rxmsg;
131 int msg, data, fw, timeout, b, c;
132 int bufsize = 512, verbose = 0;
136 while ((c = getopt(argc, argv, "d:v")) != -1) {
152 if (udevname == NULL)
153 errx(-1, "No device name; use -d to specify the ugen device");
160 fwname = _PATH_FIRMWARE "/ar5523.bin";
161 fw = open(fwname, O_RDONLY, 0);
163 err(-1, "open(%s)", fwname);
164 if (fstat(fw, &sb) < 0)
165 err(-1, "fstat(%s)", fwname);
166 txdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fw, 0);
167 if (txdata == MAP_FAILED)
168 err(-1, "mmap(%s)", fwname);
170 /* XXX verify device is an AR5005 part */
171 if (getdevname(udevname, msgdev, datadev))
172 err(-1, "getdevname error");
174 msg = open(msgdev, O_RDWR, 0);
176 err(-1, "open(%s)", msgdev);
177 timeout = UATH_DATA_TIMEOUT;
178 if (ioctl(msg, USB_SET_RX_TIMEOUT, &timeout) < 0)
179 err(-1, "%s: USB_SET_RX_TIMEOUT(%u)", msgdev, UATH_DATA_TIMEOUT);
180 if (ioctl(msg, USB_SET_RX_BUFFER_SIZE, &bufsize) < 0)
181 err(-1, "%s: USB_SET_RX_BUFFER_SIZE(%u)", msgdev, bufsize);
183 data = open(datadev, O_WRONLY, 0);
185 err(-1, "open(%s)", datadev);
186 timeout = UATH_DATA_TIMEOUT;
187 if (ioctl(data, USB_SET_TX_TIMEOUT, &timeout) < 0)
188 err(-1, "%s: USB_SET_TX_TIMEOUT(%u)", datadev,
191 VERBOSE("Load firmware %s to %s\n", fwname, udevname);
193 bzero(&txmsg, sizeof (struct uath_fwmsg));
194 txmsg.flags = htobe32(UATH_WRITE_BLOCK);
195 txmsg.total = htobe32(len);
202 if (mlen > UATH_MAX_FWBLOCK_SIZE)
203 mlen = UATH_MAX_FWBLOCK_SIZE;
204 txmsg.remain = htobe32(len - mlen);
205 txmsg.len = htobe32(mlen);
207 /* send firmware block meta-data */
208 VERBOSE("send block %2u: %zd bytes remaining", b, len - mlen);
209 if (write(msg, &txmsg, sizeof(txmsg)) != sizeof(txmsg)) {
211 err(-1, "error sending msg (%s)", msgdev);
215 /* send firmware block data */
216 VERBOSE("%s", "\n : data...");
217 if (write(data, txdata, mlen) != mlen) {
219 err(-1, "error sending data (%s)", datadev);
223 /* wait for ack from firmware */
224 VERBOSE("%s", "\n : wait for ack...");
225 bzero(&rxmsg, sizeof(rxmsg));
226 if (read(msg, &rxmsg, sizeof(rxmsg)) != sizeof(rxmsg)) {
228 err(-1, "error reading msg (%s)", msgdev);
232 VERBOSE("flags=0x%x total=%d\n",
233 be32toh(rxmsg.flags), be32toh(rxmsg.rxtotal));