1 /* $DragonFly: src/sys/netbt/hci_unit.c,v 1.2 2008/03/18 13:41:42 hasso Exp $ */
2 /* $OpenBSD: src/sys/netbt/hci_unit.c,v 1.8 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: hci_unit.c,v 1.9 2007/12/30 18:26:42 plunky 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/param.h>
37 #include <sys/device.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/endian.h>
47 #include <net/netisr.h>
49 #include <netbt/bluetooth.h>
50 #include <netbt/hci.h>
52 struct hci_unit_list hci_unit_list = TAILQ_HEAD_INITIALIZER(hci_unit_list);
55 * HCI Input Queue max lengths.
57 int hci_eventq_max = 20;
58 int hci_aclrxq_max = 50;
59 int hci_scorxq_max = 50;
60 int hci_cmdwait_max = 50;
61 int hci_scodone_max = 50;
64 * This is the default minimum command set supported by older
65 * devices. Anything conforming to 1.2 spec or later will get
66 * updated during init.
68 static const uint8_t hci_cmds_v10[HCI_COMMANDS_SIZE] = {
69 0xff, 0xff, 0xff, 0x01, 0xfe, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0x7f, 0x32, 0x03, 0xb8, 0xfe,
71 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
80 * bluetooth unit functions
84 hci_attach(const struct hci_if *hci_if, struct device *dev, uint16_t flags)
86 struct hci_unit *unit;
88 KKASSERT(dev != NULL);
89 KKASSERT(hci_if->enable != NULL);
90 KKASSERT(hci_if->disable != NULL);
91 KKASSERT(hci_if->output_cmd != NULL);
92 KKASSERT(hci_if->output_acl != NULL);
93 KKASSERT(hci_if->output_sco != NULL);
94 KKASSERT(hci_if->get_stats != NULL);
96 unit = kmalloc(sizeof(struct hci_unit), M_BLUETOOTH, M_ZERO | M_WAITOK);
97 KKASSERT(unit != NULL);
100 unit->hci_if = hci_if;
101 unit->hci_flags = flags;
103 lockinit(&unit->hci_devlock, "HCI device lock", 0, 0);
105 unit->hci_eventq.ifq_maxlen = hci_eventq_max;
106 unit->hci_aclrxq.ifq_maxlen = hci_aclrxq_max;
107 unit->hci_scorxq.ifq_maxlen = hci_scorxq_max;
108 unit->hci_cmdwait.ifq_maxlen = hci_cmdwait_max;
109 unit->hci_scodone.ifq_maxlen = hci_scodone_max;
111 TAILQ_INIT(&unit->hci_links);
112 LIST_INIT(&unit->hci_memos);
115 TAILQ_INSERT_TAIL(&hci_unit_list, unit, hci_next);
122 hci_detach(struct hci_unit *unit)
127 TAILQ_REMOVE(&hci_unit_list, unit, hci_next);
130 kfree(unit, M_BLUETOOTH);
134 hci_enable(struct hci_unit *unit)
139 * Bluetooth spec says that a device can accept one
140 * command on power up until they send a Command Status
141 * or Command Complete event with more information, but
142 * it seems that some devices cant and prefer to send a
143 * No-op Command Status packet when they are ready.
145 unit->hci_num_cmd_pkts = (unit->hci_flags & BTF_POWER_UP_NOOP) ? 0 : 1;
146 unit->hci_num_acl_pkts = 0;
147 unit->hci_num_sco_pkts = 0;
150 * only allow the basic packet types until
151 * the features report is in
153 unit->hci_acl_mask = HCI_PKT_DM1 | HCI_PKT_DH1;
154 unit->hci_packet_type = unit->hci_acl_mask;
156 memcpy(unit->hci_cmds, hci_cmds_v10, HCI_COMMANDS_SIZE);
157 err = (*unit->hci_if->enable)(unit->hci_dev);
161 unit->hci_flags |= BTF_RUNNING;
164 * Reset the device, this will trigger initialisation
168 unit->hci_flags |= BTF_INIT;
171 err = hci_send_cmd(unit, HCI_CMD_RESET, NULL, 0);
175 while (unit->hci_flags & BTF_INIT) {
176 err = tsleep(unit, PCATCH, "hciena", 5 * hz);
181 * "What If", while we were sleeping, the device
182 * was removed and detached? Ho Hum.
188 * Attach Bluetooth Device Hub
190 unit->hci_bthub = NULL;
192 unit->hci_bthub = device_add_child(unit->hci_softc, "bthub", -1);
193 if (!unit->hci_bthub) {
194 device_printf(unit->hci_softc, "Device creation failed\n");
198 DPRINTFN(10, "%s is added as child to %s\n",
199 device_get_nameunit(unit->hci_bthub),
200 device_get_nameunit(unit->hci_softc));
202 device_set_desc(unit->hci_bthub,"Bluetooth Device Hub");
204 device_set_ivars(unit->hci_bthub, &unit->hci_bdaddr);
206 device_probe_and_attach(unit->hci_bthub);
211 (*unit->hci_if->disable)(unit->hci_dev);
212 unit->hci_flags &= ~BTF_RUNNING;
219 hci_disable(struct hci_unit *unit)
221 struct hci_link *link, *next;
222 struct hci_memo *memo;
226 if (unit->hci_bthub) {
227 device_delete_child(unit->hci_softc, unit->hci_bthub);
228 unit->hci_bthub = NULL;
232 (*unit->hci_if->disable)(unit->hci_dev);
233 unit->hci_flags &= ~BTF_RUNNING;
236 * close down any links, take care to close SCO first since
237 * they may depend on ACL links.
239 for (acl = 0 ; acl < 2 ; acl++) {
240 next = TAILQ_FIRST(&unit->hci_links);
241 while ((link = next) != NULL) {
242 next = TAILQ_NEXT(link, hl_next);
243 if (acl || link->hl_type != HCI_LINK_ACL)
244 hci_link_free(link, ECONNABORTED);
248 while ((memo = LIST_FIRST(&unit->hci_memos)) != NULL)
251 /* (no need to hold hci_devlock, the driver is disabled) */
253 IF_DRAIN(&unit->hci_eventq);
254 unit->hci_eventqlen = 0;
256 IF_DRAIN(&unit->hci_aclrxq);
257 unit->hci_aclrxqlen = 0;
259 IF_DRAIN(&unit->hci_scorxq);
260 unit->hci_scorxqlen = 0;
262 IF_DRAIN(&unit->hci_cmdwait);
263 IF_DRAIN(&unit->hci_scodone);
267 hci_unit_lookup(bdaddr_t *addr)
269 struct hci_unit *unit;
271 TAILQ_FOREACH(unit, &hci_unit_list, hci_next) {
272 if ((unit->hci_flags & BTF_UP) == 0)
275 if (bdaddr_same(&unit->hci_bdaddr, addr))
283 * construct and queue a HCI command packet
286 hci_send_cmd(struct hci_unit *unit, uint16_t opcode, void *buf, uint8_t len)
291 KKASSERT(unit != NULL);
293 m = m_gethdr(MB_DONTWAIT, MT_DATA);
297 p = mtod(m, hci_cmd_hdr_t *);
298 p->type = HCI_CMD_PKT;
299 p->opcode = htole16(opcode);
301 m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
302 M_SETCTX(m, NULL); /* XXX is this needed? */
305 KKASSERT(buf != NULL);
307 m_copyback(m, sizeof(hci_cmd_hdr_t), len, buf);
308 if (m->m_pkthdr.len != (sizeof(hci_cmd_hdr_t) + len)) {
314 DPRINTFN(2, "(%s) opcode (%3.3x|%4.4x)\n",
315 device_get_nameunit(unit->hci_dev),
316 HCI_OGF(opcode), HCI_OCF(opcode));
319 if (unit->hci_num_cmd_pkts == 0)
320 IF_ENQUEUE(&unit->hci_cmdwait, m);
322 hci_output_cmd(unit, m);
328 * Incoming packet processing. Since the code is single threaded
329 * in any case (IPL_SOFTNET), we handle it all in one interrupt function
330 * picking our way through more important packets first so that hopefully
331 * we will never get clogged up with bulk data.
336 struct hci_unit *unit = arg;
340 lockmgr(&unit->hci_devlock, LK_EXCLUSIVE);
342 if (unit->hci_eventqlen > 0) {
343 IF_DEQUEUE(&unit->hci_eventq, m);
344 unit->hci_eventqlen--;
345 lockmgr(&unit->hci_devlock, LK_RELEASE);
349 DPRINTFN(10, "(%s) recv event, len = %d\n",
350 device_get_nameunit(unit->hci_dev), m->m_pkthdr.len);
352 m->m_flags |= IFF_LINK0; /* mark incoming packet */
359 if (unit->hci_scorxqlen > 0) {
360 IF_DEQUEUE(&unit->hci_scorxq, m);
361 unit->hci_scorxqlen--;
362 lockmgr(&unit->hci_devlock, LK_RELEASE);
365 DPRINTFN(10, "(%s) recv SCO, len = %d\n",
366 device_get_nameunit(unit->hci_dev), m->m_pkthdr.len);
368 m->m_flags |= IFF_LINK0; /* mark incoming packet */
370 hci_sco_recv(m, unit);
375 if (unit->hci_aclrxqlen > 0) {
376 IF_DEQUEUE(&unit->hci_aclrxq, m);
377 unit->hci_aclrxqlen--;
378 lockmgr(&unit->hci_devlock, LK_RELEASE);
382 DPRINTFN(10, "(%s) recv ACL, len = %d\n",
383 device_get_nameunit(unit->hci_dev), m->m_pkthdr.len);
385 m->m_flags |= IFF_LINK0; /* mark incoming packet */
387 hci_acl_recv(m, unit);
392 IF_DEQUEUE(&unit->hci_scodone, m);
394 struct hci_link *link;
395 lockmgr(&unit->hci_devlock, LK_RELEASE);
397 DPRINTFN(11, "(%s) complete SCO\n",
398 device_get_nameunit(unit->hci_dev));
400 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
401 if (link == M_GETCTX(m, struct hci_link *)) {
402 hci_sco_complete(link, 1);
407 unit->hci_num_sco_pkts++;
413 lockmgr(&unit->hci_devlock, LK_RELEASE);
415 DPRINTFN(10, "done\n");
418 /**********************************************************************
422 * input & complete routines will be called from device drivers,
423 * possibly in interrupt context. We return success or failure to
424 * enable proper accounting but we own the mbuf.
428 hci_input_event(struct hci_unit *unit, struct mbuf *m)
432 lockmgr(&unit->hci_devlock, LK_EXCLUSIVE);
434 if (unit->hci_eventqlen > hci_eventq_max) {
435 DPRINTF("(%s) dropped event packet.\n",
436 device_get_nameunit(unit->hci_dev));
440 unit->hci_eventqlen++;
441 IF_ENQUEUE(&unit->hci_eventq, m);
442 netisr_queue(NETISR_BLUETOOTH, m);
445 lockmgr(&unit->hci_devlock, LK_RELEASE);
451 hci_input_acl(struct hci_unit *unit, struct mbuf *m)
455 lockmgr(&unit->hci_devlock, LK_EXCLUSIVE);
456 if (unit->hci_aclrxqlen > hci_aclrxq_max) {
457 DPRINTF("(%s) dropped ACL packet.\n",
458 device_get_nameunit(unit->hci_dev));
462 unit->hci_aclrxqlen++;
463 IF_ENQUEUE(&unit->hci_aclrxq, m);
464 netisr_queue(NETISR_BLUETOOTH, m);
467 lockmgr(&unit->hci_devlock, LK_RELEASE);
473 hci_input_sco(struct hci_unit *unit, struct mbuf *m)
477 lockmgr(&unit->hci_devlock, LK_EXCLUSIVE);
478 if (unit->hci_scorxqlen > hci_scorxq_max) {
479 DPRINTF("(%s) dropped SCO packet.\n",
480 device_get_nameunit(unit->hci_dev));
484 unit->hci_scorxqlen++;
485 IF_ENQUEUE(&unit->hci_scorxq, m);
486 netisr_queue(NETISR_BLUETOOTH, m);
489 lockmgr(&unit->hci_devlock, LK_RELEASE);
495 hci_output_cmd(struct hci_unit *unit, struct mbuf *m)
501 DPRINTFN(10, "(%s) num_cmd_pkts=%d\n",
502 device_get_nameunit(unit->hci_dev), unit->hci_num_cmd_pkts);
504 unit->hci_num_cmd_pkts--;
507 * If context is set, this was from a HCI raw socket
508 * and a record needs to be dropped from the sockbuf.
510 arg = M_GETCTX(m, void *);
514 (*unit->hci_if->output_cmd)(unit->hci_dev, m);
518 hci_output_acl(struct hci_unit *unit, struct mbuf *m)
522 DPRINTFN(10, "(%s) num_acl_pkts=%d\n",
523 device_get_nameunit(unit->hci_dev), unit->hci_num_acl_pkts);
525 unit->hci_num_acl_pkts--;
527 (*unit->hci_if->output_acl)(unit->hci_dev, m);
531 hci_output_sco(struct hci_unit *unit, struct mbuf *m)
535 DPRINTFN(10, "(%s) num_sco_pkts=%d\n",
536 device_get_nameunit(unit->hci_dev), unit->hci_num_sco_pkts);
538 unit->hci_num_sco_pkts--;
540 (*unit->hci_if->output_sco)(unit->hci_dev, m);
544 hci_complete_sco(struct hci_unit *unit, struct mbuf *m)
546 lockmgr(&unit->hci_devlock, LK_EXCLUSIVE);
547 IF_ENQUEUE(&unit->hci_scodone, m);
548 netisr_queue(NETISR_BLUETOOTH,m);
549 lockmgr(&unit->hci_devlock, LK_RELEASE);