xref: /dragonfly/sys/netbt/bt_proto.c (revision 81c11cd3)
1 /* $DragonFly: src/sys/netbt/bt_proto.c,v 1.7 2008/11/01 04:22:15 sephe Exp $ */
2 /* $OpenBSD: bt_proto.c,v 1.4 2007/06/24 20:55:27 uwe Exp $ */
3 
4 /*
5  * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/domain.h>
22 #include <sys/protosw.h>
23 #include <sys/socket.h>
24 #include <sys/socketvar.h>
25 #include <sys/queue.h>
26 #include <sys/kernel.h>
27 #include <sys/mbuf.h>
28 #include <sys/sysctl.h>
29 #include <sys/bus.h>
30 #include <sys/malloc.h>
31 #include <net/if.h>
32 
33 #include <netbt/bluetooth.h>
34 #include <netbt/hci.h>
35 #include <netbt/l2cap.h>
36 #include <netbt/rfcomm.h>
37 #include <netbt/sco.h>
38 
39 MALLOC_DEFINE(M_BLUETOOTH, "Bluetooth", "Bluetooth system memory");
40 
41 extern struct pr_usrreqs hci_usrreqs;
42 
43 static int
44 netbt_modevent(module_t mod, int type, void *data)
45 {
46 	switch (type) {
47 	case MOD_LOAD:
48 		break;
49 	case MOD_UNLOAD:
50 		return EBUSY;
51 		break;
52 	default:
53 		break;
54 	}
55 	return 0;
56 }
57 
58 static moduledata_t netbt_mod = {
59 	"netbt",
60 	netbt_modevent,
61 	NULL
62 };
63 
64 DECLARE_MODULE(netbt, netbt_mod, SI_SUB_EXEC, SI_ORDER_ANY);
65 MODULE_VERSION(netbt, 1);
66 
67 struct domain btdomain;
68 
69 struct protosw btsw[] = {
70 	{ /* raw HCI commands */
71 		.pr_type = SOCK_RAW,
72 		.pr_domain = &btdomain,
73 		.pr_protocol = BTPROTO_HCI,
74 		.pr_flags = (PR_ADDR | PR_ATOMIC),
75 		.pr_input = 0,
76 		.pr_output = 0,
77 		.pr_ctlinput = 0,
78 		.pr_ctloutput = hci_ctloutput,
79 		.pr_ctlport = NULL,
80 		.pr_init = 0,
81 		.pr_fasttimo =	0,
82 		.pr_slowtimo = 0,
83 		.pr_drain = 0,
84 		.pr_usrreqs = &hci_usrreqs
85 	},
86 	{ /* HCI SCO data (audio) */
87 		.pr_type = SOCK_SEQPACKET,
88 		.pr_domain = &btdomain,
89 		.pr_protocol = BTPROTO_SCO,
90 		.pr_flags = (PR_CONNREQUIRED | PR_ATOMIC ),
91 		.pr_input = 0,
92 		.pr_output = 0,
93 		.pr_ctlinput = 0,
94 		.pr_ctloutput = sco_ctloutput,
95 		.pr_ctlport = NULL,
96 		.pr_init = 0,
97 		.pr_fasttimo =	0,
98 		.pr_slowtimo = 0,
99 		.pr_drain = 0,
100 		.pr_usrreqs = &sco_usrreqs
101 
102 	},
103 	{ /* L2CAP Connection Oriented */
104 		.pr_type = SOCK_SEQPACKET,
105 		.pr_domain = &btdomain,
106 		.pr_protocol = BTPROTO_L2CAP,
107 		.pr_flags = (PR_CONNREQUIRED | PR_ATOMIC ),
108 		.pr_input = 0,
109 		.pr_output = 0,
110 		.pr_ctlinput = 0,
111 		.pr_ctloutput = l2cap_ctloutput,
112 		.pr_ctlport = NULL,
113 		.pr_init = 0,
114 		.pr_fasttimo =	0,
115 		.pr_slowtimo = 0,
116 		.pr_drain = 0,
117 		.pr_usrreqs = &l2cap_usrreqs
118 	},
119 	{ /* RFCOMM */
120 		.pr_type = SOCK_STREAM,
121 		.pr_domain = &btdomain,
122 		.pr_protocol = BTPROTO_RFCOMM,
123 		.pr_flags = (PR_CONNREQUIRED | PR_WANTRCVD),
124 		.pr_input = 0,
125 		.pr_output = 0,
126 		.pr_ctlinput = 0,
127 		.pr_ctloutput = rfcomm_ctloutput,
128 		.pr_ctlport = NULL,
129 		.pr_init = 0,
130 		.pr_fasttimo =	0,
131 		.pr_slowtimo = 0,
132 		.pr_drain = 0,
133 		.pr_usrreqs = &rfcomm_usrreqs
134 	},
135 };
136 
137 static void
138 netbt_dispose(struct mbuf* m)
139 {
140 	zdestroy(l2cap_pdu_pool);
141 	zdestroy(l2cap_req_pool);
142 	zdestroy(rfcomm_credit_pool);
143 }
144 
145 static void
146 netbt_init(void)
147 {
148 	l2cap_pdu_pool = zinit("l2cap_pdu", sizeof(struct l2cap_pdu), 1,
149 	    ZONE_DESTROYABLE, 1);
150 	if (l2cap_pdu_pool == NULL)
151 		goto fail;
152 	l2cap_req_pool = zinit("l2cap_req", sizeof(struct l2cap_req), 1,
153 	    ZONE_DESTROYABLE, 1);
154 	if (l2cap_req_pool == NULL)
155 		goto fail;
156 	rfcomm_credit_pool = zinit("rfcomm_credit",
157 	    sizeof(struct rfcomm_credit), 1, ZONE_DESTROYABLE, 1);
158 	if (rfcomm_credit_pool == NULL)
159 		goto fail;
160 	return;
161 fail:
162 	netbt_dispose(NULL);
163 	panic("Can't create vm_zones");
164 }
165 
166 struct domain btdomain = {
167 	.dom_family = AF_BLUETOOTH,
168 	.dom_name = "bluetooth",
169 	.dom_init = netbt_init,
170 	.dom_externalize = NULL,
171 	.dom_dispose = netbt_dispose,
172 	.dom_protosw = btsw,
173 	.dom_protoswNPROTOSW = &btsw[sizeof(btsw)/sizeof(btsw[0])],
174 	.dom_next = SLIST_ENTRY_INITIALIZER,
175 	.dom_rtattach = 0,
176 	.dom_rtoffset = 32,
177 	.dom_maxrtkey = sizeof(struct sockaddr_bt),
178 	.dom_ifattach = 0,
179 	.dom_ifdetach = 0,
180 };
181 
182 DOMAIN_SET(bt);
183 SYSCTL_NODE(_net, OID_AUTO, bluetooth, CTLFLAG_RD, 0,
184     "Bluetooth Protocol Family");
185 
186 /* HCI sysctls */
187 SYSCTL_NODE(_net_bluetooth, OID_AUTO, hci, CTLFLAG_RD, 0,
188     "Host Controller Interface");
189 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, sendspace, CTLFLAG_RW, &hci_sendspace,
190     0, "Socket Send Buffer Size");
191 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, recvspace, CTLFLAG_RW, &hci_recvspace,
192     0, "Socket Receive Buffer Size");
193 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, acl_expiry, CTLFLAG_RW,
194     &hci_acl_expiry, 0, "ACL Connection Expiry Time");
195 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, memo_expiry, CTLFLAG_RW,
196     &hci_memo_expiry, 0, "Memo Expiry Time");
197 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, eventq_max, CTLFLAG_RW,
198     &hci_eventq_max, 0, "Max Event queue length");
199 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, aclrxq_max, CTLFLAG_RW,
200     &hci_aclrxq_max, 0, "Max ACL rx queue length");
201 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, scorxq_max, CTLFLAG_RW,
202     &hci_scorxq_max, 0, "Max SCO rx queue length");
203 
204 /* L2CAP sysctls */
205 SYSCTL_NODE(_net_bluetooth, OID_AUTO, l2cap, CTLFLAG_RD, 0,
206     "Logical Link Control & Adaptation Protocol");
207 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, sendspace, CTLFLAG_RW,
208     &l2cap_sendspace, 0, "Socket Send Buffer Size");
209 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, recvspace, CTLFLAG_RW,
210     &l2cap_recvspace, 0, "Socket Receive Buffer Size");
211 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, rtx, CTLFLAG_RW,
212     &l2cap_response_timeout, 0, "Response Timeout");
213 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, ertx, CTLFLAG_RW,
214     &l2cap_response_extended_timeout, 0, "Extended Response Timeout");
215 
216 /* RFCOMM sysctls */
217 SYSCTL_NODE(_net_bluetooth, OID_AUTO, rfcomm, CTLFLAG_RD, 0,
218     "Serial Cable Emulation");
219 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, sendspace, CTLFLAG_RW,
220     &rfcomm_sendspace, 0, "Socket Send Buffer Size");
221 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, recvspace, CTLFLAG_RW,
222     &rfcomm_recvspace, 0, "Socket Receive Buffer Size");
223 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, mtu_default, CTLFLAG_RW,
224     &rfcomm_mtu_default, 0, "Default MTU");
225 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, ack_timeout, CTLFLAG_RW,
226     &rfcomm_ack_timeout, 0, "Acknowledgement Timer");
227 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, mcc_timeout, CTLFLAG_RW,
228     &rfcomm_mcc_timeout, 0, "Response Timeout for Multiplexer Control Channel");
229 
230 /* SCO sysctls */
231 SYSCTL_NODE(_net_bluetooth, OID_AUTO, sco, CTLFLAG_RD, 0, "SCO data");
232 SYSCTL_INT(_net_bluetooth_sco, OID_AUTO, sendspace, CTLFLAG_RW, &sco_sendspace,
233     0, "Socket Send Buffer Size");
234 SYSCTL_INT(_net_bluetooth_sco, OID_AUTO, recvspace, CTLFLAG_RW, &sco_recvspace,
235     0, "Socket Receive Buffer Size");
236 
237 static void
238 netisr_netbt_setup(void *dummy __unused)
239 {
240 	netisr_register(NETISR_BLUETOOTH, btintr, NULL);
241 }
242 
243 SYSINIT(netbt_setup, SI_BOOT2_KLD, SI_ORDER_ANY, netisr_netbt_setup, NULL);
244