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