btbc.c revision 1.11 1 1.11 plunky /* $NetBSD: btbc.c,v 1.11 2007/11/28 20:16:11 plunky Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2007 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara /*
28 1.1 kiyohara * This driver is support to the AnyCom BlueCard. written with reference to
29 1.1 kiyohara * Linux driver: (drivers/bluetooth/bluecard_cs.c)
30 1.1 kiyohara */
31 1.1 kiyohara
32 1.1 kiyohara #include <sys/cdefs.h>
33 1.11 plunky __KERNEL_RCSID(0, "$NetBSD: btbc.c,v 1.11 2007/11/28 20:16:11 plunky Exp $");
34 1.1 kiyohara
35 1.1 kiyohara #include <sys/param.h>
36 1.1 kiyohara #include <sys/callout.h>
37 1.1 kiyohara #include <sys/device.h>
38 1.1 kiyohara #include <sys/errno.h>
39 1.1 kiyohara #include <sys/kernel.h>
40 1.1 kiyohara #include <sys/mbuf.h>
41 1.1 kiyohara #include <sys/proc.h>
42 1.1 kiyohara
43 1.5 ad #include <sys/bus.h>
44 1.5 ad #include <sys/intr.h>
45 1.1 kiyohara
46 1.1 kiyohara #include <dev/pcmcia/pcmciareg.h>
47 1.1 kiyohara #include <dev/pcmcia/pcmciavar.h>
48 1.1 kiyohara #include <dev/pcmcia/pcmciadevs.h>
49 1.1 kiyohara
50 1.1 kiyohara #include <netbt/bluetooth.h>
51 1.1 kiyohara #include <netbt/hci.h>
52 1.1 kiyohara
53 1.1 kiyohara #include <dev/pcmcia/bluecardreg.h>
54 1.1 kiyohara
55 1.1 kiyohara
56 1.1 kiyohara /* sc_state */ /* receiving */
57 1.1 kiyohara #define BTBC_RECV_PKT_TYPE 0 /* packet type */
58 1.1 kiyohara #define BTBC_RECV_ACL_HDR 1 /* acl header */
59 1.1 kiyohara #define BTBC_RECV_SCO_HDR 2 /* sco header */
60 1.1 kiyohara #define BTBC_RECV_EVENT_HDR 3 /* event header */
61 1.1 kiyohara #define BTBC_RECV_ACL_DATA 4 /* acl packet data */
62 1.1 kiyohara #define BTBC_RECV_SCO_DATA 5 /* sco packet data */
63 1.1 kiyohara #define BTBC_RECV_EVENT_DATA 6 /* event packet data */
64 1.1 kiyohara
65 1.1 kiyohara /* sc_flags */
66 1.1 kiyohara #define BTBC_SLEEPING (1 << 0) /* but not with the fishes */
67 1.11 plunky #define BTBC_XMIT (1 << 1) /* transmit active */
68 1.11 plunky #define BTBC_ENABLED (1 << 2) /* is enabled */
69 1.1 kiyohara
70 1.1 kiyohara /* Default baud rate: 57600, 115200, 230400 or 460800 */
71 1.10 kiyohara #ifndef BTBC_DEFAULT_BAUDRATE
72 1.4 kiyohara #define BTBC_DEFAULT_BAUDRATE 57600
73 1.10 kiyohara #endif
74 1.1 kiyohara
75 1.1 kiyohara struct btbc_softc {
76 1.6 plunky device_t sc_dev;
77 1.1 kiyohara
78 1.1 kiyohara struct pcmcia_function *sc_pf; /* our PCMCIA function */
79 1.1 kiyohara struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
80 1.1 kiyohara void *sc_powerhook; /* power hook descriptor */
81 1.1 kiyohara int sc_flags; /* flags */
82 1.1 kiyohara
83 1.11 plunky struct hci_unit *sc_unit; /* Bluetooth HCI Unit */
84 1.11 plunky struct bt_stats sc_stats; /* HCI stats */
85 1.1 kiyohara
86 1.1 kiyohara /* hardware interrupt */
87 1.1 kiyohara void *sc_intr; /* cookie */
88 1.1 kiyohara int sc_state; /* receive state */
89 1.1 kiyohara int sc_want; /* how much we want */
90 1.1 kiyohara struct mbuf *sc_rxp; /* incoming packet */
91 1.1 kiyohara struct mbuf *sc_txp; /* outgoing packet */
92 1.1 kiyohara int sc_txstate;
93 1.1 kiyohara #define TXBUF1_EMPTY (1 << 0)
94 1.1 kiyohara #define TXBUF2_EMPTY (1 << 1)
95 1.1 kiyohara #define TXBUF_MASK (1 << 2)
96 1.1 kiyohara
97 1.11 plunky /* output queues */
98 1.11 plunky MBUFQ_HEAD() sc_cmdq;
99 1.11 plunky MBUFQ_HEAD() sc_aclq;
100 1.11 plunky MBUFQ_HEAD() sc_scoq;
101 1.11 plunky
102 1.1 kiyohara callout_t sc_ledch; /* callout handler for LED */
103 1.1 kiyohara uint8_t sc_ctrlreg; /* value for control register */
104 1.1 kiyohara };
105 1.1 kiyohara
106 1.6 plunky static int btbc_match(device_t, struct cfdata *, void *);
107 1.6 plunky static void btbc_attach(device_t, device_t, void *);
108 1.6 plunky static int btbc_detach(device_t, int);
109 1.1 kiyohara static void btbc_power(int, void *);
110 1.1 kiyohara
111 1.1 kiyohara static void btbc_activity_led_timeout(void *);
112 1.1 kiyohara static void btbc_enable_activity_led(struct btbc_softc *);
113 1.1 kiyohara static int btbc_read(struct btbc_softc *, uint32_t, uint8_t *, int);
114 1.1 kiyohara static int btbc_write(struct btbc_softc *, uint32_t, uint8_t *, int);
115 1.1 kiyohara static int btbc_set_baudrate(struct btbc_softc *, int);
116 1.1 kiyohara static void btbc_receive(struct btbc_softc *, uint32_t);
117 1.1 kiyohara static void btbc_transmit(struct btbc_softc *);
118 1.1 kiyohara static int btbc_intr(void *);
119 1.11 plunky static void btbc_start(struct btbc_softc *);
120 1.1 kiyohara
121 1.8 plunky static int btbc_enable(device_t);
122 1.8 plunky static void btbc_disable(device_t);
123 1.11 plunky static void btbc_output_cmd(device_t, struct mbuf *);
124 1.11 plunky static void btbc_output_acl(device_t, struct mbuf *);
125 1.11 plunky static void btbc_output_sco(device_t, struct mbuf *);
126 1.11 plunky static void btbc_stats(device_t, struct bt_stats *, int);
127 1.1 kiyohara
128 1.6 plunky CFATTACH_DECL_NEW(btbc, sizeof(struct btbc_softc),
129 1.3 plunky btbc_match, btbc_attach, btbc_detach, NULL);
130 1.1 kiyohara
131 1.11 plunky static const struct hci_if btbc_hci = {
132 1.11 plunky .enable = btbc_enable,
133 1.11 plunky .disable = btbc_disable,
134 1.11 plunky .output_cmd = btbc_output_cmd,
135 1.11 plunky .output_acl = btbc_output_acl,
136 1.11 plunky .output_sco = btbc_output_sco,
137 1.11 plunky .get_stats = btbc_stats,
138 1.11 plunky .ipl = IPL_TTY,
139 1.11 plunky };
140 1.1 kiyohara
141 1.1 kiyohara /* ARGSUSED */
142 1.1 kiyohara static int
143 1.6 plunky btbc_match(device_t parent, struct cfdata *match, void *aux)
144 1.1 kiyohara {
145 1.1 kiyohara struct pcmcia_attach_args *pa = aux;
146 1.1 kiyohara
147 1.1 kiyohara if (pa->manufacturer == PCMCIA_VENDOR_ANYCOM)
148 1.1 kiyohara if ((pa->product == PCMCIA_PRODUCT_ANYCOM_LSE041) ||
149 1.1 kiyohara (pa->product == PCMCIA_PRODUCT_ANYCOM_LSE039) ||
150 1.1 kiyohara (pa->product == PCMCIA_PRODUCT_ANYCOM_LSE139))
151 1.1 kiyohara return 1;
152 1.1 kiyohara return 0;
153 1.1 kiyohara }
154 1.1 kiyohara
155 1.1 kiyohara static int
156 1.1 kiyohara btbc_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
157 1.1 kiyohara {
158 1.1 kiyohara
159 1.1 kiyohara if (cfe->iftype != PCMCIA_IFTYPE_IO ||
160 1.1 kiyohara cfe->num_iospace < 1 || cfe->num_iospace > 2)
161 1.1 kiyohara return EINVAL;
162 1.1 kiyohara return 0;
163 1.1 kiyohara }
164 1.1 kiyohara
165 1.1 kiyohara /* ARGSUSED */
166 1.1 kiyohara static void
167 1.6 plunky btbc_attach(device_t parent, device_t self, void *aux)
168 1.1 kiyohara {
169 1.8 plunky struct btbc_softc *sc = device_private(self);
170 1.1 kiyohara struct pcmcia_attach_args *pa = aux;
171 1.1 kiyohara struct pcmcia_config_entry *cfe;
172 1.1 kiyohara int error;
173 1.1 kiyohara
174 1.6 plunky sc->sc_dev = self;
175 1.1 kiyohara sc->sc_pf = pa->pf;
176 1.1 kiyohara
177 1.11 plunky MBUFQ_INIT(&sc->sc_cmdq);
178 1.11 plunky MBUFQ_INIT(&sc->sc_aclq);
179 1.11 plunky MBUFQ_INIT(&sc->sc_scoq);
180 1.11 plunky
181 1.1 kiyohara if ((error = pcmcia_function_configure(pa->pf,
182 1.1 kiyohara btbc_pcmcia_validate_config)) != 0) {
183 1.9 plunky aprint_error_dev(self, "configure failed, error=%d\n", error);
184 1.1 kiyohara return;
185 1.1 kiyohara }
186 1.1 kiyohara
187 1.1 kiyohara cfe = pa->pf->cfe;
188 1.1 kiyohara sc->sc_pcioh = cfe->iospace[0].handle;
189 1.1 kiyohara
190 1.1 kiyohara /* Attach Bluetooth unit */
191 1.11 plunky sc->sc_unit = hci_attach(&btbc_hci, self, 0);
192 1.1 kiyohara
193 1.1 kiyohara /* establish a power change hook */
194 1.6 plunky sc->sc_powerhook = powerhook_establish(device_xname(sc->sc_dev),
195 1.1 kiyohara btbc_power, sc);
196 1.1 kiyohara
197 1.1 kiyohara callout_init(&sc->sc_ledch, 0);
198 1.7 plunky callout_setfunc(&sc->sc_ledch, btbc_activity_led_timeout, sc);
199 1.1 kiyohara
200 1.1 kiyohara return;
201 1.1 kiyohara }
202 1.1 kiyohara
203 1.1 kiyohara /* ARGSUSED */
204 1.1 kiyohara static int
205 1.6 plunky btbc_detach(device_t self, int flags)
206 1.1 kiyohara {
207 1.8 plunky struct btbc_softc *sc = device_private(self);
208 1.1 kiyohara int err = 0;
209 1.1 kiyohara
210 1.8 plunky btbc_disable(sc->sc_dev);
211 1.1 kiyohara
212 1.1 kiyohara if (sc->sc_powerhook) {
213 1.1 kiyohara powerhook_disestablish(sc->sc_powerhook);
214 1.1 kiyohara sc->sc_powerhook = NULL;
215 1.1 kiyohara }
216 1.1 kiyohara
217 1.1 kiyohara callout_stop(&sc->sc_ledch);
218 1.7 plunky callout_destroy(&sc->sc_ledch);
219 1.1 kiyohara
220 1.11 plunky if (sc->sc_unit) {
221 1.11 plunky hci_detach(sc->sc_unit);
222 1.11 plunky sc->sc_unit = NULL;
223 1.11 plunky }
224 1.1 kiyohara
225 1.1 kiyohara pcmcia_function_unconfigure(sc->sc_pf);
226 1.1 kiyohara
227 1.1 kiyohara return err;
228 1.1 kiyohara }
229 1.1 kiyohara
230 1.1 kiyohara static void
231 1.1 kiyohara btbc_power(int why, void *arg)
232 1.1 kiyohara {
233 1.1 kiyohara struct btbc_softc *sc = arg;
234 1.1 kiyohara
235 1.1 kiyohara switch(why) {
236 1.1 kiyohara case PWR_SUSPEND:
237 1.1 kiyohara case PWR_STANDBY:
238 1.11 plunky if (sc->sc_flags & BTBC_ENABLED) {
239 1.11 plunky if (sc->sc_unit) {
240 1.11 plunky hci_detach(sc->sc_unit);
241 1.11 plunky sc->sc_unit = NULL;
242 1.11 plunky }
243 1.1 kiyohara
244 1.1 kiyohara sc->sc_flags |= BTBC_SLEEPING;
245 1.8 plunky aprint_verbose_dev(sc->sc_dev, "sleeping\n");
246 1.1 kiyohara }
247 1.1 kiyohara break;
248 1.1 kiyohara
249 1.1 kiyohara case PWR_RESUME:
250 1.1 kiyohara if (sc->sc_flags & BTBC_SLEEPING) {
251 1.8 plunky aprint_verbose_dev(sc->sc_dev, "waking up\n");
252 1.1 kiyohara sc->sc_flags &= ~BTBC_SLEEPING;
253 1.1 kiyohara
254 1.11 plunky sc->sc_unit = hci_attach(&btbc_hci, sc->sc_dev, 0);
255 1.1 kiyohara }
256 1.1 kiyohara break;
257 1.1 kiyohara
258 1.1 kiyohara case PWR_SOFTSUSPEND:
259 1.1 kiyohara case PWR_SOFTSTANDBY:
260 1.1 kiyohara case PWR_SOFTRESUME:
261 1.1 kiyohara break;
262 1.1 kiyohara }
263 1.1 kiyohara }
264 1.1 kiyohara
265 1.1 kiyohara static void
266 1.1 kiyohara btbc_activity_led_timeout(void *arg)
267 1.1 kiyohara {
268 1.1 kiyohara struct btbc_softc *sc = arg;
269 1.1 kiyohara uint8_t id;
270 1.1 kiyohara
271 1.1 kiyohara id = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
272 1.1 kiyohara BLUECARD_LEDCONTROL);
273 1.1 kiyohara if (id & 0x20)
274 1.1 kiyohara /* Disable activity LED */
275 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
276 1.1 kiyohara BLUECARD_LEDCONTROL, 0x08 | 0x20);
277 1.1 kiyohara else
278 1.1 kiyohara /* Disable power LED */
279 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
280 1.1 kiyohara BLUECARD_LEDCONTROL, 0x00);
281 1.1 kiyohara }
282 1.1 kiyohara
283 1.1 kiyohara static void
284 1.1 kiyohara btbc_enable_activity_led(struct btbc_softc *sc)
285 1.1 kiyohara {
286 1.1 kiyohara uint8_t id;
287 1.1 kiyohara
288 1.1 kiyohara id = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
289 1.1 kiyohara BLUECARD_LEDCONTROL);
290 1.1 kiyohara if (id & 0x20) {
291 1.1 kiyohara /* Enable activity LED */
292 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
293 1.1 kiyohara BLUECARD_LEDCONTROL, 0x10 | 0x40);
294 1.1 kiyohara
295 1.1 kiyohara /* Stop the LED after hz/4 */
296 1.7 plunky callout_schedule(&sc->sc_ledch, hz / 4);
297 1.1 kiyohara } else {
298 1.1 kiyohara /* Enable power LED */
299 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
300 1.1 kiyohara BLUECARD_LEDCONTROL, 0x08 | 0x20);
301 1.1 kiyohara
302 1.1 kiyohara /* Stop the LED after HZ/2 */
303 1.7 plunky callout_schedule(&sc->sc_ledch, hz / 2);
304 1.1 kiyohara }
305 1.1 kiyohara }
306 1.1 kiyohara
307 1.1 kiyohara static int
308 1.1 kiyohara btbc_read(struct btbc_softc *sc, uint32_t offset, uint8_t *buf, int buflen)
309 1.1 kiyohara {
310 1.1 kiyohara int i, n, len;
311 1.1 kiyohara
312 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
313 1.1 kiyohara BLUECARD_COMMAND, BLUECARD_COMMAND_RXWIN1);
314 1.1 kiyohara len = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, offset);
315 1.1 kiyohara
316 1.1 kiyohara n = 0;
317 1.1 kiyohara i = 1;
318 1.1 kiyohara while (n < len) {
319 1.1 kiyohara if (i == 16) {
320 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
321 1.1 kiyohara BLUECARD_COMMAND, BLUECARD_COMMAND_RXWIN2);
322 1.1 kiyohara i = 0;
323 1.1 kiyohara }
324 1.1 kiyohara
325 1.1 kiyohara buf[n] = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
326 1.1 kiyohara offset + i);
327 1.1 kiyohara i++;
328 1.1 kiyohara if (++n > buflen)
329 1.1 kiyohara break;
330 1.1 kiyohara }
331 1.1 kiyohara return len;
332 1.1 kiyohara }
333 1.1 kiyohara
334 1.1 kiyohara static int
335 1.1 kiyohara btbc_write(struct btbc_softc *sc, uint32_t offset, uint8_t *buf, int buflen)
336 1.1 kiyohara {
337 1.1 kiyohara int i, actual;
338 1.1 kiyohara
339 1.1 kiyohara actual = (buflen > 15) ? 15 : buflen;
340 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, offset, actual);
341 1.1 kiyohara for (i = 0; i < actual; i++)
342 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
343 1.1 kiyohara offset + i + 1, buf[i]);
344 1.1 kiyohara return actual;
345 1.1 kiyohara }
346 1.1 kiyohara
347 1.1 kiyohara /*
348 1.1 kiyohara * send Ericsson baud rate command
349 1.1 kiyohara */
350 1.1 kiyohara static int
351 1.1 kiyohara btbc_set_baudrate(struct btbc_softc *sc, int baud)
352 1.1 kiyohara {
353 1.1 kiyohara hci_cmd_hdr_t *p;
354 1.1 kiyohara struct mbuf *m;
355 1.1 kiyohara const uint16_t opcode = htole16(HCI_CMD_ERICSSON_SET_UART_BAUD_RATE);
356 1.1 kiyohara uint8_t param;
357 1.1 kiyohara
358 1.1 kiyohara m = m_gethdr(M_WAIT, MT_DATA);
359 1.1 kiyohara
360 1.1 kiyohara switch (baud) {
361 1.1 kiyohara case 460800:
362 1.1 kiyohara param = 0x00;
363 1.1 kiyohara break;
364 1.1 kiyohara
365 1.1 kiyohara case 230400:
366 1.1 kiyohara param = 0x01;
367 1.1 kiyohara break;
368 1.1 kiyohara
369 1.1 kiyohara case 115200:
370 1.1 kiyohara param = 0x02;
371 1.1 kiyohara break;
372 1.1 kiyohara
373 1.1 kiyohara case 57600:
374 1.1 kiyohara default:
375 1.1 kiyohara param = 0x03;
376 1.1 kiyohara break;
377 1.1 kiyohara }
378 1.1 kiyohara
379 1.1 kiyohara p = mtod(m, hci_cmd_hdr_t *);
380 1.1 kiyohara p->type = HCI_CMD_PKT;
381 1.1 kiyohara p->opcode = opcode;
382 1.1 kiyohara p->length = sizeof(param);
383 1.1 kiyohara m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
384 1.1 kiyohara m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, ¶m);
385 1.1 kiyohara
386 1.11 plunky btbc_output_cmd(sc->sc_dev, m);
387 1.1 kiyohara return 0;
388 1.1 kiyohara }
389 1.1 kiyohara
390 1.1 kiyohara static void
391 1.1 kiyohara btbc_receive(struct btbc_softc *sc, uint32_t offset)
392 1.1 kiyohara {
393 1.1 kiyohara struct mbuf *m = sc->sc_rxp;
394 1.1 kiyohara int count, space = 0, i;
395 1.1 kiyohara uint8_t buf[31];
396 1.1 kiyohara
397 1.1 kiyohara btbc_enable_activity_led(sc);
398 1.1 kiyohara
399 1.1 kiyohara /*
400 1.1 kiyohara * If we already started a packet, find the
401 1.1 kiyohara * trailing end of it.
402 1.1 kiyohara */
403 1.1 kiyohara if (m) {
404 1.1 kiyohara while (m->m_next)
405 1.1 kiyohara m = m->m_next;
406 1.1 kiyohara
407 1.1 kiyohara space = M_TRAILINGSPACE(m);
408 1.1 kiyohara }
409 1.1 kiyohara
410 1.1 kiyohara count = btbc_read(sc, offset, buf, sizeof(buf));
411 1.1 kiyohara i = 0;
412 1.1 kiyohara
413 1.1 kiyohara while (i < count) {
414 1.1 kiyohara if (space == 0) {
415 1.1 kiyohara if (m == NULL) {
416 1.1 kiyohara /* new packet */
417 1.1 kiyohara MGETHDR(m, M_DONTWAIT, MT_DATA);
418 1.1 kiyohara if (m == NULL) {
419 1.9 plunky aprint_error_dev(sc->sc_dev,
420 1.9 plunky "out of memory\n");
421 1.11 plunky sc->sc_stats.err_rx++;
422 1.1 kiyohara return; /* (lost sync) */
423 1.1 kiyohara }
424 1.1 kiyohara
425 1.1 kiyohara sc->sc_rxp = m;
426 1.1 kiyohara m->m_pkthdr.len = m->m_len = 0;
427 1.1 kiyohara space = MHLEN;
428 1.1 kiyohara
429 1.1 kiyohara sc->sc_state = BTBC_RECV_PKT_TYPE;
430 1.1 kiyohara sc->sc_want = 1;
431 1.1 kiyohara } else {
432 1.1 kiyohara /* extend mbuf */
433 1.1 kiyohara MGET(m->m_next, M_DONTWAIT, MT_DATA);
434 1.1 kiyohara if (m->m_next == NULL) {
435 1.9 plunky aprint_error_dev(sc->sc_dev,
436 1.9 plunky "out of memory\n");
437 1.11 plunky sc->sc_stats.err_rx++;
438 1.1 kiyohara return; /* (lost sync) */
439 1.1 kiyohara }
440 1.1 kiyohara
441 1.1 kiyohara m = m->m_next;
442 1.1 kiyohara m->m_len = 0;
443 1.1 kiyohara space = MLEN;
444 1.1 kiyohara
445 1.1 kiyohara if (sc->sc_want > MINCLSIZE) {
446 1.1 kiyohara MCLGET(m, M_DONTWAIT);
447 1.1 kiyohara if (m->m_flags & M_EXT)
448 1.1 kiyohara space = MCLBYTES;
449 1.1 kiyohara }
450 1.1 kiyohara }
451 1.1 kiyohara }
452 1.1 kiyohara
453 1.1 kiyohara mtod(m, uint8_t *)[m->m_len++] = buf[i];
454 1.1 kiyohara space--;
455 1.1 kiyohara sc->sc_rxp->m_pkthdr.len++;
456 1.11 plunky sc->sc_stats.byte_rx++;
457 1.1 kiyohara
458 1.1 kiyohara sc->sc_want--;
459 1.1 kiyohara if (sc->sc_want > 0) {
460 1.1 kiyohara i++;
461 1.1 kiyohara continue; /* want more */
462 1.1 kiyohara }
463 1.1 kiyohara
464 1.1 kiyohara switch (sc->sc_state) {
465 1.1 kiyohara case BTBC_RECV_PKT_TYPE: /* Got packet type */
466 1.1 kiyohara switch (buf[i]) {
467 1.1 kiyohara case 0x00: /* init packet */
468 1.1 kiyohara m_freem(sc->sc_rxp);
469 1.1 kiyohara sc->sc_rxp = NULL;
470 1.1 kiyohara break;
471 1.1 kiyohara
472 1.1 kiyohara case HCI_ACL_DATA_PKT:
473 1.1 kiyohara sc->sc_state = BTBC_RECV_ACL_HDR;
474 1.1 kiyohara sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
475 1.1 kiyohara break;
476 1.1 kiyohara
477 1.1 kiyohara case HCI_SCO_DATA_PKT:
478 1.1 kiyohara sc->sc_state = BTBC_RECV_SCO_HDR;
479 1.1 kiyohara sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
480 1.1 kiyohara break;
481 1.1 kiyohara
482 1.1 kiyohara case HCI_EVENT_PKT:
483 1.1 kiyohara sc->sc_state = BTBC_RECV_EVENT_HDR;
484 1.1 kiyohara sc->sc_want = sizeof(hci_event_hdr_t) - 1;
485 1.1 kiyohara break;
486 1.1 kiyohara
487 1.1 kiyohara default:
488 1.9 plunky aprint_error_dev(sc->sc_dev,
489 1.9 plunky "Unknown packet type=%#x!\n", buf[i]);
490 1.11 plunky sc->sc_stats.err_rx++;
491 1.1 kiyohara m_freem(sc->sc_rxp);
492 1.1 kiyohara sc->sc_rxp = NULL;
493 1.1 kiyohara return; /* (lost sync) */
494 1.1 kiyohara }
495 1.1 kiyohara
496 1.1 kiyohara break;
497 1.1 kiyohara
498 1.1 kiyohara /*
499 1.1 kiyohara * we assume (correctly of course :) that the packet headers
500 1.1 kiyohara * all fit into a single pkthdr mbuf
501 1.1 kiyohara */
502 1.1 kiyohara case BTBC_RECV_ACL_HDR: /* Got ACL Header */
503 1.1 kiyohara sc->sc_state = BTBC_RECV_ACL_DATA;
504 1.1 kiyohara sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
505 1.1 kiyohara sc->sc_want = le16toh(sc->sc_want);
506 1.1 kiyohara break;
507 1.1 kiyohara
508 1.1 kiyohara case BTBC_RECV_SCO_HDR: /* Got SCO Header */
509 1.1 kiyohara sc->sc_state = BTBC_RECV_SCO_DATA;
510 1.1 kiyohara sc->sc_want = mtod(m, hci_scodata_hdr_t *)->length;
511 1.1 kiyohara break;
512 1.1 kiyohara
513 1.1 kiyohara case BTBC_RECV_EVENT_HDR: /* Got Event Header */
514 1.1 kiyohara sc->sc_state = BTBC_RECV_EVENT_DATA;
515 1.1 kiyohara sc->sc_want = mtod(m, hci_event_hdr_t *)->length;
516 1.1 kiyohara break;
517 1.1 kiyohara
518 1.1 kiyohara case BTBC_RECV_ACL_DATA: /* ACL Packet Complete */
519 1.11 plunky if (!hci_input_acl(sc->sc_unit, sc->sc_rxp))
520 1.11 plunky sc->sc_stats.err_rx++;
521 1.11 plunky
522 1.11 plunky sc->sc_stats.acl_rx++;
523 1.1 kiyohara sc->sc_rxp = m = NULL;
524 1.1 kiyohara space = 0;
525 1.1 kiyohara break;
526 1.1 kiyohara
527 1.1 kiyohara case BTBC_RECV_SCO_DATA: /* SCO Packet Complete */
528 1.11 plunky if (!hci_input_sco(sc->sc_unit, sc->sc_rxp))
529 1.11 plunky sc->sc_stats.err_rx++;
530 1.11 plunky
531 1.11 plunky sc->sc_stats.sco_rx++;
532 1.1 kiyohara sc->sc_rxp = m = NULL;
533 1.1 kiyohara space = 0;
534 1.1 kiyohara break;
535 1.1 kiyohara
536 1.1 kiyohara case BTBC_RECV_EVENT_DATA: /* Event Packet Complete */
537 1.11 plunky if (!hci_input_event(sc->sc_unit, sc->sc_rxp))
538 1.11 plunky sc->sc_stats.err_rx++;
539 1.11 plunky
540 1.11 plunky sc->sc_stats.evt_rx++;
541 1.1 kiyohara sc->sc_rxp = m = NULL;
542 1.1 kiyohara space = 0;
543 1.1 kiyohara break;
544 1.1 kiyohara
545 1.1 kiyohara default:
546 1.1 kiyohara panic("%s: invalid state %d!\n",
547 1.6 plunky device_xname(sc->sc_dev), sc->sc_state);
548 1.1 kiyohara }
549 1.1 kiyohara i++;
550 1.1 kiyohara }
551 1.1 kiyohara }
552 1.1 kiyohara
553 1.1 kiyohara /*
554 1.1 kiyohara * write data from current packet to Transmit FIFO.
555 1.1 kiyohara * restart when done.
556 1.1 kiyohara */
557 1.1 kiyohara static void
558 1.1 kiyohara btbc_transmit(struct btbc_softc *sc)
559 1.1 kiyohara {
560 1.1 kiyohara hci_cmd_hdr_t *p;
561 1.1 kiyohara struct mbuf *m;
562 1.4 kiyohara int count, set_baudrate, n, s;
563 1.1 kiyohara uint32_t offset, command;
564 1.1 kiyohara uint8_t *rptr;
565 1.1 kiyohara
566 1.1 kiyohara m = sc->sc_txp;
567 1.1 kiyohara if (m == NULL) {
568 1.11 plunky sc->sc_flags &= ~BTBC_XMIT;
569 1.11 plunky btbc_start(sc);
570 1.1 kiyohara return;
571 1.1 kiyohara }
572 1.1 kiyohara
573 1.1 kiyohara set_baudrate = 0;
574 1.1 kiyohara p = mtod(m, hci_cmd_hdr_t *);
575 1.1 kiyohara if ((void *)m->m_pktdat == (void *)p) {
576 1.1 kiyohara const uint16_t opcode =
577 1.1 kiyohara htole16(HCI_CMD_ERICSSON_SET_UART_BAUD_RATE);
578 1.1 kiyohara
579 1.1 kiyohara if (p->type == HCI_CMD_PKT &&
580 1.1 kiyohara p->opcode == opcode &&
581 1.1 kiyohara p->length == 1) {
582 1.1 kiyohara set_baudrate = 1;
583 1.1 kiyohara sc->sc_txp = NULL; /* safe reentrant */
584 1.1 kiyohara }
585 1.1 kiyohara }
586 1.1 kiyohara
587 1.1 kiyohara count = 0;
588 1.1 kiyohara rptr = mtod(m, uint8_t *);
589 1.1 kiyohara for(;;) {
590 1.4 kiyohara if (m->m_len == 0) {
591 1.1 kiyohara m = m->m_next;
592 1.1 kiyohara if (m == NULL) {
593 1.1 kiyohara m = sc->sc_txp;
594 1.1 kiyohara sc->sc_txp = NULL;
595 1.1 kiyohara
596 1.1 kiyohara if (M_GETCTX(m, void *) == NULL)
597 1.1 kiyohara m_freem(m);
598 1.11 plunky else if (!hci_complete_sco(sc->sc_unit, m))
599 1.11 plunky sc->sc_stats.err_tx++;
600 1.1 kiyohara
601 1.1 kiyohara break;
602 1.1 kiyohara }
603 1.1 kiyohara
604 1.1 kiyohara rptr = mtod(m, uint8_t *);
605 1.1 kiyohara continue;
606 1.1 kiyohara }
607 1.1 kiyohara
608 1.1 kiyohara s = splhigh();
609 1.1 kiyohara if (sc->sc_txstate & TXBUF_MASK) {
610 1.1 kiyohara if (sc->sc_txstate & TXBUF2_EMPTY) {
611 1.1 kiyohara offset = BLUECARD_BUF2;
612 1.1 kiyohara command = BLUECARD_COMMAND_TXBUF2;
613 1.1 kiyohara sc->sc_txstate &= ~(TXBUF2_EMPTY | TXBUF_MASK);
614 1.1 kiyohara } else {
615 1.1 kiyohara splx(s);
616 1.1 kiyohara break;
617 1.1 kiyohara }
618 1.1 kiyohara } else {
619 1.1 kiyohara if (sc->sc_txstate & TXBUF1_EMPTY) {
620 1.1 kiyohara offset = BLUECARD_BUF1;
621 1.1 kiyohara command = BLUECARD_COMMAND_TXBUF1;
622 1.1 kiyohara sc->sc_txstate &= ~TXBUF1_EMPTY;
623 1.1 kiyohara sc->sc_txstate |= TXBUF_MASK;
624 1.1 kiyohara } else {
625 1.1 kiyohara splx(s);
626 1.1 kiyohara break;
627 1.1 kiyohara }
628 1.1 kiyohara }
629 1.1 kiyohara splx(s);
630 1.1 kiyohara
631 1.1 kiyohara if (set_baudrate) {
632 1.1 kiyohara /* Disable RTS */
633 1.1 kiyohara sc->sc_ctrlreg |= BLUECARD_CONTROL_RTS;
634 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
635 1.1 kiyohara BLUECARD_CONTROL, sc->sc_ctrlreg);
636 1.1 kiyohara }
637 1.1 kiyohara
638 1.1 kiyohara /* Activate LED */
639 1.1 kiyohara btbc_enable_activity_led(sc);
640 1.1 kiyohara
641 1.1 kiyohara /* Send frame */
642 1.4 kiyohara n = btbc_write(sc, offset, rptr, m->m_len);
643 1.1 kiyohara count += n;
644 1.1 kiyohara rptr += n;
645 1.4 kiyohara m_adj(m, n);
646 1.1 kiyohara
647 1.1 kiyohara /* Tell the FPGA to send the data */
648 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
649 1.1 kiyohara BLUECARD_COMMAND, command);
650 1.1 kiyohara
651 1.1 kiyohara if (set_baudrate) {
652 1.1 kiyohara unsigned char baud_reg;
653 1.1 kiyohara
654 1.1 kiyohara switch (*(uint8_t *)(p + 1)) {
655 1.1 kiyohara case 0x00: /* baud rate 460800 */
656 1.1 kiyohara baud_reg = BLUECARD_CONTROL_BAUDRATE_460800;
657 1.1 kiyohara break;
658 1.1 kiyohara case 0x01: /* baud rate 230400 */
659 1.1 kiyohara baud_reg = BLUECARD_CONTROL_BAUDRATE_230400;
660 1.1 kiyohara break;
661 1.1 kiyohara case 0x02: /* baud rate 115200 */
662 1.1 kiyohara baud_reg = BLUECARD_CONTROL_BAUDRATE_115200;
663 1.1 kiyohara break;
664 1.1 kiyohara case 0x03: /* baud rate 57600 */
665 1.1 kiyohara default:
666 1.1 kiyohara baud_reg = BLUECARD_CONTROL_BAUDRATE_57600;
667 1.1 kiyohara break;
668 1.1 kiyohara }
669 1.1 kiyohara
670 1.1 kiyohara /* Wait until the command reaches the baseband */
671 1.1 kiyohara tsleep(sc, PCATCH, "btbc_wait", hz / 5);
672 1.1 kiyohara
673 1.1 kiyohara /* Set baud on baseband */
674 1.1 kiyohara sc->sc_ctrlreg &= ~BLUECARD_CONTROL_BAUDRATE_MASK;
675 1.1 kiyohara sc->sc_ctrlreg |= baud_reg;
676 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
677 1.1 kiyohara BLUECARD_CONTROL, sc->sc_ctrlreg);
678 1.1 kiyohara
679 1.1 kiyohara /* Enable RTS */
680 1.1 kiyohara sc->sc_ctrlreg &= ~BLUECARD_CONTROL_RTS;
681 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
682 1.1 kiyohara BLUECARD_CONTROL, sc->sc_ctrlreg);
683 1.1 kiyohara
684 1.1 kiyohara /* Wait before the next HCI packet can be send */
685 1.1 kiyohara tsleep(sc, PCATCH, "btbc_wait", hz);
686 1.1 kiyohara
687 1.1 kiyohara m_freem(m);
688 1.1 kiyohara break;
689 1.1 kiyohara }
690 1.1 kiyohara }
691 1.11 plunky sc->sc_stats.byte_tx += count;
692 1.1 kiyohara }
693 1.1 kiyohara
694 1.1 kiyohara static int
695 1.1 kiyohara btbc_intr(void *arg)
696 1.1 kiyohara {
697 1.1 kiyohara struct btbc_softc *sc = arg;
698 1.1 kiyohara int handled = 0;
699 1.1 kiyohara uint8_t isr;
700 1.1 kiyohara
701 1.1 kiyohara isr = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
702 1.1 kiyohara BLUECARD_INTERRUPT);
703 1.1 kiyohara if (isr != 0x00 && isr != 0xff) {
704 1.1 kiyohara if (isr & BLUECARD_INTERRUPT_RXBUF1) {
705 1.1 kiyohara isr &= ~BLUECARD_INTERRUPT_RXBUF1;
706 1.1 kiyohara handled = 1;
707 1.1 kiyohara btbc_receive(sc, BLUECARD_BUF1);
708 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
709 1.1 kiyohara BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_RXBUF1);
710 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
711 1.1 kiyohara BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF1);
712 1.1 kiyohara }
713 1.1 kiyohara if (isr & BLUECARD_INTERRUPT_RXBUF2) {
714 1.1 kiyohara isr &= ~BLUECARD_INTERRUPT_RXBUF2;
715 1.1 kiyohara handled = 1;
716 1.1 kiyohara btbc_receive(sc, BLUECARD_BUF2);
717 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
718 1.1 kiyohara BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_RXBUF2);
719 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
720 1.1 kiyohara BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF2);
721 1.1 kiyohara }
722 1.1 kiyohara if (isr & BLUECARD_INTERRUPT_TXBUF1) {
723 1.1 kiyohara isr &= ~BLUECARD_INTERRUPT_TXBUF1;
724 1.1 kiyohara handled = 1;
725 1.1 kiyohara sc->sc_txstate |= TXBUF1_EMPTY;
726 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
727 1.1 kiyohara BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_TXBUF1);
728 1.1 kiyohara btbc_transmit(sc);
729 1.1 kiyohara }
730 1.1 kiyohara if (isr & BLUECARD_INTERRUPT_TXBUF2) {
731 1.1 kiyohara isr &= ~BLUECARD_INTERRUPT_TXBUF2;
732 1.1 kiyohara handled = 1;
733 1.1 kiyohara sc->sc_txstate |= TXBUF2_EMPTY;
734 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
735 1.1 kiyohara BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_TXBUF2);
736 1.1 kiyohara btbc_transmit(sc);
737 1.1 kiyohara }
738 1.1 kiyohara
739 1.1 kiyohara if (isr & 0x40) { /* card eject ? */
740 1.9 plunky aprint_normal_dev(sc->sc_dev, "card eject?\n");
741 1.1 kiyohara isr &= ~0x40;
742 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
743 1.1 kiyohara BLUECARD_INTERRUPT, 0x40);
744 1.1 kiyohara }
745 1.1 kiyohara if (isr != 0x00) {
746 1.9 plunky aprint_error_dev(sc->sc_dev,
747 1.9 plunky "unknown interrupt: isr=0x%x\n", isr);
748 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
749 1.1 kiyohara BLUECARD_INTERRUPT, isr);
750 1.1 kiyohara }
751 1.1 kiyohara }
752 1.1 kiyohara
753 1.1 kiyohara return handled;
754 1.1 kiyohara }
755 1.1 kiyohara
756 1.1 kiyohara /*
757 1.1 kiyohara * start sending on btbc
758 1.11 plunky *
759 1.11 plunky * should be called at spltty() and when BTBC_XMIT is not set
760 1.1 kiyohara */
761 1.1 kiyohara static void
762 1.11 plunky btbc_start(struct btbc_softc *sc)
763 1.1 kiyohara {
764 1.1 kiyohara struct mbuf *m;
765 1.1 kiyohara
766 1.11 plunky KASSERT((sc->sc_flags & BTBC_XMIT) == 0);
767 1.1 kiyohara KASSERT(sc->sc_txp == NULL);
768 1.1 kiyohara
769 1.11 plunky if (MBUFQ_FIRST(&sc->sc_cmdq)) {
770 1.11 plunky MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
771 1.11 plunky sc->sc_stats.cmd_tx++;
772 1.1 kiyohara goto start;
773 1.1 kiyohara }
774 1.1 kiyohara
775 1.11 plunky if (MBUFQ_FIRST(&sc->sc_scoq)) {
776 1.11 plunky MBUFQ_DEQUEUE(&sc->sc_scoq, m);
777 1.11 plunky sc->sc_stats.sco_tx++;
778 1.1 kiyohara goto start;
779 1.1 kiyohara }
780 1.1 kiyohara
781 1.11 plunky if (MBUFQ_FIRST(&sc->sc_aclq)) {
782 1.11 plunky MBUFQ_DEQUEUE(&sc->sc_aclq, m);
783 1.11 plunky sc->sc_stats.acl_tx++;
784 1.1 kiyohara goto start;
785 1.1 kiyohara }
786 1.1 kiyohara
787 1.1 kiyohara /* Nothing to send */
788 1.1 kiyohara return;
789 1.1 kiyohara
790 1.1 kiyohara start:
791 1.1 kiyohara sc->sc_txp = m;
792 1.11 plunky sc->sc_flags |= BTBC_XMIT;
793 1.1 kiyohara btbc_transmit(sc);
794 1.1 kiyohara }
795 1.1 kiyohara
796 1.1 kiyohara static int
797 1.8 plunky btbc_enable(device_t self)
798 1.1 kiyohara {
799 1.8 plunky struct btbc_softc *sc = device_private(self);
800 1.11 plunky int err, s;
801 1.1 kiyohara uint8_t id, ctrl;
802 1.1 kiyohara
803 1.11 plunky if (sc->sc_flags & BTBC_ENABLED)
804 1.1 kiyohara return 0;
805 1.1 kiyohara
806 1.11 plunky s = spltty();
807 1.11 plunky
808 1.1 kiyohara sc->sc_txstate = TXBUF1_EMPTY | TXBUF2_EMPTY;
809 1.1 kiyohara sc->sc_intr = pcmcia_intr_establish(sc->sc_pf, IPL_TTY, btbc_intr, sc);
810 1.1 kiyohara if (sc->sc_intr == NULL) {
811 1.1 kiyohara err = EIO;
812 1.1 kiyohara goto fail1;
813 1.1 kiyohara }
814 1.1 kiyohara
815 1.1 kiyohara err = pcmcia_function_enable(sc->sc_pf);
816 1.1 kiyohara if (err)
817 1.1 kiyohara goto fail2;
818 1.1 kiyohara
819 1.11 plunky sc->sc_flags |= BTBC_ENABLED;
820 1.11 plunky sc->sc_flags &= ~BTBC_XMIT;
821 1.1 kiyohara
822 1.1 kiyohara /* Reset card */
823 1.1 kiyohara ctrl = BLUECARD_CONTROL_RESET | BLUECARD_CONTROL_CARDRESET;
824 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
825 1.1 kiyohara ctrl);
826 1.1 kiyohara
827 1.1 kiyohara /* Turn FPGA off */
828 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
829 1.1 kiyohara BLUECARD_CARDRESET, 0x80);
830 1.1 kiyohara
831 1.1 kiyohara /* Wait some time */
832 1.1 kiyohara tsleep(sc, PCATCH, "btbc_reset", 1);
833 1.1 kiyohara
834 1.1 kiyohara /* Turn FPGA on */
835 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
836 1.1 kiyohara BLUECARD_CARDRESET, 0x00);
837 1.1 kiyohara
838 1.1 kiyohara /* Activate card */
839 1.1 kiyohara ctrl = BLUECARD_CONTROL_ON | BLUECARD_CONTROL_RESPU;
840 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
841 1.1 kiyohara ctrl);
842 1.1 kiyohara
843 1.1 kiyohara tsleep(sc, PCATCH, "btbc_enable", 1);
844 1.1 kiyohara sc->sc_ctrlreg = ctrl;
845 1.1 kiyohara
846 1.1 kiyohara /* Enable interrupt */
847 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
848 1.1 kiyohara BLUECARD_INTERRUPT, 0xff);
849 1.1 kiyohara sc->sc_ctrlreg |= BLUECARD_CONTROL_INTERRUPT;
850 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
851 1.1 kiyohara sc->sc_ctrlreg);
852 1.1 kiyohara
853 1.1 kiyohara id = bus_space_read_1(sc->sc_pcioh.iot,
854 1.1 kiyohara sc->sc_pcioh.ioh, BLUECARD_LEDCONTROL);
855 1.1 kiyohara switch (id & 0x0f) {
856 1.1 kiyohara case 0x02:
857 1.1 kiyohara /* Enable LED */
858 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
859 1.1 kiyohara BLUECARD_LEDCONTROL, 0x08 | 0x20);
860 1.1 kiyohara break;
861 1.1 kiyohara
862 1.1 kiyohara case 0x03:
863 1.1 kiyohara /* Disable RTS */
864 1.1 kiyohara ctrl |= BLUECARD_CONTROL_RTS;
865 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
866 1.1 kiyohara BLUECARD_CONTROL, ctrl);
867 1.1 kiyohara
868 1.1 kiyohara /* Set baud rate */
869 1.1 kiyohara ctrl |= BLUECARD_CONTROL_BAUDRATE_460800;
870 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
871 1.1 kiyohara BLUECARD_CONTROL, ctrl);
872 1.1 kiyohara
873 1.1 kiyohara /* Enable RTS */
874 1.1 kiyohara ctrl &= ~BLUECARD_CONTROL_RTS;
875 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
876 1.1 kiyohara BLUECARD_CONTROL, ctrl);
877 1.1 kiyohara break;
878 1.1 kiyohara }
879 1.1 kiyohara
880 1.1 kiyohara /* Start the RX buffers */
881 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
882 1.1 kiyohara BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF1);
883 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
884 1.1 kiyohara BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF2);
885 1.1 kiyohara
886 1.1 kiyohara /* XXX: Control the point at which RTS is enabled */
887 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
888 1.1 kiyohara BLUECARD_RXCONTROL, BLUECARD_RXCONTROL_RTSLEVEL(0x0f) | 1);
889 1.1 kiyohara
890 1.1 kiyohara /* Timeout before it is safe to send the first HCI packet */
891 1.1 kiyohara tsleep(sc, PCATCH, "btbc_enable", hz * 2);
892 1.1 kiyohara
893 1.1 kiyohara btbc_set_baudrate(sc, BTBC_DEFAULT_BAUDRATE);
894 1.1 kiyohara
895 1.11 plunky splx(s);
896 1.1 kiyohara return 0;
897 1.1 kiyohara
898 1.1 kiyohara fail2:
899 1.1 kiyohara pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
900 1.1 kiyohara sc->sc_intr = NULL;
901 1.1 kiyohara fail1:
902 1.11 plunky splx(s);
903 1.1 kiyohara return err;
904 1.1 kiyohara }
905 1.1 kiyohara
906 1.1 kiyohara static void
907 1.8 plunky btbc_disable(device_t self)
908 1.1 kiyohara {
909 1.8 plunky struct btbc_softc *sc = device_private(self);
910 1.11 plunky int s;
911 1.1 kiyohara
912 1.11 plunky if ((sc->sc_flags & BTBC_ENABLED) == 0)
913 1.1 kiyohara return;
914 1.1 kiyohara
915 1.11 plunky s = spltty();
916 1.11 plunky
917 1.1 kiyohara pcmcia_function_disable(sc->sc_pf);
918 1.1 kiyohara
919 1.1 kiyohara if (sc->sc_intr) {
920 1.1 kiyohara pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
921 1.1 kiyohara sc->sc_intr = NULL;
922 1.1 kiyohara }
923 1.1 kiyohara
924 1.1 kiyohara if (sc->sc_rxp) {
925 1.1 kiyohara m_freem(sc->sc_rxp);
926 1.1 kiyohara sc->sc_rxp = NULL;
927 1.1 kiyohara }
928 1.1 kiyohara
929 1.1 kiyohara if (sc->sc_txp) {
930 1.1 kiyohara m_freem(sc->sc_txp);
931 1.1 kiyohara sc->sc_txp = NULL;
932 1.1 kiyohara }
933 1.1 kiyohara
934 1.11 plunky MBUFQ_DRAIN(&sc->sc_cmdq);
935 1.11 plunky MBUFQ_DRAIN(&sc->sc_aclq);
936 1.11 plunky MBUFQ_DRAIN(&sc->sc_scoq);
937 1.11 plunky
938 1.11 plunky sc->sc_flags &= ~BTBC_ENABLED;
939 1.1 kiyohara
940 1.1 kiyohara /* Disable LED */
941 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
942 1.1 kiyohara BLUECARD_LEDCONTROL, 0x00);
943 1.1 kiyohara
944 1.1 kiyohara /* Reset card */
945 1.1 kiyohara sc->sc_ctrlreg = BLUECARD_CONTROL_RESET | BLUECARD_CONTROL_CARDRESET;
946 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
947 1.1 kiyohara sc->sc_ctrlreg);
948 1.1 kiyohara
949 1.1 kiyohara /* Turn FPGA off */
950 1.1 kiyohara bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
951 1.1 kiyohara BLUECARD_CARDRESET, 0x80);
952 1.11 plunky
953 1.11 plunky splx(s);
954 1.11 plunky }
955 1.11 plunky
956 1.11 plunky static void
957 1.11 plunky btbc_output_cmd(device_t self, struct mbuf *m)
958 1.11 plunky {
959 1.11 plunky struct btbc_softc *sc = device_private(self);
960 1.11 plunky int s;
961 1.11 plunky
962 1.11 plunky KASSERT(sc->sc_flags & BTBC_ENABLED);
963 1.11 plunky
964 1.11 plunky M_SETCTX(m, NULL);
965 1.11 plunky
966 1.11 plunky s = spltty();
967 1.11 plunky MBUFQ_ENQUEUE(&sc->sc_cmdq, m);
968 1.11 plunky if ((sc->sc_flags & BTBC_XMIT) == 0)
969 1.11 plunky btbc_start(sc);
970 1.11 plunky
971 1.11 plunky splx(s);
972 1.11 plunky }
973 1.11 plunky
974 1.11 plunky static void
975 1.11 plunky btbc_output_acl(device_t self, struct mbuf *m)
976 1.11 plunky {
977 1.11 plunky struct btbc_softc *sc = device_private(self);
978 1.11 plunky int s;
979 1.11 plunky
980 1.11 plunky KASSERT(sc->sc_flags & BTBC_ENABLED);
981 1.11 plunky
982 1.11 plunky M_SETCTX(m, NULL);
983 1.11 plunky
984 1.11 plunky s = spltty();
985 1.11 plunky MBUFQ_ENQUEUE(&sc->sc_aclq, m);
986 1.11 plunky if ((sc->sc_flags & BTBC_XMIT) == 0)
987 1.11 plunky btbc_start(sc);
988 1.11 plunky
989 1.11 plunky splx(s);
990 1.11 plunky }
991 1.11 plunky
992 1.11 plunky static void
993 1.11 plunky btbc_output_sco(device_t self, struct mbuf *m)
994 1.11 plunky {
995 1.11 plunky struct btbc_softc *sc = device_private(self);
996 1.11 plunky int s;
997 1.11 plunky
998 1.11 plunky KASSERT(sc->sc_flags & BTBC_ENABLED);
999 1.11 plunky
1000 1.11 plunky s = spltty();
1001 1.11 plunky MBUFQ_ENQUEUE(&sc->sc_scoq, m);
1002 1.11 plunky if ((sc->sc_flags & BTBC_XMIT) == 0)
1003 1.11 plunky btbc_start(sc);
1004 1.11 plunky
1005 1.11 plunky splx(s);
1006 1.11 plunky }
1007 1.11 plunky
1008 1.11 plunky static void
1009 1.11 plunky btbc_stats(device_t self, struct bt_stats *dest, int flush)
1010 1.11 plunky {
1011 1.11 plunky struct btbc_softc *sc = device_private(self);
1012 1.11 plunky int s;
1013 1.11 plunky
1014 1.11 plunky s = spltty();
1015 1.11 plunky memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
1016 1.11 plunky
1017 1.11 plunky if (flush)
1018 1.11 plunky memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
1019 1.11 plunky
1020 1.11 plunky splx(s);
1021 1.1 kiyohara }
1022