6f20b4206e08a56bc3b79902b9fededeac826cd9
[linux-2.6.git] / net / irda / ircomm / ircomm_tty.c
1 /*********************************************************************
2  *                
3  * Filename:      ircomm_tty.c
4  * Version:       1.0
5  * Description:   IrCOMM serial TTY driver
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Jun  6 21:00:56 1999
9  * Modified at:   Wed Feb 23 00:09:02 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * Sources:       serial.c and previous IrCOMM work by Takahide Higuchi
12  * 
13  *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14  *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15  *     
16  *     This program is free software; you can redistribute it and/or 
17  *     modify it under the terms of the GNU General Public License as 
18  *     published by the Free Software Foundation; either version 2 of 
19  *     the License, or (at your option) any later version.
20  * 
21  *     This program is distributed in the hope that it will be useful,
22  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  *     GNU General Public License for more details.
25  * 
26  *     You should have received a copy of the GNU General Public License 
27  *     along with this program; if not, write to the Free Software 
28  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
29  *     MA 02111-1307 USA
30  *     
31  ********************************************************************/
32
33 #include <linux/config.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/sched.h>
38 #include <linux/termios.h>
39 #include <linux/tty.h>
40 #include <linux/interrupt.h>
41 #include <linux/device.h>               /* for MODULE_ALIAS_CHARDEV_MAJOR */
42
43 #include <asm/uaccess.h>
44
45 #include <net/irda/irda.h>
46 #include <net/irda/irmod.h>
47
48 #include <net/irda/ircomm_core.h>
49 #include <net/irda/ircomm_param.h>
50 #include <net/irda/ircomm_tty_attach.h>
51 #include <net/irda/ircomm_tty.h>
52
53 static int  ircomm_tty_open(struct tty_struct *tty, struct file *filp);
54 static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
55 static int  ircomm_tty_write(struct tty_struct * tty,
56                              const unsigned char *buf, int count);
57 static int  ircomm_tty_write_room(struct tty_struct *tty);
58 static void ircomm_tty_throttle(struct tty_struct *tty);
59 static void ircomm_tty_unthrottle(struct tty_struct *tty);
60 static int  ircomm_tty_chars_in_buffer(struct tty_struct *tty);
61 static void ircomm_tty_flush_buffer(struct tty_struct *tty);
62 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
63 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
64 static void ircomm_tty_hangup(struct tty_struct *tty);
65 static void ircomm_tty_do_softint(void *private_);
66 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
67 static void ircomm_tty_stop(struct tty_struct *tty);
68
69 static int ircomm_tty_data_indication(void *instance, void *sap,
70                                       struct sk_buff *skb);
71 static int ircomm_tty_control_indication(void *instance, void *sap,
72                                          struct sk_buff *skb);
73 static void ircomm_tty_flow_indication(void *instance, void *sap, 
74                                        LOCAL_FLOW cmd);
75 #ifdef CONFIG_PROC_FS
76 static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
77                                 int *eof, void *unused);
78 #endif /* CONFIG_PROC_FS */
79 static struct tty_driver *driver;
80
81 hashbin_t *ircomm_tty = NULL;
82
83 static struct tty_operations ops = {
84         .open            = ircomm_tty_open,
85         .close           = ircomm_tty_close,
86         .write           = ircomm_tty_write,
87         .write_room      = ircomm_tty_write_room,
88         .chars_in_buffer = ircomm_tty_chars_in_buffer,
89         .flush_buffer    = ircomm_tty_flush_buffer,
90         .ioctl           = ircomm_tty_ioctl,    /* ircomm_tty_ioctl.c */
91         .tiocmget        = ircomm_tty_tiocmget, /* ircomm_tty_ioctl.c */
92         .tiocmset        = ircomm_tty_tiocmset, /* ircomm_tty_ioctl.c */
93         .throttle        = ircomm_tty_throttle,
94         .unthrottle      = ircomm_tty_unthrottle,
95         .send_xchar      = ircomm_tty_send_xchar,
96         .set_termios     = ircomm_tty_set_termios,
97         .stop            = ircomm_tty_stop,
98         .start           = ircomm_tty_start,
99         .hangup          = ircomm_tty_hangup,
100         .wait_until_sent = ircomm_tty_wait_until_sent,
101 #ifdef CONFIG_PROC_FS
102         .read_proc       = ircomm_tty_read_proc,
103 #endif /* CONFIG_PROC_FS */
104 };
105
106 /*
107  * Function ircomm_tty_init()
108  *
109  *    Init IrCOMM TTY layer/driver
110  *
111  */
112 static int __init ircomm_tty_init(void)
113 {
114         driver = alloc_tty_driver(IRCOMM_TTY_PORTS);
115         if (!driver)
116                 return -ENOMEM;
117         ircomm_tty = hashbin_new(HB_LOCK); 
118         if (ircomm_tty == NULL) {
119                 IRDA_ERROR("%s(), can't allocate hashbin!\n", __FUNCTION__);
120                 put_tty_driver(driver);
121                 return -ENOMEM;
122         }
123
124         driver->owner           = THIS_MODULE;
125         driver->driver_name     = "ircomm";
126         driver->name            = "ircomm";
127         driver->devfs_name      = "ircomm";
128         driver->major           = IRCOMM_TTY_MAJOR;
129         driver->minor_start     = IRCOMM_TTY_MINOR;
130         driver->type            = TTY_DRIVER_TYPE_SERIAL;
131         driver->subtype         = SERIAL_TYPE_NORMAL;
132         driver->init_termios    = tty_std_termios;
133         driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
134         driver->flags           = TTY_DRIVER_REAL_RAW;
135         tty_set_operations(driver, &ops);
136         if (tty_register_driver(driver)) {
137                 IRDA_ERROR("%s(): Couldn't register serial driver\n",
138                            __FUNCTION__);
139                 put_tty_driver(driver);
140                 return -1;
141         }
142         return 0;
143 }
144
145 static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
146 {
147         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
148
149         IRDA_ASSERT(self != NULL, return;);
150         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
151
152         ircomm_tty_shutdown(self);
153
154         self->magic = 0;
155         kfree(self);
156 }
157
158 /*
159  * Function ircomm_tty_cleanup ()
160  *
161  *    Remove IrCOMM TTY layer/driver
162  *
163  */
164 static void __exit ircomm_tty_cleanup(void)
165 {
166         int ret;
167
168         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ ); 
169
170         ret = tty_unregister_driver(driver);
171         if (ret) {
172                 IRDA_ERROR("%s(), failed to unregister driver\n",
173                            __FUNCTION__);
174                 return;
175         }
176
177         hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
178         put_tty_driver(driver);
179 }
180
181 /*
182  * Function ircomm_startup (self)
183  *
184  *    
185  *
186  */
187 static int ircomm_tty_startup(struct ircomm_tty_cb *self)
188 {
189         notify_t notify;
190         int ret = -ENODEV;
191
192         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
193
194         IRDA_ASSERT(self != NULL, return -1;);
195         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
196
197         /* Check if already open */
198         if (test_and_set_bit(ASYNC_B_INITIALIZED, &self->flags)) {
199                 IRDA_DEBUG(2, "%s(), already open so break out!\n", __FUNCTION__ );
200                 return 0;
201         }
202
203         /* Register with IrCOMM */
204         irda_notify_init(&notify);
205         /* These callbacks we must handle ourselves */
206         notify.data_indication       = ircomm_tty_data_indication;
207         notify.udata_indication      = ircomm_tty_control_indication;
208         notify.flow_indication       = ircomm_tty_flow_indication;
209
210         /* Use the ircomm_tty interface for these ones */
211         notify.disconnect_indication = ircomm_tty_disconnect_indication;
212         notify.connect_confirm       = ircomm_tty_connect_confirm;
213         notify.connect_indication    = ircomm_tty_connect_indication;
214         strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
215         notify.instance = self;
216
217         if (!self->ircomm) {
218                 self->ircomm = ircomm_open(&notify, self->service_type, 
219                                            self->line);
220         }
221         if (!self->ircomm)
222                 goto err;
223
224         self->slsap_sel = self->ircomm->slsap_sel;
225
226         /* Connect IrCOMM link with remote device */
227         ret = ircomm_tty_attach_cable(self);
228         if (ret < 0) {
229                 IRDA_ERROR("%s(), error attaching cable!\n", __FUNCTION__);
230                 goto err;
231         }
232
233         return 0;
234 err:
235         clear_bit(ASYNC_B_INITIALIZED, &self->flags);
236         return ret;
237 }
238
239 /*
240  * Function ircomm_block_til_ready (self, filp)
241  *
242  *    
243  *
244  */
245 static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self, 
246                                       struct file *filp)
247 {
248         DECLARE_WAITQUEUE(wait, current);
249         int             retval;
250         int             do_clocal = 0, extra_count = 0;
251         unsigned long   flags;
252         struct tty_struct *tty;
253         
254         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
255
256         tty = self->tty;
257
258         /*
259          * If non-blocking mode is set, or the port is not enabled,
260          * then make the check up front and then exit.
261          */     
262         if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
263                 /* nonblock mode is set or port is not enabled */
264                 self->flags |= ASYNC_NORMAL_ACTIVE;
265                 IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __FUNCTION__ );
266                 return 0;
267         }
268
269         if (tty->termios->c_cflag & CLOCAL) {
270                 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __FUNCTION__ );
271                 do_clocal = 1;
272         }
273         
274         /* Wait for carrier detect and the line to become
275          * free (i.e., not in use by the callout).  While we are in
276          * this loop, self->open_count is dropped by one, so that
277          * mgsl_close() knows when to free things.  We restore it upon
278          * exit, either normal or abnormal.
279          */
280          
281         retval = 0;
282         add_wait_queue(&self->open_wait, &wait);
283         
284         IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
285               __FILE__,__LINE__, tty->driver->name, self->open_count );
286
287         /* As far as I can see, we protect open_count - Jean II */
288         spin_lock_irqsave(&self->spinlock, flags);
289         if (!tty_hung_up_p(filp)) {
290                 extra_count = 1;
291                 self->open_count--;
292         }
293         spin_unlock_irqrestore(&self->spinlock, flags);
294         self->blocked_open++;
295         
296         while (1) {
297                 if (tty->termios->c_cflag & CBAUD) {
298                         /* Here, we use to lock those two guys, but
299                          * as ircomm_param_request() does it itself,
300                          * I don't see the point (and I see the deadlock).
301                          * Jean II */
302                         self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
303                         
304                         ircomm_param_request(self, IRCOMM_DTE, TRUE);
305                 }
306                 
307                 current->state = TASK_INTERRUPTIBLE;
308                 
309                 if (tty_hung_up_p(filp) ||
310                     !test_bit(ASYNC_B_INITIALIZED, &self->flags)) {
311                         retval = (self->flags & ASYNC_HUP_NOTIFY) ?
312                                         -EAGAIN : -ERESTARTSYS;
313                         break;
314                 }
315                 
316                 /*  
317                  * Check if link is ready now. Even if CLOCAL is
318                  * specified, we cannot return before the IrCOMM link is
319                  * ready 
320                  */
321                 if (!test_bit(ASYNC_B_CLOSING, &self->flags) &&
322                     (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
323                     self->state == IRCOMM_TTY_READY)
324                 {
325                         break;
326                 }
327                         
328                 if (signal_pending(current)) {
329                         retval = -ERESTARTSYS;
330                         break;
331                 }
332                 
333                 IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
334                       __FILE__,__LINE__, tty->driver->name, self->open_count );
335                 
336                 schedule();
337         }
338         
339         __set_current_state(TASK_RUNNING);
340         remove_wait_queue(&self->open_wait, &wait);
341         
342         if (extra_count) {
343                 /* ++ is not atomic, so this should be protected - Jean II */
344                 spin_lock_irqsave(&self->spinlock, flags);
345                 self->open_count++;
346                 spin_unlock_irqrestore(&self->spinlock, flags);
347         }
348         self->blocked_open--;
349         
350         IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
351               __FILE__,__LINE__, tty->driver->name, self->open_count);
352                          
353         if (!retval)
354                 self->flags |= ASYNC_NORMAL_ACTIVE;
355                 
356         return retval;  
357 }
358
359 /*
360  * Function ircomm_tty_open (tty, filp)
361  *
362  *    This routine is called when a particular tty device is opened. This
363  *    routine is mandatory; if this routine is not filled in, the attempted
364  *    open will fail with ENODEV.
365  */
366 static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
367 {
368         struct ircomm_tty_cb *self;
369         unsigned int line;
370         unsigned long   flags;
371         int ret;
372
373         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
374
375         line = tty->index;
376         if ((line < 0) || (line >= IRCOMM_TTY_PORTS)) {
377                 return -ENODEV;
378         }
379
380         /* Check if instance already exists */
381         self = hashbin_lock_find(ircomm_tty, line, NULL);
382         if (!self) {
383                 /* No, so make new instance */
384                 self = kmalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
385                 if (self == NULL) {
386                         IRDA_ERROR("%s(), kmalloc failed!\n", __FUNCTION__);
387                         return -ENOMEM;
388                 }
389                 memset(self, 0, sizeof(struct ircomm_tty_cb));
390                 
391                 self->magic = IRCOMM_TTY_MAGIC;
392                 self->flow = FLOW_STOP;
393
394                 self->line = line;
395                 INIT_WORK(&self->tqueue, ircomm_tty_do_softint, self);
396                 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
397                 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
398                 self->close_delay = 5*HZ/10;
399                 self->closing_wait = 30*HZ;
400
401                 /* Init some important stuff */
402                 init_timer(&self->watchdog_timer);
403                 init_waitqueue_head(&self->open_wait);
404                 init_waitqueue_head(&self->close_wait);
405                 spin_lock_init(&self->spinlock);
406
407                 /* 
408                  * Force TTY into raw mode by default which is usually what
409                  * we want for IrCOMM and IrLPT. This way applications will
410                  * not have to twiddle with printcap etc.  
411                  */
412                 tty->termios->c_iflag = 0;
413                 tty->termios->c_oflag = 0;
414
415                 /* Insert into hash */
416                 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
417         }
418         /* ++ is not atomic, so this should be protected - Jean II */
419         spin_lock_irqsave(&self->spinlock, flags);
420         self->open_count++;
421
422         tty->driver_data = self;
423         self->tty = tty;
424         spin_unlock_irqrestore(&self->spinlock, flags);
425
426         IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __FUNCTION__ , tty->driver->name, 
427                    self->line, self->open_count);
428
429         /* Not really used by us, but lets do it anyway */
430         self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
431
432         /*
433          * If the port is the middle of closing, bail out now
434          */
435         if (tty_hung_up_p(filp) ||
436             test_bit(ASYNC_B_CLOSING, &self->flags)) {
437
438                 /* Hm, why are we blocking on ASYNC_CLOSING if we
439                  * do return -EAGAIN/-ERESTARTSYS below anyway?
440                  * IMHO it's either not needed in the first place
441                  * or for some reason we need to make sure the async
442                  * closing has been finished - if so, wouldn't we
443                  * probably better sleep uninterruptible?
444                  */
445
446                 if (wait_event_interruptible(self->close_wait, !test_bit(ASYNC_B_CLOSING, &self->flags))) {
447                         IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
448                                      __FUNCTION__);
449                         return -ERESTARTSYS;
450                 }
451
452 #ifdef SERIAL_DO_RESTART
453                 return ((self->flags & ASYNC_HUP_NOTIFY) ?
454                         -EAGAIN : -ERESTARTSYS);
455 #else
456                 return -EAGAIN;
457 #endif
458         }
459
460         /* Check if this is a "normal" ircomm device, or an irlpt device */
461         if (line < 0x10) {
462                 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
463                 self->settings.service_type = IRCOMM_9_WIRE; /* 9 wire as default */
464                 /* Jan Kiszka -> add DSR/RI -> Conform to IrCOMM spec */
465                 self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI; /* Default line settings */
466                 IRDA_DEBUG(2, "%s(), IrCOMM device\n", __FUNCTION__ );
467         } else {
468                 IRDA_DEBUG(2, "%s(), IrLPT device\n", __FUNCTION__ );
469                 self->service_type = IRCOMM_3_WIRE_RAW;
470                 self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
471         }
472
473         ret = ircomm_tty_startup(self);
474         if (ret)
475                 return ret;
476
477         ret = ircomm_tty_block_til_ready(self, filp);
478         if (ret) {
479                 IRDA_DEBUG(2, 
480                       "%s(), returning after block_til_ready with %d\n", __FUNCTION__ ,
481                       ret);
482
483                 return ret;
484         }
485         return 0;
486 }
487
488 /*
489  * Function ircomm_tty_close (tty, filp)
490  *
491  *    This routine is called when a particular tty device is closed.
492  *
493  */
494 static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
495 {
496         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
497         unsigned long flags;
498
499         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
500
501         if (!tty)
502                 return;
503
504         IRDA_ASSERT(self != NULL, return;);
505         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
506
507         spin_lock_irqsave(&self->spinlock, flags);
508
509         if (tty_hung_up_p(filp)) {
510                 spin_unlock_irqrestore(&self->spinlock, flags);
511
512                 IRDA_DEBUG(0, "%s(), returning 1\n", __FUNCTION__ );
513                 return;
514         }
515
516         if ((tty->count == 1) && (self->open_count != 1)) {
517                 /*
518                  * Uh, oh.  tty->count is 1, which means that the tty
519                  * structure will be freed.  state->count should always
520                  * be one in these conditions.  If it's greater than
521                  * one, we've got real problems, since it means the
522                  * serial port won't be shutdown.
523                  */
524                 IRDA_DEBUG(0, "%s(), bad serial port count; "
525                            "tty->count is 1, state->count is %d\n", __FUNCTION__ , 
526                            self->open_count);
527                 self->open_count = 1;
528         }
529
530         if (--self->open_count < 0) {
531                 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
532                            __FUNCTION__, self->line, self->open_count);
533                 self->open_count = 0;
534         }
535         if (self->open_count) {
536                 spin_unlock_irqrestore(&self->spinlock, flags);
537
538                 IRDA_DEBUG(0, "%s(), open count > 0\n", __FUNCTION__ );
539                 return;
540         }
541
542         /* Hum... Should be test_and_set_bit ??? - Jean II */
543         set_bit(ASYNC_B_CLOSING, &self->flags);
544
545         /* We need to unlock here (we were unlocking at the end of this
546          * function), because tty_wait_until_sent() may schedule.
547          * I don't know if the rest should be protected somehow,
548          * so someone should check. - Jean II */
549         spin_unlock_irqrestore(&self->spinlock, flags);
550
551         /*
552          * Now we wait for the transmit buffer to clear; and we notify 
553          * the line discipline to only process XON/XOFF characters.
554          */
555         tty->closing = 1;
556         if (self->closing_wait != ASYNC_CLOSING_WAIT_NONE)
557                 tty_wait_until_sent(tty, self->closing_wait);
558
559         ircomm_tty_shutdown(self);
560
561         if (tty->driver->flush_buffer)
562                 tty->driver->flush_buffer(tty);
563         if (tty->ldisc.flush_buffer)
564                 tty->ldisc.flush_buffer(tty);
565
566         tty->closing = 0;
567         self->tty = NULL;
568
569         if (self->blocked_open) {
570                 if (self->close_delay)
571                         schedule_timeout_interruptible(self->close_delay);
572                 wake_up_interruptible(&self->open_wait);
573         }
574
575         self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
576         wake_up_interruptible(&self->close_wait);
577 }
578
579 /*
580  * Function ircomm_tty_flush_buffer (tty)
581  *
582  *    
583  *
584  */
585 static void ircomm_tty_flush_buffer(struct tty_struct *tty)
586 {
587         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
588
589         IRDA_ASSERT(self != NULL, return;);
590         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
591
592         /* 
593          * Let do_softint() do this to avoid race condition with 
594          * do_softint() ;-) 
595          */
596         schedule_work(&self->tqueue);
597 }
598
599 /*
600  * Function ircomm_tty_do_softint (private_)
601  *
602  *    We use this routine to give the write wakeup to the user at at a
603  *    safe time (as fast as possible after write have completed). This 
604  *    can be compared to the Tx interrupt.
605  */
606 static void ircomm_tty_do_softint(void *private_)
607 {
608         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) private_;
609         struct tty_struct *tty;
610         unsigned long flags;
611         struct sk_buff *skb, *ctrl_skb;
612
613         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
614
615         if (!self || self->magic != IRCOMM_TTY_MAGIC)
616                 return;
617
618         tty = self->tty;
619         if (!tty)
620                 return;
621
622         /* Unlink control buffer */
623         spin_lock_irqsave(&self->spinlock, flags);
624
625         ctrl_skb = self->ctrl_skb;
626         self->ctrl_skb = NULL;
627
628         spin_unlock_irqrestore(&self->spinlock, flags);
629
630         /* Flush control buffer if any */
631         if(ctrl_skb) {
632                 if(self->flow == FLOW_START)
633                         ircomm_control_request(self->ircomm, ctrl_skb);
634                 /* Drop reference count - see ircomm_ttp_data_request(). */
635                 dev_kfree_skb(ctrl_skb);
636         }
637
638         if (tty->hw_stopped)
639                 return;
640
641         /* Unlink transmit buffer */
642         spin_lock_irqsave(&self->spinlock, flags);
643         
644         skb = self->tx_skb;
645         self->tx_skb = NULL;
646
647         spin_unlock_irqrestore(&self->spinlock, flags);
648
649         /* Flush transmit buffer if any */
650         if (skb) {
651                 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
652                 /* Drop reference count - see ircomm_ttp_data_request(). */
653                 dev_kfree_skb(skb);
654         }
655                 
656         /* Check if user (still) wants to be waken up */
657         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && 
658             tty->ldisc.write_wakeup)
659         {
660                 (tty->ldisc.write_wakeup)(tty);
661         }
662         wake_up_interruptible(&tty->write_wait);
663 }
664
665 /*
666  * Function ircomm_tty_write (tty, buf, count)
667  *
668  *    This routine is called by the kernel to write a series of characters
669  *    to the tty device. The characters may come from user space or kernel
670  *    space. This routine will return the number of characters actually
671  *    accepted for writing. This routine is mandatory.
672  */
673 static int ircomm_tty_write(struct tty_struct *tty,
674                             const unsigned char *buf, int count)
675 {
676         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
677         unsigned long flags;
678         struct sk_buff *skb;
679         int tailroom = 0;
680         int len = 0;
681         int size;
682
683         IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __FUNCTION__ , count,
684                    tty->hw_stopped);
685
686         IRDA_ASSERT(self != NULL, return -1;);
687         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
688
689         /* We may receive packets from the TTY even before we have finished
690          * our setup. Not cool.
691          * The problem is that we don't know the final header and data size
692          * to create the proper skb, so any skb we would create would have
693          * bogus header and data size, so need care.
694          * We use a bogus header size to safely detect this condition.
695          * Another problem is that hw_stopped was set to 0 way before it
696          * should be, so we would drop this skb. It should now be fixed.
697          * One option is to not accept data until we are properly setup.
698          * But, I suspect that when it happens, the ppp line discipline
699          * just "drops" the data, which might screw up connect scripts.
700          * The second option is to create a "safe skb", with large header
701          * and small size (see ircomm_tty_open() for values).
702          * We just need to make sure that when the real values get filled,
703          * we don't mess up the original "safe skb" (see tx_data_size).
704          * Jean II */
705         if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
706                 IRDA_DEBUG(1, "%s() : not initialised\n", __FUNCTION__);
707 #ifdef IRCOMM_NO_TX_BEFORE_INIT
708                 /* We didn't consume anything, TTY will retry */
709                 return 0;
710 #endif
711         }
712
713         if (count < 1)
714                 return 0;
715
716         /* Protect our manipulation of self->tx_skb and related */
717         spin_lock_irqsave(&self->spinlock, flags);
718
719         /* Fetch current transmit buffer */
720         skb = self->tx_skb;
721
722         /*  
723          * Send out all the data we get, possibly as multiple fragmented
724          * frames, but this will only happen if the data is larger than the
725          * max data size. The normal case however is just the opposite, and
726          * this function may be called multiple times, and will then actually
727          * defragment the data and send it out as one packet as soon as 
728          * possible, but at a safer point in time
729          */
730         while (count) {
731                 size = count;
732
733                 /* Adjust data size to the max data size */
734                 if (size > self->max_data_size)
735                         size = self->max_data_size;
736                 
737                 /* 
738                  * Do we already have a buffer ready for transmit, or do
739                  * we need to allocate a new frame 
740                  */
741                 if (skb) {                      
742                         /* 
743                          * Any room for more data at the end of the current 
744                          * transmit buffer? Cannot use skb_tailroom, since
745                          * dev_alloc_skb gives us a larger skb than we 
746                          * requested
747                          * Note : use tx_data_size, because max_data_size
748                          * may have changed and we don't want to overwrite
749                          * the skb. - Jean II
750                          */
751                         if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
752                                 /* Adjust data to tailroom */
753                                 if (size > tailroom)
754                                         size = tailroom;
755                         } else {
756                                 /* 
757                                  * Current transmit frame is full, so break 
758                                  * out, so we can send it as soon as possible
759                                  */
760                                 break;
761                         }
762                 } else {
763                         /* Prepare a full sized frame */
764                         skb = dev_alloc_skb(self->max_data_size+
765                                             self->max_header_size);
766                         if (!skb) {
767                                 spin_unlock_irqrestore(&self->spinlock, flags);
768                                 return -ENOBUFS;
769                         }
770                         skb_reserve(skb, self->max_header_size);
771                         self->tx_skb = skb;
772                         /* Remember skb size because max_data_size may
773                          * change later on - Jean II */
774                         self->tx_data_size = self->max_data_size;
775                 }
776
777                 /* Copy data */
778                 memcpy(skb_put(skb,size), buf + len, size);
779
780                 count -= size;
781                 len += size;
782         }
783
784         spin_unlock_irqrestore(&self->spinlock, flags);
785
786         /*     
787          * Schedule a new thread which will transmit the frame as soon
788          * as possible, but at a safe point in time. We do this so the
789          * "user" can give us data multiple times, as PPP does (because of
790          * its 256 byte tx buffer). We will then defragment and send out
791          * all this data as one single packet.  
792          */
793         schedule_work(&self->tqueue);
794         
795         return len;
796 }
797
798 /*
799  * Function ircomm_tty_write_room (tty)
800  *
801  *    This routine returns the numbers of characters the tty driver will
802  *    accept for queuing to be written. This number is subject to change as
803  *    output buffers get emptied, or if the output flow control is acted.
804  */
805 static int ircomm_tty_write_room(struct tty_struct *tty)
806 {
807         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
808         unsigned long flags;
809         int ret;
810
811         IRDA_ASSERT(self != NULL, return -1;);
812         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
813
814 #ifdef IRCOMM_NO_TX_BEFORE_INIT
815         /* max_header_size tells us if the channel is initialised or not. */
816         if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
817                 /* Don't bother us yet */
818                 return 0;
819 #endif
820
821         /* Check if we are allowed to transmit any data.
822          * hw_stopped is the regular flow control.
823          * Jean II */
824         if (tty->hw_stopped)
825                 ret = 0;
826         else {
827                 spin_lock_irqsave(&self->spinlock, flags);
828                 if (self->tx_skb)
829                         ret = self->tx_data_size - self->tx_skb->len;
830                 else
831                         ret = self->max_data_size;
832                 spin_unlock_irqrestore(&self->spinlock, flags);
833         }
834         IRDA_DEBUG(2, "%s(), ret=%d\n", __FUNCTION__ , ret);
835
836         return ret;
837 }
838
839 /*
840  * Function ircomm_tty_wait_until_sent (tty, timeout)
841  *
842  *    This routine waits until the device has written out all of the
843  *    characters in its transmitter FIFO.
844  */
845 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
846 {
847         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
848         unsigned long orig_jiffies, poll_time;
849         unsigned long flags;
850         
851         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
852
853         IRDA_ASSERT(self != NULL, return;);
854         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
855
856         orig_jiffies = jiffies;
857
858         /* Set poll time to 200 ms */
859         poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
860
861         spin_lock_irqsave(&self->spinlock, flags);
862         while (self->tx_skb && self->tx_skb->len) {
863                 spin_unlock_irqrestore(&self->spinlock, flags);
864                 schedule_timeout_interruptible(poll_time);
865                 spin_lock_irqsave(&self->spinlock, flags);
866                 if (signal_pending(current))
867                         break;
868                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
869                         break;
870         }
871         spin_unlock_irqrestore(&self->spinlock, flags);
872         current->state = TASK_RUNNING;
873 }
874
875 /*
876  * Function ircomm_tty_throttle (tty)
877  *
878  *    This routine notifies the tty driver that input buffers for the line
879  *    discipline are close to full, and it should somehow signal that no
880  *    more characters should be sent to the tty.  
881  */
882 static void ircomm_tty_throttle(struct tty_struct *tty)
883 {
884         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
885
886         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
887
888         IRDA_ASSERT(self != NULL, return;);
889         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
890
891         /* Software flow control? */
892         if (I_IXOFF(tty))
893                 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
894         
895         /* Hardware flow control? */
896         if (tty->termios->c_cflag & CRTSCTS) {
897                 self->settings.dte &= ~IRCOMM_RTS;
898                 self->settings.dte |= IRCOMM_DELTA_RTS;
899         
900                 ircomm_param_request(self, IRCOMM_DTE, TRUE);
901         }
902
903         ircomm_flow_request(self->ircomm, FLOW_STOP);
904 }
905
906 /*
907  * Function ircomm_tty_unthrottle (tty)
908  *
909  *    This routine notifies the tty drivers that it should signals that
910  *    characters can now be sent to the tty without fear of overrunning the
911  *    input buffers of the line disciplines.
912  */
913 static void ircomm_tty_unthrottle(struct tty_struct *tty)
914 {
915         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
916
917         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
918
919         IRDA_ASSERT(self != NULL, return;);
920         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
921
922         /* Using software flow control? */
923         if (I_IXOFF(tty)) {
924                 ircomm_tty_send_xchar(tty, START_CHAR(tty));
925         }
926
927         /* Using hardware flow control? */
928         if (tty->termios->c_cflag & CRTSCTS) {
929                 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
930
931                 ircomm_param_request(self, IRCOMM_DTE, TRUE);
932                 IRDA_DEBUG(1, "%s(), FLOW_START\n", __FUNCTION__ );
933         }
934         ircomm_flow_request(self->ircomm, FLOW_START);
935 }
936
937 /*
938  * Function ircomm_tty_chars_in_buffer (tty)
939  *
940  *    Indicates if there are any data in the buffer
941  *
942  */
943 static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
944 {
945         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
946         unsigned long flags;
947         int len = 0;
948
949         IRDA_ASSERT(self != NULL, return -1;);
950         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
951
952         spin_lock_irqsave(&self->spinlock, flags);
953
954         if (self->tx_skb)
955                 len = self->tx_skb->len;
956
957         spin_unlock_irqrestore(&self->spinlock, flags);
958
959         return len;
960 }
961
962 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
963 {
964         unsigned long flags;
965
966         IRDA_ASSERT(self != NULL, return;);
967         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
968
969         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
970
971         if (!test_and_clear_bit(ASYNC_B_INITIALIZED, &self->flags))
972                 return;
973
974         ircomm_tty_detach_cable(self);
975
976         spin_lock_irqsave(&self->spinlock, flags);
977
978         del_timer(&self->watchdog_timer);
979         
980         /* Free parameter buffer */
981         if (self->ctrl_skb) {
982                 dev_kfree_skb(self->ctrl_skb);
983                 self->ctrl_skb = NULL;
984         }
985
986         /* Free transmit buffer */
987         if (self->tx_skb) {
988                 dev_kfree_skb(self->tx_skb);
989                 self->tx_skb = NULL;
990         }
991
992         if (self->ircomm) {
993                 ircomm_close(self->ircomm);
994                 self->ircomm = NULL;
995         }
996
997         spin_unlock_irqrestore(&self->spinlock, flags);
998 }
999
1000 /*
1001  * Function ircomm_tty_hangup (tty)
1002  *
1003  *    This routine notifies the tty driver that it should hangup the tty
1004  *    device.
1005  * 
1006  */
1007 static void ircomm_tty_hangup(struct tty_struct *tty)
1008 {
1009         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1010         unsigned long   flags;
1011
1012         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
1013
1014         IRDA_ASSERT(self != NULL, return;);
1015         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1016
1017         if (!tty)
1018                 return;
1019
1020         /* ircomm_tty_flush_buffer(tty); */
1021         ircomm_tty_shutdown(self);
1022
1023         /* I guess we need to lock here - Jean II */
1024         spin_lock_irqsave(&self->spinlock, flags);
1025         self->flags &= ~ASYNC_NORMAL_ACTIVE;
1026         self->tty = NULL;
1027         self->open_count = 0;
1028         spin_unlock_irqrestore(&self->spinlock, flags);
1029
1030         wake_up_interruptible(&self->open_wait);
1031 }
1032
1033 /*
1034  * Function ircomm_tty_send_xchar (tty, ch)
1035  *
1036  *    This routine is used to send a high-priority XON/XOFF character to
1037  *    the device.
1038  */
1039 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1040 {
1041         IRDA_DEBUG(0, "%s(), not impl\n", __FUNCTION__ );
1042 }
1043
1044 /*
1045  * Function ircomm_tty_start (tty)
1046  *
1047  *    This routine notifies the tty driver that it resume sending
1048  *    characters to the tty device.  
1049  */
1050 void ircomm_tty_start(struct tty_struct *tty)
1051 {
1052         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1053
1054         ircomm_flow_request(self->ircomm, FLOW_START);
1055 }
1056
1057 /*
1058  * Function ircomm_tty_stop (tty)
1059  *
1060  *     This routine notifies the tty driver that it should stop outputting
1061  *     characters to the tty device. 
1062  */
1063 static void ircomm_tty_stop(struct tty_struct *tty) 
1064 {
1065         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1066
1067         IRDA_ASSERT(self != NULL, return;);
1068         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1069
1070         ircomm_flow_request(self->ircomm, FLOW_STOP);
1071 }
1072
1073 /*
1074  * Function ircomm_check_modem_status (self)
1075  *
1076  *    Check for any changes in the DCE's line settings. This function should
1077  *    be called whenever the dce parameter settings changes, to update the
1078  *    flow control settings and other things
1079  */
1080 void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1081 {
1082         struct tty_struct *tty;
1083         int status;
1084
1085         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
1086
1087         IRDA_ASSERT(self != NULL, return;);
1088         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1089
1090         tty = self->tty;
1091
1092         status = self->settings.dce;
1093
1094         if (status & IRCOMM_DCE_DELTA_ANY) {
1095                 /*wake_up_interruptible(&self->delta_msr_wait);*/
1096         }
1097         if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1098                 IRDA_DEBUG(2, 
1099                            "%s(), ircomm%d CD now %s...\n", __FUNCTION__ , self->line,
1100                            (status & IRCOMM_CD) ? "on" : "off");
1101
1102                 if (status & IRCOMM_CD) {
1103                         wake_up_interruptible(&self->open_wait);
1104                 } else {
1105                         IRDA_DEBUG(2, 
1106                                    "%s(), Doing serial hangup..\n", __FUNCTION__ );
1107                         if (tty)
1108                                 tty_hangup(tty);
1109
1110                         /* Hangup will remote the tty, so better break out */
1111                         return;
1112                 }
1113         }
1114         if (self->flags & ASYNC_CTS_FLOW) {
1115                 if (tty->hw_stopped) {
1116                         if (status & IRCOMM_CTS) {
1117                                 IRDA_DEBUG(2, 
1118                                            "%s(), CTS tx start...\n", __FUNCTION__ );
1119                                 tty->hw_stopped = 0;
1120                                 
1121                                 /* Wake up processes blocked on open */
1122                                 wake_up_interruptible(&self->open_wait);
1123
1124                                 schedule_work(&self->tqueue);
1125                                 return;
1126                         }
1127                 } else {
1128                         if (!(status & IRCOMM_CTS)) {
1129                                 IRDA_DEBUG(2, 
1130                                            "%s(), CTS tx stop...\n", __FUNCTION__ );
1131                                 tty->hw_stopped = 1;
1132                         }
1133                 }
1134         }
1135 }
1136
1137 /*
1138  * Function ircomm_tty_data_indication (instance, sap, skb)
1139  *
1140  *    Handle incoming data, and deliver it to the line discipline
1141  *
1142  */
1143 static int ircomm_tty_data_indication(void *instance, void *sap,
1144                                       struct sk_buff *skb)
1145 {
1146         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1147
1148         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
1149         
1150         IRDA_ASSERT(self != NULL, return -1;);
1151         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1152         IRDA_ASSERT(skb != NULL, return -1;);
1153
1154         if (!self->tty) {
1155                 IRDA_DEBUG(0, "%s(), no tty!\n", __FUNCTION__ );
1156                 return 0;
1157         }
1158
1159         /* 
1160          * If we receive data when hardware is stopped then something is wrong.
1161          * We try to poll the peers line settings to check if we are up todate.
1162          * Devices like WinCE can do this, and since they don't send any 
1163          * params, we can just as well declare the hardware for running.
1164          */
1165         if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
1166                 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __FUNCTION__ );
1167                 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1168
1169                 /* We can just as well declare the hardware for running */
1170                 ircomm_tty_send_initial_parameters(self);
1171                 ircomm_tty_link_established(self);
1172         }
1173
1174         /* 
1175          * Just give it over to the line discipline. There is no need to
1176          * involve the flip buffers, since we are not running in an interrupt 
1177          * handler
1178          */
1179         self->tty->ldisc.receive_buf(self->tty, skb->data, NULL, skb->len);
1180
1181         /* No need to kfree_skb - see ircomm_ttp_data_indication() */
1182
1183         return 0;
1184 }
1185
1186 /*
1187  * Function ircomm_tty_control_indication (instance, sap, skb)
1188  *
1189  *    Parse all incoming parameters (easy!)
1190  *
1191  */
1192 static int ircomm_tty_control_indication(void *instance, void *sap,
1193                                          struct sk_buff *skb)
1194 {
1195         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1196         int clen;
1197
1198         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
1199         
1200         IRDA_ASSERT(self != NULL, return -1;);
1201         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1202         IRDA_ASSERT(skb != NULL, return -1;);
1203
1204         clen = skb->data[0];
1205
1206         irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen), 
1207                                &ircomm_param_info);
1208
1209         /* No need to kfree_skb - see ircomm_control_indication() */
1210
1211         return 0;
1212 }
1213
1214 /*
1215  * Function ircomm_tty_flow_indication (instance, sap, cmd)
1216  *
1217  *    This function is called by IrTTP when it wants us to slow down the
1218  *    transmission of data. We just mark the hardware as stopped, and wait
1219  *    for IrTTP to notify us that things are OK again.
1220  */
1221 static void ircomm_tty_flow_indication(void *instance, void *sap, 
1222                                        LOCAL_FLOW cmd)
1223 {
1224         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1225         struct tty_struct *tty;
1226
1227         IRDA_ASSERT(self != NULL, return;);
1228         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1229
1230         tty = self->tty;
1231
1232         switch (cmd) {
1233         case FLOW_START:
1234                 IRDA_DEBUG(2, "%s(), hw start!\n", __FUNCTION__ );
1235                 tty->hw_stopped = 0;
1236
1237                 /* ircomm_tty_do_softint will take care of the rest */
1238                 schedule_work(&self->tqueue);
1239                 break;
1240         default:  /* If we get here, something is very wrong, better stop */
1241         case FLOW_STOP:
1242                 IRDA_DEBUG(2, "%s(), hw stopped!\n", __FUNCTION__ );
1243                 tty->hw_stopped = 1;
1244                 break;
1245         }
1246         self->flow = cmd;
1247 }
1248
1249 static int ircomm_tty_line_info(struct ircomm_tty_cb *self, char *buf)
1250 {
1251         int  ret=0;
1252
1253         ret += sprintf(buf+ret, "State: %s\n", ircomm_tty_state[self->state]);
1254
1255         ret += sprintf(buf+ret, "Service type: ");
1256         if (self->service_type & IRCOMM_9_WIRE)
1257                 ret += sprintf(buf+ret, "9_WIRE");
1258         else if (self->service_type & IRCOMM_3_WIRE)
1259                 ret += sprintf(buf+ret, "3_WIRE");
1260         else if (self->service_type & IRCOMM_3_WIRE_RAW)
1261                 ret += sprintf(buf+ret, "3_WIRE_RAW");
1262         else
1263                 ret += sprintf(buf+ret, "No common service type!\n");
1264         ret += sprintf(buf+ret, "\n");
1265
1266         ret += sprintf(buf+ret, "Port name: %s\n", self->settings.port_name);
1267
1268         ret += sprintf(buf+ret, "DTE status: ");        
1269         if (self->settings.dte & IRCOMM_RTS)
1270                 ret += sprintf(buf+ret, "RTS|");
1271         if (self->settings.dte & IRCOMM_DTR)
1272                 ret += sprintf(buf+ret, "DTR|");
1273         if (self->settings.dte)
1274                 ret--; /* remove the last | */
1275         ret += sprintf(buf+ret, "\n");
1276
1277         ret += sprintf(buf+ret, "DCE status: ");
1278         if (self->settings.dce & IRCOMM_CTS)
1279                 ret += sprintf(buf+ret, "CTS|");
1280         if (self->settings.dce & IRCOMM_DSR)
1281                 ret += sprintf(buf+ret, "DSR|");
1282         if (self->settings.dce & IRCOMM_CD)
1283                 ret += sprintf(buf+ret, "CD|");
1284         if (self->settings.dce & IRCOMM_RI) 
1285                 ret += sprintf(buf+ret, "RI|");
1286         if (self->settings.dce)
1287                 ret--; /* remove the last | */
1288         ret += sprintf(buf+ret, "\n");
1289
1290         ret += sprintf(buf+ret, "Configuration: ");
1291         if (!self->settings.null_modem)
1292                 ret += sprintf(buf+ret, "DTE <-> DCE\n");
1293         else
1294                 ret += sprintf(buf+ret, 
1295                                "DTE <-> DTE (null modem emulation)\n");
1296
1297         ret += sprintf(buf+ret, "Data rate: %d\n", self->settings.data_rate);
1298
1299         ret += sprintf(buf+ret, "Flow control: ");
1300         if (self->settings.flow_control & IRCOMM_XON_XOFF_IN)
1301                 ret += sprintf(buf+ret, "XON_XOFF_IN|");
1302         if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT)
1303                 ret += sprintf(buf+ret, "XON_XOFF_OUT|");
1304         if (self->settings.flow_control & IRCOMM_RTS_CTS_IN)
1305                 ret += sprintf(buf+ret, "RTS_CTS_IN|");
1306         if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT)
1307                 ret += sprintf(buf+ret, "RTS_CTS_OUT|");
1308         if (self->settings.flow_control & IRCOMM_DSR_DTR_IN)
1309                 ret += sprintf(buf+ret, "DSR_DTR_IN|");
1310         if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT)
1311                 ret += sprintf(buf+ret, "DSR_DTR_OUT|");
1312         if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN)
1313                 ret += sprintf(buf+ret, "ENQ_ACK_IN|");
1314         if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT)
1315                 ret += sprintf(buf+ret, "ENQ_ACK_OUT|");
1316         if (self->settings.flow_control)
1317                 ret--; /* remove the last | */
1318         ret += sprintf(buf+ret, "\n");
1319
1320         ret += sprintf(buf+ret, "Flags: ");
1321         if (self->flags & ASYNC_CTS_FLOW)
1322                 ret += sprintf(buf+ret, "ASYNC_CTS_FLOW|");
1323         if (self->flags & ASYNC_CHECK_CD)
1324                 ret += sprintf(buf+ret, "ASYNC_CHECK_CD|");
1325         if (self->flags & ASYNC_INITIALIZED)
1326                 ret += sprintf(buf+ret, "ASYNC_INITIALIZED|");
1327         if (self->flags & ASYNC_LOW_LATENCY)
1328                 ret += sprintf(buf+ret, "ASYNC_LOW_LATENCY|");
1329         if (self->flags & ASYNC_CLOSING)
1330                 ret += sprintf(buf+ret, "ASYNC_CLOSING|");
1331         if (self->flags & ASYNC_NORMAL_ACTIVE)
1332                 ret += sprintf(buf+ret, "ASYNC_NORMAL_ACTIVE|");
1333         if (self->flags)
1334                 ret--; /* remove the last | */
1335         ret += sprintf(buf+ret, "\n");
1336
1337         ret += sprintf(buf+ret, "Role: %s\n", self->client ? 
1338                        "client" : "server");
1339         ret += sprintf(buf+ret, "Open count: %d\n", self->open_count);
1340         ret += sprintf(buf+ret, "Max data size: %d\n", self->max_data_size);
1341         ret += sprintf(buf+ret, "Max header size: %d\n", self->max_header_size);
1342                 
1343         if (self->tty)
1344                 ret += sprintf(buf+ret, "Hardware: %s\n", 
1345                                self->tty->hw_stopped ? "Stopped" : "Running");
1346
1347         ret += sprintf(buf+ret, "\n");
1348         return ret;
1349 }
1350
1351
1352 /*
1353  * Function ircomm_tty_read_proc (buf, start, offset, len, eof, unused)
1354  *
1355  *    
1356  *
1357  */
1358 #ifdef CONFIG_PROC_FS
1359 static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
1360                                 int *eof, void *unused)
1361 {
1362         struct ircomm_tty_cb *self;
1363         int count = 0, l;
1364         off_t begin = 0;
1365         unsigned long flags;
1366
1367         spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1368
1369         self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1370         while ((self != NULL) && (count < 4000)) {
1371                 if (self->magic != IRCOMM_TTY_MAGIC)
1372                         break;
1373
1374                 l = ircomm_tty_line_info(self, buf + count);
1375                 count += l;
1376                 if (count+begin > offset+len)
1377                         goto done;
1378                 if (count+begin < offset) {
1379                         begin += count;
1380                         count = 0;
1381                 }
1382                                 
1383                 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1384         }
1385         *eof = 1;
1386 done:
1387         spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
1388
1389         if (offset >= count+begin)
1390                 return 0;
1391         *start = buf + (offset-begin);
1392         return ((len < begin+count-offset) ? len : begin+count-offset);
1393 }
1394 #endif /* CONFIG_PROC_FS */
1395
1396 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1397 MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1398 MODULE_LICENSE("GPL");
1399 MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1400
1401 module_init(ircomm_tty_init);
1402 module_exit(ircomm_tty_cleanup);