NET: irda, remove unnecessary checks
[linux-3.10.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/init.h>
34 #include <linux/module.h>
35 #include <linux/fs.h>
36 #include <linux/sched.h>
37 #include <linux/seq_file.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(struct work_struct *work);
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 const struct file_operations ircomm_tty_proc_fops;
77 #endif /* CONFIG_PROC_FS */
78 static struct tty_driver *driver;
79
80 static hashbin_t *ircomm_tty = NULL;
81
82 static const 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         .proc_fops       = &ircomm_tty_proc_fops,
102 #endif /* CONFIG_PROC_FS */
103 };
104
105 /*
106  * Function ircomm_tty_init()
107  *
108  *    Init IrCOMM TTY layer/driver
109  *
110  */
111 static 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                 IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
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->major           = IRCOMM_TTY_MAJOR;
127         driver->minor_start     = IRCOMM_TTY_MINOR;
128         driver->type            = TTY_DRIVER_TYPE_SERIAL;
129         driver->subtype         = SERIAL_TYPE_NORMAL;
130         driver->init_termios    = tty_std_termios;
131         driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
132         driver->flags           = TTY_DRIVER_REAL_RAW;
133         tty_set_operations(driver, &ops);
134         if (tty_register_driver(driver)) {
135                 IRDA_ERROR("%s(): Couldn't register serial driver\n",
136                            __func__);
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", __func__ );
146
147         IRDA_ASSERT(self != NULL, return;);
148         IRDA_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 static void __exit ircomm_tty_cleanup(void)
163 {
164         int ret;
165
166         IRDA_DEBUG(4, "%s()\n", __func__ );
167
168         ret = tty_unregister_driver(driver);
169         if (ret) {
170                 IRDA_ERROR("%s(), failed to unregister driver\n",
171                            __func__);
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", __func__ );
191
192         IRDA_ASSERT(self != NULL, return -1;);
193         IRDA_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", __func__ );
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                 IRDA_ERROR("%s(), error attaching cable!\n", __func__);
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", __func__ );
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", __func__ );
264                 return 0;
265         }
266
267         if (tty->termios->c_cflag & CLOCAL) {
268                 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
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", __func__ );
372
373         line = tty->index;
374         if (line >= IRCOMM_TTY_PORTS)
375                 return -ENODEV;
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 = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
382                 if (self == NULL) {
383                         IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
384                         return -ENOMEM;
385                 }
386
387                 self->magic = IRCOMM_TTY_MAGIC;
388                 self->flow = FLOW_STOP;
389
390                 self->line = line;
391                 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
392                 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
393                 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
394                 self->close_delay = 5*HZ/10;
395                 self->closing_wait = 30*HZ;
396
397                 /* Init some important stuff */
398                 init_timer(&self->watchdog_timer);
399                 init_waitqueue_head(&self->open_wait);
400                 init_waitqueue_head(&self->close_wait);
401                 spin_lock_init(&self->spinlock);
402
403                 /*
404                  * Force TTY into raw mode by default which is usually what
405                  * we want for IrCOMM and IrLPT. This way applications will
406                  * not have to twiddle with printcap etc.
407                  *
408                  * Note this is completely usafe and doesn't work properly
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", __func__ , 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                         IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
446                                      __func__);
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", __func__ );
465         } else {
466                 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
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", __func__ ,
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", __func__ );
498
499         IRDA_ASSERT(self != NULL, return;);
500         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
501
502         spin_lock_irqsave(&self->spinlock, flags);
503
504         if (tty_hung_up_p(filp)) {
505                 spin_unlock_irqrestore(&self->spinlock, flags);
506
507                 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
508                 return;
509         }
510
511         if ((tty->count == 1) && (self->open_count != 1)) {
512                 /*
513                  * Uh, oh.  tty->count is 1, which means that the tty
514                  * structure will be freed.  state->count should always
515                  * be one in these conditions.  If it's greater than
516                  * one, we've got real problems, since it means the
517                  * serial port won't be shutdown.
518                  */
519                 IRDA_DEBUG(0, "%s(), bad serial port count; "
520                            "tty->count is 1, state->count is %d\n", __func__ ,
521                            self->open_count);
522                 self->open_count = 1;
523         }
524
525         if (--self->open_count < 0) {
526                 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
527                            __func__, self->line, self->open_count);
528                 self->open_count = 0;
529         }
530         if (self->open_count) {
531                 spin_unlock_irqrestore(&self->spinlock, flags);
532
533                 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
534                 return;
535         }
536
537         /* Hum... Should be test_and_set_bit ??? - Jean II */
538         set_bit(ASYNC_B_CLOSING, &self->flags);
539
540         /* We need to unlock here (we were unlocking at the end of this
541          * function), because tty_wait_until_sent() may schedule.
542          * I don't know if the rest should be protected somehow,
543          * so someone should check. - Jean II */
544         spin_unlock_irqrestore(&self->spinlock, flags);
545
546         /*
547          * Now we wait for the transmit buffer to clear; and we notify
548          * the line discipline to only process XON/XOFF characters.
549          */
550         tty->closing = 1;
551         if (self->closing_wait != ASYNC_CLOSING_WAIT_NONE)
552                 tty_wait_until_sent(tty, self->closing_wait);
553
554         ircomm_tty_shutdown(self);
555
556         tty_driver_flush_buffer(tty);
557         tty_ldisc_flush(tty);
558
559         tty->closing = 0;
560         self->tty = NULL;
561
562         if (self->blocked_open) {
563                 if (self->close_delay)
564                         schedule_timeout_interruptible(self->close_delay);
565                 wake_up_interruptible(&self->open_wait);
566         }
567
568         self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
569         wake_up_interruptible(&self->close_wait);
570 }
571
572 /*
573  * Function ircomm_tty_flush_buffer (tty)
574  *
575  *
576  *
577  */
578 static void ircomm_tty_flush_buffer(struct tty_struct *tty)
579 {
580         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
581
582         IRDA_ASSERT(self != NULL, return;);
583         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
584
585         /*
586          * Let do_softint() do this to avoid race condition with
587          * do_softint() ;-)
588          */
589         schedule_work(&self->tqueue);
590 }
591
592 /*
593  * Function ircomm_tty_do_softint (work)
594  *
595  *    We use this routine to give the write wakeup to the user at at a
596  *    safe time (as fast as possible after write have completed). This
597  *    can be compared to the Tx interrupt.
598  */
599 static void ircomm_tty_do_softint(struct work_struct *work)
600 {
601         struct ircomm_tty_cb *self =
602                 container_of(work, struct ircomm_tty_cb, tqueue);
603         struct tty_struct *tty;
604         unsigned long flags;
605         struct sk_buff *skb, *ctrl_skb;
606
607         IRDA_DEBUG(2, "%s()\n", __func__ );
608
609         if (!self || self->magic != IRCOMM_TTY_MAGIC)
610                 return;
611
612         tty = self->tty;
613         if (!tty)
614                 return;
615
616         /* Unlink control buffer */
617         spin_lock_irqsave(&self->spinlock, flags);
618
619         ctrl_skb = self->ctrl_skb;
620         self->ctrl_skb = NULL;
621
622         spin_unlock_irqrestore(&self->spinlock, flags);
623
624         /* Flush control buffer if any */
625         if(ctrl_skb) {
626                 if(self->flow == FLOW_START)
627                         ircomm_control_request(self->ircomm, ctrl_skb);
628                 /* Drop reference count - see ircomm_ttp_data_request(). */
629                 dev_kfree_skb(ctrl_skb);
630         }
631
632         if (tty->hw_stopped)
633                 return;
634
635         /* Unlink transmit buffer */
636         spin_lock_irqsave(&self->spinlock, flags);
637
638         skb = self->tx_skb;
639         self->tx_skb = NULL;
640
641         spin_unlock_irqrestore(&self->spinlock, flags);
642
643         /* Flush transmit buffer if any */
644         if (skb) {
645                 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
646                 /* Drop reference count - see ircomm_ttp_data_request(). */
647                 dev_kfree_skb(skb);
648         }
649
650         /* Check if user (still) wants to be waken up */
651         tty_wakeup(tty);
652 }
653
654 /*
655  * Function ircomm_tty_write (tty, buf, count)
656  *
657  *    This routine is called by the kernel to write a series of characters
658  *    to the tty device. The characters may come from user space or kernel
659  *    space. This routine will return the number of characters actually
660  *    accepted for writing. This routine is mandatory.
661  */
662 static int ircomm_tty_write(struct tty_struct *tty,
663                             const unsigned char *buf, int count)
664 {
665         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
666         unsigned long flags;
667         struct sk_buff *skb;
668         int tailroom = 0;
669         int len = 0;
670         int size;
671
672         IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
673                    tty->hw_stopped);
674
675         IRDA_ASSERT(self != NULL, return -1;);
676         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
677
678         /* We may receive packets from the TTY even before we have finished
679          * our setup. Not cool.
680          * The problem is that we don't know the final header and data size
681          * to create the proper skb, so any skb we would create would have
682          * bogus header and data size, so need care.
683          * We use a bogus header size to safely detect this condition.
684          * Another problem is that hw_stopped was set to 0 way before it
685          * should be, so we would drop this skb. It should now be fixed.
686          * One option is to not accept data until we are properly setup.
687          * But, I suspect that when it happens, the ppp line discipline
688          * just "drops" the data, which might screw up connect scripts.
689          * The second option is to create a "safe skb", with large header
690          * and small size (see ircomm_tty_open() for values).
691          * We just need to make sure that when the real values get filled,
692          * we don't mess up the original "safe skb" (see tx_data_size).
693          * Jean II */
694         if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
695                 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
696 #ifdef IRCOMM_NO_TX_BEFORE_INIT
697                 /* We didn't consume anything, TTY will retry */
698                 return 0;
699 #endif
700         }
701
702         if (count < 1)
703                 return 0;
704
705         /* Protect our manipulation of self->tx_skb and related */
706         spin_lock_irqsave(&self->spinlock, flags);
707
708         /* Fetch current transmit buffer */
709         skb = self->tx_skb;
710
711         /*
712          * Send out all the data we get, possibly as multiple fragmented
713          * frames, but this will only happen if the data is larger than the
714          * max data size. The normal case however is just the opposite, and
715          * this function may be called multiple times, and will then actually
716          * defragment the data and send it out as one packet as soon as
717          * possible, but at a safer point in time
718          */
719         while (count) {
720                 size = count;
721
722                 /* Adjust data size to the max data size */
723                 if (size > self->max_data_size)
724                         size = self->max_data_size;
725
726                 /*
727                  * Do we already have a buffer ready for transmit, or do
728                  * we need to allocate a new frame
729                  */
730                 if (skb) {
731                         /*
732                          * Any room for more data at the end of the current
733                          * transmit buffer? Cannot use skb_tailroom, since
734                          * dev_alloc_skb gives us a larger skb than we
735                          * requested
736                          * Note : use tx_data_size, because max_data_size
737                          * may have changed and we don't want to overwrite
738                          * the skb. - Jean II
739                          */
740                         if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
741                                 /* Adjust data to tailroom */
742                                 if (size > tailroom)
743                                         size = tailroom;
744                         } else {
745                                 /*
746                                  * Current transmit frame is full, so break
747                                  * out, so we can send it as soon as possible
748                                  */
749                                 break;
750                         }
751                 } else {
752                         /* Prepare a full sized frame */
753                         skb = alloc_skb(self->max_data_size+
754                                         self->max_header_size,
755                                         GFP_ATOMIC);
756                         if (!skb) {
757                                 spin_unlock_irqrestore(&self->spinlock, flags);
758                                 return -ENOBUFS;
759                         }
760                         skb_reserve(skb, self->max_header_size);
761                         self->tx_skb = skb;
762                         /* Remember skb size because max_data_size may
763                          * change later on - Jean II */
764                         self->tx_data_size = self->max_data_size;
765                 }
766
767                 /* Copy data */
768                 memcpy(skb_put(skb,size), buf + len, size);
769
770                 count -= size;
771                 len += size;
772         }
773
774         spin_unlock_irqrestore(&self->spinlock, flags);
775
776         /*
777          * Schedule a new thread which will transmit the frame as soon
778          * as possible, but at a safe point in time. We do this so the
779          * "user" can give us data multiple times, as PPP does (because of
780          * its 256 byte tx buffer). We will then defragment and send out
781          * all this data as one single packet.
782          */
783         schedule_work(&self->tqueue);
784
785         return len;
786 }
787
788 /*
789  * Function ircomm_tty_write_room (tty)
790  *
791  *    This routine returns the numbers of characters the tty driver will
792  *    accept for queuing to be written. This number is subject to change as
793  *    output buffers get emptied, or if the output flow control is acted.
794  */
795 static int ircomm_tty_write_room(struct tty_struct *tty)
796 {
797         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
798         unsigned long flags;
799         int ret;
800
801         IRDA_ASSERT(self != NULL, return -1;);
802         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
803
804 #ifdef IRCOMM_NO_TX_BEFORE_INIT
805         /* max_header_size tells us if the channel is initialised or not. */
806         if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
807                 /* Don't bother us yet */
808                 return 0;
809 #endif
810
811         /* Check if we are allowed to transmit any data.
812          * hw_stopped is the regular flow control.
813          * Jean II */
814         if (tty->hw_stopped)
815                 ret = 0;
816         else {
817                 spin_lock_irqsave(&self->spinlock, flags);
818                 if (self->tx_skb)
819                         ret = self->tx_data_size - self->tx_skb->len;
820                 else
821                         ret = self->max_data_size;
822                 spin_unlock_irqrestore(&self->spinlock, flags);
823         }
824         IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
825
826         return ret;
827 }
828
829 /*
830  * Function ircomm_tty_wait_until_sent (tty, timeout)
831  *
832  *    This routine waits until the device has written out all of the
833  *    characters in its transmitter FIFO.
834  */
835 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
836 {
837         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
838         unsigned long orig_jiffies, poll_time;
839         unsigned long flags;
840
841         IRDA_DEBUG(2, "%s()\n", __func__ );
842
843         IRDA_ASSERT(self != NULL, return;);
844         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
845
846         orig_jiffies = jiffies;
847
848         /* Set poll time to 200 ms */
849         poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
850
851         spin_lock_irqsave(&self->spinlock, flags);
852         while (self->tx_skb && self->tx_skb->len) {
853                 spin_unlock_irqrestore(&self->spinlock, flags);
854                 schedule_timeout_interruptible(poll_time);
855                 spin_lock_irqsave(&self->spinlock, flags);
856                 if (signal_pending(current))
857                         break;
858                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
859                         break;
860         }
861         spin_unlock_irqrestore(&self->spinlock, flags);
862         current->state = TASK_RUNNING;
863 }
864
865 /*
866  * Function ircomm_tty_throttle (tty)
867  *
868  *    This routine notifies the tty driver that input buffers for the line
869  *    discipline are close to full, and it should somehow signal that no
870  *    more characters should be sent to the tty.
871  */
872 static void ircomm_tty_throttle(struct tty_struct *tty)
873 {
874         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
875
876         IRDA_DEBUG(2, "%s()\n", __func__ );
877
878         IRDA_ASSERT(self != NULL, return;);
879         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
880
881         /* Software flow control? */
882         if (I_IXOFF(tty))
883                 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
884
885         /* Hardware flow control? */
886         if (tty->termios->c_cflag & CRTSCTS) {
887                 self->settings.dte &= ~IRCOMM_RTS;
888                 self->settings.dte |= IRCOMM_DELTA_RTS;
889
890                 ircomm_param_request(self, IRCOMM_DTE, TRUE);
891         }
892
893         ircomm_flow_request(self->ircomm, FLOW_STOP);
894 }
895
896 /*
897  * Function ircomm_tty_unthrottle (tty)
898  *
899  *    This routine notifies the tty drivers that it should signals that
900  *    characters can now be sent to the tty without fear of overrunning the
901  *    input buffers of the line disciplines.
902  */
903 static void ircomm_tty_unthrottle(struct tty_struct *tty)
904 {
905         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
906
907         IRDA_DEBUG(2, "%s()\n", __func__ );
908
909         IRDA_ASSERT(self != NULL, return;);
910         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
911
912         /* Using software flow control? */
913         if (I_IXOFF(tty)) {
914                 ircomm_tty_send_xchar(tty, START_CHAR(tty));
915         }
916
917         /* Using hardware flow control? */
918         if (tty->termios->c_cflag & CRTSCTS) {
919                 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
920
921                 ircomm_param_request(self, IRCOMM_DTE, TRUE);
922                 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
923         }
924         ircomm_flow_request(self->ircomm, FLOW_START);
925 }
926
927 /*
928  * Function ircomm_tty_chars_in_buffer (tty)
929  *
930  *    Indicates if there are any data in the buffer
931  *
932  */
933 static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
934 {
935         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
936         unsigned long flags;
937         int len = 0;
938
939         IRDA_ASSERT(self != NULL, return -1;);
940         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
941
942         spin_lock_irqsave(&self->spinlock, flags);
943
944         if (self->tx_skb)
945                 len = self->tx_skb->len;
946
947         spin_unlock_irqrestore(&self->spinlock, flags);
948
949         return len;
950 }
951
952 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
953 {
954         unsigned long flags;
955
956         IRDA_ASSERT(self != NULL, return;);
957         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
958
959         IRDA_DEBUG(0, "%s()\n", __func__ );
960
961         if (!test_and_clear_bit(ASYNC_B_INITIALIZED, &self->flags))
962                 return;
963
964         ircomm_tty_detach_cable(self);
965
966         spin_lock_irqsave(&self->spinlock, flags);
967
968         del_timer(&self->watchdog_timer);
969
970         /* Free parameter buffer */
971         if (self->ctrl_skb) {
972                 dev_kfree_skb(self->ctrl_skb);
973                 self->ctrl_skb = NULL;
974         }
975
976         /* Free transmit buffer */
977         if (self->tx_skb) {
978                 dev_kfree_skb(self->tx_skb);
979                 self->tx_skb = NULL;
980         }
981
982         if (self->ircomm) {
983                 ircomm_close(self->ircomm);
984                 self->ircomm = NULL;
985         }
986
987         spin_unlock_irqrestore(&self->spinlock, flags);
988 }
989
990 /*
991  * Function ircomm_tty_hangup (tty)
992  *
993  *    This routine notifies the tty driver that it should hangup the tty
994  *    device.
995  *
996  */
997 static void ircomm_tty_hangup(struct tty_struct *tty)
998 {
999         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1000         unsigned long   flags;
1001
1002         IRDA_DEBUG(0, "%s()\n", __func__ );
1003
1004         IRDA_ASSERT(self != NULL, return;);
1005         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1006
1007         /* ircomm_tty_flush_buffer(tty); */
1008         ircomm_tty_shutdown(self);
1009
1010         /* I guess we need to lock here - Jean II */
1011         spin_lock_irqsave(&self->spinlock, flags);
1012         self->flags &= ~ASYNC_NORMAL_ACTIVE;
1013         self->tty = NULL;
1014         self->open_count = 0;
1015         spin_unlock_irqrestore(&self->spinlock, flags);
1016
1017         wake_up_interruptible(&self->open_wait);
1018 }
1019
1020 /*
1021  * Function ircomm_tty_send_xchar (tty, ch)
1022  *
1023  *    This routine is used to send a high-priority XON/XOFF character to
1024  *    the device.
1025  */
1026 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1027 {
1028         IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
1029 }
1030
1031 /*
1032  * Function ircomm_tty_start (tty)
1033  *
1034  *    This routine notifies the tty driver that it resume sending
1035  *    characters to the tty device.
1036  */
1037 void ircomm_tty_start(struct tty_struct *tty)
1038 {
1039         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1040
1041         ircomm_flow_request(self->ircomm, FLOW_START);
1042 }
1043
1044 /*
1045  * Function ircomm_tty_stop (tty)
1046  *
1047  *     This routine notifies the tty driver that it should stop outputting
1048  *     characters to the tty device.
1049  */
1050 static void ircomm_tty_stop(struct tty_struct *tty)
1051 {
1052         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1053
1054         IRDA_ASSERT(self != NULL, return;);
1055         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1056
1057         ircomm_flow_request(self->ircomm, FLOW_STOP);
1058 }
1059
1060 /*
1061  * Function ircomm_check_modem_status (self)
1062  *
1063  *    Check for any changes in the DCE's line settings. This function should
1064  *    be called whenever the dce parameter settings changes, to update the
1065  *    flow control settings and other things
1066  */
1067 void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1068 {
1069         struct tty_struct *tty;
1070         int status;
1071
1072         IRDA_DEBUG(0, "%s()\n", __func__ );
1073
1074         IRDA_ASSERT(self != NULL, return;);
1075         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1076
1077         tty = self->tty;
1078
1079         status = self->settings.dce;
1080
1081         if (status & IRCOMM_DCE_DELTA_ANY) {
1082                 /*wake_up_interruptible(&self->delta_msr_wait);*/
1083         }
1084         if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1085                 IRDA_DEBUG(2,
1086                            "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
1087                            (status & IRCOMM_CD) ? "on" : "off");
1088
1089                 if (status & IRCOMM_CD) {
1090                         wake_up_interruptible(&self->open_wait);
1091                 } else {
1092                         IRDA_DEBUG(2,
1093                                    "%s(), Doing serial hangup..\n", __func__ );
1094                         if (tty)
1095                                 tty_hangup(tty);
1096
1097                         /* Hangup will remote the tty, so better break out */
1098                         return;
1099                 }
1100         }
1101         if (self->flags & ASYNC_CTS_FLOW) {
1102                 if (tty->hw_stopped) {
1103                         if (status & IRCOMM_CTS) {
1104                                 IRDA_DEBUG(2,
1105                                            "%s(), CTS tx start...\n", __func__ );
1106                                 tty->hw_stopped = 0;
1107
1108                                 /* Wake up processes blocked on open */
1109                                 wake_up_interruptible(&self->open_wait);
1110
1111                                 schedule_work(&self->tqueue);
1112                                 return;
1113                         }
1114                 } else {
1115                         if (!(status & IRCOMM_CTS)) {
1116                                 IRDA_DEBUG(2,
1117                                            "%s(), CTS tx stop...\n", __func__ );
1118                                 tty->hw_stopped = 1;
1119                         }
1120                 }
1121         }
1122 }
1123
1124 /*
1125  * Function ircomm_tty_data_indication (instance, sap, skb)
1126  *
1127  *    Handle incoming data, and deliver it to the line discipline
1128  *
1129  */
1130 static int ircomm_tty_data_indication(void *instance, void *sap,
1131                                       struct sk_buff *skb)
1132 {
1133         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1134         struct tty_ldisc *ld;
1135
1136         IRDA_DEBUG(2, "%s()\n", __func__ );
1137
1138         IRDA_ASSERT(self != NULL, return -1;);
1139         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1140         IRDA_ASSERT(skb != NULL, return -1;);
1141
1142         if (!self->tty) {
1143                 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
1144                 return 0;
1145         }
1146
1147         /*
1148          * If we receive data when hardware is stopped then something is wrong.
1149          * We try to poll the peers line settings to check if we are up todate.
1150          * Devices like WinCE can do this, and since they don't send any
1151          * params, we can just as well declare the hardware for running.
1152          */
1153         if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
1154                 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
1155                 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1156
1157                 /* We can just as well declare the hardware for running */
1158                 ircomm_tty_send_initial_parameters(self);
1159                 ircomm_tty_link_established(self);
1160         }
1161
1162         /*
1163          * Just give it over to the line discipline. There is no need to
1164          * involve the flip buffers, since we are not running in an interrupt
1165          * handler
1166          */
1167
1168         ld = tty_ldisc_ref(self->tty);
1169         if (ld)
1170                 ld->ops->receive_buf(self->tty, skb->data, NULL, skb->len);
1171         tty_ldisc_deref(ld);
1172
1173         /* No need to kfree_skb - see ircomm_ttp_data_indication() */
1174
1175         return 0;
1176 }
1177
1178 /*
1179  * Function ircomm_tty_control_indication (instance, sap, skb)
1180  *
1181  *    Parse all incoming parameters (easy!)
1182  *
1183  */
1184 static int ircomm_tty_control_indication(void *instance, void *sap,
1185                                          struct sk_buff *skb)
1186 {
1187         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1188         int clen;
1189
1190         IRDA_DEBUG(4, "%s()\n", __func__ );
1191
1192         IRDA_ASSERT(self != NULL, return -1;);
1193         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1194         IRDA_ASSERT(skb != NULL, return -1;);
1195
1196         clen = skb->data[0];
1197
1198         irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
1199                                &ircomm_param_info);
1200
1201         /* No need to kfree_skb - see ircomm_control_indication() */
1202
1203         return 0;
1204 }
1205
1206 /*
1207  * Function ircomm_tty_flow_indication (instance, sap, cmd)
1208  *
1209  *    This function is called by IrTTP when it wants us to slow down the
1210  *    transmission of data. We just mark the hardware as stopped, and wait
1211  *    for IrTTP to notify us that things are OK again.
1212  */
1213 static void ircomm_tty_flow_indication(void *instance, void *sap,
1214                                        LOCAL_FLOW cmd)
1215 {
1216         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1217         struct tty_struct *tty;
1218
1219         IRDA_ASSERT(self != NULL, return;);
1220         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1221
1222         tty = self->tty;
1223
1224         switch (cmd) {
1225         case FLOW_START:
1226                 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
1227                 tty->hw_stopped = 0;
1228
1229                 /* ircomm_tty_do_softint will take care of the rest */
1230                 schedule_work(&self->tqueue);
1231                 break;
1232         default:  /* If we get here, something is very wrong, better stop */
1233         case FLOW_STOP:
1234                 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
1235                 tty->hw_stopped = 1;
1236                 break;
1237         }
1238         self->flow = cmd;
1239 }
1240
1241 #ifdef CONFIG_PROC_FS
1242 static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
1243 {
1244         char sep;
1245
1246         seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
1247
1248         seq_puts(m, "Service type: ");
1249         if (self->service_type & IRCOMM_9_WIRE)
1250                 seq_puts(m, "9_WIRE");
1251         else if (self->service_type & IRCOMM_3_WIRE)
1252                 seq_puts(m, "3_WIRE");
1253         else if (self->service_type & IRCOMM_3_WIRE_RAW)
1254                 seq_puts(m, "3_WIRE_RAW");
1255         else
1256                 seq_puts(m, "No common service type!\n");
1257         seq_putc(m, '\n');
1258
1259         seq_printf(m, "Port name: %s\n", self->settings.port_name);
1260
1261         seq_printf(m, "DTE status:");
1262         sep = ' ';
1263         if (self->settings.dte & IRCOMM_RTS) {
1264                 seq_printf(m, "%cRTS", sep);
1265                 sep = '|';
1266         }
1267         if (self->settings.dte & IRCOMM_DTR) {
1268                 seq_printf(m, "%cDTR", sep);
1269                 sep = '|';
1270         }
1271         seq_putc(m, '\n');
1272
1273         seq_puts(m, "DCE status:");
1274         sep = ' ';
1275         if (self->settings.dce & IRCOMM_CTS) {
1276                 seq_printf(m, "%cCTS", sep);
1277                 sep = '|';
1278         }
1279         if (self->settings.dce & IRCOMM_DSR) {
1280                 seq_printf(m, "%cDSR", sep);
1281                 sep = '|';
1282         }
1283         if (self->settings.dce & IRCOMM_CD) {
1284                 seq_printf(m, "%cCD", sep);
1285                 sep = '|';
1286         }
1287         if (self->settings.dce & IRCOMM_RI) {
1288                 seq_printf(m, "%cRI", sep);
1289                 sep = '|';
1290         }
1291         seq_putc(m, '\n');
1292
1293         seq_puts(m, "Configuration: ");
1294         if (!self->settings.null_modem)
1295                 seq_puts(m, "DTE <-> DCE\n");
1296         else
1297                 seq_puts(m, "DTE <-> DTE (null modem emulation)\n");
1298
1299         seq_printf(m, "Data rate: %d\n", self->settings.data_rate);
1300
1301         seq_puts(m, "Flow control:");
1302         sep = ' ';
1303         if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
1304                 seq_printf(m, "%cXON_XOFF_IN", sep);
1305                 sep = '|';
1306         }
1307         if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
1308                 seq_printf(m, "%cXON_XOFF_OUT", sep);
1309                 sep = '|';
1310         }
1311         if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
1312                 seq_printf(m, "%cRTS_CTS_IN", sep);
1313                 sep = '|';
1314         }
1315         if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
1316                 seq_printf(m, "%cRTS_CTS_OUT", sep);
1317                 sep = '|';
1318         }
1319         if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
1320                 seq_printf(m, "%cDSR_DTR_IN", sep);
1321                 sep = '|';
1322         }
1323         if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
1324                 seq_printf(m, "%cDSR_DTR_OUT", sep);
1325                 sep = '|';
1326         }
1327         if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
1328                 seq_printf(m, "%cENQ_ACK_IN", sep);
1329                 sep = '|';
1330         }
1331         if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
1332                 seq_printf(m, "%cENQ_ACK_OUT", sep);
1333                 sep = '|';
1334         }
1335         seq_putc(m, '\n');
1336
1337         seq_puts(m, "Flags:");
1338         sep = ' ';
1339         if (self->flags & ASYNC_CTS_FLOW) {
1340                 seq_printf(m, "%cASYNC_CTS_FLOW", sep);
1341                 sep = '|';
1342         }
1343         if (self->flags & ASYNC_CHECK_CD) {
1344                 seq_printf(m, "%cASYNC_CHECK_CD", sep);
1345                 sep = '|';
1346         }
1347         if (self->flags & ASYNC_INITIALIZED) {
1348                 seq_printf(m, "%cASYNC_INITIALIZED", sep);
1349                 sep = '|';
1350         }
1351         if (self->flags & ASYNC_LOW_LATENCY) {
1352                 seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
1353                 sep = '|';
1354         }
1355         if (self->flags & ASYNC_CLOSING) {
1356                 seq_printf(m, "%cASYNC_CLOSING", sep);
1357                 sep = '|';
1358         }
1359         if (self->flags & ASYNC_NORMAL_ACTIVE) {
1360                 seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
1361                 sep = '|';
1362         }
1363         seq_putc(m, '\n');
1364
1365         seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
1366         seq_printf(m, "Open count: %d\n", self->open_count);
1367         seq_printf(m, "Max data size: %d\n", self->max_data_size);
1368         seq_printf(m, "Max header size: %d\n", self->max_header_size);
1369
1370         if (self->tty)
1371                 seq_printf(m, "Hardware: %s\n",
1372                                self->tty->hw_stopped ? "Stopped" : "Running");
1373 }
1374
1375 static int ircomm_tty_proc_show(struct seq_file *m, void *v)
1376 {
1377         struct ircomm_tty_cb *self;
1378         unsigned long flags;
1379
1380         spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1381
1382         self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1383         while (self != NULL) {
1384                 if (self->magic != IRCOMM_TTY_MAGIC)
1385                         break;
1386
1387                 ircomm_tty_line_info(self, m);
1388                 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1389         }
1390         spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
1391         return 0;
1392 }
1393
1394 static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
1395 {
1396         return single_open(file, ircomm_tty_proc_show, NULL);
1397 }
1398
1399 static const struct file_operations ircomm_tty_proc_fops = {
1400         .owner          = THIS_MODULE,
1401         .open           = ircomm_tty_proc_open,
1402         .read           = seq_read,
1403         .llseek         = seq_lseek,
1404         .release        = single_release,
1405 };
1406 #endif /* CONFIG_PROC_FS */
1407
1408 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1409 MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1410 MODULE_LICENSE("GPL");
1411 MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1412
1413 module_init(ircomm_tty_init);
1414 module_exit(ircomm_tty_cleanup);