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