blob: 583f7135c8118c9e8975bb74eef605c50a3779d2 [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
54 bdaddr_t src;
55 bdaddr_t dst;
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020056 u8 channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020058 uint modem_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 struct rfcomm_dlc *dlc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Marcel Holtmannc1a33132007-02-17 23:58:57 +010062 struct device *tty_dev;
63
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020064 atomic_t wmem_alloc;
Marcel Holtmanna0c22f22008-07-14 20:13:52 +020065
66 struct sk_buff_head pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69static LIST_HEAD(rfcomm_dev_list);
Gustavo F. Padovan393432c2011-12-27 15:28:45 -020070static DEFINE_SPINLOCK(rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
73static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
74static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* ---- Device functions ---- */
Jiri Slaby67054012012-04-02 13:54:51 +020077
Jiri Slaby67054012012-04-02 13:54:51 +020078static void rfcomm_dev_destruct(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Jiri Slaby67054012012-04-02 13:54:51 +020080 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct rfcomm_dlc *dlc = dev->dlc;
82
83 BT_DBG("dev %p dlc %p", dev, dlc);
84
Gianluca Anzolinebe937f2013-07-29 17:08:09 +020085 spin_lock(&rfcomm_dev_lock);
86 list_del(&dev->list);
87 spin_unlock(&rfcomm_dev_lock);
Ville Tervo8de0a152007-07-11 09:23:41 +020088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 rfcomm_dlc_lock(dlc);
90 /* Detach DLC if it's owned by this dev */
91 if (dlc->owner == dev)
92 dlc->owner = NULL;
93 rfcomm_dlc_unlock(dlc);
94
95 rfcomm_dlc_put(dlc);
96
97 tty_unregister_device(rfcomm_tty_driver, dev->id);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kfree(dev);
100
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900101 /* It's safe to call module_put() here because socket still
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 holds reference to this module. */
103 module_put(THIS_MODULE);
104}
105
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200106/* device-specific initialization: open the dlc */
107static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
108{
109 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
110
111 return rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
112}
113
114/* we block the open until the dlc->state becomes BT_CONNECTED */
115static int rfcomm_dev_carrier_raised(struct tty_port *port)
116{
117 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
118
119 return (dev->dlc->state == BT_CONNECTED);
120}
121
122/* device-specific cleanup: close the dlc */
123static void rfcomm_dev_shutdown(struct tty_port *port)
124{
125 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
126
127 if (dev->tty_dev->parent)
128 device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
129
130 /* close the dlc */
131 rfcomm_dlc_close(dev->dlc, 0);
132}
133
Jiri Slaby67054012012-04-02 13:54:51 +0200134static const struct tty_port_operations rfcomm_port_ops = {
135 .destruct = rfcomm_dev_destruct,
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200136 .activate = rfcomm_dev_activate,
137 .shutdown = rfcomm_dev_shutdown,
138 .carrier_raised = rfcomm_dev_carrier_raised,
Jiri Slaby67054012012-04-02 13:54:51 +0200139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static struct rfcomm_dev *__rfcomm_dev_get(int id)
142{
143 struct rfcomm_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200145 list_for_each_entry(dev, &rfcomm_dev_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (dev->id == id)
147 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return NULL;
150}
151
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300152static struct rfcomm_dev *rfcomm_dev_get(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct rfcomm_dev *dev;
155
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200156 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 dev = __rfcomm_dev_get(id);
Ville Tervo8de0a152007-07-11 09:23:41 +0200159
160 if (dev) {
161 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
162 dev = NULL;
163 else
Jiri Slaby67054012012-04-02 13:54:51 +0200164 tty_port_get(&dev->port);
Ville Tervo8de0a152007-07-11 09:23:41 +0200165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200167 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return dev;
170}
171
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200172static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
173{
174 struct hci_dev *hdev;
175 struct hci_conn *conn;
176
177 hdev = hci_get_route(&dev->dst, &dev->src);
178 if (!hdev)
179 return NULL;
180
181 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200182
183 hci_dev_put(hdev);
184
Marcel Holtmannb2cfcd72006-10-15 17:31:05 +0200185 return conn ? &conn->dev : NULL;
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200186}
187
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200188static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
189{
190 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300191 return sprintf(buf, "%pMR\n", &dev->dst);
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200192}
193
194static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
195{
196 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
197 return sprintf(buf, "%d\n", dev->channel);
198}
199
200static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
201static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
204{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200205 struct rfcomm_dev *dev, *entry;
Luiz Augusto von Dentze57d7582012-03-07 20:20:14 +0200206 struct list_head *head = &rfcomm_dev_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int err = 0;
208
209 BT_DBG("id %d channel %d", req->dev_id, req->channel);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900210
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200211 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!dev)
213 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200215 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (req->dev_id < 0) {
218 dev->id = 0;
219
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200220 list_for_each_entry(entry, &rfcomm_dev_list, list) {
221 if (entry->id != dev->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223
224 dev->id++;
Luiz Augusto von Dentze57d7582012-03-07 20:20:14 +0200225 head = &entry->list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 } else {
228 dev->id = req->dev_id;
229
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200230 list_for_each_entry(entry, &rfcomm_dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (entry->id == dev->id) {
232 err = -EADDRINUSE;
233 goto out;
234 }
235
236 if (entry->id > dev->id - 1)
237 break;
238
Luiz Augusto von Dentze57d7582012-03-07 20:20:14 +0200239 head = &entry->list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
242
243 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
244 err = -ENFILE;
245 goto out;
246 }
247
248 sprintf(dev->name, "rfcomm%d", dev->id);
249
250 list_add(&dev->list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 bacpy(&dev->src, &req->src);
253 bacpy(&dev->dst, &req->dst);
254 dev->channel = req->channel;
255
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900256 dev->flags = req->flags &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
258
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200259 tty_port_init(&dev->port);
Jiri Slaby67054012012-04-02 13:54:51 +0200260 dev->port.ops = &rfcomm_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200262 skb_queue_head_init(&dev->pending);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 rfcomm_dlc_lock(dlc);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200265
266 if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
267 struct sock *sk = dlc->owner;
268 struct sk_buff *skb;
269
270 BUG_ON(!sk);
271
272 rfcomm_dlc_throttle(dlc);
273
274 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
275 skb_orphan(skb);
276 skb_queue_tail(&dev->pending, skb);
277 atomic_sub(skb->len, &sk->sk_rmem_alloc);
278 }
279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dlc->data_ready = rfcomm_dev_data_ready;
282 dlc->state_change = rfcomm_dev_state_change;
283 dlc->modem_status = rfcomm_dev_modem_status;
284
285 dlc->owner = dev;
286 dev->dlc = dlc;
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +0200287
288 rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 rfcomm_dlc_unlock(dlc);
291
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900292 /* It's safe to call __module_get() here because socket already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 holds reference to this module. */
294 __module_get(THIS_MODULE);
295
296out:
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200297 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800299 if (err < 0)
300 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jiri Slaby734cc172012-08-07 21:47:47 +0200302 dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
303 dev->id, NULL);
Ville Tervo8de0a152007-07-11 09:23:41 +0200304 if (IS_ERR(dev->tty_dev)) {
Marcel Holtmann09c7d822007-07-26 00:12:25 -0700305 err = PTR_ERR(dev->tty_dev);
Gianluca Anzolinebe937f2013-07-29 17:08:09 +0200306 spin_lock(&rfcomm_dev_lock);
Ville Tervo8de0a152007-07-11 09:23:41 +0200307 list_del(&dev->list);
Gianluca Anzolinebe937f2013-07-29 17:08:09 +0200308 spin_unlock(&rfcomm_dev_lock);
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800309 goto free;
Ville Tervo8de0a152007-07-11 09:23:41 +0200310 }
311
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200312 dev_set_drvdata(dev->tty_dev, dev);
313
314 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
315 BT_ERR("Failed to create address attribute");
316
317 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
318 BT_ERR("Failed to create channel attribute");
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return dev->id;
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800321
322free:
323 kfree(dev);
324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static void rfcomm_dev_del(struct rfcomm_dev *dev)
328{
Jiri Slabyf997a012012-04-02 13:54:53 +0200329 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 BT_DBG("dev %p", dev);
331
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100332 BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags));
333
Jiri Slabyf997a012012-04-02 13:54:53 +0200334 spin_lock_irqsave(&dev->port.lock, flags);
335 if (dev->port.count > 0) {
336 spin_unlock_irqrestore(&dev->port.lock, flags);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100337 return;
Jiri Slabyf997a012012-04-02 13:54:53 +0200338 }
339 spin_unlock_irqrestore(&dev->port.lock, flags);
Dave Youngf9513752008-01-10 22:22:52 -0800340
Jiri Slaby67054012012-04-02 13:54:51 +0200341 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344/* ---- Send buffer ---- */
345static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
346{
347 /* We can't let it be zero, because we don't get a callback
348 when tx_credits becomes nonzero, hence we'd never wake up */
349 return dlc->mtu * (dlc->tx_credits?:1);
350}
351
352static void rfcomm_wfree(struct sk_buff *skb)
353{
354 struct rfcomm_dev *dev = (void *) skb->sk;
355 atomic_sub(skb->truesize, &dev->wmem_alloc);
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200356 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
357 tty_port_tty_wakeup(&dev->port);
Jiri Slaby67054012012-04-02 13:54:51 +0200358 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300361static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Jiri Slaby67054012012-04-02 13:54:51 +0200363 tty_port_get(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 atomic_add(skb->truesize, &dev->wmem_alloc);
365 skb->sk = (void *) dev;
366 skb->destructor = rfcomm_wfree;
367}
368
Al Virodd0fc662005-10-07 07:46:04 +0100369static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
372 struct sk_buff *skb = alloc_skb(size, priority);
373 if (skb) {
374 rfcomm_set_owner_w(skb, dev);
375 return skb;
376 }
377 }
378 return NULL;
379}
380
381/* ---- Device IOCTLs ---- */
382
383#define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
384
385static int rfcomm_create_dev(struct sock *sk, void __user *arg)
386{
387 struct rfcomm_dev_req req;
388 struct rfcomm_dlc *dlc;
389 int id;
390
391 if (copy_from_user(&req, arg, sizeof(req)))
392 return -EFAULT;
393
Ville Tervo8de0a152007-07-11 09:23:41 +0200394 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
397 return -EPERM;
398
399 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
400 /* Socket must be connected */
401 if (sk->sk_state != BT_CONNECTED)
402 return -EBADFD;
403
404 dlc = rfcomm_pi(sk)->dlc;
405 rfcomm_dlc_hold(dlc);
406 } else {
407 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
408 if (!dlc)
409 return -ENOMEM;
410 }
411
412 id = rfcomm_dev_add(&req, dlc);
413 if (id < 0) {
414 rfcomm_dlc_put(dlc);
415 return id;
416 }
417
418 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
419 /* DLC is now used by device.
420 * Socket must be disconnected */
421 sk->sk_state = BT_CLOSED;
422 }
423
424 return id;
425}
426
427static int rfcomm_release_dev(void __user *arg)
428{
429 struct rfcomm_dev_req req;
430 struct rfcomm_dev *dev;
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200431 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (copy_from_user(&req, arg, sizeof(req)))
434 return -EFAULT;
435
Ville Tervo8de0a152007-07-11 09:23:41 +0200436 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200438 dev = rfcomm_dev_get(req.dev_id);
439 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return -ENODEV;
441
442 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
Jiri Slaby67054012012-04-02 13:54:51 +0200443 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EPERM;
445 }
446
447 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
448 rfcomm_dlc_close(dev->dlc, 0);
449
Mikko Rapeli84950cf2007-07-11 09:18:15 +0200450 /* Shut down TTY synchronously before freeing rfcomm_dev */
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200451 tty = tty_port_tty_get(&dev->port);
452 if (tty) {
453 tty_vhangup(tty);
454 tty_kref_put(tty);
455 }
Mikko Rapeli84950cf2007-07-11 09:18:15 +0200456
Dave Young93d80742008-02-05 03:12:06 -0800457 if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
458 rfcomm_dev_del(dev);
Jiri Slaby67054012012-04-02 13:54:51 +0200459 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461}
462
463static int rfcomm_get_dev_list(void __user *arg)
464{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200465 struct rfcomm_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct rfcomm_dev_list_req *dl;
467 struct rfcomm_dev_info *di;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int n = 0, size, err;
469 u16 dev_num;
470
471 BT_DBG("");
472
473 if (get_user(dev_num, (u16 __user *) arg))
474 return -EFAULT;
475
476 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
477 return -EINVAL;
478
479 size = sizeof(*dl) + dev_num * sizeof(*di);
480
Mathias Krausef9432c52012-08-15 11:31:49 +0000481 dl = kzalloc(size, GFP_KERNEL);
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200482 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -ENOMEM;
484
485 di = dl->dev_info;
486
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200487 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200489 list_for_each_entry(dev, &rfcomm_dev_list, list) {
Ville Tervo8de0a152007-07-11 09:23:41 +0200490 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
491 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 (di + n)->id = dev->id;
493 (di + n)->flags = dev->flags;
494 (di + n)->state = dev->dlc->state;
495 (di + n)->channel = dev->channel;
496 bacpy(&(di + n)->src, &dev->src);
497 bacpy(&(di + n)->dst, &dev->dst);
498 if (++n >= dev_num)
499 break;
500 }
501
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200502 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 dl->dev_num = n;
505 size = sizeof(*dl) + n * sizeof(*di);
506
507 err = copy_to_user(arg, dl, size);
508 kfree(dl);
509
510 return err ? -EFAULT : 0;
511}
512
513static int rfcomm_get_dev_info(void __user *arg)
514{
515 struct rfcomm_dev *dev;
516 struct rfcomm_dev_info di;
517 int err = 0;
518
519 BT_DBG("");
520
521 if (copy_from_user(&di, arg, sizeof(di)))
522 return -EFAULT;
523
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200524 dev = rfcomm_dev_get(di.id);
525 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -ENODEV;
527
528 di.flags = dev->flags;
529 di.channel = dev->channel;
530 di.state = dev->dlc->state;
531 bacpy(&di.src, &dev->src);
532 bacpy(&di.dst, &dev->dst);
533
534 if (copy_to_user(arg, &di, sizeof(di)))
535 err = -EFAULT;
536
Jiri Slaby67054012012-04-02 13:54:51 +0200537 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return err;
539}
540
541int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
542{
543 BT_DBG("cmd %d arg %p", cmd, arg);
544
545 switch (cmd) {
546 case RFCOMMCREATEDEV:
547 return rfcomm_create_dev(sk, arg);
548
549 case RFCOMMRELEASEDEV:
550 return rfcomm_release_dev(arg);
551
552 case RFCOMMGETDEVLIST:
553 return rfcomm_get_dev_list(arg);
554
555 case RFCOMMGETDEVINFO:
556 return rfcomm_get_dev_info(arg);
557 }
558
559 return -EINVAL;
560}
561
562/* ---- DLC callbacks ---- */
563static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
564{
565 struct rfcomm_dev *dev = dlc->owner;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900566
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200567 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 kfree_skb(skb);
569 return;
570 }
571
Jiri Slaby2e124b42013-01-03 15:53:06 +0100572 if (!skb_queue_empty(&dev->pending)) {
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200573 skb_queue_tail(&dev->pending, skb);
574 return;
575 }
576
Jiri Slaby2e124b42013-01-03 15:53:06 +0100577 BT_DBG("dlc %p len %d", dlc, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100579 tty_insert_flip_string(&dev->port, skb->data, skb->len);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100580 tty_flip_buffer_push(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 kfree_skb(skb);
583}
584
585static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
586{
587 struct rfcomm_dev *dev = dlc->owner;
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200588 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!dev)
590 return;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
593
594 dev->err = err;
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200595 if (dlc->state == BT_CONNECTED) {
596 device_move(dev->tty_dev, rfcomm_get_device(dev),
597 DPM_ORDER_DEV_AFTER_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200599 wake_up_interruptible(&dev->port.open_wait);
600 } else if (dlc->state == BT_CLOSED) {
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200601 tty = tty_port_tty_get(&dev->port);
602 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
Dave Young537d59a2008-06-01 23:50:52 -0700604 /* Drop DLC lock here to avoid deadlock
605 * 1. rfcomm_dev_get will take rfcomm_dev_lock
606 * but in rfcomm_dev_add there's lock order:
607 * rfcomm_dev_lock -> dlc lock
Jiri Slaby67054012012-04-02 13:54:51 +0200608 * 2. tty_port_put will deadlock if it's
Dave Young537d59a2008-06-01 23:50:52 -0700609 * the last reference
610 */
611 rfcomm_dlc_unlock(dlc);
612 if (rfcomm_dev_get(dev->id) == NULL) {
613 rfcomm_dlc_lock(dlc);
Marcel Holtmann77f2a452007-05-05 00:36:10 +0200614 return;
Dave Young537d59a2008-06-01 23:50:52 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Marcel Holtmann77f2a452007-05-05 00:36:10 +0200617 rfcomm_dev_del(dev);
Jiri Slaby67054012012-04-02 13:54:51 +0200618 tty_port_put(&dev->port);
Dave Young537d59a2008-06-01 23:50:52 -0700619 rfcomm_dlc_lock(dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200621 } else {
622 tty_hangup(tty);
623 tty_kref_put(tty);
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626}
627
628static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
629{
630 struct rfcomm_dev *dev = dlc->owner;
631 if (!dev)
632 return;
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
635
Gianluca Anzolin396dc222013-07-29 17:08:08 +0200636 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
637 tty_port_tty_hangup(&dev->port, true);
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700638
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900639 dev->modem_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
641 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
642 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
643 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
644}
645
646/* ---- TTY functions ---- */
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200647static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
648{
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200649 struct sk_buff *skb;
650 int inserted = 0;
651
Jiri Slaby2e124b42013-01-03 15:53:06 +0100652 BT_DBG("dev %p", dev);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200653
654 rfcomm_dlc_lock(dev->dlc);
655
656 while ((skb = skb_dequeue(&dev->pending))) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100657 inserted += tty_insert_flip_string(&dev->port, skb->data,
658 skb->len);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200659 kfree_skb(skb);
660 }
661
662 rfcomm_dlc_unlock(dev->dlc);
663
664 if (inserted > 0)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100665 tty_flip_buffer_push(&dev->port);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200666}
667
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200668/* do the reverse of install, clearing the tty fields and releasing the
669 * reference to tty_port
670 */
671static void rfcomm_tty_cleanup(struct tty_struct *tty)
672{
673 struct rfcomm_dev *dev = tty->driver_data;
674
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200675 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
676
677 rfcomm_dlc_lock(dev->dlc);
678 tty->driver_data = NULL;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200679 rfcomm_dlc_unlock(dev->dlc);
680
681 tty_port_put(&dev->port);
682}
683
684/* we acquire the tty_port reference since it's here the tty is first used
685 * by setting the termios. We also populate the driver_data field and install
686 * the tty port
687 */
688static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct rfcomm_dev *dev;
691 struct rfcomm_dlc *dlc;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200692 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200694 dev = rfcomm_dev_get(tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (!dev)
696 return -ENODEV;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 dlc = dev->dlc;
699
700 /* Attach TTY and open DLC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 rfcomm_dlc_lock(dlc);
702 tty->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 rfcomm_dlc_unlock(dlc);
704 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
705
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200706 /* install the tty_port */
707 err = tty_port_install(&dev->port, driver, tty);
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200708 if (err)
709 rfcomm_tty_cleanup(tty);
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200710
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200711 return err;
712}
713
714static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
715{
716 struct rfcomm_dev *dev = tty->driver_data;
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200717 int err;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200718
719 BT_DBG("tty %p id %d", tty, tty->index);
720
721 BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
722 dev->channel, dev->port.count);
723
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200724 err = tty_port_open(&dev->port, tty, filp);
725 if (err)
726 return err;
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200727
728 /*
729 * FIXME: rfcomm should use proper flow control for
730 * received data. This hack will be unnecessary and can
731 * be removed when that's implemented
732 */
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200733 rfcomm_tty_copy_pending(dev);
734
735 rfcomm_dlc_unthrottle(dev->dlc);
736
Gianluca Anzolin54b926a2013-07-29 17:08:10 +0200737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
741{
742 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Jiri Slabyf997a012012-04-02 13:54:53 +0200743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!dev)
745 return;
746
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100747 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
Jiri Slabyf997a012012-04-02 13:54:53 +0200748 dev->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200750 tty_port_close(&dev->port, tty, filp);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100751
Gianluca Anzolincad348a2013-07-29 17:08:11 +0200752 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
753 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
757{
758 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
759 struct rfcomm_dlc *dlc = dev->dlc;
760 struct sk_buff *skb;
761 int err = 0, sent = 0, size;
762
763 BT_DBG("tty %p count %d", tty, count);
764
765 while (count) {
766 size = min_t(uint, count, dlc->mtu);
767
768 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!skb)
771 break;
772
773 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
774
775 memcpy(skb_put(skb, size), buf + sent, size);
776
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200777 err = rfcomm_dlc_send(dlc, skb);
778 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 kfree_skb(skb);
780 break;
781 }
782
783 sent += size;
784 count -= size;
785 }
786
787 return sent ? sent : err;
788}
789
790static int rfcomm_tty_write_room(struct tty_struct *tty)
791{
792 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
793 int room;
794
795 BT_DBG("tty %p", tty);
796
Marcel Holtmannb6e557f2007-01-08 02:16:27 +0100797 if (!dev || !dev->dlc)
798 return 0;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
801 if (room < 0)
802 room = 0;
Marcel Holtmannb6e557f2007-01-08 02:16:27 +0100803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return room;
805}
806
Alan Cox6caa76b2011-02-14 16:27:22 +0000807static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
810
811 switch (cmd) {
812 case TCGETS:
813 BT_DBG("TCGETS is not supported");
814 return -ENOIOCTLCMD;
815
816 case TCSETS:
817 BT_DBG("TCSETS is not supported");
818 return -ENOIOCTLCMD;
819
820 case TIOCMIWAIT:
821 BT_DBG("TIOCMIWAIT");
822 break;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 case TIOCGSERIAL:
825 BT_ERR("TIOCGSERIAL is not supported");
826 return -ENOIOCTLCMD;
827
828 case TIOCSSERIAL:
829 BT_ERR("TIOCSSERIAL is not supported");
830 return -ENOIOCTLCMD;
831
832 case TIOCSERGSTRUCT:
833 BT_ERR("TIOCSERGSTRUCT is not supported");
834 return -ENOIOCTLCMD;
835
836 case TIOCSERGETLSR:
837 BT_ERR("TIOCSERGETLSR is not supported");
838 return -ENOIOCTLCMD;
839
840 case TIOCSERCONFIG:
841 BT_ERR("TIOCSERCONFIG is not supported");
842 return -ENOIOCTLCMD;
843
844 default:
845 return -ENOIOCTLCMD; /* ioctls which we must ignore */
846
847 }
848
849 return -ENOIOCTLCMD;
850}
851
Alan Cox606d0992006-12-08 02:38:45 -0800852static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Alan Coxadc8d742012-07-14 15:31:47 +0100854 struct ktermios *new = &tty->termios;
J. Suter3a5e9032005-08-09 20:28:46 -0700855 int old_baud_rate = tty_termios_baud_rate(old);
856 int new_baud_rate = tty_termios_baud_rate(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
J. Suter3a5e9032005-08-09 20:28:46 -0700858 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
859 u16 changes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
J. Suter3a5e9032005-08-09 20:28:46 -0700861 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
862
863 BT_DBG("tty %p termios %p", tty, old);
864
Marcel Holtmannff2d3672006-11-18 22:14:42 +0100865 if (!dev || !dev->dlc || !dev->dlc->session)
Marcel Holtmanncb19d9e2006-10-15 17:31:10 +0200866 return;
867
J. Suter3a5e9032005-08-09 20:28:46 -0700868 /* Handle turning off CRTSCTS */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900869 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
J. Suter3a5e9032005-08-09 20:28:46 -0700870 BT_DBG("Turning off CRTSCTS unsupported");
871
872 /* Parity on/off and when on, odd/even */
873 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200874 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
J. Suter3a5e9032005-08-09 20:28:46 -0700875 changes |= RFCOMM_RPN_PM_PARITY;
876 BT_DBG("Parity change detected.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
J. Suter3a5e9032005-08-09 20:28:46 -0700878
879 /* Mark and space parity are not supported! */
880 if (new->c_cflag & PARENB) {
881 if (new->c_cflag & PARODD) {
882 BT_DBG("Parity is ODD");
883 parity = RFCOMM_RPN_PARITY_ODD;
884 } else {
885 BT_DBG("Parity is EVEN");
886 parity = RFCOMM_RPN_PARITY_EVEN;
887 }
888 } else {
889 BT_DBG("Parity is OFF");
890 parity = RFCOMM_RPN_PARITY_NONE;
891 }
892
893 /* Setting the x_on / x_off characters */
894 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
895 BT_DBG("XOFF custom");
896 x_on = new->c_cc[VSTOP];
897 changes |= RFCOMM_RPN_PM_XON;
898 } else {
899 BT_DBG("XOFF default");
900 x_on = RFCOMM_RPN_XON_CHAR;
901 }
902
903 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
904 BT_DBG("XON custom");
905 x_off = new->c_cc[VSTART];
906 changes |= RFCOMM_RPN_PM_XOFF;
907 } else {
908 BT_DBG("XON default");
909 x_off = RFCOMM_RPN_XOFF_CHAR;
910 }
911
912 /* Handle setting of stop bits */
913 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
914 changes |= RFCOMM_RPN_PM_STOP;
915
916 /* POSIX does not support 1.5 stop bits and RFCOMM does not
917 * support 2 stop bits. So a request for 2 stop bits gets
918 * translated to 1.5 stop bits */
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200919 if (new->c_cflag & CSTOPB)
J. Suter3a5e9032005-08-09 20:28:46 -0700920 stop_bits = RFCOMM_RPN_STOP_15;
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200921 else
J. Suter3a5e9032005-08-09 20:28:46 -0700922 stop_bits = RFCOMM_RPN_STOP_1;
J. Suter3a5e9032005-08-09 20:28:46 -0700923
924 /* Handle number of data bits [5-8] */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900925 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
J. Suter3a5e9032005-08-09 20:28:46 -0700926 changes |= RFCOMM_RPN_PM_DATA;
927
928 switch (new->c_cflag & CSIZE) {
929 case CS5:
930 data_bits = RFCOMM_RPN_DATA_5;
931 break;
932 case CS6:
933 data_bits = RFCOMM_RPN_DATA_6;
934 break;
935 case CS7:
936 data_bits = RFCOMM_RPN_DATA_7;
937 break;
938 case CS8:
939 data_bits = RFCOMM_RPN_DATA_8;
940 break;
941 default:
942 data_bits = RFCOMM_RPN_DATA_8;
943 break;
944 }
945
946 /* Handle baudrate settings */
947 if (old_baud_rate != new_baud_rate)
948 changes |= RFCOMM_RPN_PM_BITRATE;
949
950 switch (new_baud_rate) {
951 case 2400:
952 baud = RFCOMM_RPN_BR_2400;
953 break;
954 case 4800:
955 baud = RFCOMM_RPN_BR_4800;
956 break;
957 case 7200:
958 baud = RFCOMM_RPN_BR_7200;
959 break;
960 case 9600:
961 baud = RFCOMM_RPN_BR_9600;
962 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900963 case 19200:
J. Suter3a5e9032005-08-09 20:28:46 -0700964 baud = RFCOMM_RPN_BR_19200;
965 break;
966 case 38400:
967 baud = RFCOMM_RPN_BR_38400;
968 break;
969 case 57600:
970 baud = RFCOMM_RPN_BR_57600;
971 break;
972 case 115200:
973 baud = RFCOMM_RPN_BR_115200;
974 break;
975 case 230400:
976 baud = RFCOMM_RPN_BR_230400;
977 break;
978 default:
979 /* 9600 is standard accordinag to the RFCOMM specification */
980 baud = RFCOMM_RPN_BR_9600;
981 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900982
J. Suter3a5e9032005-08-09 20:28:46 -0700983 }
984
985 if (changes)
986 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
987 data_bits, stop_bits, parity,
988 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static void rfcomm_tty_throttle(struct tty_struct *tty)
992{
993 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
994
995 BT_DBG("tty %p dev %p", tty, dev);
J. Suter3a5e9032005-08-09 20:28:46 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rfcomm_dlc_throttle(dev->dlc);
998}
999
1000static void rfcomm_tty_unthrottle(struct tty_struct *tty)
1001{
1002 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1003
1004 BT_DBG("tty %p dev %p", tty, dev);
J. Suter3a5e9032005-08-09 20:28:46 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 rfcomm_dlc_unthrottle(dev->dlc);
1007}
1008
1009static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1010{
1011 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 BT_DBG("tty %p dev %p", tty, dev);
1014
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001015 if (!dev || !dev->dlc)
1016 return 0;
1017
1018 if (!skb_queue_empty(&dev->dlc->tx_queue))
1019 return dev->dlc->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 return 0;
1022}
1023
1024static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1025{
1026 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 BT_DBG("tty %p dev %p", tty, dev);
1029
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001030 if (!dev || !dev->dlc)
1031 return;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 skb_queue_purge(&dev->dlc->tx_queue);
Alan Coxa352def2008-07-16 21:53:12 +01001034 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1038{
1039 BT_DBG("tty %p ch %c", tty, ch);
1040}
1041
1042static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1043{
1044 BT_DBG("tty %p timeout %d", tty, timeout);
1045}
1046
1047static void rfcomm_tty_hangup(struct tty_struct *tty)
1048{
1049 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 BT_DBG("tty %p dev %p", tty, dev);
1052
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001053 if (!dev)
1054 return;
1055
Gianluca Anzolincad348a2013-07-29 17:08:11 +02001056 tty_port_hangup(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Marcel Holtmann77f2a452007-05-05 00:36:10 +02001058 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
1059 if (rfcomm_dev_get(dev->id) == NULL)
1060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 rfcomm_dev_del(dev);
Jiri Slaby67054012012-04-02 13:54:51 +02001062 tty_port_put(&dev->port);
Marcel Holtmann77f2a452007-05-05 00:36:10 +02001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Alan Cox60b33c12011-02-14 16:26:14 +00001066static int rfcomm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001068 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 BT_DBG("tty %p dev %p", tty, dev);
1071
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001072 return dev->modem_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Alan Cox20b9d172011-02-14 16:26:50 +00001075static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
J. Suter3a5e9032005-08-09 20:28:46 -07001077 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1078 struct rfcomm_dlc *dlc = dev->dlc;
1079 u8 v24_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1082
J. Suter3a5e9032005-08-09 20:28:46 -07001083 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
J. Suter3a5e9032005-08-09 20:28:46 -07001085 if (set & TIOCM_DSR || set & TIOCM_DTR)
1086 v24_sig |= RFCOMM_V24_RTC;
1087 if (set & TIOCM_RTS || set & TIOCM_CTS)
1088 v24_sig |= RFCOMM_V24_RTR;
1089 if (set & TIOCM_RI)
1090 v24_sig |= RFCOMM_V24_IC;
1091 if (set & TIOCM_CD)
1092 v24_sig |= RFCOMM_V24_DV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
J. Suter3a5e9032005-08-09 20:28:46 -07001094 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1095 v24_sig &= ~RFCOMM_V24_RTC;
1096 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1097 v24_sig &= ~RFCOMM_V24_RTR;
1098 if (clear & TIOCM_RI)
1099 v24_sig &= ~RFCOMM_V24_IC;
1100 if (clear & TIOCM_CD)
1101 v24_sig &= ~RFCOMM_V24_DV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
J. Suter3a5e9032005-08-09 20:28:46 -07001103 rfcomm_dlc_set_modem_status(dlc, v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
J. Suter3a5e9032005-08-09 20:28:46 -07001105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108/* ---- TTY structure ---- */
1109
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001110static const struct tty_operations rfcomm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 .open = rfcomm_tty_open,
1112 .close = rfcomm_tty_close,
1113 .write = rfcomm_tty_write,
1114 .write_room = rfcomm_tty_write_room,
1115 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1116 .flush_buffer = rfcomm_tty_flush_buffer,
1117 .ioctl = rfcomm_tty_ioctl,
1118 .throttle = rfcomm_tty_throttle,
1119 .unthrottle = rfcomm_tty_unthrottle,
1120 .set_termios = rfcomm_tty_set_termios,
1121 .send_xchar = rfcomm_tty_send_xchar,
1122 .hangup = rfcomm_tty_hangup,
1123 .wait_until_sent = rfcomm_tty_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 .tiocmget = rfcomm_tty_tiocmget,
1125 .tiocmset = rfcomm_tty_tiocmset,
Gianluca Anzolin54b926a2013-07-29 17:08:10 +02001126 .install = rfcomm_tty_install,
1127 .cleanup = rfcomm_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128};
1129
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001130int __init rfcomm_init_ttys(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
David Herrmann5ada9912011-10-24 15:30:57 +02001132 int error;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1135 if (!rfcomm_tty_driver)
David Herrmann5ada9912011-10-24 15:30:57 +02001136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 rfcomm_tty_driver->driver_name = "rfcomm";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 rfcomm_tty_driver->name = "rfcomm";
1140 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1141 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1142 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1143 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001144 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 rfcomm_tty_driver->init_termios = tty_std_termios;
Gianluca Anzolincad348a2013-07-29 17:08:11 +02001146 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
Marcel Holtmannca37bdd2008-07-14 20:13:52 +02001147 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1149
David Herrmann5ada9912011-10-24 15:30:57 +02001150 error = tty_register_driver(rfcomm_tty_driver);
1151 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 BT_ERR("Can't register RFCOMM TTY driver");
1153 put_tty_driver(rfcomm_tty_driver);
David Herrmann5ada9912011-10-24 15:30:57 +02001154 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 BT_INFO("RFCOMM TTY layer initialized");
1158
1159 return 0;
1160}
1161
Gustavo F. Padovan28e95092010-07-31 19:57:05 -03001162void rfcomm_cleanup_ttys(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 tty_unregister_driver(rfcomm_tty_driver);
1165 put_tty_driver(rfcomm_tty_driver);
1166}