blob: ccc248791d50239c24fce5c0635245f33f654fa3 [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 wait_queue_head_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Marcel Holtmannc1a33132007-02-17 23:58:57 +010063 struct device *tty_dev;
64
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020065 atomic_t wmem_alloc;
Marcel Holtmanna0c22f22008-07-14 20:13:52 +020066
67 struct sk_buff_head pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70static LIST_HEAD(rfcomm_dev_list);
Gustavo F. Padovan393432c2011-12-27 15:28:45 -020071static DEFINE_SPINLOCK(rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
74static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
75static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* ---- Device functions ---- */
Jiri Slaby67054012012-04-02 13:54:51 +020078
79/*
80 * The reason this isn't actually a race, as you no doubt have a little voice
81 * screaming at you in your head, is that the refcount should never actually
82 * reach zero unless the device has already been taken off the list, in
83 * rfcomm_dev_del(). And if that's not true, we'll hit the BUG() in
84 * rfcomm_dev_destruct() anyway.
85 */
86static void rfcomm_dev_destruct(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Jiri Slaby67054012012-04-02 13:54:51 +020088 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct rfcomm_dlc *dlc = dev->dlc;
90
91 BT_DBG("dev %p dlc %p", dev, dlc);
92
Dave Youngf9513752008-01-10 22:22:52 -080093 /* Refcount should only hit zero when called from rfcomm_dev_del()
94 which will have taken us off the list. Everything else are
95 refcounting bugs. */
96 BUG_ON(!list_empty(&dev->list));
Ville Tervo8de0a152007-07-11 09:23:41 +020097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 rfcomm_dlc_lock(dlc);
99 /* Detach DLC if it's owned by this dev */
100 if (dlc->owner == dev)
101 dlc->owner = NULL;
102 rfcomm_dlc_unlock(dlc);
103
104 rfcomm_dlc_put(dlc);
105
106 tty_unregister_device(rfcomm_tty_driver, dev->id);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 kfree(dev);
109
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900110 /* It's safe to call module_put() here because socket still
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 holds reference to this module. */
112 module_put(THIS_MODULE);
113}
114
Jiri Slaby67054012012-04-02 13:54:51 +0200115static const struct tty_port_operations rfcomm_port_ops = {
116 .destruct = rfcomm_dev_destruct,
117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static struct rfcomm_dev *__rfcomm_dev_get(int id)
120{
121 struct rfcomm_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200123 list_for_each_entry(dev, &rfcomm_dev_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (dev->id == id)
125 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return NULL;
128}
129
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300130static struct rfcomm_dev *rfcomm_dev_get(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct rfcomm_dev *dev;
133
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200134 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 dev = __rfcomm_dev_get(id);
Ville Tervo8de0a152007-07-11 09:23:41 +0200137
138 if (dev) {
139 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
140 dev = NULL;
141 else
Jiri Slaby67054012012-04-02 13:54:51 +0200142 tty_port_get(&dev->port);
Ville Tervo8de0a152007-07-11 09:23:41 +0200143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200145 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return dev;
148}
149
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200150static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
151{
152 struct hci_dev *hdev;
153 struct hci_conn *conn;
154
155 hdev = hci_get_route(&dev->dst, &dev->src);
156 if (!hdev)
157 return NULL;
158
159 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200160
161 hci_dev_put(hdev);
162
Marcel Holtmannb2cfcd72006-10-15 17:31:05 +0200163 return conn ? &conn->dev : NULL;
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200164}
165
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200166static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
167{
168 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -0300169 return sprintf(buf, "%s\n", batostr(&dev->dst));
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200170}
171
172static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
173{
174 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
175 return sprintf(buf, "%d\n", dev->channel);
176}
177
178static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
179static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
182{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200183 struct rfcomm_dev *dev, *entry;
Luiz Augusto von Dentze57d758a2012-03-07 20:20:14 +0200184 struct list_head *head = &rfcomm_dev_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int err = 0;
186
187 BT_DBG("id %d channel %d", req->dev_id, req->channel);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900188
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200189 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!dev)
191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200193 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (req->dev_id < 0) {
196 dev->id = 0;
197
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200198 list_for_each_entry(entry, &rfcomm_dev_list, list) {
199 if (entry->id != dev->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 dev->id++;
Luiz Augusto von Dentze57d758a2012-03-07 20:20:14 +0200203 head = &entry->list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 } else {
206 dev->id = req->dev_id;
207
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200208 list_for_each_entry(entry, &rfcomm_dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (entry->id == dev->id) {
210 err = -EADDRINUSE;
211 goto out;
212 }
213
214 if (entry->id > dev->id - 1)
215 break;
216
Luiz Augusto von Dentze57d758a2012-03-07 20:20:14 +0200217 head = &entry->list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219 }
220
221 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
222 err = -ENFILE;
223 goto out;
224 }
225
226 sprintf(dev->name, "rfcomm%d", dev->id);
227
228 list_add(&dev->list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 bacpy(&dev->src, &req->src);
231 bacpy(&dev->dst, &req->dst);
232 dev->channel = req->channel;
233
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900234 dev->flags = req->flags &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
236
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200237 tty_port_init(&dev->port);
Jiri Slaby67054012012-04-02 13:54:51 +0200238 dev->port.ops = &rfcomm_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 init_waitqueue_head(&dev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200241 skb_queue_head_init(&dev->pending);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 rfcomm_dlc_lock(dlc);
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200244
245 if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
246 struct sock *sk = dlc->owner;
247 struct sk_buff *skb;
248
249 BUG_ON(!sk);
250
251 rfcomm_dlc_throttle(dlc);
252
253 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
254 skb_orphan(skb);
255 skb_queue_tail(&dev->pending, skb);
256 atomic_sub(skb->len, &sk->sk_rmem_alloc);
257 }
258 }
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dlc->data_ready = rfcomm_dev_data_ready;
261 dlc->state_change = rfcomm_dev_state_change;
262 dlc->modem_status = rfcomm_dev_modem_status;
263
264 dlc->owner = dev;
265 dev->dlc = dlc;
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +0200266
267 rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 rfcomm_dlc_unlock(dlc);
270
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900271 /* It's safe to call __module_get() here because socket already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 holds reference to this module. */
273 __module_get(THIS_MODULE);
274
275out:
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200276 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800278 if (err < 0)
279 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jiri Slaby734cc172012-08-07 21:47:47 +0200281 dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
282 dev->id, NULL);
Ville Tervo8de0a152007-07-11 09:23:41 +0200283 if (IS_ERR(dev->tty_dev)) {
Marcel Holtmann09c7d822007-07-26 00:12:25 -0700284 err = PTR_ERR(dev->tty_dev);
Ville Tervo8de0a152007-07-11 09:23:41 +0200285 list_del(&dev->list);
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800286 goto free;
Ville Tervo8de0a152007-07-11 09:23:41 +0200287 }
288
Marcel Holtmanndae6a0f2007-10-20 14:52:38 +0200289 dev_set_drvdata(dev->tty_dev, dev);
290
291 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
292 BT_ERR("Failed to create address attribute");
293
294 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
295 BT_ERR("Failed to create channel attribute");
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return dev->id;
Ilpo Järvinen037322a2008-12-14 23:18:00 -0800298
299free:
300 kfree(dev);
301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static void rfcomm_dev_del(struct rfcomm_dev *dev)
305{
Jiri Slabyf997a012012-04-02 13:54:53 +0200306 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 BT_DBG("dev %p", dev);
308
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100309 BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags));
310
Jiri Slabyf997a012012-04-02 13:54:53 +0200311 spin_lock_irqsave(&dev->port.lock, flags);
312 if (dev->port.count > 0) {
313 spin_unlock_irqrestore(&dev->port.lock, flags);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100314 return;
Jiri Slabyf997a012012-04-02 13:54:53 +0200315 }
316 spin_unlock_irqrestore(&dev->port.lock, flags);
Dave Youngf9513752008-01-10 22:22:52 -0800317
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200318 spin_lock(&rfcomm_dev_lock);
Dave Youngf9513752008-01-10 22:22:52 -0800319 list_del_init(&dev->list);
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200320 spin_unlock(&rfcomm_dev_lock);
Dave Youngf9513752008-01-10 22:22:52 -0800321
Jiri Slaby67054012012-04-02 13:54:51 +0200322 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/* ---- Send buffer ---- */
326static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
327{
328 /* We can't let it be zero, because we don't get a callback
329 when tx_credits becomes nonzero, hence we'd never wake up */
330 return dlc->mtu * (dlc->tx_credits?:1);
331}
332
333static void rfcomm_wfree(struct sk_buff *skb)
334{
335 struct rfcomm_dev *dev = (void *) skb->sk;
Jiri Slabyb2c4be32012-04-02 13:54:52 +0200336 struct tty_struct *tty = dev->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 atomic_sub(skb->truesize, &dev->wmem_alloc);
Jiri Slabyb2c4be32012-04-02 13:54:52 +0200338 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags) && tty)
339 tty_wakeup(tty);
Jiri Slaby67054012012-04-02 13:54:51 +0200340 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300343static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Jiri Slaby67054012012-04-02 13:54:51 +0200345 tty_port_get(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 atomic_add(skb->truesize, &dev->wmem_alloc);
347 skb->sk = (void *) dev;
348 skb->destructor = rfcomm_wfree;
349}
350
Al Virodd0fc662005-10-07 07:46:04 +0100351static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
354 struct sk_buff *skb = alloc_skb(size, priority);
355 if (skb) {
356 rfcomm_set_owner_w(skb, dev);
357 return skb;
358 }
359 }
360 return NULL;
361}
362
363/* ---- Device IOCTLs ---- */
364
365#define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
366
367static int rfcomm_create_dev(struct sock *sk, void __user *arg)
368{
369 struct rfcomm_dev_req req;
370 struct rfcomm_dlc *dlc;
371 int id;
372
373 if (copy_from_user(&req, arg, sizeof(req)))
374 return -EFAULT;
375
Ville Tervo8de0a152007-07-11 09:23:41 +0200376 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
379 return -EPERM;
380
381 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
382 /* Socket must be connected */
383 if (sk->sk_state != BT_CONNECTED)
384 return -EBADFD;
385
386 dlc = rfcomm_pi(sk)->dlc;
387 rfcomm_dlc_hold(dlc);
388 } else {
389 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
390 if (!dlc)
391 return -ENOMEM;
392 }
393
394 id = rfcomm_dev_add(&req, dlc);
395 if (id < 0) {
396 rfcomm_dlc_put(dlc);
397 return id;
398 }
399
400 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
401 /* DLC is now used by device.
402 * Socket must be disconnected */
403 sk->sk_state = BT_CLOSED;
404 }
405
406 return id;
407}
408
409static int rfcomm_release_dev(void __user *arg)
410{
411 struct rfcomm_dev_req req;
412 struct rfcomm_dev *dev;
413
414 if (copy_from_user(&req, arg, sizeof(req)))
415 return -EFAULT;
416
Ville Tervo8de0a152007-07-11 09:23:41 +0200417 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200419 dev = rfcomm_dev_get(req.dev_id);
420 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -ENODEV;
422
423 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
Jiri Slaby67054012012-04-02 13:54:51 +0200424 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -EPERM;
426 }
427
428 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
429 rfcomm_dlc_close(dev->dlc, 0);
430
Mikko Rapeli84950cf2007-07-11 09:18:15 +0200431 /* Shut down TTY synchronously before freeing rfcomm_dev */
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200432 if (dev->port.tty)
433 tty_vhangup(dev->port.tty);
Mikko Rapeli84950cf2007-07-11 09:18:15 +0200434
Dave Young93d80742008-02-05 03:12:06 -0800435 if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
436 rfcomm_dev_del(dev);
Jiri Slaby67054012012-04-02 13:54:51 +0200437 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
441static int rfcomm_get_dev_list(void __user *arg)
442{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200443 struct rfcomm_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct rfcomm_dev_list_req *dl;
445 struct rfcomm_dev_info *di;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int n = 0, size, err;
447 u16 dev_num;
448
449 BT_DBG("");
450
451 if (get_user(dev_num, (u16 __user *) arg))
452 return -EFAULT;
453
454 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
455 return -EINVAL;
456
457 size = sizeof(*dl) + dev_num * sizeof(*di);
458
Mathias Krausef9432c52012-08-15 11:31:49 +0000459 dl = kzalloc(size, GFP_KERNEL);
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200460 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -ENOMEM;
462
463 di = dl->dev_info;
464
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200465 spin_lock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200467 list_for_each_entry(dev, &rfcomm_dev_list, list) {
Ville Tervo8de0a152007-07-11 09:23:41 +0200468 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
469 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 (di + n)->id = dev->id;
471 (di + n)->flags = dev->flags;
472 (di + n)->state = dev->dlc->state;
473 (di + n)->channel = dev->channel;
474 bacpy(&(di + n)->src, &dev->src);
475 bacpy(&(di + n)->dst, &dev->dst);
476 if (++n >= dev_num)
477 break;
478 }
479
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200480 spin_unlock(&rfcomm_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 dl->dev_num = n;
483 size = sizeof(*dl) + n * sizeof(*di);
484
485 err = copy_to_user(arg, dl, size);
486 kfree(dl);
487
488 return err ? -EFAULT : 0;
489}
490
491static int rfcomm_get_dev_info(void __user *arg)
492{
493 struct rfcomm_dev *dev;
494 struct rfcomm_dev_info di;
495 int err = 0;
496
497 BT_DBG("");
498
499 if (copy_from_user(&di, arg, sizeof(di)))
500 return -EFAULT;
501
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200502 dev = rfcomm_dev_get(di.id);
503 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENODEV;
505
506 di.flags = dev->flags;
507 di.channel = dev->channel;
508 di.state = dev->dlc->state;
509 bacpy(&di.src, &dev->src);
510 bacpy(&di.dst, &dev->dst);
511
512 if (copy_to_user(arg, &di, sizeof(di)))
513 err = -EFAULT;
514
Jiri Slaby67054012012-04-02 13:54:51 +0200515 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return err;
517}
518
519int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
520{
521 BT_DBG("cmd %d arg %p", cmd, arg);
522
523 switch (cmd) {
524 case RFCOMMCREATEDEV:
525 return rfcomm_create_dev(sk, arg);
526
527 case RFCOMMRELEASEDEV:
528 return rfcomm_release_dev(arg);
529
530 case RFCOMMGETDEVLIST:
531 return rfcomm_get_dev_list(arg);
532
533 case RFCOMMGETDEVINFO:
534 return rfcomm_get_dev_info(arg);
535 }
536
537 return -EINVAL;
538}
539
540/* ---- DLC callbacks ---- */
541static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
542{
543 struct rfcomm_dev *dev = dlc->owner;
544 struct tty_struct *tty;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900545
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200546 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 kfree_skb(skb);
548 return;
549 }
550
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200551 tty = dev->port.tty;
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200552 if (!tty || !skb_queue_empty(&dev->pending)) {
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200553 skb_queue_tail(&dev->pending, skb);
554 return;
555 }
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
558
Paul Fulghum817d6d32006-06-28 04:26:47 -0700559 tty_insert_flip_string(tty, skb->data, skb->len);
560 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 kfree_skb(skb);
563}
564
565static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
566{
567 struct rfcomm_dev *dev = dlc->owner;
568 if (!dev)
569 return;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
572
573 dev->err = err;
574 wake_up_interruptible(&dev->wait);
575
576 if (dlc->state == BT_CLOSED) {
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200577 if (!dev->port.tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
Dave Young537d59a2008-06-01 23:50:52 -0700579 /* Drop DLC lock here to avoid deadlock
580 * 1. rfcomm_dev_get will take rfcomm_dev_lock
581 * but in rfcomm_dev_add there's lock order:
582 * rfcomm_dev_lock -> dlc lock
Jiri Slaby67054012012-04-02 13:54:51 +0200583 * 2. tty_port_put will deadlock if it's
Dave Young537d59a2008-06-01 23:50:52 -0700584 * the last reference
585 */
586 rfcomm_dlc_unlock(dlc);
587 if (rfcomm_dev_get(dev->id) == NULL) {
588 rfcomm_dlc_lock(dlc);
Marcel Holtmann77f2a452007-05-05 00:36:10 +0200589 return;
Dave Young537d59a2008-06-01 23:50:52 -0700590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Marcel Holtmann77f2a452007-05-05 00:36:10 +0200592 rfcomm_dev_del(dev);
Jiri Slaby67054012012-04-02 13:54:51 +0200593 tty_port_put(&dev->port);
Dave Young537d59a2008-06-01 23:50:52 -0700594 rfcomm_dlc_lock(dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900596 } else
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200597 tty_hangup(dev->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599}
600
601static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
602{
603 struct rfcomm_dev *dev = dlc->owner;
604 if (!dev)
605 return;
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
608
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700609 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200610 if (dev->port.tty && !C_CLOCAL(dev->port.tty))
611 tty_hangup(dev->port.tty);
Timo Teräs7b9eb9e2005-08-09 20:28:21 -0700612 }
613
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900614 dev->modem_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
616 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
617 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
618 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
619}
620
621/* ---- TTY functions ---- */
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200622static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
623{
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200624 struct tty_struct *tty = dev->port.tty;
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200625 struct sk_buff *skb;
626 int inserted = 0;
627
628 if (!tty)
629 return;
630
631 BT_DBG("dev %p tty %p", dev, tty);
632
633 rfcomm_dlc_lock(dev->dlc);
634
635 while ((skb = skb_dequeue(&dev->pending))) {
636 inserted += tty_insert_flip_string(tty, skb->data, skb->len);
637 kfree_skb(skb);
638 }
639
640 rfcomm_dlc_unlock(dev->dlc);
641
642 if (inserted > 0)
643 tty_flip_buffer_push(tty);
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
647{
648 DECLARE_WAITQUEUE(wait, current);
649 struct rfcomm_dev *dev;
650 struct rfcomm_dlc *dlc;
Jiri Slabyf997a012012-04-02 13:54:53 +0200651 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int err, id;
653
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900654 id = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 BT_DBG("tty %p id %d", tty, id);
657
658 /* We don't leak this refcount. For reasons which are not entirely
659 clear, the TTY layer will call our ->close() method even if the
660 open fails. We decrease the refcount there, and decreasing it
661 here too would cause breakage. */
662 dev = rfcomm_dev_get(id);
663 if (!dev)
664 return -ENODEV;
665
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100666 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst),
Jiri Slabyf997a012012-04-02 13:54:53 +0200667 dev->channel, dev->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Jiri Slabyf997a012012-04-02 13:54:53 +0200669 spin_lock_irqsave(&dev->port.lock, flags);
670 if (++dev->port.count > 1) {
671 spin_unlock_irqrestore(&dev->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
Jiri Slabyf997a012012-04-02 13:54:53 +0200673 }
674 spin_unlock_irqrestore(&dev->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 dlc = dev->dlc;
677
678 /* Attach TTY and open DLC */
679
680 rfcomm_dlc_lock(dlc);
681 tty->driver_data = dev;
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200682 dev->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 rfcomm_dlc_unlock(dlc);
684 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
685
686 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
687 if (err < 0)
688 return err;
689
690 /* Wait for DLC to connect */
691 add_wait_queue(&dev->wait, &wait);
692 while (1) {
693 set_current_state(TASK_INTERRUPTIBLE);
694
695 if (dlc->state == BT_CLOSED) {
696 err = -dev->err;
697 break;
698 }
699
700 if (dlc->state == BT_CONNECTED)
701 break;
702
703 if (signal_pending(current)) {
704 err = -EINTR;
705 break;
706 }
707
Alan Cox89c8d912012-08-08 16:30:13 +0100708 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +0100710 tty_lock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 set_current_state(TASK_RUNNING);
713 remove_wait_queue(&dev->wait, &wait);
714
Marcel Holtmannc1a33132007-02-17 23:58:57 +0100715 if (err == 0)
Cornelia Huckffa6a702009-03-04 12:44:00 +0100716 device_move(dev->tty_dev, rfcomm_get_device(dev),
717 DPM_ORDER_DEV_AFTER_PARENT);
Marcel Holtmannc1a33132007-02-17 23:58:57 +0100718
Marcel Holtmanna0c22f22008-07-14 20:13:52 +0200719 rfcomm_tty_copy_pending(dev);
720
721 rfcomm_dlc_unthrottle(dev->dlc);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return err;
724}
725
726static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
727{
728 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Jiri Slabyf997a012012-04-02 13:54:53 +0200729 unsigned long flags;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!dev)
732 return;
733
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100734 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
Jiri Slabyf997a012012-04-02 13:54:53 +0200735 dev->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jiri Slabyf997a012012-04-02 13:54:53 +0200737 spin_lock_irqsave(&dev->port.lock, flags);
738 if (!--dev->port.count) {
739 spin_unlock_irqrestore(&dev->port.lock, flags);
Dave Youngacea6852008-01-21 22:35:21 -0800740 if (dev->tty_dev->parent)
Cornelia Huckffa6a702009-03-04 12:44:00 +0100741 device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
Marcel Holtmannc1a33132007-02-17 23:58:57 +0100742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* Close DLC and dettach TTY */
744 rfcomm_dlc_close(dev->dlc, 0);
745
746 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 rfcomm_dlc_lock(dev->dlc);
749 tty->driver_data = NULL;
Jiri Slabyf60db8c2012-04-02 13:54:50 +0200750 dev->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rfcomm_dlc_unlock(dev->dlc);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100752
753 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) {
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200754 spin_lock(&rfcomm_dev_lock);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100755 list_del_init(&dev->list);
Gustavo F. Padovan393432c2011-12-27 15:28:45 -0200756 spin_unlock(&rfcomm_dev_lock);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100757
Jiri Slaby67054012012-04-02 13:54:51 +0200758 tty_port_put(&dev->port);
Marcel Holtmann9a5df922008-11-30 12:17:29 +0100759 }
Jiri Slabyf997a012012-04-02 13:54:53 +0200760 } else
761 spin_unlock_irqrestore(&dev->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Jiri Slaby67054012012-04-02 13:54:51 +0200763 tty_port_put(&dev->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
767{
768 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
769 struct rfcomm_dlc *dlc = dev->dlc;
770 struct sk_buff *skb;
771 int err = 0, sent = 0, size;
772
773 BT_DBG("tty %p count %d", tty, count);
774
775 while (count) {
776 size = min_t(uint, count, dlc->mtu);
777
778 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!skb)
781 break;
782
783 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
784
785 memcpy(skb_put(skb, size), buf + sent, size);
786
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200787 err = rfcomm_dlc_send(dlc, skb);
788 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 kfree_skb(skb);
790 break;
791 }
792
793 sent += size;
794 count -= size;
795 }
796
797 return sent ? sent : err;
798}
799
800static int rfcomm_tty_write_room(struct tty_struct *tty)
801{
802 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
803 int room;
804
805 BT_DBG("tty %p", tty);
806
Marcel Holtmannb6e557f2007-01-08 02:16:27 +0100807 if (!dev || !dev->dlc)
808 return 0;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
811 if (room < 0)
812 room = 0;
Marcel Holtmannb6e557f2007-01-08 02:16:27 +0100813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return room;
815}
816
Alan Cox6caa76b2011-02-14 16:27:22 +0000817static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
820
821 switch (cmd) {
822 case TCGETS:
823 BT_DBG("TCGETS is not supported");
824 return -ENOIOCTLCMD;
825
826 case TCSETS:
827 BT_DBG("TCSETS is not supported");
828 return -ENOIOCTLCMD;
829
830 case TIOCMIWAIT:
831 BT_DBG("TIOCMIWAIT");
832 break;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 case TIOCGSERIAL:
835 BT_ERR("TIOCGSERIAL is not supported");
836 return -ENOIOCTLCMD;
837
838 case TIOCSSERIAL:
839 BT_ERR("TIOCSSERIAL is not supported");
840 return -ENOIOCTLCMD;
841
842 case TIOCSERGSTRUCT:
843 BT_ERR("TIOCSERGSTRUCT is not supported");
844 return -ENOIOCTLCMD;
845
846 case TIOCSERGETLSR:
847 BT_ERR("TIOCSERGETLSR is not supported");
848 return -ENOIOCTLCMD;
849
850 case TIOCSERCONFIG:
851 BT_ERR("TIOCSERCONFIG is not supported");
852 return -ENOIOCTLCMD;
853
854 default:
855 return -ENOIOCTLCMD; /* ioctls which we must ignore */
856
857 }
858
859 return -ENOIOCTLCMD;
860}
861
Alan Cox606d0992006-12-08 02:38:45 -0800862static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Alan Coxadc8d742012-07-14 15:31:47 +0100864 struct ktermios *new = &tty->termios;
J. Suter3a5e9032005-08-09 20:28:46 -0700865 int old_baud_rate = tty_termios_baud_rate(old);
866 int new_baud_rate = tty_termios_baud_rate(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
J. Suter3a5e9032005-08-09 20:28:46 -0700868 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
869 u16 changes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
J. Suter3a5e9032005-08-09 20:28:46 -0700871 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
872
873 BT_DBG("tty %p termios %p", tty, old);
874
Marcel Holtmannff2d3672006-11-18 22:14:42 +0100875 if (!dev || !dev->dlc || !dev->dlc->session)
Marcel Holtmanncb19d9e2006-10-15 17:31:10 +0200876 return;
877
J. Suter3a5e9032005-08-09 20:28:46 -0700878 /* Handle turning off CRTSCTS */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900879 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
J. Suter3a5e9032005-08-09 20:28:46 -0700880 BT_DBG("Turning off CRTSCTS unsupported");
881
882 /* Parity on/off and when on, odd/even */
883 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200884 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
J. Suter3a5e9032005-08-09 20:28:46 -0700885 changes |= RFCOMM_RPN_PM_PARITY;
886 BT_DBG("Parity change detected.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
J. Suter3a5e9032005-08-09 20:28:46 -0700888
889 /* Mark and space parity are not supported! */
890 if (new->c_cflag & PARENB) {
891 if (new->c_cflag & PARODD) {
892 BT_DBG("Parity is ODD");
893 parity = RFCOMM_RPN_PARITY_ODD;
894 } else {
895 BT_DBG("Parity is EVEN");
896 parity = RFCOMM_RPN_PARITY_EVEN;
897 }
898 } else {
899 BT_DBG("Parity is OFF");
900 parity = RFCOMM_RPN_PARITY_NONE;
901 }
902
903 /* Setting the x_on / x_off characters */
904 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
905 BT_DBG("XOFF custom");
906 x_on = new->c_cc[VSTOP];
907 changes |= RFCOMM_RPN_PM_XON;
908 } else {
909 BT_DBG("XOFF default");
910 x_on = RFCOMM_RPN_XON_CHAR;
911 }
912
913 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
914 BT_DBG("XON custom");
915 x_off = new->c_cc[VSTART];
916 changes |= RFCOMM_RPN_PM_XOFF;
917 } else {
918 BT_DBG("XON default");
919 x_off = RFCOMM_RPN_XOFF_CHAR;
920 }
921
922 /* Handle setting of stop bits */
923 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
924 changes |= RFCOMM_RPN_PM_STOP;
925
926 /* POSIX does not support 1.5 stop bits and RFCOMM does not
927 * support 2 stop bits. So a request for 2 stop bits gets
928 * translated to 1.5 stop bits */
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200929 if (new->c_cflag & CSTOPB)
J. Suter3a5e9032005-08-09 20:28:46 -0700930 stop_bits = RFCOMM_RPN_STOP_15;
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200931 else
J. Suter3a5e9032005-08-09 20:28:46 -0700932 stop_bits = RFCOMM_RPN_STOP_1;
J. Suter3a5e9032005-08-09 20:28:46 -0700933
934 /* Handle number of data bits [5-8] */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900935 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
J. Suter3a5e9032005-08-09 20:28:46 -0700936 changes |= RFCOMM_RPN_PM_DATA;
937
938 switch (new->c_cflag & CSIZE) {
939 case CS5:
940 data_bits = RFCOMM_RPN_DATA_5;
941 break;
942 case CS6:
943 data_bits = RFCOMM_RPN_DATA_6;
944 break;
945 case CS7:
946 data_bits = RFCOMM_RPN_DATA_7;
947 break;
948 case CS8:
949 data_bits = RFCOMM_RPN_DATA_8;
950 break;
951 default:
952 data_bits = RFCOMM_RPN_DATA_8;
953 break;
954 }
955
956 /* Handle baudrate settings */
957 if (old_baud_rate != new_baud_rate)
958 changes |= RFCOMM_RPN_PM_BITRATE;
959
960 switch (new_baud_rate) {
961 case 2400:
962 baud = RFCOMM_RPN_BR_2400;
963 break;
964 case 4800:
965 baud = RFCOMM_RPN_BR_4800;
966 break;
967 case 7200:
968 baud = RFCOMM_RPN_BR_7200;
969 break;
970 case 9600:
971 baud = RFCOMM_RPN_BR_9600;
972 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900973 case 19200:
J. Suter3a5e9032005-08-09 20:28:46 -0700974 baud = RFCOMM_RPN_BR_19200;
975 break;
976 case 38400:
977 baud = RFCOMM_RPN_BR_38400;
978 break;
979 case 57600:
980 baud = RFCOMM_RPN_BR_57600;
981 break;
982 case 115200:
983 baud = RFCOMM_RPN_BR_115200;
984 break;
985 case 230400:
986 baud = RFCOMM_RPN_BR_230400;
987 break;
988 default:
989 /* 9600 is standard accordinag to the RFCOMM specification */
990 baud = RFCOMM_RPN_BR_9600;
991 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900992
J. Suter3a5e9032005-08-09 20:28:46 -0700993 }
994
995 if (changes)
996 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
997 data_bits, stop_bits, parity,
998 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static void rfcomm_tty_throttle(struct tty_struct *tty)
1002{
1003 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1004
1005 BT_DBG("tty %p dev %p", tty, dev);
J. Suter3a5e9032005-08-09 20:28:46 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 rfcomm_dlc_throttle(dev->dlc);
1008}
1009
1010static void rfcomm_tty_unthrottle(struct tty_struct *tty)
1011{
1012 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1013
1014 BT_DBG("tty %p dev %p", tty, dev);
J. Suter3a5e9032005-08-09 20:28:46 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 rfcomm_dlc_unthrottle(dev->dlc);
1017}
1018
1019static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1020{
1021 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 BT_DBG("tty %p dev %p", tty, dev);
1024
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001025 if (!dev || !dev->dlc)
1026 return 0;
1027
1028 if (!skb_queue_empty(&dev->dlc->tx_queue))
1029 return dev->dlc->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return 0;
1032}
1033
1034static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1035{
1036 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 BT_DBG("tty %p dev %p", tty, dev);
1039
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001040 if (!dev || !dev->dlc)
1041 return;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 skb_queue_purge(&dev->dlc->tx_queue);
Alan Coxa352def2008-07-16 21:53:12 +01001044 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1048{
1049 BT_DBG("tty %p ch %c", tty, ch);
1050}
1051
1052static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1053{
1054 BT_DBG("tty %p timeout %d", tty, timeout);
1055}
1056
1057static void rfcomm_tty_hangup(struct tty_struct *tty)
1058{
1059 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 BT_DBG("tty %p dev %p", tty, dev);
1062
Marcel Holtmannb6e557f2007-01-08 02:16:27 +01001063 if (!dev)
1064 return;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 rfcomm_tty_flush_buffer(tty);
1067
Marcel Holtmann77f2a452007-05-05 00:36:10 +02001068 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
1069 if (rfcomm_dev_get(dev->id) == NULL)
1070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 rfcomm_dev_del(dev);
Jiri Slaby67054012012-04-02 13:54:51 +02001072 tty_port_put(&dev->port);
Marcel Holtmann77f2a452007-05-05 00:36:10 +02001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Alan Cox60b33c12011-02-14 16:26:14 +00001076static int rfcomm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001078 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 BT_DBG("tty %p dev %p", tty, dev);
1081
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001082 return dev->modem_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Alan Cox20b9d172011-02-14 16:26:50 +00001085static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
J. Suter3a5e9032005-08-09 20:28:46 -07001087 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1088 struct rfcomm_dlc *dlc = dev->dlc;
1089 u8 v24_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1092
J. Suter3a5e9032005-08-09 20:28:46 -07001093 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
J. Suter3a5e9032005-08-09 20:28:46 -07001095 if (set & TIOCM_DSR || set & TIOCM_DTR)
1096 v24_sig |= RFCOMM_V24_RTC;
1097 if (set & TIOCM_RTS || set & TIOCM_CTS)
1098 v24_sig |= RFCOMM_V24_RTR;
1099 if (set & TIOCM_RI)
1100 v24_sig |= RFCOMM_V24_IC;
1101 if (set & TIOCM_CD)
1102 v24_sig |= RFCOMM_V24_DV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
J. Suter3a5e9032005-08-09 20:28:46 -07001104 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1105 v24_sig &= ~RFCOMM_V24_RTC;
1106 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1107 v24_sig &= ~RFCOMM_V24_RTR;
1108 if (clear & TIOCM_RI)
1109 v24_sig &= ~RFCOMM_V24_IC;
1110 if (clear & TIOCM_CD)
1111 v24_sig &= ~RFCOMM_V24_DV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
J. Suter3a5e9032005-08-09 20:28:46 -07001113 rfcomm_dlc_set_modem_status(dlc, v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
J. Suter3a5e9032005-08-09 20:28:46 -07001115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/* ---- TTY structure ---- */
1119
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001120static const struct tty_operations rfcomm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 .open = rfcomm_tty_open,
1122 .close = rfcomm_tty_close,
1123 .write = rfcomm_tty_write,
1124 .write_room = rfcomm_tty_write_room,
1125 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1126 .flush_buffer = rfcomm_tty_flush_buffer,
1127 .ioctl = rfcomm_tty_ioctl,
1128 .throttle = rfcomm_tty_throttle,
1129 .unthrottle = rfcomm_tty_unthrottle,
1130 .set_termios = rfcomm_tty_set_termios,
1131 .send_xchar = rfcomm_tty_send_xchar,
1132 .hangup = rfcomm_tty_hangup,
1133 .wait_until_sent = rfcomm_tty_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 .tiocmget = rfcomm_tty_tiocmget,
1135 .tiocmset = rfcomm_tty_tiocmset,
1136};
1137
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001138int __init rfcomm_init_ttys(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
David Herrmann5ada9912011-10-24 15:30:57 +02001140 int error;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1143 if (!rfcomm_tty_driver)
David Herrmann5ada9912011-10-24 15:30:57 +02001144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 rfcomm_tty_driver->driver_name = "rfcomm";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 rfcomm_tty_driver->name = "rfcomm";
1148 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1149 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1150 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1151 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001152 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 rfcomm_tty_driver->init_termios = tty_std_termios;
1154 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Marcel Holtmannca37bdd2008-07-14 20:13:52 +02001155 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1157
David Herrmann5ada9912011-10-24 15:30:57 +02001158 error = tty_register_driver(rfcomm_tty_driver);
1159 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 BT_ERR("Can't register RFCOMM TTY driver");
1161 put_tty_driver(rfcomm_tty_driver);
David Herrmann5ada9912011-10-24 15:30:57 +02001162 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164
1165 BT_INFO("RFCOMM TTY layer initialized");
1166
1167 return 0;
1168}
1169
Gustavo F. Padovan28e95092010-07-31 19:57:05 -03001170void rfcomm_cleanup_ttys(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 tty_unregister_driver(rfcomm_tty_driver);
1173 put_tty_driver(rfcomm_tty_driver);
1174}