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