Path: blob/main/sys/netgraph/bluetooth/hci/ng_hci_misc.c
34814 views
/*1* ng_hci_misc.c2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) Maksim Yevmenkin <[email protected]>8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*31* $Id: ng_hci_misc.c,v 1.5 2003/09/08 18:57:51 max Exp $32*/3334#include <sys/param.h>35#include <sys/systm.h>36#include <sys/kernel.h>37#include <sys/malloc.h>38#include <sys/mbuf.h>39#include <sys/queue.h>40#include <netgraph/ng_message.h>41#include <netgraph/netgraph.h>42#include <netgraph/bluetooth/include/ng_bluetooth.h>43#include <netgraph/bluetooth/include/ng_hci.h>44#include <netgraph/bluetooth/hci/ng_hci_var.h>45#include <netgraph/bluetooth/hci/ng_hci_cmds.h>46#include <netgraph/bluetooth/hci/ng_hci_evnt.h>47#include <netgraph/bluetooth/hci/ng_hci_ulpi.h>48#include <netgraph/bluetooth/hci/ng_hci_misc.h>4950/******************************************************************************51******************************************************************************52** Utility routines53******************************************************************************54******************************************************************************/5556/*57* Give packet to RAW hook58* Assumes input mbuf is read only.59*/6061void62ng_hci_mtap(ng_hci_unit_p unit, struct mbuf *m0)63{64struct mbuf *m = NULL;65int error = 0;6667if (unit->raw != NULL && NG_HOOK_IS_VALID(unit->raw)) {68m = m_dup(m0, M_NOWAIT);69if (m != NULL)70NG_SEND_DATA_ONLY(error, unit->raw, m);7172if (error != 0)73NG_HCI_INFO(74"%s: %s - Could not forward packet, error=%d\n",75__func__, NG_NODE_NAME(unit->node), error);76}77} /* ng_hci_mtap */7879/*80* Send notification to the upper layer's81*/8283void84ng_hci_node_is_up(node_p node, hook_p hook, void *arg1, int arg2)85{86ng_hci_unit_p unit = NULL;87struct ng_mesg *msg = NULL;88ng_hci_node_up_ep *ep = NULL;89int error;9091if (node == NULL || NG_NODE_NOT_VALID(node) ||92hook == NULL || NG_HOOK_NOT_VALID(hook))93return;9495unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);96if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY)97return;9899if (hook != unit->acl && hook != unit->sco)100return;101102NG_MKMESSAGE(msg,NGM_HCI_COOKIE,NGM_HCI_NODE_UP,sizeof(*ep),M_NOWAIT);103if (msg != NULL) {104ep = (ng_hci_node_up_ep *)(msg->data);105106if (hook == unit->acl) {107NG_HCI_BUFF_ACL_SIZE(unit->buffer, ep->pkt_size);108NG_HCI_BUFF_ACL_TOTAL(unit->buffer, ep->num_pkts);109} else {110NG_HCI_BUFF_SCO_SIZE(unit->buffer, ep->pkt_size);111NG_HCI_BUFF_SCO_TOTAL(unit->buffer, ep->num_pkts);112}113114bcopy(&unit->bdaddr, &ep->bdaddr, sizeof(ep->bdaddr));115116NG_SEND_MSG_HOOK(error, node, msg, hook, 0);117} else118error = ENOMEM;119120if (error != 0)121NG_HCI_INFO(122"%s: %s - failed to send NODE_UP message to hook \"%s\", error=%d\n",123__func__, NG_NODE_NAME(unit->node),124NG_HOOK_NAME(hook), error);125} /* ng_hci_node_is_up */126127/*128* Clean unit (helper)129*/130131void132ng_hci_unit_clean(ng_hci_unit_p unit, int reason)133{134int size;135136/* Drain command queue */137if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)138ng_hci_command_untimeout(unit);139140NG_BT_MBUFQ_DRAIN(&unit->cmdq);141NG_HCI_BUFF_CMD_SET(unit->buffer, 1);142143/* Clean up connection list */144while (!LIST_EMPTY(&unit->con_list)) {145ng_hci_unit_con_p con = LIST_FIRST(&unit->con_list);146147/* Remove all timeouts (if any) */148if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)149ng_hci_con_untimeout(con);150151/*152* Notify upper layer protocol and destroy connection153* descriptor. Do not really care about the result.154*/155156ng_hci_lp_discon_ind(con, reason);157ng_hci_free_con(con);158}159160NG_HCI_BUFF_ACL_TOTAL(unit->buffer, size);161NG_HCI_BUFF_ACL_FREE(unit->buffer, size);162163NG_HCI_BUFF_SCO_TOTAL(unit->buffer, size);164NG_HCI_BUFF_SCO_FREE(unit->buffer, size);165166/* Clean up neighbors list */167ng_hci_flush_neighbor_cache(unit);168} /* ng_hci_unit_clean */169170/*171* Allocate and link new unit neighbor cache entry172*/173174ng_hci_neighbor_p175ng_hci_new_neighbor(ng_hci_unit_p unit)176{177ng_hci_neighbor_p n = NULL;178179n = malloc(sizeof(*n), M_NETGRAPH_HCI,180M_NOWAIT | M_ZERO);181if (n != NULL) {182getmicrotime(&n->updated);183LIST_INSERT_HEAD(&unit->neighbors, n, next);184}185186return (n);187} /* ng_hci_new_neighbor */188189/*190* Free unit neighbor cache entry191*/192193void194ng_hci_free_neighbor(ng_hci_neighbor_p n)195{196LIST_REMOVE(n, next);197bzero(n, sizeof(*n));198free(n, M_NETGRAPH_HCI);199} /* ng_hci_free_neighbor */200201/*202* Flush neighbor cache203*/204205void206ng_hci_flush_neighbor_cache(ng_hci_unit_p unit)207{208while (!LIST_EMPTY(&unit->neighbors))209ng_hci_free_neighbor(LIST_FIRST(&unit->neighbors));210} /* ng_hci_flush_neighbor_cache */211212/*213* Lookup unit in neighbor cache214*/215216ng_hci_neighbor_p217ng_hci_get_neighbor(ng_hci_unit_p unit, bdaddr_p bdaddr,int link_type)218{219ng_hci_neighbor_p n = NULL;220221for (n = LIST_FIRST(&unit->neighbors); n != NULL; ) {222ng_hci_neighbor_p nn = LIST_NEXT(n, next);223224if (!ng_hci_neighbor_stale(n)) {225if (n->addrtype == link_type &&226bcmp(&n->bdaddr, bdaddr, sizeof(*bdaddr)) == 0)227break;228} else229ng_hci_free_neighbor(n); /* remove old entry */230231n = nn;232}233234return (n);235} /* ng_hci_get_neighbor */236237/*238* Check if neighbor entry is stale239*/240241int242ng_hci_neighbor_stale(ng_hci_neighbor_p n)243{244struct timeval now;245246getmicrotime(&now);247248return (now.tv_sec - n->updated.tv_sec > bluetooth_hci_max_neighbor_age());249} /* ng_hci_neighbor_stale */250251/*252* Allocate and link new connection descriptor253*/254255ng_hci_unit_con_p256ng_hci_new_con(ng_hci_unit_p unit, int link_type)257{258ng_hci_unit_con_p con = NULL;259int num_pkts;260static int fake_con_handle = 0x0f00;261262con = malloc(sizeof(*con), M_NETGRAPH_HCI,263M_NOWAIT | M_ZERO);264if (con != NULL) {265con->unit = unit;266con->state = NG_HCI_CON_CLOSED;267268/*269* XXX270*271* Assign fake connection handle to the connection descriptor.272* Bluetooth specification marks 0x0f00 - 0x0fff connection273* handles as reserved. We need this fake connection handles274* for timeouts. Connection handle will be passed as argument275* to timeout so when timeout happens we can find the right276* connection descriptor. We can not pass pointers, because277* timeouts are external (to Netgraph) events and there might278* be a race when node/hook goes down and timeout event already279* went into node's queue280*/281282con->con_handle = fake_con_handle ++;283if (fake_con_handle > 0x0fff)284fake_con_handle = 0x0f00;285286con->link_type = link_type;287288if (con->link_type != NG_HCI_LINK_SCO)289NG_HCI_BUFF_ACL_TOTAL(unit->buffer, num_pkts);290else291NG_HCI_BUFF_SCO_TOTAL(unit->buffer, num_pkts);292293NG_BT_ITEMQ_INIT(&con->conq, num_pkts);294295ng_callout_init(&con->con_timo);296297LIST_INSERT_HEAD(&unit->con_list, con, next);298}299300return (con);301} /* ng_hci_new_con */302303/*304* Free connection descriptor305*/306307void308ng_hci_free_con(ng_hci_unit_con_p con)309{310LIST_REMOVE(con, next);311312/*313* If we have pending packets then assume that Host Controller has314* flushed these packets and we can free them too315*/316317if (con->link_type != NG_HCI_LINK_SCO)318NG_HCI_BUFF_ACL_FREE(con->unit->buffer, con->pending);319else320NG_HCI_BUFF_SCO_FREE(con->unit->buffer, con->pending);321322NG_BT_ITEMQ_DESTROY(&con->conq);323324bzero(con, sizeof(*con));325free(con, M_NETGRAPH_HCI);326} /* ng_hci_free_con */327328/*329* Lookup connection for given unit and connection handle.330*/331332ng_hci_unit_con_p333ng_hci_con_by_handle(ng_hci_unit_p unit, int con_handle)334{335ng_hci_unit_con_p con = NULL;336337LIST_FOREACH(con, &unit->con_list, next)338if (con->con_handle == con_handle)339break;340341return (con);342} /* ng_hci_con_by_handle */343344/*345* Lookup connection for given unit, link type and remove unit address346*/347348ng_hci_unit_con_p349ng_hci_con_by_bdaddr(ng_hci_unit_p unit, bdaddr_p bdaddr, int link_type)350{351ng_hci_unit_con_p con = NULL;352353LIST_FOREACH(con, &unit->con_list, next)354if (con->link_type == link_type &&355bcmp(&con->bdaddr, bdaddr, sizeof(bdaddr_t)) == 0)356break;357358return (con);359} /* ng_hci_con_by_bdaddr */360361/*362* Set HCI command timeout363* XXX FIXME: check return code from ng_callout364*/365366int367ng_hci_command_timeout(ng_hci_unit_p unit)368{369if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)370panic(371"%s: %s - Duplicated command timeout!\n", __func__, NG_NODE_NAME(unit->node));372373unit->state |= NG_HCI_UNIT_COMMAND_PENDING;374ng_callout(&unit->cmd_timo, unit->node, NULL,375bluetooth_hci_command_timeout(),376ng_hci_process_command_timeout, NULL, 0);377378return (0);379} /* ng_hci_command_timeout */380381/*382* Unset HCI command timeout383*/384385int386ng_hci_command_untimeout(ng_hci_unit_p unit)387{388if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))389panic(390"%s: %s - No command timeout!\n", __func__, NG_NODE_NAME(unit->node));391392if (ng_uncallout(&unit->cmd_timo, unit->node) < 1)393return (ETIMEDOUT);394395unit->state &= ~NG_HCI_UNIT_COMMAND_PENDING;396397return (0);398} /* ng_hci_command_untimeout */399400/*401* Set HCI connection timeout402* XXX FIXME: check return code from ng_callout403*/404405int406ng_hci_con_timeout(ng_hci_unit_con_p con)407{408if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)409panic(410"%s: %s - Duplicated connection timeout!\n",411__func__, NG_NODE_NAME(con->unit->node));412413con->flags |= NG_HCI_CON_TIMEOUT_PENDING;414ng_callout(&con->con_timo, con->unit->node, NULL,415bluetooth_hci_connect_timeout(),416ng_hci_process_con_timeout, NULL,417con->con_handle);418419return (0);420} /* ng_hci_con_timeout */421422/*423* Unset HCI connection timeout424*/425426int427ng_hci_con_untimeout(ng_hci_unit_con_p con)428{429if (!(con->flags & NG_HCI_CON_TIMEOUT_PENDING))430panic(431"%s: %s - No connection timeout!\n", __func__, NG_NODE_NAME(con->unit->node));432433if (ng_uncallout(&con->con_timo, con->unit->node) < 1)434return (ETIMEDOUT);435436con->flags &= ~NG_HCI_CON_TIMEOUT_PENDING;437438return (0);439} /* ng_hci_con_untimeout */440441#if 0442/*443* Convert numeric error code/reason to a string444*/445446char const * const447ng_hci_str_error(u_int16_t code)448{449#define LAST_ERROR_CODE ((sizeof(s)/sizeof(s[0]))-1)450static char const * const s[] = {451/* 0x00 */ "No error",452/* 0x01 */ "Unknown HCI command",453/* 0x02 */ "No connection",454/* 0x03 */ "Hardware failure",455/* 0x04 */ "Page timeout",456/* 0x05 */ "Authentication failure",457/* 0x06 */ "Key missing",458/* 0x07 */ "Memory full",459/* 0x08 */ "Connection timeout",460/* 0x09 */ "Max number of connections",461/* 0x0a */ "Max number of SCO connections to a unit",462/* 0x0b */ "ACL connection already exists",463/* 0x0c */ "Command disallowed",464/* 0x0d */ "Host rejected due to limited resources",465/* 0x0e */ "Host rejected due to securiity reasons",466/* 0x0f */ "Host rejected due to remote unit is a personal unit",467/* 0x10 */ "Host timeout",468/* 0x11 */ "Unsupported feature or parameter value",469/* 0x12 */ "Invalid HCI command parameter",470/* 0x13 */ "Other end terminated connection: User ended connection",471/* 0x14 */ "Other end terminated connection: Low resources",472/* 0x15 */ "Other end terminated connection: About to power off",473/* 0x16 */ "Connection terminated by local host",474/* 0x17 */ "Repeated attempts",475/* 0x18 */ "Pairing not allowed",476/* 0x19 */ "Unknown LMP PDU",477/* 0x1a */ "Unsupported remote feature",478/* 0x1b */ "SCO offset rejected",479/* 0x1c */ "SCO interval rejected",480/* 0x1d */ "SCO air mode rejected",481/* 0x1e */ "Invalid LMP parameters",482/* 0x1f */ "Unspecified error",483/* 0x20 */ "Unsupported LMP parameter value",484/* 0x21 */ "Role change not allowed",485/* 0x22 */ "LMP response timeout",486/* 0x23 */ "LMP error transaction collision",487/* 0x24 */ "LMP PSU not allowed",488/* 0x25 */ "Encryption mode not acceptable",489/* 0x26 */ "Unit key used",490/* 0x27 */ "QoS is not supported",491/* 0x28 */ "Instant passed",492/* 0x29 */ "Pairing with unit key not supported",493/* 0x2a */ "Different Transaction Collision",494/* 0x2b */ "Unknown error (Reserved for future use)",495/* 0x2c */ "QoS Unacceptable Parameter",496/* 0x2d */ "QoS Rejected",497/* 0x2e */ "Channel Classification Not Supported",498/* 0x2f */ "Insufficient Security",499/* 0x30 */ "Parameter Out Of Mandatory Range",500/* 0x31 */ "Unknown error (Reserved for future use)",501/* 0x32 */ "Role Switch Pending",502/* 0x33 */ "Unknown error (Reserved for future use)",503/* 0x34 */ "Reserved Slot Violation",504/* 0x35 */ "Role Switch Failed",505/* 0x36 */ "Extended Inquiry Response Too Large",506/* 0x37 */ "Secure Simple Pairing Not Supported By Host",507/* 0x38 */ "Host Busy - Pairing",508/* 0x39 */ "Connection Rejected due to No Suitable Channel Found",509/* 0x3a */ "Controller Busy",510/* 0x3b */ "Unacceptable Connection Parameters",511/* 0x3c */ "Advertising Timeout",512/* 0x3d */ "Connection Terminated due to MIC Failure",513/* 0x3e */ "Connection Failed to be Established / Synchronization Timeout",514/* 0x3f */ "MAC Connection Failed",515/* 0x40 */ "Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging",516/* 0x41 */ "Type0 Submap Not Defined",517/* 0x42 */ "Unknown Advertising Identifier",518/* 0x43 */ "Limit Reached",519/* 0x44 */ "Operation Cancelled by Host",520/* 0x45 */ "Packet Too Long",521/* SHOULD ALWAYS BE LAST */ "Unknown error"522};523524return ((code >= LAST_ERROR_CODE)? s[LAST_ERROR_CODE] : s[code]);525} /* ng_hci_str_error */526#endif527528529