tty.c 26.6 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13
   RFCOMM implementation for Linux Bluetooth stack (BlueZ).
   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
   Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 15 16
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
L
Linus Torvalds 已提交
17 18
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

19 20
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
   SOFTWARE IS DISCLAIMED.
*/

/*
 * RFCOMM TTY.
 */

#include <linux/module.h>

#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>

34
#include <linux/capability.h>
L
Linus Torvalds 已提交
35 36
#include <linux/slab.h>
#include <linux/skbuff.h>
37
#include <linux/workqueue.h>
L
Linus Torvalds 已提交
38 39

#include <net/bluetooth/bluetooth.h>
40
#include <net/bluetooth/hci_core.h>
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <net/bluetooth/rfcomm.h>

#define RFCOMM_TTY_MAGIC 0x6d02		/* magic number for rfcomm struct */
#define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV	/* whole lotta rfcomm devices */
#define RFCOMM_TTY_MAJOR 216		/* device node major id of the usb/bluetooth.c driver */
#define RFCOMM_TTY_MINOR 0

static struct tty_driver *rfcomm_tty_driver;

struct rfcomm_dev {
	struct list_head	list;
	atomic_t		refcnt;

	char			name[12];
	int			id;
	unsigned long		flags;
57
	atomic_t		opened;
L
Linus Torvalds 已提交
58 59 60 61
	int			err;

	bdaddr_t		src;
	bdaddr_t		dst;
62
	u8			channel;
L
Linus Torvalds 已提交
63

64
	uint			modem_status;
L
Linus Torvalds 已提交
65 66 67 68

	struct rfcomm_dlc	*dlc;
	struct tty_struct	*tty;
	wait_queue_head_t       wait;
69
	struct work_struct	wakeup_task;
L
Linus Torvalds 已提交
70

71 72
	struct device		*tty_dev;

73
	atomic_t		wmem_alloc;
74 75

	struct sk_buff_head	pending;
L
Linus Torvalds 已提交
76 77 78
};

static LIST_HEAD(rfcomm_dev_list);
79
static DEFINE_SPINLOCK(rfcomm_dev_lock);
L
Linus Torvalds 已提交
80 81 82 83 84

static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);

85
static void rfcomm_tty_wakeup(struct work_struct *work);
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93

/* ---- Device functions ---- */
static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
{
	struct rfcomm_dlc *dlc = dev->dlc;

	BT_DBG("dev %p dlc %p", dev, dlc);

94 95 96 97
	/* Refcount should only hit zero when called from rfcomm_dev_del()
	   which will have taken us off the list. Everything else are
	   refcounting bugs. */
	BUG_ON(!list_empty(&dev->list));
98

L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110
	rfcomm_dlc_lock(dlc);
	/* Detach DLC if it's owned by this dev */
	if (dlc->owner == dev)
		dlc->owner = NULL;
	rfcomm_dlc_unlock(dlc);

	rfcomm_dlc_put(dlc);

	tty_unregister_device(rfcomm_tty_driver, dev->id);

	kfree(dev);

111
	/* It's safe to call module_put() here because socket still
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	   holds reference to this module. */
	module_put(THIS_MODULE);
}

static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
{
	atomic_inc(&dev->refcnt);
}

static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
{
	/* The reason this isn't actually a race, as you no
	   doubt have a little voice screaming at you in your
	   head, is that the refcount should never actually
	   reach zero unless the device has already been taken
	   off the list, in rfcomm_dev_del(). And if that's not
	   true, we'll hit the BUG() in rfcomm_dev_destruct()
	   anyway. */
	if (atomic_dec_and_test(&dev->refcnt))
		rfcomm_dev_destruct(dev);
}

static struct rfcomm_dev *__rfcomm_dev_get(int id)
{
	struct rfcomm_dev *dev;

138
	list_for_each_entry(dev, &rfcomm_dev_list, list)
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148
		if (dev->id == id)
			return dev;

	return NULL;
}

static inline struct rfcomm_dev *rfcomm_dev_get(int id)
{
	struct rfcomm_dev *dev;

149
	spin_lock(&rfcomm_dev_lock);
L
Linus Torvalds 已提交
150 151

	dev = __rfcomm_dev_get(id);
152 153 154 155 156 157 158

	if (dev) {
		if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
			dev = NULL;
		else
			rfcomm_dev_hold(dev);
	}
L
Linus Torvalds 已提交
159

160
	spin_unlock(&rfcomm_dev_lock);
L
Linus Torvalds 已提交
161 162 163 164

	return dev;
}

