xref: /freebsd/sys/dev/malo/if_malohal.c (revision 34d381f919ccabfce01694c6ebcd7040d11553f1)
13c7e78d3SWeongyo Jeong /*-
23c7e78d3SWeongyo Jeong  * Copyright (c) 2007 Marvell Semiconductor, Inc.
33c7e78d3SWeongyo Jeong  * Copyright (c) 2007 Sam Leffler, Errno Consulting
43c7e78d3SWeongyo Jeong  * Copyright (c) 2008 Weongyo Jeong <weongyo@freebsd.org>
53c7e78d3SWeongyo Jeong  * All rights reserved.
63c7e78d3SWeongyo Jeong  *
73c7e78d3SWeongyo Jeong  * Redistribution and use in source and binary forms, with or without
83c7e78d3SWeongyo Jeong  * modification, are permitted provided that the following conditions
93c7e78d3SWeongyo Jeong  * are met:
103c7e78d3SWeongyo Jeong  * 1. Redistributions of source code must retain the above copyright
113c7e78d3SWeongyo Jeong  *    notice, this list of conditions and the following disclaimer,
123c7e78d3SWeongyo Jeong  *    without modification.
133c7e78d3SWeongyo Jeong  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
143c7e78d3SWeongyo Jeong  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
153c7e78d3SWeongyo Jeong  *    redistribution must be conditioned upon including a substantially
163c7e78d3SWeongyo Jeong  *    similar Disclaimer requirement for further binary redistribution.
173c7e78d3SWeongyo Jeong  *
183c7e78d3SWeongyo Jeong  * NO WARRANTY
193c7e78d3SWeongyo Jeong  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
203c7e78d3SWeongyo Jeong  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
213c7e78d3SWeongyo Jeong  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
223c7e78d3SWeongyo Jeong  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
233c7e78d3SWeongyo Jeong  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
243c7e78d3SWeongyo Jeong  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253c7e78d3SWeongyo Jeong  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263c7e78d3SWeongyo Jeong  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
273c7e78d3SWeongyo Jeong  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283c7e78d3SWeongyo Jeong  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
293c7e78d3SWeongyo Jeong  * THE POSSIBILITY OF SUCH DAMAGES.
303c7e78d3SWeongyo Jeong  */
313c7e78d3SWeongyo Jeong 
323c7e78d3SWeongyo Jeong #include <sys/cdefs.h>
333c7e78d3SWeongyo Jeong #ifdef __FreeBSD__
343c7e78d3SWeongyo Jeong __FBSDID("$FreeBSD$");
353c7e78d3SWeongyo Jeong #endif
363c7e78d3SWeongyo Jeong 
373c7e78d3SWeongyo Jeong #include <sys/param.h>
383c7e78d3SWeongyo Jeong #include <sys/systm.h>
393c7e78d3SWeongyo Jeong #include <sys/endian.h>
403c7e78d3SWeongyo Jeong #include <sys/kernel.h>
413c7e78d3SWeongyo Jeong #include <sys/firmware.h>
423c7e78d3SWeongyo Jeong #include <sys/socket.h>
433c7e78d3SWeongyo Jeong 
443c7e78d3SWeongyo Jeong #include <machine/bus.h>
453c7e78d3SWeongyo Jeong #include <sys/bus.h>
463c7e78d3SWeongyo Jeong 
473c7e78d3SWeongyo Jeong #include <net/if.h>
483c7e78d3SWeongyo Jeong #include <net/if_dl.h>
493c7e78d3SWeongyo Jeong #include <net/if_media.h>
503c7e78d3SWeongyo Jeong 
513c7e78d3SWeongyo Jeong #include <net80211/ieee80211_var.h>
523c7e78d3SWeongyo Jeong 
533c7e78d3SWeongyo Jeong #include <dev/malo/if_malo.h>
543c7e78d3SWeongyo Jeong 
553c7e78d3SWeongyo Jeong #define MALO_WAITOK				1
563c7e78d3SWeongyo Jeong #define MALO_NOWAIT				0
573c7e78d3SWeongyo Jeong 
583c7e78d3SWeongyo Jeong #define	_CMD_SETUP(pCmd, _type, _cmd) do {				\
593c7e78d3SWeongyo Jeong 	pCmd = (_type *)&mh->mh_cmdbuf[0];				\
603c7e78d3SWeongyo Jeong 	memset(pCmd, 0, sizeof(_type));					\
613c7e78d3SWeongyo Jeong 	pCmd->cmdhdr.cmd = htole16(_cmd);				\
623c7e78d3SWeongyo Jeong 	pCmd->cmdhdr.length = htole16(sizeof(_type));			\
633c7e78d3SWeongyo Jeong } while (0)
643c7e78d3SWeongyo Jeong 
653c7e78d3SWeongyo Jeong static __inline uint32_t
663c7e78d3SWeongyo Jeong malo_hal_read4(struct malo_hal *mh, bus_size_t off)
673c7e78d3SWeongyo Jeong {
683c7e78d3SWeongyo Jeong 	return bus_space_read_4(mh->mh_iot, mh->mh_ioh, off);
693c7e78d3SWeongyo Jeong }
703c7e78d3SWeongyo Jeong 
713c7e78d3SWeongyo Jeong static __inline void
723c7e78d3SWeongyo Jeong malo_hal_write4(struct malo_hal *mh, bus_size_t off, uint32_t val)
733c7e78d3SWeongyo Jeong {
743c7e78d3SWeongyo Jeong 	bus_space_write_4(mh->mh_iot, mh->mh_ioh, off, val);
753c7e78d3SWeongyo Jeong }
763c7e78d3SWeongyo Jeong 
773c7e78d3SWeongyo Jeong static void
783c7e78d3SWeongyo Jeong malo_hal_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
793c7e78d3SWeongyo Jeong {
803c7e78d3SWeongyo Jeong 	bus_addr_t *paddr = (bus_addr_t*) arg;
813c7e78d3SWeongyo Jeong 
823c7e78d3SWeongyo Jeong 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
833c7e78d3SWeongyo Jeong 	*paddr = segs->ds_addr;
843c7e78d3SWeongyo Jeong }
853c7e78d3SWeongyo Jeong 
863c7e78d3SWeongyo Jeong /*
873c7e78d3SWeongyo Jeong  * Setup for communication with the device.  We allocate
883c7e78d3SWeongyo Jeong  * a command buffer and map it for bus dma use.  The pci
893c7e78d3SWeongyo Jeong  * device id is used to identify whether the device has
903c7e78d3SWeongyo Jeong  * SRAM on it (in which case f/w download must include a
913c7e78d3SWeongyo Jeong  * memory controller reset).  All bus i/o operations happen
923c7e78d3SWeongyo Jeong  * in BAR 1; the driver passes in the tag and handle we need.
933c7e78d3SWeongyo Jeong  */
943c7e78d3SWeongyo Jeong struct malo_hal *
953c7e78d3SWeongyo Jeong malo_hal_attach(device_t dev, uint16_t devid,
963c7e78d3SWeongyo Jeong     bus_space_handle_t ioh, bus_space_tag_t iot, bus_dma_tag_t tag)
973c7e78d3SWeongyo Jeong {
983c7e78d3SWeongyo Jeong 	int error;
993c7e78d3SWeongyo Jeong 	struct malo_hal *mh;
1003c7e78d3SWeongyo Jeong 
1013c7e78d3SWeongyo Jeong 	mh = malloc(sizeof(struct malo_hal), M_DEVBUF, M_NOWAIT | M_ZERO);
1023c7e78d3SWeongyo Jeong 	if (mh == NULL)
1033c7e78d3SWeongyo Jeong 		return NULL;
1043c7e78d3SWeongyo Jeong 
1053c7e78d3SWeongyo Jeong 	mh->mh_dev = dev;
1063c7e78d3SWeongyo Jeong 	mh->mh_ioh = ioh;
1073c7e78d3SWeongyo Jeong 	mh->mh_iot = iot;
1083c7e78d3SWeongyo Jeong 
1093c7e78d3SWeongyo Jeong 	snprintf(mh->mh_mtxname, sizeof(mh->mh_mtxname),
1103c7e78d3SWeongyo Jeong 	    "%s_hal", device_get_nameunit(dev));
1113c7e78d3SWeongyo Jeong 	mtx_init(&mh->mh_mtx, mh->mh_mtxname, NULL, MTX_DEF);
1123c7e78d3SWeongyo Jeong 
1133c7e78d3SWeongyo Jeong 	/*
1143c7e78d3SWeongyo Jeong 	 * Allocate the command buffer and map into the address
1153c7e78d3SWeongyo Jeong 	 * space of the h/w.  We request "coherent" memory which
1163c7e78d3SWeongyo Jeong 	 * will be uncached on some architectures.
1173c7e78d3SWeongyo Jeong 	 */
1183c7e78d3SWeongyo Jeong 	error = bus_dma_tag_create(tag,		/* parent */
1193c7e78d3SWeongyo Jeong 		       PAGE_SIZE, 0,		/* alignment, bounds */
1203c7e78d3SWeongyo Jeong 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1213c7e78d3SWeongyo Jeong 		       BUS_SPACE_MAXADDR,	/* highaddr */
1223c7e78d3SWeongyo Jeong 		       NULL, NULL,		/* filter, filterarg */
1233c7e78d3SWeongyo Jeong 		       MALO_CMDBUF_SIZE,	/* maxsize */
1243c7e78d3SWeongyo Jeong 		       1,			/* nsegments */
1253c7e78d3SWeongyo Jeong 		       MALO_CMDBUF_SIZE,	/* maxsegsize */
1263c7e78d3SWeongyo Jeong 		       BUS_DMA_ALLOCNOW,	/* flags */
1273c7e78d3SWeongyo Jeong 		       NULL,			/* lockfunc */
1283c7e78d3SWeongyo Jeong 		       NULL,			/* lockarg */
1293c7e78d3SWeongyo Jeong 		       &mh->mh_dmat);
1303c7e78d3SWeongyo Jeong 	if (error != 0) {
1313c7e78d3SWeongyo Jeong 		device_printf(dev, "unable to allocate memory for cmd buffer, "
1323c7e78d3SWeongyo Jeong 			"error %u\n", error);
1333c7e78d3SWeongyo Jeong 		goto fail;
1343c7e78d3SWeongyo Jeong 	}
1353c7e78d3SWeongyo Jeong 
1363c7e78d3SWeongyo Jeong 	/* allocate descriptors */
1373c7e78d3SWeongyo Jeong 	error = bus_dmamap_create(mh->mh_dmat, BUS_DMA_NOWAIT, &mh->mh_dmamap);
1383c7e78d3SWeongyo Jeong 	if (error != 0) {
1393c7e78d3SWeongyo Jeong 		device_printf(dev, "unable to create dmamap for cmd buffers, "
1403c7e78d3SWeongyo Jeong 			"error %u\n", error);
1413c7e78d3SWeongyo Jeong 		goto fail;
1423c7e78d3SWeongyo Jeong 	}
1433c7e78d3SWeongyo Jeong 
1443c7e78d3SWeongyo Jeong 	error = bus_dmamem_alloc(mh->mh_dmat, (void**) &mh->mh_cmdbuf,
1453c7e78d3SWeongyo Jeong 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
1463c7e78d3SWeongyo Jeong 				 &mh->mh_dmamap);
1473c7e78d3SWeongyo Jeong 	if (error != 0) {
1483c7e78d3SWeongyo Jeong 		device_printf(dev, "unable to allocate memory for cmd buffer, "
1493c7e78d3SWeongyo Jeong 			"error %u\n", error);
1503c7e78d3SWeongyo Jeong 		goto fail;
1513c7e78d3SWeongyo Jeong 	}
1523c7e78d3SWeongyo Jeong 
1533c7e78d3SWeongyo Jeong 	error = bus_dmamap_load(mh->mh_dmat, mh->mh_dmamap,
1543c7e78d3SWeongyo Jeong 				mh->mh_cmdbuf, MALO_CMDBUF_SIZE,
1553c7e78d3SWeongyo Jeong 				malo_hal_load_cb, &mh->mh_cmdaddr,
1563c7e78d3SWeongyo Jeong 				BUS_DMA_NOWAIT);
1573c7e78d3SWeongyo Jeong 	if (error != 0) {
1583c7e78d3SWeongyo Jeong 		device_printf(dev, "unable to load cmd buffer, error %u\n",
1593c7e78d3SWeongyo Jeong 			error);
1603c7e78d3SWeongyo Jeong 		goto fail;
1613c7e78d3SWeongyo Jeong 	}
1623c7e78d3SWeongyo Jeong 
1633c7e78d3SWeongyo Jeong 	return (mh);
1643c7e78d3SWeongyo Jeong 
1653c7e78d3SWeongyo Jeong fail:
1663c7e78d3SWeongyo Jeong 	free(mh, M_DEVBUF);
1673c7e78d3SWeongyo Jeong 
1683c7e78d3SWeongyo Jeong 	if (mh->mh_dmamap != NULL) {
1693c7e78d3SWeongyo Jeong 		bus_dmamap_unload(mh->mh_dmat, mh->mh_dmamap);
1703c7e78d3SWeongyo Jeong 		if (mh->mh_cmdbuf != NULL)
1713c7e78d3SWeongyo Jeong 			bus_dmamem_free(mh->mh_dmat, mh->mh_cmdbuf,
1723c7e78d3SWeongyo Jeong 			    mh->mh_dmamap);
1733c7e78d3SWeongyo Jeong 		bus_dmamap_destroy(mh->mh_dmat, mh->mh_dmamap);
1743c7e78d3SWeongyo Jeong 	}
1753c7e78d3SWeongyo Jeong 	if (mh->mh_dmat)
1763c7e78d3SWeongyo Jeong 		bus_dma_tag_destroy(mh->mh_dmat);
1773c7e78d3SWeongyo Jeong 
1783c7e78d3SWeongyo Jeong 	return (NULL);
1793c7e78d3SWeongyo Jeong }
1803c7e78d3SWeongyo Jeong 
1813c7e78d3SWeongyo Jeong /*
1823c7e78d3SWeongyo Jeong  * Low level firmware cmd block handshake support.
1833c7e78d3SWeongyo Jeong  */
1843c7e78d3SWeongyo Jeong 
1853c7e78d3SWeongyo Jeong static void
1863c7e78d3SWeongyo Jeong malo_hal_send_cmd(struct malo_hal *mh)
1873c7e78d3SWeongyo Jeong {
1883c7e78d3SWeongyo Jeong 	uint32_t dummy;
1893c7e78d3SWeongyo Jeong 
1903c7e78d3SWeongyo Jeong 	bus_dmamap_sync(mh->mh_dmat, mh->mh_dmamap,
1913c7e78d3SWeongyo Jeong 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1923c7e78d3SWeongyo Jeong 
1933c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_GEN_PTR, mh->mh_cmdaddr);
1943c7e78d3SWeongyo Jeong 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
1953c7e78d3SWeongyo Jeong 
1963c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_H2A_INTERRUPT_EVENTS,
1973c7e78d3SWeongyo Jeong 	    MALO_H2ARIC_BIT_DOOR_BELL);
1983c7e78d3SWeongyo Jeong }
1993c7e78d3SWeongyo Jeong 
2003c7e78d3SWeongyo Jeong static int
2013c7e78d3SWeongyo Jeong malo_hal_waitforcmd(struct malo_hal *mh, uint16_t cmd)
2023c7e78d3SWeongyo Jeong {
2033c7e78d3SWeongyo Jeong #define MAX_WAIT_FW_COMPLETE_ITERATIONS 10000
2043c7e78d3SWeongyo Jeong 	int i;
2053c7e78d3SWeongyo Jeong 
2063c7e78d3SWeongyo Jeong 	for (i = 0; i < MAX_WAIT_FW_COMPLETE_ITERATIONS; i++) {
2073c7e78d3SWeongyo Jeong 		if (mh->mh_cmdbuf[0] == le16toh(cmd))
2083c7e78d3SWeongyo Jeong 			return 1;
2093c7e78d3SWeongyo Jeong 
2103c7e78d3SWeongyo Jeong 		DELAY(1 * 1000);
2113c7e78d3SWeongyo Jeong 	}
2123c7e78d3SWeongyo Jeong 
2133c7e78d3SWeongyo Jeong 	return 0;
2143c7e78d3SWeongyo Jeong #undef MAX_WAIT_FW_COMPLETE_ITERATIONS
2153c7e78d3SWeongyo Jeong }
2163c7e78d3SWeongyo Jeong 
2173c7e78d3SWeongyo Jeong static int
2183c7e78d3SWeongyo Jeong malo_hal_execute_cmd(struct malo_hal *mh, unsigned short cmd)
2193c7e78d3SWeongyo Jeong {
2203c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK_ASSERT(mh);
2213c7e78d3SWeongyo Jeong 
2223c7e78d3SWeongyo Jeong 	if ((mh->mh_flags & MHF_FWHANG) &&
2233c7e78d3SWeongyo Jeong 	    (mh->mh_debug & MALO_HAL_DEBUG_IGNHANG) == 0) {
2243c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "firmware hung, skipping cmd 0x%x\n",
2253c7e78d3SWeongyo Jeong 			cmd);
2263c7e78d3SWeongyo Jeong 		return ENXIO;
2273c7e78d3SWeongyo Jeong 	}
2283c7e78d3SWeongyo Jeong 
2293c7e78d3SWeongyo Jeong 	if (malo_hal_read4(mh, MALO_REG_INT_CODE) == 0xffffffff) {
2303c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "%s: device not present!\n",
2313c7e78d3SWeongyo Jeong 		    __func__);
2323c7e78d3SWeongyo Jeong 		return EIO;
2333c7e78d3SWeongyo Jeong 	}
2343c7e78d3SWeongyo Jeong 
2353c7e78d3SWeongyo Jeong 	malo_hal_send_cmd(mh);
2363c7e78d3SWeongyo Jeong 	if (!malo_hal_waitforcmd(mh, cmd | 0x8000)) {
2373c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev,
2383c7e78d3SWeongyo Jeong 		    "timeout waiting for f/w cmd 0x%x\n", cmd);
2393c7e78d3SWeongyo Jeong 		mh->mh_flags |= MHF_FWHANG;
2403c7e78d3SWeongyo Jeong 		return ETIMEDOUT;
2413c7e78d3SWeongyo Jeong 	}
2423c7e78d3SWeongyo Jeong 
2433c7e78d3SWeongyo Jeong 	bus_dmamap_sync(mh->mh_dmat, mh->mh_dmamap,
2443c7e78d3SWeongyo Jeong 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2453c7e78d3SWeongyo Jeong 
2463c7e78d3SWeongyo Jeong 	return 0;
2473c7e78d3SWeongyo Jeong }
2483c7e78d3SWeongyo Jeong 
2493c7e78d3SWeongyo Jeong static int
2503c7e78d3SWeongyo Jeong malo_hal_get_cal_table(struct malo_hal *mh, uint8_t annex, uint8_t index)
2513c7e78d3SWeongyo Jeong {
2523c7e78d3SWeongyo Jeong 	struct malo_cmd_caltable *cmd;
2533c7e78d3SWeongyo Jeong 	int ret;
2543c7e78d3SWeongyo Jeong 
2553c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK_ASSERT(mh);
2563c7e78d3SWeongyo Jeong 
2573c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_caltable, MALO_HOSTCMD_GET_CALTABLE);
2583c7e78d3SWeongyo Jeong 	cmd->annex = annex;
2593c7e78d3SWeongyo Jeong 	cmd->index = index;
2603c7e78d3SWeongyo Jeong 
2613c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_GET_CALTABLE);
2623c7e78d3SWeongyo Jeong 	if (ret == 0 && cmd->caltbl[0] != annex && annex != 0 && annex != 255)
2633c7e78d3SWeongyo Jeong 		ret = EIO;
2643c7e78d3SWeongyo Jeong 	return ret;
2653c7e78d3SWeongyo Jeong }
2663c7e78d3SWeongyo Jeong 
2673c7e78d3SWeongyo Jeong static int
2683c7e78d3SWeongyo Jeong malo_hal_get_pwrcal_table(struct malo_hal *mh, struct malo_hal_caldata *cal)
2693c7e78d3SWeongyo Jeong {
2703c7e78d3SWeongyo Jeong 	const uint8_t *data;
2713c7e78d3SWeongyo Jeong 	int len;
2723c7e78d3SWeongyo Jeong 
2733c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
2743c7e78d3SWeongyo Jeong 	/* NB: we hold the lock so it's ok to use cmdbuf */
2753c7e78d3SWeongyo Jeong 	data = ((const struct malo_cmd_caltable *) mh->mh_cmdbuf)->caltbl;
2763c7e78d3SWeongyo Jeong 	if (malo_hal_get_cal_table(mh, 33, 0) == 0) {
2773c7e78d3SWeongyo Jeong 		len = (data[2] | (data[3] << 8)) - 12;
2783c7e78d3SWeongyo Jeong 		/* XXX validate len */
2793c7e78d3SWeongyo Jeong 		memcpy(cal->pt_ratetable_20m, &data[12], len);
2803c7e78d3SWeongyo Jeong 	}
2813c7e78d3SWeongyo Jeong 	mh->mh_flags |= MHF_CALDATA;
2823c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
2833c7e78d3SWeongyo Jeong 
2843c7e78d3SWeongyo Jeong 	return 0;
2853c7e78d3SWeongyo Jeong }
2863c7e78d3SWeongyo Jeong 
2873c7e78d3SWeongyo Jeong /*
2883c7e78d3SWeongyo Jeong  * Reset internal state after a firmware download.
2893c7e78d3SWeongyo Jeong  */
2903c7e78d3SWeongyo Jeong static int
2913c7e78d3SWeongyo Jeong malo_hal_resetstate(struct malo_hal *mh)
2923c7e78d3SWeongyo Jeong {
2933c7e78d3SWeongyo Jeong 	/*
2943c7e78d3SWeongyo Jeong 	 * Fetch cal data for later use.
2953c7e78d3SWeongyo Jeong 	 * XXX may want to fetch other stuff too.
2963c7e78d3SWeongyo Jeong 	 */
2973c7e78d3SWeongyo Jeong 	if ((mh->mh_flags & MHF_CALDATA) == 0)
2983c7e78d3SWeongyo Jeong 		malo_hal_get_pwrcal_table(mh, &mh->mh_caldata);
2993c7e78d3SWeongyo Jeong 	return 0;
3003c7e78d3SWeongyo Jeong }
3013c7e78d3SWeongyo Jeong 
3023c7e78d3SWeongyo Jeong static void
3033c7e78d3SWeongyo Jeong malo_hal_fw_reset(struct malo_hal *mh)
3043c7e78d3SWeongyo Jeong {
3053c7e78d3SWeongyo Jeong 
3063c7e78d3SWeongyo Jeong 	if (malo_hal_read4(mh,  MALO_REG_INT_CODE) == 0xffffffff) {
3073c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "%s: device not present!\n",
3083c7e78d3SWeongyo Jeong 		    __func__);
3093c7e78d3SWeongyo Jeong 		return;
3103c7e78d3SWeongyo Jeong 	}
3113c7e78d3SWeongyo Jeong 
3123c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_H2A_INTERRUPT_EVENTS, MALO_ISR_RESET);
3133c7e78d3SWeongyo Jeong 	mh->mh_flags &= ~MHF_FWHANG;
3143c7e78d3SWeongyo Jeong }
3153c7e78d3SWeongyo Jeong 
3163c7e78d3SWeongyo Jeong static void
3173c7e78d3SWeongyo Jeong malo_hal_trigger_pcicmd(struct malo_hal *mh)
3183c7e78d3SWeongyo Jeong {
3193c7e78d3SWeongyo Jeong 	uint32_t dummy;
3203c7e78d3SWeongyo Jeong 
3213c7e78d3SWeongyo Jeong 	bus_dmamap_sync(mh->mh_dmat, mh->mh_dmamap, BUS_DMASYNC_PREWRITE);
3223c7e78d3SWeongyo Jeong 
3233c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_GEN_PTR, mh->mh_cmdaddr);
3243c7e78d3SWeongyo Jeong 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
3253c7e78d3SWeongyo Jeong 
3263c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_INT_CODE, 0x00);
3273c7e78d3SWeongyo Jeong 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
3283c7e78d3SWeongyo Jeong 
3293c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_H2A_INTERRUPT_EVENTS,
3303c7e78d3SWeongyo Jeong 	    MALO_H2ARIC_BIT_DOOR_BELL);
3313c7e78d3SWeongyo Jeong 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
3323c7e78d3SWeongyo Jeong }
3333c7e78d3SWeongyo Jeong 
3343c7e78d3SWeongyo Jeong static int
3353c7e78d3SWeongyo Jeong malo_hal_waitfor(struct malo_hal *mh, uint32_t val)
3363c7e78d3SWeongyo Jeong {
3373c7e78d3SWeongyo Jeong 	int i;
3383c7e78d3SWeongyo Jeong 
3393c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_FW_MAX_NUM_CHECKS; i++) {
3403c7e78d3SWeongyo Jeong 		DELAY(MALO_FW_CHECK_USECS);
3413c7e78d3SWeongyo Jeong 		if (malo_hal_read4(mh, MALO_REG_INT_CODE) == val)
3423c7e78d3SWeongyo Jeong 			return 0;
3433c7e78d3SWeongyo Jeong 	}
3443c7e78d3SWeongyo Jeong 
3453c7e78d3SWeongyo Jeong 	return -1;
3463c7e78d3SWeongyo Jeong }
3473c7e78d3SWeongyo Jeong 
3483c7e78d3SWeongyo Jeong /*
3493c7e78d3SWeongyo Jeong  * Firmware block xmit when talking to the boot-rom.
3503c7e78d3SWeongyo Jeong  */
3513c7e78d3SWeongyo Jeong static int
3523c7e78d3SWeongyo Jeong malo_hal_send_helper(struct malo_hal *mh, int bsize,
3533c7e78d3SWeongyo Jeong     const void *data, size_t dsize, int waitfor)
3543c7e78d3SWeongyo Jeong {
3553c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[0] = htole16(MALO_HOSTCMD_CODE_DNLD);
3563c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[1] = htole16(bsize);
3573c7e78d3SWeongyo Jeong 	memcpy(&mh->mh_cmdbuf[4], data , dsize);
3583c7e78d3SWeongyo Jeong 
3593c7e78d3SWeongyo Jeong 	malo_hal_trigger_pcicmd(mh);
3603c7e78d3SWeongyo Jeong 
3613c7e78d3SWeongyo Jeong 	if (waitfor == MALO_NOWAIT)
3623c7e78d3SWeongyo Jeong 		goto pass;
3633c7e78d3SWeongyo Jeong 
3643c7e78d3SWeongyo Jeong 	/* XXX 2000 vs 200 */
3653c7e78d3SWeongyo Jeong 	if (malo_hal_waitfor(mh, MALO_INT_CODE_CMD_FINISHED) != 0) {
3663c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev,
3673c7e78d3SWeongyo Jeong 		    "%s: timeout waiting for CMD_FINISHED, INT_CODE 0x%x\n",
3683c7e78d3SWeongyo Jeong 		    __func__, malo_hal_read4(mh, MALO_REG_INT_CODE));
3693c7e78d3SWeongyo Jeong 
3703c7e78d3SWeongyo Jeong 		return ETIMEDOUT;
3713c7e78d3SWeongyo Jeong 	}
3723c7e78d3SWeongyo Jeong 
3733c7e78d3SWeongyo Jeong pass:
3743c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_INT_CODE, 0);
3753c7e78d3SWeongyo Jeong 
3763c7e78d3SWeongyo Jeong 	return (0);
3773c7e78d3SWeongyo Jeong }
3783c7e78d3SWeongyo Jeong 
3793c7e78d3SWeongyo Jeong static int
3803c7e78d3SWeongyo Jeong malo_hal_fwload_helper(struct malo_hal *mh, char *helper)
3813c7e78d3SWeongyo Jeong {
3823c7e78d3SWeongyo Jeong 	const struct firmware *fw;
3833c7e78d3SWeongyo Jeong 	int error;
3843c7e78d3SWeongyo Jeong 
3853c7e78d3SWeongyo Jeong 	fw = firmware_get(helper);
3863c7e78d3SWeongyo Jeong 	if (fw == NULL) {
3873c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "could not read microcode %s!\n",
3883c7e78d3SWeongyo Jeong 		    helper);
3893c7e78d3SWeongyo Jeong 		return (EIO);
3903c7e78d3SWeongyo Jeong 	}
3913c7e78d3SWeongyo Jeong 
39234d381f9SWeongyo Jeong 	device_printf(mh->mh_dev, "load %s firmware image (%zu bytes)\n",
3933c7e78d3SWeongyo Jeong 	    helper, fw->datasize);
3943c7e78d3SWeongyo Jeong 
3953c7e78d3SWeongyo Jeong 	error = malo_hal_send_helper(mh, fw->datasize, fw->data, fw->datasize,
3963c7e78d3SWeongyo Jeong 		MALO_WAITOK);
3973c7e78d3SWeongyo Jeong 	if (error != 0)
3983c7e78d3SWeongyo Jeong 		goto fail;
3993c7e78d3SWeongyo Jeong 
4003c7e78d3SWeongyo Jeong 	/* tell the card we're done and... */
4013c7e78d3SWeongyo Jeong 	error = malo_hal_send_helper(mh, 0, NULL, 0, MALO_NOWAIT);
4023c7e78d3SWeongyo Jeong 
4033c7e78d3SWeongyo Jeong fail:
4043c7e78d3SWeongyo Jeong 	firmware_put(fw, FIRMWARE_UNLOAD);
4053c7e78d3SWeongyo Jeong 
4063c7e78d3SWeongyo Jeong 	return (error);
4073c7e78d3SWeongyo Jeong }
4083c7e78d3SWeongyo Jeong 
4093c7e78d3SWeongyo Jeong /*
4103c7e78d3SWeongyo Jeong  * Firmware block xmit when talking to the 1st-stage loader.
4113c7e78d3SWeongyo Jeong  */
4123c7e78d3SWeongyo Jeong static int
4133c7e78d3SWeongyo Jeong malo_hal_send_main(struct malo_hal *mh, const void *data, size_t dsize,
4143c7e78d3SWeongyo Jeong     uint16_t seqnum, int waitfor)
4153c7e78d3SWeongyo Jeong {
4163c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[0] = htole16(MALO_HOSTCMD_CODE_DNLD);
4173c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[1] = htole16(dsize);
4183c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[2] = htole16(seqnum);
4193c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[3] = 0;
4203c7e78d3SWeongyo Jeong 	memcpy(&mh->mh_cmdbuf[4], data, dsize);
4213c7e78d3SWeongyo Jeong 
4223c7e78d3SWeongyo Jeong 	malo_hal_trigger_pcicmd(mh);
4233c7e78d3SWeongyo Jeong 
4243c7e78d3SWeongyo Jeong 	if (waitfor == MALO_NOWAIT)
4253c7e78d3SWeongyo Jeong 		goto pass;
4263c7e78d3SWeongyo Jeong 
4273c7e78d3SWeongyo Jeong 	if (malo_hal_waitfor(mh, MALO_INT_CODE_CMD_FINISHED) != 0) {
4283c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev,
4293c7e78d3SWeongyo Jeong 		    "%s: timeout waiting for CMD_FINISHED, INT_CODE 0x%x\n",
4303c7e78d3SWeongyo Jeong 		    __func__, malo_hal_read4(mh, MALO_REG_INT_CODE));
4313c7e78d3SWeongyo Jeong 
4323c7e78d3SWeongyo Jeong 		return ETIMEDOUT;
4333c7e78d3SWeongyo Jeong 	}
4343c7e78d3SWeongyo Jeong 
4353c7e78d3SWeongyo Jeong pass:
4363c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_INT_CODE, 0);
4373c7e78d3SWeongyo Jeong 
4383c7e78d3SWeongyo Jeong 	return 0;
4393c7e78d3SWeongyo Jeong }
4403c7e78d3SWeongyo Jeong 
4413c7e78d3SWeongyo Jeong static int
4423c7e78d3SWeongyo Jeong malo_hal_fwload_main(struct malo_hal *mh, char *firmware)
4433c7e78d3SWeongyo Jeong {
4443c7e78d3SWeongyo Jeong 	const struct firmware *fw;
4453c7e78d3SWeongyo Jeong 	const uint8_t *fp;
4463c7e78d3SWeongyo Jeong 	int error;
4473c7e78d3SWeongyo Jeong 	size_t count;
4483c7e78d3SWeongyo Jeong 	uint16_t seqnum;
4493c7e78d3SWeongyo Jeong 	uint32_t blocksize;
4503c7e78d3SWeongyo Jeong 
4513c7e78d3SWeongyo Jeong 	error = 0;
4523c7e78d3SWeongyo Jeong 
4533c7e78d3SWeongyo Jeong 	fw = firmware_get(firmware);
4543c7e78d3SWeongyo Jeong 	if (fw == NULL) {
4553c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "could not read firmware %s!\n",
4563c7e78d3SWeongyo Jeong 		    firmware);
4573c7e78d3SWeongyo Jeong 		return (EIO);
4583c7e78d3SWeongyo Jeong 	}
4593c7e78d3SWeongyo Jeong 
46034d381f9SWeongyo Jeong 	device_printf(mh->mh_dev, "load %s firmware image (%zu bytes)\n",
4613c7e78d3SWeongyo Jeong 	    firmware, fw->datasize);
4623c7e78d3SWeongyo Jeong 
4633c7e78d3SWeongyo Jeong 	seqnum = 1;
4643c7e78d3SWeongyo Jeong 	for (count = 0; count < fw->datasize; count += blocksize) {
4653c7e78d3SWeongyo Jeong 		blocksize = MIN(256, fw->datasize - count);
4663c7e78d3SWeongyo Jeong 		fp = (const uint8_t *)fw->data + count;
4673c7e78d3SWeongyo Jeong 
4683c7e78d3SWeongyo Jeong 		error = malo_hal_send_main(mh, fp, blocksize, seqnum++,
4693c7e78d3SWeongyo Jeong 		    MALO_NOWAIT);
4703c7e78d3SWeongyo Jeong 		if (error != 0)
4713c7e78d3SWeongyo Jeong 			goto fail;
4723c7e78d3SWeongyo Jeong 		DELAY(500);
4733c7e78d3SWeongyo Jeong 	}
4743c7e78d3SWeongyo Jeong 
4753c7e78d3SWeongyo Jeong 	/*
4763c7e78d3SWeongyo Jeong 	 * send a command with size 0 to tell that the firmware has been
4773c7e78d3SWeongyo Jeong 	 * uploaded
4783c7e78d3SWeongyo Jeong 	 */
4793c7e78d3SWeongyo Jeong 	error = malo_hal_send_main(mh, NULL, 0, seqnum++, MALO_NOWAIT);
4803c7e78d3SWeongyo Jeong 	DELAY(100);
4813c7e78d3SWeongyo Jeong 
4823c7e78d3SWeongyo Jeong fail:
4833c7e78d3SWeongyo Jeong 	firmware_put(fw, FIRMWARE_UNLOAD);
4843c7e78d3SWeongyo Jeong 
4853c7e78d3SWeongyo Jeong 	return (error);
4863c7e78d3SWeongyo Jeong }
4873c7e78d3SWeongyo Jeong 
4883c7e78d3SWeongyo Jeong int
4893c7e78d3SWeongyo Jeong malo_hal_fwload(struct malo_hal *mh, char *helper, char *firmware)
4903c7e78d3SWeongyo Jeong {
4913c7e78d3SWeongyo Jeong 	int error, i;
4923c7e78d3SWeongyo Jeong 	uint32_t fwreadysig, opmode;
4933c7e78d3SWeongyo Jeong 
4943c7e78d3SWeongyo Jeong 	/*
4953c7e78d3SWeongyo Jeong 	 * NB: now malo(4) supports only STA mode.  It will be better if it
4963c7e78d3SWeongyo Jeong 	 * supports AP mode.
4973c7e78d3SWeongyo Jeong 	 */
4983c7e78d3SWeongyo Jeong 	fwreadysig = MALO_HOSTCMD_STA_FWRDY_SIGNATURE;
4993c7e78d3SWeongyo Jeong 	opmode = MALO_HOSTCMD_STA_MODE;
5003c7e78d3SWeongyo Jeong 
5013c7e78d3SWeongyo Jeong 	malo_hal_fw_reset(mh);
5023c7e78d3SWeongyo Jeong 
5033c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_CLEAR_SEL,
5043c7e78d3SWeongyo Jeong 	    MALO_A2HRIC_BIT_MASK);
5053c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_CAUSE, 0x00);
5063c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_MASK, 0x00);
5073c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_STATUS_MASK,
5083c7e78d3SWeongyo Jeong 	    MALO_A2HRIC_BIT_MASK);
5093c7e78d3SWeongyo Jeong 
5103c7e78d3SWeongyo Jeong 	error = malo_hal_fwload_helper(mh, helper);
5113c7e78d3SWeongyo Jeong 	if (error != 0) {
5123c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "failed to load bootrom loader.\n");
5133c7e78d3SWeongyo Jeong 		goto fail;
5143c7e78d3SWeongyo Jeong 	}
5153c7e78d3SWeongyo Jeong 
5163c7e78d3SWeongyo Jeong 	DELAY(200 * MALO_FW_CHECK_USECS);
5173c7e78d3SWeongyo Jeong 
5183c7e78d3SWeongyo Jeong 	error = malo_hal_fwload_main(mh, firmware);
5193c7e78d3SWeongyo Jeong 	if (error != 0) {
5203c7e78d3SWeongyo Jeong 		device_printf(mh->mh_dev, "failed to load firmware.\n");
5213c7e78d3SWeongyo Jeong 		goto fail;
5223c7e78d3SWeongyo Jeong 	}
5233c7e78d3SWeongyo Jeong 
5243c7e78d3SWeongyo Jeong 	/*
5253c7e78d3SWeongyo Jeong 	 * Wait for firmware to startup; we monitor the INT_CODE register
5263c7e78d3SWeongyo Jeong 	 * waiting for a signature to written back indicating it's ready to go.
5273c7e78d3SWeongyo Jeong 	 */
5283c7e78d3SWeongyo Jeong 	mh->mh_cmdbuf[1] = 0;
5293c7e78d3SWeongyo Jeong 
5303c7e78d3SWeongyo Jeong 	if (opmode != MALO_HOSTCMD_STA_MODE)
5313c7e78d3SWeongyo Jeong 		malo_hal_trigger_pcicmd(mh);
5323c7e78d3SWeongyo Jeong 
5333c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_FW_MAX_NUM_CHECKS; i++) {
5343c7e78d3SWeongyo Jeong 		malo_hal_write4(mh, MALO_REG_GEN_PTR, opmode);
5353c7e78d3SWeongyo Jeong 		DELAY(MALO_FW_CHECK_USECS);
5363c7e78d3SWeongyo Jeong 		if (malo_hal_read4(mh, MALO_REG_INT_CODE) == fwreadysig) {
5373c7e78d3SWeongyo Jeong 			malo_hal_write4(mh, MALO_REG_INT_CODE, 0x00);
5383c7e78d3SWeongyo Jeong 			return malo_hal_resetstate(mh);
5393c7e78d3SWeongyo Jeong 		}
5403c7e78d3SWeongyo Jeong 	}
5413c7e78d3SWeongyo Jeong 
5423c7e78d3SWeongyo Jeong 	return ETIMEDOUT;
5433c7e78d3SWeongyo Jeong fail:
5443c7e78d3SWeongyo Jeong 	malo_hal_fw_reset(mh);
5453c7e78d3SWeongyo Jeong 
5463c7e78d3SWeongyo Jeong 	return (error);
5473c7e78d3SWeongyo Jeong }
5483c7e78d3SWeongyo Jeong 
5493c7e78d3SWeongyo Jeong /*
5503c7e78d3SWeongyo Jeong  * Return "hw specs".  Note this must be the first cmd MUST be done after
5513c7e78d3SWeongyo Jeong  * a firmware download or the f/w will lockup.
5523c7e78d3SWeongyo Jeong  */
5533c7e78d3SWeongyo Jeong int
5543c7e78d3SWeongyo Jeong malo_hal_gethwspecs(struct malo_hal *mh, struct malo_hal_hwspec *hw)
5553c7e78d3SWeongyo Jeong {
5563c7e78d3SWeongyo Jeong 	struct malo_cmd_get_hwspec *cmd;
5573c7e78d3SWeongyo Jeong 	int ret;
5583c7e78d3SWeongyo Jeong 
5593c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
5603c7e78d3SWeongyo Jeong 
5613c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_get_hwspec, MALO_HOSTCMD_GET_HW_SPEC);
5623c7e78d3SWeongyo Jeong 	memset(&cmd->permaddr[0], 0xff, IEEE80211_ADDR_LEN);
5633c7e78d3SWeongyo Jeong 	cmd->ul_fw_awakecookie = htole32((unsigned int)mh->mh_cmdaddr + 2048);
5643c7e78d3SWeongyo Jeong 
5653c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_GET_HW_SPEC);
5663c7e78d3SWeongyo Jeong 	if (ret == 0) {
5673c7e78d3SWeongyo Jeong 		IEEE80211_ADDR_COPY(hw->macaddr, cmd->permaddr);
5683c7e78d3SWeongyo Jeong 		hw->wcbbase[0] = le32toh(cmd->wcbbase0) & 0x0000ffff;
5693c7e78d3SWeongyo Jeong 		hw->wcbbase[1] = le32toh(cmd->wcbbase1) & 0x0000ffff;
5703c7e78d3SWeongyo Jeong 		hw->wcbbase[2] = le32toh(cmd->wcbbase2) & 0x0000ffff;
5713c7e78d3SWeongyo Jeong 		hw->wcbbase[3] = le32toh(cmd->wcbbase3) & 0x0000ffff;
5723c7e78d3SWeongyo Jeong 		hw->rxdesc_read = le32toh(cmd->rxpdrd_ptr)& 0x0000ffff;
5733c7e78d3SWeongyo Jeong 		hw->rxdesc_write = le32toh(cmd->rxpdwr_ptr)& 0x0000ffff;
5743c7e78d3SWeongyo Jeong 		hw->regioncode = le16toh(cmd->regioncode) & 0x00ff;
5753c7e78d3SWeongyo Jeong 		hw->fw_releasenum = le32toh(cmd->fw_releasenum);
5763c7e78d3SWeongyo Jeong 		hw->maxnum_wcb = le16toh(cmd->num_wcb);
5773c7e78d3SWeongyo Jeong 		hw->maxnum_mcaddr = le16toh(cmd->num_mcastaddr);
5783c7e78d3SWeongyo Jeong 		hw->num_antenna = le16toh(cmd->num_antenna);
5793c7e78d3SWeongyo Jeong 		hw->hwversion = cmd->version;
5803c7e78d3SWeongyo Jeong 		hw->hostinterface = cmd->hostif;
5813c7e78d3SWeongyo Jeong 	}
5823c7e78d3SWeongyo Jeong 
5833c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
5843c7e78d3SWeongyo Jeong 
5853c7e78d3SWeongyo Jeong 	return ret;
5863c7e78d3SWeongyo Jeong }
5873c7e78d3SWeongyo Jeong 
5883c7e78d3SWeongyo Jeong void
5893c7e78d3SWeongyo Jeong malo_hal_detach(struct malo_hal *mh)
5903c7e78d3SWeongyo Jeong {
5913c7e78d3SWeongyo Jeong 
5923c7e78d3SWeongyo Jeong 	bus_dmamem_free(mh->mh_dmat, mh->mh_cmdbuf, mh->mh_dmamap);
5933c7e78d3SWeongyo Jeong 	bus_dmamap_destroy(mh->mh_dmat, mh->mh_dmamap);
5943c7e78d3SWeongyo Jeong 	bus_dma_tag_destroy(mh->mh_dmat);
5953c7e78d3SWeongyo Jeong 	mtx_destroy(&mh->mh_mtx);
5963c7e78d3SWeongyo Jeong 	free(mh, M_DEVBUF);
5973c7e78d3SWeongyo Jeong }
5983c7e78d3SWeongyo Jeong 
5993c7e78d3SWeongyo Jeong /*
6003c7e78d3SWeongyo Jeong  * Configure antenna use.  Takes effect immediately.
6013c7e78d3SWeongyo Jeong  *
6023c7e78d3SWeongyo Jeong  * XXX tx antenna setting ignored
6033c7e78d3SWeongyo Jeong  * XXX rx antenna setting should always be 3 (for now)
6043c7e78d3SWeongyo Jeong  */
6053c7e78d3SWeongyo Jeong int
6063c7e78d3SWeongyo Jeong malo_hal_setantenna(struct malo_hal *mh, enum malo_hal_antenna dirset, int ant)
6073c7e78d3SWeongyo Jeong {
6083c7e78d3SWeongyo Jeong 	struct malo_cmd_rf_antenna *cmd;
6093c7e78d3SWeongyo Jeong 	int ret;
6103c7e78d3SWeongyo Jeong 
6113c7e78d3SWeongyo Jeong 	if (!(dirset == MHA_ANTENNATYPE_RX || dirset == MHA_ANTENNATYPE_TX))
6123c7e78d3SWeongyo Jeong 		return EINVAL;
6133c7e78d3SWeongyo Jeong 
6143c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
6153c7e78d3SWeongyo Jeong 
6163c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_rf_antenna,
6173c7e78d3SWeongyo Jeong 	    MALO_HOSTCMD_802_11_RF_ANTENNA);
6183c7e78d3SWeongyo Jeong 	cmd->action = htole16(dirset);
6193c7e78d3SWeongyo Jeong 	if (ant == 0) {			/* default to all/both antennae */
6203c7e78d3SWeongyo Jeong 		/* XXX never reach now.  */
6213c7e78d3SWeongyo Jeong 		ant = 3;
6223c7e78d3SWeongyo Jeong 	}
6233c7e78d3SWeongyo Jeong 	cmd->mode = htole16(ant);
6243c7e78d3SWeongyo Jeong 
6253c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_802_11_RF_ANTENNA);
6263c7e78d3SWeongyo Jeong 
6273c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
6283c7e78d3SWeongyo Jeong 
6293c7e78d3SWeongyo Jeong 	return ret;
6303c7e78d3SWeongyo Jeong }
6313c7e78d3SWeongyo Jeong 
6323c7e78d3SWeongyo Jeong /*
6333c7e78d3SWeongyo Jeong  * Configure radio.  Takes effect immediately.
6343c7e78d3SWeongyo Jeong  *
6353c7e78d3SWeongyo Jeong  * XXX preamble installed after set fixed rate cmd
6363c7e78d3SWeongyo Jeong  */
6373c7e78d3SWeongyo Jeong int
6383c7e78d3SWeongyo Jeong malo_hal_setradio(struct malo_hal *mh, int onoff,
6393c7e78d3SWeongyo Jeong     enum malo_hal_preamble preamble)
6403c7e78d3SWeongyo Jeong {
6413c7e78d3SWeongyo Jeong 	struct malo_cmd_radio_control *cmd;
6423c7e78d3SWeongyo Jeong 	int ret;
6433c7e78d3SWeongyo Jeong 
6443c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
6453c7e78d3SWeongyo Jeong 
6463c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_radio_control,
6473c7e78d3SWeongyo Jeong 	    MALO_HOSTCMD_802_11_RADIO_CONTROL);
6483c7e78d3SWeongyo Jeong 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET);
6493c7e78d3SWeongyo Jeong 	if (onoff == 0)
6503c7e78d3SWeongyo Jeong 		cmd->control = 0;
6513c7e78d3SWeongyo Jeong 	else
6523c7e78d3SWeongyo Jeong 		cmd->control = htole16(preamble);
6533c7e78d3SWeongyo Jeong 	cmd->radio_on = htole16(onoff);
6543c7e78d3SWeongyo Jeong 
6553c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_802_11_RADIO_CONTROL);
6563c7e78d3SWeongyo Jeong 
6573c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
6583c7e78d3SWeongyo Jeong 
6593c7e78d3SWeongyo Jeong 	return ret;
6603c7e78d3SWeongyo Jeong }
6613c7e78d3SWeongyo Jeong 
6623c7e78d3SWeongyo Jeong /*
6633c7e78d3SWeongyo Jeong  * Set the interrupt mask.
6643c7e78d3SWeongyo Jeong  */
6653c7e78d3SWeongyo Jeong void
6663c7e78d3SWeongyo Jeong malo_hal_intrset(struct malo_hal *mh, uint32_t mask)
6673c7e78d3SWeongyo Jeong {
6683c7e78d3SWeongyo Jeong 
6693c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_MASK, 0);
6703c7e78d3SWeongyo Jeong 	(void)malo_hal_read4(mh, MALO_REG_INT_CODE);
6713c7e78d3SWeongyo Jeong 
6723c7e78d3SWeongyo Jeong 	mh->mh_imask = mask;
6733c7e78d3SWeongyo Jeong 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_MASK, mask);
6743c7e78d3SWeongyo Jeong 	(void)malo_hal_read4(mh, MALO_REG_INT_CODE);
6753c7e78d3SWeongyo Jeong }
6763c7e78d3SWeongyo Jeong 
6773c7e78d3SWeongyo Jeong int
6783c7e78d3SWeongyo Jeong malo_hal_setchannel(struct malo_hal *mh, const struct malo_hal_channel *chan)
6793c7e78d3SWeongyo Jeong {
6803c7e78d3SWeongyo Jeong 	struct malo_cmd_fw_set_rf_channel *cmd;
6813c7e78d3SWeongyo Jeong 	int ret;
6823c7e78d3SWeongyo Jeong 
6833c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
6843c7e78d3SWeongyo Jeong 
6853c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_fw_set_rf_channel,
6863c7e78d3SWeongyo Jeong 	    MALO_HOSTCMD_SET_RF_CHANNEL);
6873c7e78d3SWeongyo Jeong 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET);
6883c7e78d3SWeongyo Jeong 	cmd->cur_channel = chan->channel;
6893c7e78d3SWeongyo Jeong 
6903c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_RF_CHANNEL);
6913c7e78d3SWeongyo Jeong 
6923c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
6933c7e78d3SWeongyo Jeong 
6943c7e78d3SWeongyo Jeong 	return ret;
6953c7e78d3SWeongyo Jeong }
6963c7e78d3SWeongyo Jeong 
6973c7e78d3SWeongyo Jeong int
6983c7e78d3SWeongyo Jeong malo_hal_settxpower(struct malo_hal *mh, const struct malo_hal_channel *c)
6993c7e78d3SWeongyo Jeong {
7003c7e78d3SWeongyo Jeong 	struct malo_cmd_rf_tx_power *cmd;
7013c7e78d3SWeongyo Jeong 	const struct malo_hal_caldata *cal = &mh->mh_caldata;
7023c7e78d3SWeongyo Jeong 	uint8_t chan = c->channel;
7033c7e78d3SWeongyo Jeong 	uint16_t pow;
7043c7e78d3SWeongyo Jeong 	int i, idx, ret;
7053c7e78d3SWeongyo Jeong 
7063c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
7073c7e78d3SWeongyo Jeong 
7083c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_rf_tx_power,
7093c7e78d3SWeongyo Jeong 	    MALO_HOSTCMD_802_11_RF_TX_POWER);
7103c7e78d3SWeongyo Jeong 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET_LIST);
7113c7e78d3SWeongyo Jeong 	for (i = 0; i < 4; i++) {
7123c7e78d3SWeongyo Jeong 		idx = (chan - 1) * 4 + i;
7133c7e78d3SWeongyo Jeong 		pow = cal->pt_ratetable_20m[idx];
7143c7e78d3SWeongyo Jeong 		cmd->power_levellist[i] = htole16(pow);
7153c7e78d3SWeongyo Jeong 	}
7163c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_802_11_RF_TX_POWER);
7173c7e78d3SWeongyo Jeong 
7183c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
7193c7e78d3SWeongyo Jeong 
7203c7e78d3SWeongyo Jeong 	return ret;
7213c7e78d3SWeongyo Jeong }
7223c7e78d3SWeongyo Jeong 
7233c7e78d3SWeongyo Jeong int
7243c7e78d3SWeongyo Jeong malo_hal_setpromisc(struct malo_hal *mh, int enable)
7253c7e78d3SWeongyo Jeong {
7263c7e78d3SWeongyo Jeong 	/* XXX need host cmd */
7273c7e78d3SWeongyo Jeong 	return 0;
7283c7e78d3SWeongyo Jeong }
7293c7e78d3SWeongyo Jeong 
7303c7e78d3SWeongyo Jeong int
7313c7e78d3SWeongyo Jeong malo_hal_setassocid(struct malo_hal *mh,
7323c7e78d3SWeongyo Jeong     const uint8_t bssid[IEEE80211_ADDR_LEN], uint16_t associd)
7333c7e78d3SWeongyo Jeong {
7343c7e78d3SWeongyo Jeong 	struct malo_cmd_fw_set_aid *cmd;
7353c7e78d3SWeongyo Jeong 	int ret;
7363c7e78d3SWeongyo Jeong 
7373c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
7383c7e78d3SWeongyo Jeong 
7393c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_fw_set_aid,
7403c7e78d3SWeongyo Jeong 	    MALO_HOSTCMD_SET_AID);
7413c7e78d3SWeongyo Jeong 	cmd->cmdhdr.seqnum = 1;
7423c7e78d3SWeongyo Jeong 	cmd->associd = htole16(associd);
7433c7e78d3SWeongyo Jeong 	IEEE80211_ADDR_COPY(&cmd->macaddr[0], bssid);
7443c7e78d3SWeongyo Jeong 
7453c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_AID);
7463c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
7473c7e78d3SWeongyo Jeong 	return ret;
7483c7e78d3SWeongyo Jeong }
7493c7e78d3SWeongyo Jeong 
7503c7e78d3SWeongyo Jeong /*
7513c7e78d3SWeongyo Jeong  * Kick the firmware to tell it there are new tx descriptors
7523c7e78d3SWeongyo Jeong  * for processing.  The driver says what h/w q has work in
7533c7e78d3SWeongyo Jeong  * case the f/w ever gets smarter.
7543c7e78d3SWeongyo Jeong  */
7553c7e78d3SWeongyo Jeong void
7563c7e78d3SWeongyo Jeong malo_hal_txstart(struct malo_hal *mh, int qnum)
7573c7e78d3SWeongyo Jeong {
7583c7e78d3SWeongyo Jeong 	bus_space_write_4(mh->mh_iot, mh->mh_ioh,
7593c7e78d3SWeongyo Jeong 	    MALO_REG_H2A_INTERRUPT_EVENTS, MALO_H2ARIC_BIT_PPA_READY);
7603c7e78d3SWeongyo Jeong 	(void) bus_space_read_4(mh->mh_iot, mh->mh_ioh, MALO_REG_INT_CODE);
7613c7e78d3SWeongyo Jeong }
7623c7e78d3SWeongyo Jeong 
7633c7e78d3SWeongyo Jeong /*
7643c7e78d3SWeongyo Jeong  * Return the current ISR setting and clear the cause.
7653c7e78d3SWeongyo Jeong  */
7663c7e78d3SWeongyo Jeong void
7673c7e78d3SWeongyo Jeong malo_hal_getisr(struct malo_hal *mh, uint32_t *status)
7683c7e78d3SWeongyo Jeong {
7693c7e78d3SWeongyo Jeong 	uint32_t cause;
7703c7e78d3SWeongyo Jeong 
7713c7e78d3SWeongyo Jeong 	cause = bus_space_read_4(mh->mh_iot, mh->mh_ioh,
7723c7e78d3SWeongyo Jeong 	    MALO_REG_A2H_INTERRUPT_CAUSE);
7733c7e78d3SWeongyo Jeong 	if (cause == 0xffffffff) {	/* card removed */
7743c7e78d3SWeongyo Jeong 		cause = 0;
7753c7e78d3SWeongyo Jeong 	} else if (cause != 0) {
7763c7e78d3SWeongyo Jeong 		/* clear cause bits */
7773c7e78d3SWeongyo Jeong 		bus_space_write_4(mh->mh_iot, mh->mh_ioh,
7783c7e78d3SWeongyo Jeong 		    MALO_REG_A2H_INTERRUPT_CAUSE, cause &~ mh->mh_imask);
7793c7e78d3SWeongyo Jeong 		(void) bus_space_read_4(mh->mh_iot, mh->mh_ioh,
7803c7e78d3SWeongyo Jeong 		    MALO_REG_INT_CODE);
7813c7e78d3SWeongyo Jeong 		cause &= mh->mh_imask;
7823c7e78d3SWeongyo Jeong 	}
7833c7e78d3SWeongyo Jeong 
7843c7e78d3SWeongyo Jeong 	*status = cause;
7853c7e78d3SWeongyo Jeong }
7863c7e78d3SWeongyo Jeong 
7873c7e78d3SWeongyo Jeong /*
7883c7e78d3SWeongyo Jeong  * Callback from the driver on a cmd done interrupt.  Nothing to do right
7893c7e78d3SWeongyo Jeong  * now as we spin waiting for cmd completion.
7903c7e78d3SWeongyo Jeong  */
7913c7e78d3SWeongyo Jeong void
7923c7e78d3SWeongyo Jeong malo_hal_cmddone(struct malo_hal *mh)
7933c7e78d3SWeongyo Jeong {
7943c7e78d3SWeongyo Jeong 	/* NB : do nothing.  */
7953c7e78d3SWeongyo Jeong }
7963c7e78d3SWeongyo Jeong 
7973c7e78d3SWeongyo Jeong int
7983c7e78d3SWeongyo Jeong malo_hal_prescan(struct malo_hal *mh)
7993c7e78d3SWeongyo Jeong {
8003c7e78d3SWeongyo Jeong 	struct malo_cmd_prescan *cmd;
8013c7e78d3SWeongyo Jeong 	int ret;
8023c7e78d3SWeongyo Jeong 
8033c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
8043c7e78d3SWeongyo Jeong 
8053c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_prescan, MALO_HOSTCMD_SET_PRE_SCAN);
8063c7e78d3SWeongyo Jeong 	cmd->cmdhdr.seqnum = 1;
8073c7e78d3SWeongyo Jeong 
8083c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_PRE_SCAN);
8093c7e78d3SWeongyo Jeong 
8103c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
8113c7e78d3SWeongyo Jeong 
8123c7e78d3SWeongyo Jeong 	return ret;
8133c7e78d3SWeongyo Jeong }
8143c7e78d3SWeongyo Jeong 
8153c7e78d3SWeongyo Jeong int
8163c7e78d3SWeongyo Jeong malo_hal_postscan(struct malo_hal *mh, uint8_t *macaddr, uint8_t ibsson)
8173c7e78d3SWeongyo Jeong {
8183c7e78d3SWeongyo Jeong 	struct malo_cmd_postscan *cmd;
8193c7e78d3SWeongyo Jeong 	int ret;
8203c7e78d3SWeongyo Jeong 
8213c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
8223c7e78d3SWeongyo Jeong 
8233c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_postscan, MALO_HOSTCMD_SET_POST_SCAN);
8243c7e78d3SWeongyo Jeong 	cmd->cmdhdr.seqnum = 1;
8253c7e78d3SWeongyo Jeong 	cmd->isibss = htole32(ibsson);
8263c7e78d3SWeongyo Jeong 	IEEE80211_ADDR_COPY(&cmd->bssid[0], macaddr);
8273c7e78d3SWeongyo Jeong 
8283c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_POST_SCAN);
8293c7e78d3SWeongyo Jeong 
8303c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
8313c7e78d3SWeongyo Jeong 
8323c7e78d3SWeongyo Jeong 	return ret;
8333c7e78d3SWeongyo Jeong }
8343c7e78d3SWeongyo Jeong 
8353c7e78d3SWeongyo Jeong int
8363c7e78d3SWeongyo Jeong malo_hal_set_slot(struct malo_hal *mh, int is_short)
8373c7e78d3SWeongyo Jeong {
8383c7e78d3SWeongyo Jeong 	int ret;
8393c7e78d3SWeongyo Jeong 	struct malo_cmd_fw_setslot *cmd;
8403c7e78d3SWeongyo Jeong 
8413c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
8423c7e78d3SWeongyo Jeong 
8433c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_fw_setslot, MALO_HOSTCMD_SET_SLOT);
8443c7e78d3SWeongyo Jeong 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET);
8453c7e78d3SWeongyo Jeong 	cmd->slot = (is_short == 1 ? 1 : 0);
8463c7e78d3SWeongyo Jeong 
8473c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_SLOT);
8483c7e78d3SWeongyo Jeong 
8493c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
8503c7e78d3SWeongyo Jeong 
8513c7e78d3SWeongyo Jeong 	return ret;
8523c7e78d3SWeongyo Jeong }
8533c7e78d3SWeongyo Jeong 
8543c7e78d3SWeongyo Jeong int
8553c7e78d3SWeongyo Jeong malo_hal_set_rate(struct malo_hal *mh, uint16_t curmode, uint8_t rate)
8563c7e78d3SWeongyo Jeong {
8573c7e78d3SWeongyo Jeong 	int i, ret;
8583c7e78d3SWeongyo Jeong 	struct malo_cmd_set_rate *cmd;
8593c7e78d3SWeongyo Jeong 
8603c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
8613c7e78d3SWeongyo Jeong 
8623c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_set_rate, MALO_HOSTCMD_SET_RATE);
8633c7e78d3SWeongyo Jeong 	cmd->aprates[0] = 2;
8643c7e78d3SWeongyo Jeong 	cmd->aprates[1] = 4;
8653c7e78d3SWeongyo Jeong 	cmd->aprates[2] = 11;
8663c7e78d3SWeongyo Jeong 	cmd->aprates[3] = 22;
8673c7e78d3SWeongyo Jeong 	if (curmode == IEEE80211_MODE_11G) {
8683c7e78d3SWeongyo Jeong 		cmd->aprates[4] = 0;		/* XXX reserved?  */
8693c7e78d3SWeongyo Jeong 		cmd->aprates[5] = 12;
8703c7e78d3SWeongyo Jeong 		cmd->aprates[6] = 18;
8713c7e78d3SWeongyo Jeong 		cmd->aprates[7] = 24;
8723c7e78d3SWeongyo Jeong 		cmd->aprates[8] = 36;
8733c7e78d3SWeongyo Jeong 		cmd->aprates[9] = 48;
8743c7e78d3SWeongyo Jeong 		cmd->aprates[10] = 72;
8753c7e78d3SWeongyo Jeong 		cmd->aprates[11] = 96;
8763c7e78d3SWeongyo Jeong 		cmd->aprates[12] = 108;
8773c7e78d3SWeongyo Jeong 	}
8783c7e78d3SWeongyo Jeong 
8793c7e78d3SWeongyo Jeong 	if (rate != 0) {
8803c7e78d3SWeongyo Jeong 		/* fixed rate */
8813c7e78d3SWeongyo Jeong 		for (i = 0; i < 13; i++) {
8823c7e78d3SWeongyo Jeong 			if (cmd->aprates[i] == rate) {
8833c7e78d3SWeongyo Jeong 				cmd->rateindex = i;
8843c7e78d3SWeongyo Jeong 				cmd->dataratetype = 1;
8853c7e78d3SWeongyo Jeong 				break;
8863c7e78d3SWeongyo Jeong 			}
8873c7e78d3SWeongyo Jeong 		}
8883c7e78d3SWeongyo Jeong 	}
8893c7e78d3SWeongyo Jeong 
8903c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_RATE);
8913c7e78d3SWeongyo Jeong 
8923c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
8933c7e78d3SWeongyo Jeong 
8943c7e78d3SWeongyo Jeong 	return ret;
8953c7e78d3SWeongyo Jeong }
8963c7e78d3SWeongyo Jeong 
8973c7e78d3SWeongyo Jeong int
8983c7e78d3SWeongyo Jeong malo_hal_setmcast(struct malo_hal *mh, int nmc, const uint8_t macs[])
8993c7e78d3SWeongyo Jeong {
9003c7e78d3SWeongyo Jeong 	struct malo_cmd_mcast *cmd;
9013c7e78d3SWeongyo Jeong 	int ret;
9023c7e78d3SWeongyo Jeong 
9033c7e78d3SWeongyo Jeong 	if (nmc > MALO_HAL_MCAST_MAX)
9043c7e78d3SWeongyo Jeong 		return EINVAL;
9053c7e78d3SWeongyo Jeong 
9063c7e78d3SWeongyo Jeong 	MALO_HAL_LOCK(mh);
9073c7e78d3SWeongyo Jeong 
9083c7e78d3SWeongyo Jeong 	_CMD_SETUP(cmd, struct malo_cmd_mcast, MALO_HOSTCMD_MAC_MULTICAST_ADR);
9093c7e78d3SWeongyo Jeong 	memcpy(cmd->maclist, macs, nmc * IEEE80211_ADDR_LEN);
9103c7e78d3SWeongyo Jeong 	cmd->numaddr = htole16(nmc);
9113c7e78d3SWeongyo Jeong 	cmd->action = htole16(0xffff);
9123c7e78d3SWeongyo Jeong 
9133c7e78d3SWeongyo Jeong 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_MAC_MULTICAST_ADR);
9143c7e78d3SWeongyo Jeong 
9153c7e78d3SWeongyo Jeong 	MALO_HAL_UNLOCK(mh);
9163c7e78d3SWeongyo Jeong 
9173c7e78d3SWeongyo Jeong 	return ret;
9183c7e78d3SWeongyo Jeong }
919