📄 ser_a2232.c
字号:
/* drivers/char/ser_a2232.c *//* $Id: ser_a2232.c,v 0.4 2000/01/25 12:00:00 ehaase Exp $ *//* Linux serial driver for the Amiga A2232 board *//* This driver is MAINTAINED. Before applying any changes, please contact * the author. *//* Copyright (c) 2000-2001 Enver Haase <ehaase@inf.fu-berlin.de> * alias The A2232 driver project <A2232@gmx.net> * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *//***************************** Documentation ************************//* * This driver is in EXPERIMENTAL state. That means I could not find * someone with five A2232 boards with 35 ports running at 19200 bps * at the same time and test the machine's behaviour. * However, I know that you can performance-tweak this driver (see * the source code). * One thing to consider is the time this driver consumes during the * Amiga's vertical blank interrupt. Everything that is to be done * _IS DONE_ when entering the vertical blank interrupt handler of * this driver. * However, it would be more sane to only do the job for only ONE card * instead of ALL cards at a time; or, more generally, to handle only * SOME ports instead of ALL ports at a time. * However, as long as no-one runs into problems I guess I shouldn't * change the driver as it runs fine for me :) . * * Version history of this file: * 0.4 Resolved licensing issues. * 0.3 Inclusion in the Linux/m68k tree, small fixes. * 0.2 Added documentation, minor typo fixes. * 0.1 Initial release. * * TO DO: * - Handle incoming BREAK events. I guess "Stevens: Advanced * Programming in the UNIX(R) Environment" is a good reference * on what is to be done. * - When installing as a module, don't simply 'printk' text, but * send it to the TTY used by the user. * * THANKS TO: * - Jukka Marin (65EC02 code). * - The other NetBSD developers on whose A2232 driver I had a * pretty close look. However, I didn't copy any code so it * is okay to put my code under the GPL and include it into * Linux. *//***************************** End of Documentation *****************//***************************** Defines ******************************//* * Enables experimental 115200 (normal) 230400 (turbo) baud rate. * The A2232 specification states it can only operate at speeds up to * 19200 bits per second, and I was not able to send a file via * "sz"/"rz" and a null-modem cable from one A2232 port to another * at 115200 bits per second. * However, this might work for you. */#undef A2232_SPEEDHACK/* * Default is not to use RTS/CTS so you could be talked to death. */#define A2232_SUPPRESS_RTSCTS_WARNING/************************* End of Defines ***************************//***************************** Includes *****************************/#include <linux/module.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/interrupt.h>#include <linux/kernel.h>#include <linux/errno.h>#include <asm/setup.h>#include <asm/amigaints.h>#include <asm/amigahw.h>#include <linux/zorro.h>#include <asm/irq.h>#include <asm/semaphore.h>#include <linux/delay.h>#include <linux/serial.h>#include <linux/generic_serial.h>#include "ser_a2232.h"#include "ser_a2232fw.h"/************************* End of Includes **************************//***************************** Prototypes ***************************//* Helper functions */static __inline__ volatile struct a2232status *a2232stat(unsigned int board, unsigned int portonboard);static __inline__ volatile struct a2232memory *a2232mem (unsigned int board); static __inline__ void a2232_receive_char( struct a2232_port *port, int ch, int err );/* The interrupt service routine */static void a2232_vbl_inter(int irq, void *data, struct pt_regs *fp);/* Initialize the port structures */static void a2232_init_portstructs(void);/* Initialize and register TTY drivers. *//* returns 0 IFF successful */static int a2232_init_drivers(void); /* Initialize all A2232 boards; main entry point. */int a2232board_init(void);/* BEGIN GENERIC_SERIAL PROTOTYPES */static void a2232_disable_tx_interrupts(void *ptr);static void a2232_enable_tx_interrupts(void *ptr);static void a2232_disable_rx_interrupts(void *ptr);static void a2232_enable_rx_interrupts(void *ptr);static int a2232_get_CD(void *ptr);static void a2232_shutdown_port(void *ptr);static int a2232_set_real_termios(void *ptr);static int a2232_chars_in_buffer(void *ptr);static void a2232_close(void *ptr);static void a2232_hungup(void *ptr);/* static void a2232_getserial (void *ptr, struct serial_struct *sp); *//* END GENERIC_SERIAL PROTOTYPES *//* Functions that the TTY driver struct expects */static int a2232_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);static void a2232_throttle(struct tty_struct *tty);static void a2232_unthrottle(struct tty_struct *tty);static int a2232_open(struct tty_struct * tty, struct file * filp);/************************* End of Prototypes ************************//***************************** Global variables *********************//*--------------------------------------------------------------------------- * Interface from generic_serial.c back here *--------------------------------------------------------------------------*/static struct real_driver a2232_real_driver = { a2232_disable_tx_interrupts, a2232_enable_tx_interrupts, a2232_disable_rx_interrupts, a2232_enable_rx_interrupts, a2232_get_CD, a2232_shutdown_port, a2232_set_real_termios, a2232_chars_in_buffer, a2232_close, a2232_hungup, NULL /* a2232_getserial */};static void *a2232_driver_ID = &a2232_driver_ID; // Some memory address WE own./* Ports structs */static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];/* TTY driver structs */static struct tty_driver a2232_driver;static struct tty_driver a2232_callout_driver;/* Variables used by the TTY driver */static int a2232_refcount;static struct tty_struct *a2232_table[MAX_A2232_BOARDS*NUMLINES] = { NULL, };static struct termios *a2232_termios[MAX_A2232_BOARDS*NUMLINES];static struct termios *a2232_termios_locked[MAX_A2232_BOARDS*NUMLINES];/* nr of cards completely (all ports) and correctly configured */static int nr_a2232; /* zorro_dev structs for the A2232's */static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS]; /***************************** End of Global variables **************//***************************** Functions ****************************//*** BEGIN OF REAL_DRIVER FUNCTIONS ***/static void a2232_disable_tx_interrupts(void *ptr){ struct a2232_port *port; volatile struct a2232status *stat; unsigned long flags; port = ptr; stat = a2232stat(port->which_a2232, port->which_port_on_a2232); stat->OutDisable = -1; /* Does this here really have to be? */ save_flags(flags); cli(); port->gs.flags &= ~GS_TX_INTEN; restore_flags(flags);}static void a2232_enable_tx_interrupts(void *ptr){ struct a2232_port *port; volatile struct a2232status *stat; unsigned long flags; port = ptr; stat = a2232stat(port->which_a2232, port->which_port_on_a2232); stat->OutDisable = 0; /* Does this here really have to be? */ save_flags(flags); cli(); port->gs.flags |= GS_TX_INTEN; restore_flags(flags);}static void a2232_disable_rx_interrupts(void *ptr){ struct a2232_port *port; port = ptr; port->disable_rx = -1;}static void a2232_enable_rx_interrupts(void *ptr){ struct a2232_port *port; port = ptr; port->disable_rx = 0;}static int a2232_get_CD(void *ptr){ return ((struct a2232_port *) ptr)->cd_status;}static void a2232_shutdown_port(void *ptr){ struct a2232_port *port; volatile struct a2232status *stat; unsigned long flags; port = ptr; stat = a2232stat(port->which_a2232, port->which_port_on_a2232); save_flags(flags); cli(); port->gs.flags &= ~GS_ACTIVE; if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) { /* Set DTR and RTS to Low, flush output. The NetBSD driver "msc.c" does it this way. */ stat->Command = ( (stat->Command & ~A2232CMD_CMask) | A2232CMD_Close ); stat->OutFlush = -1; stat->Setup = -1; } restore_flags(flags); /* After analyzing control flow, I think a2232_shutdown_port is actually the last call from the system when at application level someone issues a "echo Hello >>/dev/ttyY0". Therefore I think the MOD_DEC_USE_COUNT should be here and not in "a2232_close()". See the comment in "sx.c", too. If you run into problems, compile this driver into the kernel instead of compiling it as a module. */ MOD_DEC_USE_COUNT;}static int a2232_set_real_termios(void *ptr){ unsigned int cflag, baud, chsize, stopb, parity, softflow; int rate; int a2232_param, a2232_cmd; unsigned long flags; unsigned int i; struct a2232_port *port = ptr; volatile struct a2232status *status; volatile struct a2232memory *mem; if (!port->gs.tty || !port->gs.tty->termios) return 0; status = a2232stat(port->which_a2232, port->which_port_on_a2232); mem = a2232mem(port->which_a2232); a2232_param = a2232_cmd = 0; // get baud rate baud = port->gs.baud; if (baud == 0) { /* speed == 0 -> drop DTR, do nothing else */ save_flags(flags); cli(); // Clear DTR (and RTS... mhhh). status->Command = ( (status->Command & ~A2232CMD_CMask) | A2232CMD_Close ); status->OutFlush = -1; status->Setup = -1; restore_flags(flags); return 0; } rate = A2232_BAUD_TABLE_NOAVAIL; for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){ if (a2232_baud_table[i] == baud){ if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2]; else rate = a2232_baud_table[i+1]; } } if (rate == A2232_BAUD_TABLE_NOAVAIL){ printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud); // This is useful for both (turbo or normal) Crystal versions. rate = A2232PARAM_B9600; } a2232_param |= rate; cflag = port->gs.tty->termios->c_cflag; // get character size chsize = cflag & CSIZE; switch (chsize){ case CS8: a2232_param |= A2232PARAM_8Bit; break; case CS7: a2232_param |= A2232PARAM_7Bit; break; case CS6: a2232_param |= A2232PARAM_6Bit; break; case CS5: a2232_param |= A2232PARAM_5Bit; break; default: printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n", port->which_a2232,port->which_port_on_a2232,chsize); a2232_param |= A2232PARAM_8Bit; break; } // get number of stop bits stopb = cflag & CSTOPB; if (stopb){ // two stop bits instead of one printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n", port->which_a2232,port->which_port_on_a2232); } // Warn if RTS/CTS not wanted if (!(cflag & CRTSCTS)){#ifndef A2232_SUPPRESS_RTSCTS_WARNING printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n", port->which_a2232,port->which_port_on_a2232);#endif } /* I think this is correct. However, IXOFF means _input_ flow control and I wonder if one should care about IXON _output_ flow control, too. If this makes problems, one should turn the A2232 firmware XON/XOFF "SoftFlow" flow control off and use the conventional way of inserting START/STOP characters by hand in throttle()/unthrottle(). */ softflow = !!( port->gs.tty->termios->c_iflag & IXOFF ); // get Parity (Enabled/Disabled? If Enabled, Odd or Even?) parity = cflag & (PARENB | PARODD); if (parity & PARENB){ if (parity & PARODD){ a2232_cmd |= A2232CMD_OddParity; } else{ a2232_cmd |= A2232CMD_EvenParity; } } else a2232_cmd |= A2232CMD_NoParity; /* Hmm. Maybe an own a2232_port structure member would be cleaner? */ if (cflag & CLOCAL) port->gs.flags &= ~ASYNC_CHECK_CD; else port->gs.flags |= ASYNC_CHECK_CD; /* Now we have all parameters and can go to set them: */ save_flags(flags); cli(); status->Param = a2232_param | A2232PARAM_RcvBaud; status->Command = a2232_cmd | A2232CMD_Open | A2232CMD_Enable; status->SoftFlow = softflow; status->OutDisable = 0; status->Setup = -1; restore_flags(flags); return 0;}static int a2232_chars_in_buffer(void *ptr){ struct a2232_port *port; volatile struct a2232status *status; unsigned char ret; /* we need modulo-256 arithmetics */ port = ptr; status = a2232stat(port->which_a2232, port->which_port_on_a2232);#if A2232_IOBUFLEN != 256#error "Re-Implement a2232_chars_in_buffer()!"#endif ret = (status->OutHead - status->OutTail); return ret;}static void a2232_close(void *ptr){ a2232_disable_tx_interrupts(ptr); a2232_disable_rx_interrupts(ptr); /* see the comment in a2232_shutdown_port above. */ /* MOD_DEC_USE_COUNT; */}static void a2232_hungup(void *ptr){ a2232_close(ptr);}/*** END OF REAL_DRIVER FUNCTIONS ***//*** BEGIN FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/static int a2232_ioctl( struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg){ return -ENOIOCTLCMD;}static void a2232_throttle(struct tty_struct *tty){/* Throttle: System cannot take another chars: Drop RTS or send the STOP char or whatever. The A2232 firmware does RTS/CTS anyway, and XON/XOFF if switched on. So the only thing we can do at this layer here is not taking any characters out of the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -