⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 con3215.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  drivers/s390/char/con3215.c *    3215 line mode terminal driver. * *  S390 version *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com), */#include <linux/config.h>#include <linux/types.h>#include <linux/kdev_t.h>#include <linux/tty.h>#include <linux/vt_kern.h>#include <linux/init.h>#include <linux/console.h>#include <linux/interrupt.h>#include <linux/malloc.h>#include <linux/bootmem.h>#include <asm/io.h>#include <asm/ebcdic.h>#include <asm/uaccess.h>#include "../../../arch/s390/kernel/cpcmd.h"#include <asm/irq.h>#define NR_3215		    1#define NR_3215_REQ	    (4*NR_3215)#define RAW3215_BUFFER_SIZE 65536     /* output buffer size */#define RAW3215_INBUF_SIZE  256	      /* input buffer size */#define RAW3215_MIN_SPACE   128	      /* minimum free space for wakeup */#define RAW3215_MIN_WRITE   1024      /* min. length for immediate output */#define RAW3215_MAX_CCWLEN  3968      /* max. bytes to write with one ccw */#define RAW3215_NR_CCWS	    ((RAW3215_BUFFER_SIZE/RAW3215_MAX_CCWLEN)+2)#define RAW3215_TIMEOUT	    HZ/10     /* time for delayed output */#define RAW3215_FIXED	    1	      /* 3215 console device is not be freed */#define RAW3215_ACTIVE	    2	      /* set if the device is in use */#define RAW3215_WORKING	    4	      /* set if a request is being worked on */#define RAW3215_THROTTLED   8	      /* set if reading is disabled */#define RAW3215_STOPPED	    16	      /* set if writing is disabled */#define RAW3215_CLOSING	    32	      /* set while in close process */#define RAW3215_TIMER_RUNS  64	      /* set if the output delay timer is on */#define RAW3215_FLUSHING    128	      /* set to flush buffer (no delay) */#define RAW3215_BH_PENDING  256       /* indication for bh scheduling */struct _raw3215_info;		      /* forward declaration ... */int raw3215_condevice = -1;           /* preset console device *//* * Request types for a 3215 device */typedef enum {	RAW3215_FREE, RAW3215_READ, RAW3215_WRITE} raw3215_type;/* * Request structure for a 3215 device */typedef struct _raw3215_req {	raw3215_type type;	      /* type of the request */	int start, end;		      /* start/end index into output buffer */	int residual;		      /* residual count for read request */	ccw1_t ccws[RAW3215_NR_CCWS]; /* space for the channel program */	struct _raw3215_info *info;   /* pointer to main structure */	struct _raw3215_req *next;    /* pointer to next request */} raw3215_req  __attribute__ ((aligned(8)));typedef struct _raw3215_info {	int flags;		      /* state flags */	int irq;		      /* interrupt number to do_IO */	char *buffer;		      /* pointer to output buffer */	char *inbuf;		      /* pointer to input buffer */	int head;		      /* first free byte in output buffer */	int count;		      /* number of bytes in output buffer */	devstat_t devstat;	      /* device status structure for do_IO */	struct tty_struct *tty;	      /* pointer to tty structure if present */	struct tq_struct tqueue;      /* task queue to bottom half */	raw3215_req *queued_read;     /* pointer to queued read requests */	raw3215_req *queued_write;    /* pointer to queued write requests */	wait_queue_head_t empty_wait; /* wait queue for flushing */	struct timer_list timer;      /* timer for delayed output */	char *message;                /* pending message from raw3215_irq */	int msg_dstat;                /* dstat for pending message */	int msg_cstat;                /* cstat for pending message */} raw3215_info;static raw3215_info *raw3215[NR_3215];	/* array of 3215 devices structures */static raw3215_req *raw3215_freelist;	/* list of free request structures */static spinlock_t raw3215_freelist_lock;/* spinlock to protect free list */static struct tty_driver tty3215_driver;static struct tty_struct *tty3215_table[NR_3215];static struct termios *tty3215_termios[NR_3215];static struct termios *tty3215_termios_locked[NR_3215];static int tty3215_refcount;#ifndef MIN#define MIN(a,b)	((a) < (b) ? (a) : (b))#endifstatic int __init con3215_setup(char *str){        int vdev;        vdev = simple_strtoul(str,&str,16);        if (vdev >= 0 && vdev < 65536)                raw3215_condevice = vdev;        return 1;}__setup("condev=", con3215_setup);/* * Get a request structure from the free list */extern inline raw3215_req *raw3215_alloc_req(void) {	raw3215_req *req;	unsigned long flags;	spin_lock_irqsave(&raw3215_freelist_lock, flags);	req = raw3215_freelist;	raw3215_freelist = req->next;	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);	return req;}/* * Put a request structure back to the free list */extern inline void raw3215_free_req(raw3215_req *req) {	unsigned long flags;        if (req->type == RAW3215_FREE)                return;         /* don't free a free request */        req->type = RAW3215_FREE;	spin_lock_irqsave(&raw3215_freelist_lock, flags);	req->next = raw3215_freelist;	raw3215_freelist = req;	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);}/* * Get a write request structure. That is either a new or the last * queued write request. The request structure is set up in  * raw3215_mk_write_ccw. */static raw3215_req *raw3215_mk_write_req(raw3215_info *raw){	raw3215_req *req;	/* check if there is a queued write request */	req = raw->queued_write;	if (req == NULL) {		/* no queued write request, use new req structure */		req = raw3215_alloc_req();		req->type = RAW3215_WRITE;		req->info = raw;		req->start = raw->head;	} else		raw->queued_write = NULL;	return req;}/* * Get a read request structure. If there is a queued read request * it is used, but that shouldn't happen because a 3215 terminal * won't accept a new read before the old one is completed. */static raw3215_req *raw3215_mk_read_req(raw3215_info *raw){	raw3215_req *req;	/* there can only be ONE read request at a time */	req = raw->queued_read;	if (req == NULL) {		/* no queued read request, use new req structure */		req = raw3215_alloc_req();		req->type = RAW3215_READ;		req->info = raw;	} else		raw->queued_read = NULL;	return req;}/* * Set up a write request with the information from the main structure. * A ccw chain is created that writes everything in the output buffer * to the 3215 device. */static int raw3215_mk_write_ccw(raw3215_info *raw, raw3215_req *req){	ccw1_t *ccw;	int len, count, ix;	ccw = req->ccws;	req->end = (raw->head - 1) & (RAW3215_BUFFER_SIZE - 1);	len = ((req->end - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;	ix = req->start;	while (len > 0) {		if (ccw > req->ccws)			ccw[-1].flags |= 0x40; /* use command chaining */		ccw->cmd_code = 0x01; /* write, auto carrier return */		ccw->flags = 0x20;    /* ignore incorrect length ind.  */		ccw->cda =			(void *) virt_to_phys(raw->buffer + ix);		count = (len > RAW3215_MAX_CCWLEN) ? 			RAW3215_MAX_CCWLEN : len;		if (ix + count > RAW3215_BUFFER_SIZE)			count = RAW3215_BUFFER_SIZE-ix;		ccw->count = count;		len -= count;		ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);		ccw++;	}	return len;}/* * Set up a read request that reads up to 160 byte from the 3215 device. */static void raw3215_mk_read_ccw(raw3215_info *raw, raw3215_req *req){	ccw1_t *ccw;	ccw = req->ccws;	ccw->cmd_code = 0x0A; /* read inquiry */	ccw->flags = 0x20;    /* ignore incorrect length */	ccw->count = 160;	ccw->cda = (void *) virt_to_phys(raw->inbuf);}/* * Start a read or a write request */static void raw3215_start_io(raw3215_info *raw){	raw3215_req *req;	int res;	req = raw->queued_read;	if (req != NULL &&	    !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {		/* dequeue request */		raw->queued_read = NULL;		res = do_IO(raw->irq, req->ccws, (__u32) req, 0, 0);		if (res != 0) {			/* do_IO failed, put request back to queue */			raw->queued_read = req;		} else {			raw->flags |= RAW3215_WORKING;		} 	}	req = raw->queued_write;	if (req != NULL &&	    !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {		/* dequeue request */		raw->queued_write = NULL;		res = do_IO(raw->irq, req->ccws, (__u32) req, 0, 0);		if (res != 0) {			/* do_IO failed, put request back to queue */			raw->queued_write = req;		} else {			raw->flags |= RAW3215_WORKING;		}	}}/* * Function to start a delayed output after RAW3215_TIMEOUT seconds */static void raw3215_timeout(unsigned long __data){	raw3215_info *raw = (raw3215_info *) __data;	unsigned long flags;	s390irq_spin_lock_irqsave(raw->irq, flags);	if (raw->flags & RAW3215_TIMER_RUNS) {		del_timer(&raw->timer);		raw->flags &= ~RAW3215_TIMER_RUNS;		raw3215_start_io(raw);	}	s390irq_spin_unlock_irqrestore(raw->irq, flags);}/* * Function to conditionally start an IO. A read is started immediatly, * a write is only started immediatly if the flush flag is on or the * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not * done immediatly a timer is started with a delay of RAW3215_TIMEOUT. */extern inline void raw3215_try_io(raw3215_info *raw){	if (!(raw->flags & RAW3215_ACTIVE))		return;	if (raw->queued_read != NULL)		raw3215_start_io(raw);	else if (raw->queued_write != NULL) {		if (raw->count >= RAW3215_MIN_WRITE ||		    (raw->flags & RAW3215_FLUSHING)) {			/* execute write requests bigger than minimum size */			raw3215_start_io(raw);			if (raw->flags & RAW3215_TIMER_RUNS) {				del_timer(&raw->timer);				raw->flags &= ~RAW3215_TIMER_RUNS;			}		} else if (!(raw->flags & RAW3215_TIMER_RUNS)) {			/* delay small writes */			init_timer(&raw->timer);			raw->timer.expires = RAW3215_TIMEOUT + jiffies;			raw->timer.data = (unsigned long) raw;			raw->timer.function = raw3215_timeout;			add_timer(&raw->timer);			raw->flags |= RAW3215_TIMER_RUNS;		}	}}/* * The bottom half handler routine for 3215 devices. It tries to start * the next IO and wakes up processes waiting on the tty. */static void raw3215_softint(void *data){	raw3215_info *raw;	struct tty_struct *tty;	unsigned long flags;	raw = (raw3215_info *) data;	s390irq_spin_lock_irqsave(raw->irq, flags);	raw3215_try_io((raw3215_info *) data);        raw->flags &= ~RAW3215_BH_PENDING;	s390irq_spin_unlock_irqrestore(raw->irq, flags);	/* Check for pending message from raw3215_irq */	if (raw->message != NULL) {		printk(raw->message, raw->irq, raw->msg_dstat, raw->msg_cstat);		raw->message = NULL;	}	tty = raw->tty;	if (tty != NULL &&	    RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&		    tty->ldisc.write_wakeup)			(tty->ldisc.write_wakeup)(tty);		wake_up_interruptible(&tty->write_wait);	}}/* * Function to safely add raw3215_softint to tq_immediate. * The s390irq spinlock must be held. */static inline void raw3215_sched_bh(raw3215_info *raw){        if (raw->flags & RAW3215_BH_PENDING)                return;       /* already pending */        raw->flags |= RAW3215_BH_PENDING;        raw->tqueue.routine = raw3215_softint;        raw->tqueue.data = raw;        queue_task(&raw->tqueue, &tq_immediate);        mark_bh(IMMEDIATE_BH);}/* * Find the raw3215_info structure associated with irq */static inline raw3215_info *raw3215_find_info(int irq) {	raw3215_info *raw;	int i;	for (i = 0; i < NR_3215; i++) {		raw = raw3215[i];		if (raw != NULL && raw->irq == irq &&		    (raw->flags & RAW3215_ACTIVE))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -