2007-02-09 22:24:33 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
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
|
2007-02-09 22:24:33 +08:00
|
|
|
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
|
2005-04-17 06:20:36 +08:00
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
|
|
|
|
COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
|
2005-04-17 06:20:36 +08:00
|
|
|
SOFTWARE IS DISCLAIMED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFCOMM TTY.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/tty_driver.h>
|
|
|
|
#include <linux/tty_flip.h>
|
|
|
|
|
|
|
|
#include <net/bluetooth/bluetooth.h>
|
2006-07-06 19:09:02 +08:00
|
|
|
#include <net/bluetooth/hci_core.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#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 {
|
2012-04-02 19:54:50 +08:00
|
|
|
struct tty_port port;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct list_head list;
|
|
|
|
|
|
|
|
char name[12];
|
|
|
|
int id;
|
|
|
|
unsigned long flags;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
bdaddr_t src;
|
|
|
|
bdaddr_t dst;
|
2010-12-01 22:58:23 +08:00
|
|
|
u8 channel;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-12-01 22:58:23 +08:00
|
|
|
uint modem_status;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct rfcomm_dlc *dlc;
|
2014-01-07 04:23:52 +08:00
|
|
|
wait_queue_head_t conn_wait;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-18 06:58:57 +08:00
|
|
|
struct device *tty_dev;
|
|
|
|
|
2010-12-01 22:58:23 +08:00
|
|
|
atomic_t wmem_alloc;
|
2008-07-15 02:13:52 +08:00
|
|
|
|
|
|
|
struct sk_buff_head pending;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(rfcomm_dev_list);
|
2011-12-28 01:28:45 +08:00
|
|
|
static DEFINE_SPINLOCK(rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* ---- Device functions ---- */
|
2012-04-02 19:54:51 +08:00
|
|
|
|
|
|
|
static void rfcomm_dev_destruct(struct tty_port *port)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-04-02 19:54:51 +08:00
|
|
|
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct rfcomm_dlc *dlc = dev->dlc;
|
|
|
|
|
|
|
|
BT_DBG("dev %p dlc %p", dev, dlc);
|
|
|
|
|
2013-07-29 23:08:09 +08:00
|
|
|
spin_lock(&rfcomm_dev_lock);
|
|
|
|
list_del(&dev->list);
|
|
|
|
spin_unlock(&rfcomm_dev_lock);
|
2007-07-11 15:23:41 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
/* It's safe to call module_put() here because socket still
|
2005-04-17 06:20:36 +08:00
|
|
|
holds reference to this module. */
|
|
|
|
module_put(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:23:51 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
return conn ? &conn->dev : NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-29 23:08:11 +08:00
|
|
|
/* device-specific initialization: open the dlc */
|
|
|
|
static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
2014-01-07 04:23:52 +08:00
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
prepare_to_wait(&dev->conn_wait, &wait, TASK_INTERRUPTIBLE);
|
2013-07-29 23:08:11 +08:00
|
|
|
|
2014-01-07 04:23:52 +08:00
|
|
|
if (dev->dlc->state == BT_CLOSED) {
|
|
|
|
err = -dev->err;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->dlc->state == BT_CONNECTED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
err = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty_unlock(tty);
|
|
|
|
schedule();
|
|
|
|
tty_lock(tty);
|
|
|
|
}
|
|
|
|
finish_wait(&dev->conn_wait, &wait);
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
device_move(dev->tty_dev, rfcomm_get_device(dev),
|
|
|
|
DPM_ORDER_DEV_AFTER_PARENT);
|
|
|
|
|
|
|
|
return err;
|
2013-07-29 23:08:11 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:59:01 +08:00
|
|
|
/* we block the open until the dlc->state becomes BT_CONNECTED */
|
|
|
|
static int rfcomm_dev_carrier_raised(struct tty_port *port)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
|
|
|
|
|
|
|
return (dev->dlc->state == BT_CONNECTED);
|
|
|
|
}
|
|
|
|
|
2013-07-29 23:08:11 +08:00
|
|
|
/* device-specific cleanup: close the dlc */
|
|
|
|
static void rfcomm_dev_shutdown(struct tty_port *port)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
|
|
|
|
|
|
|
if (dev->tty_dev->parent)
|
|
|
|
device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
|
|
|
|
|
|
|
|
/* close the dlc */
|
|
|
|
rfcomm_dlc_close(dev->dlc, 0);
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:54:51 +08:00
|
|
|
static const struct tty_port_operations rfcomm_port_ops = {
|
|
|
|
.destruct = rfcomm_dev_destruct,
|
2013-07-29 23:08:11 +08:00
|
|
|
.activate = rfcomm_dev_activate,
|
|
|
|
.shutdown = rfcomm_dev_shutdown,
|
2012-04-02 19:54:51 +08:00
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static struct rfcomm_dev *__rfcomm_dev_get(int id)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev;
|
|
|
|
|
2011-11-01 16:58:56 +08:00
|
|
|
list_for_each_entry(dev, &rfcomm_dev_list, list)
|
2005-04-17 06:20:36 +08:00
|
|
|
if (dev->id == id)
|
|
|
|
return dev;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-23 15:04:18 +08:00
|
|
|
static struct rfcomm_dev *rfcomm_dev_get(int id)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev;
|
|
|
|
|
2011-12-28 01:28:45 +08:00
|
|
|
spin_lock(&rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
dev = __rfcomm_dev_get(id);
|
2007-07-11 15:23:41 +08:00
|
|
|
|
|
|
|
if (dev) {
|
|
|
|
if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
|
|
|
|
dev = NULL;
|
|
|
|
else
|
2012-04-02 19:54:51 +08:00
|
|
|
tty_port_get(&dev->port);
|
2007-07-11 15:23:41 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-12-28 01:28:45 +08:00
|
|
|
spin_unlock(&rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2007-10-20 20:52:38 +08:00
|
|
|
static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
|
2012-09-25 17:49:44 +08:00
|
|
|
return sprintf(buf, "%pMR\n", &dev->dst);
|
2007-10-20 20:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
|
|
|
|
{
|
2011-11-01 16:58:56 +08:00
|
|
|
struct rfcomm_dev *dev, *entry;
|
2012-03-08 02:20:14 +08:00
|
|
|
struct list_head *head = &rfcomm_dev_list;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
BT_DBG("id %d channel %d", req->dev_id, req->channel);
|
2007-02-09 22:24:33 +08:00
|
|
|
|
2006-07-06 21:40:09 +08:00
|
|
|
dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-12-28 01:28:45 +08:00
|
|
|
spin_lock(&rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (req->dev_id < 0) {
|
|
|
|
dev->id = 0;
|
|
|
|
|
2011-11-01 16:58:56 +08:00
|
|
|
list_for_each_entry(entry, &rfcomm_dev_list, list) {
|
|
|
|
if (entry->id != dev->id)
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
dev->id++;
|
2012-03-08 02:20:14 +08:00
|
|
|
head = &entry->list;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dev->id = req->dev_id;
|
|
|
|
|
2011-11-01 16:58:56 +08:00
|
|
|
list_for_each_entry(entry, &rfcomm_dev_list, list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (entry->id == dev->id) {
|
|
|
|
err = -EADDRINUSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->id > dev->id - 1)
|
|
|
|
break;
|
|
|
|
|
2012-03-08 02:20:14 +08:00
|
|
|
head = &entry->list;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
bacpy(&dev->src, &req->src);
|
|
|
|
bacpy(&dev->dst, &req->dst);
|
|
|
|
dev->channel = req->channel;
|
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
dev->flags = req->flags &
|
2005-04-17 06:20:36 +08:00
|
|
|
((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
|
|
|
|
|
2012-04-02 19:54:50 +08:00
|
|
|
tty_port_init(&dev->port);
|
2012-04-02 19:54:51 +08:00
|
|
|
dev->port.ops = &rfcomm_port_ops;
|
2014-01-07 04:23:52 +08:00
|
|
|
init_waitqueue_head(&dev->conn_wait);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-07-15 02:13:52 +08:00
|
|
|
skb_queue_head_init(&dev->pending);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
rfcomm_dlc_lock(dlc);
|
2008-07-15 02:13:52 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
2008-07-15 02:13:52 +08:00
|
|
|
|
|
|
|
rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
rfcomm_dlc_unlock(dlc);
|
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
/* It's safe to call __module_get() here because socket already
|
2005-04-17 06:20:36 +08:00
|
|
|
holds reference to this module. */
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
|
|
|
|
out:
|
2011-12-28 01:28:45 +08:00
|
|
|
spin_unlock(&rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-12-15 15:18:00 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto free;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-08-08 03:47:47 +08:00
|
|
|
dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
|
|
|
|
dev->id, NULL);
|
2007-07-11 15:23:41 +08:00
|
|
|
if (IS_ERR(dev->tty_dev)) {
|
2007-07-26 15:12:25 +08:00
|
|
|
err = PTR_ERR(dev->tty_dev);
|
2013-07-29 23:08:09 +08:00
|
|
|
spin_lock(&rfcomm_dev_lock);
|
2007-07-11 15:23:41 +08:00
|
|
|
list_del(&dev->list);
|
2013-07-29 23:08:09 +08:00
|
|
|
spin_unlock(&rfcomm_dev_lock);
|
2008-12-15 15:18:00 +08:00
|
|
|
goto free;
|
2007-07-11 15:23:41 +08:00
|
|
|
}
|
|
|
|
|
2007-10-20 20:52:38 +08:00
|
|
|
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");
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return dev->id;
|
2008-12-15 15:18:00 +08:00
|
|
|
|
|
|
|
free:
|
|
|
|
kfree(dev);
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---- 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);
|
2013-07-29 23:08:08 +08:00
|
|
|
if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
|
|
|
|
tty_port_tty_wakeup(&dev->port);
|
2012-04-02 19:54:51 +08:00
|
|
|
tty_port_put(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-05-23 15:04:18 +08:00
|
|
|
static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-04-02 19:54:51 +08:00
|
|
|
tty_port_get(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
atomic_add(skb->truesize, &dev->wmem_alloc);
|
|
|
|
skb->sk = (void *) dev;
|
|
|
|
skb->destructor = rfcomm_wfree;
|
|
|
|
}
|
|
|
|
|
2005-10-07 14:46:04 +08:00
|
|
|
static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2007-07-11 15:23:41 +08:00
|
|
|
BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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;
|
2013-07-29 23:08:08 +08:00
|
|
|
struct tty_struct *tty;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (copy_from_user(&req, arg, sizeof(req)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2007-07-11 15:23:41 +08:00
|
|
|
BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-12-01 22:58:23 +08:00
|
|
|
dev = rfcomm_dev_get(req.dev_id);
|
|
|
|
if (!dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
|
2012-04-02 19:54:51 +08:00
|
|
|
tty_port_put(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.flags & (1 << RFCOMM_HANGUP_NOW))
|
|
|
|
rfcomm_dlc_close(dev->dlc, 0);
|
|
|
|
|
2007-07-11 15:18:15 +08:00
|
|
|
/* Shut down TTY synchronously before freeing rfcomm_dev */
|
2013-07-29 23:08:08 +08:00
|
|
|
tty = tty_port_tty_get(&dev->port);
|
|
|
|
if (tty) {
|
|
|
|
tty_vhangup(tty);
|
|
|
|
tty_kref_put(tty);
|
|
|
|
}
|
2007-07-11 15:18:15 +08:00
|
|
|
|
2014-01-07 04:23:50 +08:00
|
|
|
if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags) &&
|
|
|
|
!test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags))
|
2013-07-29 23:08:12 +08:00
|
|
|
tty_port_put(&dev->port);
|
|
|
|
|
2012-04-02 19:54:51 +08:00
|
|
|
tty_port_put(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rfcomm_get_dev_list(void __user *arg)
|
|
|
|
{
|
2011-11-01 16:58:56 +08:00
|
|
|
struct rfcomm_dev *dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
|
|
|
|
2012-08-15 19:31:49 +08:00
|
|
|
dl = kzalloc(size, GFP_KERNEL);
|
2010-12-01 22:58:23 +08:00
|
|
|
if (!dl)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
di = dl->dev_info;
|
|
|
|
|
2011-12-28 01:28:45 +08:00
|
|
|
spin_lock(&rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-11-01 16:58:56 +08:00
|
|
|
list_for_each_entry(dev, &rfcomm_dev_list, list) {
|
2007-07-11 15:23:41 +08:00
|
|
|
if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
|
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
(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;
|
|
|
|
}
|
|
|
|
|
2011-12-28 01:28:45 +08:00
|
|
|
spin_unlock(&rfcomm_dev_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-12-01 22:58:23 +08:00
|
|
|
dev = rfcomm_dev_get(di.id);
|
|
|
|
if (!dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
|
|
|
|
2012-04-02 19:54:51 +08:00
|
|
|
tty_port_put(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
2007-02-09 22:24:33 +08:00
|
|
|
|
2008-07-15 02:13:52 +08:00
|
|
|
if (!dev) {
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree_skb(skb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-03 22:53:06 +08:00
|
|
|
if (!skb_queue_empty(&dev->pending)) {
|
2008-07-15 02:13:52 +08:00
|
|
|
skb_queue_tail(&dev->pending, skb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-03 22:53:06 +08:00
|
|
|
BT_DBG("dlc %p len %d", dlc, skb->len);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-01-03 22:53:04 +08:00
|
|
|
tty_insert_flip_string(&dev->port, skb->data, skb->len);
|
2013-01-03 22:53:06 +08:00
|
|
|
tty_flip_buffer_push(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = dlc->owner;
|
|
|
|
if (!dev)
|
|
|
|
return;
|
2007-02-09 22:24:33 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
|
|
|
|
|
|
|
|
dev->err = err;
|
2014-01-07 04:23:52 +08:00
|
|
|
wake_up_interruptible(&dev->conn_wait);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-07 04:23:52 +08:00
|
|
|
if (dlc->state == BT_CLOSED)
|
Bluetooth: don't release the port in rfcomm_dev_state_change()
When the dlc is closed, rfcomm_dev_state_change() tries to release the
port in the case it cannot get a reference to the tty. However this is
racy and not even needed.
Infact as Peter Hurley points out:
1. Only consider dlcs that are 'stolen' from a connected socket, ie.
reused. Allocated dlcs cannot have been closed prior to port
activate and so for these dlcs a tty reference will always be avail
in rfcomm_dev_state_change() -- except for the conditions covered by
#2b below.
2. If a tty was at some point previously created for this rfcomm, then
either
(a) the tty reference is still avail, so rfcomm_dev_state_change()
will perform a hangup. So nothing to do, or,
(b) the tty reference is no longer avail, and the tty_port will be
destroyed by the last tty_port_put() in rfcomm_tty_cleanup.
Again, no action required.
3. Prior to obtaining the dlc lock in rfcomm_dev_add(),
rfcomm_dev_state_change() will not 'see' a rfcomm_dev so nothing to
do here.
4. After releasing the dlc lock in rfcomm_dev_add(),
rfcomm_dev_state_change() will 'see' an incomplete rfcomm_dev if a
tty reference could not be obtained. Again, the best thing to do here
is nothing. Any future attempted open() will block on
rfcomm_dev_carrier_raised(). The unconnected device will exist until
released by ioctl(RFCOMMRELEASEDEV).
The patch removes the aforementioned code and uses the
tty_port_tty_hangup() helper to hangup the tty.
Signed-off-by: Gianluca Anzolin <gianluca@sottospazio.it>
Reviewed-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
2013-08-28 00:28:46 +08:00
|
|
|
tty_port_tty_hangup(&dev->port, false);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = dlc->owner;
|
|
|
|
if (!dev)
|
|
|
|
return;
|
2005-08-10 11:28:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
|
|
|
|
|
2013-07-29 23:08:08 +08:00
|
|
|
if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
|
|
|
|
tty_port_tty_hangup(&dev->port, true);
|
2005-08-10 11:28:21 +08:00
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
dev->modem_status =
|
2005-04-17 06:20:36 +08:00
|
|
|
((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 ---- */
|
2008-07-15 02:13:52 +08:00
|
|
|
static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
int inserted = 0;
|
|
|
|
|
2013-01-03 22:53:06 +08:00
|
|
|
BT_DBG("dev %p", dev);
|
2008-07-15 02:13:52 +08:00
|
|
|
|
|
|
|
rfcomm_dlc_lock(dev->dlc);
|
|
|
|
|
|
|
|
while ((skb = skb_dequeue(&dev->pending))) {
|
2013-01-03 22:53:04 +08:00
|
|
|
inserted += tty_insert_flip_string(&dev->port, skb->data,
|
|
|
|
skb->len);
|
2008-07-15 02:13:52 +08:00
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
rfcomm_dlc_unlock(dev->dlc);
|
|
|
|
|
|
|
|
if (inserted > 0)
|
2013-01-03 22:53:06 +08:00
|
|
|
tty_flip_buffer_push(&dev->port);
|
2008-07-15 02:13:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-29 23:08:10 +08:00
|
|
|
/* do the reverse of install, clearing the tty fields and releasing the
|
|
|
|
* reference to tty_port
|
|
|
|
*/
|
|
|
|
static void rfcomm_tty_cleanup(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = tty->driver_data;
|
|
|
|
|
|
|
|
clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
|
|
|
|
|
|
|
|
rfcomm_dlc_lock(dev->dlc);
|
|
|
|
tty->driver_data = NULL;
|
|
|
|
rfcomm_dlc_unlock(dev->dlc);
|
|
|
|
|
2013-07-29 23:08:13 +08:00
|
|
|
/*
|
|
|
|
* purge the dlc->tx_queue to avoid circular dependencies
|
|
|
|
* between dev and dlc
|
|
|
|
*/
|
|
|
|
skb_queue_purge(&dev->dlc->tx_queue);
|
|
|
|
|
2013-07-29 23:08:10 +08:00
|
|
|
tty_port_put(&dev->port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we acquire the tty_port reference since it's here the tty is first used
|
|
|
|
* by setting the termios. We also populate the driver_data field and install
|
|
|
|
* the tty port
|
|
|
|
*/
|
|
|
|
static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev;
|
|
|
|
struct rfcomm_dlc *dlc;
|
2013-07-29 23:08:10 +08:00
|
|
|
int err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-07-29 23:08:10 +08:00
|
|
|
dev = rfcomm_dev_get(tty->index);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!dev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
dlc = dev->dlc;
|
|
|
|
|
|
|
|
/* Attach TTY and open DLC */
|
|
|
|
rfcomm_dlc_lock(dlc);
|
|
|
|
tty->driver_data = dev;
|
|
|
|
rfcomm_dlc_unlock(dlc);
|
|
|
|
set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
|
|
|
|
|
2013-07-29 23:08:10 +08:00
|
|
|
/* install the tty_port */
|
|
|
|
err = tty_port_install(&dev->port, driver, tty);
|
2014-01-07 04:23:50 +08:00
|
|
|
if (err) {
|
2013-07-29 23:08:11 +08:00
|
|
|
rfcomm_tty_cleanup(tty);
|
2014-01-07 04:23:50 +08:00
|
|
|
return err;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-07 04:23:50 +08:00
|
|
|
/* take over the tty_port reference if the port was created with the
|
|
|
|
* flag RFCOMM_RELEASE_ONHUP. This will force the release of the port
|
|
|
|
* when the last process closes the tty. The behaviour is expected by
|
|
|
|
* userspace.
|
|
|
|
*/
|
|
|
|
if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
|
|
|
|
tty_port_put(&dev->port);
|
|
|
|
|
|
|
|
return 0;
|
2013-07-29 23:08:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = tty->driver_data;
|
2013-07-29 23:08:11 +08:00
|
|
|
int err;
|
2013-07-29 23:08:10 +08:00
|
|
|
|
|
|
|
BT_DBG("tty %p id %d", tty, tty->index);
|
2007-02-18 06:58:57 +08:00
|
|
|
|
2013-07-29 23:08:10 +08:00
|
|
|
BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
|
|
|
|
dev->channel, dev->port.count);
|
|
|
|
|
2013-07-29 23:08:11 +08:00
|
|
|
err = tty_port_open(&dev->port, tty, filp);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2013-07-29 23:08:10 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: rfcomm should use proper flow control for
|
|
|
|
* received data. This hack will be unnecessary and can
|
|
|
|
* be removed when that's implemented
|
|
|
|
*/
|
2008-07-15 02:13:52 +08:00
|
|
|
rfcomm_tty_copy_pending(dev);
|
|
|
|
|
|
|
|
rfcomm_dlc_unthrottle(dev->dlc);
|
|
|
|
|
2013-07-29 23:08:10 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
|
|
|
|
{
|
|
|
|
struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
|
2012-04-02 19:54:53 +08:00
|
|
|
|
2008-11-30 19:17:29 +08:00
|
|
|
BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
|
2012-04-02 19:54:53 +08:00
|
|
|
dev->port.count);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-07-29 23:08:11 +08:00
|
|
|
tty_port_close(&dev->port, tty, filp);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2007-02-09 22:24:33 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!skb)
|
|
|
|
break;
|
|
|
|
|
|
|
|
skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
|
|
|
|
|
|
|
|
memcpy(skb_put(skb, size), buf + sent, size);
|
|
|
|
|
2010-12-01 22:58:23 +08:00
|
|
|
err = rfcomm_dlc_send(dlc, skb);
|
|
|
|
if (err < 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
|
|
|
|
2007-01-08 09:16:27 +08:00
|
|
|
if (!dev || !dev->dlc)
|
|
|
|
return 0;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
|
|
|
|
if (room < 0)
|
|
|
|
room = 0;
|
2007-01-08 09:16:27 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2011-02-15 00:27:22 +08:00
|
|
|
static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-12-08 18:38:45 +08:00
|
|
|
static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-07-14 22:31:47 +08:00
|
|
|
struct ktermios *new = &tty->termios;
|
2005-08-10 11:28:46 +08:00
|
|
|
int old_baud_rate = tty_termios_baud_rate(old);
|
|
|
|
int new_baud_rate = tty_termios_baud_rate(new);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-08-10 11:28:46 +08:00
|
|
|
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);
|
|
|
|
|
2006-11-19 05:14:42 +08:00
|
|
|
if (!dev || !dev->dlc || !dev->dlc->session)
|
2006-10-15 23:31:10 +08:00
|
|
|
return;
|
|
|
|
|
2005-08-10 11:28:46 +08:00
|
|
|
/* Handle turning off CRTSCTS */
|
2007-02-09 22:24:33 +08:00
|
|
|
if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
|
2005-08-10 11:28:46 +08:00
|
|
|
BT_DBG("Turning off CRTSCTS unsupported");
|
|
|
|
|
|
|
|
/* Parity on/off and when on, odd/even */
|
|
|
|
if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
|
2010-12-01 22:58:23 +08:00
|
|
|
((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
|
2005-08-10 11:28:46 +08:00
|
|
|
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 */
|
2010-12-01 22:58:23 +08:00
|
|
|
if (new->c_cflag & CSTOPB)
|
2005-08-10 11:28:46 +08:00
|
|
|
stop_bits = RFCOMM_RPN_STOP_15;
|
2010-12-01 22:58:23 +08:00
|
|
|
else
|
2005-08-10 11:28:46 +08:00
|
|
|
stop_bits = RFCOMM_RPN_STOP_1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-08-10 11:28:46 +08:00
|
|
|
/* Handle number of data bits [5-8] */
|
2007-02-09 22:24:33 +08:00
|
|
|
if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
|
2005-08-10 11:28:46 +08:00
|
|
|
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;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-08-10 11:28:46 +08:00
|
|
|
|
|
|
|
/* 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;
|
2007-02-09 22:24:33 +08:00
|
|
|
case 19200:
|
2005-08-10 11:28:46 +08:00
|
|
|
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;
|
2007-02-09 22:24:33 +08:00
|
|
|
|
2005-08-10 11:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2005-08-10 11:28:46 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
2005-08-10 11:28:46 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
|
|
|
|
2007-01-08 09:16:27 +08:00
|
|
|
if (!dev || !dev->dlc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!skb_queue_empty(&dev->dlc->tx_queue))
|
|
|
|
return dev->dlc->mtu;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2007-01-08 09:16:27 +08:00
|
|
|
if (!dev || !dev->dlc)
|
|
|
|
return;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
skb_queue_purge(&dev->dlc->tx_queue);
|
2008-07-17 04:53:12 +08:00
|
|
|
tty_wakeup(tty);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-07-29 23:08:11 +08:00
|
|
|
tty_port_hangup(&dev->port);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 00:26:14 +08:00
|
|
|
static int rfcomm_tty_tiocmget(struct tty_struct *tty)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-02-09 22:24:33 +08:00
|
|
|
struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
BT_DBG("tty %p dev %p", tty, dev);
|
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
return dev->modem_status;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 00:26:50 +08:00
|
|
|
static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-08-10 11:28:46 +08:00
|
|
|
struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
|
|
|
|
struct rfcomm_dlc *dlc = dev->dlc;
|
|
|
|
u8 v24_sig;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
|
|
|
|
|
2005-08-10 11:28:46 +08:00
|
|
|
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;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---- TTY structure ---- */
|
|
|
|
|
2006-10-02 17:17:18 +08:00
|
|
|
static const struct tty_operations rfcomm_ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.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,
|
2013-07-29 23:08:10 +08:00
|
|
|
.install = rfcomm_tty_install,
|
|
|
|
.cleanup = rfcomm_tty_cleanup,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2010-07-24 13:04:45 +08:00
|
|
|
int __init rfcomm_init_ttys(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-10-24 21:30:57 +08:00
|
|
|
int error;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
|
|
|
|
if (!rfcomm_tty_driver)
|
2011-10-24 21:30:57 +08:00
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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;
|
2005-06-21 12:15:16 +08:00
|
|
|
rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
rfcomm_tty_driver->init_termios = tty_std_termios;
|
2014-01-07 04:23:52 +08:00
|
|
|
rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
|
2008-07-15 02:13:52 +08:00
|
|
|
rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
|
2005-04-17 06:20:36 +08:00
|
|
|
tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
|
|
|
|
|
2011-10-24 21:30:57 +08:00
|
|
|
error = tty_register_driver(rfcomm_tty_driver);
|
|
|
|
if (error) {
|
2005-04-17 06:20:36 +08:00
|
|
|
BT_ERR("Can't register RFCOMM TTY driver");
|
|
|
|
put_tty_driver(rfcomm_tty_driver);
|
2011-10-24 21:30:57 +08:00
|
|
|
return error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BT_INFO("RFCOMM TTY layer initialized");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:57:05 +08:00
|
|
|
void rfcomm_cleanup_ttys(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
tty_unregister_driver(rfcomm_tty_driver);
|
|
|
|
put_tty_driver(rfcomm_tty_driver);
|
|
|
|
}
|