6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_bt3c_pccard.c,v 1.5 2003/04/01 18:15:21 max Exp $
33 * XXX XXX XX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
35 * Based on information obrained from: Jose Orlando Pereira <jop@di.uminho.pt>
36 * and disassembled w2k driver.
38 * XXX XXX XX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <machine/bus.h>
49 #include <sys/endian.h>
50 #include <sys/interrupt.h>
51 #include <sys/kernel.h>
53 #include <sys/module.h>
55 #include <machine/resource.h>
58 #include <sys/socket.h>
60 #include <net/if_var.h>
62 #include <dev/pccard/pccardreg.h>
63 #include <dev/pccard/pccardvar.h>
64 #include "pccarddevs.h"
66 #include <netgraph/ng_message.h>
67 #include <netgraph/netgraph.h>
68 #include <netgraph/ng_parse.h>
69 #include <netgraph/bluetooth/include/ng_bluetooth.h>
70 #include <netgraph/bluetooth/include/ng_hci.h>
71 #include <netgraph/bluetooth/include/ng_bt3c.h>
72 #include <netgraph/bluetooth/drivers/bt3c/ng_bt3c_var.h>
74 /* Netgraph methods */
75 static ng_constructor_t ng_bt3c_constructor;
76 static ng_shutdown_t ng_bt3c_shutdown;
77 static ng_newhook_t ng_bt3c_newhook;
78 static ng_connect_t ng_bt3c_connect;
79 static ng_disconnect_t ng_bt3c_disconnect;
80 static ng_rcvmsg_t ng_bt3c_rcvmsg;
81 static ng_rcvdata_t ng_bt3c_rcvdata;
83 /* PCMCIA driver methods */
84 static int bt3c_pccard_probe (device_t);
85 static int bt3c_pccard_attach (device_t);
86 static int bt3c_pccard_detach (device_t);
88 static void bt3c_intr (void *);
89 static void bt3c_receive (bt3c_softc_p);
91 static void bt3c_swi_intr (void *);
92 static void bt3c_forward (node_p, hook_p, void *, int);
93 static void bt3c_send (node_p, hook_p, void *, int);
95 static void bt3c_download_firmware (bt3c_softc_p, char const *, int);
97 #define bt3c_set_address(sc, address) \
99 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_ADDR_L, ((address) & 0xff)); \
100 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_ADDR_H, (((address) >> 8) & 0xff)); \
103 #define bt3c_read_data(sc, data) \
105 (data) = bus_space_read_1((sc)->iot, (sc)->ioh, BT3C_DATA_L); \
106 (data) |= ((bus_space_read_1((sc)->iot, (sc)->ioh, BT3C_DATA_H) & 0xff) << 8); \
109 #define bt3c_write_data(sc, data) \
111 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_DATA_L, ((data) & 0xff)); \
112 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_DATA_H, (((data) >> 8) & 0xff)); \
115 #define bt3c_read_control(sc, data) \
117 (data) = bus_space_read_1((sc)->iot, (sc)->ioh, BT3C_CONTROL); \
120 #define bt3c_write_control(sc, data) \
122 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_CONTROL, (data)); \
125 #define bt3c_read(sc, address, data) \
127 bt3c_set_address((sc), (address)); \
128 bt3c_read_data((sc), (data)); \
131 #define bt3c_write(sc, address, data) \
133 bt3c_set_address((sc), (address)); \
134 bt3c_write_data((sc), (data)); \
137 static MALLOC_DEFINE(M_BT3C, "bt3c", "bt3c data structures");
139 /****************************************************************************
140 ****************************************************************************
142 ****************************************************************************
143 ****************************************************************************/
150 static const struct ng_parse_struct_field ng_bt3c_node_qlen_type_fields[] =
152 { "queue", &ng_parse_int32_type, },
153 { "qlen", &ng_parse_int32_type, },
156 static const struct ng_parse_type ng_bt3c_node_qlen_type = {
157 &ng_parse_struct_type,
158 &ng_bt3c_node_qlen_type_fields
162 static const struct ng_parse_struct_field ng_bt3c_node_stat_type_fields[] =
164 { "pckts_recv", &ng_parse_uint32_type, },
165 { "bytes_recv", &ng_parse_uint32_type, },
166 { "pckts_sent", &ng_parse_uint32_type, },
167 { "bytes_sent", &ng_parse_uint32_type, },
168 { "oerrors", &ng_parse_uint32_type, },
169 { "ierrors", &ng_parse_uint32_type, },
172 static const struct ng_parse_type ng_bt3c_node_stat_type = {
173 &ng_parse_struct_type,
174 &ng_bt3c_node_stat_type_fields
177 static const struct ng_cmdlist ng_bt3c_cmdlist[] = {
180 NGM_BT3C_NODE_GET_STATE,
183 &ng_parse_uint16_type
187 NGM_BT3C_NODE_SET_DEBUG,
189 &ng_parse_uint16_type,
194 NGM_BT3C_NODE_GET_DEBUG,
197 &ng_parse_uint16_type
201 NGM_BT3C_NODE_GET_QLEN,
204 &ng_bt3c_node_qlen_type
208 NGM_BT3C_NODE_SET_QLEN,
210 &ng_bt3c_node_qlen_type,
215 NGM_BT3C_NODE_GET_STAT,
218 &ng_bt3c_node_stat_type
222 NGM_BT3C_NODE_RESET_STAT,
230 static struct ng_type typestruct = {
231 .version = NG_ABI_VERSION,
232 .name = NG_BT3C_NODE_TYPE,
233 .constructor = ng_bt3c_constructor,
234 .rcvmsg = ng_bt3c_rcvmsg,
235 .shutdown = ng_bt3c_shutdown,
236 .newhook = ng_bt3c_newhook,
237 .connect = ng_bt3c_connect,
238 .rcvdata = ng_bt3c_rcvdata,
239 .disconnect = ng_bt3c_disconnect,
240 .cmdlist = ng_bt3c_cmdlist
244 * Netgraph node constructor. Do not allow to create node of this type.
248 ng_bt3c_constructor(node_p node)
251 } /* ng_bt3c_constructor */
254 * Netgraph node destructor. Destroy node only when device has been detached
258 ng_bt3c_shutdown(node_p node)
260 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
262 /* Let old node go */
263 NG_NODE_SET_PRIVATE(node, NULL);
266 /* Create new fresh one if we are not going down */
270 /* Create new Netgraph node */
271 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
272 device_printf(sc->dev, "Could not create Netgraph node\n");
277 /* Name new Netgraph node */
278 if (ng_name_node(sc->node, device_get_nameunit(sc->dev)) != 0) {
279 device_printf(sc->dev, "Could not name Netgraph node\n");
280 NG_NODE_UNREF(sc->node);
285 NG_NODE_SET_PRIVATE(sc->node, sc);
288 } /* ng_bt3c_shutdown */
291 * Create new hook. There can only be one.
295 ng_bt3c_newhook(node_p node, hook_p hook, char const *name)
297 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
299 if (strcmp(name, NG_BT3C_HOOK) != 0)
302 if (sc->hook != NULL)
308 } /* ng_bt3c_newhook */
311 * Connect hook. Say YEP, that's OK with me.
315 ng_bt3c_connect(hook_p hook)
317 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
319 if (hook != sc->hook) {
324 /* set the hook into queueing mode (for incoming (from wire) packets) */
325 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
328 } /* ng_bt3c_connect */
335 ng_bt3c_disconnect(hook_p hook)
337 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
340 * We need to check for sc != NULL because we can be called from
341 * bt3c_pccard_detach() via ng_rmnode_self()
345 if (hook != sc->hook)
355 } /* ng_bt3c_disconnect */
358 * Process control message
362 ng_bt3c_rcvmsg(node_p node, item_p item, hook_p lasthook)
364 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
365 struct ng_mesg *msg = NULL, *rsp = NULL;
373 NGI_GET_MSG(item, msg);
375 switch (msg->header.typecookie) {
376 case NGM_GENERIC_COOKIE:
377 switch (msg->header.cmd) {
378 case NGM_TEXT_STATUS:
379 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
383 snprintf(rsp->data, NG_TEXTRESPONSE,
388 "IncmQ: [len:%d,max:%d]\n" \
389 "OutgQ: [len:%d,max:%d]\n",
390 (sc->hook != NULL)? NG_BT3C_HOOK : "",
394 _IF_QLEN(&sc->inq), /* XXX */
395 sc->inq.ifq_maxlen, /* XXX */
396 _IF_QLEN(&sc->outq), /* XXX */
397 sc->outq.ifq_maxlen /* XXX */
407 case NGM_BT3C_COOKIE:
408 switch (msg->header.cmd) {
409 case NGM_BT3C_NODE_GET_STATE:
410 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_state_ep),
415 *((ng_bt3c_node_state_ep *)(rsp->data)) =
419 case NGM_BT3C_NODE_SET_DEBUG:
420 if (msg->header.arglen != sizeof(ng_bt3c_node_debug_ep))
424 *((ng_bt3c_node_debug_ep *)(msg->data));
427 case NGM_BT3C_NODE_GET_DEBUG:
428 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_debug_ep),
433 *((ng_bt3c_node_debug_ep *)(rsp->data)) =
437 case NGM_BT3C_NODE_GET_QLEN:
438 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_qlen_ep),
445 switch (((ng_bt3c_node_qlen_ep *)(msg->data))->queue) {
446 case NGM_BT3C_NODE_IN_QUEUE:
447 ((ng_bt3c_node_qlen_ep *)(rsp->data))->queue =
448 NGM_BT3C_NODE_IN_QUEUE;
449 ((ng_bt3c_node_qlen_ep *)(rsp->data))->qlen =
453 case NGM_BT3C_NODE_OUT_QUEUE:
454 ((ng_bt3c_node_qlen_ep *)(rsp->data))->queue =
455 NGM_BT3C_NODE_OUT_QUEUE;
456 ((ng_bt3c_node_qlen_ep *)(rsp->data))->qlen =
467 case NGM_BT3C_NODE_SET_QLEN:
468 if (msg->header.arglen != sizeof(ng_bt3c_node_qlen_ep)){
473 if (((ng_bt3c_node_qlen_ep *)(msg->data))->qlen <= 0) {
478 switch (((ng_bt3c_node_qlen_ep *)(msg->data))->queue) {
479 case NGM_BT3C_NODE_IN_QUEUE:
480 sc->inq.ifq_maxlen = ((ng_bt3c_node_qlen_ep *)
481 (msg->data))->qlen; /* XXX */
484 case NGM_BT3C_NODE_OUT_QUEUE:
485 sc->outq.ifq_maxlen = ((ng_bt3c_node_qlen_ep *)
486 (msg->data))->qlen; /* XXX */
495 case NGM_BT3C_NODE_GET_STAT:
496 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_stat_ep),
501 bcopy(&sc->stat, rsp->data,
502 sizeof(ng_bt3c_node_stat_ep));
505 case NGM_BT3C_NODE_RESET_STAT:
506 NG_BT3C_STAT_RESET(sc->stat);
509 case NGM_BT3C_NODE_DOWNLOAD_FIRMWARE:
510 if (msg->header.arglen <
511 sizeof(ng_bt3c_firmware_block_ep))
514 bt3c_download_firmware(sc, msg->data,
529 NG_RESPOND_MSG(error, node, item, rsp);
533 } /* ng_bt3c_rcvmsg */
540 ng_bt3c_rcvdata(hook_p hook, item_p item)
542 bt3c_softc_p sc = (bt3c_softc_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
543 struct mbuf *m = NULL;
551 if (hook != sc->hook) {
559 if (_IF_QFULL(&sc->outq)) {
561 "Outgoing queue is full. Dropping mbuf, len=%d\n", m->m_pkthdr.len);
563 NG_BT3C_STAT_OERROR(sc->stat);
567 _IF_ENQUEUE(&sc->outq, m);
568 IF_UNLOCK(&sc->outq);
570 error = ng_send_fn(sc->node, NULL, bt3c_send, NULL, 0 /* new send */);
575 } /* ng_bt3c_rcvdata */
577 /****************************************************************************
578 ****************************************************************************
579 ** PCMCIA driver specific
580 ****************************************************************************
581 ****************************************************************************/
584 * PC Card (PCMCIA) probe routine
587 static struct pccard_product const bt3c_pccard_products[] = {
588 PCMCIA_CARD(3COM, 3CRWB609),
593 bt3c_pccard_probe(device_t dev)
595 struct pccard_product const *pp = NULL;
597 pp = pccard_product_lookup(dev, bt3c_pccard_products,
598 sizeof(bt3c_pccard_products[0]), NULL);
602 device_set_desc(dev, pp->pp_name);
605 } /* bt3c_pccard_probe */
608 * PC Card (PCMCIA) attach routine
612 bt3c_pccard_attach(device_t dev)
614 bt3c_softc_p sc = (bt3c_softc_p) device_get_softc(dev);
616 /* Allocate I/O ports */
618 sc->iobase = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
619 &sc->iobase_rid, 8, RF_ACTIVE);
620 if (sc->iobase == NULL) {
621 device_printf(dev, "Could not allocate I/O ports\n");
624 sc->iot = rman_get_bustag(sc->iobase);
625 sc->ioh = rman_get_bushandle(sc->iobase);
629 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
631 if (sc->irq == NULL) {
632 device_printf(dev, "Could not allocate IRQ\n");
636 sc->irq_cookie = NULL;
637 if (bus_setup_intr(dev, sc->irq, INTR_TYPE_TTY, NULL, bt3c_intr, sc,
638 &sc->irq_cookie) != 0) {
639 device_printf(dev, "Could not setup ISR\n");
643 /* Attach handler to TTY SWI thread */
645 if (swi_add(&tty_intr_event, device_get_nameunit(dev),
646 bt3c_swi_intr, sc, SWI_TTY, 0, &sc->ith) < 0) {
647 device_printf(dev, "Could not setup SWI ISR\n");
651 /* Create Netgraph node */
652 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
653 device_printf(dev, "Could not create Netgraph node\n");
658 /* Name Netgraph node */
659 if (ng_name_node(sc->node, device_get_nameunit(dev)) != 0) {
660 device_printf(dev, "Could not name Netgraph node\n");
661 NG_NODE_UNREF(sc->node);
667 sc->debug = NG_BT3C_WARN_LEVEL;
669 sc->inq.ifq_maxlen = sc->outq.ifq_maxlen = BT3C_DEFAULTQLEN;
670 mtx_init(&sc->inq.ifq_mtx, "BT3C inq", NULL, MTX_DEF);
671 mtx_init(&sc->outq.ifq_mtx, "BT3C outq", NULL, MTX_DEF);
673 sc->state = NG_BT3C_W4_PKT_IND;
676 NG_NODE_SET_PRIVATE(sc->node, sc);
680 if (sc->ith != NULL) {
685 if (sc->irq != NULL) {
686 if (sc->irq_cookie != NULL)
687 bus_teardown_intr(dev, sc->irq, sc->irq_cookie);
689 bus_release_resource(dev, SYS_RES_IRQ,
690 sc->irq_rid, sc->irq);
696 if (sc->iobase != NULL) {
697 bus_release_resource(dev, SYS_RES_IOPORT,
698 sc->iobase_rid, sc->iobase);
705 } /* bt3c_pccacd_attach */
708 * PC Card (PCMCIA) detach routine
712 bt3c_pccard_detach(device_t dev)
714 bt3c_softc_p sc = (bt3c_softc_p) device_get_softc(dev);
722 bus_teardown_intr(dev, sc->irq, sc->irq_cookie);
723 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
724 sc->irq_cookie = NULL;
728 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
732 if (sc->node != NULL) {
733 NG_NODE_SET_PRIVATE(sc->node, NULL);
734 ng_rmnode_self(sc->node);
742 mtx_destroy(&sc->inq.ifq_mtx);
743 mtx_destroy(&sc->outq.ifq_mtx);
746 } /* bt3c_pccacd_detach */
749 * Interrupt service routine's
753 bt3c_intr(void *context)
755 bt3c_softc_p sc = (bt3c_softc_p) context;
756 u_int16_t control, status;
758 if (sc == NULL || sc->ith == NULL) {
759 printf("%s: bogus interrupt\n", NG_BT3C_NODE_TYPE);
763 bt3c_read_control(sc, control);
764 if ((control & 0x80) == 0)
767 bt3c_read(sc, 0x7001, status);
768 NG_BT3C_INFO(sc->dev, "control=%#x, status=%#x\n", control, status);
770 if ((status & 0xff) == 0x7f || (status & 0xff) == 0xff) {
771 NG_BT3C_WARN(sc->dev, "Strange status=%#x\n", status);
775 /* Receive complete */
779 /* Record status and schedule SWI */
780 sc->status |= status;
781 swi_sched(sc->ith, 0);
783 /* Complete interrupt */
784 bt3c_write(sc, 0x7001, 0x0000);
785 bt3c_write_control(sc, control);
793 bt3c_receive(bt3c_softc_p sc)
795 u_int16_t i, count, c;
797 /* Receive data from the card */
798 bt3c_read(sc, 0x7006, count);
799 NG_BT3C_INFO(sc->dev, "The card has %d characters\n", count);
801 bt3c_set_address(sc, 0x7480);
803 for (i = 0; i < count; i++) {
804 /* Allocate new mbuf if needed */
806 sc->state = NG_BT3C_W4_PKT_IND;
809 MGETHDR(sc->m, M_NOWAIT, MT_DATA);
811 NG_BT3C_ERR(sc->dev, "Could not get mbuf\n");
812 NG_BT3C_STAT_IERROR(sc->stat);
814 break; /* XXX lost of sync */
817 if (!(MCLGET(sc->m, M_NOWAIT))) {
820 NG_BT3C_ERR(sc->dev, "Could not get cluster\n");
821 NG_BT3C_STAT_IERROR(sc->stat);
823 break; /* XXX lost of sync */
826 sc->m->m_len = sc->m->m_pkthdr.len = 0;
829 /* Read and append character to mbuf */
830 bt3c_read_data(sc, c);
831 if (sc->m->m_pkthdr.len >= MCLBYTES) {
832 NG_BT3C_ERR(sc->dev, "Oversized frame\n");
835 sc->state = NG_BT3C_W4_PKT_IND;
838 break; /* XXX lost of sync */
841 mtod(sc->m, u_int8_t *)[sc->m->m_len ++] = (u_int8_t) c;
842 sc->m->m_pkthdr.len ++;
844 NG_BT3C_INFO(sc->dev,
845 "Got char %#x, want=%d, got=%d\n", c, sc->want, sc->m->m_pkthdr.len);
847 if (sc->m->m_pkthdr.len < sc->want)
848 continue; /* wait for more */
851 /* Got packet indicator */
852 case NG_BT3C_W4_PKT_IND:
853 NG_BT3C_INFO(sc->dev,
854 "Got packet indicator %#x\n", *mtod(sc->m, u_int8_t *));
856 sc->state = NG_BT3C_W4_PKT_HDR;
859 * Since packet indicator included in the packet
860 * header just set sc->want to sizeof(packet header).
863 switch (*mtod(sc->m, u_int8_t *)) {
864 case NG_HCI_ACL_DATA_PKT:
865 sc->want = sizeof(ng_hci_acldata_pkt_t);
868 case NG_HCI_SCO_DATA_PKT:
869 sc->want = sizeof(ng_hci_scodata_pkt_t);
872 case NG_HCI_EVENT_PKT:
873 sc->want = sizeof(ng_hci_event_pkt_t);
878 "Ignoring unknown packet type=%#x\n", *mtod(sc->m, u_int8_t *));
880 NG_BT3C_STAT_IERROR(sc->stat);
883 sc->state = NG_BT3C_W4_PKT_IND;
889 /* Got packet header */
890 case NG_BT3C_W4_PKT_HDR:
891 sc->state = NG_BT3C_W4_PKT_DATA;
893 switch (*mtod(sc->m, u_int8_t *)) {
894 case NG_HCI_ACL_DATA_PKT:
895 c = le16toh(mtod(sc->m,
896 ng_hci_acldata_pkt_t *)->length);
899 case NG_HCI_SCO_DATA_PKT:
900 c = mtod(sc->m, ng_hci_scodata_pkt_t*)->length;
903 case NG_HCI_EVENT_PKT:
904 c = mtod(sc->m, ng_hci_event_pkt_t *)->length;
909 ("Invalid packet type=%#x\n", *mtod(sc->m, u_int8_t *)));
913 NG_BT3C_INFO(sc->dev,
914 "Got packet header, packet type=%#x, got so far %d, payload size=%d\n",
915 *mtod(sc->m, u_int8_t *), sc->m->m_pkthdr.len,
923 /* else FALLTHROUGH and deliver frame */
924 /* XXX is this true? should we deliver empty frame? */
926 /* Got packet data */
927 case NG_BT3C_W4_PKT_DATA:
928 NG_BT3C_INFO(sc->dev,
929 "Got full packet, packet type=%#x, packet size=%d\n",
930 *mtod(sc->m, u_int8_t *), sc->m->m_pkthdr.len);
932 NG_BT3C_STAT_BYTES_RECV(sc->stat, sc->m->m_pkthdr.len);
933 NG_BT3C_STAT_PCKTS_RECV(sc->stat);
936 if (_IF_QFULL(&sc->inq)) {
938 "Incoming queue is full. Dropping mbuf, len=%d\n", sc->m->m_pkthdr.len);
940 NG_BT3C_STAT_IERROR(sc->stat);
944 _IF_ENQUEUE(&sc->inq, sc->m);
949 sc->state = NG_BT3C_W4_PKT_IND;
955 ("Invalid node state=%d", sc->state));
960 bt3c_write(sc, 0x7006, 0x0000);
964 * SWI interrupt handler
965 * Netgraph part is handled via ng_send_fn() to avoid race with hook
966 * connection/disconnection
970 bt3c_swi_intr(void *context)
972 bt3c_softc_p sc = (bt3c_softc_p) context;
975 /* Receive complete */
976 if (sc->status & 0x0001) {
977 sc->status &= ~0x0001; /* XXX is it safe? */
979 if (ng_send_fn(sc->node, NULL, &bt3c_forward, NULL, 0) != 0)
980 NG_BT3C_ALERT(sc->dev, "Could not forward frames!\n");
984 if (sc->status & 0x0002) {
985 sc->status &= ~0x0002; /* XXX is it safe */
987 if (ng_send_fn(sc->node, NULL, &bt3c_send, NULL, 1) != 0)
988 NG_BT3C_ALERT(sc->dev, "Could not send frames!\n");
991 /* Antenna position */
992 if (sc->status & 0x0020) {
993 sc->status &= ~0x0020; /* XXX is it safe */
995 bt3c_read(sc, 0x7002, data);
999 sc->flags |= BT3C_ANTENNA_OUT;
1001 sc->flags &= ~BT3C_ANTENNA_OUT;
1003 NG_BT3C_INFO(sc->dev, "Antenna %s\n", data? "OUT" : "IN");
1005 } /* bt3c_swi_intr */
1008 * Send all incoming frames to the upper layer
1012 bt3c_forward(node_p node, hook_p hook, void *arg1, int arg2)
1014 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
1015 struct mbuf *m = NULL;
1021 if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) {
1023 IF_DEQUEUE(&sc->inq, m);
1027 NG_SEND_DATA_ONLY(error, sc->hook, m);
1029 NG_BT3C_STAT_IERROR(sc->stat);
1034 _IF_DEQUEUE(&sc->inq, m);
1038 NG_BT3C_STAT_IERROR(sc->stat);
1041 IF_UNLOCK(&sc->inq);
1043 } /* bt3c_forward */
1046 * Send more data to the device. Must be called when node is locked
1050 bt3c_send(node_p node, hook_p hook, void *arg, int completed)
1052 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
1053 struct mbuf *m = NULL;
1060 sc->flags &= ~BT3C_XMIT;
1062 if (sc->flags & BT3C_XMIT)
1065 bt3c_set_address(sc, 0x7080);
1067 for (wrote = 0; wrote < BT3C_FIFO_SIZE; ) {
1068 IF_DEQUEUE(&sc->outq, m);
1073 len = min((BT3C_FIFO_SIZE - wrote), m->m_len);
1075 for (i = 0; i < len; i++)
1076 bt3c_write_data(sc, m->m_data[i]);
1089 IF_PREPEND(&sc->outq, m);
1093 NG_BT3C_STAT_PCKTS_SENT(sc->stat);
1097 NG_BT3C_INFO(sc->dev, "Wrote %d bytes\n", wrote);
1098 NG_BT3C_STAT_BYTES_SENT(sc->stat, wrote);
1100 bt3c_write(sc, 0x7005, wrote);
1101 sc->flags |= BT3C_XMIT;
1106 * Download chip firmware
1110 bt3c_download_firmware(bt3c_softc_p sc, char const *firmware, int firmware_size)
1112 ng_bt3c_firmware_block_ep const *block = NULL;
1113 u_int16_t const *data = NULL;
1118 device_printf(sc->dev, "Reseting the card...\n");
1119 bt3c_write(sc, 0x8040, 0x0404);
1120 bt3c_write(sc, 0x8040, 0x0400);
1123 bt3c_write(sc, 0x8040, 0x0404);
1126 /* Download firmware */
1127 device_printf(sc->dev, "Starting firmware download process...\n");
1129 for (size = 0; size < firmware_size; ) {
1130 block = (ng_bt3c_firmware_block_ep const *)(firmware + size);
1131 data = (u_int16_t const *)(block + 1);
1134 device_printf(sc->dev, "Download firmware block, " \
1135 "address=%#08x, size=%d words, aligment=%d\n",
1136 block->block_address, block->block_size,
1137 block->block_alignment);
1139 bt3c_set_address(sc, block->block_address);
1140 for (i = 0; i < block->block_size; i++)
1141 bt3c_write_data(sc, data[i]);
1143 size += (sizeof(*block) + (block->block_size * 2) +
1144 block->block_alignment);
1148 device_printf(sc->dev, "Firmware download process complete\n");
1151 device_printf(sc->dev, "Starting the card...\n");
1152 bt3c_set_address(sc, 0x3000);
1153 bt3c_read_control(sc, c);
1154 bt3c_write_control(sc, (c | 0x40));
1157 /* Clear registers */
1158 device_printf(sc->dev, "Clearing card registers...\n");
1159 bt3c_write(sc, 0x7006, 0x0000);
1160 bt3c_write(sc, 0x7005, 0x0000);
1161 bt3c_write(sc, 0x7001, 0x0000);
1163 } /* bt3c_download_firmware */
1165 /****************************************************************************
1166 ****************************************************************************
1168 ****************************************************************************
1169 ****************************************************************************/
1172 * PC Card (PCMCIA) driver
1175 static device_method_t bt3c_pccard_methods[] = {
1176 /* Device interface */
1177 DEVMETHOD(device_probe, bt3c_pccard_probe),
1178 DEVMETHOD(device_attach, bt3c_pccard_attach),
1179 DEVMETHOD(device_detach, bt3c_pccard_detach),
1184 static driver_t bt3c_pccard_driver = {
1186 bt3c_pccard_methods,
1187 sizeof(bt3c_softc_t)
1190 static devclass_t bt3c_devclass;
1194 * Load/Unload the driver module
1198 bt3c_modevent(module_t mod, int event, void *data)
1204 error = ng_newtype(&typestruct);
1206 printf("%s: Could not register Netgraph node type, " \
1207 "error=%d\n", NG_BT3C_NODE_TYPE, error);
1211 error = ng_rmtype(&typestruct);
1220 } /* bt3c_modevent */
1222 DRIVER_MODULE(bt3c, pccard, bt3c_pccard_driver, bt3c_devclass, bt3c_modevent,0);
1223 MODULE_VERSION(ng_bt3c, NG_BLUETOOTH_VERSION);
1224 MODULE_DEPEND(ng_bt3c, netgraph, NG_ABI_VERSION, NG_ABI_VERSION,NG_ABI_VERSION);
1225 PCCARD_PNP_INFO(bt3c_pccard_products);