Subject: Problem with IOCTL calls, READ THIS!
To: None <current-users@sun-lamp.cs.berkeley.edu>
From: Robert Shady <rls@zeus.id.net>
List: current-users
Date: 03/08/1994 16:20:25
The following is information I had found in /usr/include/sys/ioctl.h, I then
then wrote the appended program to test things out for myself (remember, I 
the one trying to read the data carrier detect line on the serial ports...
Anyways, it looks as if by using the TIOCMGET call, I get the proper 
information back, but it seems to be off by one bit or so.  Run the
attached program, then try to dial out via your modem, and you will see 
what I mean.  It returns a TIOCM_RNG in place of the TIOCM_CAR, which I
*think* is wrong, but maybe it's just me.  This is running kernels from
about Feb 27 days ago.  Am I doing something wrong, or did something get
screwed while we were updating something...

#define	TIOCMODG	_IOR('t', 3, int)	/* get modem control state */
#define	TIOCMODS	_IOW('t', 4, int)	/* set modem control state */
#define	TIOCMSET	_IOW('t', 109, int)	/* set all modem bits */
#define	TIOCMBIS	_IOW('t', 108, int)	/* bis modem bits */
#define	TIOCMBIC	_IOW('t', 107, int)	/* bic modem bits */
#define	TIOCMGET	_IOR('t', 106, int)	/* get all modem bits */

#define		TIOCM_LE	0001		/* line enable */
#define		TIOCM_DTR	0002		/* data terminal ready */
#define		TIOCM_RTS	0004		/* request to send */
#define		TIOCM_ST	0010		/* secondary transmit */
#define		TIOCM_SR	0020		/* secondary receive */
#define		TIOCM_CTS	0040		/* clear to send */
#define		TIOCM_CAR	0100		/* carrier detect */
#define		TIOCM_CD	TIOCM_CAR
#define		TIOCM_RNG	0200		/* ring */
#define		TIOCM_RI	TIOCM_RNG
#define		TIOCM_DSR	0400		/* data set ready */

-- Cut here --

#include <stdio.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>

#define SIG_DCD		TIOCM_CAR
#define SIG_DTR		TIOCM_DTR

int		fd, mdmctl;

#define check(x)	if (mdmctl&x) putchar('*'); else putchar(' ');

void	printbits( int byte );

void	main( int argc, char **argv )
{
	if (argc < 2)
		{
		printf("Usage: checkdcd <device>\n");
		exit( 1 );
		}
	if ((fd = open( argv[1], O_RDONLY, 0 )) < 0)
		{
		printf("Error opening device '%s'\n", argv[1] );
		exit( 2 );
		}
	if (ioctl( fd, TIOCMGET, &mdmctl ) == -1)
		{
		printf("ERROR #%d: %s reading port information.\n", 
			errno, sys_errlist[errno] );
		close( fd );
		exit( 3 );
		}
	close( fd );

	check(TIOCM_LE ); printf("TIOCM_LE  = "); printbits( TIOCM_LE  );
	check(TIOCM_DTR); printf("TIOCM_DTR = "); printbits( TIOCM_DTR );
	check(TIOCM_RTS); printf("TIOCM_RTS = "); printbits( TIOCM_RTS );
	check(TIOCM_ST ); printf("TIOCM_ST  = "); printbits( TIOCM_ST  );
	check(TIOCM_SR ); printf("TIOCM_SR  = "); printbits( TIOCM_SR  );
	check(TIOCM_CTS); printf("TIOCM_CTS = "); printbits( TIOCM_CTS );
	check(TIOCM_CAR); printf("TIOCM_CAR = "); printbits( TIOCM_CAR );
	check(TIOCM_RNG); printf("TIOCM_RNG = "); printbits( TIOCM_RNG );
	check(TIOCM_DSR); printf("TIOCM_DSR = "); printbits( TIOCM_DSR );
    printf("             ==========\n");
	printf(" Modem ctl = "); printbits( mdmctl );
	printf("\n");

	if ((mdmctl & SIG_DCD) == SIG_DCD)
		{
		printf("Data carrier detect is present on %s.\n", argv[1] );
		}
	if ((mdmctl & SIG_DTR) == SIG_DTR)
		{
		printf("Data terminal ready signal is present on %s.\n", argv[1] );
		}
	close( fd );
}

void	printbits( int byte )
{
	int		i, bits[] = {256, 128, 64, 32, 16, 8, 4, 2, 1};

	for (i=0; i<=8; i++)
		{
		if ((byte & bits[i]) == bits[i])
			putchar('1');
		else
			putchar('0');
		}
	putchar('\n');
}


------------------------------------------------------------------------------