165 166 167 168 169 170 171 172 173 174 175 176 177
static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
{
	struct hci_dev *hdev;
	struct hci_conn *conn;

	hdev = hci_get_route(&dev->dst, &dev->src);
	if (!hdev)
		return NULL;

	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);

	hci_dev_put(hdev);

178
	return conn ? &conn->dev : NULL;
179 180
}

181 182 183
static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
{
	struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
184
	return sprintf(buf, "%s\n", batostr(&dev->dst));
185 186 187 188 189 190 191 192 193 194 195
}

static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
{
	struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
	return sprintf(buf, "%d\n", dev->channel);
}

static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);

L
Linus Torvalds 已提交
196 197
static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
{
198
	struct rfcomm_dev *dev, *entry;
L
Linus Torvalds 已提交
199 200 201 202
	struct list_head *head = &rfcomm_dev_list, *p;
	int err = 0;

	BT_DBG("id %d channel %d", req->dev_id, req->channel);
203

204
	dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
L
Linus Torvalds 已提交
205 206 207
	if (!dev)
		return -ENOMEM;

208
	spin_lock(&rfcomm_dev_lock);
L
Linus Torvalds 已提交
209 210 211 212

	if (req->dev_id < 0) {
		dev->id = 0;

213 214
		list_for_each_entry(entry, &rfcomm_dev_list, list) {
			if (entry->id != dev->id)
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222
				break;

			dev->id++;
			head = p;
		}
	} else {
		dev->id = req->dev_id;

223
		list_for_each_entry(entry, &rfcomm_dev_list, list) {
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
			if (entry->id == dev->id) {
				err = -EADDRINUSE;
				goto out;
			}

			if (entry->id > dev->id - 1)
				break;

			head = p;
		}
	}

	if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
		err = -ENFILE;
		goto out;
	}

	sprintf(dev->name, "rfcomm%d", dev->id);

	list_add(&dev->list, head);
	atomic_set(&dev->refcnt, 1);

	bacpy(&dev->src, &req->src);
	bacpy(&dev->dst, &req->dst);
	dev->channel = req->channel;

250
	dev->flags = req->flags &
L
Linus Torvalds 已提交
251 252
		((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));

253 254
	atomic_set(&dev->opened, 0);

L
Linus Torvalds 已提交
255
	init_waitqueue_head(&dev->wait);
256
	INIT_WORK(&dev->wakeup_task, rfcomm_tty_wakeup);
L
Linus Torvalds 已提交
257

258 259
	skb_queue_head_init(&dev->pending);

L
Linus Torvalds 已提交
260
	rfcomm_dlc_lock(dlc);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

	if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
		struct sock *sk = dlc->owner;
		struct sk_buff *skb;

		BUG_ON(!sk);

		rfcomm_dlc_throttle(dlc);

		while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
			skb_orphan(skb);
			skb_queue_tail(&dev->pending, skb);
			atomic_sub(skb->len, &sk->sk_rmem_alloc);
		}
	}

L
Linus Torvalds 已提交
277 278 279 280 281 282
	dlc->data_ready   = rfcomm_dev_data_ready;
	dlc->state_change = rfcomm_dev_state_change;
	dlc->modem_status = rfcomm_dev_modem_status;

	dlc->owner = dev;
	dev->dlc   = dlc;
283 284 285

	rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);

L
Linus Torvalds 已提交
286 287
	rfcomm_dlc_unlock(dlc);

288
	/* It's safe to call __module_get() here because socket already
L
Linus Torvalds 已提交
289 290 291 292
	   holds reference to this module. */
	__module_get(THIS_MODULE);

out:
293
	spin_unlock(&rfcomm_dev_lock);
L
Linus Torvalds 已提交
294

I
Ilpo Järvinen 已提交
295 296
	if (err < 0)
		goto free;
L
Linus Torvalds 已提交
297

298
	dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
L
Linus Torvalds 已提交
299

300
	if (IS_ERR(dev->tty_dev)) {
301
		err = PTR_ERR(dev->tty_dev);
302
		list_del(&dev->list);
I
Ilpo Järvinen 已提交
303
		goto free;
304 305
	}

306 307 308 309 310 311 312 313
	dev_set_drvdata(dev->tty_dev, dev);

	if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
		BT_ERR("Failed to create address attribute");

	if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
		BT_ERR("Failed to create channel attribute");

