1 /*********************************************************************
3 * Filename: ircomm_tty.c
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
13 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
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.
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.
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,
31 ********************************************************************/
33 #include <linux/init.h>
34 #include <linux/module.h>
36 #include <linux/sched.h>
37 #include <linux/termios.h>
38 #include <linux/tty.h>
39 #include <linux/interrupt.h>
40 #include <linux/device.h> /* for MODULE_ALIAS_CHARDEV_MAJOR */
42 #include <asm/uaccess.h>
44 #include <net/irda/irda.h>
45 #include <net/irda/irmod.h>
47 #include <net/irda/ircomm_core.h>
48 #include <net/irda/ircomm_param.h>
49 #include <net/irda/ircomm_tty_attach.h>
50 #include <net/irda/ircomm_tty.h>
52 static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);
53 static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
54 static int ircomm_tty_write(struct tty_struct * tty,
55 const unsigned char *buf, int count);
56 static int ircomm_tty_write_room(struct tty_struct *tty);
57 static void ircomm_tty_throttle(struct tty_struct *tty);
58 static void ircomm_tty_unthrottle(struct tty_struct *tty);
59 static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);
60 static void ircomm_tty_flush_buffer(struct tty_struct *tty);
61 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
62 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
63 static void ircomm_tty_hangup(struct tty_struct *tty);
64 static void ircomm_tty_do_softint(struct work_struct *work);
65 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
66 static void ircomm_tty_stop(struct tty_struct *tty);
68 static int ircomm_tty_data_indication(void *instance, void *sap,
70 static int ircomm_tty_control_indication(void *instance, void *sap,
72 static void ircomm_tty_flow_indication(void *instance, void *sap,
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;
80 static hashbin_t *ircomm_tty = NULL;
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 .read_proc = ircomm_tty_read_proc,
102 #endif /* CONFIG_PROC_FS */
106 * Function ircomm_tty_init()
108 * Init IrCOMM TTY layer/driver
111 static int __init ircomm_tty_init(void)
113 driver = alloc_tty_driver(IRCOMM_TTY_PORTS);
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);
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",
137 put_tty_driver(driver);
143 static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
145 IRDA_DEBUG(0, "%s()\n", __func__ );
147 IRDA_ASSERT(self != NULL, return;);
148 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
150 ircomm_tty_shutdown(self);
157 * Function ircomm_tty_cleanup ()
159 * Remove IrCOMM TTY layer/driver
162 static void __exit ircomm_tty_cleanup(void)
166 IRDA_DEBUG(4, "%s()\n", __func__ );
168 ret = tty_unregister_driver(driver);
170 IRDA_ERROR("%s(), failed to unregister driver\n",
175 hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
176 put_tty_driver(driver);
180 * Function ircomm_startup (self)
185 static int ircomm_tty_startup(struct ircomm_tty_cb *self)
190 IRDA_DEBUG(2, "%s()\n", __func__ );
192 IRDA_ASSERT(self != NULL, return -1;);
193 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
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__ );
201 /* Register with IrCOMM */
202 irda_notify_init(¬ify);
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;
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;
216 self->ircomm = ircomm_open(¬ify, self->service_type,
222 self->slsap_sel = self->ircomm->slsap_sel;
224 /* Connect IrCOMM link with remote device */
225 ret = ircomm_tty_attach_cable(self);
227 IRDA_ERROR("%s(), error attaching cable!\n", __func__);
233 clear_bit(ASYNC_B_INITIALIZED, &self->flags);
238 * Function ircomm_block_til_ready (self, filp)
243 static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
246 DECLARE_WAITQUEUE(wait, current);
248 int do_clocal = 0, extra_count = 0;
250 struct tty_struct *tty;
252 IRDA_DEBUG(2, "%s()\n", __func__ );
257 * If non-blocking mode is set, or the port is not enabled,
258 * then make the check up front and then exit.
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__ );
267 if (tty->termios->c_cflag & CLOCAL) {
268 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
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.
280 add_wait_queue(&self->open_wait, &wait);
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 );
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)) {
291 spin_unlock_irqrestore(&self->spinlock, flags);
292 self->blocked_open++;
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).
300 self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
302 ircomm_param_request(self, IRCOMM_DTE, TRUE);
305 current->state = TASK_INTERRUPTIBLE;
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;
315 * Check if link is ready now. Even if CLOCAL is
316 * specified, we cannot return before the IrCOMM link is
319 if (!test_bit(ASYNC_B_CLOSING, &self->flags) &&
320 (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
321 self->state == IRCOMM_TTY_READY)
326 if (signal_pending(current)) {
327 retval = -ERESTARTSYS;
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 );
337 __set_current_state(TASK_RUNNING);
338 remove_wait_queue(&self->open_wait, &wait);
341 /* ++ is not atomic, so this should be protected - Jean II */
342 spin_lock_irqsave(&self->spinlock, flags);
344 spin_unlock_irqrestore(&self->spinlock, flags);
346 self->blocked_open--;
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);
352 self->flags |= ASYNC_NORMAL_ACTIVE;
358 * Function ircomm_tty_open (tty, filp)
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.
364 static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
366 struct ircomm_tty_cb *self;
371 IRDA_DEBUG(2, "%s()\n", __func__ );
374 if ((line < 0) || (line >= IRCOMM_TTY_PORTS)) {
378 /* Check if instance already exists */
379 self = hashbin_lock_find(ircomm_tty, line, NULL);
381 /* No, so make new instance */
382 self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
384 IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
388 self->magic = IRCOMM_TTY_MAGIC;
389 self->flow = FLOW_STOP;
392 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
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;
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);
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.
409 tty->termios->c_iflag = 0;
410 tty->termios->c_oflag = 0;
412 /* Insert into hash */
413 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
415 /* ++ is not atomic, so this should be protected - Jean II */
416 spin_lock_irqsave(&self->spinlock, flags);
419 tty->driver_data = self;
421 spin_unlock_irqrestore(&self->spinlock, flags);
423 IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
424 self->line, self->open_count);
426 /* Not really used by us, but lets do it anyway */
427 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
430 * If the port is the middle of closing, bail out now
432 if (tty_hung_up_p(filp) ||
433 test_bit(ASYNC_B_CLOSING, &self->flags)) {
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?
443 if (wait_event_interruptible(self->close_wait, !test_bit(ASYNC_B_CLOSING, &self->flags))) {
444 IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
449 #ifdef SERIAL_DO_RESTART
450 return ((self->flags & ASYNC_HUP_NOTIFY) ?
451 -EAGAIN : -ERESTARTSYS);
457 /* Check if this is a "normal" ircomm device, or an irlpt device */
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", __func__ );
465 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
466 self->service_type = IRCOMM_3_WIRE_RAW;
467 self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
470 ret = ircomm_tty_startup(self);
474 ret = ircomm_tty_block_til_ready(self, filp);
477 "%s(), returning after block_til_ready with %d\n", __func__ ,
486 * Function ircomm_tty_close (tty, filp)
488 * This routine is called when a particular tty device is closed.
491 static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
493 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
496 IRDA_DEBUG(0, "%s()\n", __func__ );
501 IRDA_ASSERT(self != NULL, return;);
502 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
504 spin_lock_irqsave(&self->spinlock, flags);
506 if (tty_hung_up_p(filp)) {
507 spin_unlock_irqrestore(&self->spinlock, flags);
509 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
513 if ((tty->count == 1) && (self->open_count != 1)) {
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.
521 IRDA_DEBUG(0, "%s(), bad serial port count; "
522 "tty->count is 1, state->count is %d\n", __func__ ,
524 self->open_count = 1;
527 if (--self->open_count < 0) {
528 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
529 __func__, self->line, self->open_count);
530 self->open_count = 0;
532 if (self->open_count) {
533 spin_unlock_irqrestore(&self->spinlock, flags);
535 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
539 /* Hum... Should be test_and_set_bit ??? - Jean II */
540 set_bit(ASYNC_B_CLOSING, &self->flags);
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);
549 * Now we wait for the transmit buffer to clear; and we notify
550 * the line discipline to only process XON/XOFF characters.
553 if (self->closing_wait != ASYNC_CLOSING_WAIT_NONE)
554 tty_wait_until_sent(tty, self->closing_wait);
556 ircomm_tty_shutdown(self);
558 tty_driver_flush_buffer(tty);
559 tty_ldisc_flush(tty);
564 if (self->blocked_open) {
565 if (self->close_delay)
566 schedule_timeout_interruptible(self->close_delay);
567 wake_up_interruptible(&self->open_wait);
570 self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
571 wake_up_interruptible(&self->close_wait);
575 * Function ircomm_tty_flush_buffer (tty)
580 static void ircomm_tty_flush_buffer(struct tty_struct *tty)
582 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
584 IRDA_ASSERT(self != NULL, return;);
585 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
588 * Let do_softint() do this to avoid race condition with
591 schedule_work(&self->tqueue);
595 * Function ircomm_tty_do_softint (work)
597 * We use this routine to give the write wakeup to the user at at a
598 * safe time (as fast as possible after write have completed). This
599 * can be compared to the Tx interrupt.
601 static void ircomm_tty_do_softint(struct work_struct *work)
603 struct ircomm_tty_cb *self =
604 container_of(work, struct ircomm_tty_cb, tqueue);
605 struct tty_struct *tty;
607 struct sk_buff *skb, *ctrl_skb;
609 IRDA_DEBUG(2, "%s()\n", __func__ );
611 if (!self || self->magic != IRCOMM_TTY_MAGIC)
618 /* Unlink control buffer */
619 spin_lock_irqsave(&self->spinlock, flags);
621 ctrl_skb = self->ctrl_skb;
622 self->ctrl_skb = NULL;
624 spin_unlock_irqrestore(&self->spinlock, flags);
626 /* Flush control buffer if any */
628 if(self->flow == FLOW_START)
629 ircomm_control_request(self->ircomm, ctrl_skb);
630 /* Drop reference count - see ircomm_ttp_data_request(). */
631 dev_kfree_skb(ctrl_skb);
637 /* Unlink transmit buffer */
638 spin_lock_irqsave(&self->spinlock, flags);
643 spin_unlock_irqrestore(&self->spinlock, flags);
645 /* Flush transmit buffer if any */
647 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
648 /* Drop reference count - see ircomm_ttp_data_request(). */
652 /* Check if user (still) wants to be waken up */
653 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
654 tty->ldisc.write_wakeup)
656 (tty->ldisc.write_wakeup)(tty);
658 wake_up_interruptible(&tty->write_wait);
662 * Function ircomm_tty_write (tty, buf, count)
664 * This routine is called by the kernel to write a series of characters
665 * to the tty device. The characters may come from user space or kernel
666 * space. This routine will return the number of characters actually
667 * accepted for writing. This routine is mandatory.
669 static int ircomm_tty_write(struct tty_struct *tty,
670 const unsigned char *buf, int count)
672 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
679 IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
682 IRDA_ASSERT(self != NULL, return -1;);
683 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
685 /* We may receive packets from the TTY even before we have finished
686 * our setup. Not cool.
687 * The problem is that we don't know the final header and data size
688 * to create the proper skb, so any skb we would create would have
689 * bogus header and data size, so need care.
690 * We use a bogus header size to safely detect this condition.
691 * Another problem is that hw_stopped was set to 0 way before it
692 * should be, so we would drop this skb. It should now be fixed.
693 * One option is to not accept data until we are properly setup.
694 * But, I suspect that when it happens, the ppp line discipline
695 * just "drops" the data, which might screw up connect scripts.
696 * The second option is to create a "safe skb", with large header
697 * and small size (see ircomm_tty_open() for values).
698 * We just need to make sure that when the real values get filled,
699 * we don't mess up the original "safe skb" (see tx_data_size).
701 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
702 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
703 #ifdef IRCOMM_NO_TX_BEFORE_INIT
704 /* We didn't consume anything, TTY will retry */
712 /* Protect our manipulation of self->tx_skb and related */
713 spin_lock_irqsave(&self->spinlock, flags);
715 /* Fetch current transmit buffer */
719 * Send out all the data we get, possibly as multiple fragmented
720 * frames, but this will only happen if the data is larger than the
721 * max data size. The normal case however is just the opposite, and
722 * this function may be called multiple times, and will then actually
723 * defragment the data and send it out as one packet as soon as
724 * possible, but at a safer point in time
729 /* Adjust data size to the max data size */
730 if (size > self->max_data_size)
731 size = self->max_data_size;
734 * Do we already have a buffer ready for transmit, or do
735 * we need to allocate a new frame
739 * Any room for more data at the end of the current
740 * transmit buffer? Cannot use skb_tailroom, since
741 * dev_alloc_skb gives us a larger skb than we
743 * Note : use tx_data_size, because max_data_size
744 * may have changed and we don't want to overwrite
747 if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
748 /* Adjust data to tailroom */
753 * Current transmit frame is full, so break
754 * out, so we can send it as soon as possible
759 /* Prepare a full sized frame */
760 skb = alloc_skb(self->max_data_size+
761 self->max_header_size,
764 spin_unlock_irqrestore(&self->spinlock, flags);
767 skb_reserve(skb, self->max_header_size);
769 /* Remember skb size because max_data_size may
770 * change later on - Jean II */
771 self->tx_data_size = self->max_data_size;
775 memcpy(skb_put(skb,size), buf + len, size);
781 spin_unlock_irqrestore(&self->spinlock, flags);
784 * Schedule a new thread which will transmit the frame as soon
785 * as possible, but at a safe point in time. We do this so the
786 * "user" can give us data multiple times, as PPP does (because of
787 * its 256 byte tx buffer). We will then defragment and send out
788 * all this data as one single packet.
790 schedule_work(&self->tqueue);
796 * Function ircomm_tty_write_room (tty)
798 * This routine returns the numbers of characters the tty driver will
799 * accept for queuing to be written. This number is subject to change as
800 * output buffers get emptied, or if the output flow control is acted.
802 static int ircomm_tty_write_room(struct tty_struct *tty)
804 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
808 IRDA_ASSERT(self != NULL, return -1;);
809 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
811 #ifdef IRCOMM_NO_TX_BEFORE_INIT
812 /* max_header_size tells us if the channel is initialised or not. */
813 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
814 /* Don't bother us yet */
818 /* Check if we are allowed to transmit any data.
819 * hw_stopped is the regular flow control.
824 spin_lock_irqsave(&self->spinlock, flags);
826 ret = self->tx_data_size - self->tx_skb->len;
828 ret = self->max_data_size;
829 spin_unlock_irqrestore(&self->spinlock, flags);
831 IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
837 * Function ircomm_tty_wait_until_sent (tty, timeout)
839 * This routine waits until the device has written out all of the
840 * characters in its transmitter FIFO.
842 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
844 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
845 unsigned long orig_jiffies, poll_time;
848 IRDA_DEBUG(2, "%s()\n", __func__ );
850 IRDA_ASSERT(self != NULL, return;);
851 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
853 orig_jiffies = jiffies;
855 /* Set poll time to 200 ms */
856 poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
858 spin_lock_irqsave(&self->spinlock, flags);
859 while (self->tx_skb && self->tx_skb->len) {
860 spin_unlock_irqrestore(&self->spinlock, flags);
861 schedule_timeout_interruptible(poll_time);
862 spin_lock_irqsave(&self->spinlock, flags);
863 if (signal_pending(current))
865 if (timeout && time_after(jiffies, orig_jiffies + timeout))
868 spin_unlock_irqrestore(&self->spinlock, flags);
869 current->state = TASK_RUNNING;
873 * Function ircomm_tty_throttle (tty)
875 * This routine notifies the tty driver that input buffers for the line
876 * discipline are close to full, and it should somehow signal that no
877 * more characters should be sent to the tty.
879 static void ircomm_tty_throttle(struct tty_struct *tty)
881 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
883 IRDA_DEBUG(2, "%s()\n", __func__ );
885 IRDA_ASSERT(self != NULL, return;);
886 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
888 /* Software flow control? */
890 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
892 /* Hardware flow control? */
893 if (tty->termios->c_cflag & CRTSCTS) {
894 self->settings.dte &= ~IRCOMM_RTS;
895 self->settings.dte |= IRCOMM_DELTA_RTS;
897 ircomm_param_request(self, IRCOMM_DTE, TRUE);
900 ircomm_flow_request(self->ircomm, FLOW_STOP);
904 * Function ircomm_tty_unthrottle (tty)
906 * This routine notifies the tty drivers that it should signals that
907 * characters can now be sent to the tty without fear of overrunning the
908 * input buffers of the line disciplines.
910 static void ircomm_tty_unthrottle(struct tty_struct *tty)
912 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
914 IRDA_DEBUG(2, "%s()\n", __func__ );
916 IRDA_ASSERT(self != NULL, return;);
917 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
919 /* Using software flow control? */
921 ircomm_tty_send_xchar(tty, START_CHAR(tty));
924 /* Using hardware flow control? */
925 if (tty->termios->c_cflag & CRTSCTS) {
926 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
928 ircomm_param_request(self, IRCOMM_DTE, TRUE);
929 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
931 ircomm_flow_request(self->ircomm, FLOW_START);
935 * Function ircomm_tty_chars_in_buffer (tty)
937 * Indicates if there are any data in the buffer
940 static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
942 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
946 IRDA_ASSERT(self != NULL, return -1;);
947 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
949 spin_lock_irqsave(&self->spinlock, flags);
952 len = self->tx_skb->len;
954 spin_unlock_irqrestore(&self->spinlock, flags);
959 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
963 IRDA_ASSERT(self != NULL, return;);
964 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
966 IRDA_DEBUG(0, "%s()\n", __func__ );
968 if (!test_and_clear_bit(ASYNC_B_INITIALIZED, &self->flags))
971 ircomm_tty_detach_cable(self);
973 spin_lock_irqsave(&self->spinlock, flags);
975 del_timer(&self->watchdog_timer);
977 /* Free parameter buffer */
978 if (self->ctrl_skb) {
979 dev_kfree_skb(self->ctrl_skb);
980 self->ctrl_skb = NULL;
983 /* Free transmit buffer */
985 dev_kfree_skb(self->tx_skb);
990 ircomm_close(self->ircomm);
994 spin_unlock_irqrestore(&self->spinlock, flags);
998 * Function ircomm_tty_hangup (tty)
1000 * This routine notifies the tty driver that it should hangup the tty
1004 static void ircomm_tty_hangup(struct tty_struct *tty)
1006 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1007 unsigned long flags;
1009 IRDA_DEBUG(0, "%s()\n", __func__ );
1011 IRDA_ASSERT(self != NULL, return;);
1012 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1017 /* ircomm_tty_flush_buffer(tty); */
1018 ircomm_tty_shutdown(self);
1020 /* I guess we need to lock here - Jean II */
1021 spin_lock_irqsave(&self->spinlock, flags);
1022 self->flags &= ~ASYNC_NORMAL_ACTIVE;
1024 self->open_count = 0;
1025 spin_unlock_irqrestore(&self->spinlock, flags);
1027 wake_up_interruptible(&self->open_wait);
1031 * Function ircomm_tty_send_xchar (tty, ch)
1033 * This routine is used to send a high-priority XON/XOFF character to
1036 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1038 IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
1042 * Function ircomm_tty_start (tty)
1044 * This routine notifies the tty driver that it resume sending
1045 * characters to the tty device.
1047 void ircomm_tty_start(struct tty_struct *tty)
1049 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1051 ircomm_flow_request(self->ircomm, FLOW_START);
1055 * Function ircomm_tty_stop (tty)
1057 * This routine notifies the tty driver that it should stop outputting
1058 * characters to the tty device.
1060 static void ircomm_tty_stop(struct tty_struct *tty)
1062 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1064 IRDA_ASSERT(self != NULL, return;);
1065 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1067 ircomm_flow_request(self->ircomm, FLOW_STOP);
1071 * Function ircomm_check_modem_status (self)
1073 * Check for any changes in the DCE's line settings. This function should
1074 * be called whenever the dce parameter settings changes, to update the
1075 * flow control settings and other things
1077 void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1079 struct tty_struct *tty;
1082 IRDA_DEBUG(0, "%s()\n", __func__ );
1084 IRDA_ASSERT(self != NULL, return;);
1085 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1089 status = self->settings.dce;
1091 if (status & IRCOMM_DCE_DELTA_ANY) {
1092 /*wake_up_interruptible(&self->delta_msr_wait);*/
1094 if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1096 "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
1097 (status & IRCOMM_CD) ? "on" : "off");
1099 if (status & IRCOMM_CD) {
1100 wake_up_interruptible(&self->open_wait);
1103 "%s(), Doing serial hangup..\n", __func__ );
1107 /* Hangup will remote the tty, so better break out */
1111 if (self->flags & ASYNC_CTS_FLOW) {
1112 if (tty->hw_stopped) {
1113 if (status & IRCOMM_CTS) {
1115 "%s(), CTS tx start...\n", __func__ );
1116 tty->hw_stopped = 0;
1118 /* Wake up processes blocked on open */
1119 wake_up_interruptible(&self->open_wait);
1121 schedule_work(&self->tqueue);
1125 if (!(status & IRCOMM_CTS)) {
1127 "%s(), CTS tx stop...\n", __func__ );
1128 tty->hw_stopped = 1;
1135 * Function ircomm_tty_data_indication (instance, sap, skb)
1137 * Handle incoming data, and deliver it to the line discipline
1140 static int ircomm_tty_data_indication(void *instance, void *sap,
1141 struct sk_buff *skb)
1143 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1145 IRDA_DEBUG(2, "%s()\n", __func__ );
1147 IRDA_ASSERT(self != NULL, return -1;);
1148 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1149 IRDA_ASSERT(skb != NULL, return -1;);
1152 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
1157 * If we receive data when hardware is stopped then something is wrong.
1158 * We try to poll the peers line settings to check if we are up todate.
1159 * Devices like WinCE can do this, and since they don't send any
1160 * params, we can just as well declare the hardware for running.
1162 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
1163 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
1164 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1166 /* We can just as well declare the hardware for running */
1167 ircomm_tty_send_initial_parameters(self);
1168 ircomm_tty_link_established(self);
1172 * Just give it over to the line discipline. There is no need to
1173 * involve the flip buffers, since we are not running in an interrupt
1176 self->tty->ldisc.receive_buf(self->tty, skb->data, NULL, skb->len);
1178 /* No need to kfree_skb - see ircomm_ttp_data_indication() */
1184 * Function ircomm_tty_control_indication (instance, sap, skb)
1186 * Parse all incoming parameters (easy!)
1189 static int ircomm_tty_control_indication(void *instance, void *sap,
1190 struct sk_buff *skb)
1192 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1195 IRDA_DEBUG(4, "%s()\n", __func__ );
1197 IRDA_ASSERT(self != NULL, return -1;);
1198 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1199 IRDA_ASSERT(skb != NULL, return -1;);
1201 clen = skb->data[0];
1203 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
1204 &ircomm_param_info);
1206 /* No need to kfree_skb - see ircomm_control_indication() */
1212 * Function ircomm_tty_flow_indication (instance, sap, cmd)
1214 * This function is called by IrTTP when it wants us to slow down the
1215 * transmission of data. We just mark the hardware as stopped, and wait
1216 * for IrTTP to notify us that things are OK again.
1218 static void ircomm_tty_flow_indication(void *instance, void *sap,
1221 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1222 struct tty_struct *tty;
1224 IRDA_ASSERT(self != NULL, return;);
1225 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1231 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
1232 tty->hw_stopped = 0;
1234 /* ircomm_tty_do_softint will take care of the rest */
1235 schedule_work(&self->tqueue);
1237 default: /* If we get here, something is very wrong, better stop */
1239 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
1240 tty->hw_stopped = 1;
1246 #ifdef CONFIG_PROC_FS
1247 static int ircomm_tty_line_info(struct ircomm_tty_cb *self, char *buf)
1251 ret += sprintf(buf+ret, "State: %s\n", ircomm_tty_state[self->state]);
1253 ret += sprintf(buf+ret, "Service type: ");
1254 if (self->service_type & IRCOMM_9_WIRE)
1255 ret += sprintf(buf+ret, "9_WIRE");
1256 else if (self->service_type & IRCOMM_3_WIRE)
1257 ret += sprintf(buf+ret, "3_WIRE");
1258 else if (self->service_type & IRCOMM_3_WIRE_RAW)
1259 ret += sprintf(buf+ret, "3_WIRE_RAW");
1261 ret += sprintf(buf+ret, "No common service type!\n");
1262 ret += sprintf(buf+ret, "\n");
1264 ret += sprintf(buf+ret, "Port name: %s\n", self->settings.port_name);
1266 ret += sprintf(buf+ret, "DTE status: ");
1267 if (self->settings.dte & IRCOMM_RTS)
1268 ret += sprintf(buf+ret, "RTS|");
1269 if (self->settings.dte & IRCOMM_DTR)
1270 ret += sprintf(buf+ret, "DTR|");
1271 if (self->settings.dte)
1272 ret--; /* remove the last | */
1273 ret += sprintf(buf+ret, "\n");
1275 ret += sprintf(buf+ret, "DCE status: ");
1276 if (self->settings.dce & IRCOMM_CTS)
1277 ret += sprintf(buf+ret, "CTS|");
1278 if (self->settings.dce & IRCOMM_DSR)
1279 ret += sprintf(buf+ret, "DSR|");
1280 if (self->settings.dce & IRCOMM_CD)
1281 ret += sprintf(buf+ret, "CD|");
1282 if (self->settings.dce & IRCOMM_RI)
1283 ret += sprintf(buf+ret, "RI|");
1284 if (self->settings.dce)
1285 ret--; /* remove the last | */
1286 ret += sprintf(buf+ret, "\n");
1288 ret += sprintf(buf+ret, "Configuration: ");
1289 if (!self->settings.null_modem)
1290 ret += sprintf(buf+ret, "DTE <-> DCE\n");
1292 ret += sprintf(buf+ret,
1293 "DTE <-> DTE (null modem emulation)\n");
1295 ret += sprintf(buf+ret, "Data rate: %d\n", self->settings.data_rate);
1297 ret += sprintf(buf+ret, "Flow control: ");
1298 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN)
1299 ret += sprintf(buf+ret, "XON_XOFF_IN|");
1300 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT)
1301 ret += sprintf(buf+ret, "XON_XOFF_OUT|");
1302 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN)
1303 ret += sprintf(buf+ret, "RTS_CTS_IN|");
1304 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT)
1305 ret += sprintf(buf+ret, "RTS_CTS_OUT|");
1306 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN)
1307 ret += sprintf(buf+ret, "DSR_DTR_IN|");
1308 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT)
1309 ret += sprintf(buf+ret, "DSR_DTR_OUT|");
1310 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN)
1311 ret += sprintf(buf+ret, "ENQ_ACK_IN|");
1312 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT)
1313 ret += sprintf(buf+ret, "ENQ_ACK_OUT|");
1314 if (self->settings.flow_control)
1315 ret--; /* remove the last | */
1316 ret += sprintf(buf+ret, "\n");
1318 ret += sprintf(buf+ret, "Flags: ");
1319 if (self->flags & ASYNC_CTS_FLOW)
1320 ret += sprintf(buf+ret, "ASYNC_CTS_FLOW|");
1321 if (self->flags & ASYNC_CHECK_CD)
1322 ret += sprintf(buf+ret, "ASYNC_CHECK_CD|");
1323 if (self->flags & ASYNC_INITIALIZED)
1324 ret += sprintf(buf+ret, "ASYNC_INITIALIZED|");
1325 if (self->flags & ASYNC_LOW_LATENCY)
1326 ret += sprintf(buf+ret, "ASYNC_LOW_LATENCY|");
1327 if (self->flags & ASYNC_CLOSING)
1328 ret += sprintf(buf+ret, "ASYNC_CLOSING|");
1329 if (self->flags & ASYNC_NORMAL_ACTIVE)
1330 ret += sprintf(buf+ret, "ASYNC_NORMAL_ACTIVE|");
1332 ret--; /* remove the last | */
1333 ret += sprintf(buf+ret, "\n");
1335 ret += sprintf(buf+ret, "Role: %s\n", self->client ?
1336 "client" : "server");
1337 ret += sprintf(buf+ret, "Open count: %d\n", self->open_count);
1338 ret += sprintf(buf+ret, "Max data size: %d\n", self->max_data_size);
1339 ret += sprintf(buf+ret, "Max header size: %d\n", self->max_header_size);
1342 ret += sprintf(buf+ret, "Hardware: %s\n",
1343 self->tty->hw_stopped ? "Stopped" : "Running");
1345 ret += sprintf(buf+ret, "\n");
1351 * Function ircomm_tty_read_proc (buf, start, offset, len, eof, unused)
1356 static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
1357 int *eof, void *unused)
1359 struct ircomm_tty_cb *self;
1362 unsigned long flags;
1364 spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1366 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1367 while ((self != NULL) && (count < 4000)) {
1368 if (self->magic != IRCOMM_TTY_MAGIC)
1371 l = ircomm_tty_line_info(self, buf + count);
1373 if (count+begin > offset+len)
1375 if (count+begin < offset) {
1380 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1384 spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
1386 if (offset >= count+begin)
1388 *start = buf + (offset-begin);
1389 return ((len < begin+count-offset) ? len : begin+count-offset);
1391 #endif /* CONFIG_PROC_FS */
1393 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1394 MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1395 MODULE_LICENSE("GPL");
1396 MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1398 module_init(ircomm_tty_init);
1399 module_exit(ircomm_tty_cleanup);