Home | History | Annotate | Line # | Download | only in pcmcia
bt3c.c revision 1.1.2.7
      1  1.1.2.7  yamt /* $NetBSD: bt3c.c,v 1.1.2.7 2007/12/07 17:30:59 yamt Exp $ */
      2  1.1.2.2  yamt 
      3  1.1.2.2  yamt /*-
      4  1.1.2.2  yamt  * Copyright (c) 2005 Iain D. Hibbert,
      5  1.1.2.2  yamt  * All rights reserved.
      6  1.1.2.2  yamt  *
      7  1.1.2.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  yamt  * modification, are permitted provided that the following conditions
      9  1.1.2.2  yamt  * are met:
     10  1.1.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.2  yamt  * 3. The name of the author may not be used to endorse or promote products
     16  1.1.2.2  yamt  *    derived from this software without specific prior written permission.
     17  1.1.2.2  yamt  *
     18  1.1.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1.2.2  yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1.2.2  yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1.2.2  yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1.2.2  yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1.2.2  yamt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1.2.2  yamt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1.2.2  yamt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1.2.2  yamt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1.2.2  yamt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1.2.2  yamt  */
     29  1.1.2.2  yamt 
     30  1.1.2.2  yamt /*
     31  1.1.2.2  yamt  * Driver for the 3Com Bluetooth PC Card 3CRWB6096, written with reference to
     32  1.1.2.2  yamt  *  FreeBSD and BlueZ drivers for same, with credit for those going to:
     33  1.1.2.2  yamt  *
     34  1.1.2.2  yamt  *		Maksim Yevmenkin <m_evmenkin (at) yahoo.com>		(FreeBSD)
     35  1.1.2.2  yamt  *		Marcel Holtmann <marcel (at) holtmann.org>		(BlueZ)
     36  1.1.2.2  yamt  *		Jose Orlando Pereira <jop (at) di.uminho.pt>		(BlueZ)
     37  1.1.2.2  yamt  *		David Hinds <dahinds (at) users.sourceforge.net>	(Original Code)
     38  1.1.2.2  yamt  */
     39  1.1.2.2  yamt 
     40  1.1.2.2  yamt /*
     41  1.1.2.2  yamt  * The CIS info from my card:
     42  1.1.2.2  yamt  *
     43  1.1.2.2  yamt  *	pcmcia1: CIS tuple chain:
     44  1.1.2.2  yamt  *	CISTPL_DEVICE type=null speed=null
     45  1.1.2.2  yamt  *	 01 03 00 00 ff
     46  1.1.2.2  yamt  *	CISTPL_VERS_1
     47  1.1.2.2  yamt  *	 15 24 05 00 33 43 4f 4d 00 33 43 52 57 42 36 30
     48  1.1.2.2  yamt  *	 2d 41 00 42 6c 75 65 74 6f 6f 74 68 20 50 43 20
     49  1.1.2.2  yamt  *	 43 61 72 64 00 ff
     50  1.1.2.2  yamt  *	CISTPL_MANFID
     51  1.1.2.2  yamt  *	 20 04 01 01 40 00
     52  1.1.2.2  yamt  *	CISTPL_FUNCID
     53  1.1.2.2  yamt  *	 21 02 02 01
     54  1.1.2.2  yamt  *	CISTPL_CONFIG
     55  1.1.2.2  yamt  *	 1a 06 05 30 20 03 17 00
     56  1.1.2.2  yamt  *	CISTPL_CFTABLE_ENTRY
     57  1.1.2.2  yamt  *	 1b 09 f0 41 18 a0 40 07 30 ff ff
     58  1.1.2.2  yamt  *	unhandled CISTPL 80
     59  1.1.2.2  yamt  *	 80 0a 02 01 40 00 2d 00 00 00 00 ff
     60  1.1.2.2  yamt  *	CISTPL_NO_LINK
     61  1.1.2.2  yamt  *	 14 00
     62  1.1.2.2  yamt  *	CISTPL_END
     63  1.1.2.2  yamt  *	 ff
     64  1.1.2.2  yamt  *	pcmcia1: CIS version PC Card Standard 5.0
     65  1.1.2.2  yamt  *	pcmcia1: CIS info: 3COM, 3CRWB60-A, Bluetooth PC Card
     66  1.1.2.2  yamt  *	pcmcia1: Manufacturer code 0x101, product 0x40
     67  1.1.2.2  yamt  *	pcmcia1: function 0: serial port, ccr addr 320 mask 17
     68  1.1.2.2  yamt  *	pcmcia1: function 0, config table entry 48: I/O card; irq mask ffff; iomask 0, iospace 0-7; rdybsy_active io8 irqlevel
     69  1.1.2.2  yamt  */
     70  1.1.2.2  yamt 
     71  1.1.2.2  yamt #include <sys/cdefs.h>
     72  1.1.2.7  yamt __KERNEL_RCSID(0, "$NetBSD: bt3c.c,v 1.1.2.7 2007/12/07 17:30:59 yamt Exp $");
     73  1.1.2.2  yamt 
     74  1.1.2.2  yamt #include <sys/param.h>
     75  1.1.2.2  yamt #include <sys/device.h>
     76  1.1.2.2  yamt #include <sys/mbuf.h>
     77  1.1.2.2  yamt #include <sys/systm.h>
     78  1.1.2.2  yamt 
     79  1.1.2.5  yamt #include <sys/cpu.h>
     80  1.1.2.5  yamt #include <sys/bus.h>
     81  1.1.2.5  yamt #include <sys/intr.h>
     82  1.1.2.2  yamt 
     83  1.1.2.2  yamt #include <dev/pcmcia/pcmciareg.h>
     84  1.1.2.2  yamt #include <dev/pcmcia/pcmciavar.h>
     85  1.1.2.2  yamt #include <dev/pcmcia/pcmciadevs.h>
     86  1.1.2.2  yamt 
     87  1.1.2.2  yamt #include <netbt/bluetooth.h>
     88  1.1.2.2  yamt #include <netbt/hci.h>
     89  1.1.2.2  yamt 
     90  1.1.2.2  yamt #include <dev/firmload.h>
     91  1.1.2.2  yamt #define BT3C_FIRMWARE_FILE	"BT3CPCC.bin"
     92  1.1.2.2  yamt 
     93  1.1.2.2  yamt /**************************************************************************
     94  1.1.2.2  yamt  *
     95  1.1.2.2  yamt  *	bt3c autoconfig glue
     96  1.1.2.2  yamt  */
     97  1.1.2.2  yamt 
     98  1.1.2.2  yamt struct bt3c_softc {
     99  1.1.2.6  yamt 	device_t	sc_dev;
    100  1.1.2.2  yamt 
    101  1.1.2.2  yamt 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    102  1.1.2.2  yamt 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    103  1.1.2.2  yamt 	int		sc_iow;			/* our i/o window */
    104  1.1.2.2  yamt 	void		*sc_powerhook;		/* power hook descriptor */
    105  1.1.2.2  yamt 	int		sc_flags;		/* flags */
    106  1.1.2.2  yamt 
    107  1.1.2.7  yamt 	struct hci_unit *sc_unit;		/* Bluetooth HCI Unit */
    108  1.1.2.7  yamt 	struct bt_stats	sc_stats;		/* HCI stats */
    109  1.1.2.2  yamt 
    110  1.1.2.2  yamt 	/* hardware interrupt */
    111  1.1.2.2  yamt 	void		*sc_intr;		/* cookie */
    112  1.1.2.2  yamt 	int		sc_state;		/* receive state */
    113  1.1.2.2  yamt 	int		sc_want;		/* how much we want */
    114  1.1.2.2  yamt 	struct mbuf	*sc_rxp;		/* incoming packet */
    115  1.1.2.2  yamt 	struct mbuf	*sc_txp;		/* outgoing packet */
    116  1.1.2.7  yamt 
    117  1.1.2.7  yamt 	/* transmit queues */
    118  1.1.2.7  yamt 	MBUFQ_HEAD()	sc_cmdq;		/* commands */
    119  1.1.2.7  yamt 	MBUFQ_HEAD()	sc_aclq;		/* ACL data */
    120  1.1.2.7  yamt 	MBUFQ_HEAD()	sc_scoq;		/* SCO data */
    121  1.1.2.2  yamt };
    122  1.1.2.2  yamt 
    123  1.1.2.2  yamt /* sc_state */				/* receiving */
    124  1.1.2.2  yamt #define BT3C_RECV_PKT_TYPE	0		/* packet type */
    125  1.1.2.2  yamt #define BT3C_RECV_ACL_HDR	1		/* acl header */
    126  1.1.2.2  yamt #define BT3C_RECV_SCO_HDR	2		/* sco header */
    127  1.1.2.2  yamt #define BT3C_RECV_EVENT_HDR	3		/* event header */
    128  1.1.2.2  yamt #define BT3C_RECV_ACL_DATA	4		/* acl packet data */
    129  1.1.2.2  yamt #define BT3C_RECV_SCO_DATA	5		/* sco packet data */
    130  1.1.2.2  yamt #define BT3C_RECV_EVENT_DATA	6		/* event packet data */
    131  1.1.2.2  yamt 
    132  1.1.2.2  yamt /* sc_flags */
    133  1.1.2.2  yamt #define BT3C_SLEEPING		(1 << 0)	/* but not with the fishes */
    134  1.1.2.7  yamt #define BT3C_XMIT		(1 << 1)	/* transmit active */
    135  1.1.2.7  yamt #define BT3C_ENABLED		(1 << 2)	/* enabled */
    136  1.1.2.2  yamt 
    137  1.1.2.6  yamt static int bt3c_match(device_t, struct cfdata *, void *);
    138  1.1.2.6  yamt static void bt3c_attach(device_t, device_t, void *);
    139  1.1.2.6  yamt static int bt3c_detach(device_t, int);
    140  1.1.2.2  yamt static void bt3c_power(int, void *);
    141  1.1.2.2  yamt 
    142  1.1.2.6  yamt CFATTACH_DECL_NEW(bt3c, sizeof(struct bt3c_softc),
    143  1.1.2.5  yamt     bt3c_match, bt3c_attach, bt3c_detach, NULL);
    144  1.1.2.2  yamt 
    145  1.1.2.6  yamt static int bt3c_enable(device_t);
    146  1.1.2.6  yamt static void bt3c_disable(device_t);
    147  1.1.2.7  yamt static void bt3c_output_cmd(device_t, struct mbuf *);
    148  1.1.2.7  yamt static void bt3c_output_acl(device_t, struct mbuf *);
    149  1.1.2.7  yamt static void bt3c_output_sco(device_t, struct mbuf *);
    150  1.1.2.7  yamt static void bt3c_stats(device_t, struct bt_stats *, int);
    151  1.1.2.7  yamt 
    152  1.1.2.7  yamt static const struct hci_if bt3c_hci = {
    153  1.1.2.7  yamt 	.enable =	bt3c_enable,
    154  1.1.2.7  yamt 	.disable =	bt3c_disable,
    155  1.1.2.7  yamt 	.output_cmd =	bt3c_output_cmd,
    156  1.1.2.7  yamt 	.output_acl =	bt3c_output_acl,
    157  1.1.2.7  yamt 	.output_sco =	bt3c_output_sco,
    158  1.1.2.7  yamt 	.get_stats =	bt3c_stats,
    159  1.1.2.7  yamt 	.ipl =		IPL_TTY,
    160  1.1.2.7  yamt };
    161  1.1.2.7  yamt 
    162  1.1.2.7  yamt static void bt3c_start(struct bt3c_softc *);
    163  1.1.2.2  yamt 
    164  1.1.2.2  yamt /**************************************************************************
    165  1.1.2.2  yamt  *
    166  1.1.2.2  yamt  *	Hardware Definitions & IO routines
    167  1.1.2.2  yamt  *
    168  1.1.2.2  yamt  *	I made up the names for most of these defs since we dont have
    169  1.1.2.2  yamt  *	manufacturers recommendations, but I dont like raw numbers..
    170  1.1.2.2  yamt  *
    171  1.1.2.2  yamt  *	all hardware routines are running at IPL_TTY
    172  1.1.2.2  yamt  *
    173  1.1.2.2  yamt  */
    174  1.1.2.2  yamt #define BT3C_ISR		0x7001		/* Interrupt Status Register */
    175  1.1.2.2  yamt #define BT3C_ISR_RXRDY			(1<<0)	/* Device has data */
    176  1.1.2.2  yamt #define BT3C_ISR_TXRDY			(1<<1)	/* Finished sending data */
    177  1.1.2.2  yamt #define BT3C_ISR_ANTENNA		(1<<5)	/* Antenna position changed */
    178  1.1.2.2  yamt 
    179  1.1.2.2  yamt #define BT3C_CSR		0x7002		/* Card Status Register */
    180  1.1.2.2  yamt #define BT3C_CSR_ANTENNA		(1<<4)	/* Antenna position */
    181  1.1.2.2  yamt 
    182  1.1.2.2  yamt #define BT3C_TX_COUNT		0x7005		/* Tx fifo contents */
    183  1.1.2.2  yamt #define BT3C_TX_FIFO		0x7080		/* Transmit Fifo */
    184  1.1.2.2  yamt #define BT3C_RX_COUNT		0x7006		/* Rx fifo contents */
    185  1.1.2.2  yamt #define BT3C_RX_FIFO		0x7480		/* Receive Fifo */
    186  1.1.2.2  yamt #define BT3C_FIFO_SIZE			256
    187  1.1.2.2  yamt 
    188  1.1.2.2  yamt /* IO Registers */
    189  1.1.2.2  yamt #define BT3C_IOR_DATA_L		0x00		/* data low byte */
    190  1.1.2.2  yamt #define BT3C_IOR_DATA_H		0x01		/* data high byte */
    191  1.1.2.2  yamt #define BT3C_IOR_ADDR_L		0x02		/* address low byte */
    192  1.1.2.2  yamt #define BT3C_IOR_ADDR_H		0x03		/* address high byte */
    193  1.1.2.2  yamt #define BT3C_IOR_CNTL		0x04		/* control byte */
    194  1.1.2.2  yamt #define BT3C_IOR_CNTL_BOOT		(1<<6)	/* Boot Card */
    195  1.1.2.2  yamt #define BT3C_IOR_CNTL_INTR		(1<<7)	/* Interrupt Requested */
    196  1.1.2.2  yamt #define BT3C_IOR_LEN		0x05
    197  1.1.2.2  yamt 
    198  1.1.2.2  yamt static inline uint16_t
    199  1.1.2.2  yamt bt3c_get(struct bt3c_softc *sc)
    200  1.1.2.2  yamt {
    201  1.1.2.2  yamt 	uint16_t data;
    202  1.1.2.2  yamt 
    203  1.1.2.2  yamt 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    204  1.1.2.2  yamt 				0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_READ);
    205  1.1.2.2  yamt 	data = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    206  1.1.2.2  yamt 				BT3C_IOR_DATA_L);
    207  1.1.2.2  yamt 	data |= bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    208  1.1.2.2  yamt 				BT3C_IOR_DATA_H) << 8;
    209  1.1.2.2  yamt 
    210  1.1.2.2  yamt 	return data;
    211  1.1.2.2  yamt }
    212  1.1.2.2  yamt 
    213  1.1.2.2  yamt static inline void
    214  1.1.2.2  yamt bt3c_put(struct bt3c_softc *sc, uint16_t data)
    215  1.1.2.2  yamt {
    216  1.1.2.2  yamt 
    217  1.1.2.2  yamt 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    218  1.1.2.2  yamt 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_WRITE);
    219  1.1.2.2  yamt 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    220  1.1.2.2  yamt 			BT3C_IOR_DATA_L, data & 0xff);
    221  1.1.2.2  yamt 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    222  1.1.2.2  yamt 			BT3C_IOR_DATA_H, (data >> 8) & 0xff);
    223  1.1.2.2  yamt }
    224  1.1.2.2  yamt 
    225  1.1.2.2  yamt static inline uint8_t
    226  1.1.2.2  yamt bt3c_read_control(struct bt3c_softc *sc)
    227  1.1.2.2  yamt {
    228  1.1.2.2  yamt 
    229  1.1.2.2  yamt 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    230  1.1.2.2  yamt 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_READ);
    231  1.1.2.2  yamt 	return bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    232  1.1.2.2  yamt 			BT3C_IOR_CNTL);
    233  1.1.2.2  yamt }
    234  1.1.2.2  yamt 
    235  1.1.2.2  yamt static inline void
    236  1.1.2.2  yamt bt3c_write_control(struct bt3c_softc *sc, uint8_t data)
    237  1.1.2.2  yamt {
    238  1.1.2.2  yamt 
    239  1.1.2.2  yamt 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    240  1.1.2.2  yamt 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_WRITE);
    241  1.1.2.2  yamt 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    242  1.1.2.2  yamt 			BT3C_IOR_CNTL, data);
    243  1.1.2.2  yamt }
    244  1.1.2.2  yamt 
    245  1.1.2.2  yamt static inline void
    246  1.1.2.2  yamt bt3c_set_address(struct bt3c_softc *sc, uint16_t addr)
    247  1.1.2.2  yamt {
    248  1.1.2.2  yamt 
    249  1.1.2.2  yamt 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    250  1.1.2.2  yamt 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_WRITE);
    251  1.1.2.2  yamt 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    252  1.1.2.2  yamt 			BT3C_IOR_ADDR_L, addr & 0xff);
    253  1.1.2.2  yamt 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    254  1.1.2.2  yamt 			BT3C_IOR_ADDR_H, (addr >> 8) & 0xff);
    255  1.1.2.2  yamt }
    256  1.1.2.2  yamt 
    257  1.1.2.2  yamt static inline uint16_t
    258  1.1.2.2  yamt bt3c_read(struct bt3c_softc *sc, uint16_t addr)
    259  1.1.2.2  yamt {
    260  1.1.2.2  yamt 
    261  1.1.2.2  yamt 	bt3c_set_address(sc, addr);
    262  1.1.2.2  yamt 	return bt3c_get(sc);
    263  1.1.2.2  yamt }
    264  1.1.2.2  yamt 
    265  1.1.2.2  yamt static inline void
    266  1.1.2.2  yamt bt3c_write(struct bt3c_softc *sc, uint16_t addr, uint16_t data)
    267  1.1.2.2  yamt {
    268  1.1.2.2  yamt 
    269  1.1.2.2  yamt 	bt3c_set_address(sc, addr);
    270  1.1.2.2  yamt 	bt3c_put(sc, data);
    271  1.1.2.2  yamt }
    272  1.1.2.2  yamt 
    273  1.1.2.2  yamt /*
    274  1.1.2.2  yamt  * receive incoming data from device, store in mbuf chain and
    275  1.1.2.2  yamt  * pass on complete packets to bt device
    276  1.1.2.2  yamt  */
    277  1.1.2.2  yamt static void
    278  1.1.2.2  yamt bt3c_receive(struct bt3c_softc *sc)
    279  1.1.2.2  yamt {
    280  1.1.2.2  yamt 	struct mbuf *m = sc->sc_rxp;
    281  1.1.2.2  yamt 	int space = 0;
    282  1.1.2.2  yamt 	uint16_t count;
    283  1.1.2.2  yamt 	uint8_t b;
    284  1.1.2.2  yamt 
    285  1.1.2.2  yamt 	/*
    286  1.1.2.2  yamt 	 * If we already started a packet, find the
    287  1.1.2.2  yamt 	 * trailing end of it.
    288  1.1.2.2  yamt 	 */
    289  1.1.2.2  yamt 	if (m) {
    290  1.1.2.2  yamt 		while (m->m_next)
    291  1.1.2.2  yamt 			m = m->m_next;
    292  1.1.2.2  yamt 
    293  1.1.2.2  yamt 		space = M_TRAILINGSPACE(m);
    294  1.1.2.2  yamt 	}
    295  1.1.2.2  yamt 
    296  1.1.2.2  yamt 	count = bt3c_read(sc, BT3C_RX_COUNT);
    297  1.1.2.2  yamt 	bt3c_set_address(sc, BT3C_RX_FIFO);
    298  1.1.2.2  yamt 
    299  1.1.2.2  yamt 	while (count > 0) {
    300  1.1.2.2  yamt 		if (space == 0) {
    301  1.1.2.2  yamt 			if (m == NULL) {
    302  1.1.2.2  yamt 				/* new packet */
    303  1.1.2.2  yamt 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    304  1.1.2.2  yamt 				if (m == NULL) {
    305  1.1.2.6  yamt 					aprint_error_dev(sc->sc_dev,
    306  1.1.2.6  yamt 					    "out of memory\n");
    307  1.1.2.7  yamt 					sc->sc_stats.err_rx++;
    308  1.1.2.2  yamt 					goto out;	/* (lost sync) */
    309  1.1.2.2  yamt 				}
    310  1.1.2.2  yamt 
    311  1.1.2.2  yamt 				sc->sc_rxp = m;
    312  1.1.2.2  yamt 				m->m_pkthdr.len = m->m_len = 0;
    313  1.1.2.2  yamt 				space = MHLEN;
    314  1.1.2.2  yamt 
    315  1.1.2.2  yamt 				sc->sc_state = BT3C_RECV_PKT_TYPE;
    316  1.1.2.2  yamt 				sc->sc_want = 1;
    317  1.1.2.2  yamt 			} else {
    318  1.1.2.2  yamt 				/* extend mbuf */
    319  1.1.2.2  yamt 				MGET(m->m_next, M_DONTWAIT, MT_DATA);
    320  1.1.2.2  yamt 				if (m->m_next == NULL) {
    321  1.1.2.6  yamt 					aprint_error_dev(sc->sc_dev,
    322  1.1.2.6  yamt 					    "out of memory\n");
    323  1.1.2.7  yamt 					sc->sc_stats.err_rx++;
    324  1.1.2.2  yamt 					goto out;	/* (lost sync) */
    325  1.1.2.2  yamt 				}
    326  1.1.2.2  yamt 
    327  1.1.2.2  yamt 				m = m->m_next;
    328  1.1.2.2  yamt 				m->m_len = 0;
    329  1.1.2.2  yamt 				space = MLEN;
    330  1.1.2.2  yamt 
    331  1.1.2.2  yamt 				if (sc->sc_want > MINCLSIZE) {
    332  1.1.2.2  yamt 					MCLGET(m, M_DONTWAIT);
    333  1.1.2.2  yamt 					if (m->m_flags & M_EXT)
    334  1.1.2.2  yamt 						space = MCLBYTES;
    335  1.1.2.2  yamt 				}
    336  1.1.2.2  yamt 			}
    337  1.1.2.2  yamt 		}
    338  1.1.2.2  yamt 
    339  1.1.2.2  yamt 		b = bt3c_get(sc);
    340  1.1.2.2  yamt 		mtod(m, uint8_t *)[m->m_len++] = b;
    341  1.1.2.2  yamt 		count--;
    342  1.1.2.2  yamt 		space--;
    343  1.1.2.2  yamt 		sc->sc_rxp->m_pkthdr.len++;
    344  1.1.2.7  yamt 		sc->sc_stats.byte_rx++;
    345  1.1.2.2  yamt 
    346  1.1.2.2  yamt 		sc->sc_want--;
    347  1.1.2.2  yamt 		if (sc->sc_want > 0)
    348  1.1.2.2  yamt 			continue; /* want more */
    349  1.1.2.2  yamt 
    350  1.1.2.2  yamt 		switch (sc->sc_state) {
    351  1.1.2.2  yamt 		case BT3C_RECV_PKT_TYPE:		/* Got packet type */
    352  1.1.2.2  yamt 
    353  1.1.2.2  yamt 			switch (b) {
    354  1.1.2.2  yamt 			case HCI_ACL_DATA_PKT:
    355  1.1.2.2  yamt 				sc->sc_state = BT3C_RECV_ACL_HDR;
    356  1.1.2.2  yamt 				sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
    357  1.1.2.2  yamt 				break;
    358  1.1.2.2  yamt 
    359  1.1.2.2  yamt 			case HCI_SCO_DATA_PKT:
    360  1.1.2.2  yamt 				sc->sc_state = BT3C_RECV_SCO_HDR;
    361  1.1.2.2  yamt 				sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
    362  1.1.2.2  yamt 				break;
    363  1.1.2.2  yamt 
    364  1.1.2.2  yamt 			case HCI_EVENT_PKT:
    365  1.1.2.2  yamt 				sc->sc_state = BT3C_RECV_EVENT_HDR;
    366  1.1.2.2  yamt 				sc->sc_want = sizeof(hci_event_hdr_t) - 1;
    367  1.1.2.2  yamt 				break;
    368  1.1.2.2  yamt 
    369  1.1.2.2  yamt 			default:
    370  1.1.2.6  yamt 				aprint_error_dev(sc->sc_dev,
    371  1.1.2.6  yamt 				    "Unknown packet type=%#x!\n", b);
    372  1.1.2.7  yamt 				sc->sc_stats.err_rx++;
    373  1.1.2.2  yamt 				m_freem(sc->sc_rxp);
    374  1.1.2.2  yamt 				sc->sc_rxp = NULL;
    375  1.1.2.2  yamt 				goto out;	/* (lost sync) */
    376  1.1.2.2  yamt 			}
    377  1.1.2.2  yamt 
    378  1.1.2.2  yamt 			break;
    379  1.1.2.2  yamt 
    380  1.1.2.2  yamt 		/*
    381  1.1.2.2  yamt 		 * we assume (correctly of course :) that the packet headers
    382  1.1.2.2  yamt 		 * all fit into a single pkthdr mbuf
    383  1.1.2.2  yamt 		 */
    384  1.1.2.2  yamt 		case BT3C_RECV_ACL_HDR:		/* Got ACL Header */
    385  1.1.2.2  yamt 			sc->sc_state = BT3C_RECV_ACL_DATA;
    386  1.1.2.2  yamt 			sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
    387  1.1.2.2  yamt 			sc->sc_want = le16toh(sc->sc_want);
    388  1.1.2.2  yamt 			break;
    389  1.1.2.2  yamt 
    390  1.1.2.2  yamt 		case BT3C_RECV_SCO_HDR:		/* Got SCO Header */
    391  1.1.2.2  yamt 			sc->sc_state = BT3C_RECV_SCO_DATA;
    392  1.1.2.2  yamt 			sc->sc_want =  mtod(m, hci_scodata_hdr_t *)->length;
    393  1.1.2.2  yamt 			break;
    394  1.1.2.2  yamt 
    395  1.1.2.2  yamt 		case BT3C_RECV_EVENT_HDR:	/* Got Event Header */
    396  1.1.2.2  yamt 			sc->sc_state = BT3C_RECV_EVENT_DATA;
    397  1.1.2.2  yamt 			sc->sc_want =  mtod(m, hci_event_hdr_t *)->length;
    398  1.1.2.2  yamt 			break;
    399  1.1.2.2  yamt 
    400  1.1.2.2  yamt 		case BT3C_RECV_ACL_DATA:	/* ACL Packet Complete */
    401  1.1.2.7  yamt 			if (!hci_input_acl(sc->sc_unit, sc->sc_rxp))
    402  1.1.2.7  yamt 				sc->sc_stats.err_rx++;
    403  1.1.2.7  yamt 
    404  1.1.2.7  yamt 			sc->sc_stats.acl_rx++;
    405  1.1.2.2  yamt 			sc->sc_rxp = m = NULL;
    406  1.1.2.2  yamt 			space = 0;
    407  1.1.2.2  yamt 			break;
    408  1.1.2.2  yamt 
    409  1.1.2.2  yamt 		case BT3C_RECV_SCO_DATA:	/* SCO Packet Complete */
    410  1.1.2.7  yamt 			if (!hci_input_sco(sc->sc_unit, sc->sc_rxp))
    411  1.1.2.7  yamt 				sc->sc_stats.err_rx++;
    412  1.1.2.7  yamt 
    413  1.1.2.7  yamt 			sc->sc_stats.sco_rx++;
    414  1.1.2.2  yamt 			sc->sc_rxp = m = NULL;
    415  1.1.2.2  yamt 			space = 0;
    416  1.1.2.2  yamt 			break;
    417  1.1.2.2  yamt 
    418  1.1.2.2  yamt 		case BT3C_RECV_EVENT_DATA:	/* Event Packet Complete */
    419  1.1.2.7  yamt 			if (!hci_input_event(sc->sc_unit, sc->sc_rxp))
    420  1.1.2.7  yamt 				sc->sc_stats.err_rx++;
    421  1.1.2.7  yamt 
    422  1.1.2.7  yamt 			sc->sc_stats.evt_rx++;
    423  1.1.2.2  yamt 			sc->sc_rxp = m = NULL;
    424  1.1.2.2  yamt 			space = 0;
    425  1.1.2.2  yamt 			break;
    426  1.1.2.2  yamt 
    427  1.1.2.2  yamt 		default:
    428  1.1.2.2  yamt 			panic("%s: invalid state %d!\n",
    429  1.1.2.6  yamt 				device_xname(sc->sc_dev), sc->sc_state);
    430  1.1.2.2  yamt 		}
    431  1.1.2.2  yamt 	}
    432  1.1.2.2  yamt 
    433  1.1.2.2  yamt out:
    434  1.1.2.2  yamt 	bt3c_write(sc, BT3C_RX_COUNT, 0x0000);
    435  1.1.2.2  yamt }
    436  1.1.2.2  yamt 
    437  1.1.2.2  yamt /*
    438  1.1.2.2  yamt  * write data from current packet to Transmit FIFO.
    439  1.1.2.2  yamt  * restart when done.
    440  1.1.2.2  yamt  */
    441  1.1.2.2  yamt static void
    442  1.1.2.2  yamt bt3c_transmit(struct bt3c_softc *sc)
    443  1.1.2.2  yamt {
    444  1.1.2.2  yamt 	struct mbuf *m;
    445  1.1.2.2  yamt 	int count, rlen;
    446  1.1.2.2  yamt 	uint8_t *rptr;
    447  1.1.2.2  yamt 
    448  1.1.2.2  yamt 	m = sc->sc_txp;
    449  1.1.2.2  yamt 	if (m == NULL) {
    450  1.1.2.7  yamt 		sc->sc_flags &= ~BT3C_XMIT;
    451  1.1.2.7  yamt 		bt3c_start(sc);
    452  1.1.2.2  yamt 		return;
    453  1.1.2.2  yamt 	}
    454  1.1.2.2  yamt 
    455  1.1.2.2  yamt 	count = 0;
    456  1.1.2.2  yamt 	rlen = 0;
    457  1.1.2.2  yamt 	rptr = mtod(m, uint8_t *);
    458  1.1.2.2  yamt 
    459  1.1.2.2  yamt 	bt3c_set_address(sc, BT3C_TX_FIFO);
    460  1.1.2.2  yamt 
    461  1.1.2.2  yamt 	for(;;) {
    462  1.1.2.2  yamt 		if (rlen >= m->m_len) {
    463  1.1.2.2  yamt 			m = m->m_next;
    464  1.1.2.2  yamt 			if (m == NULL) {
    465  1.1.2.2  yamt 				m = sc->sc_txp;
    466  1.1.2.2  yamt 				sc->sc_txp = NULL;
    467  1.1.2.2  yamt 
    468  1.1.2.2  yamt 				if (M_GETCTX(m, void *) == NULL)
    469  1.1.2.2  yamt 					m_freem(m);
    470  1.1.2.7  yamt 				else if (!hci_complete_sco(sc->sc_unit, m))
    471  1.1.2.7  yamt 					sc->sc_stats.err_tx++;
    472  1.1.2.2  yamt 
    473  1.1.2.2  yamt 				break;
    474  1.1.2.2  yamt 			}
    475  1.1.2.2  yamt 
    476  1.1.2.2  yamt 			rlen = 0;
    477  1.1.2.2  yamt 			rptr = mtod(m, uint8_t *);
    478  1.1.2.2  yamt 			continue;
    479  1.1.2.2  yamt 		}
    480  1.1.2.2  yamt 
    481  1.1.2.2  yamt 		if (count >= BT3C_FIFO_SIZE) {
    482  1.1.2.2  yamt 			m_adj(m, rlen);
    483  1.1.2.2  yamt 			break;
    484  1.1.2.2  yamt 		}
    485  1.1.2.2  yamt 
    486  1.1.2.2  yamt 		bt3c_put(sc, *rptr++);
    487  1.1.2.2  yamt 		rlen++;
    488  1.1.2.2  yamt 		count++;
    489  1.1.2.2  yamt 	}
    490  1.1.2.2  yamt 
    491  1.1.2.2  yamt 	bt3c_write(sc, BT3C_TX_COUNT, count);
    492  1.1.2.7  yamt 	sc->sc_stats.byte_tx += count;
    493  1.1.2.2  yamt }
    494  1.1.2.2  yamt 
    495  1.1.2.2  yamt /*
    496  1.1.2.2  yamt  * interrupt routine
    497  1.1.2.2  yamt  */
    498  1.1.2.2  yamt static int
    499  1.1.2.2  yamt bt3c_intr(void *arg)
    500  1.1.2.2  yamt {
    501  1.1.2.2  yamt 	struct bt3c_softc *sc = arg;
    502  1.1.2.2  yamt 	uint16_t control, isr;
    503  1.1.2.2  yamt 
    504  1.1.2.2  yamt 	control = bt3c_read_control(sc);
    505  1.1.2.2  yamt 	if (control & BT3C_IOR_CNTL_INTR) {
    506  1.1.2.2  yamt 		isr = bt3c_read(sc, BT3C_ISR);
    507  1.1.2.2  yamt 		if ((isr & 0xff) == 0x7f) {
    508  1.1.2.6  yamt 			aprint_error_dev(sc->sc_dev, "strange ISR=%04x\n", isr);
    509  1.1.2.2  yamt 		} else if ((isr & 0xff) != 0xff) {
    510  1.1.2.2  yamt 
    511  1.1.2.2  yamt 			if (isr & BT3C_ISR_RXRDY)
    512  1.1.2.2  yamt 				bt3c_receive(sc);
    513  1.1.2.2  yamt 
    514  1.1.2.2  yamt 			if (isr & BT3C_ISR_TXRDY)
    515  1.1.2.2  yamt 				bt3c_transmit(sc);
    516  1.1.2.2  yamt 
    517  1.1.2.2  yamt #ifdef DIAGNOSTIC
    518  1.1.2.2  yamt 			if (isr & BT3C_ISR_ANTENNA) {
    519  1.1.2.2  yamt 				if (bt3c_read(sc, BT3C_CSR) & BT3C_CSR_ANTENNA)
    520  1.1.2.6  yamt 					aprint_verbose_dev(sc->sc_dev,
    521  1.1.2.6  yamt 					    "Antenna Out\n");
    522  1.1.2.2  yamt 				else
    523  1.1.2.6  yamt 					aprint_verbose_dev(sc->sc_dev,
    524  1.1.2.6  yamt 					    "Antenna In\n");
    525  1.1.2.2  yamt 			}
    526  1.1.2.2  yamt #endif
    527  1.1.2.2  yamt 
    528  1.1.2.2  yamt 			bt3c_write(sc, BT3C_ISR, 0x0000);
    529  1.1.2.2  yamt 			bt3c_write_control(sc, control);
    530  1.1.2.2  yamt 
    531  1.1.2.2  yamt 			return 1; /* handled */
    532  1.1.2.2  yamt 		}
    533  1.1.2.2  yamt 	}
    534  1.1.2.2  yamt 
    535  1.1.2.2  yamt 	return 0; /* not handled */
    536  1.1.2.2  yamt }
    537  1.1.2.2  yamt 
    538  1.1.2.2  yamt /*
    539  1.1.2.2  yamt  * load firmware for the device
    540  1.1.2.2  yamt  *
    541  1.1.2.4  yamt  * The firmware file is a plain ASCII file in the Motorola S-Record format,
    542  1.1.2.4  yamt  * with lines in the format:
    543  1.1.2.2  yamt  *
    544  1.1.2.2  yamt  *	S<Digit><Len><Address><Data1><Data2>...<DataN><Checksum>
    545  1.1.2.2  yamt  *
    546  1.1.2.4  yamt  * <Digit>:	0	header
    547  1.1.2.4  yamt  *		3	data record (4 byte address)
    548  1.1.2.4  yamt  *		7	boot record (4 byte address)
    549  1.1.2.2  yamt  *
    550  1.1.2.2  yamt  * <Len>:	1 byte, and is the number of bytes in the rest of the line
    551  1.1.2.2  yamt  * <Address>:	4 byte address (only 2 bytes are valid for bt3c I think)
    552  1.1.2.2  yamt  * <Data>:	2 byte data word to be written to the address
    553  1.1.2.2  yamt  * <Checksum>:	checksum of all bytes in the line including <Len>
    554  1.1.2.2  yamt  *
    555  1.1.2.2  yamt  * all bytes are in hexadecimal
    556  1.1.2.2  yamt  */
    557  1.1.2.2  yamt static inline int32_t
    558  1.1.2.2  yamt hex(const uint8_t *p, int n)
    559  1.1.2.2  yamt {
    560  1.1.2.2  yamt 	uint32_t val = 0;
    561  1.1.2.2  yamt 
    562  1.1.2.2  yamt 	while (n > 0) {
    563  1.1.2.2  yamt 		val <<= 4;
    564  1.1.2.2  yamt 
    565  1.1.2.2  yamt 		if ('0' <= *p && *p <= '9')
    566  1.1.2.2  yamt 			val += (*p - '0');
    567  1.1.2.2  yamt 		else if ('a' <= *p && *p <= 'f')
    568  1.1.2.2  yamt 			val += (*p - 'a' + 0xa);
    569  1.1.2.2  yamt 		else if ('A' <= *p && *p <= 'F')
    570  1.1.2.2  yamt 			val += (*p - 'A' + 0xa);
    571  1.1.2.2  yamt 		else
    572  1.1.2.2  yamt 			return -1;
    573  1.1.2.2  yamt 
    574  1.1.2.2  yamt 		p++;
    575  1.1.2.2  yamt 		n--;
    576  1.1.2.2  yamt 	}
    577  1.1.2.2  yamt 
    578  1.1.2.2  yamt 	return val;
    579  1.1.2.2  yamt }
    580  1.1.2.2  yamt 
    581  1.1.2.2  yamt static int
    582  1.1.2.2  yamt bt3c_load_firmware(struct bt3c_softc *sc)
    583  1.1.2.2  yamt {
    584  1.1.2.2  yamt 	uint8_t *buf, *line, *next, *p;
    585  1.1.2.2  yamt 	int32_t addr, data;
    586  1.1.2.2  yamt 	int err, sum, len;
    587  1.1.2.2  yamt 	firmware_handle_t fh;
    588  1.1.2.6  yamt 	struct cfdata *cf = device_cfdata(sc->sc_dev);
    589  1.1.2.2  yamt 	size_t size;
    590  1.1.2.2  yamt 
    591  1.1.2.6  yamt 	err = firmware_open(cf->cf_name,
    592  1.1.2.2  yamt 			    BT3C_FIRMWARE_FILE, &fh);
    593  1.1.2.2  yamt 	if (err) {
    594  1.1.2.6  yamt 		aprint_error_dev(sc->sc_dev, "Cannot open firmware %s/%s\n",
    595  1.1.2.6  yamt 		    cf->cf_name, BT3C_FIRMWARE_FILE);
    596  1.1.2.2  yamt 		return err;
    597  1.1.2.2  yamt 	}
    598  1.1.2.2  yamt 
    599  1.1.2.2  yamt 	size = (size_t)firmware_get_size(fh);
    600  1.1.2.2  yamt #ifdef DIAGNOSTIC
    601  1.1.2.3  yamt 	if (size > 10 * 1024) {	/* sanity check */
    602  1.1.2.6  yamt 		aprint_error_dev(sc->sc_dev, "insane firmware file size!\n");
    603  1.1.2.3  yamt 		firmware_close(fh);
    604  1.1.2.2  yamt 		return EFBIG;
    605  1.1.2.2  yamt 	}
    606  1.1.2.2  yamt #endif
    607  1.1.2.2  yamt 
    608  1.1.2.2  yamt 	buf = firmware_malloc(size);
    609  1.1.2.2  yamt 	KASSERT(buf != NULL);
    610  1.1.2.2  yamt 
    611  1.1.2.2  yamt 	err = firmware_read(fh, 0, buf, size);
    612  1.1.2.2  yamt 	if (err) {
    613  1.1.2.6  yamt 		aprint_error_dev(sc->sc_dev, "Firmware read failed (%d)\n", err);
    614  1.1.2.2  yamt 		goto out;
    615  1.1.2.2  yamt 	}
    616  1.1.2.2  yamt 
    617  1.1.2.2  yamt 	/* Reset */
    618  1.1.2.2  yamt 	bt3c_write(sc, 0x8040, 0x0404);
    619  1.1.2.2  yamt 	bt3c_write(sc, 0x8040, 0x0400);
    620  1.1.2.2  yamt 	DELAY(1);
    621  1.1.2.2  yamt 	bt3c_write(sc, 0x8040, 0x0404);
    622  1.1.2.2  yamt 	DELAY(17);
    623  1.1.2.2  yamt 
    624  1.1.2.2  yamt 	next = buf;
    625  1.1.2.2  yamt 	err = EFTYPE;
    626  1.1.2.2  yamt 
    627  1.1.2.2  yamt 	while (next < buf + size) {
    628  1.1.2.2  yamt 		line = next;
    629  1.1.2.2  yamt 
    630  1.1.2.2  yamt 		while (*next != '\r' && *next != '\n') {
    631  1.1.2.2  yamt 			if (next >= buf + size)
    632  1.1.2.2  yamt 				goto out;
    633  1.1.2.2  yamt 
    634  1.1.2.2  yamt 			next++;
    635  1.1.2.2  yamt 		}
    636  1.1.2.2  yamt 
    637  1.1.2.2  yamt 		/* 14 covers address and checksum minimum */
    638  1.1.2.2  yamt 		if (next - line < 14)
    639  1.1.2.2  yamt 			goto out;
    640  1.1.2.2  yamt 
    641  1.1.2.2  yamt 		if (line[0] != 'S')
    642  1.1.2.2  yamt 			goto out;
    643  1.1.2.2  yamt 
    644  1.1.2.2  yamt 		/* verify line length */
    645  1.1.2.2  yamt 		len = hex(line + 2, 2);
    646  1.1.2.2  yamt 		if (len < 0 || next - line != len * 2 + 4)
    647  1.1.2.2  yamt 			goto out;
    648  1.1.2.2  yamt 
    649  1.1.2.2  yamt 		/* checksum the line */
    650  1.1.2.2  yamt 		sum = 0;
    651  1.1.2.2  yamt 		for (p = line + 2 ; p < next ; p += 2)
    652  1.1.2.2  yamt 			sum += hex(p, 2);
    653  1.1.2.2  yamt 
    654  1.1.2.2  yamt 		if ((sum & 0xff) != 0xff)
    655  1.1.2.2  yamt 			goto out;
    656  1.1.2.2  yamt 
    657  1.1.2.2  yamt 		/* extract relevant data */
    658  1.1.2.2  yamt 		switch (line[1]) {
    659  1.1.2.2  yamt 		case '0':
    660  1.1.2.4  yamt 			/* we ignore the header */
    661  1.1.2.2  yamt 			break;
    662  1.1.2.2  yamt 
    663  1.1.2.2  yamt 		case '3':
    664  1.1.2.2  yamt 			/* find number of data words */
    665  1.1.2.2  yamt 			len = (len - 5) / 2;
    666  1.1.2.2  yamt 			if (len > 15)
    667  1.1.2.2  yamt 				goto out;
    668  1.1.2.2  yamt 
    669  1.1.2.2  yamt 			addr = hex(line + 8, 4);
    670  1.1.2.2  yamt 			if (addr < 0)
    671  1.1.2.2  yamt 				goto out;
    672  1.1.2.2  yamt 
    673  1.1.2.2  yamt 			bt3c_set_address(sc, addr);
    674  1.1.2.2  yamt 
    675  1.1.2.2  yamt 			for (p = line + 12 ; p + 4 < next ; p += 4) {
    676  1.1.2.2  yamt 				data = hex(p, 4);
    677  1.1.2.2  yamt 				if (data < 0)
    678  1.1.2.2  yamt 					goto out;
    679  1.1.2.2  yamt 
    680  1.1.2.2  yamt 				bt3c_put(sc, data);
    681  1.1.2.2  yamt 			}
    682  1.1.2.2  yamt 			break;
    683  1.1.2.2  yamt 
    684  1.1.2.2  yamt 		case '7':
    685  1.1.2.4  yamt 			/*
    686  1.1.2.4  yamt 			 * for some reason we ignore this record
    687  1.1.2.4  yamt 			 * and boot from 0x3000 which happens to
    688  1.1.2.4  yamt 			 * be the first record in the file.
    689  1.1.2.4  yamt 			 */
    690  1.1.2.2  yamt 			break;
    691  1.1.2.2  yamt 
    692  1.1.2.2  yamt 		default:
    693  1.1.2.2  yamt 			goto out;
    694  1.1.2.2  yamt 		}
    695  1.1.2.2  yamt 
    696  1.1.2.2  yamt 		/* skip to start of next line */
    697  1.1.2.2  yamt 		while (next < buf + size && (*next == '\r' || *next == '\n'))
    698  1.1.2.2  yamt 			next++;
    699  1.1.2.2  yamt 	}
    700  1.1.2.2  yamt 
    701  1.1.2.2  yamt 	err = 0;
    702  1.1.2.2  yamt 	DELAY(17);
    703  1.1.2.2  yamt 
    704  1.1.2.2  yamt 	/* Boot */
    705  1.1.2.2  yamt 	bt3c_set_address(sc, 0x3000);
    706  1.1.2.2  yamt 	bt3c_write_control(sc, (bt3c_read_control(sc) | BT3C_IOR_CNTL_BOOT));
    707  1.1.2.2  yamt 	DELAY(17);
    708  1.1.2.2  yamt 
    709  1.1.2.2  yamt 	/* Clear Registers */
    710  1.1.2.2  yamt 	bt3c_write(sc, BT3C_RX_COUNT, 0x0000);
    711  1.1.2.2  yamt 	bt3c_write(sc, BT3C_TX_COUNT, 0x0000);
    712  1.1.2.2  yamt 	bt3c_write(sc, BT3C_ISR, 0x0000);
    713  1.1.2.2  yamt 	DELAY(1000);
    714  1.1.2.2  yamt 
    715  1.1.2.2  yamt out:
    716  1.1.2.2  yamt 	firmware_free(buf, size);
    717  1.1.2.2  yamt 	firmware_close(fh);
    718  1.1.2.2  yamt 	return err;
    719  1.1.2.2  yamt }
    720  1.1.2.2  yamt 
    721  1.1.2.2  yamt /**************************************************************************
    722  1.1.2.2  yamt  *
    723  1.1.2.7  yamt  *  bt device callbacks
    724  1.1.2.2  yamt  */
    725  1.1.2.2  yamt 
    726  1.1.2.2  yamt /*
    727  1.1.2.2  yamt  * start sending on bt3c
    728  1.1.2.7  yamt  * should be called at spltty() when BT3C_XMIT is not set
    729  1.1.2.2  yamt  */
    730  1.1.2.2  yamt static void
    731  1.1.2.7  yamt bt3c_start(struct bt3c_softc *sc)
    732  1.1.2.2  yamt {
    733  1.1.2.2  yamt 	struct mbuf *m;
    734  1.1.2.2  yamt 
    735  1.1.2.7  yamt 	KASSERT((sc->sc_flags & BT3C_XMIT) == 0);
    736  1.1.2.2  yamt 	KASSERT(sc->sc_txp == NULL);
    737  1.1.2.2  yamt 
    738  1.1.2.7  yamt 	if (MBUFQ_FIRST(&sc->sc_cmdq)) {
    739  1.1.2.7  yamt 		MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
    740  1.1.2.7  yamt 		sc->sc_stats.cmd_tx++;
    741  1.1.2.2  yamt 		goto start;
    742  1.1.2.2  yamt 	}
    743  1.1.2.2  yamt 
    744  1.1.2.7  yamt 	if (MBUFQ_FIRST(&sc->sc_scoq)) {
    745  1.1.2.7  yamt 		MBUFQ_DEQUEUE(&sc->sc_scoq, m);
    746  1.1.2.7  yamt 		sc->sc_stats.sco_tx++;
    747  1.1.2.2  yamt 		goto start;
    748  1.1.2.2  yamt 	}
    749  1.1.2.2  yamt 
    750  1.1.2.7  yamt 	if (MBUFQ_FIRST(&sc->sc_aclq)) {
    751  1.1.2.7  yamt 		MBUFQ_DEQUEUE(&sc->sc_aclq, m);
    752  1.1.2.7  yamt 		sc->sc_stats.acl_tx++;
    753  1.1.2.2  yamt 		goto start;
    754  1.1.2.2  yamt 	}
    755  1.1.2.2  yamt 
    756  1.1.2.2  yamt 	/* Nothing to send */
    757  1.1.2.2  yamt 	return;
    758  1.1.2.2  yamt 
    759  1.1.2.2  yamt start:
    760  1.1.2.2  yamt 	sc->sc_txp = m;
    761  1.1.2.7  yamt 	sc->sc_flags |= BT3C_XMIT;
    762  1.1.2.2  yamt 	bt3c_transmit(sc);
    763  1.1.2.2  yamt }
    764  1.1.2.2  yamt 
    765  1.1.2.7  yamt static void
    766  1.1.2.7  yamt bt3c_output_cmd(device_t self, struct mbuf *m)
    767  1.1.2.7  yamt {
    768  1.1.2.7  yamt 	struct bt3c_softc *sc = device_private(self);
    769  1.1.2.7  yamt 	int s;
    770  1.1.2.7  yamt 
    771  1.1.2.7  yamt 	KASSERT(sc->sc_flags & BT3C_ENABLED);
    772  1.1.2.7  yamt 
    773  1.1.2.7  yamt 	M_SETCTX(m, NULL);
    774  1.1.2.7  yamt 
    775  1.1.2.7  yamt 	s = spltty();
    776  1.1.2.7  yamt 	MBUFQ_ENQUEUE(&sc->sc_cmdq, m);
    777  1.1.2.7  yamt 	if ((sc->sc_flags & BT3C_XMIT) == 0)
    778  1.1.2.7  yamt 		bt3c_start(sc);
    779  1.1.2.7  yamt 
    780  1.1.2.7  yamt 	splx(s);
    781  1.1.2.7  yamt }
    782  1.1.2.7  yamt 
    783  1.1.2.7  yamt static void
    784  1.1.2.7  yamt bt3c_output_acl(device_t self, struct mbuf *m)
    785  1.1.2.7  yamt {
    786  1.1.2.7  yamt 	struct bt3c_softc *sc = device_private(self);
    787  1.1.2.7  yamt 	int s;
    788  1.1.2.7  yamt 
    789  1.1.2.7  yamt 	KASSERT(sc->sc_flags & BT3C_ENABLED);
    790  1.1.2.7  yamt 
    791  1.1.2.7  yamt 	M_SETCTX(m, NULL);
    792  1.1.2.7  yamt 
    793  1.1.2.7  yamt 	s = spltty();
    794  1.1.2.7  yamt 	MBUFQ_ENQUEUE(&sc->sc_aclq, m);
    795  1.1.2.7  yamt 	if ((sc->sc_flags & BT3C_XMIT) == 0)
    796  1.1.2.7  yamt 		bt3c_start(sc);
    797  1.1.2.7  yamt 
    798  1.1.2.7  yamt 	splx(s);
    799  1.1.2.7  yamt }
    800  1.1.2.7  yamt 
    801  1.1.2.7  yamt static void
    802  1.1.2.7  yamt bt3c_output_sco(device_t self, struct mbuf *m)
    803  1.1.2.7  yamt {
    804  1.1.2.7  yamt 	struct bt3c_softc *sc = device_private(self);
    805  1.1.2.7  yamt 	int s;
    806  1.1.2.7  yamt 
    807  1.1.2.7  yamt 	KASSERT(sc->sc_flags & BT3C_ENABLED);
    808  1.1.2.7  yamt 
    809  1.1.2.7  yamt 	s = spltty();
    810  1.1.2.7  yamt 	MBUFQ_ENQUEUE(&sc->sc_scoq, m);
    811  1.1.2.7  yamt 	if ((sc->sc_flags & BT3C_XMIT) == 0)
    812  1.1.2.7  yamt 		bt3c_start(sc);
    813  1.1.2.7  yamt 
    814  1.1.2.7  yamt 	splx(s);
    815  1.1.2.7  yamt }
    816  1.1.2.7  yamt 
    817  1.1.2.2  yamt /*
    818  1.1.2.2  yamt  * enable device
    819  1.1.2.2  yamt  *	turn on card
    820  1.1.2.2  yamt  *	load firmware
    821  1.1.2.2  yamt  *	establish interrupts
    822  1.1.2.2  yamt  */
    823  1.1.2.2  yamt static int
    824  1.1.2.6  yamt bt3c_enable(device_t self)
    825  1.1.2.2  yamt {
    826  1.1.2.6  yamt 	struct bt3c_softc *sc = device_private(self);
    827  1.1.2.7  yamt 	int err, s;
    828  1.1.2.2  yamt 
    829  1.1.2.7  yamt 	if (sc->sc_flags & BT3C_ENABLED)
    830  1.1.2.2  yamt 		return 0;
    831  1.1.2.2  yamt 
    832  1.1.2.7  yamt 	s = spltty();
    833  1.1.2.7  yamt 
    834  1.1.2.2  yamt 	sc->sc_intr = pcmcia_intr_establish(sc->sc_pf, IPL_TTY, bt3c_intr, sc);
    835  1.1.2.2  yamt 	if (sc->sc_intr == NULL) {
    836  1.1.2.2  yamt 		err = EIO;
    837  1.1.2.2  yamt 		goto bad;
    838  1.1.2.2  yamt 	}
    839  1.1.2.2  yamt 
    840  1.1.2.2  yamt 	err = pcmcia_function_enable(sc->sc_pf);
    841  1.1.2.2  yamt 	if (err)
    842  1.1.2.2  yamt 		goto bad1;
    843  1.1.2.2  yamt 
    844  1.1.2.2  yamt 	err = bt3c_load_firmware(sc);
    845  1.1.2.2  yamt 	if (err)
    846  1.1.2.2  yamt 		goto bad2;
    847  1.1.2.2  yamt 
    848  1.1.2.7  yamt 	sc->sc_flags |= BT3C_ENABLED;
    849  1.1.2.7  yamt 	sc->sc_flags &= ~BT3C_XMIT;
    850  1.1.2.2  yamt 
    851  1.1.2.7  yamt 	splx(s);
    852  1.1.2.2  yamt 
    853  1.1.2.2  yamt 	return 0;
    854  1.1.2.2  yamt 
    855  1.1.2.2  yamt bad2:
    856  1.1.2.2  yamt 	pcmcia_function_disable(sc->sc_pf);
    857  1.1.2.2  yamt bad1:
    858  1.1.2.2  yamt 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
    859  1.1.2.2  yamt 	sc->sc_intr = NULL;
    860  1.1.2.2  yamt bad:
    861  1.1.2.7  yamt 	splx(s);
    862  1.1.2.2  yamt 	return err;
    863  1.1.2.2  yamt }
    864  1.1.2.2  yamt 
    865  1.1.2.2  yamt /*
    866  1.1.2.2  yamt  * disable device
    867  1.1.2.2  yamt  *	shut down card
    868  1.1.2.2  yamt  *	disestablish interrupts
    869  1.1.2.2  yamt  *	free held packets
    870  1.1.2.2  yamt  */
    871  1.1.2.2  yamt static void
    872  1.1.2.6  yamt bt3c_disable(device_t self)
    873  1.1.2.2  yamt {
    874  1.1.2.6  yamt 	struct bt3c_softc *sc = device_private(self);
    875  1.1.2.7  yamt 	int s;
    876  1.1.2.2  yamt 
    877  1.1.2.7  yamt 	if ((sc->sc_flags & BT3C_ENABLED) == 0)
    878  1.1.2.2  yamt 		return;
    879  1.1.2.2  yamt 
    880  1.1.2.7  yamt 	s = spltty();
    881  1.1.2.7  yamt 
    882  1.1.2.2  yamt 	pcmcia_function_disable(sc->sc_pf);
    883  1.1.2.2  yamt 
    884  1.1.2.2  yamt 	if (sc->sc_intr) {
    885  1.1.2.2  yamt 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
    886  1.1.2.2  yamt 		sc->sc_intr = NULL;
    887  1.1.2.2  yamt 	}
    888  1.1.2.2  yamt 
    889  1.1.2.2  yamt 	if (sc->sc_rxp) {
    890  1.1.2.2  yamt 		m_freem(sc->sc_rxp);
    891  1.1.2.2  yamt 		sc->sc_rxp = NULL;
    892  1.1.2.2  yamt 	}
    893  1.1.2.2  yamt 
    894  1.1.2.2  yamt 	if (sc->sc_txp) {
    895  1.1.2.2  yamt 		m_freem(sc->sc_txp);
    896  1.1.2.2  yamt 		sc->sc_txp = NULL;
    897  1.1.2.2  yamt 	}
    898  1.1.2.2  yamt 
    899  1.1.2.7  yamt 	MBUFQ_DRAIN(&sc->sc_cmdq);
    900  1.1.2.7  yamt 	MBUFQ_DRAIN(&sc->sc_aclq);
    901  1.1.2.7  yamt 	MBUFQ_DRAIN(&sc->sc_scoq);
    902  1.1.2.7  yamt 
    903  1.1.2.7  yamt 	sc->sc_flags &= ~BT3C_ENABLED;
    904  1.1.2.7  yamt 	splx(s);
    905  1.1.2.7  yamt }
    906  1.1.2.7  yamt 
    907  1.1.2.7  yamt void
    908  1.1.2.7  yamt bt3c_stats(device_t self, struct bt_stats *dest, int flush)
    909  1.1.2.7  yamt {
    910  1.1.2.7  yamt 	struct bt3c_softc *sc = device_private(self);
    911  1.1.2.7  yamt 	int s;
    912  1.1.2.7  yamt 
    913  1.1.2.7  yamt 	s = spltty();
    914  1.1.2.7  yamt 	memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
    915  1.1.2.7  yamt 
    916  1.1.2.7  yamt 	if (flush)
    917  1.1.2.7  yamt 		memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
    918  1.1.2.7  yamt 
    919  1.1.2.7  yamt 	splx(s);
    920  1.1.2.2  yamt }
    921  1.1.2.2  yamt 
    922  1.1.2.2  yamt /**************************************************************************
    923  1.1.2.2  yamt  *
    924  1.1.2.2  yamt  *	bt3c PCMCIA autoconfig glue
    925  1.1.2.2  yamt  */
    926  1.1.2.2  yamt 
    927  1.1.2.2  yamt static int
    928  1.1.2.6  yamt bt3c_match(device_t parent, struct cfdata *match, void *aux)
    929  1.1.2.2  yamt {
    930  1.1.2.2  yamt 	struct pcmcia_attach_args *pa = aux;
    931  1.1.2.2  yamt 
    932  1.1.2.2  yamt 	if (pa->manufacturer == PCMCIA_VENDOR_3COM &&
    933  1.1.2.2  yamt 	    pa->product == PCMCIA_PRODUCT_3COM_3CRWB6096)
    934  1.1.2.2  yamt 	    return 10;		/* 'com' also claims this, so trump them */
    935  1.1.2.2  yamt 
    936  1.1.2.2  yamt 	return 0;
    937  1.1.2.2  yamt }
    938  1.1.2.2  yamt 
    939  1.1.2.2  yamt static void
    940  1.1.2.6  yamt bt3c_attach(device_t parent, device_t self, void *aux)
    941  1.1.2.2  yamt {
    942  1.1.2.6  yamt 	struct bt3c_softc *sc = device_private(self);
    943  1.1.2.2  yamt 	struct pcmcia_attach_args *pa = aux;
    944  1.1.2.2  yamt 	struct pcmcia_config_entry *cfe;
    945  1.1.2.2  yamt 
    946  1.1.2.6  yamt 	sc->sc_dev = self;
    947  1.1.2.2  yamt 	sc->sc_pf = pa->pf;
    948  1.1.2.2  yamt 
    949  1.1.2.7  yamt 	MBUFQ_INIT(&sc->sc_cmdq);
    950  1.1.2.7  yamt 	MBUFQ_INIT(&sc->sc_aclq);
    951  1.1.2.7  yamt 	MBUFQ_INIT(&sc->sc_scoq);
    952  1.1.2.7  yamt 
    953  1.1.2.2  yamt 	/* Find a PCMCIA config entry we can use */
    954  1.1.2.2  yamt 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    955  1.1.2.2  yamt 		if (cfe->num_memspace != 0)
    956  1.1.2.2  yamt 			continue;
    957  1.1.2.2  yamt 
    958  1.1.2.2  yamt 		if (cfe->num_iospace != 1)
    959  1.1.2.2  yamt 			continue;
    960  1.1.2.2  yamt 
    961  1.1.2.2  yamt 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    962  1.1.2.2  yamt 				cfe->iospace[0].length, 0, &sc->sc_pcioh) == 0)
    963  1.1.2.2  yamt 			break;
    964  1.1.2.2  yamt 	}
    965  1.1.2.2  yamt 
    966  1.1.2.2  yamt 	if (cfe == 0) {
    967  1.1.2.2  yamt 		aprint_error("bt3c_attach: cannot allocate io space\n");
    968  1.1.2.2  yamt 		goto no_config_entry;
    969  1.1.2.2  yamt 	}
    970  1.1.2.2  yamt 
    971  1.1.2.2  yamt 	/* Initialise it */
    972  1.1.2.2  yamt 	pcmcia_function_init(pa->pf, cfe);
    973  1.1.2.2  yamt 
    974  1.1.2.2  yamt 	/* Map in the io space */
    975  1.1.2.2  yamt 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO,
    976  1.1.2.2  yamt 			&sc->sc_pcioh, &sc->sc_iow)) {
    977  1.1.2.2  yamt 		aprint_error("bt3c_attach: cannot map io space\n");
    978  1.1.2.2  yamt 		goto iomap_failed;
    979  1.1.2.2  yamt 	}
    980  1.1.2.2  yamt 
    981  1.1.2.2  yamt 	/* Attach Bluetooth unit */
    982  1.1.2.7  yamt 	sc->sc_unit = hci_attach(&bt3c_hci, self, BTF_POWER_UP_NOOP);
    983  1.1.2.2  yamt 
    984  1.1.2.2  yamt 	/* establish a power change hook */
    985  1.1.2.6  yamt 	sc->sc_powerhook = powerhook_establish(device_xname(sc->sc_dev),
    986  1.1.2.3  yamt 	    bt3c_power, sc);
    987  1.1.2.2  yamt 	return;
    988  1.1.2.2  yamt 
    989  1.1.2.2  yamt iomap_failed:
    990  1.1.2.2  yamt 	/* unmap io space */
    991  1.1.2.2  yamt 	pcmcia_io_free(pa->pf, &sc->sc_pcioh);
    992  1.1.2.2  yamt 
    993  1.1.2.2  yamt no_config_entry:
    994  1.1.2.2  yamt 	sc->sc_iow = -1;
    995  1.1.2.2  yamt }
    996  1.1.2.2  yamt 
    997  1.1.2.2  yamt static int
    998  1.1.2.6  yamt bt3c_detach(device_t self, int flags)
    999  1.1.2.2  yamt {
   1000  1.1.2.6  yamt 	struct bt3c_softc *sc = device_private(self);
   1001  1.1.2.2  yamt 	int err = 0;
   1002  1.1.2.2  yamt 
   1003  1.1.2.6  yamt 	bt3c_disable(self);
   1004  1.1.2.2  yamt 
   1005  1.1.2.2  yamt 	if (sc->sc_powerhook) {
   1006  1.1.2.2  yamt 		powerhook_disestablish(sc->sc_powerhook);
   1007  1.1.2.2  yamt 		sc->sc_powerhook = NULL;
   1008  1.1.2.2  yamt 	}
   1009  1.1.2.2  yamt 
   1010  1.1.2.7  yamt 	if (sc->sc_unit) {
   1011  1.1.2.7  yamt 		hci_detach(sc->sc_unit);
   1012  1.1.2.7  yamt 		sc->sc_unit = NULL;
   1013  1.1.2.7  yamt 	}
   1014  1.1.2.2  yamt 
   1015  1.1.2.2  yamt 	if (sc->sc_iow != -1) {
   1016  1.1.2.2  yamt 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iow);
   1017  1.1.2.2  yamt 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
   1018  1.1.2.2  yamt 		sc->sc_iow = -1;
   1019  1.1.2.2  yamt 	}
   1020  1.1.2.2  yamt 
   1021  1.1.2.2  yamt 	return err;
   1022  1.1.2.2  yamt }
   1023  1.1.2.2  yamt 
   1024  1.1.2.2  yamt static void
   1025  1.1.2.2  yamt bt3c_power(int why, void *arg)
   1026  1.1.2.2  yamt {
   1027  1.1.2.2  yamt 	struct bt3c_softc *sc = arg;
   1028  1.1.2.2  yamt 
   1029  1.1.2.2  yamt 	switch(why) {
   1030  1.1.2.2  yamt 	case PWR_SUSPEND:
   1031  1.1.2.2  yamt 	case PWR_STANDBY:
   1032  1.1.2.7  yamt 		if (sc->sc_flags & BT3C_ENABLED) {
   1033  1.1.2.7  yamt 			if (sc->sc_unit) {
   1034  1.1.2.7  yamt 				hci_detach(sc->sc_unit);
   1035  1.1.2.7  yamt 				sc->sc_unit = NULL;
   1036  1.1.2.7  yamt 			}
   1037  1.1.2.3  yamt 
   1038  1.1.2.2  yamt 			sc->sc_flags |= BT3C_SLEEPING;
   1039  1.1.2.6  yamt 			aprint_verbose_dev(sc->sc_dev, "sleeping\n");
   1040  1.1.2.2  yamt 		}
   1041  1.1.2.2  yamt 		break;
   1042  1.1.2.2  yamt 
   1043  1.1.2.2  yamt 	case PWR_RESUME:
   1044  1.1.2.2  yamt 		if (sc->sc_flags & BT3C_SLEEPING) {
   1045  1.1.2.6  yamt 			aprint_verbose_dev(sc->sc_dev, "waking up\n");
   1046  1.1.2.2  yamt 			sc->sc_flags &= ~BT3C_SLEEPING;
   1047  1.1.2.3  yamt 
   1048  1.1.2.7  yamt 			sc->sc_unit = hci_attach(&bt3c_hci, sc->sc_dev,
   1049  1.1.2.7  yamt 			    BTF_POWER_UP_NOOP);
   1050  1.1.2.2  yamt 		}
   1051  1.1.2.2  yamt 		break;
   1052  1.1.2.2  yamt 
   1053  1.1.2.2  yamt 	case PWR_SOFTSUSPEND:
   1054  1.1.2.2  yamt 	case PWR_SOFTSTANDBY:
   1055  1.1.2.2  yamt 	case PWR_SOFTRESUME:
   1056  1.1.2.2  yamt 		break;
   1057  1.1.2.2  yamt 	}
   1058  1.1.2.2  yamt }
   1059