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