YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | RFCOMM implementation for Linux Bluetooth stack (BlueZ). |
| 3 | Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com> |
| 4 | Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License version 2 as |
| 8 | published by the Free Software Foundation; |
| 9 | |
| 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 11 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 13 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 14 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 15 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 19 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 20 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | SOFTWARE IS DISCLAIMED. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * RFCOMM TTY. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | |
| 30 | #include <linux/tty.h> |
| 31 | #include <linux/tty_driver.h> |
| 32 | #include <linux/tty_flip.h> |
| 33 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 34 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/slab.h> |
| 36 | #include <linux/skbuff.h> |
David Herrmann | 96af739 | 2011-10-24 15:30:58 +0200 | [diff] [blame] | 37 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <net/bluetooth/bluetooth.h> |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 40 | #include <net/bluetooth/hci_core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/bluetooth/rfcomm.h> |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #define RFCOMM_TTY_MAGIC 0x6d02 /* magic number for rfcomm struct */ |
| 44 | #define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV /* whole lotta rfcomm devices */ |
| 45 | #define RFCOMM_TTY_MAJOR 216 /* device node major id of the usb/bluetooth.c driver */ |
| 46 | #define RFCOMM_TTY_MINOR 0 |
| 47 | |
| 48 | static struct tty_driver *rfcomm_tty_driver; |
| 49 | |
| 50 | struct rfcomm_dev { |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 51 | struct tty_port port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | char name[12]; |
| 55 | int id; |
| 56 | unsigned long flags; |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 57 | atomic_t opened; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | int err; |
| 59 | |
| 60 | bdaddr_t src; |
| 61 | bdaddr_t dst; |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 62 | u8 channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 64 | uint modem_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | struct rfcomm_dlc *dlc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | wait_queue_head_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Marcel Holtmann | c1a3313 | 2007-02-17 23:58:57 +0100 | [diff] [blame] | 69 | struct device *tty_dev; |
| 70 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 71 | atomic_t wmem_alloc; |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 72 | |
| 73 | struct sk_buff_head pending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static LIST_HEAD(rfcomm_dev_list); |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 77 | static DEFINE_SPINLOCK(rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb); |
| 80 | static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err); |
| 81 | static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig); |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* ---- Device functions ---- */ |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * The reason this isn't actually a race, as you no doubt have a little voice |
| 87 | * screaming at you in your head, is that the refcount should never actually |
| 88 | * reach zero unless the device has already been taken off the list, in |
| 89 | * rfcomm_dev_del(). And if that's not true, we'll hit the BUG() in |
| 90 | * rfcomm_dev_destruct() anyway. |
| 91 | */ |
| 92 | static void rfcomm_dev_destruct(struct tty_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 94 | struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct rfcomm_dlc *dlc = dev->dlc; |
| 96 | |
| 97 | BT_DBG("dev %p dlc %p", dev, dlc); |
| 98 | |
Dave Young | f951375 | 2008-01-10 22:22:52 -0800 | [diff] [blame] | 99 | /* Refcount should only hit zero when called from rfcomm_dev_del() |
| 100 | which will have taken us off the list. Everything else are |
| 101 | refcounting bugs. */ |
| 102 | BUG_ON(!list_empty(&dev->list)); |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | rfcomm_dlc_lock(dlc); |
| 105 | /* Detach DLC if it's owned by this dev */ |
| 106 | if (dlc->owner == dev) |
| 107 | dlc->owner = NULL; |
| 108 | rfcomm_dlc_unlock(dlc); |
| 109 | |
| 110 | rfcomm_dlc_put(dlc); |
| 111 | |
| 112 | tty_unregister_device(rfcomm_tty_driver, dev->id); |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | kfree(dev); |
| 115 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 116 | /* It's safe to call module_put() here because socket still |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | holds reference to this module. */ |
| 118 | module_put(THIS_MODULE); |
| 119 | } |
| 120 | |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 121 | static const struct tty_port_operations rfcomm_port_ops = { |
| 122 | .destruct = rfcomm_dev_destruct, |
| 123 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | static struct rfcomm_dev *__rfcomm_dev_get(int id) |
| 126 | { |
| 127 | struct rfcomm_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 129 | list_for_each_entry(dev, &rfcomm_dev_list, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (dev->id == id) |
| 131 | return dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | static inline struct rfcomm_dev *rfcomm_dev_get(int id) |
| 137 | { |
| 138 | struct rfcomm_dev *dev; |
| 139 | |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 140 | spin_lock(&rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | dev = __rfcomm_dev_get(id); |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 143 | |
| 144 | if (dev) { |
| 145 | if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) |
| 146 | dev = NULL; |
| 147 | else |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 148 | tty_port_get(&dev->port); |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 151 | spin_unlock(&rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | return dev; |
| 154 | } |
| 155 | |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 156 | static struct device *rfcomm_get_device(struct rfcomm_dev *dev) |
| 157 | { |
| 158 | struct hci_dev *hdev; |
| 159 | struct hci_conn *conn; |
| 160 | |
| 161 | hdev = hci_get_route(&dev->dst, &dev->src); |
| 162 | if (!hdev) |
| 163 | return NULL; |
| 164 | |
| 165 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst); |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 166 | |
| 167 | hci_dev_put(hdev); |
| 168 | |
Marcel Holtmann | b2cfcd7 | 2006-10-15 17:31:05 +0200 | [diff] [blame] | 169 | return conn ? &conn->dev : NULL; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Marcel Holtmann | dae6a0f | 2007-10-20 14:52:38 +0200 | [diff] [blame] | 172 | static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf) |
| 173 | { |
| 174 | struct rfcomm_dev *dev = dev_get_drvdata(tty_dev); |
Gustavo F. Padovan | d6b2eb2 | 2010-09-03 18:29:46 -0300 | [diff] [blame] | 175 | return sprintf(buf, "%s\n", batostr(&dev->dst)); |
Marcel Holtmann | dae6a0f | 2007-10-20 14:52:38 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf) |
| 179 | { |
| 180 | struct rfcomm_dev *dev = dev_get_drvdata(tty_dev); |
| 181 | return sprintf(buf, "%d\n", dev->channel); |
| 182 | } |
| 183 | |
| 184 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
| 185 | static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL); |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc) |
| 188 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 189 | struct rfcomm_dev *dev, *entry; |
Luiz Augusto von Dentz | e57d758a | 2012-03-07 20:20:14 +0200 | [diff] [blame] | 190 | struct list_head *head = &rfcomm_dev_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | int err = 0; |
| 192 | |
| 193 | BT_DBG("id %d channel %d", req->dev_id, req->channel); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 194 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 195 | dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (!dev) |
| 197 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 199 | spin_lock(&rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | if (req->dev_id < 0) { |
| 202 | dev->id = 0; |
| 203 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 204 | list_for_each_entry(entry, &rfcomm_dev_list, list) { |
| 205 | if (entry->id != dev->id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | break; |
| 207 | |
| 208 | dev->id++; |
Luiz Augusto von Dentz | e57d758a | 2012-03-07 20:20:14 +0200 | [diff] [blame] | 209 | head = &entry->list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | } else { |
| 212 | dev->id = req->dev_id; |
| 213 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 214 | list_for_each_entry(entry, &rfcomm_dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (entry->id == dev->id) { |
| 216 | err = -EADDRINUSE; |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | if (entry->id > dev->id - 1) |
| 221 | break; |
| 222 | |
Luiz Augusto von Dentz | e57d758a | 2012-03-07 20:20:14 +0200 | [diff] [blame] | 223 | head = &entry->list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) { |
| 228 | err = -ENFILE; |
| 229 | goto out; |
| 230 | } |
| 231 | |
| 232 | sprintf(dev->name, "rfcomm%d", dev->id); |
| 233 | |
| 234 | list_add(&dev->list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | bacpy(&dev->src, &req->src); |
| 237 | bacpy(&dev->dst, &req->dst); |
| 238 | dev->channel = req->channel; |
| 239 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 240 | dev->flags = req->flags & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC)); |
| 242 | |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 243 | atomic_set(&dev->opened, 0); |
| 244 | |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 245 | tty_port_init(&dev->port); |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 246 | dev->port.ops = &rfcomm_port_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | init_waitqueue_head(&dev->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 249 | skb_queue_head_init(&dev->pending); |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | rfcomm_dlc_lock(dlc); |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 252 | |
| 253 | if (req->flags & (1 << RFCOMM_REUSE_DLC)) { |
| 254 | struct sock *sk = dlc->owner; |
| 255 | struct sk_buff *skb; |
| 256 | |
| 257 | BUG_ON(!sk); |
| 258 | |
| 259 | rfcomm_dlc_throttle(dlc); |
| 260 | |
| 261 | while ((skb = skb_dequeue(&sk->sk_receive_queue))) { |
| 262 | skb_orphan(skb); |
| 263 | skb_queue_tail(&dev->pending, skb); |
| 264 | atomic_sub(skb->len, &sk->sk_rmem_alloc); |
| 265 | } |
| 266 | } |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | dlc->data_ready = rfcomm_dev_data_ready; |
| 269 | dlc->state_change = rfcomm_dev_state_change; |
| 270 | dlc->modem_status = rfcomm_dev_modem_status; |
| 271 | |
| 272 | dlc->owner = dev; |
| 273 | dev->dlc = dlc; |
Marcel Holtmann | 8b6b3da | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 274 | |
| 275 | rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig); |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | rfcomm_dlc_unlock(dlc); |
| 278 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 279 | /* It's safe to call __module_get() here because socket already |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | holds reference to this module. */ |
| 281 | __module_get(THIS_MODULE); |
| 282 | |
| 283 | out: |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 284 | spin_unlock(&rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Ilpo Järvinen | 037322a | 2008-12-14 23:18:00 -0800 | [diff] [blame] | 286 | if (err < 0) |
| 287 | goto free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Marcel Holtmann | c1a3313 | 2007-02-17 23:58:57 +0100 | [diff] [blame] | 289 | dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 291 | if (IS_ERR(dev->tty_dev)) { |
Marcel Holtmann | 09c7d82 | 2007-07-26 00:12:25 -0700 | [diff] [blame] | 292 | err = PTR_ERR(dev->tty_dev); |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 293 | list_del(&dev->list); |
Ilpo Järvinen | 037322a | 2008-12-14 23:18:00 -0800 | [diff] [blame] | 294 | goto free; |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Marcel Holtmann | dae6a0f | 2007-10-20 14:52:38 +0200 | [diff] [blame] | 297 | dev_set_drvdata(dev->tty_dev, dev); |
| 298 | |
| 299 | if (device_create_file(dev->tty_dev, &dev_attr_address) < 0) |
| 300 | BT_ERR("Failed to create address attribute"); |
| 301 | |
| 302 | if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0) |
| 303 | BT_ERR("Failed to create channel attribute"); |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return dev->id; |
Ilpo Järvinen | 037322a | 2008-12-14 23:18:00 -0800 | [diff] [blame] | 306 | |
| 307 | free: |
| 308 | kfree(dev); |
| 309 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void rfcomm_dev_del(struct rfcomm_dev *dev) |
| 313 | { |
| 314 | BT_DBG("dev %p", dev); |
| 315 | |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 316 | BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags)); |
| 317 | |
| 318 | if (atomic_read(&dev->opened) > 0) |
| 319 | return; |
Dave Young | f951375 | 2008-01-10 22:22:52 -0800 | [diff] [blame] | 320 | |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 321 | spin_lock(&rfcomm_dev_lock); |
Dave Young | f951375 | 2008-01-10 22:22:52 -0800 | [diff] [blame] | 322 | list_del_init(&dev->list); |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 323 | spin_unlock(&rfcomm_dev_lock); |
Dave Young | f951375 | 2008-01-10 22:22:52 -0800 | [diff] [blame] | 324 | |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 325 | tty_port_put(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* ---- Send buffer ---- */ |
| 329 | static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc) |
| 330 | { |
| 331 | /* We can't let it be zero, because we don't get a callback |
| 332 | when tx_credits becomes nonzero, hence we'd never wake up */ |
| 333 | return dlc->mtu * (dlc->tx_credits?:1); |
| 334 | } |
| 335 | |
| 336 | static void rfcomm_wfree(struct sk_buff *skb) |
| 337 | { |
| 338 | struct rfcomm_dev *dev = (void *) skb->sk; |
Jiri Slaby | b2c4be3 | 2012-04-02 13:54:52 +0200 | [diff] [blame^] | 339 | struct tty_struct *tty = dev->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | atomic_sub(skb->truesize, &dev->wmem_alloc); |
Jiri Slaby | b2c4be3 | 2012-04-02 13:54:52 +0200 | [diff] [blame^] | 341 | if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags) && tty) |
| 342 | tty_wakeup(tty); |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 343 | tty_port_put(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev) |
| 347 | { |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 348 | tty_port_get(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | atomic_add(skb->truesize, &dev->wmem_alloc); |
| 350 | skb->sk = (void *) dev; |
| 351 | skb->destructor = rfcomm_wfree; |
| 352 | } |
| 353 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 354 | static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) { |
| 357 | struct sk_buff *skb = alloc_skb(size, priority); |
| 358 | if (skb) { |
| 359 | rfcomm_set_owner_w(skb, dev); |
| 360 | return skb; |
| 361 | } |
| 362 | } |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | /* ---- Device IOCTLs ---- */ |
| 367 | |
| 368 | #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP)) |
| 369 | |
| 370 | static int rfcomm_create_dev(struct sock *sk, void __user *arg) |
| 371 | { |
| 372 | struct rfcomm_dev_req req; |
| 373 | struct rfcomm_dlc *dlc; |
| 374 | int id; |
| 375 | |
| 376 | if (copy_from_user(&req, arg, sizeof(req))) |
| 377 | return -EFAULT; |
| 378 | |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 379 | BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) |
| 382 | return -EPERM; |
| 383 | |
| 384 | if (req.flags & (1 << RFCOMM_REUSE_DLC)) { |
| 385 | /* Socket must be connected */ |
| 386 | if (sk->sk_state != BT_CONNECTED) |
| 387 | return -EBADFD; |
| 388 | |
| 389 | dlc = rfcomm_pi(sk)->dlc; |
| 390 | rfcomm_dlc_hold(dlc); |
| 391 | } else { |
| 392 | dlc = rfcomm_dlc_alloc(GFP_KERNEL); |
| 393 | if (!dlc) |
| 394 | return -ENOMEM; |
| 395 | } |
| 396 | |
| 397 | id = rfcomm_dev_add(&req, dlc); |
| 398 | if (id < 0) { |
| 399 | rfcomm_dlc_put(dlc); |
| 400 | return id; |
| 401 | } |
| 402 | |
| 403 | if (req.flags & (1 << RFCOMM_REUSE_DLC)) { |
| 404 | /* DLC is now used by device. |
| 405 | * Socket must be disconnected */ |
| 406 | sk->sk_state = BT_CLOSED; |
| 407 | } |
| 408 | |
| 409 | return id; |
| 410 | } |
| 411 | |
| 412 | static int rfcomm_release_dev(void __user *arg) |
| 413 | { |
| 414 | struct rfcomm_dev_req req; |
| 415 | struct rfcomm_dev *dev; |
| 416 | |
| 417 | if (copy_from_user(&req, arg, sizeof(req))) |
| 418 | return -EFAULT; |
| 419 | |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 420 | BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 422 | dev = rfcomm_dev_get(req.dev_id); |
| 423 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return -ENODEV; |
| 425 | |
| 426 | if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) { |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 427 | tty_port_put(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return -EPERM; |
| 429 | } |
| 430 | |
| 431 | if (req.flags & (1 << RFCOMM_HANGUP_NOW)) |
| 432 | rfcomm_dlc_close(dev->dlc, 0); |
| 433 | |
Mikko Rapeli | 84950cf | 2007-07-11 09:18:15 +0200 | [diff] [blame] | 434 | /* Shut down TTY synchronously before freeing rfcomm_dev */ |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 435 | if (dev->port.tty) |
| 436 | tty_vhangup(dev->port.tty); |
Mikko Rapeli | 84950cf | 2007-07-11 09:18:15 +0200 | [diff] [blame] | 437 | |
Dave Young | 93d8074 | 2008-02-05 03:12:06 -0800 | [diff] [blame] | 438 | if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) |
| 439 | rfcomm_dev_del(dev); |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 440 | tty_port_put(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int rfcomm_get_dev_list(void __user *arg) |
| 445 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 446 | struct rfcomm_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct rfcomm_dev_list_req *dl; |
| 448 | struct rfcomm_dev_info *di; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | int n = 0, size, err; |
| 450 | u16 dev_num; |
| 451 | |
| 452 | BT_DBG(""); |
| 453 | |
| 454 | if (get_user(dev_num, (u16 __user *) arg)) |
| 455 | return -EFAULT; |
| 456 | |
| 457 | if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di)) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | size = sizeof(*dl) + dev_num * sizeof(*di); |
| 461 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 462 | dl = kmalloc(size, GFP_KERNEL); |
| 463 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return -ENOMEM; |
| 465 | |
| 466 | di = dl->dev_info; |
| 467 | |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 468 | spin_lock(&rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 470 | list_for_each_entry(dev, &rfcomm_dev_list, list) { |
Ville Tervo | 8de0a15 | 2007-07-11 09:23:41 +0200 | [diff] [blame] | 471 | if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) |
| 472 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | (di + n)->id = dev->id; |
| 474 | (di + n)->flags = dev->flags; |
| 475 | (di + n)->state = dev->dlc->state; |
| 476 | (di + n)->channel = dev->channel; |
| 477 | bacpy(&(di + n)->src, &dev->src); |
| 478 | bacpy(&(di + n)->dst, &dev->dst); |
| 479 | if (++n >= dev_num) |
| 480 | break; |
| 481 | } |
| 482 | |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 483 | spin_unlock(&rfcomm_dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | dl->dev_num = n; |
| 486 | size = sizeof(*dl) + n * sizeof(*di); |
| 487 | |
| 488 | err = copy_to_user(arg, dl, size); |
| 489 | kfree(dl); |
| 490 | |
| 491 | return err ? -EFAULT : 0; |
| 492 | } |
| 493 | |
| 494 | static int rfcomm_get_dev_info(void __user *arg) |
| 495 | { |
| 496 | struct rfcomm_dev *dev; |
| 497 | struct rfcomm_dev_info di; |
| 498 | int err = 0; |
| 499 | |
| 500 | BT_DBG(""); |
| 501 | |
| 502 | if (copy_from_user(&di, arg, sizeof(di))) |
| 503 | return -EFAULT; |
| 504 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 505 | dev = rfcomm_dev_get(di.id); |
| 506 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | return -ENODEV; |
| 508 | |
| 509 | di.flags = dev->flags; |
| 510 | di.channel = dev->channel; |
| 511 | di.state = dev->dlc->state; |
| 512 | bacpy(&di.src, &dev->src); |
| 513 | bacpy(&di.dst, &dev->dst); |
| 514 | |
| 515 | if (copy_to_user(arg, &di, sizeof(di))) |
| 516 | err = -EFAULT; |
| 517 | |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 518 | tty_port_put(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return err; |
| 520 | } |
| 521 | |
| 522 | int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) |
| 523 | { |
| 524 | BT_DBG("cmd %d arg %p", cmd, arg); |
| 525 | |
| 526 | switch (cmd) { |
| 527 | case RFCOMMCREATEDEV: |
| 528 | return rfcomm_create_dev(sk, arg); |
| 529 | |
| 530 | case RFCOMMRELEASEDEV: |
| 531 | return rfcomm_release_dev(arg); |
| 532 | |
| 533 | case RFCOMMGETDEVLIST: |
| 534 | return rfcomm_get_dev_list(arg); |
| 535 | |
| 536 | case RFCOMMGETDEVINFO: |
| 537 | return rfcomm_get_dev_info(arg); |
| 538 | } |
| 539 | |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
| 543 | /* ---- DLC callbacks ---- */ |
| 544 | static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb) |
| 545 | { |
| 546 | struct rfcomm_dev *dev = dlc->owner; |
| 547 | struct tty_struct *tty; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 548 | |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 549 | if (!dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | kfree_skb(skb); |
| 551 | return; |
| 552 | } |
| 553 | |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 554 | tty = dev->port.tty; |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 555 | if (!tty || !skb_queue_empty(&dev->pending)) { |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 556 | skb_queue_tail(&dev->pending, skb); |
| 557 | return; |
| 558 | } |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len); |
| 561 | |
Paul Fulghum | 817d6d3 | 2006-06-28 04:26:47 -0700 | [diff] [blame] | 562 | tty_insert_flip_string(tty, skb->data, skb->len); |
| 563 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | kfree_skb(skb); |
| 566 | } |
| 567 | |
| 568 | static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err) |
| 569 | { |
| 570 | struct rfcomm_dev *dev = dlc->owner; |
| 571 | if (!dev) |
| 572 | return; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | BT_DBG("dlc %p dev %p err %d", dlc, dev, err); |
| 575 | |
| 576 | dev->err = err; |
| 577 | wake_up_interruptible(&dev->wait); |
| 578 | |
| 579 | if (dlc->state == BT_CLOSED) { |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 580 | if (!dev->port.tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) { |
Dave Young | 537d59a | 2008-06-01 23:50:52 -0700 | [diff] [blame] | 582 | /* Drop DLC lock here to avoid deadlock |
| 583 | * 1. rfcomm_dev_get will take rfcomm_dev_lock |
| 584 | * but in rfcomm_dev_add there's lock order: |
| 585 | * rfcomm_dev_lock -> dlc lock |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 586 | * 2. tty_port_put will deadlock if it's |
Dave Young | 537d59a | 2008-06-01 23:50:52 -0700 | [diff] [blame] | 587 | * the last reference |
| 588 | */ |
| 589 | rfcomm_dlc_unlock(dlc); |
| 590 | if (rfcomm_dev_get(dev->id) == NULL) { |
| 591 | rfcomm_dlc_lock(dlc); |
Marcel Holtmann | 77f2a45 | 2007-05-05 00:36:10 +0200 | [diff] [blame] | 592 | return; |
Dave Young | 537d59a | 2008-06-01 23:50:52 -0700 | [diff] [blame] | 593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Marcel Holtmann | 77f2a45 | 2007-05-05 00:36:10 +0200 | [diff] [blame] | 595 | rfcomm_dev_del(dev); |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 596 | tty_port_put(&dev->port); |
Dave Young | 537d59a | 2008-06-01 23:50:52 -0700 | [diff] [blame] | 597 | rfcomm_dlc_lock(dlc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 599 | } else |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 600 | tty_hangup(dev->port.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig) |
| 605 | { |
| 606 | struct rfcomm_dev *dev = dlc->owner; |
| 607 | if (!dev) |
| 608 | return; |
Timo Teräs | 7b9eb9e | 2005-08-09 20:28:21 -0700 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig); |
| 611 | |
Timo Teräs | 7b9eb9e | 2005-08-09 20:28:21 -0700 | [diff] [blame] | 612 | if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) { |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 613 | if (dev->port.tty && !C_CLOCAL(dev->port.tty)) |
| 614 | tty_hangup(dev->port.tty); |
Timo Teräs | 7b9eb9e | 2005-08-09 20:28:21 -0700 | [diff] [blame] | 615 | } |
| 616 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 617 | dev->modem_status = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) | |
| 619 | ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) | |
| 620 | ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) | |
| 621 | ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0); |
| 622 | } |
| 623 | |
| 624 | /* ---- TTY functions ---- */ |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 625 | static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev) |
| 626 | { |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 627 | struct tty_struct *tty = dev->port.tty; |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 628 | struct sk_buff *skb; |
| 629 | int inserted = 0; |
| 630 | |
| 631 | if (!tty) |
| 632 | return; |
| 633 | |
| 634 | BT_DBG("dev %p tty %p", dev, tty); |
| 635 | |
| 636 | rfcomm_dlc_lock(dev->dlc); |
| 637 | |
| 638 | while ((skb = skb_dequeue(&dev->pending))) { |
| 639 | inserted += tty_insert_flip_string(tty, skb->data, skb->len); |
| 640 | kfree_skb(skb); |
| 641 | } |
| 642 | |
| 643 | rfcomm_dlc_unlock(dev->dlc); |
| 644 | |
| 645 | if (inserted > 0) |
| 646 | tty_flip_buffer_push(tty); |
| 647 | } |
| 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp) |
| 650 | { |
| 651 | DECLARE_WAITQUEUE(wait, current); |
| 652 | struct rfcomm_dev *dev; |
| 653 | struct rfcomm_dlc *dlc; |
| 654 | int err, id; |
| 655 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 656 | id = tty->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | BT_DBG("tty %p id %d", tty, id); |
| 659 | |
| 660 | /* We don't leak this refcount. For reasons which are not entirely |
| 661 | clear, the TTY layer will call our ->close() method even if the |
| 662 | open fails. We decrease the refcount there, and decreasing it |
| 663 | here too would cause breakage. */ |
| 664 | dev = rfcomm_dev_get(id); |
| 665 | if (!dev) |
| 666 | return -ENODEV; |
| 667 | |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 668 | BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), |
| 669 | dev->channel, atomic_read(&dev->opened)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 671 | if (atomic_inc_return(&dev->opened) > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | return 0; |
| 673 | |
| 674 | dlc = dev->dlc; |
| 675 | |
| 676 | /* Attach TTY and open DLC */ |
| 677 | |
| 678 | rfcomm_dlc_lock(dlc); |
| 679 | tty->driver_data = dev; |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 680 | dev->port.tty = tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | rfcomm_dlc_unlock(dlc); |
| 682 | set_bit(RFCOMM_TTY_ATTACHED, &dev->flags); |
| 683 | |
| 684 | err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel); |
| 685 | if (err < 0) |
| 686 | return err; |
| 687 | |
| 688 | /* Wait for DLC to connect */ |
| 689 | add_wait_queue(&dev->wait, &wait); |
| 690 | while (1) { |
| 691 | set_current_state(TASK_INTERRUPTIBLE); |
| 692 | |
| 693 | if (dlc->state == BT_CLOSED) { |
| 694 | err = -dev->err; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | if (dlc->state == BT_CONNECTED) |
| 699 | break; |
| 700 | |
| 701 | if (signal_pending(current)) { |
| 702 | err = -EINTR; |
| 703 | break; |
| 704 | } |
| 705 | |
Vladislav P | 840af82 | 2011-02-14 15:21:50 -0200 | [diff] [blame] | 706 | tty_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | schedule(); |
Vladislav P | 840af82 | 2011-02-14 15:21:50 -0200 | [diff] [blame] | 708 | tty_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | set_current_state(TASK_RUNNING); |
| 711 | remove_wait_queue(&dev->wait, &wait); |
| 712 | |
Marcel Holtmann | c1a3313 | 2007-02-17 23:58:57 +0100 | [diff] [blame] | 713 | if (err == 0) |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 714 | device_move(dev->tty_dev, rfcomm_get_device(dev), |
| 715 | DPM_ORDER_DEV_AFTER_PARENT); |
Marcel Holtmann | c1a3313 | 2007-02-17 23:58:57 +0100 | [diff] [blame] | 716 | |
Marcel Holtmann | a0c22f2 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 717 | rfcomm_tty_copy_pending(dev); |
| 718 | |
| 719 | rfcomm_dlc_unthrottle(dev->dlc); |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | return err; |
| 722 | } |
| 723 | |
| 724 | static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp) |
| 725 | { |
| 726 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 727 | if (!dev) |
| 728 | return; |
| 729 | |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 730 | BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, |
| 731 | atomic_read(&dev->opened)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 733 | if (atomic_dec_and_test(&dev->opened)) { |
Dave Young | acea685 | 2008-01-21 22:35:21 -0800 | [diff] [blame] | 734 | if (dev->tty_dev->parent) |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 735 | device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST); |
Marcel Holtmann | c1a3313 | 2007-02-17 23:58:57 +0100 | [diff] [blame] | 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | /* Close DLC and dettach TTY */ |
| 738 | rfcomm_dlc_close(dev->dlc, 0); |
| 739 | |
| 740 | clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | rfcomm_dlc_lock(dev->dlc); |
| 743 | tty->driver_data = NULL; |
Jiri Slaby | f60db8c | 2012-04-02 13:54:50 +0200 | [diff] [blame] | 744 | dev->port.tty = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | rfcomm_dlc_unlock(dev->dlc); |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 746 | |
| 747 | if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) { |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 748 | spin_lock(&rfcomm_dev_lock); |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 749 | list_del_init(&dev->list); |
Gustavo F. Padovan | 393432c | 2011-12-27 15:28:45 -0200 | [diff] [blame] | 750 | spin_unlock(&rfcomm_dev_lock); |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 751 | |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 752 | tty_port_put(&dev->port); |
Marcel Holtmann | 9a5df92 | 2008-11-30 12:17:29 +0100 | [diff] [blame] | 753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 756 | tty_port_put(&dev->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 760 | { |
| 761 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 762 | struct rfcomm_dlc *dlc = dev->dlc; |
| 763 | struct sk_buff *skb; |
| 764 | int err = 0, sent = 0, size; |
| 765 | |
| 766 | BT_DBG("tty %p count %d", tty, count); |
| 767 | |
| 768 | while (count) { |
| 769 | size = min_t(uint, count, dlc->mtu); |
| 770 | |
| 771 | skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if (!skb) |
| 774 | break; |
| 775 | |
| 776 | skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE); |
| 777 | |
| 778 | memcpy(skb_put(skb, size), buf + sent, size); |
| 779 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 780 | err = rfcomm_dlc_send(dlc, skb); |
| 781 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | kfree_skb(skb); |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | sent += size; |
| 787 | count -= size; |
| 788 | } |
| 789 | |
| 790 | return sent ? sent : err; |
| 791 | } |
| 792 | |
| 793 | static int rfcomm_tty_write_room(struct tty_struct *tty) |
| 794 | { |
| 795 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 796 | int room; |
| 797 | |
| 798 | BT_DBG("tty %p", tty); |
| 799 | |
Marcel Holtmann | b6e557f | 2007-01-08 02:16:27 +0100 | [diff] [blame] | 800 | if (!dev || !dev->dlc) |
| 801 | return 0; |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc); |
| 804 | if (room < 0) |
| 805 | room = 0; |
Marcel Holtmann | b6e557f | 2007-01-08 02:16:27 +0100 | [diff] [blame] | 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | return room; |
| 808 | } |
| 809 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 810 | static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
| 812 | BT_DBG("tty %p cmd 0x%02x", tty, cmd); |
| 813 | |
| 814 | switch (cmd) { |
| 815 | case TCGETS: |
| 816 | BT_DBG("TCGETS is not supported"); |
| 817 | return -ENOIOCTLCMD; |
| 818 | |
| 819 | case TCSETS: |
| 820 | BT_DBG("TCSETS is not supported"); |
| 821 | return -ENOIOCTLCMD; |
| 822 | |
| 823 | case TIOCMIWAIT: |
| 824 | BT_DBG("TIOCMIWAIT"); |
| 825 | break; |
| 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | case TIOCGSERIAL: |
| 828 | BT_ERR("TIOCGSERIAL is not supported"); |
| 829 | return -ENOIOCTLCMD; |
| 830 | |
| 831 | case TIOCSSERIAL: |
| 832 | BT_ERR("TIOCSSERIAL is not supported"); |
| 833 | return -ENOIOCTLCMD; |
| 834 | |
| 835 | case TIOCSERGSTRUCT: |
| 836 | BT_ERR("TIOCSERGSTRUCT is not supported"); |
| 837 | return -ENOIOCTLCMD; |
| 838 | |
| 839 | case TIOCSERGETLSR: |
| 840 | BT_ERR("TIOCSERGETLSR is not supported"); |
| 841 | return -ENOIOCTLCMD; |
| 842 | |
| 843 | case TIOCSERCONFIG: |
| 844 | BT_ERR("TIOCSERCONFIG is not supported"); |
| 845 | return -ENOIOCTLCMD; |
| 846 | |
| 847 | default: |
| 848 | return -ENOIOCTLCMD; /* ioctls which we must ignore */ |
| 849 | |
| 850 | } |
| 851 | |
| 852 | return -ENOIOCTLCMD; |
| 853 | } |
| 854 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 855 | static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 857 | struct ktermios *new = tty->termios; |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 858 | int old_baud_rate = tty_termios_baud_rate(old); |
| 859 | int new_baud_rate = tty_termios_baud_rate(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 861 | u8 baud, data_bits, stop_bits, parity, x_on, x_off; |
| 862 | u16 changes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 864 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 865 | |
| 866 | BT_DBG("tty %p termios %p", tty, old); |
| 867 | |
Marcel Holtmann | ff2d367 | 2006-11-18 22:14:42 +0100 | [diff] [blame] | 868 | if (!dev || !dev->dlc || !dev->dlc->session) |
Marcel Holtmann | cb19d9e | 2006-10-15 17:31:10 +0200 | [diff] [blame] | 869 | return; |
| 870 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 871 | /* Handle turning off CRTSCTS */ |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 872 | if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS)) |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 873 | BT_DBG("Turning off CRTSCTS unsupported"); |
| 874 | |
| 875 | /* Parity on/off and when on, odd/even */ |
| 876 | if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) || |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 877 | ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) { |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 878 | changes |= RFCOMM_RPN_PM_PARITY; |
| 879 | BT_DBG("Parity change detected."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 881 | |
| 882 | /* Mark and space parity are not supported! */ |
| 883 | if (new->c_cflag & PARENB) { |
| 884 | if (new->c_cflag & PARODD) { |
| 885 | BT_DBG("Parity is ODD"); |
| 886 | parity = RFCOMM_RPN_PARITY_ODD; |
| 887 | } else { |
| 888 | BT_DBG("Parity is EVEN"); |
| 889 | parity = RFCOMM_RPN_PARITY_EVEN; |
| 890 | } |
| 891 | } else { |
| 892 | BT_DBG("Parity is OFF"); |
| 893 | parity = RFCOMM_RPN_PARITY_NONE; |
| 894 | } |
| 895 | |
| 896 | /* Setting the x_on / x_off characters */ |
| 897 | if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) { |
| 898 | BT_DBG("XOFF custom"); |
| 899 | x_on = new->c_cc[VSTOP]; |
| 900 | changes |= RFCOMM_RPN_PM_XON; |
| 901 | } else { |
| 902 | BT_DBG("XOFF default"); |
| 903 | x_on = RFCOMM_RPN_XON_CHAR; |
| 904 | } |
| 905 | |
| 906 | if (old->c_cc[VSTART] != new->c_cc[VSTART]) { |
| 907 | BT_DBG("XON custom"); |
| 908 | x_off = new->c_cc[VSTART]; |
| 909 | changes |= RFCOMM_RPN_PM_XOFF; |
| 910 | } else { |
| 911 | BT_DBG("XON default"); |
| 912 | x_off = RFCOMM_RPN_XOFF_CHAR; |
| 913 | } |
| 914 | |
| 915 | /* Handle setting of stop bits */ |
| 916 | if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB)) |
| 917 | changes |= RFCOMM_RPN_PM_STOP; |
| 918 | |
| 919 | /* POSIX does not support 1.5 stop bits and RFCOMM does not |
| 920 | * support 2 stop bits. So a request for 2 stop bits gets |
| 921 | * translated to 1.5 stop bits */ |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 922 | if (new->c_cflag & CSTOPB) |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 923 | stop_bits = RFCOMM_RPN_STOP_15; |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 924 | else |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 925 | stop_bits = RFCOMM_RPN_STOP_1; |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 926 | |
| 927 | /* Handle number of data bits [5-8] */ |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 928 | if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE)) |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 929 | changes |= RFCOMM_RPN_PM_DATA; |
| 930 | |
| 931 | switch (new->c_cflag & CSIZE) { |
| 932 | case CS5: |
| 933 | data_bits = RFCOMM_RPN_DATA_5; |
| 934 | break; |
| 935 | case CS6: |
| 936 | data_bits = RFCOMM_RPN_DATA_6; |
| 937 | break; |
| 938 | case CS7: |
| 939 | data_bits = RFCOMM_RPN_DATA_7; |
| 940 | break; |
| 941 | case CS8: |
| 942 | data_bits = RFCOMM_RPN_DATA_8; |
| 943 | break; |
| 944 | default: |
| 945 | data_bits = RFCOMM_RPN_DATA_8; |
| 946 | break; |
| 947 | } |
| 948 | |
| 949 | /* Handle baudrate settings */ |
| 950 | if (old_baud_rate != new_baud_rate) |
| 951 | changes |= RFCOMM_RPN_PM_BITRATE; |
| 952 | |
| 953 | switch (new_baud_rate) { |
| 954 | case 2400: |
| 955 | baud = RFCOMM_RPN_BR_2400; |
| 956 | break; |
| 957 | case 4800: |
| 958 | baud = RFCOMM_RPN_BR_4800; |
| 959 | break; |
| 960 | case 7200: |
| 961 | baud = RFCOMM_RPN_BR_7200; |
| 962 | break; |
| 963 | case 9600: |
| 964 | baud = RFCOMM_RPN_BR_9600; |
| 965 | break; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 966 | case 19200: |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 967 | baud = RFCOMM_RPN_BR_19200; |
| 968 | break; |
| 969 | case 38400: |
| 970 | baud = RFCOMM_RPN_BR_38400; |
| 971 | break; |
| 972 | case 57600: |
| 973 | baud = RFCOMM_RPN_BR_57600; |
| 974 | break; |
| 975 | case 115200: |
| 976 | baud = RFCOMM_RPN_BR_115200; |
| 977 | break; |
| 978 | case 230400: |
| 979 | baud = RFCOMM_RPN_BR_230400; |
| 980 | break; |
| 981 | default: |
| 982 | /* 9600 is standard accordinag to the RFCOMM specification */ |
| 983 | baud = RFCOMM_RPN_BR_9600; |
| 984 | break; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 985 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | if (changes) |
| 989 | rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud, |
| 990 | data_bits, stop_bits, parity, |
| 991 | RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static void rfcomm_tty_throttle(struct tty_struct *tty) |
| 995 | { |
| 996 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 997 | |
| 998 | BT_DBG("tty %p dev %p", tty, dev); |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | rfcomm_dlc_throttle(dev->dlc); |
| 1001 | } |
| 1002 | |
| 1003 | static void rfcomm_tty_unthrottle(struct tty_struct *tty) |
| 1004 | { |
| 1005 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 1006 | |
| 1007 | BT_DBG("tty %p dev %p", tty, dev); |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | rfcomm_dlc_unthrottle(dev->dlc); |
| 1010 | } |
| 1011 | |
| 1012 | static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty) |
| 1013 | { |
| 1014 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
| 1016 | BT_DBG("tty %p dev %p", tty, dev); |
| 1017 | |
Marcel Holtmann | b6e557f | 2007-01-08 02:16:27 +0100 | [diff] [blame] | 1018 | if (!dev || !dev->dlc) |
| 1019 | return 0; |
| 1020 | |
| 1021 | if (!skb_queue_empty(&dev->dlc->tx_queue)) |
| 1022 | return dev->dlc->mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | static void rfcomm_tty_flush_buffer(struct tty_struct *tty) |
| 1028 | { |
| 1029 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | BT_DBG("tty %p dev %p", tty, dev); |
| 1032 | |
Marcel Holtmann | b6e557f | 2007-01-08 02:16:27 +0100 | [diff] [blame] | 1033 | if (!dev || !dev->dlc) |
| 1034 | return; |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | skb_queue_purge(&dev->dlc->tx_queue); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1037 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch) |
| 1041 | { |
| 1042 | BT_DBG("tty %p ch %c", tty, ch); |
| 1043 | } |
| 1044 | |
| 1045 | static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout) |
| 1046 | { |
| 1047 | BT_DBG("tty %p timeout %d", tty, timeout); |
| 1048 | } |
| 1049 | |
| 1050 | static void rfcomm_tty_hangup(struct tty_struct *tty) |
| 1051 | { |
| 1052 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | BT_DBG("tty %p dev %p", tty, dev); |
| 1055 | |
Marcel Holtmann | b6e557f | 2007-01-08 02:16:27 +0100 | [diff] [blame] | 1056 | if (!dev) |
| 1057 | return; |
| 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | rfcomm_tty_flush_buffer(tty); |
| 1060 | |
Marcel Holtmann | 77f2a45 | 2007-05-05 00:36:10 +0200 | [diff] [blame] | 1061 | if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) { |
| 1062 | if (rfcomm_dev_get(dev->id) == NULL) |
| 1063 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | rfcomm_dev_del(dev); |
Jiri Slaby | 6705401 | 2012-04-02 13:54:51 +0200 | [diff] [blame] | 1065 | tty_port_put(&dev->port); |
Marcel Holtmann | 77f2a45 | 2007-05-05 00:36:10 +0200 | [diff] [blame] | 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 1069 | static int rfcomm_tty_tiocmget(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1071 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | BT_DBG("tty %p dev %p", tty, dev); |
| 1074 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1075 | return dev->modem_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 1078 | static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | { |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1080 | struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data; |
| 1081 | struct rfcomm_dlc *dlc = dev->dlc; |
| 1082 | u8 v24_sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
| 1084 | BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear); |
| 1085 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1086 | rfcomm_dlc_get_modem_status(dlc, &v24_sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1088 | if (set & TIOCM_DSR || set & TIOCM_DTR) |
| 1089 | v24_sig |= RFCOMM_V24_RTC; |
| 1090 | if (set & TIOCM_RTS || set & TIOCM_CTS) |
| 1091 | v24_sig |= RFCOMM_V24_RTR; |
| 1092 | if (set & TIOCM_RI) |
| 1093 | v24_sig |= RFCOMM_V24_IC; |
| 1094 | if (set & TIOCM_CD) |
| 1095 | v24_sig |= RFCOMM_V24_DV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1097 | if (clear & TIOCM_DSR || clear & TIOCM_DTR) |
| 1098 | v24_sig &= ~RFCOMM_V24_RTC; |
| 1099 | if (clear & TIOCM_RTS || clear & TIOCM_CTS) |
| 1100 | v24_sig &= ~RFCOMM_V24_RTR; |
| 1101 | if (clear & TIOCM_RI) |
| 1102 | v24_sig &= ~RFCOMM_V24_IC; |
| 1103 | if (clear & TIOCM_CD) |
| 1104 | v24_sig &= ~RFCOMM_V24_DV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1106 | rfcomm_dlc_set_modem_status(dlc, v24_sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1108 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* ---- TTY structure ---- */ |
| 1112 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1113 | static const struct tty_operations rfcomm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | .open = rfcomm_tty_open, |
| 1115 | .close = rfcomm_tty_close, |
| 1116 | .write = rfcomm_tty_write, |
| 1117 | .write_room = rfcomm_tty_write_room, |
| 1118 | .chars_in_buffer = rfcomm_tty_chars_in_buffer, |
| 1119 | .flush_buffer = rfcomm_tty_flush_buffer, |
| 1120 | .ioctl = rfcomm_tty_ioctl, |
| 1121 | .throttle = rfcomm_tty_throttle, |
| 1122 | .unthrottle = rfcomm_tty_unthrottle, |
| 1123 | .set_termios = rfcomm_tty_set_termios, |
| 1124 | .send_xchar = rfcomm_tty_send_xchar, |
| 1125 | .hangup = rfcomm_tty_hangup, |
| 1126 | .wait_until_sent = rfcomm_tty_wait_until_sent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | .tiocmget = rfcomm_tty_tiocmget, |
| 1128 | .tiocmset = rfcomm_tty_tiocmset, |
| 1129 | }; |
| 1130 | |
Gustavo F. Padovan | 2f8362a | 2010-07-24 02:04:45 -0300 | [diff] [blame] | 1131 | int __init rfcomm_init_ttys(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | { |
David Herrmann | 5ada991 | 2011-10-24 15:30:57 +0200 | [diff] [blame] | 1133 | int error; |
| 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS); |
| 1136 | if (!rfcomm_tty_driver) |
David Herrmann | 5ada991 | 2011-10-24 15:30:57 +0200 | [diff] [blame] | 1137 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | rfcomm_tty_driver->driver_name = "rfcomm"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | rfcomm_tty_driver->name = "rfcomm"; |
| 1141 | rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR; |
| 1142 | rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR; |
| 1143 | rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 1144 | rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1145 | rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | rfcomm_tty_driver->init_termios = tty_std_termios; |
| 1147 | rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
Marcel Holtmann | ca37bdd5 | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 1148 | rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | tty_set_operations(rfcomm_tty_driver, &rfcomm_ops); |
| 1150 | |
David Herrmann | 5ada991 | 2011-10-24 15:30:57 +0200 | [diff] [blame] | 1151 | error = tty_register_driver(rfcomm_tty_driver); |
| 1152 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | BT_ERR("Can't register RFCOMM TTY driver"); |
| 1154 | put_tty_driver(rfcomm_tty_driver); |
David Herrmann | 5ada991 | 2011-10-24 15:30:57 +0200 | [diff] [blame] | 1155 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | BT_INFO("RFCOMM TTY layer initialized"); |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
Gustavo F. Padovan | 28e9509 | 2010-07-31 19:57:05 -0300 | [diff] [blame] | 1163 | void rfcomm_cleanup_ttys(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
| 1165 | tty_unregister_driver(rfcomm_tty_driver); |
| 1166 | put_tty_driver(rfcomm_tty_driver); |
| 1167 | } |