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

📄 con3215.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  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), * *  Updated: *   Aug-2000: Added tab support *	       Dan Morrison, IBM Corporation (dmorriso@cse.buffalo.edu) */#include <linux/config.h>#include <linux/module.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/slab.h>#include <linux/bootmem.h>#include <asm/ccwdev.h>#include <asm/cio.h>#include <asm/io.h>#include <asm/ebcdic.h>#include <asm/uaccess.h>#include <asm/delay.h>#include <asm/cpcmd.h>#include <asm/setup.h>#include "ctrlchar.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_BYTES   3968      /* max. bytes to write with one ssch */#define RAW3215_MAX_NEWLINE 50	      /* max. lines to write with one ssch */#define RAW3215_NR_CCWS	    3#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 TAB_STOP_SIZE	    8	      /* tab stop size *//* * Request types for a 3215 device */enum raw3215_type {	RAW3215_FREE, RAW3215_READ, RAW3215_WRITE};/* * Request structure for a 3215 device */struct raw3215_req {	enum raw3215_type type;	      /* type of the request */	int start, len;		      /* start index & len in output buffer */	int delayable;		      /* indication to wait for more data */	int residual;		      /* residual count for read request */	struct ccw1 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 */} __attribute__ ((aligned(8)));struct raw3215_info {	struct ccw_device *cdev;      /* device for tty driver */	spinlock_t *lock;	      /* pointer to irq lock */	int flags;		      /* state flags */	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 */	int written;		      /* number of bytes in write requests */	struct tty_struct *tty;	      /* pointer to tty structure if present */	struct tasklet_struct tasklet;	struct raw3215_req *queued_read; /* pointer to queued read requests */	struct 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 */	int line_pos;		      /* position on the line (for tabs) */	char ubuffer[80];	      /* copy_from_user buffer */};/* array of 3215 devices structures */static struct raw3215_info *raw3215[NR_3215];/* spinlock to protect the raw3215 array */static DEFINE_SPINLOCK(raw3215_device_lock);/* list of free request structures */static struct raw3215_req *raw3215_freelist;/* spinlock to protect free list */static spinlock_t raw3215_freelist_lock;static struct tty_driver *tty3215_driver;/* * Get a request structure from the free list */static inline struct raw3215_req *raw3215_alloc_req(void) {	struct 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 */static inline voidraw3215_free_req(struct 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);}/* * Set up a read request that reads up to 160 byte from the 3215 device. * 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 voidraw3215_mk_read_req(struct raw3215_info *raw){	struct raw3215_req *req;	struct ccw1 *ccw;	/* 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;		raw->queued_read = req;	}	ccw = req->ccws;	ccw->cmd_code = 0x0A; /* read inquiry */	ccw->flags = 0x20;    /* ignore incorrect length */	ccw->count = 160;	ccw->cda = (__u32) __pa(raw->inbuf);}/* * Set up a write request with the information from the main structure. * A ccw chain is created that writes as much as possible from the output * buffer to the 3215 device. If a queued write exists it is replaced by * the new, probably lengthened request. */static voidraw3215_mk_write_req(struct raw3215_info *raw){	struct raw3215_req *req;	struct ccw1 *ccw;	int len, count, ix, lines;	if (raw->count <= raw->written)		return;	/* 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;		raw->queued_write = req;	} else {		raw->written -= req->len;	}	ccw = req->ccws;	req->start = (raw->head - raw->count + raw->written) &		     (RAW3215_BUFFER_SIZE - 1);	/*	 * now we have to count newlines. We can at max accept	 * RAW3215_MAX_NEWLINE newlines in a single ssch due to	 * a restriction in VM	 */	lines = 0;	ix = req->start;	while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {		if (raw->buffer[ix] == 0x15)			lines++;		ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);	}	len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;	if (len > RAW3215_MAX_BYTES)		len = RAW3215_MAX_BYTES;	req->len = len;	raw->written += len;	/* set the indication if we should try to enlarge this request */	req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);	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 =			(__u32) __pa(raw->buffer + ix);		count = 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++;	}	/*	 * Add a NOP to the channel program. 3215 devices are purely	 * emulated and its much better to avoid the channel end	 * interrupt in this case.	 */	if (ccw > req->ccws)		ccw[-1].flags |= 0x40; /* use command chaining */	ccw->cmd_code = 0x03; /* NOP */	ccw->flags = 0;	ccw->cda = 0;	ccw->count = 1;}/* * Start a read or a write request */static voidraw3215_start_io(struct raw3215_info *raw){	struct 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 = ccw_device_start(raw->cdev, req->ccws,				       (unsigned long) 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 = ccw_device_start(raw->cdev, req->ccws,				       (unsigned long) 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 voidraw3215_timeout(unsigned long __data){	struct raw3215_info *raw = (struct raw3215_info *) __data;	unsigned long flags;	spin_lock_irqsave(raw->lock, flags);	if (raw->flags & RAW3215_TIMER_RUNS) {		del_timer(&raw->timer);		raw->flags &= ~RAW3215_TIMER_RUNS;		raw3215_mk_write_req(raw);		raw3215_start_io(raw);	}	spin_unlock_irqrestore(raw->lock, flags);}/* * Function to conditionally start an IO. A read is started immediately, * a write is only started immediately if the flush flag is on or the * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not * done immediately a timer is started with a delay of RAW3215_TIMEOUT. */static inline voidraw3215_try_io(struct 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->queued_write->delayable == 0) ||		    (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 voidraw3215_tasklet(void *data){	struct raw3215_info *raw;	struct tty_struct *tty;	unsigned long flags;	raw = (struct raw3215_info *) data;	spin_lock_irqsave(raw->lock, flags);	raw3215_mk_write_req(raw);	raw3215_try_io(raw);	spin_unlock_irqrestore(raw->lock, flags);	/* Check for pending message from raw3215_irq */	if (raw->message != NULL) {		printk(raw->message, raw->msg_dstat, raw->msg_cstat);		raw->message = NULL;	}	tty = raw->tty;	if (tty != NULL &&	    RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {	    	tty_wakeup(tty);	}}/* * Interrupt routine, called from common io layer */static voidraw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb){	struct raw3215_info *raw;	struct raw3215_req *req;	struct tty_struct *tty;	int cstat, dstat;	int count, slen;	raw = cdev->dev.driver_data;	req = (struct raw3215_req *) intparm;	cstat = irb->scsw.cstat;	dstat = irb->scsw.dstat;	if (cstat != 0) {		raw->message = KERN_WARNING			"Got nonzero channel status in raw3215_irq "			"(dev sts 0x%2x, sch sts 0x%2x)";		raw->msg_dstat = dstat;		raw->msg_cstat = cstat;		tasklet_schedule(&raw->tasklet);	}	if (dstat & 0x01) { /* we got a unit exception */		dstat &= ~0x01;	 /* we can ignore it */	}	switch (dstat) {	case 0x80:		if (cstat != 0)			break;		/* Attention interrupt, someone hit the enter key */		raw3215_mk_read_req(raw);		if (MACHINE_IS_P390)			memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);		tasklet_schedule(&raw->tasklet);		break;	case 0x08:	case 0x0C:		/* Channel end interrupt. */		if ((raw = req->info) == NULL)			return;		     /* That shouldn't happen ... */		if (req->type == RAW3215_READ) {			/* store residual count, then wait for device end */			req->residual = irb->scsw.count;		}		if (dstat == 0x08)			break;	case 0x04:		/* Device end interrupt. */		if ((raw = req->info) == NULL)			return;		     /* That shouldn't happen ... */		if (req->type == RAW3215_READ && raw->tty != NULL) {			unsigned int cchar;			tty = raw->tty;			count = 160 - req->residual;			if (MACHINE_IS_P390) {				slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);				if (count > slen)					count = slen;			} else			if (count >= TTY_FLIPBUF_SIZE - tty->flip.count)				count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;			EBCASC(raw->inbuf, count);			cchar = ctrlchar_handle(raw->inbuf, count, tty);			switch (cchar & CTRLCHAR_MASK) {			case CTRLCHAR_SYSRQ:				break;			case CTRLCHAR_CTRL:				tty->flip.count++;				*tty->flip.flag_buf_ptr++ = TTY_NORMAL;				*tty->flip.char_buf_ptr++ = cchar;				tty_flip_buffer_push(raw->tty);				break;			case CTRLCHAR_NONE:				memcpy(tty->flip.char_buf_ptr,				       raw->inbuf, count);				if (count < 2 ||				    (strncmp(raw->inbuf+count-2, "^n", 2) &&				    strncmp(raw->inbuf+count-2, "\252n", 2)) ) {					/* don't add the auto \n */					tty->flip.char_buf_ptr[count] = '\n';					memset(tty->flip.flag_buf_ptr,					       TTY_NORMAL, count + 1);					count++;				} else					count-=2;				tty->flip.char_buf_ptr += count;				tty->flip.flag_buf_ptr += count;				tty->flip.count += count;				tty_flip_buffer_push(raw->tty);				break;			}		} else if (req->type == RAW3215_WRITE) {			raw->count -= req->len;			raw->written -= req->len;		}		raw->flags &= ~RAW3215_WORKING;		raw3215_free_req(req);		/* check for empty wait */		if (waitqueue_active(&raw->empty_wait) &&		    raw->queued_write == NULL &&		    raw->queued_read == NULL) {			wake_up_interruptible(&raw->empty_wait);		}		tasklet_schedule(&raw->tasklet);		break;	default:		/* Strange interrupt, I'll do my best to clean up */		if (req != NULL && req->type != RAW3215_FREE) {			if (req->type == RAW3215_WRITE) {				raw->count -= req->len;				raw->written -= req->len;			}			raw->flags &= ~RAW3215_WORKING;			raw3215_free_req(req);		}		raw->message = KERN_WARNING			"Spurious interrupt in in raw3215_irq "			"(dev sts 0x%2x, sch sts 0x%2x)";		raw->msg_dstat = dstat;		raw->msg_cstat = cstat;		tasklet_schedule(&raw->tasklet);	}	return;}/* * Wait until length bytes are available int the output buffer. * Has to be called with the s390irq lock held. Can be called * disabled. */static voidraw3215_make_room(struct raw3215_info *raw, unsigned int length){	while (RAW3215_BUFFER_SIZE - raw->count < length) {		/* there might be a request pending */		raw->flags |= RAW3215_FLUSHING;		raw3215_mk_write_req(raw);		raw3215_try_io(raw);		raw->flags &= ~RAW3215_FLUSHING;#ifdef CONFIG_TN3215_CONSOLE		wait_cons_dev();#endif		/* Enough room freed up ? */		if (RAW3215_BUFFER_SIZE - raw->count >= length)			break;		/* there might be another cpu waiting for the lock */		spin_unlock(raw->lock);		udelay(100);		spin_lock(raw->lock);	}}/* * String write routine for 3215 devices */static voidraw3215_write(struct raw3215_info *raw, const char *str, unsigned int length){	unsigned long flags;	int c, count;	while (length > 0) {		spin_lock_irqsave(raw->lock, flags);		count = (length > RAW3215_BUFFER_SIZE) ?					     RAW3215_BUFFER_SIZE : length;		length -= count;		raw3215_make_room(raw, count);		/* copy string to output buffer and convert it to EBCDIC */		while (1) {			c = min_t(int, count,				  min(RAW3215_BUFFER_SIZE - raw->count,				      RAW3215_BUFFER_SIZE - raw->head));			if (c <= 0)				break;			memcpy(raw->buffer + raw->head, str, c);			ASCEBC(raw->buffer + raw->head, c);			raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);			raw->count += c;			raw->line_pos += c;			str += c;			count -= c;		}		if (!(raw->flags & RAW3215_WORKING)) {			raw3215_mk_write_req(raw);			/* start or queue request */			raw3215_try_io(raw);		}		spin_unlock_irqrestore(raw->lock, flags);	}}/* * Put character routine for 3215 devices */static voidraw3215_putchar(struct raw3215_info *raw, unsigned char ch){	unsigned long flags;	unsigned int length, i;	spin_lock_irqsave(raw->lock, flags);	if (ch == '\t') {		length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);		raw->line_pos += length;		ch = ' ';	} else if (ch == '\n') {		length = 1;		raw->line_pos = 0;	} else {		length = 1;		raw->line_pos++;	}	raw3215_make_room(raw, length);	for (i = 0; i < length; i++) {		raw->buffer[raw->head] = (char) _ascebc[(int) ch];		raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);

⌨️ 快捷键说明

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