sclp_vt220.c

来自「优龙2410linux2.6.8内核源代码」· C语言 代码 · 共 837 行 · 第 1/2 页

C
837
字号
/* *  drivers/s390/char/sclp_vt220.c *    SCLP VT220 terminal driver. * *  S390 version *    Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation *    Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com> */#include <linux/config.h>#include <linux/module.h>#include <linux/spinlock.h>#include <linux/list.h>#include <linux/wait.h>#include <linux/timer.h>#include <linux/kernel.h>#include <linux/tty.h>#include <linux/tty_driver.h>#include <linux/sched.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/major.h>#include <linux/console.h>#include <linux/kdev_t.h>#include <linux/bootmem.h>#include <linux/interrupt.h>#include <linux/init.h>#include <asm/uaccess.h>#include "sclp.h"#define SCLP_VT220_PRINT_HEADER 	"sclp vt220 tty driver: "#define SCLP_VT220_MAJOR		TTY_MAJOR#define SCLP_VT220_MINOR		65#define SCLP_VT220_DRIVER_NAME		"sclp_vt220"#define SCLP_VT220_DEVICE_NAME		"ttysclp"#define SCLP_VT220_CONSOLE_NAME		"ttyS"#define SCLP_VT220_CONSOLE_INDEX	1	/* console=ttyS1 */#define SCLP_VT220_BUF_SIZE		80/* Representation of a single write request */struct sclp_vt220_request {	struct list_head list;	struct sclp_req sclp_req;	int retry_count;	struct timer_list retry_timer;};/* VT220 SCCB */struct sclp_vt220_sccb {	struct sccb_header header;	struct evbuf_header evbuf;};#define SCLP_VT220_MAX_CHARS_PER_BUFFER	(PAGE_SIZE - \					 sizeof(struct sclp_vt220_request) - \					 sizeof(struct sclp_vt220_sccb))/* Structures and data needed to register tty driver */static struct tty_driver *sclp_vt220_driver;/* The tty_struct that the kernel associated with us */static struct tty_struct *sclp_vt220_tty;/* Lock to protect internal data from concurrent access */static spinlock_t sclp_vt220_lock;/* List of empty pages to be used as write request buffers */static struct list_head sclp_vt220_empty;/* List of pending requests */static struct list_head sclp_vt220_outqueue;/* Number of requests in outqueue */static int sclp_vt220_outqueue_count;/* Wait queue used to delay write requests while we've run out of buffers */static wait_queue_head_t sclp_vt220_waitq;/* Timer used for delaying write requests to merge subsequent messages into * a single buffer */static struct timer_list sclp_vt220_timer;/* Pointer to current request buffer which has been partially filled but not * yet sent */static struct sclp_vt220_request *sclp_vt220_current_request;/* Number of characters in current request buffer */static int sclp_vt220_buffered_chars;/* Flag indicating whether this driver has already been initialized */static int sclp_vt220_initialized = 0;/* Flag indicating that sclp_vt220_current_request should really * have been already queued but wasn't because the SCLP was processing * another buffer */static int sclp_vt220_flush_later;static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);static void __sclp_vt220_emit(struct sclp_vt220_request *request);static void sclp_vt220_emit_current(void);/* Registration structure for our interest in SCLP event buffers */static struct sclp_register sclp_vt220_register = {	.send_mask		= EvTyp_VT220Msg_Mask,	.receive_mask		= EvTyp_VT220Msg_Mask,	.state_change_fn	= NULL,	.receiver_fn		= sclp_vt220_receiver_fn};/* * Put provided request buffer back into queue and check emit pending * buffers if necessary. */static voidsclp_vt220_process_queue(struct sclp_vt220_request *request){	unsigned long flags;	struct sclp_vt220_request *next;	void *page;	/* Put buffer back to list of empty buffers */	page = request->sclp_req.sccb;	spin_lock_irqsave(&sclp_vt220_lock, flags);	/* Move request from outqueue to empty queue */	list_del(&request->list);	sclp_vt220_outqueue_count--;	list_add_tail((struct list_head *) page, &sclp_vt220_empty);	/* Check if there is a pending buffer on the out queue. */	next = NULL;	if (!list_empty(&sclp_vt220_outqueue))		next = list_entry(sclp_vt220_outqueue.next,				  struct sclp_vt220_request, list);	spin_unlock_irqrestore(&sclp_vt220_lock, flags);	if (next != NULL)		__sclp_vt220_emit(next);	else if (sclp_vt220_flush_later)		sclp_vt220_emit_current();	wake_up(&sclp_vt220_waitq);	/* Check if the tty needs a wake up call */	if (sclp_vt220_tty != NULL) {		if ((sclp_vt220_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&		    (sclp_vt220_tty->ldisc.write_wakeup != NULL))			(sclp_vt220_tty->ldisc.write_wakeup)(sclp_vt220_tty);		wake_up_interruptible(&sclp_vt220_tty->write_wait);	}}/* * Retry sclp write request after waiting some time for an sclp equipment * check to pass. */static voidsclp_vt220_retry(unsigned long data){	struct sclp_vt220_request *request;	struct sclp_vt220_sccb *sccb;	request = (struct sclp_vt220_request *) data;	request->sclp_req.status = SCLP_REQ_FILLED;	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;	sccb->header.response_code = 0x0000;	sclp_add_request(&request->sclp_req);}#define SCLP_BUFFER_MAX_RETRY		5#define	SCLP_BUFFER_RETRY_INTERVAL	2/* * Callback through which the result of a write request is reported by the * SCLP. */static voidsclp_vt220_callback(struct sclp_req *request, void *data){	struct sclp_vt220_request *vt220_request;	struct sclp_vt220_sccb *sccb;	vt220_request = (struct sclp_vt220_request *) data;	if (request->status == SCLP_REQ_FAILED) {		sclp_vt220_process_queue(vt220_request);		return;	}	sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;	/* Check SCLP response code and choose suitable action	*/	switch (sccb->header.response_code) {	case 0x0020 :		break;	case 0x05f0: /* Target resource in improper state */		break;	case 0x0340: /* Contained SCLP equipment check */		if (vt220_request->retry_count++ > SCLP_BUFFER_MAX_RETRY)			break;		/* Remove processed buffers and requeue rest */		if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {			/* Not all buffers were processed */			sccb->header.response_code = 0x0000;			vt220_request->sclp_req.status = SCLP_REQ_FILLED;			sclp_add_request(request);			return;		}		break;	case 0x0040: /* SCLP equipment check */		if (vt220_request->retry_count++ > SCLP_BUFFER_MAX_RETRY)			break;		/* Wait some time, then retry request */		vt220_request->retry_timer.function = sclp_vt220_retry;		vt220_request->retry_timer.data =			(unsigned long) vt220_request;		vt220_request->retry_timer.expires =			jiffies + SCLP_BUFFER_RETRY_INTERVAL*HZ;		add_timer(&vt220_request->retry_timer);		return;	default:		break;	}	sclp_vt220_process_queue(vt220_request);}/* * Emit vt220 request buffer to SCLP. */static void__sclp_vt220_emit(struct sclp_vt220_request *request){	if (!(sclp_vt220_register.sclp_send_mask & EvTyp_VT220Msg_Mask)) {		request->sclp_req.status = SCLP_REQ_FAILED;		sclp_vt220_callback(&request->sclp_req, (void *) request);		return;	}	request->sclp_req.command = SCLP_CMDW_WRITEDATA;	request->sclp_req.status = SCLP_REQ_FILLED;	request->sclp_req.callback = sclp_vt220_callback;	request->sclp_req.callback_data = (void *) request;	sclp_add_request(&request->sclp_req);}/* * Queue and emit given request. */static voidsclp_vt220_emit(struct sclp_vt220_request *request){	unsigned long flags;	int count;	spin_lock_irqsave(&sclp_vt220_lock, flags);	list_add_tail(&request->list, &sclp_vt220_outqueue);	count = sclp_vt220_outqueue_count++;	spin_unlock_irqrestore(&sclp_vt220_lock, flags);	/* Emit only the first buffer immediately - callback takes care of	 * the rest */	if (count == 0)		__sclp_vt220_emit(request);}/* * Queue and emit current request. */static voidsclp_vt220_emit_current(void){	unsigned long flags;	struct sclp_vt220_request *request;	struct sclp_vt220_sccb *sccb;	spin_lock_irqsave(&sclp_vt220_lock, flags);	request = NULL;	if (sclp_vt220_current_request != NULL) {		sccb = (struct sclp_vt220_sccb *) 				sclp_vt220_current_request->sclp_req.sccb;		/* Only emit buffers with content */		if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {			request = sclp_vt220_current_request;			sclp_vt220_current_request = NULL;			if (timer_pending(&sclp_vt220_timer))				del_timer(&sclp_vt220_timer);		}		sclp_vt220_flush_later = 0;	}	spin_unlock_irqrestore(&sclp_vt220_lock, flags);	if (request != NULL)		sclp_vt220_emit(request);}#define SCLP_NORMAL_WRITE	0x00/* * Helper function to initialize a page with the sclp request structure. */static struct sclp_vt220_request *sclp_vt220_initialize_page(void *page){	struct sclp_vt220_request *request;	struct sclp_vt220_sccb *sccb;	/* Place request structure at end of page */	request = ((struct sclp_vt220_request *)			((addr_t) page + PAGE_SIZE)) - 1;	init_timer(&request->retry_timer);	request->retry_count = 0;	request->sclp_req.sccb = page;	/* SCCB goes at start of page */	sccb = (struct sclp_vt220_sccb *) page;	memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));	sccb->header.length = sizeof(struct sclp_vt220_sccb);	sccb->header.function_code = SCLP_NORMAL_WRITE;	sccb->header.response_code = 0x0000;	sccb->evbuf.type = EvTyp_VT220Msg;	sccb->evbuf.length = sizeof(struct evbuf_header);	return request;}static inline unsigned intsclp_vt220_space_left(struct sclp_vt220_request *request){	struct sclp_vt220_sccb *sccb;	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;	return PAGE_SIZE - sizeof(struct sclp_vt220_request) -	       sccb->header.length;}static inline unsigned intsclp_vt220_chars_stored(struct sclp_vt220_request *request){	struct sclp_vt220_sccb *sccb;	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;	return sccb->evbuf.length - sizeof(struct evbuf_header);}/* * Add msg to buffer associated with request. Return the number of characters * added. */static intsclp_vt220_add_msg(struct sclp_vt220_request *request,		   const unsigned char *msg, int count, int convertlf){	struct sclp_vt220_sccb *sccb;	void *buffer;	unsigned char c;	int from;	int to;	if (count > sclp_vt220_space_left(request))		count = sclp_vt220_space_left(request);	if (count <= 0)		return 0;	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;	buffer = (void *) ((addr_t) sccb + sccb->header.length);	if (convertlf) {		/* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/		for (from=0, to=0;		     (from < count) && (to < sclp_vt220_space_left(request));		     from++) {			/* Retrieve character */			c = msg[from];			/* Perform conversion */			if (c == 0x0a) {				if (to + 1 < sclp_vt220_space_left(request)) {					((unsigned char *) buffer)[to++] = c;					((unsigned char *) buffer)[to++] = 0x0d;				} else					break;			} else				((unsigned char *) buffer)[to++] = c;		}		sccb->header.length += to;		sccb->evbuf.length += to;		return from;	} else {		memcpy(buffer, (const void *) msg, count);		sccb->header.length += count;		sccb->evbuf.length += count;		return count;	}}/* * Emit buffer after having waited long enough for more data to arrive. */static voidsclp_vt220_timeout(unsigned long data){	sclp_vt220_emit_current();}#define BUFFER_MAX_DELAY	HZ/2/*  * Internal implementation of the write function. Write COUNT bytes of data * from memory at BUF * to the SCLP interface. In case that the data does not fit into the current * write buffer, emit the current one and allocate a new one. If there are no * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE * is non-zero, the buffer will be scheduled for emitting after a timeout - * otherwise the user has to explicitly call the flush function. * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message * buffer should be converted to 0x0a 0x0d. After completion, return the number * of bytes written. */static int__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,		   int convertlf){	unsigned long flags;	void *page;	int written;	int overall_written;

⌨️ 快捷键说明

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