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