1 /*
2  * ng_hci_var.h
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $Id: ng_hci_var.h,v 1.3 2003/04/26 22:35:21 max Exp $
33  * $FreeBSD$
34  */
35 
36 #ifndef _NETGRAPH_HCI_VAR_H_
37 #define _NETGRAPH_HCI_VAR_H_
38 
39 /* MALLOC decalation */
40 #ifdef NG_SEPARATE_MALLOC
41 MALLOC_DECLARE(M_NETGRAPH_HCI);
42 #else
43 #define M_NETGRAPH_HCI M_NETGRAPH
44 #endif /* NG_SEPARATE_MALLOC */
45 
46 /* Debug */
47 #define	NG_HCI_ALERT	if (unit->debug >= NG_HCI_ALERT_LEVEL) printf
48 #define	NG_HCI_ERR	if (unit->debug >= NG_HCI_ERR_LEVEL)   printf
49 #define	NG_HCI_WARN	if (unit->debug >= NG_HCI_WARN_LEVEL)  printf
50 #define	NG_HCI_INFO	if (unit->debug >= NG_HCI_INFO_LEVEL)  printf
51 
52 /* Wrapper around m_pullup */
53 #define NG_HCI_M_PULLUP(m, s) 				\
54 	do { 						\
55 		if ((m)->m_len < (s)) 			\
56 			(m) = m_pullup((m), (s)); 	\
57 		if ((m) == NULL) 			\
58 			NG_HCI_ALERT("%s: %s - m_pullup(%zd) failed\n", \
59 				__func__, NG_NODE_NAME(unit->node), (s)); \
60 	} while (0)
61 
62 /*
63  * Unit hardware buffer descriptor
64  */
65 
66 typedef struct ng_hci_unit_buff {
67 	u_int8_t			cmd_free; /* space available (cmds) */
68 
69 	u_int8_t			sco_size; /* max. size of one packet */
70 	u_int16_t			sco_pkts; /* size of buffer (packets) */
71 	u_int16_t			sco_free; /* space available (packets)*/
72 
73 	u_int16_t			acl_size; /* max. size of one packet */
74 	u_int16_t			acl_pkts; /* size of buffer (packets) */
75 	u_int16_t			acl_free; /* space available (packets)*/
76 } ng_hci_unit_buff_t;
77 
78 /*
79  * These macro's must be used everywhere in the code. So if extra locking
80  * is required later, it can be added without much troubles.
81  */
82 
83 #define NG_HCI_BUFF_CMD_SET(b, v)	(b).cmd_free = (v)
84 #define NG_HCI_BUFF_CMD_GET(b, v)	(v) = (b).cmd_free
85 #define NG_HCI_BUFF_CMD_USE(b, v)	(b).cmd_free -= (v)
86 
87 #define NG_HCI_BUFF_ACL_USE(b, v)	(b).acl_free -= (v)
88 #define NG_HCI_BUFF_ACL_FREE(b, v) 			\
89 	do { 						\
90 		(b).acl_free += (v);			\
91 		if ((b).acl_free > (b).acl_pkts) 	\
92 			(b).acl_free = (b).acl_pkts; 	\
93 	} while (0)
94 #define NG_HCI_BUFF_ACL_AVAIL(b, v)	(v) = (b).acl_free
95 #define NG_HCI_BUFF_ACL_TOTAL(b, v)	(v) = (b).acl_pkts
96 #define NG_HCI_BUFF_ACL_SIZE(b, v)	(v) = (b).acl_size
97 #define NG_HCI_BUFF_ACL_SET(b, n, s, f) 		\
98 	do { 						\
99 		(b).acl_free = (f); 			\
100 		(b).acl_size = (s); 			\
101 		(b).acl_pkts = (n); 			\
102 	} while (0)
103 
104 #define NG_HCI_BUFF_SCO_USE(b, v)	(b).sco_free -= (v)
105 #define NG_HCI_BUFF_SCO_FREE(b, v) 			\
106 	do { 						\
107 		(b).sco_free += (v); 			\
108 		if ((b).sco_free > (b).sco_pkts) 	\
109 			(b).sco_free = (b).sco_pkts; 	\
110 	} while (0)
111 #define NG_HCI_BUFF_SCO_AVAIL(b, v)	(v) = (b).sco_free
112 #define NG_HCI_BUFF_SCO_TOTAL(b, v)	(v) = (b).sco_pkts
113 #define NG_HCI_BUFF_SCO_SIZE(b, v)	(v) = (b).sco_size
114 #define NG_HCI_BUFF_SCO_SET(b, n, s, f) 		\
115 	do { 						\
116 		(b).sco_free = (f); 			\
117 		(b).sco_size = (s); 			\
118 		(b).sco_pkts = (n); 			\
119 	} while (0)
120 
121 /*
122  * Unit (Node private)
123  */
124 
125 struct ng_hci_unit_con;
126 struct ng_hci_neighbor;
127 
128 typedef struct ng_hci_unit {
129 	node_p				node;           /* node ptr */
130 
131 	ng_hci_node_debug_ep		debug;          /* debug level */
132 	ng_hci_node_state_ep		state;          /* unit state */
133 
134 	bdaddr_t			bdaddr;         /* unit address */
135 	u_int8_t			features[NG_HCI_FEATURES_SIZE];
136 					                /* LMP features */
137 
138 	ng_hci_node_link_policy_mask_ep	link_policy_mask; /* link policy mask */
139 	ng_hci_node_packet_mask_ep	packet_mask;	/* packet mask */
140 	ng_hci_node_role_switch_ep	role_switch;	/* role switch */
141 
142 	ng_hci_node_stat_ep		stat;           /* statistic */
143 #define NG_HCI_STAT_CMD_SENT(s)		(s).cmd_sent ++
144 #define NG_HCI_STAT_EVNT_RECV(s)	(s).evnt_recv ++
145 #define NG_HCI_STAT_ACL_SENT(s, n)	(s).acl_sent += (n)
146 #define NG_HCI_STAT_ACL_RECV(s)		(s).acl_recv ++
147 #define NG_HCI_STAT_SCO_SENT(s, n)	(s).sco_sent += (n)
148 #define NG_HCI_STAT_SCO_RECV(s)		(s).sco_recv ++
149 #define NG_HCI_STAT_BYTES_SENT(s, b)	(s).bytes_sent += (b)
150 #define NG_HCI_STAT_BYTES_RECV(s, b)	(s).bytes_recv += (b)
151 #define NG_HCI_STAT_RESET(s)		bzero(&(s), sizeof((s)))
152 
153 	ng_hci_unit_buff_t		buffer;         /* buffer info */
154 
155 	struct callout			cmd_timo;       /* command timeout */
156 	ng_bt_mbufq_t			cmdq;           /* command queue */
157 #define NG_HCI_CMD_QUEUE_LEN		12		/* max. size of cmd q */
158 
159 	hook_p				drv;            /* driver hook */
160 	hook_p				acl;            /* upstream hook */
161 	hook_p				sco;            /* upstream hook */
162 	hook_p				raw;            /* upstream hook */
163 
164 	LIST_HEAD(, ng_hci_unit_con)	con_list;       /* connections */
165 	LIST_HEAD(, ng_hci_neighbor)	neighbors;      /* unit neighbors */
166 } ng_hci_unit_t;
167 typedef ng_hci_unit_t *			ng_hci_unit_p;
168 
169 /*
170  * Unit connection descriptor
171  */
172 
173 typedef struct ng_hci_unit_con {
174 	ng_hci_unit_p			unit;            /* pointer back */
175 
176 	u_int16_t			state;           /* con. state */
177 	u_int16_t			flags;           /* con. flags */
178 #define NG_HCI_CON_TIMEOUT_PENDING		(1 << 0)
179 #define NG_HCI_CON_NOTIFY_ACL			(1 << 1)
180 #define NG_HCI_CON_NOTIFY_SCO			(1 << 2)
181 
182 	bdaddr_t			bdaddr;          /* remote address */
183 	u_int16_t			con_handle;      /* con. handle */
184 
185 	u_int8_t			link_type;       /* ACL or SCO */
186 	u_int8_t			encryption_mode; /* none, p2p, ... */
187 	u_int8_t			mode;            /* ACTIVE, HOLD ... */
188 	u_int8_t			role;            /* MASTER/SLAVE */
189 
190 	struct callout			con_timo;        /* con. timeout */
191 
192 	int				pending;         /* # of data pkts */
193 	ng_bt_itemq_t			conq;            /* con. queue */
194 
195 	LIST_ENTRY(ng_hci_unit_con)	next;            /* next */
196 } ng_hci_unit_con_t;
197 typedef ng_hci_unit_con_t *		ng_hci_unit_con_p;
198 
199 /*
200  * Unit's neighbor descriptor.
201  * Neighbor is a remote unit that responded to our inquiry.
202  */
203 
204 typedef struct ng_hci_neighbor {
205 	struct timeval			updated;	/* entry was updated */
206 
207 	bdaddr_t			bdaddr;         /* address */
208 	u_int8_t			features[NG_HCI_FEATURES_SIZE];
209 					                /* LMP features */
210 	u_int8_t 			addrtype;	/*Address Type*/
211 
212 	u_int8_t			page_scan_rep_mode; /* PS rep. mode */
213 	u_int8_t			page_scan_mode; /* page scan mode */
214 	u_int16_t			clock_offset;   /* clock offset */
215 	uint8_t				extinq_size;
216 	uint8_t				extinq_data[NG_HCI_EXTINQ_MAX];
217 	LIST_ENTRY(ng_hci_neighbor)	next;
218 } ng_hci_neighbor_t;
219 typedef ng_hci_neighbor_t *		ng_hci_neighbor_p;
220 
221 #endif /* ndef _NETGRAPH_HCI_VAR_H_ */
222