cleanup
[linux-2.4.21-pre4.git] / net / bluetooth / af_bluetooth.c
1 /* 
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
22    SOFTWARE IS DISCLAIMED.
23 */
24
25 /*
26  * BlueZ Bluetooth address family and sockets.
27  *
28  * $Id: af_bluetooth.c,v 1.1.1.1 2005/04/11 02:51:12 jack Exp $
29  */
30 #define VERSION "2.2"
31
32 #include <linux/config.h>
33 #include <linux/module.h>
34
35 #include <linux/types.h>
36 #include <linux/list.h>
37 #include <linux/errno.h>
38 #include <linux/kernel.h>
39 #include <linux/major.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/skbuff.h>
43 #include <linux/init.h>
44 #include <linux/poll.h>
45 #include <linux/proc_fs.h>
46 #include <net/sock.h>
47
48 #if defined(CONFIG_KMOD)
49 #include <linux/kmod.h>
50 #endif
51
52 #include <net/bluetooth/bluetooth.h>
53
54 #ifndef AF_BLUETOOTH_DEBUG
55 #undef  BT_DBG
56 #define BT_DBG( A... )
57 #endif
58
59 /* Bluetooth sockets */
60 #define BLUEZ_MAX_PROTO 5
61 static struct net_proto_family *bluez_proto[BLUEZ_MAX_PROTO];
62
63 int bluez_sock_register(int proto, struct net_proto_family *ops)
64 {
65         if (proto >= BLUEZ_MAX_PROTO)
66                 return -EINVAL;
67
68         if (bluez_proto[proto])
69                 return -EEXIST;
70
71         bluez_proto[proto] = ops;
72         return 0;
73 }
74
75 int bluez_sock_unregister(int proto)
76 {
77         if (proto >= BLUEZ_MAX_PROTO)
78                 return -EINVAL;
79
80         if (!bluez_proto[proto])
81                 return -ENOENT;
82
83         bluez_proto[proto] = NULL;
84         return 0;
85 }
86
87 static int bluez_sock_create(struct socket *sock, int proto)
88 {
89         if (proto >= BLUEZ_MAX_PROTO)
90                 return -EINVAL;
91
92 #if defined(CONFIG_KMOD)
93         if (!bluez_proto[proto]) {
94                 char module_name[30];
95                 sprintf(module_name, "bt-proto-%d", proto);
96                 request_module(module_name);
97         }
98 #endif
99
100         if (!bluez_proto[proto])
101                 return -ENOENT;
102
103         return bluez_proto[proto]->create(sock, proto);
104 }
105
106 void bluez_sock_init(struct socket *sock, struct sock *sk)
107
108         sock_init_data(sock, sk);
109         INIT_LIST_HEAD(&bluez_pi(sk)->accept_q);
110 }
111
112 void bluez_sock_link(struct bluez_sock_list *l, struct sock *sk)
113 {
114         write_lock_bh(&l->lock);
115         sk->next = l->head;
116         l->head = sk;
117         sock_hold(sk);
118         write_unlock_bh(&l->lock);
119 }
120
121 void bluez_sock_unlink(struct bluez_sock_list *l, struct sock *sk)
122 {
123         struct sock **skp;
124
125         write_lock_bh(&l->lock);
126         for (skp = &l->head; *skp; skp = &((*skp)->next)) {
127                 if (*skp == sk) {
128                         *skp = sk->next;
129                         __sock_put(sk);
130                         break;
131                 }
132         }
133         write_unlock_bh(&l->lock);
134 }
135
136 void bluez_accept_enqueue(struct sock *parent, struct sock *sk)
137 {
138         BT_DBG("parent %p, sk %p", parent, sk);
139
140         sock_hold(sk);
141         list_add_tail(&bluez_pi(sk)->accept_q, &bluez_pi(parent)->accept_q);
142         bluez_pi(sk)->parent = parent;
143         parent->ack_backlog++;
144 }
145
146 static void bluez_accept_unlink(struct sock *sk)
147 {
148         BT_DBG("sk %p state %d", sk, sk->state);
149
150         list_del_init(&bluez_pi(sk)->accept_q);
151         bluez_pi(sk)->parent->ack_backlog--;
152         bluez_pi(sk)->parent = NULL;
153         sock_put(sk);
154 }
155
156 struct sock *bluez_accept_dequeue(struct sock *parent, struct socket *newsock)
157 {
158         struct list_head *p, *n;
159         struct bluez_pinfo *pi;
160         struct sock *sk;
161         
162         BT_DBG("parent %p", parent);
163
164         list_for_each_safe(p, n, &bluez_pi(parent)->accept_q) {
165                 pi = list_entry(p, struct bluez_pinfo, accept_q);
166                 sk = bluez_sk(pi);
167                 
168                 lock_sock(sk);
169                 if (sk->state == BT_CLOSED) {
170                         release_sock(sk);
171                         bluez_accept_unlink(sk);
172                         continue;
173                 }
174                 
175                 if (sk->state == BT_CONNECTED || !newsock) {
176                         bluez_accept_unlink(sk);
177                         if (newsock)
178                                 sock_graft(sk, newsock);
179                         release_sock(sk);
180                         return sk;
181                 }
182                 release_sock(sk);
183         }
184         return NULL;
185 }
186
187 int bluez_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm)
188 {
189         int noblock = flags & MSG_DONTWAIT;
190         struct sock *sk = sock->sk;
191         struct sk_buff *skb;
192         int copied, err;
193
194         BT_DBG("sock %p sk %p len %d", sock, sk, len);
195
196         if (flags & (MSG_OOB))
197                 return -EOPNOTSUPP;
198
199         if (!(skb = skb_recv_datagram(sk, flags, noblock, &err))) {
200                 if (sk->shutdown & RCV_SHUTDOWN)
201                         return 0;
202                 return err;
203         }
204
205         msg->msg_namelen = 0;
206
207         copied = skb->len;
208         if (len < copied) {
209                 msg->msg_flags |= MSG_TRUNC;
210                 copied = len;
211         }
212
213         skb->h.raw = skb->data;
214         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
215
216         skb_free_datagram(sk, skb);
217
218         return err ? : copied;
219 }
220
221 unsigned int bluez_sock_poll(struct file * file, struct socket *sock, poll_table *wait)
222 {
223         struct sock *sk = sock->sk;
224         unsigned int mask;
225
226         BT_DBG("sock %p, sk %p", sock, sk);
227
228         poll_wait(file, sk->sleep, wait);
229         mask = 0;
230
231         if (sk->err || !skb_queue_empty(&sk->error_queue))
232                 mask |= POLLERR;
233
234         if (sk->shutdown == SHUTDOWN_MASK)
235                 mask |= POLLHUP;
236
237         if (!skb_queue_empty(&sk->receive_queue) || 
238                         !list_empty(&bluez_pi(sk)->accept_q) ||
239                         (sk->shutdown & RCV_SHUTDOWN))
240                 mask |= POLLIN | POLLRDNORM;
241
242         if (sk->state == BT_CLOSED)
243                 mask |= POLLHUP;
244
245         if (sk->state == BT_CONNECT || sk->state == BT_CONNECT2)
246                 return mask;
247         
248         if (sock_writeable(sk))
249                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
250         else
251                 set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
252
253         return mask;
254 }
255
256 int bluez_sock_w4_connect(struct sock *sk, int flags)
257 {
258         DECLARE_WAITQUEUE(wait, current);
259         long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
260         int err = 0;
261
262         BT_DBG("sk %p", sk);
263
264         add_wait_queue(sk->sleep, &wait);
265         while (sk->state != BT_CONNECTED) {
266                 set_current_state(TASK_INTERRUPTIBLE);
267                 if (!timeo) {
268                         err = -EAGAIN;
269                         break;
270                 }
271
272                 release_sock(sk);
273                 timeo = schedule_timeout(timeo);
274                 lock_sock(sk);
275
276                 err = 0;
277                 if (sk->state == BT_CONNECTED)
278                         break;
279
280                 if (sk->err) {
281                         err = sock_error(sk);
282                         break;
283                 }
284
285                 if (signal_pending(current)) {
286                         err = sock_intr_errno(timeo);
287                         break;
288                 }
289         }
290         set_current_state(TASK_RUNNING);
291         remove_wait_queue(sk->sleep, &wait);
292         return err;
293 }
294
295 struct net_proto_family bluez_sock_family_ops =
296 {
297         PF_BLUETOOTH, bluez_sock_create
298 };
299
300 int bluez_init(void)
301 {
302         BT_INFO("BlueZ Core ver %s Copyright (C) 2000,2001 Qualcomm Inc",
303                  VERSION);
304         BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
305
306         proc_mkdir("bluetooth", NULL);
307
308         sock_register(&bluez_sock_family_ops);
309
310         /* Init HCI Core */
311         hci_core_init();
312
313         /* Init sockets */
314         hci_sock_init();
315
316         return 0;
317 }
318
319 void bluez_cleanup(void)
320 {
321         /* Release socket */
322         hci_sock_cleanup();
323
324         /* Release core */
325         hci_core_cleanup();
326
327         sock_unregister(PF_BLUETOOTH);
328
329         remove_proc_entry("bluetooth", NULL);
330 }
331
332 #ifdef MODULE
333 module_init(bluez_init);
334 module_exit(bluez_cleanup);
335
336 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
337 MODULE_DESCRIPTION("BlueZ Core ver " VERSION);
338 MODULE_LICENSE("GPL");
339 #endif