blob: 2975bc4b91885f0f80812af4b8d263cb0e7923bc [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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 Hideaki8e87d142007-02-09 23:24:33 +090014 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 Torvalds1da177e2005-04-16 15:20:36 -070017 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090019 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 SOFTWARE IS DISCLAIMED.
22*/
23
24/*
25 * RFCOMM TTY.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29
30#include <linux/tty.h>
31#include <linux/tty_driver.h>
32#include <linux/tty_flip.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/bluetooth/bluetooth.h>
Marcel Holtmann0a85b962006-07-06 13:09:02 +020035#include <net/bluetooth/hci_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/bluetooth/rfcomm.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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
43static struct tty_driver *rfcomm_tty_driver;
44
45struct rfcomm_dev {
Jiri Slabyf60db8c2012-04-02 13:54:50 +020046 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 char name[12];
50 int id;
51 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int err;
53
Peter Hurley1c648342014-02-09 20:59:07 -050054 unsigned long status; /* don't export to userspace */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 bdaddr_t src;
57 bdaddr_t dst;
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020058 u8 channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020060 uint modem_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 struct rfcomm_dlc *dlc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Marcel Holtmannc1a33132007-02-17 23:58:57 +010064 struct device *tty_dev;
65
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020066 atomic_t wmem_alloc;
Marcel Holtmanna0c22f22008-07-14 20:13:52 +020067
68 struct sk_buff_head pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71static LIST_HEAD(rfcomm_dev_list);
Gustavo F. Padovan393432c2011-12-27 15:28:45 -020072static DEFINE_SPINLOCK(rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
75static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
76static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* ---- Device functions ---- */
Jiri Slaby67054012012-04-02 13:54:51 +020079
Jiri Slaby67054012012-04-02 13:54:51 +020080static void rfcomm_dev_destruct(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Jiri Slaby67054012012-04-02 13:54:51 +020082 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct rfcomm_dlc *dlc = dev->dlc;
84
85 BT_DBG("dev %p dlc %p", dev, dlc);
86
87 rfcomm_dlc_lock(dlc);
88 /* Detach DLC if it's owned by this dev */
89 if (dlc->owner == dev)
90 dlc->owner = NULL;
91 rfcomm_dlc_unlock(dlc);
92
93 rfcomm_dlc_put(dlc);
94
95 tty_unregister_device(rfcomm_tty_driver, dev->id);
96
Peter Hurleyc949c222014-02-09 20:59:09 -050097 spin_lock(&rfcomm_dev_lock);
98 list_del(&dev->list);
99 spin_unlock(&rfcomm_dev_lock);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 kfree(dev);
102
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900103 /* It's safe to call module_put() here because socket still
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 holds reference to this module. */
105 module_put(THIS_MODULE);
106}
107
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200108/* device-specific initialization: open the dlc */
109static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
110{
111 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
112
Peter Hurley136c3732014-02-09 20:59:02 -0500113 return rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200114}
115
Peter Hurley7f717b92014-02-09 20:59:01 -0500116/* we block the open until the dlc->state becomes BT_CONNECTED */
117static int rfcomm_dev_carrier_raised(struct tty_port *port)
118{
119 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
120
121 return (dev->dlc->state == BT_CONNECTED);
122}
123
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200124/* device-specific cleanup: close the dlc */
125static void rfcomm_dev_shutdown(struct tty_port *port)
126{
127 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
128
129 if (dev->tty_dev->parent)
130 device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
131
132 /* close the dlc */
133 rfcomm_dlc_close(dev->dlc, 0);
134}
135
Jiri Slaby67054012012-04-02 13:54:51 +0200136static const struct tty_port_operations rfcomm_port_ops = {
137 .destruct = rfcomm_dev_destruct,
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200138 .activate = rfcomm_dev_activate,
139 .shutdown = rfcomm_dev_shutdown,
Peter Hurley136c3732014-02-09 20:59:02 -0500140 .carrier_raised = rfcomm_dev_carrier_raised,
Jiri Slaby67054012012-04-02 13:54:51 +0200141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143static struct rfcomm_dev *__rfcomm_dev_get(int id)
144{
145 struct rfcomm_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200147 list_for_each_entry(dev, &rfcomm_dev_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (dev->id == id)
149 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return NULL;
152}
153
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300154static struct rfcomm_dev *rfcomm_dev_get(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct rfcomm_dev *dev;
157
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200158 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 dev = __rfcomm_dev_get(id);
Ville Tervo8de0a152007-07-11 09:23:41 +0200161
Peter Hurley082a1532014-02-09 20:59:05 -0500162 if (dev && !tty_port_get(&dev->port))
163 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200165 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return dev;
168}
169
Peter Hurleyb92483d2014-02-09 20:59:15 -0500170static void rfcomm_reparent_device(struct rfcomm_dev *dev)
Peter Hurleyf87c24e2014-02-09 20:59:03 -0500171{
172 struct hci_dev *hdev;
173 struct hci_conn *conn;
174
175 hdev = hci_get_route(&dev->dst, &dev->src);
176 if (!hdev)
Peter Hurleyb92483d2014-02-09 20:59:15 -0500177 return;
Peter Hurleyf87c24e2014-02-09 20:59:03 -0500178
Peter Hurleyb92483d2014-02-09 20:59:15 -0500179 /* The lookup results are unsafe to access without the
180 * hci device lock (FIXME: why is this not documented?)
181 */
182 hci_dev_lock(hdev);
Peter Hurleyf87c24e2014-02-09 20:59:03 -0500183 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
184
Peter Hurleyb92483d2014-02-09 20:59:15 -0500185 /* Just because the acl link is in the hash table is no
186 * guarantee the sysfs device has been added ...
187 */
188 if (conn && device_is_registered(&conn->dev))
189 device_move(dev->tty_dev, &conn->dev, DPM_ORDER_DEV_AFTER_PARENT);
Peter Hurleyf87c24e2014-02-09 20:59:03 -0500190
Peter Hurleyb92483d2014-02-09 20:59:15 -0500191 hci_dev_unlock(hdev);
192 hci_dev_put(hdev);
Peter Hurleyf87c24e2014-02-09 20:59:03 -0500193}
194
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200195static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
196{
197 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300198 return sprintf(buf, "%pMR\n", &dev->dst);
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200199}
200
201static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
202{
203 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
204 return sprintf(buf, "%d\n", dev->channel);
205}
206
207static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
208static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
211{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200212 struct rfcomm_dev *dev, *entry;
Luiz Augusto von Dentze57d758a2012-03-07 20:20:14 +0200213 struct list_head *head = &rfcomm_dev_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int err = 0;
215
216 BT_DBG("id %d channel %d", req->dev_id, req->channel);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900217
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200218 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!dev)
220 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200222 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (req->dev_id < 0) {
225 dev->id = 0;
226
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200227 list_for_each_entry(entry, &rfcomm_dev_list, list) {
228 if (entry->id != dev->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230
231 dev->id++;
Luiz Augusto von Dentze57d758a2012-03-07 20:20:14 +0200232 head = &entry->list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 } else {
235 dev->id = req->dev_id;
236
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200237 list_for_each_entry(entry, &rfcomm_dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (entry->id == dev->id) {
239 err = -EADDRINUSE;
240 goto out;
241 }
242
243 if (entry->id > dev->id - 1)
244 break;
245
Luiz Augusto von Dentze57d758a2012-03-07 20:20:14 +0200246 head = &entry->list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 }
249
250 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
251 err = -ENFILE;
252 goto out;
253 }
254
255 sprintf(dev->name, "rfcomm%d", dev->id);
256
257 list_add(&dev->list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 bacpy(&dev->src, &req->src);
260 bacpy(&dev->dst, &req->dst);
261 dev->channel = req->channel;
262
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900263 dev->flags = req->flags &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
265
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200266 tty_port_init(&dev->port);
Jiri Slaby67054012012-04-02 13:54:51 +0200267 dev->port.ops = &rfcomm_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200269 skb_queue_head_init(&dev->pending);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 rfcomm_dlc_lock(dlc);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200272
273 if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
274 struct sock *sk = dlc->owner;
275 struct sk_buff *skb;
276
277 BUG_ON(!sk);
278
279 rfcomm_dlc_throttle(dlc);
280
281 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
282 skb_orphan(skb);
283 skb_queue_tail(&dev->pending, skb);
284 atomic_sub(skb->len, &sk->sk_rmem_alloc);
285 }
286 }
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dlc->data_ready = rfcomm_dev_data_ready;
289 dlc->state_change = rfcomm_dev_state_change;
290 dlc->modem_status = rfcomm_dev_modem_status;
291
292 dlc->owner = dev;
293 dev->dlc = dlc;
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +0200294
295 rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 rfcomm_dlc_unlock(dlc);
298
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900299 /* It's safe to call __module_get() here because socket already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 holds reference to this module. */
301 __module_get(THIS_MODULE);
302
303out:
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200304 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800306 if (err < 0)
307 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jiri Slaby734cc172012-08-07 21:47:47 +0200309 dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
310 dev->id, NULL);
Ville Tervo8de0a152007-07-11 09:23:41 +0200311 if (IS_ERR(dev->tty_dev)) {
Marcel Holtmann09c7d822007-07-26 00:12:25 -0700312 err = PTR_ERR(dev->tty_dev);
Gianluca Anzolinebe937f2013-07-29 17:08:09 +0200313 spin_lock(&rfcomm_dev_lock);
Ville Tervo8de0a152007-07-11 09:23:41 +0200314 list_del(&dev->list);
Gianluca Anzolinebe937f2013-07-29 17:08:09 +0200315 spin_unlock(&rfcomm_dev_lock);
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800316 goto free;
Ville Tervo8de0a152007-07-11 09:23:41 +0200317 }
318
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200319 dev_set_drvdata(dev->tty_dev, dev);
320
321 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
322 BT_ERR("Failed to create address attribute");
323
324 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
325 BT_ERR("Failed to create channel attribute");
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return dev->id;
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800328
329free:
330 kfree(dev);
331 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/* ---- Send buffer ---- */
335static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
336{
337 /* We can't let it be zero, because we don't get a callback
338 when tx_credits becomes nonzero, hence we'd never wake up */
339 return dlc->mtu * (dlc->tx_credits?:1);
340}
341
342static void rfcomm_wfree(struct sk_buff *skb)
343{
344 struct rfcomm_dev *dev = (void *) skb->sk;
345 atomic_sub(skb->truesize, &dev->wmem_alloc);
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200346 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
347 tty_port_tty_wakeup(&dev->port);
Jiri Slaby67054012012-04-02 13:54:51 +0200348 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300351static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Jiri Slaby67054012012-04-02 13:54:51 +0200353 tty_port_get(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 atomic_add(skb->truesize, &dev->wmem_alloc);
355 skb->sk = (void *) dev;
356 skb->destructor = rfcomm_wfree;
357}
358
Al Virodd0fc662005-10-07 07:46:04 +0100359static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
362 struct sk_buff *skb = alloc_skb(size, priority);
363 if (skb) {
364 rfcomm_set_owner_w(skb, dev);
365 return skb;
366 }
367 }
368 return NULL;
369}
370
371/* ---- Device IOCTLs ---- */
372
373#define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
374
375static int rfcomm_create_dev(struct sock *sk, void __user *arg)
376{
377 struct rfcomm_dev_req req;
378 struct rfcomm_dlc *dlc;
379 int id;
380
381 if (copy_from_user(&req, arg, sizeof(req)))
382 return -EFAULT;
383
Ville Tervo8de0a152007-07-11 09:23:41 +0200384 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
387 return -EPERM;
388
389 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
390 /* Socket must be connected */
391 if (sk->sk_state != BT_CONNECTED)
392 return -EBADFD;
393
394 dlc = rfcomm_pi(sk)->dlc;
395 rfcomm_dlc_hold(dlc);
396 } else {
Peter Hurleyc10a8482014-02-09 20:59:10 -0500397 /* Validate the channel is unused */
398 dlc = rfcomm_dlc_exists(&req.src, &req.dst, req.channel);
399 if (IS_ERR(dlc))
400 return PTR_ERR(dlc);
401 else if (dlc) {
402 rfcomm_dlc_put(dlc);
403 return -EBUSY;
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
406 if (!dlc)
407 return -ENOMEM;
408 }
409
410 id = rfcomm_dev_add(&req, dlc);
411 if (id < 0) {
412 rfcomm_dlc_put(dlc);
413 return id;
414 }
415
416 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
417 /* DLC is now used by device.
418 * Socket must be disconnected */
419 sk->sk_state = BT_CLOSED;
420 }
421
422 return id;
423}
424
425static int rfcomm_release_dev(void __user *arg)
426{
427 struct rfcomm_dev_req req;
428 struct rfcomm_dev *dev;
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200429 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (copy_from_user(&req, arg, sizeof(req)))
432 return -EFAULT;
433
Ville Tervo8de0a152007-07-11 09:23:41 +0200434 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200436 dev = rfcomm_dev_get(req.dev_id);
437 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -ENODEV;
439
440 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
Jiri Slaby67054012012-04-02 13:54:51 +0200441 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -EPERM;
443 }
444
Peter Hurley1c648342014-02-09 20:59:07 -0500445 /* only release once */
446 if (test_and_set_bit(RFCOMM_DEV_RELEASED, &dev->status)) {
447 tty_port_put(&dev->port);
448 return -EALREADY;
449 }
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
452 rfcomm_dlc_close(dev->dlc, 0);
453
Mikko Rapeli84950cf2007-07-11 09:18:15 +0200454 /* Shut down TTY synchronously before freeing rfcomm_dev */
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200455 tty = tty_port_tty_get(&dev->port);
456 if (tty) {
457 tty_vhangup(tty);
458 tty_kref_put(tty);
459 }
Mikko Rapeli84950cf2007-07-11 09:18:15 +0200460
Peter Hurley80ea7332014-02-09 20:59:08 -0500461 if (!test_bit(RFCOMM_TTY_OWNED, &dev->status))
Gianluca Anzolinece31502013-07-29 17:08:12 +0200462 tty_port_put(&dev->port);
463
Jiri Slaby67054012012-04-02 13:54:51 +0200464 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
468static int rfcomm_get_dev_list(void __user *arg)
469{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200470 struct rfcomm_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct rfcomm_dev_list_req *dl;
472 struct rfcomm_dev_info *di;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int n = 0, size, err;
474 u16 dev_num;
475
476 BT_DBG("");
477
478 if (get_user(dev_num, (u16 __user *) arg))
479 return -EFAULT;
480
481 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
482 return -EINVAL;
483
484 size = sizeof(*dl) + dev_num * sizeof(*di);
485
Mathias Krausef9432c52012-08-15 11:31:49 +0000486 dl = kzalloc(size, GFP_KERNEL);
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200487 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -ENOMEM;
489
490 di = dl->dev_info;
491
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200492 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200494 list_for_each_entry(dev, &rfcomm_dev_list, list) {
Peter Hurley960603a2014-02-09 20:59:06 -0500495 if (!tty_port_get(&dev->port))
Ville Tervo8de0a152007-07-11 09:23:41 +0200496 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 (di + n)->id = dev->id;
498 (di + n)->flags = dev->flags;
499 (di + n)->state = dev->dlc->state;
500 (di + n)->channel = dev->channel;
501 bacpy(&(di + n)->src, &dev->src);
502 bacpy(&(di + n)->dst, &dev->dst);
Peter Hurley960603a2014-02-09 20:59:06 -0500503 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (++n >= dev_num)
505 break;
506 }
507
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200508 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 dl->dev_num = n;
511 size = sizeof(*dl) + n * sizeof(*di);
512
513 err = copy_to_user(arg, dl, size);
514 kfree(dl);
515
516 return err ? -EFAULT : 0;
517}
518
519static int rfcomm_get_dev_info(void __user *arg)
520{
521 struct rfcomm_dev *dev;
522 struct rfcomm_dev_info di;
523 int err = 0;
524
525 BT_DBG("");
526
527 if (copy_from_user(&di, arg, sizeof(di)))
528 return -EFAULT;
529
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200530 dev = rfcomm_dev_get(di.id);
531 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -ENODEV;
533
534 di.flags = dev->flags;
535 di.channel = dev->channel;
536 di.state = dev->dlc->state;
537 bacpy(&di.src, &dev->src);
538 bacpy(&di.dst, &dev->dst);
539
540 if (copy_to_user(arg, &di, sizeof(di)))
541 err = -EFAULT;
542
Jiri Slaby67054012012-04-02 13:54:51 +0200543 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return err;
545}
546
547int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
548{
549 BT_DBG("cmd %d arg %p", cmd, arg);
550
551 switch (cmd) {
552 case RFCOMMCREATEDEV:
553 return rfcomm_create_dev(sk, arg);
554
555 case RFCOMMRELEASEDEV:
556 return rfcomm_release_dev(arg);
557
558 case RFCOMMGETDEVLIST:
559 return rfcomm_get_dev_list(arg);
560
561 case RFCOMMGETDEVINFO:
562 return rfcomm_get_dev_info(arg);
563 }
564
565 return -EINVAL;
566}
567
568/* ---- DLC callbacks ---- */
569static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
570{
571 struct rfcomm_dev *dev = dlc->owner;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900572
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200573 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 kfree_skb(skb);
575 return;
576 }
577
Jiri Slaby2e124b42013-01-03 15:53:06 +0100578 if (!skb_queue_empty(&dev->pending)) {
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200579 skb_queue_tail(&dev->pending, skb);
580 return;
581 }
582
Jiri Slaby2e124b42013-01-03 15:53:06 +0100583 BT_DBG("dlc %p len %d", dlc, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100585 tty_insert_flip_string(&dev->port, skb->data, skb->len);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100586 tty_flip_buffer_push(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 kfree_skb(skb);
589}
590
591static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
592{
593 struct rfcomm_dev *dev = dlc->owner;
594 if (!dev)
595 return;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
598
599 dev->err = err;
Peter Hurley136c3732014-02-09 20:59:02 -0500600 if (dlc->state == BT_CONNECTED) {
Peter Hurleyb92483d2014-02-09 20:59:15 -0500601 rfcomm_reparent_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Peter Hurley136c3732014-02-09 20:59:02 -0500603 wake_up_interruptible(&dev->port.open_wait);
604 } else if (dlc->state == BT_CLOSED)
Gianluca Anzolin29cd7182013-08-27 18:28:46 +0200605 tty_port_tty_hangup(&dev->port, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
609{
610 struct rfcomm_dev *dev = dlc->owner;
611 if (!dev)
612 return;
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
615
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200616 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
617 tty_port_tty_hangup(&dev->port, true);
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700618
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900619 dev->modem_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
621 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
622 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
623 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
624}
625
626/* ---- TTY functions ---- */
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200627static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
628{
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200629 struct sk_buff *skb;
630 int inserted = 0;
631
Jiri Slaby2e124b42013-01-03 15:53:06 +0100632 BT_DBG("dev %p", dev);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200633
634 rfcomm_dlc_lock(dev->dlc);
635
636 while ((skb = skb_dequeue(&dev->pending))) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100637 inserted += tty_insert_flip_string(&dev->port, skb->data,
638 skb->len);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200639 kfree_skb(skb);
640 }
641
642 rfcomm_dlc_unlock(dev->dlc);
643
644 if (inserted > 0)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100645 tty_flip_buffer_push(&dev->port);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200646}
647
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200648/* do the reverse of install, clearing the tty fields and releasing the
649 * reference to tty_port
650 */
651static void rfcomm_tty_cleanup(struct tty_struct *tty)
652{
653 struct rfcomm_dev *dev = tty->driver_data;
654
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200655 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
656
657 rfcomm_dlc_lock(dev->dlc);
658 tty->driver_data = NULL;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200659 rfcomm_dlc_unlock(dev->dlc);
660
Gianluca Anzolinffe6b682013-07-29 17:08:13 +0200661 /*
662 * purge the dlc->tx_queue to avoid circular dependencies
663 * between dev and dlc
664 */
665 skb_queue_purge(&dev->dlc->tx_queue);
666
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200667 tty_port_put(&dev->port);
668}
669
670/* we acquire the tty_port reference since it's here the tty is first used
671 * by setting the termios. We also populate the driver_data field and install
672 * the tty port
673 */
674static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct rfcomm_dev *dev;
677 struct rfcomm_dlc *dlc;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200678 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200680 dev = rfcomm_dev_get(tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (!dev)
682 return -ENODEV;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 dlc = dev->dlc;
685
686 /* Attach TTY and open DLC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 rfcomm_dlc_lock(dlc);
688 tty->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 rfcomm_dlc_unlock(dlc);
690 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
691
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200692 /* install the tty_port */
693 err = tty_port_install(&dev->port, driver, tty);
Gianluca Anzolin5b899242014-01-06 21:23:50 +0100694 if (err) {
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200695 rfcomm_tty_cleanup(tty);
Gianluca Anzolin5b899242014-01-06 21:23:50 +0100696 return err;
697 }
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200698
Gianluca Anzolin5b899242014-01-06 21:23:50 +0100699 /* take over the tty_port reference if the port was created with the
700 * flag RFCOMM_RELEASE_ONHUP. This will force the release of the port
701 * when the last process closes the tty. The behaviour is expected by
702 * userspace.
703 */
Peter Hurley80ea7332014-02-09 20:59:08 -0500704 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
705 set_bit(RFCOMM_TTY_OWNED, &dev->status);
Gianluca Anzolin5b899242014-01-06 21:23:50 +0100706 tty_port_put(&dev->port);
Peter Hurley80ea7332014-02-09 20:59:08 -0500707 }
Gianluca Anzolin5b899242014-01-06 21:23:50 +0100708
709 return 0;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200710}
711
712static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
713{
714 struct rfcomm_dev *dev = tty->driver_data;
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200715 int err;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200716
717 BT_DBG("tty %p id %d", tty, tty->index);
718
719 BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
720 dev->channel, dev->port.count);
721
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200722 err = tty_port_open(&dev->port, tty, filp);
723 if (err)
724 return err;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200725
726 /*
727 * FIXME: rfcomm should use proper flow control for
728 * received data. This hack will be unnecessary and can
729 * be removed when that's implemented
730 */
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200731 rfcomm_tty_copy_pending(dev);
732
733 rfcomm_dlc_unthrottle(dev->dlc);
734
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
739{
740 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Jiri Slabyf997a012012-04-02 13:54:53 +0200741
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100742 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
Jiri Slabyf997a012012-04-02 13:54:53 +0200743 dev->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200745 tty_port_close(&dev->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
749{
750 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
751 struct rfcomm_dlc *dlc = dev->dlc;
752 struct sk_buff *skb;
753 int err = 0, sent = 0, size;
754
755 BT_DBG("tty %p count %d", tty, count);
756
757 while (count) {
758 size = min_t(uint, count, dlc->mtu);
759
760 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!skb)
763 break;
764
765 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
766
767 memcpy(skb_put(skb, size), buf + sent, size);
768
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200769 err = rfcomm_dlc_send(dlc, skb);
770 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 kfree_skb(skb);
772 break;
773 }
774
775 sent += size;
776 count -= size;
777 }
778
779 return sent ? sent : err;
780}
781
782static int rfcomm_tty_write_room(struct tty_struct *tty)
783{
784 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
785 int room;
786
787 BT_DBG("tty %p", tty);
788
Marcel Holtmannb6e557f2007-01-08 02:16:27 +0100789 if (!dev || !dev->dlc)
790 return 0;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
793 if (room < 0)
794 room = 0;
Marcel Holtmannb6e557f2007-01-08 02:16:27 +0100795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return room;
797}
798
Alan Cox6caa76b2011-02-14 16:27:22 +0000799static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
802
803 switch (cmd) {
804 case TCGETS:
805 BT_DBG("TCGETS is not supported");
806 return -ENOIOCTLCMD;
807
808 case TCSETS:
809 BT_DBG("TCSETS is not supported");
810 return -ENOIOCTLCMD;
811
812 case TIOCMIWAIT:
813 BT_DBG("TIOCMIWAIT");
814 break;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 case TIOCGSERIAL:
817 BT_ERR("TIOCGSERIAL is not supported");
818 return -ENOIOCTLCMD;
819
820 case TIOCSSERIAL:
821 BT_ERR("TIOCSSERIAL is not supported");
822 return -ENOIOCTLCMD;
823
824 case TIOCSERGSTRUCT:
825 BT_ERR("TIOCSERGSTRUCT is not supported");
826 return -ENOIOCTLCMD;
827
828 case TIOCSERGETLSR:
829 BT_ERR("TIOCSERGETLSR is not supported");
830 return -ENOIOCTLCMD;
831
832 case TIOCSERCONFIG:
833 BT_ERR("TIOCSERCONFIG is not supported");
834 return -ENOIOCTLCMD;
835
836 default:
837 return -ENOIOCTLCMD; /* ioctls which we must ignore */
838
839 }
840
841 return -ENOIOCTLCMD;
842}
843
Alan Cox606d0992006-12-08 02:38:45 -0800844static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Alan Coxadc8d742012-07-14 15:31:47 +0100846 struct ktermios *new = &tty->termios;
J. Suter3a5e9032005-08-09 20:28:46 -0700847 int old_baud_rate = tty_termios_baud_rate(old);
848 int new_baud_rate = tty_termios_baud_rate(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
J. Suter3a5e9032005-08-09 20:28:46 -0700850 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
851 u16 changes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
J. Suter3a5e9032005-08-09 20:28:46 -0700853 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
854
855 BT_DBG("tty %p termios %p", tty, old);
856
Marcel Holtmannff2d3672006-11-18 22:14:42 +0100857 if (!dev || !dev->dlc || !dev->dlc->session)
Marcel Holtmanncb19d9e2006-10-15 17:31:10 +0200858 return;
859
J. Suter3a5e9032005-08-09 20:28:46 -0700860 /* Handle turning off CRTSCTS */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900861 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
J. Suter3a5e9032005-08-09 20:28:46 -0700862 BT_DBG("Turning off CRTSCTS unsupported");
863
864 /* Parity on/off and when on, odd/even */
865 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200866 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
J. Suter3a5e9032005-08-09 20:28:46 -0700867 changes |= RFCOMM_RPN_PM_PARITY;
868 BT_DBG("Parity change detected.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
J. Suter3a5e9032005-08-09 20:28:46 -0700870
871 /* Mark and space parity are not supported! */
872 if (new->c_cflag & PARENB) {
873 if (new->c_cflag & PARODD) {
874 BT_DBG("Parity is ODD");
875 parity = RFCOMM_RPN_PARITY_ODD;
876 } else {
877 BT_DBG("Parity is EVEN");
878 parity = RFCOMM_RPN_PARITY_EVEN;
879 }
880 } else {
881 BT_DBG("Parity is OFF");
882 parity = RFCOMM_RPN_PARITY_NONE;
883 }
884
885 /* Setting the x_on / x_off characters */
886 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
887 BT_DBG("XOFF custom");
888 x_on = new->c_cc[VSTOP];
889 changes |= RFCOMM_RPN_PM_XON;
890 } else {
891 BT_DBG("XOFF default");
892 x_on = RFCOMM_RPN_XON_CHAR;
893 }
894
895 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
896 BT_DBG("XON custom");
897 x_off = new->c_cc[VSTART];
898 changes |= RFCOMM_RPN_PM_XOFF;
899 } else {
900 BT_DBG("XON default");
901 x_off = RFCOMM_RPN_XOFF_CHAR;
902 }
903
904 /* Handle setting of stop bits */
905 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
906 changes |= RFCOMM_RPN_PM_STOP;
907
908 /* POSIX does not support 1.5 stop bits and RFCOMM does not
909 * support 2 stop bits. So a request for 2 stop bits gets
910 * translated to 1.5 stop bits */
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200911 if (new->c_cflag & CSTOPB)
J. Suter3a5e9032005-08-09 20:28:46 -0700912 stop_bits = RFCOMM_RPN_STOP_15;
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200913 else
J. Suter3a5e9032005-08-09 20:28:46 -0700914 stop_bits = RFCOMM_RPN_STOP_1;
J. Suter3a5e9032005-08-09 20:28:46 -0700915
916 /* Handle number of data bits [5-8] */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900917 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
J. Suter3a5e9032005-08-09 20:28:46 -0700918 changes |= RFCOMM_RPN_PM_DATA;
919
920 switch (new->c_cflag & CSIZE) {
921 case CS5:
922 data_bits = RFCOMM_RPN_DATA_5;
923 break;
924 case CS6:
925 data_bits = RFCOMM_RPN_DATA_6;
926 break;
927 case CS7:
928 data_bits = RFCOMM_RPN_DATA_7;
929 break;
930 case CS8:
931 data_bits = RFCOMM_RPN_DATA_8;
932 break;
933 default:
934 data_bits = RFCOMM_RPN_DATA_8;
935 break;
936 }
937
938 /* Handle baudrate settings */
939 if (old_baud_rate != new_baud_rate)
940 changes |= RFCOMM_RPN_PM_BITRATE;
941
942 switch (new_baud_rate) {
943 case 2400:
944 baud = RFCOMM_RPN_BR_2400;
945 break;
946 case 4800:
947 baud = RFCOMM_RPN_BR_4800;
948 break;
949 case 7200:
950 baud = RFCOMM_RPN_BR_7200;
951 break;
952 case 9600:
953 baud = RFCOMM_RPN_BR_9600;
954 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900955 case 19200:
J. Suter3a5e9032005-08-09 20:28:46 -0700956 baud = RFCOMM_RPN_BR_19200;
957 break;
958 case 38400:
959 baud = RFCOMM_RPN_BR_38400;
960 break;
961 case 57600:
962 baud = RFCOMM_RPN_BR_57600;
963 break;
964 case 115200:
965 baud = RFCOMM_RPN_BR_115200;
966 break;
967 case 230400:
968 baud = RFCOMM_RPN_BR_230400;
969 break;
970 default:
971 /* 9600 is standard accordinag to the RFCOMM specification */
972 baud = RFCOMM_RPN_BR_9600;
973 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900974
J. Suter3a5e9032005-08-09 20:28:46 -0700975 }
976
977 if (changes)
978 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
979 data_bits, stop_bits, parity,
980 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983static void rfcomm_tty_throttle(struct tty_struct *tty)
984{
985 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
986
987 BT_DBG("tty %p dev %p", tty, dev);
J. Suter3a5e9032005-08-09 20:28:46 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rfcomm_dlc_throttle(dev->dlc);
990}
991
992static void rfcomm_tty_unthrottle(struct tty_struct *tty)
993{
994 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
995
996 BT_DBG("tty %p dev %p", tty, dev);
J. Suter3a5e9032005-08-09 20:28:46 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 rfcomm_dlc_unthrottle(dev->dlc);
999}
1000
1001static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1002{
1003 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 BT_DBG("tty %p dev %p", tty, dev);
1006
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001007 if (!dev || !dev->dlc)
1008 return 0;
1009
1010 if (!skb_queue_empty(&dev->dlc->tx_queue))
1011 return dev->dlc->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return 0;
1014}
1015
1016static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1017{
1018 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 BT_DBG("tty %p dev %p", tty, dev);
1021
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001022 if (!dev || !dev->dlc)
1023 return;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 skb_queue_purge(&dev->dlc->tx_queue);
Alan Coxa352def2008-07-16 21:53:12 +01001026 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1030{
1031 BT_DBG("tty %p ch %c", tty, ch);
1032}
1033
1034static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1035{
1036 BT_DBG("tty %p timeout %d", tty, timeout);
1037}
1038
1039static void rfcomm_tty_hangup(struct tty_struct *tty)
1040{
1041 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 BT_DBG("tty %p dev %p", tty, dev);
1044
Gianluca Anzolincad348a2013-07-29 17:08:11 +02001045 tty_port_hangup(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Alan Cox60b33c12011-02-14 16:26:14 +00001048static int rfcomm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001050 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 BT_DBG("tty %p dev %p", tty, dev);
1053
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001054 return dev->modem_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Alan Cox20b9d172011-02-14 16:26:50 +00001057static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
J. Suter3a5e9032005-08-09 20:28:46 -07001059 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1060 struct rfcomm_dlc *dlc = dev->dlc;
1061 u8 v24_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1064
J. Suter3a5e9032005-08-09 20:28:46 -07001065 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
J. Suter3a5e9032005-08-09 20:28:46 -07001067 if (set & TIOCM_DSR || set & TIOCM_DTR)
1068 v24_sig |= RFCOMM_V24_RTC;
1069 if (set & TIOCM_RTS || set & TIOCM_CTS)
1070 v24_sig |= RFCOMM_V24_RTR;
1071 if (set & TIOCM_RI)
1072 v24_sig |= RFCOMM_V24_IC;
1073 if (set & TIOCM_CD)
1074 v24_sig |= RFCOMM_V24_DV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
J. Suter3a5e9032005-08-09 20:28:46 -07001076 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1077 v24_sig &= ~RFCOMM_V24_RTC;
1078 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1079 v24_sig &= ~RFCOMM_V24_RTR;
1080 if (clear & TIOCM_RI)
1081 v24_sig &= ~RFCOMM_V24_IC;
1082 if (clear & TIOCM_CD)
1083 v24_sig &= ~RFCOMM_V24_DV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
J. Suter3a5e9032005-08-09 20:28:46 -07001085 rfcomm_dlc_set_modem_status(dlc, v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
J. Suter3a5e9032005-08-09 20:28:46 -07001087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/* ---- TTY structure ---- */
1091
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001092static const struct tty_operations rfcomm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 .open = rfcomm_tty_open,
1094 .close = rfcomm_tty_close,
1095 .write = rfcomm_tty_write,
1096 .write_room = rfcomm_tty_write_room,
1097 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1098 .flush_buffer = rfcomm_tty_flush_buffer,
1099 .ioctl = rfcomm_tty_ioctl,
1100 .throttle = rfcomm_tty_throttle,
1101 .unthrottle = rfcomm_tty_unthrottle,
1102 .set_termios = rfcomm_tty_set_termios,
1103 .send_xchar = rfcomm_tty_send_xchar,
1104 .hangup = rfcomm_tty_hangup,
1105 .wait_until_sent = rfcomm_tty_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 .tiocmget = rfcomm_tty_tiocmget,
1107 .tiocmset = rfcomm_tty_tiocmset,
Gianluca Anzolin54b926a2013-07-29 17:08:10 +02001108 .install = rfcomm_tty_install,
1109 .cleanup = rfcomm_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110};
1111
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001112int __init rfcomm_init_ttys(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
David Herrmann5ada9912011-10-24 15:30:57 +02001114 int error;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1117 if (!rfcomm_tty_driver)
David Herrmann5ada9912011-10-24 15:30:57 +02001118 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 rfcomm_tty_driver->driver_name = "rfcomm";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 rfcomm_tty_driver->name = "rfcomm";
1122 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1123 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1124 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1125 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001126 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 rfcomm_tty_driver->init_termios = tty_std_termios;
Peter Hurley136c3732014-02-09 20:59:02 -05001128 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
Marcel Holtmannca37bdd2008-07-14 20:13:52 +02001129 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1131
David Herrmann5ada9912011-10-24 15:30:57 +02001132 error = tty_register_driver(rfcomm_tty_driver);
1133 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 BT_ERR("Can't register RFCOMM TTY driver");
1135 put_tty_driver(rfcomm_tty_driver);
David Herrmann5ada9912011-10-24 15:30:57 +02001136 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
1139 BT_INFO("RFCOMM TTY layer initialized");
1140
1141 return 0;
1142}
1143
Gustavo F. Padovan28e95092010-07-31 19:57:05 -03001144void rfcomm_cleanup_ttys(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 tty_unregister_driver(rfcomm_tty_driver);
1147 put_tty_driver(rfcomm_tty_driver);
1148}