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