L
Linus Torvalds 已提交
314
	return dev->id;
I
Ilpo Järvinen 已提交
315 316 317 318

free:
	kfree(dev);
	return err;
L
Linus Torvalds 已提交
319 320 321 322 323 324
}

static void rfcomm_dev_del(struct rfcomm_dev *dev)
{
	BT_DBG("dev %p", dev);

325 326 327 328
	BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags));

	if (atomic_read(&dev->opened) > 0)
		return;
329

330
	spin_lock(&rfcomm_dev_lock);
331
	list_del_init(&dev->list);
332
	spin_unlock(&rfcomm_dev_lock);
333

L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	rfcomm_dev_put(dev);
}

/* ---- Send buffer ---- */
static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
{
	/* We can't let it be zero, because we don't get a callback
	   when tx_credits becomes nonzero, hence we'd never wake up */
	return dlc->mtu * (dlc->tx_credits?:1);
}

static void rfcomm_wfree(struct sk_buff *skb)
{
	struct rfcomm_dev *dev = (void *) skb->sk;
	atomic_sub(skb->truesize, &dev->wmem_alloc);
	if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
350
		queue_work(system_nrt_wq, &dev->wakeup_task);
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361
	rfcomm_dev_put(dev);
}

static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
{
	rfcomm_dev_hold(dev);
	atomic_add(skb->truesize, &dev->wmem_alloc);
	skb->sk = (void *) dev;
	skb->destructor = rfcomm_wfree;
}

A
Al Viro 已提交
362
static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
{
	if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
		struct sk_buff *skb = alloc_skb(size, priority);
		if (skb) {
			rfcomm_set_owner_w(skb, dev);
			return skb;
		}
	}
	return NULL;
}

/* ---- Device IOCTLs ---- */

#define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))

static int rfcomm_create_dev(struct sock *sk, void __user *arg)
{
	struct rfcomm_dev_req req;
	struct rfcomm_dlc *dlc;
	int id;

	if (copy_from_user(&req, arg, sizeof(req)))
		return -EFAULT;

387
	BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

	if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
		return -EPERM;

	if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
		/* Socket must be connected */
		if (sk->sk_state != BT_CONNECTED)
			return -EBADFD;

		dlc = rfcomm_pi(sk)->dlc;
		rfcomm_dlc_hold(dlc);
	} else {
		dlc = rfcomm_dlc_alloc(GFP_KERNEL);
		if (!dlc)
			return -ENOMEM;
	}

	id = rfcomm_dev_add(&req, dlc);
	if (id < 0) {
		rfcomm_dlc_put(dlc);
		return id;
	}

	if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
		/* DLC is now used by device.
		 * Socket must be disconnected */
		sk->sk_state = BT_CLOSED;
	}

	return id;
}

static int rfcomm_release_dev(void __user *arg)
{
	struct rfcomm_dev_req req;
	struct rfcomm_dev *dev;

	if (copy_from_user(&req, arg, sizeof(req)))
		return -EFAULT;

428
	BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
L
Linus Torvalds 已提交
429

430 431
	dev = rfcomm_dev_get(req.dev_id);
	if (!dev)
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441
		return -ENODEV;

	if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
		rfcomm_dev_put(dev);
		return -EPERM;
	}

	if (req.flags & (1 << RFCOMM_HANGUP_NOW))
		rfcomm_dlc_close(dev->dlc, 0);

442 443 444 445
	/* Shut down TTY synchronously before freeing rfcomm_dev */
	if (dev->tty)
		tty_vhangup(dev->tty);

446 447
	if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
		rfcomm_dev_del(dev);
L
Linus Torvalds 已提交
448 449 450 451 452 453
	rfcomm_dev_put(dev);
	return 0;
}

