xref: /netbsd-src/sys/dev/pcmcia/bt3c.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1*481d3881Srin /* $NetBSD: bt3c.c,v 1.24 2024/07/05 04:31:51 rin Exp $ */
2a5c89047Sgdamore 
3a5c89047Sgdamore /*-
4a5c89047Sgdamore  * Copyright (c) 2005 Iain D. Hibbert,
5a5c89047Sgdamore  * All rights reserved.
6a5c89047Sgdamore  *
7a5c89047Sgdamore  * Redistribution and use in source and binary forms, with or without
8a5c89047Sgdamore  * modification, are permitted provided that the following conditions
9a5c89047Sgdamore  * are met:
10a5c89047Sgdamore  * 1. Redistributions of source code must retain the above copyright
11a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer.
12a5c89047Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
13a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer in the
14a5c89047Sgdamore  *    documentation and/or other materials provided with the distribution.
15a5c89047Sgdamore  * 3. The name of the author may not be used to endorse or promote products
16a5c89047Sgdamore  *    derived from this software without specific prior written permission.
17a5c89047Sgdamore  *
18a5c89047Sgdamore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19a5c89047Sgdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20a5c89047Sgdamore  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21a5c89047Sgdamore  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22a5c89047Sgdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23a5c89047Sgdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24a5c89047Sgdamore  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25a5c89047Sgdamore  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26a5c89047Sgdamore  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27a5c89047Sgdamore  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28a5c89047Sgdamore  */
29a5c89047Sgdamore 
30a5c89047Sgdamore /*
31a5c89047Sgdamore  * Driver for the 3Com Bluetooth PC Card 3CRWB6096, written with reference to
32a5c89047Sgdamore  *  FreeBSD and BlueZ drivers for same, with credit for those going to:
33a5c89047Sgdamore  *
34a5c89047Sgdamore  *		Maksim Yevmenkin <m_evmenkin@yahoo.com>		(FreeBSD)
35a5c89047Sgdamore  *		Marcel Holtmann <marcel@holtmann.org>		(BlueZ)
36a5c89047Sgdamore  *		Jose Orlando Pereira <jop@di.uminho.pt>		(BlueZ)
37a5c89047Sgdamore  *		David Hinds <dahinds@users.sourceforge.net>	(Original Code)
38a5c89047Sgdamore  */
39a5c89047Sgdamore 
40a5c89047Sgdamore /*
41a5c89047Sgdamore  * The CIS info from my card:
42a5c89047Sgdamore  *
43a5c89047Sgdamore  *	pcmcia1: CIS tuple chain:
44a5c89047Sgdamore  *	CISTPL_DEVICE type=null speed=null
45a5c89047Sgdamore  *	 01 03 00 00 ff
46a5c89047Sgdamore  *	CISTPL_VERS_1
47a5c89047Sgdamore  *	 15 24 05 00 33 43 4f 4d 00 33 43 52 57 42 36 30
48a5c89047Sgdamore  *	 2d 41 00 42 6c 75 65 74 6f 6f 74 68 20 50 43 20
49a5c89047Sgdamore  *	 43 61 72 64 00 ff
50a5c89047Sgdamore  *	CISTPL_MANFID
51a5c89047Sgdamore  *	 20 04 01 01 40 00
52a5c89047Sgdamore  *	CISTPL_FUNCID
53a5c89047Sgdamore  *	 21 02 02 01
54a5c89047Sgdamore  *	CISTPL_CONFIG
55a5c89047Sgdamore  *	 1a 06 05 30 20 03 17 00
56a5c89047Sgdamore  *	CISTPL_CFTABLE_ENTRY
57a5c89047Sgdamore  *	 1b 09 f0 41 18 a0 40 07 30 ff ff
58a5c89047Sgdamore  *	unhandled CISTPL 80
59a5c89047Sgdamore  *	 80 0a 02 01 40 00 2d 00 00 00 00 ff
60a5c89047Sgdamore  *	CISTPL_NO_LINK
61a5c89047Sgdamore  *	 14 00
62a5c89047Sgdamore  *	CISTPL_END
63a5c89047Sgdamore  *	 ff
64a5c89047Sgdamore  *	pcmcia1: CIS version PC Card Standard 5.0
65a5c89047Sgdamore  *	pcmcia1: CIS info: 3COM, 3CRWB60-A, Bluetooth PC Card
66a5c89047Sgdamore  *	pcmcia1: Manufacturer code 0x101, product 0x40
67a5c89047Sgdamore  *	pcmcia1: function 0: serial port, ccr addr 320 mask 17
68a5c89047Sgdamore  *	pcmcia1: function 0, config table entry 48: I/O card; irq mask ffff; iomask 0, iospace 0-7; rdybsy_active io8 irqlevel
69a5c89047Sgdamore  */
70a5c89047Sgdamore 
71a5c89047Sgdamore #include <sys/cdefs.h>
72*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: bt3c.c,v 1.24 2024/07/05 04:31:51 rin Exp $");
73a5c89047Sgdamore 
74a5c89047Sgdamore #include <sys/param.h>
75a5c89047Sgdamore #include <sys/device.h>
76a5c89047Sgdamore #include <sys/mbuf.h>
77a5c89047Sgdamore #include <sys/systm.h>
78a5c89047Sgdamore 
79a2a38285Sad #include <sys/cpu.h>
80a2a38285Sad #include <sys/bus.h>
81a2a38285Sad #include <sys/intr.h>
82a5c89047Sgdamore 
83a5c89047Sgdamore #include <dev/pcmcia/pcmciareg.h>
84a5c89047Sgdamore #include <dev/pcmcia/pcmciavar.h>
85a5c89047Sgdamore #include <dev/pcmcia/pcmciadevs.h>
86a5c89047Sgdamore 
87a5c89047Sgdamore #include <netbt/bluetooth.h>
88a5c89047Sgdamore #include <netbt/hci.h>
89a5c89047Sgdamore 
90a5c89047Sgdamore #include <dev/firmload.h>
91a5c89047Sgdamore #define BT3C_FIRMWARE_FILE	"BT3CPCC.bin"
92a5c89047Sgdamore 
93a5c89047Sgdamore /**************************************************************************
94a5c89047Sgdamore  *
95a5c89047Sgdamore  *	bt3c autoconfig glue
96a5c89047Sgdamore  */
97a5c89047Sgdamore 
98a5c89047Sgdamore struct bt3c_softc {
99edb74239Splunky 	device_t	sc_dev;
100a5c89047Sgdamore 
101a5c89047Sgdamore 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
102a5c89047Sgdamore 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
103a5c89047Sgdamore 	int		sc_iow;			/* our i/o window */
104a5c89047Sgdamore 	int		sc_flags;		/* flags */
105a5c89047Sgdamore 
106736a9db0Splunky 	struct hci_unit *sc_unit;		/* Bluetooth HCI Unit */
107736a9db0Splunky 	struct bt_stats	sc_stats;		/* HCI stats */
108a5c89047Sgdamore 
109a5c89047Sgdamore 	/* hardware interrupt */
110a5c89047Sgdamore 	void		*sc_intr;		/* cookie */
111a5c89047Sgdamore 	int		sc_state;		/* receive state */
112a5c89047Sgdamore 	int		sc_want;		/* how much we want */
113a5c89047Sgdamore 	struct mbuf	*sc_rxp;		/* incoming packet */
114a5c89047Sgdamore 	struct mbuf	*sc_txp;		/* outgoing packet */
115736a9db0Splunky 
116736a9db0Splunky 	/* transmit queues */
117736a9db0Splunky 	MBUFQ_HEAD()	sc_cmdq;		/* commands */
118736a9db0Splunky 	MBUFQ_HEAD()	sc_aclq;		/* ACL data */
119736a9db0Splunky 	MBUFQ_HEAD()	sc_scoq;		/* SCO data */
120a5c89047Sgdamore };
121a5c89047Sgdamore 
122a5c89047Sgdamore /* sc_state */				/* receiving */
123a5c89047Sgdamore #define BT3C_RECV_PKT_TYPE	0		/* packet type */
124a5c89047Sgdamore #define BT3C_RECV_ACL_HDR	1		/* acl header */
125a5c89047Sgdamore #define BT3C_RECV_SCO_HDR	2		/* sco header */
126a5c89047Sgdamore #define BT3C_RECV_EVENT_HDR	3		/* event header */
127a5c89047Sgdamore #define BT3C_RECV_ACL_DATA	4		/* acl packet data */
128a5c89047Sgdamore #define BT3C_RECV_SCO_DATA	5		/* sco packet data */
129a5c89047Sgdamore #define BT3C_RECV_EVENT_DATA	6		/* event packet data */
130a5c89047Sgdamore 
131a5c89047Sgdamore /* sc_flags */
132736a9db0Splunky #define BT3C_XMIT		(1 << 1)	/* transmit active */
133736a9db0Splunky #define BT3C_ENABLED		(1 << 2)	/* enabled */
134a5c89047Sgdamore 
135d16a259fScegger static int bt3c_match(device_t, cfdata_t, void *);
136edb74239Splunky static void bt3c_attach(device_t, device_t, void *);
137edb74239Splunky static int bt3c_detach(device_t, int);
138c1b390d4Sdyoung static bool bt3c_suspend(device_t, const pmf_qual_t *);
139c1b390d4Sdyoung static bool bt3c_resume(device_t, const pmf_qual_t *);
140a5c89047Sgdamore 
141edb74239Splunky CFATTACH_DECL_NEW(bt3c, sizeof(struct bt3c_softc),
14241358e9bSplunky     bt3c_match, bt3c_attach, bt3c_detach, NULL);
143a5c89047Sgdamore 
1440b799668Splunky static int bt3c_enable(device_t);
1450b799668Splunky static void bt3c_disable(device_t);
146736a9db0Splunky static void bt3c_output_cmd(device_t, struct mbuf *);
147736a9db0Splunky static void bt3c_output_acl(device_t, struct mbuf *);
148736a9db0Splunky static void bt3c_output_sco(device_t, struct mbuf *);
149736a9db0Splunky static void bt3c_stats(device_t, struct bt_stats *, int);
150736a9db0Splunky 
151736a9db0Splunky static const struct hci_if bt3c_hci = {
152736a9db0Splunky 	.enable =	bt3c_enable,
153736a9db0Splunky 	.disable =	bt3c_disable,
154736a9db0Splunky 	.output_cmd =	bt3c_output_cmd,
155736a9db0Splunky 	.output_acl =	bt3c_output_acl,
156736a9db0Splunky 	.output_sco =	bt3c_output_sco,
157736a9db0Splunky 	.get_stats =	bt3c_stats,
158736a9db0Splunky 	.ipl =		IPL_TTY,
159736a9db0Splunky };
160736a9db0Splunky 
161736a9db0Splunky static void bt3c_start(struct bt3c_softc *);
162a5c89047Sgdamore 
163a5c89047Sgdamore /**************************************************************************
164a5c89047Sgdamore  *
165a5c89047Sgdamore  *	Hardware Definitions & IO routines
166a5c89047Sgdamore  *
167a5c89047Sgdamore  *	I made up the names for most of these defs since we dont have
168a5c89047Sgdamore  *	manufacturers recommendations, but I dont like raw numbers..
169a5c89047Sgdamore  *
170a5c89047Sgdamore  *	all hardware routines are running at IPL_TTY
171a5c89047Sgdamore  *
172a5c89047Sgdamore  */
173a5c89047Sgdamore #define BT3C_ISR		0x7001		/* Interrupt Status Register */
174a5c89047Sgdamore #define BT3C_ISR_RXRDY			(1<<0)	/* Device has data */
175a5c89047Sgdamore #define BT3C_ISR_TXRDY			(1<<1)	/* Finished sending data */
176a5c89047Sgdamore #define BT3C_ISR_ANTENNA		(1<<5)	/* Antenna position changed */
177a5c89047Sgdamore 
178a5c89047Sgdamore #define BT3C_CSR		0x7002		/* Card Status Register */
179a5c89047Sgdamore #define BT3C_CSR_ANTENNA		(1<<4)	/* Antenna position */
180a5c89047Sgdamore 
181a5c89047Sgdamore #define BT3C_TX_COUNT		0x7005		/* Tx fifo contents */
182a5c89047Sgdamore #define BT3C_TX_FIFO		0x7080		/* Transmit Fifo */
183a5c89047Sgdamore #define BT3C_RX_COUNT		0x7006		/* Rx fifo contents */
184a5c89047Sgdamore #define BT3C_RX_FIFO		0x7480		/* Receive Fifo */
185a5c89047Sgdamore #define BT3C_FIFO_SIZE			256
186a5c89047Sgdamore 
187a5c89047Sgdamore /* IO Registers */
188a5c89047Sgdamore #define BT3C_IOR_DATA_L		0x00		/* data low byte */
189a5c89047Sgdamore #define BT3C_IOR_DATA_H		0x01		/* data high byte */
190a5c89047Sgdamore #define BT3C_IOR_ADDR_L		0x02		/* address low byte */
191a5c89047Sgdamore #define BT3C_IOR_ADDR_H		0x03		/* address high byte */
192a5c89047Sgdamore #define BT3C_IOR_CNTL		0x04		/* control byte */
193a5c89047Sgdamore #define BT3C_IOR_CNTL_BOOT		(1<<6)	/* Boot Card */
194a5c89047Sgdamore #define BT3C_IOR_CNTL_INTR		(1<<7)	/* Interrupt Requested */
195a5c89047Sgdamore #define BT3C_IOR_LEN		0x05
196a5c89047Sgdamore 
197a5c89047Sgdamore static inline uint16_t
bt3c_get(struct bt3c_softc * sc)198a5c89047Sgdamore bt3c_get(struct bt3c_softc *sc)
199a5c89047Sgdamore {
200a5c89047Sgdamore 	uint16_t data;
201a5c89047Sgdamore 
202a5c89047Sgdamore 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
203a5c89047Sgdamore 				0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_READ);
204a5c89047Sgdamore 	data = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
205a5c89047Sgdamore 				BT3C_IOR_DATA_L);
206a5c89047Sgdamore 	data |= bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
207a5c89047Sgdamore 				BT3C_IOR_DATA_H) << 8;
208a5c89047Sgdamore 
209a5c89047Sgdamore 	return data;
210a5c89047Sgdamore }
211a5c89047Sgdamore 
212a5c89047Sgdamore static inline void
bt3c_put(struct bt3c_softc * sc,uint16_t data)213a5c89047Sgdamore bt3c_put(struct bt3c_softc *sc, uint16_t data)
214a5c89047Sgdamore {
215a5c89047Sgdamore 
216a5c89047Sgdamore 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
217a5c89047Sgdamore 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_WRITE);
218a5c89047Sgdamore 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
219a5c89047Sgdamore 			BT3C_IOR_DATA_L, data & 0xff);
220a5c89047Sgdamore 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
221a5c89047Sgdamore 			BT3C_IOR_DATA_H, (data >> 8) & 0xff);
222a5c89047Sgdamore }
223a5c89047Sgdamore 
224a5c89047Sgdamore static inline uint8_t
bt3c_read_control(struct bt3c_softc * sc)225a5c89047Sgdamore bt3c_read_control(struct bt3c_softc *sc)
226a5c89047Sgdamore {
227a5c89047Sgdamore 
228a5c89047Sgdamore 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
229a5c89047Sgdamore 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_READ);
230a5c89047Sgdamore 	return bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
231a5c89047Sgdamore 			BT3C_IOR_CNTL);
232a5c89047Sgdamore }
233a5c89047Sgdamore 
234a5c89047Sgdamore static inline void
bt3c_write_control(struct bt3c_softc * sc,uint8_t data)235a5c89047Sgdamore bt3c_write_control(struct bt3c_softc *sc, uint8_t data)
236a5c89047Sgdamore {
237a5c89047Sgdamore 
238a5c89047Sgdamore 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
239a5c89047Sgdamore 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_WRITE);
240a5c89047Sgdamore 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
241a5c89047Sgdamore 			BT3C_IOR_CNTL, data);
242a5c89047Sgdamore }
243a5c89047Sgdamore 
244a5c89047Sgdamore static inline void
bt3c_set_address(struct bt3c_softc * sc,uint16_t addr)245a5c89047Sgdamore bt3c_set_address(struct bt3c_softc *sc, uint16_t addr)
246a5c89047Sgdamore {
247a5c89047Sgdamore 
248a5c89047Sgdamore 	bus_space_barrier(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
249a5c89047Sgdamore 			0, BT3C_IOR_LEN, BUS_SPACE_BARRIER_WRITE);
250a5c89047Sgdamore 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
251a5c89047Sgdamore 			BT3C_IOR_ADDR_L, addr & 0xff);
252a5c89047Sgdamore 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
253a5c89047Sgdamore 			BT3C_IOR_ADDR_H, (addr >> 8) & 0xff);
254a5c89047Sgdamore }
255a5c89047Sgdamore 
256a5c89047Sgdamore static inline uint16_t
bt3c_read(struct bt3c_softc * sc,uint16_t addr)257a5c89047Sgdamore bt3c_read(struct bt3c_softc *sc, uint16_t addr)
258a5c89047Sgdamore {
259a5c89047Sgdamore 
260a5c89047Sgdamore 	bt3c_set_address(sc, addr);
261a5c89047Sgdamore 	return bt3c_get(sc);
262a5c89047Sgdamore }
263a5c89047Sgdamore 
264a5c89047Sgdamore static inline void
bt3c_write(struct bt3c_softc * sc,uint16_t addr,uint16_t data)265a5c89047Sgdamore bt3c_write(struct bt3c_softc *sc, uint16_t addr, uint16_t data)
266a5c89047Sgdamore {
267a5c89047Sgdamore 
268a5c89047Sgdamore 	bt3c_set_address(sc, addr);
269a5c89047Sgdamore 	bt3c_put(sc, data);
270a5c89047Sgdamore }
271a5c89047Sgdamore 
272a5c89047Sgdamore /*
273a5c89047Sgdamore  * receive incoming data from device, store in mbuf chain and
274a5c89047Sgdamore  * pass on complete packets to bt device
275a5c89047Sgdamore  */
276a5c89047Sgdamore static void
bt3c_receive(struct bt3c_softc * sc)277a5c89047Sgdamore bt3c_receive(struct bt3c_softc *sc)
278a5c89047Sgdamore {
279a5c89047Sgdamore 	struct mbuf *m = sc->sc_rxp;
280a5c89047Sgdamore 	int space = 0;
281a5c89047Sgdamore 	uint16_t count;
282a5c89047Sgdamore 	uint8_t b;
283a5c89047Sgdamore 
284a5c89047Sgdamore 	/*
285a5c89047Sgdamore 	 * If we already started a packet, find the
286a5c89047Sgdamore 	 * trailing end of it.
287a5c89047Sgdamore 	 */
288a5c89047Sgdamore 	if (m) {
289a5c89047Sgdamore 		while (m->m_next)
290a5c89047Sgdamore 			m = m->m_next;
291a5c89047Sgdamore 
292a5c89047Sgdamore 		space = M_TRAILINGSPACE(m);
293a5c89047Sgdamore 	}
294a5c89047Sgdamore 
295a5c89047Sgdamore 	count = bt3c_read(sc, BT3C_RX_COUNT);
296a5c89047Sgdamore 	bt3c_set_address(sc, BT3C_RX_FIFO);
297a5c89047Sgdamore 
298a5c89047Sgdamore 	while (count > 0) {
299a5c89047Sgdamore 		if (space == 0) {
300a5c89047Sgdamore 			if (m == NULL) {
301a5c89047Sgdamore 				/* new packet */
302a5c89047Sgdamore 				MGETHDR(m, M_DONTWAIT, MT_DATA);
303a5c89047Sgdamore 				if (m == NULL) {
3047cca9485Splunky 					aprint_error_dev(sc->sc_dev,
3057cca9485Splunky 					    "out of memory\n");
306736a9db0Splunky 					sc->sc_stats.err_rx++;
307a5c89047Sgdamore 					goto out;	/* (lost sync) */
308a5c89047Sgdamore 				}
309a5c89047Sgdamore 
310a5c89047Sgdamore 				sc->sc_rxp = m;
311a5c89047Sgdamore 				m->m_pkthdr.len = m->m_len = 0;
312a5c89047Sgdamore 				space = MHLEN;
313a5c89047Sgdamore 
314a5c89047Sgdamore 				sc->sc_state = BT3C_RECV_PKT_TYPE;
315a5c89047Sgdamore 				sc->sc_want = 1;
316a5c89047Sgdamore 			} else {
317a5c89047Sgdamore 				/* extend mbuf */
318a5c89047Sgdamore 				MGET(m->m_next, M_DONTWAIT, MT_DATA);
319a5c89047Sgdamore 				if (m->m_next == NULL) {
3207cca9485Splunky 					aprint_error_dev(sc->sc_dev,
3217cca9485Splunky 					    "out of memory\n");
322736a9db0Splunky 					sc->sc_stats.err_rx++;
323a5c89047Sgdamore 					goto out;	/* (lost sync) */
324a5c89047Sgdamore 				}
325a5c89047Sgdamore 
326a5c89047Sgdamore 				m = m->m_next;
327a5c89047Sgdamore 				m->m_len = 0;
328a5c89047Sgdamore 				space = MLEN;
329a5c89047Sgdamore 
330a5c89047Sgdamore 				if (sc->sc_want > MINCLSIZE) {
331a5c89047Sgdamore 					MCLGET(m, M_DONTWAIT);
332a5c89047Sgdamore 					if (m->m_flags & M_EXT)
333a5c89047Sgdamore 						space = MCLBYTES;
334a5c89047Sgdamore 				}
335a5c89047Sgdamore 			}
336a5c89047Sgdamore 		}
337a5c89047Sgdamore 
338a5c89047Sgdamore 		b = bt3c_get(sc);
339a5c89047Sgdamore 		mtod(m, uint8_t *)[m->m_len++] = b;
340a5c89047Sgdamore 		count--;
341a5c89047Sgdamore 		space--;
342a5c89047Sgdamore 		sc->sc_rxp->m_pkthdr.len++;
343736a9db0Splunky 		sc->sc_stats.byte_rx++;
344a5c89047Sgdamore 
345a5c89047Sgdamore 		sc->sc_want--;
346a5c89047Sgdamore 		if (sc->sc_want > 0)
347a5c89047Sgdamore 			continue; /* want more */
348a5c89047Sgdamore 
349a5c89047Sgdamore 		switch (sc->sc_state) {
350a5c89047Sgdamore 		case BT3C_RECV_PKT_TYPE:		/* Got packet type */
351a5c89047Sgdamore 
352a5c89047Sgdamore 			switch (b) {
353a5c89047Sgdamore 			case HCI_ACL_DATA_PKT:
354a5c89047Sgdamore 				sc->sc_state = BT3C_RECV_ACL_HDR;
355a5c89047Sgdamore 				sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
356a5c89047Sgdamore 				break;
357a5c89047Sgdamore 
358a5c89047Sgdamore 			case HCI_SCO_DATA_PKT:
359a5c89047Sgdamore 				sc->sc_state = BT3C_RECV_SCO_HDR;
360a5c89047Sgdamore 				sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
361a5c89047Sgdamore 				break;
362a5c89047Sgdamore 
363a5c89047Sgdamore 			case HCI_EVENT_PKT:
364a5c89047Sgdamore 				sc->sc_state = BT3C_RECV_EVENT_HDR;
365a5c89047Sgdamore 				sc->sc_want = sizeof(hci_event_hdr_t) - 1;
366a5c89047Sgdamore 				break;
367a5c89047Sgdamore 
368a5c89047Sgdamore 			default:
3697cca9485Splunky 				aprint_error_dev(sc->sc_dev,
3707cca9485Splunky 				    "Unknown packet type=%#x!\n", b);
371736a9db0Splunky 				sc->sc_stats.err_rx++;
372a5c89047Sgdamore 				m_freem(sc->sc_rxp);
373a5c89047Sgdamore 				sc->sc_rxp = NULL;
374a5c89047Sgdamore 				goto out;	/* (lost sync) */
375a5c89047Sgdamore 			}
376a5c89047Sgdamore 
377a5c89047Sgdamore 			break;
378a5c89047Sgdamore 
379a5c89047Sgdamore 		/*
380a5c89047Sgdamore 		 * we assume (correctly of course :) that the packet headers
381a5c89047Sgdamore 		 * all fit into a single pkthdr mbuf
382a5c89047Sgdamore 		 */
383a5c89047Sgdamore 		case BT3C_RECV_ACL_HDR:		/* Got ACL Header */
384a5c89047Sgdamore 			sc->sc_state = BT3C_RECV_ACL_DATA;
385a5c89047Sgdamore 			sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
386a5c89047Sgdamore 			sc->sc_want = le16toh(sc->sc_want);
387a5c89047Sgdamore 			break;
388a5c89047Sgdamore 
389a5c89047Sgdamore 		case BT3C_RECV_SCO_HDR:		/* Got SCO Header */
390a5c89047Sgdamore 			sc->sc_state = BT3C_RECV_SCO_DATA;
391a5c89047Sgdamore 			sc->sc_want =  mtod(m, hci_scodata_hdr_t *)->length;
392a5c89047Sgdamore 			break;
393a5c89047Sgdamore 
394a5c89047Sgdamore 		case BT3C_RECV_EVENT_HDR:	/* Got Event Header */
395a5c89047Sgdamore 			sc->sc_state = BT3C_RECV_EVENT_DATA;
396a5c89047Sgdamore 			sc->sc_want =  mtod(m, hci_event_hdr_t *)->length;
397a5c89047Sgdamore 			break;
398a5c89047Sgdamore 
399a5c89047Sgdamore 		case BT3C_RECV_ACL_DATA:	/* ACL Packet Complete */
400736a9db0Splunky 			if (!hci_input_acl(sc->sc_unit, sc->sc_rxp))
401736a9db0Splunky 				sc->sc_stats.err_rx++;
402736a9db0Splunky 
403736a9db0Splunky 			sc->sc_stats.acl_rx++;
404a5c89047Sgdamore 			sc->sc_rxp = m = NULL;
405a5c89047Sgdamore 			space = 0;
406a5c89047Sgdamore 			break;
407a5c89047Sgdamore 
408a5c89047Sgdamore 		case BT3C_RECV_SCO_DATA:	/* SCO Packet Complete */
409736a9db0Splunky 			if (!hci_input_sco(sc->sc_unit, sc->sc_rxp))
410736a9db0Splunky 				sc->sc_stats.err_rx++;
411736a9db0Splunky 
412736a9db0Splunky 			sc->sc_stats.sco_rx++;
413a5c89047Sgdamore 			sc->sc_rxp = m = NULL;
414a5c89047Sgdamore 			space = 0;
415a5c89047Sgdamore 			break;
416a5c89047Sgdamore 
417a5c89047Sgdamore 		case BT3C_RECV_EVENT_DATA:	/* Event Packet Complete */
418736a9db0Splunky 			if (!hci_input_event(sc->sc_unit, sc->sc_rxp))
419736a9db0Splunky 				sc->sc_stats.err_rx++;
420736a9db0Splunky 
421736a9db0Splunky 			sc->sc_stats.evt_rx++;
422a5c89047Sgdamore 			sc->sc_rxp = m = NULL;
423a5c89047Sgdamore 			space = 0;
424a5c89047Sgdamore 			break;
425a5c89047Sgdamore 
426a5c89047Sgdamore 		default:
427a5c89047Sgdamore 			panic("%s: invalid state %d!\n",
428edb74239Splunky 				device_xname(sc->sc_dev), sc->sc_state);
429a5c89047Sgdamore 		}
430a5c89047Sgdamore 	}
431a5c89047Sgdamore 
432a5c89047Sgdamore out:
433a5c89047Sgdamore 	bt3c_write(sc, BT3C_RX_COUNT, 0x0000);
434a5c89047Sgdamore }
435a5c89047Sgdamore 
436a5c89047Sgdamore /*
437a5c89047Sgdamore  * write data from current packet to Transmit FIFO.
438a5c89047Sgdamore  * restart when done.
439a5c89047Sgdamore  */
440a5c89047Sgdamore static void
bt3c_transmit(struct bt3c_softc * sc)441a5c89047Sgdamore bt3c_transmit(struct bt3c_softc *sc)
442a5c89047Sgdamore {
443a5c89047Sgdamore 	struct mbuf *m;
444a5c89047Sgdamore 	int count, rlen;
445a5c89047Sgdamore 	uint8_t *rptr;
446a5c89047Sgdamore 
447a5c89047Sgdamore 	m = sc->sc_txp;
448a5c89047Sgdamore 	if (m == NULL) {
449736a9db0Splunky 		sc->sc_flags &= ~BT3C_XMIT;
450736a9db0Splunky 		bt3c_start(sc);
451a5c89047Sgdamore 		return;
452a5c89047Sgdamore 	}
453a5c89047Sgdamore 
454a5c89047Sgdamore 	count = 0;
455a5c89047Sgdamore 	rlen = 0;
456a5c89047Sgdamore 	rptr = mtod(m, uint8_t *);
457a5c89047Sgdamore 
458a5c89047Sgdamore 	bt3c_set_address(sc, BT3C_TX_FIFO);
459a5c89047Sgdamore 
460a5c89047Sgdamore 	for(;;) {
461a5c89047Sgdamore 		if (rlen >= m->m_len) {
462a5c89047Sgdamore 			m = m->m_next;
463a5c89047Sgdamore 			if (m == NULL) {
464a5c89047Sgdamore 				m = sc->sc_txp;
465a5c89047Sgdamore 				sc->sc_txp = NULL;
466a5c89047Sgdamore 
467a5c89047Sgdamore 				if (M_GETCTX(m, void *) == NULL)
468a5c89047Sgdamore 					m_freem(m);
469736a9db0Splunky 				else if (!hci_complete_sco(sc->sc_unit, m))
470736a9db0Splunky 					sc->sc_stats.err_tx++;
471a5c89047Sgdamore 
472a5c89047Sgdamore 				break;
473a5c89047Sgdamore 			}
474a5c89047Sgdamore 
475a5c89047Sgdamore 			rlen = 0;
476a5c89047Sgdamore 			rptr = mtod(m, uint8_t *);
477a5c89047Sgdamore 			continue;
478a5c89047Sgdamore 		}
479a5c89047Sgdamore 
480a5c89047Sgdamore 		if (count >= BT3C_FIFO_SIZE) {
481a5c89047Sgdamore 			m_adj(m, rlen);
482a5c89047Sgdamore 			break;
483a5c89047Sgdamore 		}
484a5c89047Sgdamore 
485a5c89047Sgdamore 		bt3c_put(sc, *rptr++);
486a5c89047Sgdamore 		rlen++;
487a5c89047Sgdamore 		count++;
488a5c89047Sgdamore 	}
489a5c89047Sgdamore 
490a5c89047Sgdamore 	bt3c_write(sc, BT3C_TX_COUNT, count);
491736a9db0Splunky 	sc->sc_stats.byte_tx += count;
492a5c89047Sgdamore }
493a5c89047Sgdamore 
494a5c89047Sgdamore /*
495a5c89047Sgdamore  * interrupt routine
496a5c89047Sgdamore  */
497a5c89047Sgdamore static int
bt3c_intr(void * arg)498a5c89047Sgdamore bt3c_intr(void *arg)
499a5c89047Sgdamore {
500a5c89047Sgdamore 	struct bt3c_softc *sc = arg;
501a5c89047Sgdamore 	uint16_t control, isr;
502a5c89047Sgdamore 
503a5c89047Sgdamore 	control = bt3c_read_control(sc);
504a5c89047Sgdamore 	if (control & BT3C_IOR_CNTL_INTR) {
505a5c89047Sgdamore 		isr = bt3c_read(sc, BT3C_ISR);
506a5c89047Sgdamore 		if ((isr & 0xff) == 0x7f) {
5077cca9485Splunky 			aprint_error_dev(sc->sc_dev, "strange ISR=%04x\n", isr);
508a5c89047Sgdamore 		} else if ((isr & 0xff) != 0xff) {
509a5c89047Sgdamore 
510a5c89047Sgdamore 			if (isr & BT3C_ISR_RXRDY)
511a5c89047Sgdamore 				bt3c_receive(sc);
512a5c89047Sgdamore 
513a5c89047Sgdamore 			if (isr & BT3C_ISR_TXRDY)
514a5c89047Sgdamore 				bt3c_transmit(sc);
515a5c89047Sgdamore 
516a5c89047Sgdamore #ifdef DIAGNOSTIC
517a5c89047Sgdamore 			if (isr & BT3C_ISR_ANTENNA) {
518a5c89047Sgdamore 				if (bt3c_read(sc, BT3C_CSR) & BT3C_CSR_ANTENNA)
5197cca9485Splunky 					aprint_verbose_dev(sc->sc_dev,
5207cca9485Splunky 					    "Antenna Out\n");
521a5c89047Sgdamore 				else
5227cca9485Splunky 					aprint_verbose_dev(sc->sc_dev,
5237cca9485Splunky 					    "Antenna In\n");
524a5c89047Sgdamore 			}
525a5c89047Sgdamore #endif
526a5c89047Sgdamore 
527a5c89047Sgdamore 			bt3c_write(sc, BT3C_ISR, 0x0000);
528a5c89047Sgdamore 			bt3c_write_control(sc, control);
529a5c89047Sgdamore 
530a5c89047Sgdamore 			return 1; /* handled */
531a5c89047Sgdamore 		}
532a5c89047Sgdamore 	}
533a5c89047Sgdamore 
534a5c89047Sgdamore 	return 0; /* not handled */
535a5c89047Sgdamore }
536a5c89047Sgdamore 
537a5c89047Sgdamore /*
538a5c89047Sgdamore  * load firmware for the device
539a5c89047Sgdamore  *
540cd917419Splunky  * The firmware file is a plain ASCII file in the Motorola S-Record format,
541cd917419Splunky  * with lines in the format:
542a5c89047Sgdamore  *
543a5c89047Sgdamore  *	S<Digit><Len><Address><Data1><Data2>...<DataN><Checksum>
544a5c89047Sgdamore  *
545cd917419Splunky  * <Digit>:	0	header
546cd917419Splunky  *		3	data record (4 byte address)
547cd917419Splunky  *		7	boot record (4 byte address)
548a5c89047Sgdamore  *
549a5c89047Sgdamore  * <Len>:	1 byte, and is the number of bytes in the rest of the line
550a5c89047Sgdamore  * <Address>:	4 byte address (only 2 bytes are valid for bt3c I think)
551a5c89047Sgdamore  * <Data>:	2 byte data word to be written to the address
552a5c89047Sgdamore  * <Checksum>:	checksum of all bytes in the line including <Len>
553a5c89047Sgdamore  *
554a5c89047Sgdamore  * all bytes are in hexadecimal
555a5c89047Sgdamore  */
556a5c89047Sgdamore static inline int32_t
hex(const uint8_t * p,int n)557a5c89047Sgdamore hex(const uint8_t *p, int n)
558a5c89047Sgdamore {
559a5c89047Sgdamore 	uint32_t val = 0;
560a5c89047Sgdamore 
561a5c89047Sgdamore 	while (n > 0) {
562a5c89047Sgdamore 		val <<= 4;
563a5c89047Sgdamore 
564a5c89047Sgdamore 		if ('0' <= *p && *p <= '9')
565a5c89047Sgdamore 			val += (*p - '0');
566a5c89047Sgdamore 		else if ('a' <= *p && *p <= 'f')
567a5c89047Sgdamore 			val += (*p - 'a' + 0xa);
568a5c89047Sgdamore 		else if ('A' <= *p && *p <= 'F')
569a5c89047Sgdamore 			val += (*p - 'A' + 0xa);
570a5c89047Sgdamore 		else
571a5c89047Sgdamore 			return -1;
572a5c89047Sgdamore 
573a5c89047Sgdamore 		p++;
574a5c89047Sgdamore 		n--;
575a5c89047Sgdamore 	}
576a5c89047Sgdamore 
577a5c89047Sgdamore 	return val;
578a5c89047Sgdamore }
579a5c89047Sgdamore 
580a5c89047Sgdamore static int
bt3c_load_firmware(struct bt3c_softc * sc)581a5c89047Sgdamore bt3c_load_firmware(struct bt3c_softc *sc)
582a5c89047Sgdamore {
583a5c89047Sgdamore 	uint8_t *buf, *line, *next, *p;
584a5c89047Sgdamore 	int32_t addr, data;
585a5c89047Sgdamore 	int err, sum, len;
586a5c89047Sgdamore 	firmware_handle_t fh;
587d16a259fScegger 	cfdata_t cf = device_cfdata(sc->sc_dev);
588a5c89047Sgdamore 	size_t size;
589a5c89047Sgdamore 
590edb74239Splunky 	err = firmware_open(cf->cf_name,
591a5c89047Sgdamore 			    BT3C_FIRMWARE_FILE, &fh);
592a5c89047Sgdamore 	if (err) {
5937cca9485Splunky 		aprint_error_dev(sc->sc_dev, "Cannot open firmware %s/%s\n",
5947cca9485Splunky 		    cf->cf_name, BT3C_FIRMWARE_FILE);
595a5c89047Sgdamore 		return err;
596a5c89047Sgdamore 	}
597a5c89047Sgdamore 
598a5c89047Sgdamore 	size = (size_t)firmware_get_size(fh);
599d3b80ef9Schristos 	if (size > 10 * 1024) {	/* sanity check */
6007cca9485Splunky 		aprint_error_dev(sc->sc_dev, "insane firmware file size!\n");
601be3e9fcdSdogcow 		firmware_close(fh);
602be3e9fcdSdogcow 		return EFBIG;
603a5c89047Sgdamore 	}
604a5c89047Sgdamore 
605a5c89047Sgdamore 	buf = firmware_malloc(size);
606a5c89047Sgdamore 	KASSERT(buf != NULL);
607a5c89047Sgdamore 
608a5c89047Sgdamore 	err = firmware_read(fh, 0, buf, size);
609a5c89047Sgdamore 	if (err) {
6107cca9485Splunky 		aprint_error_dev(sc->sc_dev, "Firmware read failed (%d)\n", err);
611a5c89047Sgdamore 		goto out;
612a5c89047Sgdamore 	}
613a5c89047Sgdamore 
614a5c89047Sgdamore 	/* Reset */
615a5c89047Sgdamore 	bt3c_write(sc, 0x8040, 0x0404);
616a5c89047Sgdamore 	bt3c_write(sc, 0x8040, 0x0400);
617a5c89047Sgdamore 	DELAY(1);
618a5c89047Sgdamore 	bt3c_write(sc, 0x8040, 0x0404);
619a5c89047Sgdamore 	DELAY(17);
620a5c89047Sgdamore 
621a5c89047Sgdamore 	next = buf;
622a5c89047Sgdamore 	err = EFTYPE;
623a5c89047Sgdamore 
624a5c89047Sgdamore 	while (next < buf + size) {
625a5c89047Sgdamore 		line = next;
626a5c89047Sgdamore 
627a5c89047Sgdamore 		while (*next != '\r' && *next != '\n') {
628a5c89047Sgdamore 			if (next >= buf + size)
629a5c89047Sgdamore 				goto out;
630a5c89047Sgdamore 
631a5c89047Sgdamore 			next++;
632a5c89047Sgdamore 		}
633a5c89047Sgdamore 
634a5c89047Sgdamore 		/* 14 covers address and checksum minimum */
635a5c89047Sgdamore 		if (next - line < 14)
636a5c89047Sgdamore 			goto out;
637a5c89047Sgdamore 
638a5c89047Sgdamore 		if (line[0] != 'S')
639a5c89047Sgdamore 			goto out;
640a5c89047Sgdamore 
641a5c89047Sgdamore 		/* verify line length */
642a5c89047Sgdamore 		len = hex(line + 2, 2);
643a5c89047Sgdamore 		if (len < 0 || next - line != len * 2 + 4)
644a5c89047Sgdamore 			goto out;
645a5c89047Sgdamore 
646a5c89047Sgdamore 		/* checksum the line */
647a5c89047Sgdamore 		sum = 0;
648a5c89047Sgdamore 		for (p = line + 2 ; p < next ; p += 2)
649a5c89047Sgdamore 			sum += hex(p, 2);
650a5c89047Sgdamore 
651a5c89047Sgdamore 		if ((sum & 0xff) != 0xff)
652a5c89047Sgdamore 			goto out;
653a5c89047Sgdamore 
654a5c89047Sgdamore 		/* extract relevant data */
655a5c89047Sgdamore 		switch (line[1]) {
656a5c89047Sgdamore 		case '0':
657cd917419Splunky 			/* we ignore the header */
658a5c89047Sgdamore 			break;
659a5c89047Sgdamore 
660a5c89047Sgdamore 		case '3':
661a5c89047Sgdamore 			/* find number of data words */
662a5c89047Sgdamore 			len = (len - 5) / 2;
663a5c89047Sgdamore 			if (len > 15)
664a5c89047Sgdamore 				goto out;
665a5c89047Sgdamore 
666a5c89047Sgdamore 			addr = hex(line + 8, 4);
667a5c89047Sgdamore 			if (addr < 0)
668a5c89047Sgdamore 				goto out;
669a5c89047Sgdamore 
670a5c89047Sgdamore 			bt3c_set_address(sc, addr);
671a5c89047Sgdamore 
672a5c89047Sgdamore 			for (p = line + 12 ; p + 4 < next ; p += 4) {
673a5c89047Sgdamore 				data = hex(p, 4);
674a5c89047Sgdamore 				if (data < 0)
675a5c89047Sgdamore 					goto out;
676a5c89047Sgdamore 
677a5c89047Sgdamore 				bt3c_put(sc, data);
678a5c89047Sgdamore 			}
679a5c89047Sgdamore 			break;
680a5c89047Sgdamore 
681a5c89047Sgdamore 		case '7':
682cd917419Splunky 			/*
683cd917419Splunky 			 * for some reason we ignore this record
684cd917419Splunky 			 * and boot from 0x3000 which happens to
685cd917419Splunky 			 * be the first record in the file.
686cd917419Splunky 			 */
687a5c89047Sgdamore 			break;
688a5c89047Sgdamore 
689a5c89047Sgdamore 		default:
690a5c89047Sgdamore 			goto out;
691a5c89047Sgdamore 		}
692a5c89047Sgdamore 
693a5c89047Sgdamore 		/* skip to start of next line */
694a5c89047Sgdamore 		while (next < buf + size && (*next == '\r' || *next == '\n'))
695a5c89047Sgdamore 			next++;
696a5c89047Sgdamore 	}
697a5c89047Sgdamore 
698a5c89047Sgdamore 	err = 0;
699a5c89047Sgdamore 	DELAY(17);
700a5c89047Sgdamore 
701a5c89047Sgdamore 	/* Boot */
702a5c89047Sgdamore 	bt3c_set_address(sc, 0x3000);
703a5c89047Sgdamore 	bt3c_write_control(sc, (bt3c_read_control(sc) | BT3C_IOR_CNTL_BOOT));
704a5c89047Sgdamore 	DELAY(17);
705a5c89047Sgdamore 
706a5c89047Sgdamore 	/* Clear Registers */
707a5c89047Sgdamore 	bt3c_write(sc, BT3C_RX_COUNT, 0x0000);
708a5c89047Sgdamore 	bt3c_write(sc, BT3C_TX_COUNT, 0x0000);
709a5c89047Sgdamore 	bt3c_write(sc, BT3C_ISR, 0x0000);
710a5c89047Sgdamore 	DELAY(1000);
711a5c89047Sgdamore 
712a5c89047Sgdamore out:
713a5c89047Sgdamore 	firmware_free(buf, size);
714a5c89047Sgdamore 	firmware_close(fh);
715a5c89047Sgdamore 	return err;
716a5c89047Sgdamore }
717a5c89047Sgdamore 
718a5c89047Sgdamore /**************************************************************************
719a5c89047Sgdamore  *
720736a9db0Splunky  *  bt device callbacks
721a5c89047Sgdamore  */
722a5c89047Sgdamore 
723a5c89047Sgdamore /*
724a5c89047Sgdamore  * start sending on bt3c
725736a9db0Splunky  * should be called at spltty() when BT3C_XMIT is not set
726a5c89047Sgdamore  */
727a5c89047Sgdamore static void
bt3c_start(struct bt3c_softc * sc)728736a9db0Splunky bt3c_start(struct bt3c_softc *sc)
729a5c89047Sgdamore {
730a5c89047Sgdamore 	struct mbuf *m;
731a5c89047Sgdamore 
732736a9db0Splunky 	KASSERT((sc->sc_flags & BT3C_XMIT) == 0);
733a5c89047Sgdamore 	KASSERT(sc->sc_txp == NULL);
734a5c89047Sgdamore 
735736a9db0Splunky 	if (MBUFQ_FIRST(&sc->sc_cmdq)) {
736736a9db0Splunky 		MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
737736a9db0Splunky 		sc->sc_stats.cmd_tx++;
738a5c89047Sgdamore 		goto start;
739a5c89047Sgdamore 	}
740a5c89047Sgdamore 
741736a9db0Splunky 	if (MBUFQ_FIRST(&sc->sc_scoq)) {
742736a9db0Splunky 		MBUFQ_DEQUEUE(&sc->sc_scoq, m);
743736a9db0Splunky 		sc->sc_stats.sco_tx++;
744a5c89047Sgdamore 		goto start;
745a5c89047Sgdamore 	}
746a5c89047Sgdamore 
747736a9db0Splunky 	if (MBUFQ_FIRST(&sc->sc_aclq)) {
748736a9db0Splunky 		MBUFQ_DEQUEUE(&sc->sc_aclq, m);
749736a9db0Splunky 		sc->sc_stats.acl_tx++;
750a5c89047Sgdamore 		goto start;
751a5c89047Sgdamore 	}
752a5c89047Sgdamore 
753a5c89047Sgdamore 	/* Nothing to send */
754a5c89047Sgdamore 	return;
755a5c89047Sgdamore 
756a5c89047Sgdamore start:
757a5c89047Sgdamore 	sc->sc_txp = m;
758736a9db0Splunky 	sc->sc_flags |= BT3C_XMIT;
759a5c89047Sgdamore 	bt3c_transmit(sc);
760a5c89047Sgdamore }
761a5c89047Sgdamore 
762736a9db0Splunky static void
bt3c_output_cmd(device_t self,struct mbuf * m)763736a9db0Splunky bt3c_output_cmd(device_t self, struct mbuf *m)
764736a9db0Splunky {
765736a9db0Splunky 	struct bt3c_softc *sc = device_private(self);
766736a9db0Splunky 	int s;
767736a9db0Splunky 
768736a9db0Splunky 	KASSERT(sc->sc_flags & BT3C_ENABLED);
769736a9db0Splunky 
770736a9db0Splunky 	M_SETCTX(m, NULL);
771736a9db0Splunky 
772736a9db0Splunky 	s = spltty();
773736a9db0Splunky 	MBUFQ_ENQUEUE(&sc->sc_cmdq, m);
774736a9db0Splunky 	if ((sc->sc_flags & BT3C_XMIT) == 0)
775736a9db0Splunky 		bt3c_start(sc);
776736a9db0Splunky 
777736a9db0Splunky 	splx(s);
778736a9db0Splunky }
779736a9db0Splunky 
780736a9db0Splunky static void
bt3c_output_acl(device_t self,struct mbuf * m)781736a9db0Splunky bt3c_output_acl(device_t self, struct mbuf *m)
782736a9db0Splunky {
783736a9db0Splunky 	struct bt3c_softc *sc = device_private(self);
784736a9db0Splunky 	int s;
785736a9db0Splunky 
786736a9db0Splunky 	KASSERT(sc->sc_flags & BT3C_ENABLED);
787736a9db0Splunky 
788736a9db0Splunky 	M_SETCTX(m, NULL);
789736a9db0Splunky 
790736a9db0Splunky 	s = spltty();
791736a9db0Splunky 	MBUFQ_ENQUEUE(&sc->sc_aclq, m);
792736a9db0Splunky 	if ((sc->sc_flags & BT3C_XMIT) == 0)
793736a9db0Splunky 		bt3c_start(sc);
794736a9db0Splunky 
795736a9db0Splunky 	splx(s);
796736a9db0Splunky }
797736a9db0Splunky 
798736a9db0Splunky static void
bt3c_output_sco(device_t self,struct mbuf * m)799736a9db0Splunky bt3c_output_sco(device_t self, struct mbuf *m)
800736a9db0Splunky {
801736a9db0Splunky 	struct bt3c_softc *sc = device_private(self);
802736a9db0Splunky 	int s;
803736a9db0Splunky 
804736a9db0Splunky 	KASSERT(sc->sc_flags & BT3C_ENABLED);
805736a9db0Splunky 
806736a9db0Splunky 	s = spltty();
807736a9db0Splunky 	MBUFQ_ENQUEUE(&sc->sc_scoq, m);
808736a9db0Splunky 	if ((sc->sc_flags & BT3C_XMIT) == 0)
809736a9db0Splunky 		bt3c_start(sc);
810736a9db0Splunky 
811736a9db0Splunky 	splx(s);
812736a9db0Splunky }
813736a9db0Splunky 
814a5c89047Sgdamore /*
815a5c89047Sgdamore  * enable device
816a5c89047Sgdamore  *	turn on card
817a5c89047Sgdamore  *	load firmware
818a5c89047Sgdamore  *	establish interrupts
819a5c89047Sgdamore  */
820a5c89047Sgdamore static int
bt3c_enable(device_t self)8210b799668Splunky bt3c_enable(device_t self)
822a5c89047Sgdamore {
8230b799668Splunky 	struct bt3c_softc *sc = device_private(self);
824736a9db0Splunky 	int err, s;
825a5c89047Sgdamore 
826736a9db0Splunky 	if (sc->sc_flags & BT3C_ENABLED)
827a5c89047Sgdamore 		return 0;
828a5c89047Sgdamore 
829736a9db0Splunky 	s = spltty();
830736a9db0Splunky 
831a5c89047Sgdamore 	sc->sc_intr = pcmcia_intr_establish(sc->sc_pf, IPL_TTY, bt3c_intr, sc);
832a5c89047Sgdamore 	if (sc->sc_intr == NULL) {
833a5c89047Sgdamore 		err = EIO;
834a5c89047Sgdamore 		goto bad;
835a5c89047Sgdamore 	}
836a5c89047Sgdamore 
837a5c89047Sgdamore 	err = pcmcia_function_enable(sc->sc_pf);
838a5c89047Sgdamore 	if (err)
839a5c89047Sgdamore 		goto bad1;
840a5c89047Sgdamore 
841a5c89047Sgdamore 	err = bt3c_load_firmware(sc);
842a5c89047Sgdamore 	if (err)
843a5c89047Sgdamore 		goto bad2;
844a5c89047Sgdamore 
845736a9db0Splunky 	sc->sc_flags |= BT3C_ENABLED;
846736a9db0Splunky 	sc->sc_flags &= ~BT3C_XMIT;
847a5c89047Sgdamore 
848736a9db0Splunky 	splx(s);
849a5c89047Sgdamore 
850a5c89047Sgdamore 	return 0;
851a5c89047Sgdamore 
852a5c89047Sgdamore bad2:
853a5c89047Sgdamore 	pcmcia_function_disable(sc->sc_pf);
854a5c89047Sgdamore bad1:
855a5c89047Sgdamore 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
856a5c89047Sgdamore 	sc->sc_intr = NULL;
857a5c89047Sgdamore bad:
858736a9db0Splunky 	splx(s);
859a5c89047Sgdamore 	return err;
860a5c89047Sgdamore }
861a5c89047Sgdamore 
862a5c89047Sgdamore /*
863a5c89047Sgdamore  * disable device
864a5c89047Sgdamore  *	shut down card
865a5c89047Sgdamore  *	disestablish interrupts
866a5c89047Sgdamore  *	free held packets
867a5c89047Sgdamore  */
868a5c89047Sgdamore static void
bt3c_disable(device_t self)8690b799668Splunky bt3c_disable(device_t self)
870a5c89047Sgdamore {
8710b799668Splunky 	struct bt3c_softc *sc = device_private(self);
872736a9db0Splunky 	int s;
873a5c89047Sgdamore 
874736a9db0Splunky 	if ((sc->sc_flags & BT3C_ENABLED) == 0)
875a5c89047Sgdamore 		return;
876a5c89047Sgdamore 
877736a9db0Splunky 	s = spltty();
878736a9db0Splunky 
879a5c89047Sgdamore 	pcmcia_function_disable(sc->sc_pf);
880a5c89047Sgdamore 
881a5c89047Sgdamore 	if (sc->sc_intr) {
882a5c89047Sgdamore 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
883a5c89047Sgdamore 		sc->sc_intr = NULL;
884a5c89047Sgdamore 	}
885a5c89047Sgdamore 
886a5c89047Sgdamore 	m_freem(sc->sc_rxp);
887a5c89047Sgdamore 	sc->sc_rxp = NULL;
888a5c89047Sgdamore 
889a5c89047Sgdamore 	m_freem(sc->sc_txp);
890a5c89047Sgdamore 	sc->sc_txp = NULL;
891a5c89047Sgdamore 
892736a9db0Splunky 	MBUFQ_DRAIN(&sc->sc_cmdq);
893736a9db0Splunky 	MBUFQ_DRAIN(&sc->sc_aclq);
894736a9db0Splunky 	MBUFQ_DRAIN(&sc->sc_scoq);
895736a9db0Splunky 
896736a9db0Splunky 	sc->sc_flags &= ~BT3C_ENABLED;
897736a9db0Splunky 	splx(s);
898736a9db0Splunky }
899736a9db0Splunky 
900736a9db0Splunky void
bt3c_stats(device_t self,struct bt_stats * dest,int flush)901736a9db0Splunky bt3c_stats(device_t self, struct bt_stats *dest, int flush)
902736a9db0Splunky {
903736a9db0Splunky 	struct bt3c_softc *sc = device_private(self);
904736a9db0Splunky 	int s;
905736a9db0Splunky 
906736a9db0Splunky 	s = spltty();
907736a9db0Splunky 	memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
908736a9db0Splunky 
909736a9db0Splunky 	if (flush)
910736a9db0Splunky 		memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
911736a9db0Splunky 
912736a9db0Splunky 	splx(s);
913a5c89047Sgdamore }
914a5c89047Sgdamore 
915a5c89047Sgdamore /**************************************************************************
916a5c89047Sgdamore  *
917a5c89047Sgdamore  *	bt3c PCMCIA autoconfig glue
918a5c89047Sgdamore  */
919a5c89047Sgdamore 
920a5c89047Sgdamore static int
bt3c_match(device_t parent,cfdata_t match,void * aux)921d16a259fScegger bt3c_match(device_t parent, cfdata_t match, void *aux)
922a5c89047Sgdamore {
923a5c89047Sgdamore 	struct pcmcia_attach_args *pa = aux;
924a5c89047Sgdamore 
925a5c89047Sgdamore 	if (pa->manufacturer == PCMCIA_VENDOR_3COM &&
926a5c89047Sgdamore 	    pa->product == PCMCIA_PRODUCT_3COM_3CRWB6096)
927a5c89047Sgdamore 	    return 10;		/* 'com' also claims this, so trump them */
928a5c89047Sgdamore 
929a5c89047Sgdamore 	return 0;
930a5c89047Sgdamore }
931a5c89047Sgdamore 
932a5c89047Sgdamore static void
bt3c_attach(device_t parent,device_t self,void * aux)933edb74239Splunky bt3c_attach(device_t parent, device_t self, void *aux)
934a5c89047Sgdamore {
935edb74239Splunky 	struct bt3c_softc *sc = device_private(self);
936a5c89047Sgdamore 	struct pcmcia_attach_args *pa = aux;
937a5c89047Sgdamore 	struct pcmcia_config_entry *cfe;
938a5c89047Sgdamore 
939edb74239Splunky 	sc->sc_dev = self;
940a5c89047Sgdamore 	sc->sc_pf = pa->pf;
941a5c89047Sgdamore 
942736a9db0Splunky 	MBUFQ_INIT(&sc->sc_cmdq);
943736a9db0Splunky 	MBUFQ_INIT(&sc->sc_aclq);
944736a9db0Splunky 	MBUFQ_INIT(&sc->sc_scoq);
945736a9db0Splunky 
946a5c89047Sgdamore 	/* Find a PCMCIA config entry we can use */
947a5c89047Sgdamore 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
948a5c89047Sgdamore 		if (cfe->num_memspace != 0)
949a5c89047Sgdamore 			continue;
950a5c89047Sgdamore 
951a5c89047Sgdamore 		if (cfe->num_iospace != 1)
952a5c89047Sgdamore 			continue;
953a5c89047Sgdamore 
954a5c89047Sgdamore 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
955a5c89047Sgdamore 				cfe->iospace[0].length, 0, &sc->sc_pcioh) == 0)
956a5c89047Sgdamore 			break;
957a5c89047Sgdamore 	}
958a5c89047Sgdamore 
959a5c89047Sgdamore 	if (cfe == 0) {
96010b824ffSplunky 		aprint_error_dev(self, "cannot allocate io space\n");
961a5c89047Sgdamore 		goto no_config_entry;
962a5c89047Sgdamore 	}
963a5c89047Sgdamore 
964a5c89047Sgdamore 	/* Initialise it */
965a5c89047Sgdamore 	pcmcia_function_init(pa->pf, cfe);
966a5c89047Sgdamore 
967a5c89047Sgdamore 	/* Map in the io space */
968a5c89047Sgdamore 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO,
969a5c89047Sgdamore 			&sc->sc_pcioh, &sc->sc_iow)) {
97010b824ffSplunky 		aprint_error_dev(self, "cannot map io space\n");
971a5c89047Sgdamore 		goto iomap_failed;
972a5c89047Sgdamore 	}
973a5c89047Sgdamore 
974a5c89047Sgdamore 	/* Attach Bluetooth unit */
97556a73a7dSrmind 	sc->sc_unit = hci_attach_pcb(&bt3c_hci, self, BTF_POWER_UP_NOOP);
976587a6627Splunky 	if (sc->sc_unit == NULL)
977587a6627Splunky 		aprint_error_dev(self, "HCI attach failed\n");
978a5c89047Sgdamore 
979587a6627Splunky 	if (!pmf_device_register(self, bt3c_suspend, bt3c_resume))
980587a6627Splunky 		aprint_error_dev(self, "couldn't establish power handler\n");
981587a6627Splunky 
982a5c89047Sgdamore 	return;
983a5c89047Sgdamore 
984a5c89047Sgdamore iomap_failed:
985a5c89047Sgdamore 	/* unmap io space */
986a5c89047Sgdamore 	pcmcia_io_free(pa->pf, &sc->sc_pcioh);
987a5c89047Sgdamore 
988a5c89047Sgdamore no_config_entry:
989a5c89047Sgdamore 	sc->sc_iow = -1;
990a5c89047Sgdamore }
991a5c89047Sgdamore 
992a5c89047Sgdamore static int
bt3c_detach(device_t self,int flags)993edb74239Splunky bt3c_detach(device_t self, int flags)
994a5c89047Sgdamore {
995edb74239Splunky 	struct bt3c_softc *sc = device_private(self);
996a5c89047Sgdamore 	int err = 0;
997a5c89047Sgdamore 
998587a6627Splunky 	pmf_device_deregister(self);
9990b799668Splunky 	bt3c_disable(self);
1000a5c89047Sgdamore 
1001736a9db0Splunky 	if (sc->sc_unit) {
100256a73a7dSrmind 		hci_detach_pcb(sc->sc_unit);
1003736a9db0Splunky 		sc->sc_unit = NULL;
1004736a9db0Splunky 	}
1005a5c89047Sgdamore 
1006a5c89047Sgdamore 	if (sc->sc_iow != -1) {
1007a5c89047Sgdamore 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iow);
1008a5c89047Sgdamore 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
1009a5c89047Sgdamore 		sc->sc_iow = -1;
1010a5c89047Sgdamore 	}
1011a5c89047Sgdamore 
1012a5c89047Sgdamore 	return err;
1013a5c89047Sgdamore }
1014a5c89047Sgdamore 
1015587a6627Splunky static bool
bt3c_suspend(device_t self,const pmf_qual_t * qual)1016c1b390d4Sdyoung bt3c_suspend(device_t self, const pmf_qual_t *qual)
1017a5c89047Sgdamore {
1018587a6627Splunky 	struct bt3c_softc *sc = device_private(self);
1019a5c89047Sgdamore 
1020736a9db0Splunky 	if (sc->sc_unit) {
102156a73a7dSrmind 		hci_detach_pcb(sc->sc_unit);
1022736a9db0Splunky 		sc->sc_unit = NULL;
1023736a9db0Splunky 	}
1024f2c2a49aSplunky 
1025587a6627Splunky 	return true;
1026a5c89047Sgdamore }
1027a5c89047Sgdamore 
1028587a6627Splunky static bool
bt3c_resume(device_t self,const pmf_qual_t * qual)1029c1b390d4Sdyoung bt3c_resume(device_t self, const pmf_qual_t *qual)
1030587a6627Splunky {
1031587a6627Splunky 	struct bt3c_softc *sc = device_private(self);
1032f2c2a49aSplunky 
1033587a6627Splunky 	KASSERT(sc->sc_unit == NULL);
1034a5c89047Sgdamore 
103556a73a7dSrmind 	sc->sc_unit = hci_attach_pcb(&bt3c_hci, self, BTF_POWER_UP_NOOP);
1036587a6627Splunky 	if (sc->sc_unit == NULL)
1037587a6627Splunky 		return false;
1038587a6627Splunky 
1039587a6627Splunky 	return true;
1040a5c89047Sgdamore }
1041