2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
26 * BlueZ Bluetooth address family and sockets.
28 * $Id: af_bluetooth.c,v 1.8 2002/07/22 20:32:54 maxk Exp $
32 #include <linux/config.h>
33 #include <linux/module.h>
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>
48 #if defined(CONFIG_KMOD)
49 #include <linux/kmod.h>
52 #include <net/bluetooth/bluetooth.h>
54 #ifndef AF_BLUETOOTH_DEBUG
56 #define BT_DBG( A... )
59 /* Bluetooth sockets */
60 #define BLUEZ_MAX_PROTO 7
61 static struct net_proto_family *bluez_proto[BLUEZ_MAX_PROTO];
63 int bluez_sock_register(int proto, struct net_proto_family *ops)
65 if (proto < 0 || proto >= BLUEZ_MAX_PROTO)
68 if (bluez_proto[proto])
71 bluez_proto[proto] = ops;
75 int bluez_sock_unregister(int proto)
77 if (proto < 0 || proto >= BLUEZ_MAX_PROTO)
80 if (!bluez_proto[proto])
83 bluez_proto[proto] = NULL;
87 static int bluez_sock_create(struct socket *sock, int proto)
89 if (proto < 0 || proto >= BLUEZ_MAX_PROTO)
92 #if defined(CONFIG_KMOD)
93 if (!bluez_proto[proto]) {
95 sprintf(module_name, "bt-proto-%d", proto);
96 request_module(module_name);
100 if (!bluez_proto[proto])
103 return bluez_proto[proto]->create(sock, proto);
106 void bluez_sock_init(struct socket *sock, struct sock *sk)
108 sock_init_data(sock, sk);
109 INIT_LIST_HEAD(&bluez_pi(sk)->accept_q);
112 void bluez_sock_link(struct bluez_sock_list *l, struct sock *sk)
114 write_lock_bh(&l->lock);
118 write_unlock_bh(&l->lock);
121 void bluez_sock_unlink(struct bluez_sock_list *l, struct sock *sk)
125 write_lock_bh(&l->lock);
126 for (skp = &l->head; *skp; skp = &((*skp)->next)) {
133 write_unlock_bh(&l->lock);
136 void bluez_accept_enqueue(struct sock *parent, struct sock *sk)
138 BT_DBG("parent %p, sk %p", parent, 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++;
146 static void bluez_accept_unlink(struct sock *sk)
148 BT_DBG("sk %p state %d", sk, sk->state);
150 list_del_init(&bluez_pi(sk)->accept_q);
151 bluez_pi(sk)->parent->ack_backlog--;
152 bluez_pi(sk)->parent = NULL;
156 struct sock *bluez_accept_dequeue(struct sock *parent, struct socket *newsock)
158 struct list_head *p, *n;
159 struct bluez_pinfo *pi;
162 BT_DBG("parent %p", parent);
164 list_for_each_safe(p, n, &bluez_pi(parent)->accept_q) {
165 pi = list_entry(p, struct bluez_pinfo, accept_q);
169 if (sk->state == BT_CLOSED) {
171 bluez_accept_unlink(sk);
175 if (sk->state == BT_CONNECTED || !newsock) {
176 bluez_accept_unlink(sk);
178 sock_graft(sk, newsock);
187 int bluez_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm)
189 int noblock = flags & MSG_DONTWAIT;
190 struct sock *sk = sock->sk;
194 BT_DBG("sock %p sk %p len %d", sock, sk, len);
196 if (flags & (MSG_OOB))
199 if (!(skb = skb_recv_datagram(sk, flags, noblock, &err))) {
200 if (sk->shutdown & RCV_SHUTDOWN)
205 msg->msg_namelen = 0;
209 msg->msg_flags |= MSG_TRUNC;
213 skb->h.raw = skb->data;
214 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
216 skb_free_datagram(sk, skb);
218 return err ? : copied;
221 unsigned int bluez_sock_poll(struct file * file, struct socket *sock, poll_table *wait)
223 struct sock *sk = sock->sk;
224 unsigned int mask = 0;
226 BT_DBG("sock %p, sk %p", sock, sk);
228 poll_wait(file, sk->sleep, wait);
230 if (sk->err || !skb_queue_empty(&sk->error_queue))
233 if (sk->shutdown == SHUTDOWN_MASK)
236 if (!skb_queue_empty(&sk->receive_queue) ||
237 !list_empty(&bluez_pi(sk)->accept_q) ||
238 (sk->shutdown & RCV_SHUTDOWN))
239 mask |= POLLIN | POLLRDNORM;
241 if (sk->state == BT_CLOSED)
244 if (sk->state == BT_CONNECT ||
245 sk->state == BT_CONNECT2 ||
246 sk->state == BT_CONFIG)
249 if (sock_writeable(sk))
250 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
252 set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
257 int bluez_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
259 DECLARE_WAITQUEUE(wait, current);
264 add_wait_queue(sk->sleep, &wait);
265 while (sk->state != state) {
266 set_current_state(TASK_INTERRUPTIBLE);
273 if (signal_pending(current)) {
274 err = sock_intr_errno(timeo);
279 timeo = schedule_timeout(timeo);
283 err = sock_error(sk);
287 set_current_state(TASK_RUNNING);
288 remove_wait_queue(sk->sleep, &wait);
292 struct net_proto_family bluez_sock_family_ops =
294 PF_BLUETOOTH, bluez_sock_create
299 BT_INFO("BlueZ Core ver %s Copyright (C) 2000,2001 Qualcomm Inc",
301 BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
303 proc_mkdir("bluetooth", NULL);
305 sock_register(&bluez_sock_family_ops);
316 void bluez_cleanup(void)
324 sock_unregister(PF_BLUETOOTH);
326 remove_proc_entry("bluetooth", NULL);
330 module_init(bluez_init);
331 module_exit(bluez_cleanup);
333 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
334 MODULE_DESCRIPTION("BlueZ Core ver " VERSION);
335 MODULE_LICENSE("GPL");