static int rfcomm_get_dev_list(void __user *arg)
{
454
	struct rfcomm_dev *dev;
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	struct rfcomm_dev_list_req *dl;
	struct rfcomm_dev_info *di;
	int n = 0, size, err;
	u16 dev_num;

	BT_DBG("");

	if (get_user(dev_num, (u16 __user *) arg))
		return -EFAULT;

	if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
		return -EINVAL;

	size = sizeof(*dl) + dev_num * sizeof(*di);

470 471
	dl = kmalloc(size, GFP_KERNEL);
	if (!dl)
L
Linus Torvalds 已提交
472 473 474 475
		return -ENOMEM;

	di = dl->dev_info;

476
	spin_lock(&rfcomm_dev_lock);
L
Linus Torvalds 已提交
477

478
	list_for_each_entry(dev, &rfcomm_dev_list, list) {
479 480
		if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
			continue;
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489 490
		(di + n)->id      = dev->id;
		(di + n)->flags   = dev->flags;
		(di + n)->state   = dev->dlc->state;
		(di + n)->channel = dev->channel;
		bacpy(&(di + n)->src, &dev->src);
		bacpy(&(di + n)->dst, &dev->dst);
		if (++n >= dev_num)
			break;
	}

491
	spin_unlock(&rfcomm_dev_lock);
L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

	dl->dev_num = n;
	size = sizeof(*dl) + n * sizeof(*di);

	err = copy_to_user(arg, dl, size);
	kfree(dl);

	return err ? -EFAULT : 0;
}

static int rfcomm_get_dev_info(void __user *arg)
{
	struct rfcomm_dev *dev;
	struct rfcomm_dev_info di;
	int err = 0;

	BT_DBG("");

	if (copy_from_user(&di, arg, sizeof(di)))
		return -EFAULT;

513 514
	dev = rfcomm_dev_get(di.id);
	if (!dev)
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		return -ENODEV;

	di.flags   = dev->flags;
	di.channel = dev->channel;
	di.state   = dev->dlc->state;
	bacpy(&di.src, &dev->src);
	bacpy(&di.dst, &dev->dst);

	if (copy_to_user(arg, &di, sizeof(di)))
		err = -EFAULT;

	rfcomm_dev_put(dev);
	return err;
}

int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
{
	BT_DBG("cmd %d arg %p", cmd, arg);

	switch (cmd) {
	case RFCOMMCREATEDEV:
		return rfcomm_create_dev(sk, arg);

	case RFCOMMRELEASEDEV:
		return rfcomm_release_dev(arg);

	case RFCOMMGETDEVLIST:
		return rfcomm_get_dev_list(arg);

	case RFCOMMGETDEVINFO:
		return rfcomm_get_dev_info(arg);
	}

	return -EINVAL;
}

/* ---- DLC callbacks ---- */
static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
{
	struct rfcomm_dev *dev = dlc->owner;
	struct tty_struct *tty;
556

557
	if (!dev) {
L
Linus Torvalds 已提交
558 559 560 561
		kfree_skb(skb);
		return;
	}

562 563
	tty = dev->tty;
	if (!tty || !skb_queue_empty(&dev->pending)) {
564 565 566 567
		skb_queue_tail(&dev->pending, skb);
		return;
	}

L
Linus Torvalds 已提交
568 569
	BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);

P
Paul Fulghum 已提交
570 571
	tty_insert_flip_string(tty, skb->data, skb->len);
	tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580

	kfree_skb(skb);
}

static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
{
	struct rfcomm_dev *dev = dlc->owner;
	if (!dev)
		return;
581

L
Linus Torvalds 已提交
582 583 584 585 586 587 588 589
	BT_DBG("dlc %p dev %p err %d", dlc, dev, err);

	dev->err = err;
	wake_up_interruptible(&dev->wait);

	if (dlc->state == BT_CLOSED) {
		if (!dev->tty) {
			if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
590 591 592 593 594 595 596 597 598 599
				/* Drop DLC lock here to avoid deadlock
				 * 1. rfcomm_dev_get will take rfcomm_dev_lock
				 *    but in rfcomm_dev_add there's lock order:
				 *    rfcomm_dev_lock -> dlc lock
				 * 2. rfcomm_dev_put will deadlock if it's
				 *    the last reference
				 */
				rfcomm_dlc_unlock(dlc);
				if (rfcomm_dev_get(dev->id) == NULL) {
					rfcomm_dlc_lock(dlc);
600
					return;
601
				}
L
Linus Torvalds 已提交
602

603
				rfcomm_dev_del(dev);
L
Linus Torvalds 已提交
604
				rfcomm_dev_put(dev);
605
				rfcomm_dlc_lock(dlc);
L
Linus Torvalds 已提交
606
			}
607
		} else
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616
			tty_hangup(dev->tty);
	}
}

static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
{
	struct rfcomm_dev *dev = dlc->owner;
	if (!dev)
		return;
617

L
Linus Torvalds 已提交
618 619
	BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);

620 621 622 623 624
	if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
		if (dev->tty && !C_CLOCAL(dev->tty))
			tty_hangup(dev->tty);
	}

