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