network - Major netmsg retooling, part 1
[dragonfly.git] / sys / netbt / bt_proto.c
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 #include <net/pf/pfvar.h>
33
34 #include <netbt/bluetooth.h>
35 #include <netbt/hci.h>
36 #include <netbt/l2cap.h>
37 #include <netbt/rfcomm.h>
38 #include <netbt/sco.h>
39
40 MALLOC_DEFINE(M_BLUETOOTH, "Bluetooth", "Bluetooth system memory");
41
42 extern struct pr_usrreqs hci_usrreqs;
43
44 static int
45 netbt_modevent(module_t mod, int type, void *data)
46 {
47         switch (type) {
48         case MOD_LOAD:
49                 break;
50         case MOD_UNLOAD:
51                 return EBUSY;
52                 break;
53         default:
54                 break;
55         }
56         return 0;
57 }
58
59 static moduledata_t netbt_mod = {
60         "netbt",
61         netbt_modevent,
62         NULL
63 };
64
65 DECLARE_MODULE(netbt, netbt_mod, SI_SUB_EXEC, SI_ORDER_ANY);
66 MODULE_VERSION(netbt, 1);
67
68 struct domain btdomain;
69
70 struct protosw btsw[] = {
71         { /* raw HCI commands */
72                 .pr_type = SOCK_RAW,
73                 .pr_domain = &btdomain,
74                 .pr_protocol = BTPROTO_HCI,
75                 .pr_flags = (PR_ADDR | PR_ATOMIC),
76                 .pr_input = 0,
77                 .pr_output = 0,
78                 .pr_ctlinput = 0,
79                 .pr_ctloutput = hci_ctloutput,
80                 .pr_ctlport = NULL,
81                 .pr_init = 0,
82                 .pr_fasttimo =  0,
83                 .pr_slowtimo = 0,
84                 .pr_drain = 0,
85                 .pr_usrreqs = &hci_usrreqs
86         },
87         { /* HCI SCO data (audio) */
88                 .pr_type = SOCK_SEQPACKET,
89                 .pr_domain = &btdomain,
90                 .pr_protocol = BTPROTO_SCO,
91                 .pr_flags = (PR_CONNREQUIRED | PR_ATOMIC ),
92                 .pr_input = 0,
93                 .pr_output = 0,
94                 .pr_ctlinput = 0,
95                 .pr_ctloutput = sco_ctloutput,
96                 .pr_ctlport = NULL,
97                 .pr_init = 0,
98                 .pr_fasttimo =  0,
99                 .pr_slowtimo = 0,
100                 .pr_drain = 0,
101                 .pr_usrreqs = &sco_usrreqs
102
103         },
104         { /* L2CAP Connection Oriented */
105                 .pr_type = SOCK_SEQPACKET,
106                 .pr_domain = &btdomain,
107                 .pr_protocol = BTPROTO_L2CAP,
108                 .pr_flags = (PR_CONNREQUIRED | PR_ATOMIC ),
109                 .pr_input = 0,
110                 .pr_output = 0,
111                 .pr_ctlinput = 0,
112                 .pr_ctloutput = l2cap_ctloutput,
113                 .pr_ctlport = NULL,
114                 .pr_init = 0,
115                 .pr_fasttimo =  0,
116                 .pr_slowtimo = 0,
117                 .pr_drain = 0,
118                 .pr_usrreqs = &l2cap_usrreqs
119         },
120         { /* RFCOMM */
121                 .pr_type = SOCK_STREAM,
122                 .pr_domain = &btdomain,
123                 .pr_protocol = BTPROTO_RFCOMM,
124                 .pr_flags = (PR_CONNREQUIRED | PR_WANTRCVD),
125                 .pr_input = 0,
126                 .pr_output = 0,
127                 .pr_ctlinput = 0,
128                 .pr_ctloutput = rfcomm_ctloutput,
129                 .pr_ctlport = NULL,
130                 .pr_init = 0,
131                 .pr_fasttimo =  0,
132                 .pr_slowtimo = 0,
133                 .pr_drain = 0,
134                 .pr_usrreqs = &rfcomm_usrreqs
135         },
136 };
137
138 static void
139 netbt_dispose(struct mbuf* m)
140 {
141         ZONE_DESTROY(l2cap_pdu_pool);
142         ZONE_DESTROY(l2cap_req_pool);
143         ZONE_DESTROY(rfcomm_credit_pool);
144 }
145
146 static void
147 netbt_init(void)
148 {
149         int error = 1;
150         do {
151                 ZONE_CREATE(l2cap_pdu_pool, struct l2cap_pdu, "l2cap_pdu");
152                 ZONE_CREATE(l2cap_req_pool, struct l2cap_req, "l2cap_req");
153                 ZONE_CREATE(rfcomm_credit_pool, struct rfcomm_credit,
154                     "rfcomm_credit");
155                 error = 0;
156         } while(0); 
157
158         if (error) {
159                 netbt_dispose(NULL);
160                 panic("Can't create vm_zones");
161         }
162 }
163
164 struct domain btdomain = {
165         .dom_family = AF_BLUETOOTH,
166         .dom_name = "bluetooth",
167         .dom_init = netbt_init,
168         .dom_externalize = NULL,
169         .dom_dispose = netbt_dispose,
170         .dom_protosw = btsw,
171         .dom_protoswNPROTOSW = &btsw[sizeof(btsw)/sizeof(btsw[0])],
172         .dom_next = SLIST_ENTRY_INITIALIZER,
173         .dom_rtattach = 0,
174         .dom_rtoffset = 32,
175         .dom_maxrtkey = sizeof(struct sockaddr_bt),
176         .dom_ifattach = 0,
177         .dom_ifdetach = 0,
178 };
179
180 DOMAIN_SET(bt);
181 SYSCTL_NODE(_net, OID_AUTO, bluetooth, CTLFLAG_RD, 0,
182     "Bluetooth Protocol Family");
183
184 /* HCI sysctls */
185 SYSCTL_NODE(_net_bluetooth, OID_AUTO, hci, CTLFLAG_RD, 0,
186     "Host Controller Interface");
187 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, sendspace, CTLFLAG_RW, &hci_sendspace,
188     0, "Socket Send Buffer Size");
189 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, recvspace, CTLFLAG_RW, &hci_recvspace,
190     0, "Socket Receive Buffer Size");
191 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, acl_expiry, CTLFLAG_RW,
192     &hci_acl_expiry, 0, "ACL Connection Expiry Time");
193 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, memo_expiry, CTLFLAG_RW,
194     &hci_memo_expiry, 0, "Memo Expiry Time");
195 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, eventq_max, CTLFLAG_RW,
196     &hci_eventq_max, 0, "Max Event queue length");
197 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, aclrxq_max, CTLFLAG_RW,
198     &hci_aclrxq_max, 0, "Max ACL rx queue length");
199 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, scorxq_max, CTLFLAG_RW,
200     &hci_scorxq_max, 0, "Max SCO rx queue length");
201
202 /* L2CAP sysctls */
203 SYSCTL_NODE(_net_bluetooth, OID_AUTO, l2cap, CTLFLAG_RD, 0,
204     "Logical Link Control & Adaptation Protocol");
205 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, sendspace, CTLFLAG_RW,
206     &l2cap_sendspace, 0, "Socket Send Buffer Size");
207 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, recvspace, CTLFLAG_RW,
208     &l2cap_recvspace, 0, "Socket Receive Buffer Size");
209 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, rtx, CTLFLAG_RW,
210     &l2cap_response_timeout, 0, "Response Timeout");
211 SYSCTL_INT(_net_bluetooth_l2cap, OID_AUTO, ertx, CTLFLAG_RW,
212     &l2cap_response_extended_timeout, 0, "Extended Response Timeout");
213
214 /* RFCOMM sysctls */
215 SYSCTL_NODE(_net_bluetooth, OID_AUTO, rfcomm, CTLFLAG_RD, 0,
216     "Serial Cable Emulation");
217 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, sendspace, CTLFLAG_RW,
218     &rfcomm_sendspace, 0, "Socket Send Buffer Size");
219 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, recvspace, CTLFLAG_RW,
220     &rfcomm_recvspace, 0, "Socket Receive Buffer Size");
221 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, mtu_default, CTLFLAG_RW,
222     &rfcomm_mtu_default, 0, "Default MTU");
223 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, ack_timeout, CTLFLAG_RW,
224     &rfcomm_ack_timeout, 0, "Acknowledgement Timer");
225 SYSCTL_INT(_net_bluetooth_rfcomm, OID_AUTO, mcc_timeout, CTLFLAG_RW,
226     &rfcomm_mcc_timeout, 0, "Response Timeout for Multiplexer Control Channel");
227
228 /* SCO sysctls */
229 SYSCTL_NODE(_net_bluetooth, OID_AUTO, sco, CTLFLAG_RD, 0, "SCO data");
230 SYSCTL_INT(_net_bluetooth_sco, OID_AUTO, sendspace, CTLFLAG_RW, &sco_sendspace,
231     0, "Socket Send Buffer Size");
232 SYSCTL_INT(_net_bluetooth_sco, OID_AUTO, recvspace, CTLFLAG_RW, &sco_recvspace,
233     0, "Socket Receive Buffer Size");
234
235 static void
236 netisr_netbt_setup(void *dummy __unused)
237 {
238         netisr_register(NETISR_BLUETOOTH, btintr, NULL);
239 }
240
241 SYSINIT(netbt_setup, SI_BOOT2_KLD, SI_ORDER_ANY, netisr_netbt_setup, NULL);