625
	dev->modem_status =
L
Linus Torvalds 已提交
626 627 628 629 630 631 632
		((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
		((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
		((v24_sig & RFCOMM_V24_IC)  ? TIOCM_RI : 0) |
		((v24_sig & RFCOMM_V24_DV)  ? TIOCM_CD : 0);
}

/* ---- TTY functions ---- */
633
static void rfcomm_tty_wakeup(struct work_struct *work)
L
Linus Torvalds 已提交
634
{
635 636
	struct rfcomm_dev *dev = container_of(work, struct rfcomm_dev,
								wakeup_task);
L
Linus Torvalds 已提交
637 638 639 640 641
	struct tty_struct *tty = dev->tty;
	if (!tty)
		return;

	BT_DBG("dev %p tty %p", dev, tty);
A
Alan Cox 已提交
642
	tty_wakeup(tty);
L
Linus Torvalds 已提交
643 644
}

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
{
	struct tty_struct *tty = dev->tty;
	struct sk_buff *skb;
	int inserted = 0;

	if (!tty)
		return;

	BT_DBG("dev %p tty %p", dev, tty);

	rfcomm_dlc_lock(dev->dlc);

	while ((skb = skb_dequeue(&dev->pending))) {
		inserted += tty_insert_flip_string(tty, skb->data, skb->len);
		kfree_skb(skb);
	}

	rfcomm_dlc_unlock(dev->dlc);

	if (inserted > 0)
		tty_flip_buffer_push(tty);
}

L
Linus Torvalds 已提交
669 670 671 672 673 674 675
static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
{
	DECLARE_WAITQUEUE(wait, current);
	struct rfcomm_dev *dev;
	struct rfcomm_dlc *dlc;
	int err, id;

676
	id = tty->index;
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687

	BT_DBG("tty %p id %d", tty, id);

	/* We don't leak this refcount. For reasons which are not entirely
	   clear, the TTY layer will call our ->close() method even if the
	   open fails. We decrease the refcount there, and decreasing it
	   here too would cause breakage. */
	dev = rfcomm_dev_get(id);
	if (!dev)
		return -ENODEV;

688 689
	BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst),
				dev->channel, atomic_read(&dev->opened));
L
Linus Torvalds 已提交
690

691
	if (atomic_inc_return(&dev->opened) > 1)
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
		return 0;

	dlc = dev->dlc;

	/* Attach TTY and open DLC */

	rfcomm_dlc_lock(dlc);
	tty->driver_data = dev;
	dev->tty = tty;
	rfcomm_dlc_unlock(dlc);
	set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);

	err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
	if (err < 0)
		return err;

	/* Wait for DLC to connect */
	add_wait_queue(&dev->wait, &wait);
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);

		if (dlc->state == BT_CLOSED) {
			err = -dev->err;
			break;
		}

		if (dlc->state == BT_CONNECTED)
			break;

		if (signal_pending(current)) {
			err = -EINTR;
			break;
		}

726
		tty_unlock();
L
Linus Torvalds 已提交
727
		schedule();
728
		tty_lock();
L
Linus Torvalds 已提交
729 730 731 732
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&dev->wait, &wait);

733
	if (err == 0)
734 735
		device_move(dev->tty_dev, rfcomm_get_device(dev),
			    DPM_ORDER_DEV_AFTER_PARENT);
736

737 738 739 740
	rfcomm_tty_copy_pending(dev);

	rfcomm_dlc_unthrottle(dev->dlc);

L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749
	return err;
}

static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
	if (!dev)
		return;

750 751
	BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
						atomic_read(&dev->opened));
L
Linus Torvalds 已提交
752

753
	if (atomic_dec_and_test(&dev->opened)) {
754
		if (dev->tty_dev->parent)
755
			device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
756

L
Linus Torvalds 已提交
757 758 759 760
		/* Close DLC and dettach TTY */
		rfcomm_dlc_close(dev->dlc, 0);

		clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
761
		cancel_work_sync(&dev->wakeup_task);
L
Linus Torvalds 已提交
762 763 764 765 766

		rfcomm_dlc_lock(dev->dlc);
		tty->driver_data = NULL;
		dev->tty = NULL;
		rfcomm_dlc_unlock(dev->dlc);
767 768

		if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) {
769
			spin_lock(&rfcomm_dev_lock);
770
			list_del_init(&dev->list);
771
			spin_unlock(&rfcomm_dev_lock);
772 773 774

			rfcomm_dev_put(dev);
		}
