Home | History | Annotate | Line # | Download | only in usb
aubtfwl.c revision 1.4
      1 /* $NetBSD: aubtfwl.c,v 1.4 2012/12/27 16:42:32 skrll 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.4 2012/12/27 16:42:32 skrll 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/usbdi_util.h>
     37 #include <dev/firmload.h>
     38 
     39 #define AR3K_FIRMWARE_HEADER_SIZE 20
     40 #define AR3K_FIRMWARE_CHUNK_SIZE 4096
     41 
     42 static int aubtfwl_match(device_t, cfdata_t, void *);
     43 static void aubtfwl_attach(device_t, device_t, void *);
     44 static int aubtfwl_detach(device_t, int);
     45 static void aubtfwl_attach_hook(device_t);
     46 
     47 struct aubtfwl_softc {
     48 	usbd_device_handle sc_udev;
     49 };
     50 
     51 CFATTACH_DECL_NEW(aubtfwl, sizeof(struct aubtfwl_softc), aubtfwl_match, aubtfwl_attach, aubtfwl_detach, NULL);
     52 
     53 static int
     54 aubtfwl_match(device_t parent, cfdata_t match, void *aux)
     55 {
     56 	const struct usb_attach_arg * const uaa = aux;
     57 
     58 	if (uaa->vendor == USB_VENDOR_ATHEROS2 &&
     59 	    uaa->product == USB_PRODUCT_ATHEROS2_AR3011)
     60 		return UMATCH_VENDOR_PRODUCT;
     61 
     62 	return UMATCH_NONE;
     63 }
     64 
     65 static void
     66 aubtfwl_attach(device_t parent, device_t self, void *aux)
     67 {
     68 	const struct usb_attach_arg * const uaa = aux;
     69 	struct aubtfwl_softc * const sc = device_private(self);
     70 	aprint_naive("\n");
     71 	aprint_normal("\n");
     72 	sc->sc_udev = uaa->device;
     73 
     74 	config_mountroot(self, aubtfwl_attach_hook);
     75 }
     76 
     77 static int
     78 aubtfwl_detach(device_t self, int flags)
     79 {
     80 	struct aubtfwl_softc * const sc = device_private(self);
     81 
     82 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, self);
     83 
     84 	return 0;
     85 }
     86 
     87 static void
     88 aubtfwl_attach_hook(device_t self)
     89 {
     90 	struct aubtfwl_softc * const sc = device_private(self);
     91 	usbd_interface_handle iface;
     92 	usbd_pipe_handle pipe;
     93 	usbd_xfer_handle xfer;
     94 	void *buf;
     95 	usb_device_request_t req;
     96 	int error;
     97 	firmware_handle_t fwh;
     98 	size_t fws;
     99 	size_t fwo = 0;
    100 	uint32_t n;
    101 
    102 	memset(&req, 0, sizeof(req));
    103 
    104 	error = firmware_open("ubt", "ath3k-1.fw", &fwh); /* XXX revisit name */
    105 	if (error != 0) {
    106 		aprint_error_dev(self, "ath3k-1.fw open fail %d\n", error);
    107 		return;
    108 	}
    109 	fws = firmware_get_size(fwh);
    110 
    111 	error = usbd_set_config_no(sc->sc_udev, 1, 0);
    112 	if (error != 0) {
    113 		aprint_error_dev(self, "failed to set configuration"
    114 		    ", err=%s\n", usbd_errstr(error));
    115 		goto out_firmware;
    116 	}
    117 
    118 	error = usbd_device2interface_handle(sc->sc_udev, 0, &iface);
    119 	if (error) {
    120 		aprint_error_dev(self, "failed to get interface, %s\n",
    121 		   usbd_errstr(error));
    122 		goto out_firmware;
    123 	}
    124 
    125 	error = usbd_open_pipe(iface, UE_DIR_OUT|2, USBD_EXCLUSIVE_USE, &pipe);
    126 	if (error) {
    127 		aprint_error_dev(self, "failed to open pipe, %s\n",
    128 		   usbd_errstr(error));
    129 		goto out_firmware;
    130 	}
    131 
    132 	xfer = usbd_alloc_xfer(sc->sc_udev);
    133 	if (xfer == NULL) {
    134 		aprint_error_dev(self, "failed to alloc xfer\n");
    135 		goto out_pipe;
    136 	}
    137 
    138 	buf = usbd_alloc_buffer(xfer, 4096);
    139 	if (buf == NULL) {
    140 		aprint_error_dev(self, "failed to alloc buffer\n");
    141 		goto out_xfer;
    142 	}
    143 
    144 	error = firmware_read(fwh, fwo, buf, AR3K_FIRMWARE_HEADER_SIZE);
    145 	if (error != 0) {
    146 		aprint_error_dev(self, "firmware_read failed %d\n", error);
    147 		goto out_xfer;
    148 	}
    149 
    150 	req.bRequest = 1;
    151 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    152 	USETW(req.wValue, 0);
    153 	USETW(req.wIndex, 0);
    154 	USETW(req.wLength, AR3K_FIRMWARE_HEADER_SIZE);
    155 
    156 	aprint_verbose_dev(self, "beginning firmware load\n");
    157 
    158 	error = usbd_do_request(sc->sc_udev, &req, buf);
    159 	if (error != 0) {
    160 		aprint_error_dev(self, "%s\n", usbd_errstr(error));
    161 		return;
    162 	}
    163 	fwo = AR3K_FIRMWARE_HEADER_SIZE;
    164 
    165 	while (fwo < fws) {
    166 		n = min(AR3K_FIRMWARE_CHUNK_SIZE, fws - fwo);
    167 		error = firmware_read(fwh, fwo, buf, n);
    168 		if (error != 0) {
    169 			break;;
    170 		}
    171 		error = usbd_bulk_transfer(xfer, pipe,
    172 		    USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
    173 		    buf, &n, device_xname(self));
    174 		if (error != USBD_NORMAL_COMPLETION) {
    175 			aprint_error_dev(self, "xfer failed, %s\n",
    176 			   usbd_errstr(error));
    177 			break;;
    178 		}
    179 		fwo += n;
    180 	}
    181 	aprint_verbose_dev(self, "firmware load complete\n");
    182 
    183 out_xfer:
    184 	usbd_free_xfer(xfer);
    185 out_pipe:
    186 	usbd_close_pipe(pipe);
    187 out_firmware:
    188 	firmware_close(fwh);
    189 
    190 	return;
    191 }
    192