com.c revision 1.1
11.1Scdi/* $NetBSD: com.c,v 1.1 2003/06/25 17:24:22 cdi 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.1Scdi * 3. All advertising materials mentioning features or use of this software 491.1Scdi * must display the following acknowledgement: 501.1Scdi * This product includes software developed by the University of 511.1Scdi * California, Berkeley and its contributors. 521.1Scdi * 4. Neither the name of the University nor the names of its contributors 531.1Scdi * may be used to endorse or promote products derived from this software 541.1Scdi * without specific prior written permission. 551.1Scdi * 561.1Scdi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 571.1Scdi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 581.1Scdi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 591.1Scdi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 601.1Scdi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 611.1Scdi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 621.1Scdi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 631.1Scdi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 641.1Scdi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 651.1Scdi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 661.1Scdi * SUCH DAMAGE. 671.1Scdi * 681.1Scdi * @(#)com.c 7.5 (Berkeley) 5/16/91 691.1Scdi */ 701.1Scdi 711.1Scdi#include <sys/types.h> 721.1Scdi 731.1Scdi#define COM_FREQ 1843200 /* 16-bit baud rate divisor */ 741.1Scdi#define COM_TOLERANCE 30 /* baud rate tolerance, in 0.1% units */ 751.1Scdi 761.1Scdiint 771.1Scdicomspeed(speed) 781.1Scdi long speed; 791.1Scdi{ 801.1Scdi#define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 811.1Scdi 821.1Scdi int x, err; 831.1Scdi long frequency = COM_FREQ * 10; 841.1Scdi 851.1Scdi if (speed <= 0) 861.1Scdi return (-1); 871.1Scdi x = divrnd((frequency / 16), speed); 881.1Scdi if (x <= 0) 891.1Scdi return (-1); 901.1Scdi err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000; 911.1Scdi if (err < 0) 921.1Scdi err = -err; 931.1Scdi if (err > COM_TOLERANCE) 941.1Scdi return (-1); 951.1Scdi return (x); 961.1Scdi 971.1Scdi#undef divrnd(n, q) 981.1Scdi} 99