L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
	}

	rfcomm_dev_put(dev);
}

static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
	struct rfcomm_dlc *dlc = dev->dlc;
	struct sk_buff *skb;
	int err = 0, sent = 0, size;

	BT_DBG("tty %p count %d", tty, count);

	while (count) {
		size = min_t(uint, count, dlc->mtu);

		skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
793

L
Linus Torvalds 已提交
794 795 796 797 798 799 800
		if (!skb)
			break;

		skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);

		memcpy(skb_put(skb, size), buf + sent, size);

801 802
		err = rfcomm_dlc_send(dlc, skb);
		if (err < 0) {
L
Linus Torvalds 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
			kfree_skb(skb);
			break;
		}

		sent  += size;
		count -= size;
	}

	return sent ? sent : err;
}

static int rfcomm_tty_write_room(struct tty_struct *tty)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
	int room;

	BT_DBG("tty %p", tty);

821 822 823
	if (!dev || !dev->dlc)
		return 0;

L
Linus Torvalds 已提交
824 825 826
	room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
	if (room < 0)
		room = 0;
827

L
Linus Torvalds 已提交
828 829 830
	return room;
}

831
static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
{
	BT_DBG("tty %p cmd 0x%02x", tty, cmd);

	switch (cmd) {
	case TCGETS:
		BT_DBG("TCGETS is not supported");
		return -ENOIOCTLCMD;

	case TCSETS:
		BT_DBG("TCSETS is not supported");
		return -ENOIOCTLCMD;

	case TIOCMIWAIT:
		BT_DBG("TIOCMIWAIT");
		break;

	case TIOCGSERIAL:
		BT_ERR("TIOCGSERIAL is not supported");
		return -ENOIOCTLCMD;

	case TIOCSSERIAL:
		BT_ERR("TIOCSSERIAL is not supported");
		return -ENOIOCTLCMD;

	case TIOCSERGSTRUCT:
		BT_ERR("TIOCSERGSTRUCT is not supported");
		return -ENOIOCTLCMD;

	case TIOCSERGETLSR:
		BT_ERR("TIOCSERGETLSR is not supported");
		return -ENOIOCTLCMD;

	case TIOCSERCONFIG:
		BT_ERR("TIOCSERCONFIG is not supported");
		return -ENOIOCTLCMD;

	default:
		return -ENOIOCTLCMD;	/* ioctls which we must ignore */

	}

	return -ENOIOCTLCMD;
}

A
Alan Cox 已提交
876
static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
L
Linus Torvalds 已提交
877
{
A
Alan Cox 已提交
878
	struct ktermios *new = tty->termios;
879 880
	int old_baud_rate = tty_termios_baud_rate(old);
	int new_baud_rate = tty_termios_baud_rate(new);
L
Linus Torvalds 已提交
881

882 883 884 885 886 887 888
	u8 baud, data_bits, stop_bits, parity, x_on, x_off;
	u16 changes = 0;

	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;

	BT_DBG("tty %p termios %p", tty, old);

889
	if (!dev || !dev->dlc || !dev->dlc->session)
890 891
		return;

892
	/* Handle turning off CRTSCTS */
893
	if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
894 895 896 897
		BT_DBG("Turning off CRTSCTS unsupported");

	/* Parity on/off and when on, odd/even */
	if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
898
			((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
		changes |= RFCOMM_RPN_PM_PARITY;
		BT_DBG("Parity change detected.");
	}

	/* Mark and space parity are not supported! */
	if (new->c_cflag & PARENB) {
		if (new->c_cflag & PARODD) {
			BT_DBG("Parity is ODD");
			parity = RFCOMM_RPN_PARITY_ODD;
		} else {
			BT_DBG("Parity is EVEN");
			parity = RFCOMM_RPN_PARITY_EVEN;
		}
	} else {
		BT_DBG("Parity is OFF");
		parity = RFCOMM_RPN_PARITY_NONE;
	}

	/* Setting the x_on / x_off characters */
	if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
		BT_DBG("XOFF custom");
		x_on = new->c_cc[VSTOP];
		changes |= RFCOMM_RPN_PM_XON;
	} else {
		BT_DBG("XOFF default");
		x_on = RFCOMM_RPN_XON_CHAR;
	}

	if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
		BT_DBG("XON custom");
		x_off = new->c_cc[VSTART];
		changes |= RFCOMM_RPN_PM_XOFF;
	} else {
		BT_DBG("XON default");
		x_off = RFCOMM_RPN_XOFF_CHAR;
	}

	/* Handle setting of stop bits */
	if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
		changes |= RFCOMM_RPN_PM_STOP;

	/* POSIX does not support 1.5 stop bits and RFCOMM does not
	 * support 2 stop bits. So a request for 2 stop bits gets
	 * translated to 1.5 stop bits */
