mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Pfaff <tpfaff@pcs.com>
To: linux-kernel@vger.kernel.org
Subject: Question about TTY_DO_WRITE_WAKEUP
Date: Thu, 20 Nov 2008 11:06:19 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.64.0811201007120.30713@lxtpfaff.pcs.ditec.de> (raw)

I have written a program that reads and writes data to the serial uart using 
async io.
Unfortunately it does not work properly because a SIGIO is never gerated when 
output becomes possible, the reason is that TTY_DO_WRITE_WAKEUP is not set for 
most of the tty drivers. After i set the bit in serial_core.c the program works 
as expected.
Now i wonder why TTY_DO_WRITE_WAKEUP is almost always disabled ?

Below is a test case.

Thank you in advance,

Thomas

#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <termios.h>
#include <errno.h>

static const char outbuf [] = "01234567890abcdef\n";

static void sigio_handler (int sig __attribute__((unused)))
{
}

static int sigact (int sig, void (*handler)(int))
{	
	struct sigaction sa;

	memset (&sa, 0, sizeof (sa));

	sa.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP;
	sigfillset (&sa.sa_mask);
	sa.sa_handler = handler;

	return sigaction (sig, &sa, NULL);
}

static int uart_set_termio (int fd)
{
	struct termios term;

	if (tcgetattr (fd, &term) == -1)
		return -1;

	term.c_cflag = B19200 | CS8 | CREAD | CLOCAL;
	term.c_iflag = IGNPAR;
	term.c_oflag = 0;
	term.c_lflag = 0;

	term.c_cc [VTIME] = 0;
	term.c_cc [VMIN] = 0;

	return tcsetattr (fd, TCSAFLUSH, &term);
}

static int uart_fd_init (int fd)
{
	if (fcntl (fd, F_SETSIG, 0) == -1 ||
			fcntl (fd, F_SETOWN, getpid ()) == -1)
		return -1;

	return fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) | O_ASYNC | O_NONBLOCK);
}

static int uart_sig_init (void)
{
	sigset_t sigset;

	if (sigact (SIGIO, sigio_handler) == -1)
		return -1;

	if (sigemptyset (&sigset) == -1 ||
			sigaddset (&sigset, SIGIO) == -1)
		return -1;

	return sigprocmask (SIG_BLOCK, &sigset, NULL);
}

int main (int argc, char *argv [])
{
	int uart_fd;
	sigset_t sigmask;
	const int outbuflen = strlen (outbuf);
	const char *dev = "/dev/ttyS0";

	if (argc > 1)
		dev = argv [1];

	if (uart_sig_init () == -1)
		return EXIT_FAILURE;

	uart_fd = open (dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
	if (uart_fd == -1)
		return EXIT_FAILURE;

	if (uart_set_termio (uart_fd) == -1 ||
			uart_fd_init (uart_fd) == -1)
		return EXIT_FAILURE;

	if (sigprocmask (SIG_SETMASK, NULL, &sigmask) == -1 ||
			sigdelset (&sigmask, SIGIO) == -1)
		return EXIT_FAILURE;

	while (1)
	{
		int written;
		int towrite = outbuflen;
		const char *buf = outbuf;

		while (towrite)
		{
			written = write (uart_fd, buf, towrite);
			if (written == -1)
			{
				if (errno == EAGAIN)
				{
					sigsuspend (&sigmask);
					continue;
				}
				else
					return EXIT_FAILURE;
			}

			buf += written;
			towrite -= written;
		}
	}

	return EXIT_SUCCESS;
}

             reply	other threads:[~2008-11-20 10:17 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-20 10:06 Thomas Pfaff [this message]
2008-11-20 13:17 ` Alan Cox
2008-11-20 14:09   ` Thomas Pfaff
2008-11-20 14:31     ` Alan Cox
2008-11-20 15:12       ` Thomas Pfaff
2008-11-20 15:55         ` Alan Cox
2008-11-20 16:39           ` Thomas Pfaff
2008-11-20 16:47             ` Alan Cox
2008-11-20 16:58             ` Alan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0811201007120.30713@lxtpfaff.pcs.ditec.de \
    --to=tpfaff@pcs.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®