6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: ng_bt3c_pccard.c,v 1.5 2003/04/01 18:15:21 max Exp $
35 * XXX XXX XX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
37 * Based on information obrained from: Jose Orlando Pereira <jop@di.uminho.pt>
38 * and disassembled w2k driver.
40 * XXX XXX XX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <machine/bus.h>
51 #include <sys/endian.h>
52 #include <sys/interrupt.h>
53 #include <sys/kernel.h>
55 #include <sys/module.h>
57 #include <machine/resource.h>
60 #include <sys/socket.h>
62 #include <net/if_var.h>
64 #include <dev/pccard/pccardreg.h>
65 #include <dev/pccard/pccardvar.h>
66 #include "pccarddevs.h"
68 #include <netgraph/ng_message.h>
69 #include <netgraph/netgraph.h>
70 #include <netgraph/ng_parse.h>
71 #include <netgraph/bluetooth/include/ng_bluetooth.h>
72 #include <netgraph/bluetooth/include/ng_hci.h>
73 #include <netgraph/bluetooth/include/ng_bt3c.h>
74 #include <netgraph/bluetooth/drivers/bt3c/ng_bt3c_var.h>
76 /* Netgraph methods */
77 static ng_constructor_t ng_bt3c_constructor;
78 static ng_shutdown_t ng_bt3c_shutdown;
79 static ng_newhook_t ng_bt3c_newhook;
80 static ng_connect_t ng_bt3c_connect;
81 static ng_disconnect_t ng_bt3c_disconnect;
82 static ng_rcvmsg_t ng_bt3c_rcvmsg;
83 static ng_rcvdata_t ng_bt3c_rcvdata;
85 /* PCMCIA driver methods */
86 static int bt3c_pccard_probe (device_t);
87 static int bt3c_pccard_attach (device_t);
88 static int bt3c_pccard_detach (device_t);
90 static void bt3c_intr (void *);
91 static void bt3c_receive (bt3c_softc_p);
93 static void bt3c_swi_intr (void *);
94 static void bt3c_forward (node_p, hook_p, void *, int);
95 static void bt3c_send (node_p, hook_p, void *, int);
97 static void bt3c_download_firmware (bt3c_softc_p, char const *, int);
99 #define bt3c_set_address(sc, address) \
101 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_ADDR_L, ((address) & 0xff)); \
102 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_ADDR_H, (((address) >> 8) & 0xff)); \
105 #define bt3c_read_data(sc, data) \
107 (data) = bus_space_read_1((sc)->iot, (sc)->ioh, BT3C_DATA_L); \
108 (data) |= ((bus_space_read_1((sc)->iot, (sc)->ioh, BT3C_DATA_H) & 0xff) << 8); \
111 #define bt3c_write_data(sc, data) \
113 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_DATA_L, ((data) & 0xff)); \
114 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_DATA_H, (((data) >> 8) & 0xff)); \
117 #define bt3c_read_control(sc, data) \
119 (data) = bus_space_read_1((sc)->iot, (sc)->ioh, BT3C_CONTROL); \
122 #define bt3c_write_control(sc, data) \
124 bus_space_write_1((sc)->iot, (sc)->ioh, BT3C_CONTROL, (data)); \
127 #define bt3c_read(sc, address, data) \
129 bt3c_set_address((sc), (address)); \
130 bt3c_read_data((sc), (data)); \
133 #define bt3c_write(sc, address, data) \
135 bt3c_set_address((sc), (address)); \
136 bt3c_write_data((sc), (data)); \
139 static MALLOC_DEFINE(M_BT3C, "bt3c", "bt3c data structures");
141 /****************************************************************************
142 ****************************************************************************
144 ****************************************************************************
145 ****************************************************************************/
152 static const struct ng_parse_struct_field ng_bt3c_node_qlen_type_fields[] =
154 { "queue", &ng_parse_int32_type, },
155 { "qlen", &ng_parse_int32_type, },
158 static const struct ng_parse_type ng_bt3c_node_qlen_type = {
159 &ng_parse_struct_type,
160 &ng_bt3c_node_qlen_type_fields
164 static const struct ng_parse_struct_field ng_bt3c_node_stat_type_fields[] =
166 { "pckts_recv", &ng_parse_uint32_type, },
167 { "bytes_recv", &ng_parse_uint32_type, },
168 { "pckts_sent", &ng_parse_uint32_type, },
169 { "bytes_sent", &ng_parse_uint32_type, },
170 { "oerrors", &ng_parse_uint32_type, },
171 { "ierrors", &ng_parse_uint32_type, },
174 static const struct ng_parse_type ng_bt3c_node_stat_type = {
175 &ng_parse_struct_type,
176 &ng_bt3c_node_stat_type_fields
179 static const struct ng_cmdlist ng_bt3c_cmdlist[] = {
182 NGM_BT3C_NODE_GET_STATE,
185 &ng_parse_uint16_type
189 NGM_BT3C_NODE_SET_DEBUG,
191 &ng_parse_uint16_type,
196 NGM_BT3C_NODE_GET_DEBUG,
199 &ng_parse_uint16_type
203 NGM_BT3C_NODE_GET_QLEN,
206 &ng_bt3c_node_qlen_type
210 NGM_BT3C_NODE_SET_QLEN,
212 &ng_bt3c_node_qlen_type,
217 NGM_BT3C_NODE_GET_STAT,
220 &ng_bt3c_node_stat_type
224 NGM_BT3C_NODE_RESET_STAT,
232 static struct ng_type typestruct = {
233 .version = NG_ABI_VERSION,
234 .name = NG_BT3C_NODE_TYPE,
235 .constructor = ng_bt3c_constructor,
236 .rcvmsg = ng_bt3c_rcvmsg,
237 .shutdown = ng_bt3c_shutdown,
238 .newhook = ng_bt3c_newhook,
239 .connect = ng_bt3c_connect,
240 .rcvdata = ng_bt3c_rcvdata,
241 .disconnect = ng_bt3c_disconnect,
242 .cmdlist = ng_bt3c_cmdlist
246 * Netgraph node constructor. Do not allow to create node of this type.
250 ng_bt3c_constructor(node_p node)
253 } /* ng_bt3c_constructor */
256 * Netgraph node destructor. Destroy node only when device has been detached
260 ng_bt3c_shutdown(node_p node)
262 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
264 /* Let old node go */
265 NG_NODE_SET_PRIVATE(node, NULL);
268 /* Create new fresh one if we are not going down */
272 /* Create new Netgraph node */
273 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
274 device_printf(sc->dev, "Could not create Netgraph node\n");
279 /* Name new Netgraph node */
280 if (ng_name_node(sc->node, device_get_nameunit(sc->dev)) != 0) {
281 device_printf(sc->dev, "Could not name Netgraph node\n");
282 NG_NODE_UNREF(sc->node);
287 NG_NODE_SET_PRIVATE(sc->node, sc);
290 } /* ng_bt3c_shutdown */
293 * Create new hook. There can only be one.
297 ng_bt3c_newhook(node_p node, hook_p hook, char const *name)
299 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
301 if (strcmp(name, NG_BT3C_HOOK) != 0)
304 if (sc->hook != NULL)
310 } /* ng_bt3c_newhook */
313 * Connect hook. Say YEP, that's OK with me.
317 ng_bt3c_connect(hook_p hook)
319 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
321 if (hook != sc->hook) {
326 /* set the hook into queueing mode (for incoming (from wire) packets) */
327 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
330 } /* ng_bt3c_connect */
337 ng_bt3c_disconnect(hook_p hook)
339 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
342 * We need to check for sc != NULL because we can be called from
343 * bt3c_pccard_detach() via ng_rmnode_self()
347 if (hook != sc->hook)
357 } /* ng_bt3c_disconnect */
360 * Process control message
364 ng_bt3c_rcvmsg(node_p node, item_p item, hook_p lasthook)
366 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
367 struct ng_mesg *msg = NULL, *rsp = NULL;
375 NGI_GET_MSG(item, msg);
377 switch (msg->header.typecookie) {
378 case NGM_GENERIC_COOKIE:
379 switch (msg->header.cmd) {
380 case NGM_TEXT_STATUS:
381 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
385 snprintf(rsp->data, NG_TEXTRESPONSE,
390 "IncmQ: [len:%d,max:%d]\n" \
391 "OutgQ: [len:%d,max:%d]\n",
392 (sc->hook != NULL)? NG_BT3C_HOOK : "",
396 _IF_QLEN(&sc->inq), /* XXX */
397 sc->inq.ifq_maxlen, /* XXX */
398 _IF_QLEN(&sc->outq), /* XXX */
399 sc->outq.ifq_maxlen /* XXX */
409 case NGM_BT3C_COOKIE:
410 switch (msg->header.cmd) {
411 case NGM_BT3C_NODE_GET_STATE:
412 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_state_ep),
417 *((ng_bt3c_node_state_ep *)(rsp->data)) =
421 case NGM_BT3C_NODE_SET_DEBUG:
422 if (msg->header.arglen != sizeof(ng_bt3c_node_debug_ep))
426 *((ng_bt3c_node_debug_ep *)(msg->data));
429 case NGM_BT3C_NODE_GET_DEBUG:
430 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_debug_ep),
435 *((ng_bt3c_node_debug_ep *)(rsp->data)) =
439 case NGM_BT3C_NODE_GET_QLEN:
440 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_qlen_ep),
447 switch (((ng_bt3c_node_qlen_ep *)(msg->data))->queue) {
448 case NGM_BT3C_NODE_IN_QUEUE:
449 ((ng_bt3c_node_qlen_ep *)(rsp->data))->queue =
450 NGM_BT3C_NODE_IN_QUEUE;
451 ((ng_bt3c_node_qlen_ep *)(rsp->data))->qlen =
455 case NGM_BT3C_NODE_OUT_QUEUE:
456 ((ng_bt3c_node_qlen_ep *)(rsp->data))->queue =
457 NGM_BT3C_NODE_OUT_QUEUE;
458 ((ng_bt3c_node_qlen_ep *)(rsp->data))->qlen =
469 case NGM_BT3C_NODE_SET_QLEN:
470 if (msg->header.arglen != sizeof(ng_bt3c_node_qlen_ep)){
475 if (((ng_bt3c_node_qlen_ep *)(msg->data))->qlen <= 0) {
480 switch (((ng_bt3c_node_qlen_ep *)(msg->data))->queue) {
481 case NGM_BT3C_NODE_IN_QUEUE:
482 sc->inq.ifq_maxlen = ((ng_bt3c_node_qlen_ep *)
483 (msg->data))->qlen; /* XXX */
486 case NGM_BT3C_NODE_OUT_QUEUE:
487 sc->outq.ifq_maxlen = ((ng_bt3c_node_qlen_ep *)
488 (msg->data))->qlen; /* XXX */
497 case NGM_BT3C_NODE_GET_STAT:
498 NG_MKRESPONSE(rsp, msg, sizeof(ng_bt3c_node_stat_ep),
503 bcopy(&sc->stat, rsp->data,
504 sizeof(ng_bt3c_node_stat_ep));
507 case NGM_BT3C_NODE_RESET_STAT:
508 NG_BT3C_STAT_RESET(sc->stat);
511 case NGM_BT3C_NODE_DOWNLOAD_FIRMWARE:
512 if (msg->header.arglen <
513 sizeof(ng_bt3c_firmware_block_ep))
516 bt3c_download_firmware(sc, msg->data,
531 NG_RESPOND_MSG(error, node, item, rsp);
535 } /* ng_bt3c_rcvmsg */
542 ng_bt3c_rcvdata(hook_p hook, item_p item)
544 bt3c_softc_p sc = (bt3c_softc_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
545 struct mbuf *m = NULL;
553 if (hook != sc->hook) {
561 if (_IF_QFULL(&sc->outq)) {
563 "Outgoing queue is full. Dropping mbuf, len=%d\n", m->m_pkthdr.len);
565 NG_BT3C_STAT_OERROR(sc->stat);
569 _IF_ENQUEUE(&sc->outq, m);
570 IF_UNLOCK(&sc->outq);
572 error = ng_send_fn(sc->node, NULL, bt3c_send, NULL, 0 /* new send */);
577 } /* ng_bt3c_rcvdata */
579 /****************************************************************************
580 ****************************************************************************
581 ** PCMCIA driver specific
582 ****************************************************************************
583 ****************************************************************************/
586 * PC Card (PCMCIA) probe routine
589 static struct pccard_product const bt3c_pccard_products[] = {
590 PCMCIA_CARD(3COM, 3CRWB609),
595 bt3c_pccard_probe(device_t dev)
597 struct pccard_product const *pp = NULL;
599 pp = pccard_product_lookup(dev, bt3c_pccard_products,
600 sizeof(bt3c_pccard_products[0]), NULL);
604 device_set_desc(dev, pp->pp_name);
607 } /* bt3c_pccard_probe */
610 * PC Card (PCMCIA) attach routine
614 bt3c_pccard_attach(device_t dev)
616 bt3c_softc_p sc = (bt3c_softc_p) device_get_softc(dev);
618 /* Allocate I/O ports */
620 sc->iobase = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
621 &sc->iobase_rid, 8, RF_ACTIVE);
622 if (sc->iobase == NULL) {
623 device_printf(dev, "Could not allocate I/O ports\n");
626 sc->iot = rman_get_bustag(sc->iobase);
627 sc->ioh = rman_get_bushandle(sc->iobase);
631 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
633 if (sc->irq == NULL) {
634 device_printf(dev, "Could not allocate IRQ\n");
638 sc->irq_cookie = NULL;
639 if (bus_setup_intr(dev, sc->irq, INTR_TYPE_TTY, NULL, bt3c_intr, sc,
640 &sc->irq_cookie) != 0) {
641 device_printf(dev, "Could not setup ISR\n");
645 /* Attach handler to TTY SWI thread */
647 if (swi_add(&tty_intr_event, device_get_nameunit(dev),
648 bt3c_swi_intr, sc, SWI_TTY, 0, &sc->ith) < 0) {
649 device_printf(dev, "Could not setup SWI ISR\n");
653 /* Create Netgraph node */
654 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
655 device_printf(dev, "Could not create Netgraph node\n");
660 /* Name Netgraph node */
661 if (ng_name_node(sc->node, device_get_nameunit(dev)) != 0) {
662 device_printf(dev, "Could not name Netgraph node\n");
663 NG_NODE_UNREF(sc->node);
669 sc->debug = NG_BT3C_WARN_LEVEL;
671 sc->inq.ifq_maxlen = sc->outq.ifq_maxlen = BT3C_DEFAULTQLEN;
672 mtx_init(&sc->inq.ifq_mtx, "BT3C inq", NULL, MTX_DEF);
673 mtx_init(&sc->outq.ifq_mtx, "BT3C outq", NULL, MTX_DEF);
675 sc->state = NG_BT3C_W4_PKT_IND;
678 NG_NODE_SET_PRIVATE(sc->node, sc);
682 if (sc->ith != NULL) {
687 if (sc->irq != NULL) {
688 if (sc->irq_cookie != NULL)
689 bus_teardown_intr(dev, sc->irq, sc->irq_cookie);
691 bus_release_resource(dev, SYS_RES_IRQ,
692 sc->irq_rid, sc->irq);
698 if (sc->iobase != NULL) {
699 bus_release_resource(dev, SYS_RES_IOPORT,
700 sc->iobase_rid, sc->iobase);
707 } /* bt3c_pccacd_attach */
710 * PC Card (PCMCIA) detach routine
714 bt3c_pccard_detach(device_t dev)
716 bt3c_softc_p sc = (bt3c_softc_p) device_get_softc(dev);
724 bus_teardown_intr(dev, sc->irq, sc->irq_cookie);
725 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
726 sc->irq_cookie = NULL;
730 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
734 if (sc->node != NULL) {
735 NG_NODE_SET_PRIVATE(sc->node, NULL);
736 ng_rmnode_self(sc->node);
744 mtx_destroy(&sc->inq.ifq_mtx);
745 mtx_destroy(&sc->outq.ifq_mtx);
748 } /* bt3c_pccacd_detach */
751 * Interrupt service routine's
755 bt3c_intr(void *context)
757 bt3c_softc_p sc = (bt3c_softc_p) context;
758 u_int16_t control, status;
760 if (sc == NULL || sc->ith == NULL) {
761 printf("%s: bogus interrupt\n", NG_BT3C_NODE_TYPE);
765 bt3c_read_control(sc, control);
766 if ((control & 0x80) == 0)
769 bt3c_read(sc, 0x7001, status);
770 NG_BT3C_INFO(sc->dev, "control=%#x, status=%#x\n", control, status);
772 if ((status & 0xff) == 0x7f || (status & 0xff) == 0xff) {
773 NG_BT3C_WARN(sc->dev, "Strange status=%#x\n", status);
777 /* Receive complete */
781 /* Record status and schedule SWI */
782 sc->status |= status;
783 swi_sched(sc->ith, 0);
785 /* Complete interrupt */
786 bt3c_write(sc, 0x7001, 0x0000);
787 bt3c_write_control(sc, control);
795 bt3c_receive(bt3c_softc_p sc)
797 u_int16_t i, count, c;
799 /* Receive data from the card */
800 bt3c_read(sc, 0x7006, count);
801 NG_BT3C_INFO(sc->dev, "The card has %d characters\n", count);
803 bt3c_set_address(sc, 0x7480);
805 for (i = 0; i < count; i++) {
806 /* Allocate new mbuf if needed */
808 sc->state = NG_BT3C_W4_PKT_IND;
811 MGETHDR(sc->m, M_NOWAIT, MT_DATA);
813 NG_BT3C_ERR(sc->dev, "Could not get mbuf\n");
814 NG_BT3C_STAT_IERROR(sc->stat);
816 break; /* XXX lost of sync */
819 if (!(MCLGET(sc->m, M_NOWAIT))) {
822 NG_BT3C_ERR(sc->dev, "Could not get cluster\n");
823 NG_BT3C_STAT_IERROR(sc->stat);
825 break; /* XXX lost of sync */
828 sc->m->m_len = sc->m->m_pkthdr.len = 0;
831 /* Read and append character to mbuf */
832 bt3c_read_data(sc, c);
833 if (sc->m->m_pkthdr.len >= MCLBYTES) {
834 NG_BT3C_ERR(sc->dev, "Oversized frame\n");
837 sc->state = NG_BT3C_W4_PKT_IND;
840 break; /* XXX lost of sync */
843 mtod(sc->m, u_int8_t *)[sc->m->m_len ++] = (u_int8_t) c;
844 sc->m->m_pkthdr.len ++;
846 NG_BT3C_INFO(sc->dev,
847 "Got char %#x, want=%d, got=%d\n", c, sc->want, sc->m->m_pkthdr.len);
849 if (sc->m->m_pkthdr.len < sc->want)
850 continue; /* wait for more */
853 /* Got packet indicator */
854 case NG_BT3C_W4_PKT_IND:
855 NG_BT3C_INFO(sc->dev,
856 "Got packet indicator %#x\n", *mtod(sc->m, u_int8_t *));
858 sc->state = NG_BT3C_W4_PKT_HDR;
861 * Since packet indicator included in the packet
862 * header just set sc->want to sizeof(packet header).
865 switch (*mtod(sc->m, u_int8_t *)) {
866 case NG_HCI_ACL_DATA_PKT:
867 sc->want = sizeof(ng_hci_acldata_pkt_t);
870 case NG_HCI_SCO_DATA_PKT:
871 sc->want = sizeof(ng_hci_scodata_pkt_t);
874 case NG_HCI_EVENT_PKT:
875 sc->want = sizeof(ng_hci_event_pkt_t);
880 "Ignoring unknown packet type=%#x\n", *mtod(sc->m, u_int8_t *));
882 NG_BT3C_STAT_IERROR(sc->stat);
885 sc->state = NG_BT3C_W4_PKT_IND;
891 /* Got packet header */
892 case NG_BT3C_W4_PKT_HDR:
893 sc->state = NG_BT3C_W4_PKT_DATA;
895 switch (*mtod(sc->m, u_int8_t *)) {
896 case NG_HCI_ACL_DATA_PKT:
897 c = le16toh(mtod(sc->m,
898 ng_hci_acldata_pkt_t *)->length);
901 case NG_HCI_SCO_DATA_PKT:
902 c = mtod(sc->m, ng_hci_scodata_pkt_t*)->length;
905 case NG_HCI_EVENT_PKT:
906 c = mtod(sc->m, ng_hci_event_pkt_t *)->length;
911 ("Invalid packet type=%#x\n", *mtod(sc->m, u_int8_t *)));
915 NG_BT3C_INFO(sc->dev,
916 "Got packet header, packet type=%#x, got so far %d, payload size=%d\n",
917 *mtod(sc->m, u_int8_t *), sc->m->m_pkthdr.len,
925 /* else FALLTHROUGH and deliver frame */
926 /* XXX is this true? should we deliver empty frame? */
928 /* Got packet data */
929 case NG_BT3C_W4_PKT_DATA:
930 NG_BT3C_INFO(sc->dev,
931 "Got full packet, packet type=%#x, packet size=%d\n",
932 *mtod(sc->m, u_int8_t *), sc->m->m_pkthdr.len);
934 NG_BT3C_STAT_BYTES_RECV(sc->stat, sc->m->m_pkthdr.len);
935 NG_BT3C_STAT_PCKTS_RECV(sc->stat);
938 if (_IF_QFULL(&sc->inq)) {
940 "Incoming queue is full. Dropping mbuf, len=%d\n", sc->m->m_pkthdr.len);
942 NG_BT3C_STAT_IERROR(sc->stat);
946 _IF_ENQUEUE(&sc->inq, sc->m);
951 sc->state = NG_BT3C_W4_PKT_IND;
957 ("Invalid node state=%d", sc->state));
962 bt3c_write(sc, 0x7006, 0x0000);
966 * SWI interrupt handler
967 * Netgraph part is handled via ng_send_fn() to avoid race with hook
968 * connection/disconnection
972 bt3c_swi_intr(void *context)
974 bt3c_softc_p sc = (bt3c_softc_p) context;
977 /* Receive complete */
978 if (sc->status & 0x0001) {
979 sc->status &= ~0x0001; /* XXX is it safe? */
981 if (ng_send_fn(sc->node, NULL, &bt3c_forward, NULL, 0) != 0)
982 NG_BT3C_ALERT(sc->dev, "Could not forward frames!\n");
986 if (sc->status & 0x0002) {
987 sc->status &= ~0x0002; /* XXX is it safe */
989 if (ng_send_fn(sc->node, NULL, &bt3c_send, NULL, 1) != 0)
990 NG_BT3C_ALERT(sc->dev, "Could not send frames!\n");
993 /* Antenna position */
994 if (sc->status & 0x0020) {
995 sc->status &= ~0x0020; /* XXX is it safe */
997 bt3c_read(sc, 0x7002, data);
1001 sc->flags |= BT3C_ANTENNA_OUT;
1003 sc->flags &= ~BT3C_ANTENNA_OUT;
1005 NG_BT3C_INFO(sc->dev, "Antenna %s\n", data? "OUT" : "IN");
1007 } /* bt3c_swi_intr */
1010 * Send all incoming frames to the upper layer
1014 bt3c_forward(node_p node, hook_p hook, void *arg1, int arg2)
1016 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
1017 struct mbuf *m = NULL;
1023 if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) {
1025 IF_DEQUEUE(&sc->inq, m);
1029 NG_SEND_DATA_ONLY(error, sc->hook, m);
1031 NG_BT3C_STAT_IERROR(sc->stat);
1036 _IF_DEQUEUE(&sc->inq, m);
1040 NG_BT3C_STAT_IERROR(sc->stat);
1043 IF_UNLOCK(&sc->inq);
1045 } /* bt3c_forward */
1048 * Send more data to the device. Must be called when node is locked
1052 bt3c_send(node_p node, hook_p hook, void *arg, int completed)
1054 bt3c_softc_p sc = (bt3c_softc_p) NG_NODE_PRIVATE(node);
1055 struct mbuf *m = NULL;
1062 sc->flags &= ~BT3C_XMIT;
1064 if (sc->flags & BT3C_XMIT)
1067 bt3c_set_address(sc, 0x7080);
1069 for (wrote = 0; wrote < BT3C_FIFO_SIZE; ) {
1070 IF_DEQUEUE(&sc->outq, m);
1075 len = min((BT3C_FIFO_SIZE - wrote), m->m_len);
1077 for (i = 0; i < len; i++)
1078 bt3c_write_data(sc, m->m_data[i]);
1091 IF_PREPEND(&sc->outq, m);
1095 NG_BT3C_STAT_PCKTS_SENT(sc->stat);
1099 NG_BT3C_INFO(sc->dev, "Wrote %d bytes\n", wrote);
1100 NG_BT3C_STAT_BYTES_SENT(sc->stat, wrote);
1102 bt3c_write(sc, 0x7005, wrote);
1103 sc->flags |= BT3C_XMIT;
1108 * Download chip firmware
1112 bt3c_download_firmware(bt3c_softc_p sc, char const *firmware, int firmware_size)
1114 ng_bt3c_firmware_block_ep const *block = NULL;
1115 u_int16_t const *data = NULL;
1120 device_printf(sc->dev, "Reseting the card...\n");
1121 bt3c_write(sc, 0x8040, 0x0404);
1122 bt3c_write(sc, 0x8040, 0x0400);
1125 bt3c_write(sc, 0x8040, 0x0404);
1128 /* Download firmware */
1129 device_printf(sc->dev, "Starting firmware download process...\n");
1131 for (size = 0; size < firmware_size; ) {
1132 block = (ng_bt3c_firmware_block_ep const *)(firmware + size);
1133 data = (u_int16_t const *)(block + 1);
1136 device_printf(sc->dev, "Download firmware block, " \
1137 "address=%#08x, size=%d words, aligment=%d\n",
1138 block->block_address, block->block_size,
1139 block->block_alignment);
1141 bt3c_set_address(sc, block->block_address);
1142 for (i = 0; i < block->block_size; i++)
1143 bt3c_write_data(sc, data[i]);
1145 size += (sizeof(*block) + (block->block_size * 2) +
1146 block->block_alignment);
1150 device_printf(sc->dev, "Firmware download process complete\n");
1153 device_printf(sc->dev, "Starting the card...\n");
1154 bt3c_set_address(sc, 0x3000);
1155 bt3c_read_control(sc, c);
1156 bt3c_write_control(sc, (c | 0x40));
1159 /* Clear registers */
1160 device_printf(sc->dev, "Clearing card registers...\n");
1161 bt3c_write(sc, 0x7006, 0x0000);
1162 bt3c_write(sc, 0x7005, 0x0000);
1163 bt3c_write(sc, 0x7001, 0x0000);
1165 } /* bt3c_download_firmware */
1167 /****************************************************************************
1168 ****************************************************************************
1170 ****************************************************************************
1171 ****************************************************************************/
1174 * PC Card (PCMCIA) driver
1177 static device_method_t bt3c_pccard_methods[] = {
1178 /* Device interface */
1179 DEVMETHOD(device_probe, bt3c_pccard_probe),
1180 DEVMETHOD(device_attach, bt3c_pccard_attach),
1181 DEVMETHOD(device_detach, bt3c_pccard_detach),
1186 static driver_t bt3c_pccard_driver = {
1188 bt3c_pccard_methods,
1189 sizeof(bt3c_softc_t)
1192 static devclass_t bt3c_devclass;
1196 * Load/Unload the driver module
1200 bt3c_modevent(module_t mod, int event, void *data)
1206 error = ng_newtype(&typestruct);
1208 printf("%s: Could not register Netgraph node type, " \
1209 "error=%d\n", NG_BT3C_NODE_TYPE, error);
1213 error = ng_rmtype(&typestruct);
1222 } /* bt3c_modevent */
1224 DRIVER_MODULE(bt3c, pccard, bt3c_pccard_driver, bt3c_devclass, bt3c_modevent,0);
1225 MODULE_VERSION(ng_bt3c, NG_BLUETOOTH_VERSION);
1226 MODULE_DEPEND(ng_bt3c, netgraph, NG_ABI_VERSION, NG_ABI_VERSION,NG_ABI_VERSION);
1227 PCCARD_PNP_INFO(bt3c_pccard_products);