943
	if (new->c_cflag & CSTOPB)
944
		stop_bits = RFCOMM_RPN_STOP_15;
945
	else
946
		stop_bits = RFCOMM_RPN_STOP_1;
L
Linus Torvalds 已提交
947

948
	/* Handle number of data bits [5-8] */
949
	if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
		changes |= RFCOMM_RPN_PM_DATA;

	switch (new->c_cflag & CSIZE) {
	case CS5:
		data_bits = RFCOMM_RPN_DATA_5;
		break;
	case CS6:
		data_bits = RFCOMM_RPN_DATA_6;
		break;
	case CS7:
		data_bits = RFCOMM_RPN_DATA_7;
		break;
	case CS8:
		data_bits = RFCOMM_RPN_DATA_8;
		break;
	default:
		data_bits = RFCOMM_RPN_DATA_8;
		break;
L
Linus Torvalds 已提交
968
	}
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

	/* Handle baudrate settings */
	if (old_baud_rate != new_baud_rate)
		changes |= RFCOMM_RPN_PM_BITRATE;

	switch (new_baud_rate) {
	case 2400:
		baud = RFCOMM_RPN_BR_2400;
		break;
	case 4800:
		baud = RFCOMM_RPN_BR_4800;
		break;
	case 7200:
		baud = RFCOMM_RPN_BR_7200;
		break;
	case 9600:
		baud = RFCOMM_RPN_BR_9600;
		break;
987
	case 19200:
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
		baud = RFCOMM_RPN_BR_19200;
		break;
	case 38400:
		baud = RFCOMM_RPN_BR_38400;
		break;
	case 57600:
		baud = RFCOMM_RPN_BR_57600;
		break;
	case 115200:
		baud = RFCOMM_RPN_BR_115200;
		break;
	case 230400:
		baud = RFCOMM_RPN_BR_230400;
		break;
	default:
		/* 9600 is standard accordinag to the RFCOMM specification */
		baud = RFCOMM_RPN_BR_9600;
		break;
1006

1007 1008 1009 1010 1011 1012
	}

	if (changes)
		rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
				data_bits, stop_bits, parity,
				RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019
}

static void rfcomm_tty_throttle(struct tty_struct *tty)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;

	BT_DBG("tty %p dev %p", tty, dev);
1020

L
Linus Torvalds 已提交
1021 1022 1023 1024 1025 1026 1027 1028
	rfcomm_dlc_throttle(dev->dlc);
}

static void rfcomm_tty_unthrottle(struct tty_struct *tty)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;

	BT_DBG("tty %p dev %p", tty, dev);
1029

L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038
	rfcomm_dlc_unthrottle(dev->dlc);
}

static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;

	BT_DBG("tty %p dev %p", tty, dev);

1039 1040 1041 1042 1043
	if (!dev || !dev->dlc)
		return 0;

	if (!skb_queue_empty(&dev->dlc->tx_queue))
		return dev->dlc->mtu;
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

	return 0;
}

static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;

	BT_DBG("tty %p dev %p", tty, dev);

1054 1055 1056
	if (!dev || !dev->dlc)
		return;

L
Linus Torvalds 已提交
1057
	skb_queue_purge(&dev->dlc->tx_queue);
A
Alan Cox 已提交
1058
	tty_wakeup(tty);
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
}

static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
{
	BT_DBG("tty %p ch %c", tty, ch);
}

static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
{
	BT_DBG("tty %p timeout %d", tty, timeout);
}

static void rfcomm_tty_hangup(struct tty_struct *tty)
{
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;

	BT_DBG("tty %p dev %p", tty, dev);

1077 1078 1079
	if (!dev)
		return;

L
Linus Torvalds 已提交
1080 1081
	rfcomm_tty_flush_buffer(tty);

1082 1083 1084
	if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
		if (rfcomm_dev_get(dev->id) == NULL)
			return;
L
Linus Torvalds 已提交
1085
		rfcomm_dev_del(dev);
1086 1087
		rfcomm_dev_put(dev);
	}
L
Linus Torvalds 已提交
1088 1089
}

