1 /* $OpenBSD: hci_unit.c,v 1.7 2007/06/24 20:55:27 uwe Exp $ */
2 /* $NetBSD: hci_unit.c,v 1.4 2007/03/30 20:47:03 plunky Exp $ */
3 /* $DragonFly: src/sys/netbt/hci_unit.c,v 1.1 2007/12/30 20:02:56 hasso Exp $ */
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
37 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
44 #include <sys/queue.h>
45 #include <sys/systm.h>
46 #include <sys/endian.h>
49 #include <net/netisr.h>
51 #include <netbt/bluetooth.h>
52 #include <netbt/hci.h>
54 struct hci_unit_list hci_unit_list = TAILQ_HEAD_INITIALIZER(hci_unit_list);
57 * HCI Input Queue max lengths.
59 int hci_eventq_max = 20;
60 int hci_aclrxq_max = 50;
61 int hci_scorxq_max = 50;
64 * bluetooth unit functions
68 hci_attach(struct hci_unit *unit)
70 KKASSERT(unit->hci_softc != NULL);
71 KKASSERT(unit->hci_devname != NULL);
72 KKASSERT(unit->hci_enable != NULL);
73 KKASSERT(unit->hci_disable != NULL);
74 KKASSERT(unit->hci_start_cmd != NULL);
75 KKASSERT(unit->hci_start_acl != NULL);
76 KKASSERT(unit->hci_start_sco != NULL);
78 unit->hci_eventq.ifq_maxlen = hci_eventq_max;
79 unit->hci_aclrxq.ifq_maxlen = hci_aclrxq_max;
80 unit->hci_scorxq.ifq_maxlen = hci_scorxq_max;
82 TAILQ_INIT(&unit->hci_links);
83 LIST_INIT(&unit->hci_memos);
85 TAILQ_INSERT_TAIL(&hci_unit_list, unit, hci_next);
89 hci_detach(struct hci_unit *unit)
93 TAILQ_REMOVE(&hci_unit_list, unit, hci_next);
97 hci_enable(struct hci_unit *unit)
102 * Bluetooth spec says that a device can accept one
103 * command on power up until they send a Command Status
104 * or Command Complete event with more information, but
105 * it seems that some devices cant and prefer to send a
106 * No-op Command Status packet when they are ready, so
107 * we set this here and allow the driver (bt3c) to zero
110 unit->hci_num_cmd_pkts = 1;
111 unit->hci_num_acl_pkts = 0;
112 unit->hci_num_sco_pkts = 0;
115 * only allow the basic packet types until
116 * the features report is in
118 unit->hci_acl_mask = HCI_PKT_DM1 | HCI_PKT_DH1;
119 unit->hci_packet_type = unit->hci_acl_mask;
121 err = (*unit->hci_enable)(unit);
126 * Reset the device, this will trigger initialisation
130 unit->hci_flags |= BTF_INIT;
133 err = hci_send_cmd(unit, HCI_CMD_RESET, NULL, 0);
137 while (unit->hci_flags & BTF_INIT) {
138 err = tsleep(unit, PCATCH, "hciena", 5 * hz);
143 * "What If", while we were sleeping, the device
144 * was removed and detached? Ho Hum.
150 * Attach Bluetooth Device Hub
152 unit->hci_bthub = NULL;
154 unit->hci_bthub = device_add_child(unit->hci_softc, "bthub", -1);
155 if (!unit->hci_bthub) {
156 device_printf(unit->hci_softc, "Device creation failed\n");
160 DPRINTFN(10, "%s is added as child to %s\n",
161 device_get_nameunit(unit->hci_bthub),
162 device_get_nameunit(unit->hci_softc));
164 device_set_desc(unit->hci_bthub,"Bluetooth Device Hub");
166 device_set_ivars(unit->hci_bthub, &unit->hci_bdaddr);
168 device_probe_and_attach(unit->hci_bthub);
173 (*unit->hci_disable)(unit);
180 hci_disable(struct hci_unit *unit)
182 struct hci_link *link, *next;
183 struct hci_memo *memo;
187 if (unit->hci_bthub) {
188 device_delete_child(unit->hci_softc, unit->hci_bthub);
189 unit->hci_bthub = NULL;
193 (*unit->hci_disable)(unit);
196 * close down any links, take care to close SCO first since
197 * they may depend on ACL links.
199 for (acl = 0 ; acl < 2 ; acl++) {
200 next = TAILQ_FIRST(&unit->hci_links);
201 while ((link = next) != NULL) {
202 next = TAILQ_NEXT(link, hl_next);
203 if (acl || link->hl_type != HCI_LINK_ACL)
204 hci_link_free(link, ECONNABORTED);
208 while ((memo = LIST_FIRST(&unit->hci_memos)) != NULL)
211 IF_DRAIN(&unit->hci_eventq);
212 unit->hci_eventqlen = 0;
214 IF_DRAIN(&unit->hci_aclrxq);
215 unit->hci_aclrxqlen = 0;
217 IF_DRAIN(&unit->hci_scorxq);
218 unit->hci_scorxqlen = 0;
220 IF_DRAIN(&unit->hci_cmdq);
221 IF_DRAIN(&unit->hci_cmdwait);
222 IF_DRAIN(&unit->hci_acltxq);
223 IF_DRAIN(&unit->hci_scotxq);
224 IF_DRAIN(&unit->hci_scodone);
228 hci_unit_lookup(bdaddr_t *addr)
230 struct hci_unit *unit;
232 TAILQ_FOREACH(unit, &hci_unit_list, hci_next) {
233 if ((unit->hci_flags & BTF_UP) == 0)
236 if (bdaddr_same(&unit->hci_bdaddr, addr))
244 * construct and queue a HCI command packet
247 hci_send_cmd(struct hci_unit *unit, uint16_t opcode, void *buf, uint8_t len)
253 KKASSERT(unit != NULL);
255 m = m_gethdr(MB_DONTWAIT, MT_DATA);
259 p = mtod(m, hci_cmd_hdr_t *);
260 p->type = HCI_CMD_PKT;
261 p->opcode = htole16(opcode);
263 m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
267 KKASSERT(buf != NULL);
269 m_copyback(m, sizeof(hci_cmd_hdr_t), len, buf);
270 if (m->m_pkthdr.len != (sizeof(hci_cmd_hdr_t) + len)) {
276 DPRINTFN(2, "(%s) opcode (%3.3x|%4.4x)\n", unit->hci_devname,
277 HCI_OGF(opcode), HCI_OCF(opcode));
280 if (unit->hci_num_cmd_pkts == 0) {
281 IF_ENQUEUE(&unit->hci_cmdwait, m);
283 hci_output_cmd(unit, m);
289 * Incoming packet processing. Since the code is single threaded
290 * in any case (IPL_SOFTNET), we handle it all in one interrupt function
291 * picking our way through more important packets first so that hopefully
292 * we will never get clogged up with bulk data.
297 struct hci_unit *unit = arg;
303 if (unit->hci_eventqlen > 0) {
304 IF_DEQUEUE(&unit->hci_eventq, m);
305 unit->hci_eventqlen--;
309 DPRINTFN(10, "(%s) recv event, len = %d\n",
310 unit->hci_devname, m->m_pkthdr.len);
312 m->m_flags |= IFF_LINK0; /* mark incoming packet */
319 if (unit->hci_scorxqlen > 0) {
320 IF_DEQUEUE(&unit->hci_scorxq, m);
321 unit->hci_scorxqlen--;
325 DPRINTFN(10, "(%s) recv SCO, len = %d\n",
326 unit->hci_devname, m->m_pkthdr.len);
328 m->m_flags |= IFF_LINK0; /* mark incoming packet */
330 hci_sco_recv(m, unit);
335 if (unit->hci_aclrxqlen > 0) {
336 IF_DEQUEUE(&unit->hci_aclrxq, m);
337 unit->hci_aclrxqlen--;
341 DPRINTFN(10, "(%s) recv ACL, len = %d\n",
342 unit->hci_devname, m->m_pkthdr.len);
344 m->m_flags |= IFF_LINK0; /* mark incoming packet */
346 hci_acl_recv(m, unit);
351 IF_DEQUEUE(&unit->hci_scodone, m);
353 struct hci_link *link;
356 DPRINTFN(11, "(%s) complete SCO\n",
359 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
360 if (link == M_GETCTX(m, struct hci_link *)) {
361 hci_sco_complete(link, 1);
366 unit->hci_num_sco_pkts++;
374 DPRINTFN(10, "done\n");
377 /**********************************************************************
381 * input & complete routines will be called from device driver
386 hci_input_event(struct hci_unit *unit, struct mbuf *m)
388 if (unit->hci_eventqlen > hci_eventq_max) {
389 DPRINTF("(%s) dropped event packet.\n", unit->hci_devname);
390 unit->hci_stats.err_rx++;
393 unit->hci_eventqlen++;
395 IF_ENQUEUE(&unit->hci_eventq, m);
397 netisr_queue(NETISR_BLUETOOTH, m);
402 hci_input_acl(struct hci_unit *unit, struct mbuf *m)
404 if (unit->hci_aclrxqlen > hci_aclrxq_max) {
405 DPRINTF("(%s) dropped ACL packet.\n", unit->hci_devname);
406 unit->hci_stats.err_rx++;
409 unit->hci_aclrxqlen++;
411 IF_ENQUEUE(&unit->hci_aclrxq, m);
413 netisr_queue(NETISR_BLUETOOTH,m);
418 hci_input_sco(struct hci_unit *unit, struct mbuf *m)
420 if (unit->hci_scorxqlen > hci_scorxq_max) {
421 DPRINTF("(%s) dropped SCO packet.\n", unit->hci_devname);
422 unit->hci_stats.err_rx++;
425 unit->hci_scorxqlen++;
427 IF_ENQUEUE(&unit->hci_scorxq, m);
429 netisr_queue(NETISR_BLUETOOTH,m);
434 hci_output_cmd(struct hci_unit *unit, struct mbuf *m)
440 DPRINTFN(10, "(%s) num_cmd_pkts=%d\n", unit->hci_devname,
441 unit->hci_num_cmd_pkts);
443 unit->hci_num_cmd_pkts--;
446 * If context is set, this was from a HCI raw socket
447 * and a record needs to be dropped from the sockbuf.
449 arg = M_GETCTX(m, void *);
454 IF_ENQUEUE(&unit->hci_cmdq, m);
456 if ((unit->hci_flags & BTF_XMIT_CMD) == 0)
457 (*unit->hci_start_cmd)(unit);
461 hci_output_acl(struct hci_unit *unit, struct mbuf *m)
465 DPRINTFN(10, "(%s) num_acl_pkts=%d\n", unit->hci_devname,
466 unit->hci_num_acl_pkts);
468 unit->hci_num_acl_pkts--;
471 IF_ENQUEUE(&unit->hci_acltxq, m);
474 if ((unit->hci_flags & BTF_XMIT_ACL) == 0)
475 (*unit->hci_start_acl)(unit);
479 hci_output_sco(struct hci_unit *unit, struct mbuf *m)
484 DPRINTFN(10, "(%s) num_sco_pkts=%d\n", unit->hci_devname,
485 unit->hci_num_sco_pkts);
487 unit->hci_num_sco_pkts--;
490 IF_ENQUEUE(&unit->hci_scotxq, m);
492 if ((unit->hci_flags & BTF_XMIT_SCO) == 0)
493 (*unit->hci_start_sco)(unit);
498 hci_complete_sco(struct hci_unit *unit, struct mbuf *m)
500 IF_ENQUEUE(&unit->hci_scodone, m);
502 netisr_queue(NETISR_BLUETOOTH,m);