import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / net / bluetooth / rfcomm / tty.c
1 /* 
2    RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3    Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4    Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License version 2 as
8    published by the Free Software Foundation;
9
10    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
15    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
16    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
17    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
20    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
21    SOFTWARE IS DISCLAIMED.
22 */
23
24 /*
25  * RFCOMM TTY.
26  *
27  * $Id: tty.c,v 1.1.1.1 2005/04/11 02:51:12 jack Exp $
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32
33 #include <linux/tty.h>
34 #include <linux/tty_driver.h>
35 #include <linux/tty_flip.h>
36
37 #include <linux/slab.h>
38 #include <linux/skbuff.h>
39
40 #include <net/bluetooth/bluetooth.h>
41 #include <net/bluetooth/rfcomm.h>
42
43 #ifndef CONFIG_BLUEZ_RFCOMM_DEBUG
44 #undef  BT_DBG
45 #define BT_DBG(D...)
46 #endif
47
48 #define RFCOMM_TTY_MAGIC 0x6d02         /* magic number for rfcomm struct */
49 #define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV /* whole lotta rfcomm devices */
50 #define RFCOMM_TTY_MAJOR 216            /* device node major id of the usb/bluetooth.c driver */
51 #define RFCOMM_TTY_MINOR 0
52
53 struct rfcomm_dev {
54         struct list_head        list;
55         atomic_t                refcnt;
56
57         char                    name[12];
58         int                     id;
59         unsigned long           flags;
60         int                     opened;
61         int                     err;
62
63         bdaddr_t                src;
64         bdaddr_t                dst;
65         u8                      channel;
66
67         uint                    modem_status;
68
69         struct rfcomm_dlc       *dlc;
70         struct tty_struct       *tty;
71         wait_queue_head_t       wait;
72         struct tasklet_struct   wakeup_task;
73
74         atomic_t                wmem_alloc;
75         unsigned int            sndbuf;
76 };
77
78 static LIST_HEAD(rfcomm_dev_list);
79 static rwlock_t rfcomm_dev_lock = RW_LOCK_UNLOCKED;
80
81 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
82 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
83 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
84
85 static void rfcomm_tty_wakeup(unsigned long arg);
86
87 /* ---- Device functions ---- */
88 static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
89 {
90         struct rfcomm_dlc *dlc = dev->dlc;
91
92         BT_DBG("dev %p dlc %p", dev, dlc);
93
94         rfcomm_dlc_lock(dlc);
95         /* Detach DLC if it's owned by this dev */
96         if (dlc->owner == dev)
97                 dlc->owner = NULL;
98         rfcomm_dlc_unlock(dlc);
99
100         rfcomm_dlc_put(dlc);
101         kfree(dev);
102
103         MOD_DEC_USE_COUNT;
104 }
105
106 static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
107 {
108         atomic_inc(&dev->refcnt);
109 }
110
111 static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
112 {
113         if (atomic_dec_and_test(&dev->refcnt))
114                 rfcomm_dev_destruct(dev);
115 }
116
117 static struct rfcomm_dev *__rfcomm_dev_get(int id)
118 {
119         struct rfcomm_dev *dev;
120         struct list_head  *p;
121
122         list_for_each(p, &rfcomm_dev_list) {
123                 dev = list_entry(p, struct rfcomm_dev, list);
124                 if (dev->id == id)
125                         return dev;
126         }
127
128         return NULL;
129 }
130
131 static inline struct rfcomm_dev *rfcomm_dev_get(int id)
132 {
133         struct rfcomm_dev *dev;
134
135         read_lock(&rfcomm_dev_lock);
136         dev = __rfcomm_dev_get(id);
137         read_unlock(&rfcomm_dev_lock);
138
139         if (dev) rfcomm_dev_hold(dev);
140         return dev;
141 }
142
143 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
144 {
145         struct rfcomm_dev *dev;
146         struct list_head *head = &rfcomm_dev_list, *p;
147         int err = 0;
148
149         BT_DBG("id %d channel %d", req->dev_id, req->channel);
150         
151         dev = kmalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
152         if (!dev)
153                 return -ENOMEM;
154         memset(dev, 0, sizeof(struct rfcomm_dev));
155
156         write_lock_bh(&rfcomm_dev_lock);
157
158         if (req->dev_id < 0) {
159                 dev->id = 0;
160
161                 list_for_each(p, &rfcomm_dev_list) {
162                         if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
163                                 break;
164
165                         dev->id++;
166                         head = p;
167                 }
168         } else {
169                 dev->id = req->dev_id;
170
171                 list_for_each(p, &rfcomm_dev_list) {
172                         struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
173
174                         if (entry->id == dev->id) {
175                                 err = -EADDRINUSE;
176                                 goto out;
177                         }
178
179                         if (entry->id > dev->id - 1)
180                                 break;
181
182                         head = p;
183                 }
184         }
185
186         if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
187                 err = -ENFILE;
188                 goto out;
189         }
190
191         sprintf(dev->name, "rfcomm%d", dev->id);
192
193         list_add(&dev->list, head);
194         atomic_set(&dev->refcnt, 1);
195
196         bacpy(&dev->src, &req->src);
197         bacpy(&dev->dst, &req->dst);
198         dev->channel = req->channel;
199
200         dev->flags = req->flags & 
201                 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
202
203         dev->sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
204
205         init_waitqueue_head(&dev->wait);
206         tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
207
208         rfcomm_dlc_lock(dlc);
209         dlc->data_ready   = rfcomm_dev_data_ready;
210         dlc->state_change = rfcomm_dev_state_change;
211         dlc->modem_status = rfcomm_dev_modem_status;
212
213         dlc->owner = dev;
214         dev->dlc   = dlc;
215         rfcomm_dlc_unlock(dlc);
216
217         MOD_INC_USE_COUNT;
218         
219 out:
220         write_unlock_bh(&rfcomm_dev_lock);
221
222         if (err) {
223                 kfree(dev);
224                 return err;
225         } else
226                 return dev->id;
227 }
228
229 static void rfcomm_dev_del(struct rfcomm_dev *dev)
230 {
231         BT_DBG("dev %p", dev);
232
233         write_lock_bh(&rfcomm_dev_lock);
234         list_del_init(&dev->list);
235         write_unlock_bh(&rfcomm_dev_lock);
236
237         rfcomm_dev_put(dev);
238 }
239
240 /* ---- Send buffer ---- */
241 static void rfcomm_wfree(struct sk_buff *skb)
242 {
243         struct rfcomm_dev *dev = (void *) skb->sk;
244         atomic_sub(skb->truesize, &dev->wmem_alloc);
245         if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
246                 tasklet_schedule(&dev->wakeup_task);
247         rfcomm_dev_put(dev);
248 }
249
250 static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
251 {
252         rfcomm_dev_hold(dev);
253         atomic_add(skb->truesize, &dev->wmem_alloc);
254         skb->sk = (void *) dev;
255         skb->destructor = rfcomm_wfree;
256 }
257
258 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, int priority)
259 {
260         if (atomic_read(&dev->wmem_alloc) < dev->sndbuf) {
261                 struct sk_buff *skb = alloc_skb(size, priority);
262                 if (skb) {
263                         rfcomm_set_owner_w(skb, dev);
264                         return skb;
265                 }
266         }
267         return NULL;
268 }
269
270 /* ---- Device IOCTLs ---- */
271
272 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
273
274 static int rfcomm_create_dev(struct sock *sk, unsigned long arg)
275 {
276         struct rfcomm_dev_req req;
277         struct rfcomm_dlc *dlc;
278         int id;
279
280         if (copy_from_user(&req, (void *) arg, sizeof(req)))
281                 return -EFAULT;
282
283         BT_DBG("sk %p dev_id %id flags 0x%x", sk, req.dev_id, req.flags);
284
285         if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
286                 return -EPERM;
287
288         if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
289                 /* Socket must be connected */
290                 if (sk->state != BT_CONNECTED)
291                         return -EBADFD;
292
293                 dlc = rfcomm_pi(sk)->dlc;
294                 rfcomm_dlc_hold(dlc);
295         } else {
296                 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
297                 if (!dlc)
298                         return -ENOMEM;
299         }
300
301         id = rfcomm_dev_add(&req, dlc);
302         if (id < 0) {
303                 rfcomm_dlc_put(dlc);
304                 return id;
305         }
306
307         if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
308                 /* DLC is now used by device.
309                  * Socket must be disconnected */
310                 sk->state = BT_CLOSED;
311         }
312
313         return id;
314 }
315
316 static int rfcomm_release_dev(unsigned long arg)
317 {
318         struct rfcomm_dev_req req;
319         struct rfcomm_dev *dev;
320
321         if (copy_from_user(&req, (void *) arg, sizeof(req)))
322                 return -EFAULT;
323
324         BT_DBG("dev_id %id flags 0x%x", req.dev_id, req.flags);
325
326         if (!capable(CAP_NET_ADMIN))
327                 return -EPERM;
328
329         if (!(dev = rfcomm_dev_get(req.dev_id)))
330                 return -ENODEV;
331
332         if (req.flags & (1 << RFCOMM_HANGUP_NOW))
333                 rfcomm_dlc_close(dev->dlc, 0);
334
335         rfcomm_dev_del(dev);
336         rfcomm_dev_put(dev);
337         return 0;
338 }
339
340 static int rfcomm_get_dev_list(unsigned long arg)
341 {
342         struct rfcomm_dev_list_req *dl;
343         struct rfcomm_dev_info *di;
344         struct list_head *p;
345         int n = 0, size;
346         u16 dev_num;
347
348         BT_DBG("");
349
350         if (get_user(dev_num, (u16 *) arg))
351                 return -EFAULT;
352
353         if (!dev_num)
354                 return -EINVAL;
355
356         size = sizeof(*dl) + dev_num * sizeof(*di);
357
358         if (verify_area(VERIFY_WRITE, (void *)arg, size))
359                 return -EFAULT;
360
361         if (!(dl = kmalloc(size, GFP_KERNEL)))
362                 return -ENOMEM;
363
364         di = dl->dev_info;
365
366         read_lock_bh(&rfcomm_dev_lock);
367
368         list_for_each(p, &rfcomm_dev_list) {
369                 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
370                 (di + n)->id      = dev->id;
371                 (di + n)->flags   = dev->flags;
372                 (di + n)->state   = dev->dlc->state;
373                 (di + n)->channel = dev->channel;
374                 bacpy(&(di + n)->src, &dev->src);
375                 bacpy(&(di + n)->dst, &dev->dst);
376                 if (++n >= dev_num)
377                         break;
378         }
379
380         read_unlock_bh(&rfcomm_dev_lock);
381
382         dl->dev_num = n;
383         size = sizeof(*dl) + n * sizeof(*di);
384
385         copy_to_user((void *) arg, dl, size);
386         kfree(dl);
387         return 0;
388 }
389
390 static int rfcomm_get_dev_info(unsigned long arg)
391 {
392         struct rfcomm_dev *dev;
393         struct rfcomm_dev_info di;
394         int err = 0;
395
396         BT_DBG("");
397
398         if (copy_from_user(&di, (void *)arg, sizeof(di)))
399                 return -EFAULT;
400
401         if (!(dev = rfcomm_dev_get(di.id)))
402                 return -ENODEV;
403
404         di.flags   = dev->flags;
405         di.channel = dev->channel;
406         di.state   = dev->dlc->state;
407         bacpy(&di.src, &dev->src);
408         bacpy(&di.dst, &dev->dst);
409
410         if (copy_to_user((void *)arg, &di, sizeof(di)))
411                 err = -EFAULT;
412
413         rfcomm_dev_put(dev);
414         return err;
415 }
416
417 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
418 {
419         BT_DBG("cmd %d arg %ld", cmd, arg);
420
421         switch (cmd) {
422         case RFCOMMCREATEDEV:
423                 return rfcomm_create_dev(sk, arg);
424
425         case RFCOMMRELEASEDEV:
426                 return rfcomm_release_dev(arg);
427
428         case RFCOMMGETDEVLIST:
429                 return rfcomm_get_dev_list(arg);
430
431         case RFCOMMGETDEVINFO:
432                 return rfcomm_get_dev_info(arg);
433         }
434
435         return -EINVAL;
436 }
437
438 /* ---- DLC callbacks ---- */
439 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
440 {
441         struct rfcomm_dev *dev = dlc->owner;
442         struct tty_struct *tty;
443        
444         if (!dev || !(tty = dev->tty)) {
445                 kfree_skb(skb);
446                 return;
447         }
448
449         BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
450
451         if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
452                 register int i;
453                 for (i = 0; i < skb->len; i++) {
454                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
455                                 tty_flip_buffer_push(tty);
456
457                         tty_insert_flip_char(tty, skb->data[i], 0);
458                 }
459                 tty_flip_buffer_push(tty);
460         } else
461                 tty->ldisc.receive_buf(tty, skb->data, NULL, skb->len);
462
463         kfree_skb(skb);
464 }
465
466 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
467 {
468         struct rfcomm_dev *dev = dlc->owner;
469         if (!dev)
470                 return;
471         
472         BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
473
474         dev->err = err;
475         wake_up_interruptible(&dev->wait);
476
477         if (dlc->state == BT_CLOSED) {
478                 if (!dev->tty) {
479                         if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
480                                 rfcomm_dev_hold(dev);
481                                 rfcomm_dev_del(dev);
482
483                                 /* We have to drop DLC lock here, otherwise
484                                  * rfcomm_dev_put() will dead lock if it's the last refference */
485                                 rfcomm_dlc_unlock(dlc);
486                                 rfcomm_dev_put(dev);
487                                 rfcomm_dlc_lock(dlc);
488                         }
489                 } else 
490                         tty_hangup(dev->tty);
491         }
492 }
493
494 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
495 {
496         struct rfcomm_dev *dev = dlc->owner;
497         if (!dev)
498                 return;
499         
500         BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
501
502         dev->modem_status = 
503                 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
504                 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
505                 ((v24_sig & RFCOMM_V24_IC)  ? TIOCM_RI : 0) |
506                 ((v24_sig & RFCOMM_V24_DV)  ? TIOCM_CD : 0);
507 }
508
509 /* ---- TTY functions ---- */
510 static void rfcomm_tty_wakeup(unsigned long arg)
511 {
512         struct rfcomm_dev *dev = (void *) arg;
513         struct tty_struct *tty = dev->tty;
514         if (!tty)
515                 return;
516
517         BT_DBG("dev %p tty %p", dev, tty);
518
519         if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
520                 (tty->ldisc.write_wakeup)(tty);
521
522         wake_up_interruptible(&tty->write_wait);
523 #ifdef SERIAL_HAVE_POLL_WAIT
524         wake_up_interruptible(&tty->poll_wait);
525 #endif
526 }
527
528 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
529 {
530         DECLARE_WAITQUEUE(wait, current);
531         struct rfcomm_dev *dev;
532         struct rfcomm_dlc *dlc;
533         int err, id;
534
535         id = MINOR(tty->device) - tty->driver.minor_start;
536
537         BT_DBG("tty %p id %d", tty, id);
538
539         dev = rfcomm_dev_get(id);
540         if (!dev)
541                 return -ENODEV;
542
543         BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
544
545         if (dev->opened++ != 0)
546                 return 0;
547
548         dlc = dev->dlc;
549
550         /* Attach TTY and open DLC */
551
552         rfcomm_dlc_lock(dlc);
553         tty->driver_data = dev;
554         dev->tty = tty;
555         rfcomm_dlc_unlock(dlc);
556         set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
557
558         err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
559         if (err < 0)
560                 return err;
561
562         /* Wait for DLC to connect */
563         add_wait_queue(&dev->wait, &wait);
564         while (1) {
565                 set_current_state(TASK_INTERRUPTIBLE);
566
567                 if (dlc->state == BT_CLOSED) {
568                         err = -dev->err;
569                         break;
570                 }
571
572                 if (dlc->state == BT_CONNECTED)
573                         break;
574
575                 if (signal_pending(current)) {
576                         err = -EINTR;
577                         break;
578                 }
579
580                 schedule();
581         }
582         set_current_state(TASK_RUNNING);
583         remove_wait_queue(&dev->wait, &wait);
584
585         return err;
586 }
587
588 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
589 {
590         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
591         if (!dev)
592                 return;
593
594         BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
595
596         if (--dev->opened == 0) {
597                 /* Close DLC and dettach TTY */
598                 rfcomm_dlc_close(dev->dlc, 0);
599
600                 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
601                 tasklet_kill(&dev->wakeup_task);
602
603                 rfcomm_dlc_lock(dev->dlc);
604                 tty->driver_data = NULL;
605                 dev->tty = NULL;
606                 rfcomm_dlc_unlock(dev->dlc);
607         }
608
609         rfcomm_dev_put(dev);
610 }
611
612 static int rfcomm_tty_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
613 {
614         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
615         struct rfcomm_dlc *dlc = dev->dlc;
616         struct sk_buff *skb;
617         int err = 0, sent = 0, size;
618
619         BT_DBG("tty %p from_user %d count %d", tty, from_user, count);
620
621         while (count) {
622                 size = min_t(uint, count, dlc->mtu);
623
624                 if (from_user)
625                         skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_KERNEL);
626                 else
627                         skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
628                 
629                 if (!skb)
630                         break;
631
632                 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
633
634                 if (from_user)
635                         copy_from_user(skb_put(skb, size), buf + sent, size);
636                 else
637                         memcpy(skb_put(skb, size), buf + sent, size);
638
639                 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
640                         kfree_skb(skb);
641                         break;
642                 }
643
644                 sent  += size;
645                 count -= size;
646         }
647
648         return sent ? sent : err;
649 }
650
651 static int rfcomm_tty_write_room(struct tty_struct *tty)
652 {
653         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
654         struct rfcomm_dlc *dlc = dev->dlc;
655
656         BT_DBG("tty %p", tty);
657
658         return dlc->mtu * (dlc->tx_credits ? : 10);
659 }
660
661 static int rfcomm_tty_set_modem_status(uint cmd, struct rfcomm_dlc *dlc, uint status)
662 {
663         u8 v24_sig, mask;
664
665         BT_DBG("dlc %p cmd 0x%02x", dlc, cmd);
666
667         if (cmd == TIOCMSET)
668                 v24_sig = 0;
669         else
670                 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
671
672         mask =  ((status & TIOCM_DSR) ? RFCOMM_V24_RTC : 0) |
673                 ((status & TIOCM_DTR) ? RFCOMM_V24_RTC : 0) |
674                 ((status & TIOCM_RTS) ? RFCOMM_V24_RTR : 0) |
675                 ((status & TIOCM_CTS) ? RFCOMM_V24_RTR : 0) |
676                 ((status & TIOCM_RI)  ? RFCOMM_V24_IC  : 0) |
677                 ((status & TIOCM_CD)  ? RFCOMM_V24_DV  : 0);
678
679         if (cmd == TIOCMBIC)
680                 v24_sig &= ~mask;
681         else
682                 v24_sig |= mask;
683
684         rfcomm_dlc_set_modem_status(dlc, v24_sig);
685         return 0;
686 }
687
688 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
689 {
690         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
691         struct rfcomm_dlc *dlc = dev->dlc;
692         uint status;
693         int err;
694
695         BT_DBG("tty %p cmd 0x%02x", tty, cmd);
696
697         switch (cmd) {
698         case TCGETS:
699                 BT_DBG("TCGETS is not supported");
700                 return -ENOIOCTLCMD;
701
702         case TCSETS:
703                 BT_DBG("TCSETS is not supported");
704                 return -ENOIOCTLCMD;
705
706         case TIOCMGET:
707                 BT_DBG("TIOCMGET");
708
709                 return put_user(dev->modem_status, (unsigned int *)arg);
710
711         case TIOCMSET: /* Turns on and off the lines as specified by the mask */
712         case TIOCMBIS: /* Turns on the lines as specified by the mask */
713         case TIOCMBIC: /* Turns off the lines as specified by the mask */
714                 if ((err = get_user(status, (unsigned int *)arg)))
715                         return err;
716                 return rfcomm_tty_set_modem_status(cmd, dlc, status);
717
718         case TIOCMIWAIT:
719                 BT_DBG("TIOCMIWAIT");
720                 break;
721
722         case TIOCGICOUNT:
723                 BT_DBG("TIOCGICOUNT");
724                 break;
725
726         case TIOCGSERIAL:
727                 BT_ERR("TIOCGSERIAL is not supported");
728                 return -ENOIOCTLCMD;
729
730         case TIOCSSERIAL:
731                 BT_ERR("TIOCSSERIAL is not supported");
732                 return -ENOIOCTLCMD;
733
734         case TIOCSERGSTRUCT:
735                 BT_ERR("TIOCSERGSTRUCT is not supported");
736                 return -ENOIOCTLCMD;
737
738         case TIOCSERGETLSR:
739                 BT_ERR("TIOCSERGETLSR is not supported");
740                 return -ENOIOCTLCMD;
741
742         case TIOCSERCONFIG:
743                 BT_ERR("TIOCSERCONFIG is not supported");
744                 return -ENOIOCTLCMD;
745
746         default:
747                 return -ENOIOCTLCMD;    /* ioctls which we must ignore */
748
749         }
750
751         return -ENOIOCTLCMD;
752 }
753
754 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
755
756 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
757 {
758         BT_DBG("tty %p", tty);
759
760         if ((tty->termios->c_cflag == old->c_cflag) &&
761                 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old->c_iflag)))
762                 return;
763
764         /* handle turning off CRTSCTS */
765         if ((old->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
766                 BT_DBG("turning off CRTSCTS");
767         }
768 }
769
770 static void rfcomm_tty_throttle(struct tty_struct *tty)
771 {
772         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
773
774         BT_DBG("tty %p dev %p", tty, dev);
775         
776         rfcomm_dlc_throttle(dev->dlc);
777 }
778
779 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
780 {
781         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
782
783         BT_DBG("tty %p dev %p", tty, dev);
784         
785         rfcomm_dlc_unthrottle(dev->dlc);
786 }
787
788 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
789 {
790         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
791         struct rfcomm_dlc *dlc = dev->dlc;
792
793         BT_DBG("tty %p dev %p", tty, dev);
794
795         if (skb_queue_len(&dlc->tx_queue))
796                 return dlc->mtu;
797
798         return 0;
799 }
800
801 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
802 {
803         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
804         if (!dev)
805                 return;
806
807         BT_DBG("tty %p dev %p", tty, dev);
808
809         skb_queue_purge(&dev->dlc->tx_queue);
810
811         if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
812                 tty->ldisc.write_wakeup(tty);
813 }
814
815 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
816 {
817         BT_DBG("tty %p ch %c", tty, ch);
818 }
819
820 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
821 {
822         BT_DBG("tty %p timeout %d", tty, timeout);
823 }
824
825 static void rfcomm_tty_hangup(struct tty_struct *tty)
826 {
827         struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
828         if (!dev)
829                 return;
830
831         BT_DBG("tty %p dev %p", tty, dev);
832
833         rfcomm_tty_flush_buffer(tty);
834
835         if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
836                 rfcomm_dev_del(dev);
837 }
838
839 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
840 {
841         return 0;
842 }
843
844 /* ---- TTY structure ---- */
845 static int    rfcomm_tty_refcount;       /* If we manage several devices */
846
847 static struct tty_struct *rfcomm_tty_table[RFCOMM_TTY_PORTS];
848 static struct termios *rfcomm_tty_termios[RFCOMM_TTY_PORTS];
849 static struct termios *rfcomm_tty_termios_locked[RFCOMM_TTY_PORTS];
850
851 static struct tty_driver rfcomm_tty_driver = {
852         magic:                  TTY_DRIVER_MAGIC,
853         driver_name:            "rfcomm",
854 #ifdef CONFIG_DEVFS_FS
855         name:                   "bluetooth/rfcomm/%d",
856 #else
857         name:                   "rfcomm",
858 #endif
859         major:                  RFCOMM_TTY_MAJOR,
860         minor_start:            RFCOMM_TTY_MINOR,
861         num:                    RFCOMM_TTY_PORTS,
862         type:                   TTY_DRIVER_TYPE_SERIAL,
863         subtype:                SERIAL_TYPE_NORMAL,
864         flags:                  TTY_DRIVER_REAL_RAW,
865
866         refcount:               &rfcomm_tty_refcount,
867         table:                  rfcomm_tty_table,
868         termios:                rfcomm_tty_termios,
869         termios_locked:         rfcomm_tty_termios_locked,
870
871         open:                   rfcomm_tty_open,
872         close:                  rfcomm_tty_close,
873         write:                  rfcomm_tty_write,
874         write_room:             rfcomm_tty_write_room,
875         chars_in_buffer:        rfcomm_tty_chars_in_buffer,
876         flush_buffer:           rfcomm_tty_flush_buffer,
877         ioctl:                  rfcomm_tty_ioctl,
878         throttle:               rfcomm_tty_throttle,
879         unthrottle:             rfcomm_tty_unthrottle,
880         set_termios:            rfcomm_tty_set_termios,
881         send_xchar:             rfcomm_tty_send_xchar,
882         stop:                   NULL,
883         start:                  NULL,
884         hangup:                 rfcomm_tty_hangup,
885         wait_until_sent:        rfcomm_tty_wait_until_sent,
886         read_proc:              rfcomm_tty_read_proc,
887 };
888
889 int rfcomm_init_ttys(void)
890 {
891         int i;
892
893         /* Initalize our global data */
894         for (i = 0; i < RFCOMM_TTY_PORTS; i++)
895                 rfcomm_tty_table[i] = NULL;
896
897         /* Register the TTY driver */
898         rfcomm_tty_driver.init_termios = tty_std_termios;
899         rfcomm_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
900         rfcomm_tty_driver.flags = TTY_DRIVER_REAL_RAW;
901
902         if (tty_register_driver(&rfcomm_tty_driver)) {
903                 BT_ERR("Can't register RFCOMM TTY driver");
904                 return -1;
905         }
906
907         return 0;
908 }
909
910 void rfcomm_cleanup_ttys(void)
911 {
912         tty_unregister_driver(&rfcomm_tty_driver);
913         return;
914 }