1090
static int rfcomm_tty_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
1091
{
1092
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
L
Linus Torvalds 已提交
1093 1094 1095

	BT_DBG("tty %p dev %p", tty, dev);

1096
	return dev->modem_status;
L
Linus Torvalds 已提交
1097 1098
}

1099
static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
1100
{
1101 1102 1103
	struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
	struct rfcomm_dlc *dlc = dev->dlc;
	u8 v24_sig;
L
Linus Torvalds 已提交
1104 1105 1106

	BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	rfcomm_dlc_get_modem_status(dlc, &v24_sig);

	if (set & TIOCM_DSR || set & TIOCM_DTR)
		v24_sig |= RFCOMM_V24_RTC;
	if (set & TIOCM_RTS || set & TIOCM_CTS)
		v24_sig |= RFCOMM_V24_RTR;
	if (set & TIOCM_RI)
		v24_sig |= RFCOMM_V24_IC;
	if (set & TIOCM_CD)
		v24_sig |= RFCOMM_V24_DV;

	if (clear & TIOCM_DSR || clear & TIOCM_DTR)
		v24_sig &= ~RFCOMM_V24_RTC;
	if (clear & TIOCM_RTS || clear & TIOCM_CTS)
		v24_sig &= ~RFCOMM_V24_RTR;
	if (clear & TIOCM_RI)
		v24_sig &= ~RFCOMM_V24_IC;
	if (clear & TIOCM_CD)
		v24_sig &= ~RFCOMM_V24_DV;

	rfcomm_dlc_set_modem_status(dlc, v24_sig);

	return 0;
L
Linus Torvalds 已提交
1130 1131 1132 1133
}

/* ---- TTY structure ---- */

J
Jeff Dike 已提交
1134
static const struct tty_operations rfcomm_ops = {
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	.open			= rfcomm_tty_open,
	.close			= rfcomm_tty_close,
	.write			= rfcomm_tty_write,
	.write_room		= rfcomm_tty_write_room,
	.chars_in_buffer	= rfcomm_tty_chars_in_buffer,
	.flush_buffer		= rfcomm_tty_flush_buffer,
	.ioctl			= rfcomm_tty_ioctl,
	.throttle		= rfcomm_tty_throttle,
	.unthrottle		= rfcomm_tty_unthrottle,
	.set_termios		= rfcomm_tty_set_termios,
	.send_xchar		= rfcomm_tty_send_xchar,
	.hangup			= rfcomm_tty_hangup,
	.wait_until_sent	= rfcomm_tty_wait_until_sent,
	.tiocmget		= rfcomm_tty_tiocmget,
	.tiocmset		= rfcomm_tty_tiocmset,
};

1152
int __init rfcomm_init_ttys(void)
L
Linus Torvalds 已提交
1153
{
1154 1155
	int error;

L
Linus Torvalds 已提交
1156 1157
	rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
	if (!rfcomm_tty_driver)
1158
		return -ENOMEM;
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165 1166

	rfcomm_tty_driver->owner	= THIS_MODULE;
	rfcomm_tty_driver->driver_name	= "rfcomm";
	rfcomm_tty_driver->name		= "rfcomm";
	rfcomm_tty_driver->major	= RFCOMM_TTY_MAJOR;
	rfcomm_tty_driver->minor_start	= RFCOMM_TTY_MINOR;
	rfcomm_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
	rfcomm_tty_driver->subtype	= SERIAL_TYPE_NORMAL;
1167
	rfcomm_tty_driver->flags	= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
L
Linus Torvalds 已提交
1168 1169
	rfcomm_tty_driver->init_termios	= tty_std_termios;
	rfcomm_tty_driver->init_termios.c_cflag	= B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1170
	rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
L
Linus Torvalds 已提交
1171 1172
	tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);

1173 1174
	error = tty_register_driver(rfcomm_tty_driver);
	if (error) {
L
Linus Torvalds 已提交
1175 1176
		BT_ERR("Can't register RFCOMM TTY driver");
		put_tty_driver(rfcomm_tty_driver);
1177
		return error;
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182 1183 1184
	}

	BT_INFO("RFCOMM TTY layer initialized");

	return 0;
}

1185
void rfcomm_cleanup_ttys(void)
L
Linus Torvalds 已提交
1186 1187 1188 1189
{
	tty_unregister_driver(rfcomm_tty_driver);
	put_tty_driver(rfcomm_tty_driver);
}