com.c revision 1.3
11.3Ssimonb/* $NetBSD: com.c,v 1.3 2003/10/08 01:27:22 simonb Exp $ */ 21.1Scdi 31.1Scdi/*- 41.1Scdi * Copyright (c) 1993, 1994, 1995, 1996, 1997 51.1Scdi * Charles M. Hannum. All rights reserved. 61.1Scdi * 71.1Scdi * Interrupt processing and hardware flow control partly based on code from 81.1Scdi * Onno van der Linden and Gordon Ross. 91.1Scdi * 101.1Scdi * Redistribution and use in source and binary forms, with or without 111.1Scdi * modification, are permitted provided that the following conditions 121.1Scdi * are met: 131.1Scdi * 1. Redistributions of source code must retain the above copyright 141.1Scdi * notice, this list of conditions and the following disclaimer. 151.1Scdi * 2. Redistributions in binary form must reproduce the above copyright 161.1Scdi * notice, this list of conditions and the following disclaimer in the 171.1Scdi * documentation and/or other materials provided with the distribution. 181.1Scdi * 3. All advertising materials mentioning features or use of this software 191.1Scdi * must display the following acknowledgement: 201.1Scdi * This product includes software developed by Charles M. Hannum. 211.1Scdi * 4. The name of the author may not be used to endorse or promote products 221.1Scdi * derived from this software without specific prior written permission. 231.1Scdi * 241.1Scdi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 251.1Scdi * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 261.1Scdi * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 271.1Scdi * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 281.1Scdi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 291.1Scdi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 301.1Scdi * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 311.1Scdi * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 321.1Scdi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 331.1Scdi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 341.1Scdi */ 351.1Scdi 361.1Scdi/* 371.1Scdi * Copyright (c) 1991 The Regents of the University of California. 381.1Scdi * All rights reserved. 391.1Scdi * 401.1Scdi * Redistribution and use in source and binary forms, with or without 411.1Scdi * modification, are permitted provided that the following conditions 421.1Scdi * are met: 431.1Scdi * 1. Redistributions of source code must retain the above copyright 441.1Scdi * notice, this list of conditions and the following disclaimer. 451.1Scdi * 2. Redistributions in binary form must reproduce the above copyright 461.1Scdi * notice, this list of conditions and the following disclaimer in the 471.1Scdi * documentation and/or other materials provided with the distribution. 481.2Sagc * 3. Neither the name of the University nor the names of its contributors 491.1Scdi * may be used to endorse or promote products derived from this software 501.1Scdi * without specific prior written permission. 511.1Scdi * 521.1Scdi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 531.1Scdi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 541.1Scdi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 551.1Scdi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 561.1Scdi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 571.1Scdi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 581.1Scdi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 591.1Scdi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 601.1Scdi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 611.1Scdi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 621.1Scdi * SUCH DAMAGE. 631.1Scdi * 641.1Scdi * @(#)com.c 7.5 (Berkeley) 5/16/91 651.1Scdi */ 661.1Scdi 671.1Scdi#include <sys/types.h> 681.1Scdi 691.1Scdi#define COM_FREQ 1843200 /* 16-bit baud rate divisor */ 701.1Scdi#define COM_TOLERANCE 30 /* baud rate tolerance, in 0.1% units */ 711.1Scdi 721.1Scdiint 731.1Scdicomspeed(speed) 741.1Scdi long speed; 751.1Scdi{ 761.1Scdi#define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 771.1Scdi 781.1Scdi int x, err; 791.1Scdi long frequency = COM_FREQ * 10; 801.1Scdi 811.1Scdi if (speed <= 0) 821.1Scdi return (-1); 831.1Scdi x = divrnd((frequency / 16), speed); 841.1Scdi if (x <= 0) 851.1Scdi return (-1); 861.1Scdi err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000; 871.1Scdi if (err < 0) 881.1Scdi err = -err; 891.1Scdi if (err > COM_TOLERANCE) 901.1Scdi return (-1); 911.1Scdi return (x); 921.1Scdi 931.3Ssimonb#undef divrnd 941.1Scdi} 95