File: /usr/src/linux/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.4 2001/07/05 18:42:44 maxk Exp $
29      */
30     #define VERSION "1.1"
31     
32     #include <linux/config.h>
33     #include <linux/module.h>
34     
35     #include <linux/types.h>
36     #include <linux/errno.h>
37     #include <linux/kernel.h>
38     #include <linux/major.h>
39     #include <linux/sched.h>
40     #include <linux/slab.h>
41     #include <linux/skbuff.h>
42     #include <linux/init.h>
43     #include <linux/proc_fs.h>
44     #include <net/sock.h>
45     
46     #if defined(CONFIG_KMOD)
47     #include <linux/kmod.h>
48     #endif
49     
50     #include <net/bluetooth/bluetooth.h>
51     #include <net/bluetooth/bluez.h>
52     
53     /* Bluetooth sockets */
54     static struct net_proto_family *bluez_sock[BLUEZ_MAX_PROTO];
55     
56     int bluez_sock_register(int proto, struct net_proto_family *ops)
57     {
58     	if (proto > BLUEZ_MAX_PROTO)
59     		return -EINVAL;
60     
61     	if (bluez_sock[proto])
62     		return -EEXIST;
63     
64     	bluez_sock[proto] = ops;
65     	return 0;
66     }
67     
68     int bluez_sock_unregister(int proto)
69     {
70     	if (proto > BLUEZ_MAX_PROTO)
71     		return -EINVAL;
72     
73     	if (!bluez_sock[proto])
74     		return -ENOENT;
75     
76     	bluez_sock[proto] = NULL;
77     	return 0;
78     }
79     
80     static int bluez_sock_create(struct socket *sock, int proto)
81     {
82     	if (proto > BLUEZ_MAX_PROTO)
83     		return -EINVAL;
84     
85     #if defined(CONFIG_KMOD)
86     	if (!bluez_sock[proto]) {
87     		char module_name[30];
88     		sprintf(module_name, "bt-proto-%d", proto);
89     		request_module(module_name);
90     	}
91     #endif
92     
93     	if (!bluez_sock[proto])
94     		return -ENOENT;
95     
96     	return bluez_sock[proto]->create(sock, proto);
97     }
98     
99     void bluez_sock_link(struct bluez_sock_list *l, struct sock *sk)
100     {
101     	write_lock(&l->lock);
102     
103     	sk->next = l->head;
104     	l->head = sk;
105     	sock_hold(sk);
106     
107     	write_unlock(&l->lock);
108     }
109     
110     void bluez_sock_unlink(struct bluez_sock_list *l, struct sock *sk)
111     {
112     	struct sock **skp;
113     
114     	write_lock(&l->lock);
115     	for (skp = &l->head; *skp; skp = &((*skp)->next)) {
116     		if (*skp == sk) {
117     			*skp = sk->next;
118     			__sock_put(sk);
119     			break;
120     		}
121     	}
122     	write_unlock(&l->lock);
123     }
124     
125     struct net_proto_family bluez_sock_family_ops =
126     {
127     	PF_BLUETOOTH, bluez_sock_create
128     };
129     
130     int bluez_init(void)
131     {
132     	INF("BlueZ HCI Core ver %s Copyright (C) 2000,2001 Qualcomm Inc",
133     		 VERSION);
134     	INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
135     
136     	proc_mkdir("bluetooth", NULL);
137     
138     	sock_register(&bluez_sock_family_ops);
139     
140     	/* Init HCI Core */
141     	hci_core_init();
142     
143     	/* Init sockets */
144     	hci_sock_init();
145     
146     	return 0;
147     }
148     
149     void bluez_cleanup(void)
150     {
151     	/* Release socket */
152     	hci_sock_cleanup();
153     
154     	/* Release core */
155     	hci_core_cleanup();
156     
157     	sock_unregister(PF_BLUETOOTH);
158     
159     	remove_proc_entry("bluetooth", NULL);
160     }
161     
162     #ifdef MODULE
163     module_init(bluez_init);
164     module_exit(bluez_cleanup);
165     
166     MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
167     MODULE_DESCRIPTION("BlueZ HCI Core ver " VERSION);
168     #endif
169