1 /* $NetBSD: aubtfwl.c,v 1.5 2013/05/09 12:44:31 aymeric Exp $ */ 2 3 /* 4 * Copyright (c) 2011 Jonathan A. Kollasch 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: aubtfwl.c,v 1.5 2013/05/09 12:44:31 aymeric Exp $"); 31 32 #include <sys/param.h> 33 #include <dev/usb/usb.h> 34 #include <dev/usb/usbdevs.h> 35 #include <dev/usb/usbdi.h> 36 #include <dev/usb/usbdivar.h> 37 #include <dev/usb/usbdi_util.h> 38 #include <dev/firmload.h> 39 40 #include <dev/usb/aubtfwlreg.h> 41 42 #define AR3K_FIRMWARE_CHUNK_SIZE 4096 43 44 static int aubtfwl_match(device_t, cfdata_t, void *); 45 static void aubtfwl_attach(device_t, device_t, void *); 46 static int aubtfwl_detach(device_t, int); 47 static void aubtfwl_attach_hook(device_t); 48 49 struct aubtfwl_softc { 50 usbd_device_handle sc_udev; 51 int sc_flags; 52 #define AUBT_IS_AR3012 1 53 }; 54 55 CFATTACH_DECL_NEW(aubtfwl, sizeof(struct aubtfwl_softc), aubtfwl_match, aubtfwl_attach, aubtfwl_detach, NULL); 56 57 static const struct usb_devno ar3k_devs[] = { 58 { USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR3011 }, 59 }; 60 61 static const struct usb_devno ar3k12_devs[] = { 62 { USB_VENDOR_FOXCONN, USB_PRODUCT_FOXCONN_AR3012 }, 63 }; 64 65 static int 66 aubtfwl_match(device_t parent, cfdata_t match, void *aux) 67 { 68 const struct usb_attach_arg * const uaa = aux; 69 70 if (usb_lookup(ar3k_devs, uaa->vendor, uaa->product)) 71 return UMATCH_VENDOR_PRODUCT; 72 73 if (usb_lookup(ar3k12_devs, uaa->vendor, uaa->product)) { 74 return (UGETW(uaa->device->ddesc.bcdDevice) > 1)? 75 UMATCH_NONE : UMATCH_VENDOR_PRODUCT; 76 } 77 78 return UMATCH_NONE; 79 } 80 81 static void 82 aubtfwl_attach(device_t parent, device_t self, void *aux) 83 { 84 const struct usb_attach_arg * const uaa = aux; 85 struct aubtfwl_softc * const sc = device_private(self); 86 aprint_naive("\n"); 87 aprint_normal("\n"); 88 sc->sc_udev = uaa->device; 89 sc->sc_flags = 0; 90 91 if (usb_lookup(ar3k12_devs, uaa->vendor, uaa->product)) 92 sc->sc_flags |= AUBT_IS_AR3012; 93 94 config_mountroot(self, aubtfwl_attach_hook); 95 } 96 97 static int 98 aubtfwl_detach(device_t self, int flags) 99 { 100 struct aubtfwl_softc * const sc = device_private(self); 101 102 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, self); 103 104 return 0; 105 } 106 107 /* Returns 0 if firmware was correctly loaded */ 108 static int 109 aubtfwl_firmware_load(device_t self, const char *name) { 110 struct aubtfwl_softc * const sc = device_private(self); 111 usbd_interface_handle iface; 112 usbd_pipe_handle pipe; 113 usbd_xfer_handle xfer; 114 void *buf; 115 usb_device_request_t req; 116 int error = 0; 117 firmware_handle_t fwh; 118 size_t fws; 119 size_t fwo = 0; 120 uint32_t n; 121 122 memset(&req, 0, sizeof req); 123 124 error = firmware_open("ubt", name, &fwh); 125 if (error != 0) { 126 aprint_error_dev(self, "'%s' open fail %d\n", name, error); 127 return error; 128 } 129 fws = firmware_get_size(fwh); 130 131 error = usbd_set_config_no(sc->sc_udev, 1, 0); 132 if (error != 0) { 133 aprint_error_dev(self, "failed to set configuration" 134 ", err=%s\n", usbd_errstr(error)); 135 goto out_firmware; 136 } 137 138 error = usbd_device2interface_handle(sc->sc_udev, 0, &iface); 139 if (error) { 140 aprint_error_dev(self, "failed to get interface, %s\n", 141 usbd_errstr(error)); 142 goto out_firmware; 143 } 144 145 error = usbd_open_pipe(iface, UE_DIR_OUT|2, USBD_EXCLUSIVE_USE, &pipe); 146 if (error) { 147 aprint_error_dev(self, "failed to open pipe, %s\n", 148 usbd_errstr(error)); 149 goto out_firmware; 150 } 151 152 xfer = usbd_alloc_xfer(sc->sc_udev); 153 if (xfer == NULL) { 154 aprint_error_dev(self, "failed to alloc xfer\n"); 155 error = 1; 156 goto out_pipe; 157 } 158 159 buf = usbd_alloc_buffer(xfer, AR3K_FIRMWARE_CHUNK_SIZE); 160 if (buf == NULL) { 161 aprint_error_dev(self, "failed to alloc buffer\n"); 162 error = 1; 163 goto out_xfer; 164 } 165 166 error = firmware_read(fwh, fwo, buf, AR3K_FIRMWARE_HEADER_SIZE); 167 if (error != 0) { 168 aprint_error_dev(self, "firmware_read failed %d\n", error); 169 goto out_xfer; 170 } 171 172 req.bRequest = AR3K_SEND_FIRMWARE; 173 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 174 USETW(req.wValue, 0); 175 USETW(req.wIndex, 0); 176 USETW(req.wLength, AR3K_FIRMWARE_HEADER_SIZE); 177 178 aprint_verbose_dev(self, "beginning firmware load\n"); 179 180 error = usbd_do_request(sc->sc_udev, &req, buf); 181 if (error != 0) { 182 aprint_error_dev(self, "%s\n", usbd_errstr(error)); 183 return error; 184 } 185 fwo = AR3K_FIRMWARE_HEADER_SIZE; 186 187 while (fwo < fws) { 188 n = min(AR3K_FIRMWARE_CHUNK_SIZE, fws - fwo); 189 error = firmware_read(fwh, fwo, buf, n); 190 if (error != 0) { 191 break; 192 } 193 error = usbd_bulk_transfer(xfer, pipe, 194 USBD_NO_COPY, USBD_DEFAULT_TIMEOUT, 195 buf, &n, device_xname(self)); 196 if (error != USBD_NORMAL_COMPLETION) { 197 aprint_error_dev(self, "xfer failed, %s\n", 198 usbd_errstr(error)); 199 break; 200 } 201 fwo += n; 202 } 203 204 if (error == 0) 205 aprint_verbose_dev(self, "firmware load complete\n"); 206 207 out_xfer: 208 usbd_free_xfer(xfer); 209 out_pipe: 210 usbd_close_pipe(pipe); 211 out_firmware: 212 firmware_close(fwh); 213 214 return !!error; 215 } 216 217 static int 218 aubtfwl_get_state(struct aubtfwl_softc *sc, uint8_t *state) { 219 usb_device_request_t req; 220 int error = 0; 221 222 memset(&req, 0, sizeof req); 223 224 req.bRequest = AR3K_GET_STATE; 225 req.bmRequestType = UT_READ_VENDOR_DEVICE; 226 USETW(req.wValue, 0); 227 USETW(req.wIndex, 0); 228 USETW(req.wLength, sizeof *state); 229 230 error = usbd_do_request(sc->sc_udev, &req, state); 231 232 return error; 233 } 234 235 static int 236 aubtfwl_get_version(struct aubtfwl_softc *sc, struct ar3k_version *ver) { 237 usb_device_request_t req; 238 int error = 0; 239 240 memset(&req, 0, sizeof req); 241 242 req.bRequest = AR3K_GET_VERSION; 243 req.bmRequestType = UT_READ_VENDOR_DEVICE; 244 USETW(req.wValue, 0); 245 USETW(req.wIndex, 0); 246 USETW(req.wLength, sizeof *ver); 247 248 error = usbd_do_request(sc->sc_udev, &req, ver); 249 250 #if BYTE_ORDER == BIG_ENDIAN 251 if (error == USBD_NORMAL_COMPLETION) { 252 ver->rom = bswap32(ver->rom); 253 ver->build = bswap32(ver->build); 254 ver->ram = bswap32(ver->ram); 255 } 256 #endif 257 return error; 258 } 259 260 static int 261 aubtfwl_send_command(struct aubtfwl_softc *sc, uByte cmd) { 262 usb_device_request_t req; 263 int error = 0; 264 265 memset(&req, 0, sizeof req); 266 267 req.bRequest = cmd; 268 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 269 USETW(req.wValue, 0); 270 USETW(req.wIndex, 0); 271 USETW(req.wLength, 0); 272 273 error = usbd_do_request(sc->sc_udev, &req, NULL); 274 275 return error; 276 } 277 278 static void 279 aubtfwl_attach_hook(device_t self) 280 { 281 struct aubtfwl_softc * const sc = device_private(self); 282 char firmware_name[MAXPATHLEN+1]; 283 struct ar3k_version ver; 284 uint8_t state; 285 int clock = 0; 286 int error = 0; 287 288 if (sc->sc_flags & AUBT_IS_AR3012) { 289 error = aubtfwl_get_version(sc, &ver); 290 if (!error) 291 error = aubtfwl_get_state(sc, &state); 292 293 if (error) { 294 aprint_error_dev(self, 295 "couldn't get version or state\n"); 296 return; 297 } 298 299 aprint_verbose_dev(self, "state is 0x%02x\n", state); 300 301 if (!(state & AR3K_STATE_IS_PATCHED)) { 302 snprintf(firmware_name, sizeof firmware_name, 303 "ar3k/AthrBT_0x%08x.dfu", ver.rom); 304 error = aubtfwl_firmware_load(self, firmware_name); 305 306 if (error) 307 return; 308 } 309 310 switch (ver.clock) { 311 case AR3K_CLOCK_19M: 312 clock = 19; 313 break; 314 case AR3K_CLOCK_26M: 315 clock = 26; 316 break; 317 case AR3K_CLOCK_40M: 318 clock = 40; 319 break; 320 } 321 322 snprintf(firmware_name, sizeof firmware_name, 323 "ar3k/ramps_0x%08x_%d.dfu", ver.rom, clock); 324 aubtfwl_firmware_load(self, firmware_name); 325 326 if ((state & AR3K_STATE_MODE_MASK) != AR3K_STATE_MODE_NORMAL) { 327 error = aubtfwl_send_command(sc, AR3K_SET_NORMAL_MODE); 328 if (error) { 329 aprint_error_dev(self, 330 "couldn't set normal mode: %s", 331 usbd_errstr(error)); 332 return; 333 } 334 } 335 336 /* Apparently some devices will fail this, so ignore result */ 337 (void) aubtfwl_send_command(sc, AR3K_SWITCH_VID_PID); 338 } else { 339 aubtfwl_firmware_load(self, "ath3k-1.fw"); 340 } 341 342 return; 343 } 344