kernel: Move us to using M_NOWAIT and M_WAITOK for mbuf functions.
[dragonfly.git] / sys / netgraph7 / bluetooth / socket / ng_btsocket.c
1 /*
2  * ng_btsocket.c
3  */
4
5 /*-
6  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: ng_btsocket.c,v 1.4 2003/09/14 23:29:06 max Exp $
31  * $FreeBSD: src/sys/netgraph/bluetooth/socket/ng_btsocket.c,v 1.13 2006/07/21 17:11:13 rwatson Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <bitstring.h>          /* XXX */
37 #include <sys/errno.h>
38 #include <sys/domain.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
47 #include <netgraph7/ng_message.h>
48 #include <netgraph7/netgraph.h>
49 #include <netgraph7/bluetooth/include/ng_bluetooth.h>
50 #include <netgraph7/bluetooth/include/ng_hci.h>
51 #include <netgraph7/bluetooth/include/ng_l2cap.h>
52 #include <netgraph7/bluetooth/include/ng_btsocket.h>
53 #include <netgraph7/bluetooth/include/ng_btsocket_hci_raw.h>
54 #include <netgraph7/bluetooth/include/ng_btsocket_l2cap.h>
55 #include <netgraph7/bluetooth/include/ng_btsocket_rfcomm.h>
56
57 static int                      ng_btsocket_modevent (module_t, int, void *);
58 extern struct domain            ng_btsocket_domain;
59
60 /*
61  * Bluetooth raw HCI sockets
62  */
63
64 static struct pr_usrreqs        ng_btsocket_hci_raw_usrreqs = {
65         .pru_abort =            ng_btsocket_hci_raw_abort,
66         .pru_attach =           ng_btsocket_hci_raw_attach,
67         .pru_bind =             ng_btsocket_hci_raw_bind,
68         .pru_connect =          ng_btsocket_hci_raw_connect,
69         .pru_control =          ng_btsocket_hci_raw_control,
70         .pru_detach =           ng_btsocket_hci_raw_detach,
71         .pru_disconnect =       ng_btsocket_hci_raw_disconnect,
72         .pru_peeraddr =         ng_btsocket_hci_raw_peeraddr,
73         .pru_send =             ng_btsocket_hci_raw_send,
74         .pru_shutdown =         NULL,
75         .pru_sockaddr =         ng_btsocket_hci_raw_sockaddr,
76 #if 0 /* XXX */
77         .pru_close =            ng_btsocket_hci_raw_close,
78 #endif
79         .pru_sosend =           sosend,
80         .pru_soreceive =        soreceive,
81 };
82
83 /*
84  * Bluetooth raw L2CAP sockets
85  */
86
87 static struct pr_usrreqs        ng_btsocket_l2cap_raw_usrreqs = {
88         .pru_abort =            ng_btsocket_l2cap_raw_abort,
89         .pru_attach =           ng_btsocket_l2cap_raw_attach,
90         .pru_bind =             ng_btsocket_l2cap_raw_bind,
91         .pru_connect =          ng_btsocket_l2cap_raw_connect,
92         .pru_control =          ng_btsocket_l2cap_raw_control,
93         .pru_detach =           ng_btsocket_l2cap_raw_detach,
94         .pru_disconnect =       ng_btsocket_l2cap_raw_disconnect,
95         .pru_peeraddr =         ng_btsocket_l2cap_raw_peeraddr,
96         .pru_send =             ng_btsocket_l2cap_raw_send,
97         .pru_shutdown =         NULL,
98         .pru_sockaddr =         ng_btsocket_l2cap_raw_sockaddr,
99 #if 0 /* XXX */
100         .pru_close =            ng_btsocket_l2cap_raw_close,
101 #endif
102         .pru_sosend =           sosend,
103         .pru_soreceive =        soreceive,
104 };
105
106 /*
107  * Bluetooth SEQPACKET L2CAP sockets
108  */
109
110 static struct pr_usrreqs        ng_btsocket_l2cap_usrreqs = {
111         .pru_abort =            ng_btsocket_l2cap_abort,
112         .pru_accept =           ng_btsocket_l2cap_accept,
113         .pru_attach =           ng_btsocket_l2cap_attach,
114         .pru_bind =             ng_btsocket_l2cap_bind,
115         .pru_connect =          ng_btsocket_l2cap_connect,
116         .pru_control =          ng_btsocket_l2cap_control,
117         .pru_detach =           ng_btsocket_l2cap_detach,
118         .pru_disconnect =       ng_btsocket_l2cap_disconnect,
119         .pru_listen =           ng_btsocket_l2cap_listen,
120         .pru_peeraddr =         ng_btsocket_l2cap_peeraddr,
121         .pru_send =             ng_btsocket_l2cap_send,
122         .pru_shutdown =         NULL,
123         .pru_sockaddr =         ng_btsocket_l2cap_sockaddr,
124 #if 0 /* XXX */
125         .pru_close =            ng_btsocket_l2cap_close,
126 #endif
127         .pru_sosend =           sosend,
128         .pru_soreceive =        soreceive,
129 };
130
131 /*
132  * Bluetooth STREAM RFCOMM sockets
133  */
134
135 static struct pr_usrreqs        ng_btsocket_rfcomm_usrreqs = {
136         .pru_abort =            ng_btsocket_rfcomm_abort,
137         .pru_accept =           ng_btsocket_rfcomm_accept,
138         .pru_attach =           ng_btsocket_rfcomm_attach,
139         .pru_bind =             ng_btsocket_rfcomm_bind,
140         .pru_connect =          ng_btsocket_rfcomm_connect,
141         .pru_control =          ng_btsocket_rfcomm_control,
142         .pru_detach =           ng_btsocket_rfcomm_detach,
143         .pru_disconnect =       ng_btsocket_rfcomm_disconnect,
144         .pru_listen =           ng_btsocket_rfcomm_listen,
145         .pru_peeraddr =         ng_btsocket_rfcomm_peeraddr,
146         .pru_send =             ng_btsocket_rfcomm_send,
147         .pru_shutdown =         NULL,
148         .pru_sockaddr =         ng_btsocket_rfcomm_sockaddr,
149 #if 0 /* XXX */
150         .pru_close =            ng_btsocket_rfcomm_close,
151 #endif
152         .pru_sosend =           sosend,
153         .pru_soreceive =        soreceive,
154 };
155
156 /* 
157  * Definitions of protocols supported in the BLUETOOTH domain 
158  */
159
160 static struct protosw           ng_btsocket_protosw[] = {
161 {
162         .pr_type =              SOCK_RAW,
163         .pr_domain =            &ng_btsocket_domain,
164         .pr_protocol =          BLUETOOTH_PROTO_HCI,
165         .pr_flags =             PR_ATOMIC|PR_ADDR,
166         .pr_ctloutput =         ng_btsocket_hci_raw_ctloutput,
167         .pr_init =              ng_btsocket_hci_raw_init,
168         .pr_usrreqs =           &ng_btsocket_hci_raw_usrreqs,
169 },
170 {
171         .pr_type =              SOCK_RAW,
172         .pr_domain =            &ng_btsocket_domain,
173         .pr_protocol =          BLUETOOTH_PROTO_L2CAP,
174         .pr_flags =             PR_ATOMIC|PR_ADDR,
175         .pr_init =              ng_btsocket_l2cap_raw_init,
176         .pr_usrreqs =           &ng_btsocket_l2cap_raw_usrreqs,
177 },
178 {
179         .pr_type =              SOCK_SEQPACKET,
180         .pr_domain =            &ng_btsocket_domain,
181         .pr_protocol =          BLUETOOTH_PROTO_L2CAP,
182         .pr_flags =             PR_ATOMIC|PR_CONNREQUIRED,
183         .pr_ctloutput =         ng_btsocket_l2cap_ctloutput,
184         .pr_init =              ng_btsocket_l2cap_init,
185         .pr_usrreqs =           &ng_btsocket_l2cap_usrreqs,
186 },
187 {
188         .pr_type =              SOCK_STREAM,
189         .pr_domain =            &ng_btsocket_domain,
190         .pr_protocol =          BLUETOOTH_PROTO_RFCOMM,
191         .pr_flags =             PR_CONNREQUIRED,
192         .pr_ctloutput =         ng_btsocket_rfcomm_ctloutput,
193         .pr_init =              ng_btsocket_rfcomm_init,
194         .pr_usrreqs =           &ng_btsocket_rfcomm_usrreqs,
195 }
196 };
197 #define ng_btsocket_protosw_size NELEM(ng_btsocket_protosw)
198 #define ng_btsocket_protosw_end \
199         &ng_btsocket_protosw[ng_btsocket_protosw_size]
200
201 /*
202  * BLUETOOTH domain
203  */
204
205 struct domain                   ng_btsocket_domain = {
206         .dom_family =           AF_BLUETOOTH,
207         .dom_name =             "bluetooth",
208         .dom_protosw =          ng_btsocket_protosw,
209         .dom_protoswNPROTOSW =  ng_btsocket_protosw_end
210 };
211
212 /* 
213  * Socket sysctl tree 
214  */
215
216 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW,
217         0, "Bluetooth HCI sockets family");
218 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW,
219         0, "Bluetooth L2CAP sockets family");
220 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets, CTLFLAG_RW,
221         0, "Bluetooth RFCOMM sockets family");
222
223 /* 
224  * Module 
225  */
226
227 static moduledata_t     ng_btsocket_mod = {
228         "ng_btsocket",
229         ng_btsocket_modevent,
230         NULL
231 };
232
233 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN,
234         SI_ORDER_ANY);
235 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
236 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
237         NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
238 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
239         NG_ABI_VERSION, NG_ABI_VERSION);
240
241 /*
242  * Handle loading and unloading for this node type.
243  * This is to handle auxiliary linkages (e.g protocol domain addition).
244  */
245
246 static int  
247 ng_btsocket_modevent(module_t mod, int event, void *data)
248 {
249         int     error = 0;
250         
251         switch (event) {
252         case MOD_LOAD:
253                 net_add_domain(&ng_btsocket_domain);
254                 break;
255
256         case MOD_UNLOAD:
257                 /* XXX can't unload protocol domain yet */
258                 error = EBUSY;
259                 break;
260
261         default:
262                 error = EOPNOTSUPP;
263                 break;
264         }
265
266         return (error);
267 } /* ng_btsocket_modevent */
268