xref: /freebsd/sys/netgraph/bluetooth/hci/ng_hci_var.h (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*
2  * ng_hci_var.h
3  *
4  * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: ng_hci_var.h,v 1.3 2003/04/26 22:35:21 max Exp $
29  * $FreeBSD$
30  */
31 
32 #ifndef _NETGRAPH_HCI_VAR_H_
33 #define _NETGRAPH_HCI_VAR_H_
34 
35 /* MALLOC decalation */
36 #ifdef NG_SEPARATE_MALLOC
37 MALLOC_DECLARE(M_NETGRAPH_HCI);
38 #else
39 #define M_NETGRAPH_HCI M_NETGRAPH
40 #endif /* NG_SEPARATE_MALLOC */
41 
42 /* Debug */
43 #define	NG_HCI_ALERT	if (unit->debug >= NG_HCI_ALERT_LEVEL) printf
44 #define	NG_HCI_ERR	if (unit->debug >= NG_HCI_ERR_LEVEL)   printf
45 #define	NG_HCI_WARN	if (unit->debug >= NG_HCI_WARN_LEVEL)  printf
46 #define	NG_HCI_INFO	if (unit->debug >= NG_HCI_INFO_LEVEL)  printf
47 
48 /* Wrapper around m_pullup */
49 #define NG_HCI_M_PULLUP(m, s) 				\
50 	do { 						\
51 		if ((m)->m_len < (s)) 			\
52 			(m) = m_pullup((m), (s)); 	\
53 		if ((m) == NULL) 			\
54 			NG_HCI_ALERT("%s: %s - m_pullup(%zd) failed\n", \
55 				__func__, NG_NODE_NAME(unit->node), (s)); \
56 	} while (0)
57 
58 /*
59  * Unit hardware buffer descriptor
60  */
61 
62 typedef struct ng_hci_unit_buff {
63 	u_int8_t			cmd_free; /* space available (cmds) */
64 
65 	u_int8_t			sco_size; /* max. size of one packet */
66 	u_int16_t			sco_pkts; /* size of buffer (packets) */
67 	u_int16_t			sco_free; /* space available (packets)*/
68 
69 	u_int16_t			acl_size; /* max. size of one packet */
70 	u_int16_t			acl_pkts; /* size of buffer (packets) */
71 	u_int16_t			acl_free; /* space available (packets)*/
72 } ng_hci_unit_buff_t;
73 
74 /*
75  * These macro's must be used everywhere in the code. So if extra locking
76  * is required later, it can be added without much troubles.
77  */
78 
79 #define NG_HCI_BUFF_CMD_SET(b, v)	(b).cmd_free = (v)
80 #define NG_HCI_BUFF_CMD_GET(b, v)	(v) = (b).cmd_free
81 #define NG_HCI_BUFF_CMD_USE(b, v)	(b).cmd_free -= (v)
82 
83 #define NG_HCI_BUFF_ACL_USE(b, v)	(b).acl_free -= (v)
84 #define NG_HCI_BUFF_ACL_FREE(b, v) 			\
85 	do { 						\
86 		(b).acl_free += (v);			\
87 		if ((b).acl_free > (b).acl_pkts) 	\
88 			(b).acl_free = (b).acl_pkts; 	\
89 	} while (0)
90 #define NG_HCI_BUFF_ACL_AVAIL(b, v)	(v) = (b).acl_free
91 #define NG_HCI_BUFF_ACL_TOTAL(b, v)	(v) = (b).acl_pkts
92 #define NG_HCI_BUFF_ACL_SIZE(b, v)	(v) = (b).acl_size
93 #define NG_HCI_BUFF_ACL_SET(b, n, s, f) 		\
94 	do { 						\
95 		(b).acl_free = (f); 			\
96 		(b).acl_size = (s); 			\
97 		(b).acl_pkts = (n); 			\
98 	} while (0)
99 
100 #define NG_HCI_BUFF_SCO_USE(b, v)	(b).sco_free -= (v)
101 #define NG_HCI_BUFF_SCO_FREE(b, v) 			\
102 	do { 						\
103 		(b).sco_free += (v); 			\
104 		if ((b).sco_free > (b).sco_pkts) 	\
105 			(b).sco_free = (b).sco_pkts; 	\
106 	} while (0)
107 #define NG_HCI_BUFF_SCO_AVAIL(b, v)	(v) = (b).sco_free
108 #define NG_HCI_BUFF_SCO_TOTAL(b, v)	(v) = (b).sco_pkts
109 #define NG_HCI_BUFF_SCO_SIZE(b, v)	(v) = (b).sco_size
110 #define NG_HCI_BUFF_SCO_SET(b, n, s, f) 		\
111 	do { 						\
112 		(b).sco_free = (f); 			\
113 		(b).sco_size = (s); 			\
114 		(b).sco_pkts = (n); 			\
115 	} while (0)
116 
117 /*
118  * Unit (Node private)
119  */
120 
121 struct ng_hci_unit_con;
122 struct ng_hci_neighbor;
123 
124 typedef struct ng_hci_unit {
125 	node_p				node;           /* node ptr */
126 
127 	ng_hci_node_debug_ep		debug;          /* debug level */
128 	ng_hci_node_state_ep		state;          /* unit state */
129 
130 	bdaddr_t			bdaddr;         /* unit address */
131 	u_int8_t			features[NG_HCI_FEATURES_SIZE];
132 					                /* LMP features */
133 
134 	ng_hci_node_link_policy_mask_ep	link_policy_mask; /* link policy mask */
135 	ng_hci_node_packet_mask_ep	packet_mask;	/* packet mask */
136 	ng_hci_node_role_switch_ep	role_switch;	/* role switch */
137 
138 	ng_hci_node_stat_ep		stat;           /* statistic */
139 #define NG_HCI_STAT_CMD_SENT(s)		(s).cmd_sent ++
140 #define NG_HCI_STAT_EVNT_RECV(s)	(s).evnt_recv ++
141 #define NG_HCI_STAT_ACL_SENT(s, n)	(s).acl_sent += (n)
142 #define NG_HCI_STAT_ACL_RECV(s)		(s).acl_recv ++
143 #define NG_HCI_STAT_SCO_SENT(s, n)	(s).sco_sent += (n)
144 #define NG_HCI_STAT_SCO_RECV(s)		(s).sco_recv ++
145 #define NG_HCI_STAT_BYTES_SENT(s, b)	(s).bytes_sent += (b)
146 #define NG_HCI_STAT_BYTES_RECV(s, b)	(s).bytes_recv += (b)
147 #define NG_HCI_STAT_RESET(s)		bzero(&(s), sizeof((s)))
148 
149 	ng_hci_unit_buff_t		buffer;         /* buffer info */
150 
151 	struct callout_handle		cmd_timo;       /* command timeout */
152 	ng_bt_mbufq_t			cmdq;           /* command queue */
153 #define NG_HCI_CMD_QUEUE_LEN		12		/* max. size of cmd q */
154 
155 	hook_p				drv;            /* driver hook */
156 	hook_p				acl;            /* upstream hook */
157 	hook_p				sco;            /* upstream hook */
158 	hook_p				raw;            /* upstream hook */
159 
160 	LIST_HEAD(, ng_hci_unit_con)	con_list;       /* connections */
161 	LIST_HEAD(, ng_hci_neighbor)	neighbors;      /* unit neighbors */
162 } ng_hci_unit_t;
163 typedef ng_hci_unit_t *			ng_hci_unit_p;
164 
165 /*
166  * Unit connection descriptor
167  */
168 
169 typedef struct ng_hci_unit_con {
170 	ng_hci_unit_p			unit;            /* pointer back */
171 
172 	u_int16_t			state;           /* con. state */
173 	u_int16_t			flags;           /* con. flags */
174 #define NG_HCI_CON_TIMEOUT_PENDING		(1 << 0)
175 #define NG_HCI_CON_NOTIFY_ACL			(1 << 1)
176 #define NG_HCI_CON_NOTIFY_SCO			(1 << 2)
177 
178 	bdaddr_t			bdaddr;          /* remote address */
179 	u_int16_t			con_handle;      /* con. handle */
180 
181 	u_int8_t			link_type;       /* ACL or SCO */
182 	u_int8_t			encryption_mode; /* none, p2p, ... */
183 	u_int8_t			mode;            /* ACTIVE, HOLD ... */
184 	u_int8_t			role;            /* MASTER/SLAVE */
185 
186 	struct callout_handle		con_timo;        /* con. timeout */
187 
188 	int				pending;         /* # of data pkts */
189 	ng_bt_itemq_t			conq;            /* con. queue */
190 
191 	LIST_ENTRY(ng_hci_unit_con)	next;            /* next */
192 } ng_hci_unit_con_t;
193 typedef ng_hci_unit_con_t *		ng_hci_unit_con_p;
194 
195 /*
196  * Unit's neighbor descriptor.
197  * Neighbor is a remote unit that responded to our inquiry.
198  */
199 
200 typedef struct ng_hci_neighbor {
201 	struct timeval			updated;	/* entry was updated */
202 
203 	bdaddr_t			bdaddr;         /* address */
204 	u_int8_t			features[NG_HCI_FEATURES_SIZE];
205 					                /* LMP features */
206 
207 	u_int8_t			page_scan_rep_mode; /* PS rep. mode */
208 	u_int8_t			page_scan_mode; /* page scan mode */
209 	u_int16_t			clock_offset;   /* clock offset */
210 
211 	LIST_ENTRY(ng_hci_neighbor)	next;
212 } ng_hci_neighbor_t;
213 typedef ng_hci_neighbor_t *		ng_hci_neighbor_p;
214 
215 #endif /* ndef _NETGRAPH_HCI_VAR_H_ */
216 
217