Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / net / bluetooth / cmtp / core.c
blob7f26a5a19ff6d8fb87ef5404e32eb5e2ce0b5b53
1 /*
2 CMTP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/fcntl.h>
32 #include <linux/freezer.h>
33 #include <linux/skbuff.h>
34 #include <linux/socket.h>
35 #include <linux/ioctl.h>
36 #include <linux/file.h>
37 #include <linux/init.h>
38 #include <linux/kthread.h>
39 #include <net/sock.h>
41 #include <linux/isdn/capilli.h>
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/l2cap.h>
46 #include "cmtp.h"
48 #define VERSION "1.0"
50 static DECLARE_RWSEM(cmtp_session_sem);
51 static LIST_HEAD(cmtp_session_list);
53 static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr)
55 struct cmtp_session *session;
57 BT_DBG("");
59 list_for_each_entry(session, &cmtp_session_list, list)
60 if (!bacmp(bdaddr, &session->bdaddr))
61 return session;
63 return NULL;
66 static void __cmtp_link_session(struct cmtp_session *session)
68 list_add(&session->list, &cmtp_session_list);
71 static void __cmtp_unlink_session(struct cmtp_session *session)
73 list_del(&session->list);
76 static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
78 u32 valid_flags = BIT(CMTP_LOOPBACK);
79 memset(ci, 0, sizeof(*ci));
80 bacpy(&ci->bdaddr, &session->bdaddr);
82 ci->flags = session->flags & valid_flags;
83 ci->state = session->state;
85 ci->num = session->num;
89 static inline int cmtp_alloc_block_id(struct cmtp_session *session)
91 int i, id = -1;
93 for (i = 0; i < 16; i++)
94 if (!test_and_set_bit(i, &session->blockids)) {
95 id = i;
96 break;
99 return id;
102 static inline void cmtp_free_block_id(struct cmtp_session *session, int id)
104 clear_bit(id, &session->blockids);
107 static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count)
109 struct sk_buff *skb = session->reassembly[id], *nskb;
110 int size;
112 BT_DBG("session %p buf %p count %d", session, buf, count);
114 size = (skb) ? skb->len + count : count;
116 nskb = alloc_skb(size, GFP_ATOMIC);
117 if (!nskb) {
118 BT_ERR("Can't allocate memory for CAPI message");
119 return;
122 if (skb && (skb->len > 0))
123 skb_copy_from_linear_data(skb, skb_put(nskb, skb->len), skb->len);
125 skb_put_data(nskb, buf, count);
127 session->reassembly[id] = nskb;
129 kfree_skb(skb);
132 static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb)
134 __u8 hdr, hdrlen, id;
135 __u16 len;
137 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
139 while (skb->len > 0) {
140 hdr = skb->data[0];
142 switch (hdr & 0xc0) {
143 case 0x40:
144 hdrlen = 2;
145 len = skb->data[1];
146 break;
147 case 0x80:
148 hdrlen = 3;
149 len = skb->data[1] | (skb->data[2] << 8);
150 break;
151 default:
152 hdrlen = 1;
153 len = 0;
154 break;
157 id = (hdr & 0x3c) >> 2;
159 BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id);
161 if (hdrlen + len > skb->len) {
162 BT_ERR("Wrong size or header information in CMTP frame");
163 break;
166 if (len == 0) {
167 skb_pull(skb, hdrlen);
168 continue;
171 switch (hdr & 0x03) {
172 case 0x00:
173 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
174 cmtp_recv_capimsg(session, session->reassembly[id]);
175 session->reassembly[id] = NULL;
176 break;
177 case 0x01:
178 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
179 break;
180 default:
181 kfree_skb(session->reassembly[id]);
182 session->reassembly[id] = NULL;
183 break;
186 skb_pull(skb, hdrlen + len);
189 kfree_skb(skb);
190 return 0;
193 static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len)
195 struct socket *sock = session->sock;
196 struct kvec iv = { data, len };
197 struct msghdr msg;
199 BT_DBG("session %p data %p len %d", session, data, len);
201 if (!len)
202 return 0;
204 memset(&msg, 0, sizeof(msg));
206 return kernel_sendmsg(sock, &msg, &iv, 1, len);
209 static void cmtp_process_transmit(struct cmtp_session *session)
211 struct sk_buff *skb, *nskb;
212 unsigned char *hdr;
213 unsigned int size, tail;
215 BT_DBG("session %p", session);
217 nskb = alloc_skb(session->mtu, GFP_ATOMIC);
218 if (!nskb) {
219 BT_ERR("Can't allocate memory for new frame");
220 return;
223 while ((skb = skb_dequeue(&session->transmit))) {
224 struct cmtp_scb *scb = (void *) skb->cb;
226 tail = session->mtu - nskb->len;
227 if (tail < 5) {
228 cmtp_send_frame(session, nskb->data, nskb->len);
229 skb_trim(nskb, 0);
230 tail = session->mtu;
233 size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len);
235 if (scb->id < 0) {
236 scb->id = cmtp_alloc_block_id(session);
237 if (scb->id < 0) {
238 skb_queue_head(&session->transmit, skb);
239 break;
243 if (size < 256) {
244 hdr = skb_put(nskb, 2);
245 hdr[0] = 0x40
246 | ((scb->id << 2) & 0x3c)
247 | ((skb->len == size) ? 0x00 : 0x01);
248 hdr[1] = size;
249 } else {
250 hdr = skb_put(nskb, 3);
251 hdr[0] = 0x80
252 | ((scb->id << 2) & 0x3c)
253 | ((skb->len == size) ? 0x00 : 0x01);
254 hdr[1] = size & 0xff;
255 hdr[2] = size >> 8;
258 skb_copy_from_linear_data(skb, skb_put(nskb, size), size);
259 skb_pull(skb, size);
261 if (skb->len > 0) {
262 skb_queue_head(&session->transmit, skb);
263 } else {
264 cmtp_free_block_id(session, scb->id);
265 if (scb->data) {
266 cmtp_send_frame(session, nskb->data, nskb->len);
267 skb_trim(nskb, 0);
269 kfree_skb(skb);
273 cmtp_send_frame(session, nskb->data, nskb->len);
275 kfree_skb(nskb);
278 static int cmtp_session(void *arg)
280 struct cmtp_session *session = arg;
281 struct sock *sk = session->sock->sk;
282 struct sk_buff *skb;
283 DEFINE_WAIT_FUNC(wait, woken_wake_function);
285 BT_DBG("session %p", session);
287 set_user_nice(current, -15);
289 add_wait_queue(sk_sleep(sk), &wait);
290 while (1) {
291 /* Ensure session->terminate is updated */
292 smp_mb__before_atomic();
294 if (atomic_read(&session->terminate))
295 break;
296 if (sk->sk_state != BT_CONNECTED)
297 break;
299 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
300 skb_orphan(skb);
301 if (!skb_linearize(skb))
302 cmtp_recv_frame(session, skb);
303 else
304 kfree_skb(skb);
307 cmtp_process_transmit(session);
309 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
311 remove_wait_queue(sk_sleep(sk), &wait);
313 down_write(&cmtp_session_sem);
315 if (!(session->flags & BIT(CMTP_LOOPBACK)))
316 cmtp_detach_device(session);
318 fput(session->sock->file);
320 __cmtp_unlink_session(session);
322 up_write(&cmtp_session_sem);
324 kfree(session);
325 module_put_and_exit(0);
326 return 0;
329 int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
331 u32 valid_flags = BIT(CMTP_LOOPBACK);
332 struct cmtp_session *session, *s;
333 int i, err;
335 BT_DBG("");
337 if (!l2cap_is_socket(sock))
338 return -EBADFD;
340 if (req->flags & ~valid_flags)
341 return -EINVAL;
343 session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL);
344 if (!session)
345 return -ENOMEM;
347 down_write(&cmtp_session_sem);
349 s = __cmtp_get_session(&l2cap_pi(sock->sk)->chan->dst);
350 if (s && s->state == BT_CONNECTED) {
351 err = -EEXIST;
352 goto failed;
355 bacpy(&session->bdaddr, &l2cap_pi(sock->sk)->chan->dst);
357 session->mtu = min_t(uint, l2cap_pi(sock->sk)->chan->omtu,
358 l2cap_pi(sock->sk)->chan->imtu);
360 BT_DBG("mtu %d", session->mtu);
362 sprintf(session->name, "%pMR", &session->bdaddr);
364 session->sock = sock;
365 session->state = BT_CONFIG;
367 init_waitqueue_head(&session->wait);
369 session->msgnum = CMTP_INITIAL_MSGNUM;
371 INIT_LIST_HEAD(&session->applications);
373 skb_queue_head_init(&session->transmit);
375 for (i = 0; i < 16; i++)
376 session->reassembly[i] = NULL;
378 session->flags = req->flags;
380 __cmtp_link_session(session);
382 __module_get(THIS_MODULE);
383 session->task = kthread_run(cmtp_session, session, "kcmtpd_ctr_%d",
384 session->num);
385 if (IS_ERR(session->task)) {
386 module_put(THIS_MODULE);
387 err = PTR_ERR(session->task);
388 goto unlink;
391 if (!(session->flags & BIT(CMTP_LOOPBACK))) {
392 err = cmtp_attach_device(session);
393 if (err < 0) {
394 atomic_inc(&session->terminate);
395 wake_up_interruptible(sk_sleep(session->sock->sk));
396 up_write(&cmtp_session_sem);
397 return err;
401 up_write(&cmtp_session_sem);
402 return 0;
404 unlink:
405 __cmtp_unlink_session(session);
407 failed:
408 up_write(&cmtp_session_sem);
409 kfree(session);
410 return err;
413 int cmtp_del_connection(struct cmtp_conndel_req *req)
415 u32 valid_flags = 0;
416 struct cmtp_session *session;
417 int err = 0;
419 BT_DBG("");
421 if (req->flags & ~valid_flags)
422 return -EINVAL;
424 down_read(&cmtp_session_sem);
426 session = __cmtp_get_session(&req->bdaddr);
427 if (session) {
428 /* Flush the transmit queue */
429 skb_queue_purge(&session->transmit);
431 /* Stop session thread */
432 atomic_inc(&session->terminate);
434 /* Ensure session->terminate is updated */
435 smp_mb__after_atomic();
437 wake_up_interruptible(sk_sleep(session->sock->sk));
438 } else
439 err = -ENOENT;
441 up_read(&cmtp_session_sem);
442 return err;
445 int cmtp_get_connlist(struct cmtp_connlist_req *req)
447 struct cmtp_session *session;
448 int err = 0, n = 0;
450 BT_DBG("");
452 down_read(&cmtp_session_sem);
454 list_for_each_entry(session, &cmtp_session_list, list) {
455 struct cmtp_conninfo ci;
457 __cmtp_copy_session(session, &ci);
459 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
460 err = -EFAULT;
461 break;
464 if (++n >= req->cnum)
465 break;
467 req->ci++;
469 req->cnum = n;
471 up_read(&cmtp_session_sem);
472 return err;
475 int cmtp_get_conninfo(struct cmtp_conninfo *ci)
477 struct cmtp_session *session;
478 int err = 0;
480 down_read(&cmtp_session_sem);
482 session = __cmtp_get_session(&ci->bdaddr);
483 if (session)
484 __cmtp_copy_session(session, ci);
485 else
486 err = -ENOENT;
488 up_read(&cmtp_session_sem);
489 return err;
493 static int __init cmtp_init(void)
495 BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION);
497 cmtp_init_sockets();
499 return 0;
502 static void __exit cmtp_exit(void)
504 cmtp_cleanup_sockets();
507 module_init(cmtp_init);
508 module_exit(cmtp_exit);
510 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
511 MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION);
512 MODULE_VERSION(VERSION);
513 MODULE_LICENSE("GPL");
514 MODULE_ALIAS("bt-proto-5");