xref: /OK3568_Linux_fs/kernel/drivers/s390/char/sclp_vt220.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * SCLP VT220 terminal driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright IBM Corp. 2003, 2009
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/spinlock.h>
12*4882a593Smuzhiyun #include <linux/list.h>
13*4882a593Smuzhiyun #include <linux/wait.h>
14*4882a593Smuzhiyun #include <linux/timer.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/sysrq.h>
17*4882a593Smuzhiyun #include <linux/tty.h>
18*4882a593Smuzhiyun #include <linux/tty_driver.h>
19*4882a593Smuzhiyun #include <linux/tty_flip.h>
20*4882a593Smuzhiyun #include <linux/errno.h>
21*4882a593Smuzhiyun #include <linux/mm.h>
22*4882a593Smuzhiyun #include <linux/major.h>
23*4882a593Smuzhiyun #include <linux/console.h>
24*4882a593Smuzhiyun #include <linux/kdev_t.h>
25*4882a593Smuzhiyun #include <linux/interrupt.h>
26*4882a593Smuzhiyun #include <linux/init.h>
27*4882a593Smuzhiyun #include <linux/reboot.h>
28*4882a593Smuzhiyun #include <linux/slab.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <linux/uaccess.h>
31*4882a593Smuzhiyun #include "sclp.h"
32*4882a593Smuzhiyun #include "ctrlchar.h"
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define SCLP_VT220_MAJOR		TTY_MAJOR
35*4882a593Smuzhiyun #define SCLP_VT220_MINOR		65
36*4882a593Smuzhiyun #define SCLP_VT220_DRIVER_NAME		"sclp_vt220"
37*4882a593Smuzhiyun #define SCLP_VT220_DEVICE_NAME		"ttysclp"
38*4882a593Smuzhiyun #define SCLP_VT220_CONSOLE_NAME		"ttysclp"
39*4882a593Smuzhiyun #define SCLP_VT220_CONSOLE_INDEX	0	/* console=ttysclp0 */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /* Representation of a single write request */
42*4882a593Smuzhiyun struct sclp_vt220_request {
43*4882a593Smuzhiyun 	struct list_head list;
44*4882a593Smuzhiyun 	struct sclp_req sclp_req;
45*4882a593Smuzhiyun 	int retry_count;
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /* VT220 SCCB */
49*4882a593Smuzhiyun struct sclp_vt220_sccb {
50*4882a593Smuzhiyun 	struct sccb_header header;
51*4882a593Smuzhiyun 	struct evbuf_header evbuf;
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define SCLP_VT220_MAX_CHARS_PER_BUFFER	(PAGE_SIZE - \
55*4882a593Smuzhiyun 					 sizeof(struct sclp_vt220_request) - \
56*4882a593Smuzhiyun 					 sizeof(struct sclp_vt220_sccb))
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Structures and data needed to register tty driver */
59*4882a593Smuzhiyun static struct tty_driver *sclp_vt220_driver;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static struct tty_port sclp_vt220_port;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* Lock to protect internal data from concurrent access */
64*4882a593Smuzhiyun static spinlock_t sclp_vt220_lock;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /* List of empty pages to be used as write request buffers */
67*4882a593Smuzhiyun static struct list_head sclp_vt220_empty;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* List of pending requests */
70*4882a593Smuzhiyun static struct list_head sclp_vt220_outqueue;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /* Suspend mode flag */
73*4882a593Smuzhiyun static int sclp_vt220_suspended;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* Flag that output queue is currently running */
76*4882a593Smuzhiyun static int sclp_vt220_queue_running;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /* Timer used for delaying write requests to merge subsequent messages into
79*4882a593Smuzhiyun  * a single buffer */
80*4882a593Smuzhiyun static struct timer_list sclp_vt220_timer;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* Pointer to current request buffer which has been partially filled but not
83*4882a593Smuzhiyun  * yet sent */
84*4882a593Smuzhiyun static struct sclp_vt220_request *sclp_vt220_current_request;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* Number of characters in current request buffer */
87*4882a593Smuzhiyun static int sclp_vt220_buffered_chars;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* Counter controlling core driver initialization. */
90*4882a593Smuzhiyun static int __initdata sclp_vt220_init_count;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /* Flag indicating that sclp_vt220_current_request should really
93*4882a593Smuzhiyun  * have been already queued but wasn't because the SCLP was processing
94*4882a593Smuzhiyun  * another buffer */
95*4882a593Smuzhiyun static int sclp_vt220_flush_later;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
98*4882a593Smuzhiyun static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
99*4882a593Smuzhiyun 				   enum sclp_pm_event sclp_pm_event);
100*4882a593Smuzhiyun static int __sclp_vt220_emit(struct sclp_vt220_request *request);
101*4882a593Smuzhiyun static void sclp_vt220_emit_current(void);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Registration structure for SCLP output event buffers */
104*4882a593Smuzhiyun static struct sclp_register sclp_vt220_register = {
105*4882a593Smuzhiyun 	.send_mask		= EVTYP_VT220MSG_MASK,
106*4882a593Smuzhiyun 	.pm_event_fn		= sclp_vt220_pm_event_fn,
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* Registration structure for SCLP input event buffers */
110*4882a593Smuzhiyun static struct sclp_register sclp_vt220_register_input = {
111*4882a593Smuzhiyun 	.receive_mask		= EVTYP_VT220MSG_MASK,
112*4882a593Smuzhiyun 	.receiver_fn		= sclp_vt220_receiver_fn,
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun  * Put provided request buffer back into queue and check emit pending
118*4882a593Smuzhiyun  * buffers if necessary.
119*4882a593Smuzhiyun  */
120*4882a593Smuzhiyun static void
sclp_vt220_process_queue(struct sclp_vt220_request * request)121*4882a593Smuzhiyun sclp_vt220_process_queue(struct sclp_vt220_request *request)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	unsigned long flags;
124*4882a593Smuzhiyun 	void *page;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	do {
127*4882a593Smuzhiyun 		/* Put buffer back to list of empty buffers */
128*4882a593Smuzhiyun 		page = request->sclp_req.sccb;
129*4882a593Smuzhiyun 		spin_lock_irqsave(&sclp_vt220_lock, flags);
130*4882a593Smuzhiyun 		/* Move request from outqueue to empty queue */
131*4882a593Smuzhiyun 		list_del(&request->list);
132*4882a593Smuzhiyun 		list_add_tail((struct list_head *) page, &sclp_vt220_empty);
133*4882a593Smuzhiyun 		/* Check if there is a pending buffer on the out queue. */
134*4882a593Smuzhiyun 		request = NULL;
135*4882a593Smuzhiyun 		if (!list_empty(&sclp_vt220_outqueue))
136*4882a593Smuzhiyun 			request = list_entry(sclp_vt220_outqueue.next,
137*4882a593Smuzhiyun 					     struct sclp_vt220_request, list);
138*4882a593Smuzhiyun 		if (!request || sclp_vt220_suspended) {
139*4882a593Smuzhiyun 			sclp_vt220_queue_running = 0;
140*4882a593Smuzhiyun 			spin_unlock_irqrestore(&sclp_vt220_lock, flags);
141*4882a593Smuzhiyun 			break;
142*4882a593Smuzhiyun 		}
143*4882a593Smuzhiyun 		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
144*4882a593Smuzhiyun 	} while (__sclp_vt220_emit(request));
145*4882a593Smuzhiyun 	if (request == NULL && sclp_vt220_flush_later)
146*4882a593Smuzhiyun 		sclp_vt220_emit_current();
147*4882a593Smuzhiyun 	tty_port_tty_wakeup(&sclp_vt220_port);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun #define SCLP_BUFFER_MAX_RETRY		1
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun  * Callback through which the result of a write request is reported by the
154*4882a593Smuzhiyun  * SCLP.
155*4882a593Smuzhiyun  */
156*4882a593Smuzhiyun static void
sclp_vt220_callback(struct sclp_req * request,void * data)157*4882a593Smuzhiyun sclp_vt220_callback(struct sclp_req *request, void *data)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	struct sclp_vt220_request *vt220_request;
160*4882a593Smuzhiyun 	struct sclp_vt220_sccb *sccb;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	vt220_request = (struct sclp_vt220_request *) data;
163*4882a593Smuzhiyun 	if (request->status == SCLP_REQ_FAILED) {
164*4882a593Smuzhiyun 		sclp_vt220_process_queue(vt220_request);
165*4882a593Smuzhiyun 		return;
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 	sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* Check SCLP response code and choose suitable action	*/
170*4882a593Smuzhiyun 	switch (sccb->header.response_code) {
171*4882a593Smuzhiyun 	case 0x0020 :
172*4882a593Smuzhiyun 		break;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	case 0x05f0: /* Target resource in improper state */
175*4882a593Smuzhiyun 		break;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	case 0x0340: /* Contained SCLP equipment check */
178*4882a593Smuzhiyun 		if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
179*4882a593Smuzhiyun 			break;
180*4882a593Smuzhiyun 		/* Remove processed buffers and requeue rest */
181*4882a593Smuzhiyun 		if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
182*4882a593Smuzhiyun 			/* Not all buffers were processed */
183*4882a593Smuzhiyun 			sccb->header.response_code = 0x0000;
184*4882a593Smuzhiyun 			vt220_request->sclp_req.status = SCLP_REQ_FILLED;
185*4882a593Smuzhiyun 			if (sclp_add_request(request) == 0)
186*4882a593Smuzhiyun 				return;
187*4882a593Smuzhiyun 		}
188*4882a593Smuzhiyun 		break;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	case 0x0040: /* SCLP equipment check */
191*4882a593Smuzhiyun 		if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
192*4882a593Smuzhiyun 			break;
193*4882a593Smuzhiyun 		sccb->header.response_code = 0x0000;
194*4882a593Smuzhiyun 		vt220_request->sclp_req.status = SCLP_REQ_FILLED;
195*4882a593Smuzhiyun 		if (sclp_add_request(request) == 0)
196*4882a593Smuzhiyun 			return;
197*4882a593Smuzhiyun 		break;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	default:
200*4882a593Smuzhiyun 		break;
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 	sclp_vt220_process_queue(vt220_request);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun  * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
207*4882a593Smuzhiyun  * otherwise.
208*4882a593Smuzhiyun  */
209*4882a593Smuzhiyun static int
__sclp_vt220_emit(struct sclp_vt220_request * request)210*4882a593Smuzhiyun __sclp_vt220_emit(struct sclp_vt220_request *request)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
213*4882a593Smuzhiyun 	request->sclp_req.status = SCLP_REQ_FILLED;
214*4882a593Smuzhiyun 	request->sclp_req.callback = sclp_vt220_callback;
215*4882a593Smuzhiyun 	request->sclp_req.callback_data = (void *) request;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	return sclp_add_request(&request->sclp_req);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun  * Queue and emit current request.
222*4882a593Smuzhiyun  */
223*4882a593Smuzhiyun static void
sclp_vt220_emit_current(void)224*4882a593Smuzhiyun sclp_vt220_emit_current(void)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	unsigned long flags;
227*4882a593Smuzhiyun 	struct sclp_vt220_request *request;
228*4882a593Smuzhiyun 	struct sclp_vt220_sccb *sccb;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
231*4882a593Smuzhiyun 	if (sclp_vt220_current_request) {
232*4882a593Smuzhiyun 		sccb = (struct sclp_vt220_sccb *)
233*4882a593Smuzhiyun 				sclp_vt220_current_request->sclp_req.sccb;
234*4882a593Smuzhiyun 		/* Only emit buffers with content */
235*4882a593Smuzhiyun 		if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
236*4882a593Smuzhiyun 			list_add_tail(&sclp_vt220_current_request->list,
237*4882a593Smuzhiyun 				      &sclp_vt220_outqueue);
238*4882a593Smuzhiyun 			sclp_vt220_current_request = NULL;
239*4882a593Smuzhiyun 			if (timer_pending(&sclp_vt220_timer))
240*4882a593Smuzhiyun 				del_timer(&sclp_vt220_timer);
241*4882a593Smuzhiyun 		}
242*4882a593Smuzhiyun 		sclp_vt220_flush_later = 0;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 	if (sclp_vt220_queue_running || sclp_vt220_suspended)
245*4882a593Smuzhiyun 		goto out_unlock;
246*4882a593Smuzhiyun 	if (list_empty(&sclp_vt220_outqueue))
247*4882a593Smuzhiyun 		goto out_unlock;
248*4882a593Smuzhiyun 	request = list_first_entry(&sclp_vt220_outqueue,
249*4882a593Smuzhiyun 				   struct sclp_vt220_request, list);
250*4882a593Smuzhiyun 	sclp_vt220_queue_running = 1;
251*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (__sclp_vt220_emit(request))
254*4882a593Smuzhiyun 		sclp_vt220_process_queue(request);
255*4882a593Smuzhiyun 	return;
256*4882a593Smuzhiyun out_unlock:
257*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun #define SCLP_NORMAL_WRITE	0x00
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /*
263*4882a593Smuzhiyun  * Helper function to initialize a page with the sclp request structure.
264*4882a593Smuzhiyun  */
265*4882a593Smuzhiyun static struct sclp_vt220_request *
sclp_vt220_initialize_page(void * page)266*4882a593Smuzhiyun sclp_vt220_initialize_page(void *page)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	struct sclp_vt220_request *request;
269*4882a593Smuzhiyun 	struct sclp_vt220_sccb *sccb;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	/* Place request structure at end of page */
272*4882a593Smuzhiyun 	request = ((struct sclp_vt220_request *)
273*4882a593Smuzhiyun 			((addr_t) page + PAGE_SIZE)) - 1;
274*4882a593Smuzhiyun 	request->retry_count = 0;
275*4882a593Smuzhiyun 	request->sclp_req.sccb = page;
276*4882a593Smuzhiyun 	/* SCCB goes at start of page */
277*4882a593Smuzhiyun 	sccb = (struct sclp_vt220_sccb *) page;
278*4882a593Smuzhiyun 	memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
279*4882a593Smuzhiyun 	sccb->header.length = sizeof(struct sclp_vt220_sccb);
280*4882a593Smuzhiyun 	sccb->header.function_code = SCLP_NORMAL_WRITE;
281*4882a593Smuzhiyun 	sccb->header.response_code = 0x0000;
282*4882a593Smuzhiyun 	sccb->evbuf.type = EVTYP_VT220MSG;
283*4882a593Smuzhiyun 	sccb->evbuf.length = sizeof(struct evbuf_header);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	return request;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun static inline unsigned int
sclp_vt220_space_left(struct sclp_vt220_request * request)289*4882a593Smuzhiyun sclp_vt220_space_left(struct sclp_vt220_request *request)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	struct sclp_vt220_sccb *sccb;
292*4882a593Smuzhiyun 	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
293*4882a593Smuzhiyun 	return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
294*4882a593Smuzhiyun 	       sccb->header.length;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun static inline unsigned int
sclp_vt220_chars_stored(struct sclp_vt220_request * request)298*4882a593Smuzhiyun sclp_vt220_chars_stored(struct sclp_vt220_request *request)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	struct sclp_vt220_sccb *sccb;
301*4882a593Smuzhiyun 	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
302*4882a593Smuzhiyun 	return sccb->evbuf.length - sizeof(struct evbuf_header);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun  * Add msg to buffer associated with request. Return the number of characters
307*4882a593Smuzhiyun  * added.
308*4882a593Smuzhiyun  */
309*4882a593Smuzhiyun static int
sclp_vt220_add_msg(struct sclp_vt220_request * request,const unsigned char * msg,int count,int convertlf)310*4882a593Smuzhiyun sclp_vt220_add_msg(struct sclp_vt220_request *request,
311*4882a593Smuzhiyun 		   const unsigned char *msg, int count, int convertlf)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	struct sclp_vt220_sccb *sccb;
314*4882a593Smuzhiyun 	void *buffer;
315*4882a593Smuzhiyun 	unsigned char c;
316*4882a593Smuzhiyun 	int from;
317*4882a593Smuzhiyun 	int to;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	if (count > sclp_vt220_space_left(request))
320*4882a593Smuzhiyun 		count = sclp_vt220_space_left(request);
321*4882a593Smuzhiyun 	if (count <= 0)
322*4882a593Smuzhiyun 		return 0;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
325*4882a593Smuzhiyun 	buffer = (void *) ((addr_t) sccb + sccb->header.length);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	if (convertlf) {
328*4882a593Smuzhiyun 		/* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
329*4882a593Smuzhiyun 		for (from=0, to=0;
330*4882a593Smuzhiyun 		     (from < count) && (to < sclp_vt220_space_left(request));
331*4882a593Smuzhiyun 		     from++) {
332*4882a593Smuzhiyun 			/* Retrieve character */
333*4882a593Smuzhiyun 			c = msg[from];
334*4882a593Smuzhiyun 			/* Perform conversion */
335*4882a593Smuzhiyun 			if (c == 0x0a) {
336*4882a593Smuzhiyun 				if (to + 1 < sclp_vt220_space_left(request)) {
337*4882a593Smuzhiyun 					((unsigned char *) buffer)[to++] = c;
338*4882a593Smuzhiyun 					((unsigned char *) buffer)[to++] = 0x0d;
339*4882a593Smuzhiyun 				} else
340*4882a593Smuzhiyun 					break;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 			} else
343*4882a593Smuzhiyun 				((unsigned char *) buffer)[to++] = c;
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 		sccb->header.length += to;
346*4882a593Smuzhiyun 		sccb->evbuf.length += to;
347*4882a593Smuzhiyun 		return from;
348*4882a593Smuzhiyun 	} else {
349*4882a593Smuzhiyun 		memcpy(buffer, (const void *) msg, count);
350*4882a593Smuzhiyun 		sccb->header.length += count;
351*4882a593Smuzhiyun 		sccb->evbuf.length += count;
352*4882a593Smuzhiyun 		return count;
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun /*
357*4882a593Smuzhiyun  * Emit buffer after having waited long enough for more data to arrive.
358*4882a593Smuzhiyun  */
359*4882a593Smuzhiyun static void
sclp_vt220_timeout(struct timer_list * unused)360*4882a593Smuzhiyun sclp_vt220_timeout(struct timer_list *unused)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	sclp_vt220_emit_current();
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun #define BUFFER_MAX_DELAY	HZ/20
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun  * Drop oldest console buffer if sclp_con_drop is set
369*4882a593Smuzhiyun  */
370*4882a593Smuzhiyun static int
sclp_vt220_drop_buffer(void)371*4882a593Smuzhiyun sclp_vt220_drop_buffer(void)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	struct list_head *list;
374*4882a593Smuzhiyun 	struct sclp_vt220_request *request;
375*4882a593Smuzhiyun 	void *page;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	if (!sclp_console_drop)
378*4882a593Smuzhiyun 		return 0;
379*4882a593Smuzhiyun 	list = sclp_vt220_outqueue.next;
380*4882a593Smuzhiyun 	if (sclp_vt220_queue_running)
381*4882a593Smuzhiyun 		/* The first element is in I/O */
382*4882a593Smuzhiyun 		list = list->next;
383*4882a593Smuzhiyun 	if (list == &sclp_vt220_outqueue)
384*4882a593Smuzhiyun 		return 0;
385*4882a593Smuzhiyun 	list_del(list);
386*4882a593Smuzhiyun 	request = list_entry(list, struct sclp_vt220_request, list);
387*4882a593Smuzhiyun 	page = request->sclp_req.sccb;
388*4882a593Smuzhiyun 	list_add_tail((struct list_head *) page, &sclp_vt220_empty);
389*4882a593Smuzhiyun 	return 1;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun  * Internal implementation of the write function. Write COUNT bytes of data
394*4882a593Smuzhiyun  * from memory at BUF
395*4882a593Smuzhiyun  * to the SCLP interface. In case that the data does not fit into the current
396*4882a593Smuzhiyun  * write buffer, emit the current one and allocate a new one. If there are no
397*4882a593Smuzhiyun  * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
398*4882a593Smuzhiyun  * is non-zero, the buffer will be scheduled for emitting after a timeout -
399*4882a593Smuzhiyun  * otherwise the user has to explicitly call the flush function.
400*4882a593Smuzhiyun  * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
401*4882a593Smuzhiyun  * buffer should be converted to 0x0a 0x0d. After completion, return the number
402*4882a593Smuzhiyun  * of bytes written.
403*4882a593Smuzhiyun  */
404*4882a593Smuzhiyun static int
__sclp_vt220_write(const unsigned char * buf,int count,int do_schedule,int convertlf,int may_fail)405*4882a593Smuzhiyun __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
406*4882a593Smuzhiyun 		   int convertlf, int may_fail)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun 	unsigned long flags;
409*4882a593Smuzhiyun 	void *page;
410*4882a593Smuzhiyun 	int written;
411*4882a593Smuzhiyun 	int overall_written;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	if (count <= 0)
414*4882a593Smuzhiyun 		return 0;
415*4882a593Smuzhiyun 	overall_written = 0;
416*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
417*4882a593Smuzhiyun 	do {
418*4882a593Smuzhiyun 		/* Create an sclp output buffer if none exists yet */
419*4882a593Smuzhiyun 		if (sclp_vt220_current_request == NULL) {
420*4882a593Smuzhiyun 			if (list_empty(&sclp_vt220_empty))
421*4882a593Smuzhiyun 				sclp_console_full++;
422*4882a593Smuzhiyun 			while (list_empty(&sclp_vt220_empty)) {
423*4882a593Smuzhiyun 				if (may_fail || sclp_vt220_suspended)
424*4882a593Smuzhiyun 					goto out;
425*4882a593Smuzhiyun 				if (sclp_vt220_drop_buffer())
426*4882a593Smuzhiyun 					break;
427*4882a593Smuzhiyun 				spin_unlock_irqrestore(&sclp_vt220_lock, flags);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 				sclp_sync_wait();
430*4882a593Smuzhiyun 				spin_lock_irqsave(&sclp_vt220_lock, flags);
431*4882a593Smuzhiyun 			}
432*4882a593Smuzhiyun 			page = (void *) sclp_vt220_empty.next;
433*4882a593Smuzhiyun 			list_del((struct list_head *) page);
434*4882a593Smuzhiyun 			sclp_vt220_current_request =
435*4882a593Smuzhiyun 				sclp_vt220_initialize_page(page);
436*4882a593Smuzhiyun 		}
437*4882a593Smuzhiyun 		/* Try to write the string to the current request buffer */
438*4882a593Smuzhiyun 		written = sclp_vt220_add_msg(sclp_vt220_current_request,
439*4882a593Smuzhiyun 					     buf, count, convertlf);
440*4882a593Smuzhiyun 		overall_written += written;
441*4882a593Smuzhiyun 		if (written == count)
442*4882a593Smuzhiyun 			break;
443*4882a593Smuzhiyun 		/*
444*4882a593Smuzhiyun 		 * Not all characters could be written to the current
445*4882a593Smuzhiyun 		 * output buffer. Emit the buffer, create a new buffer
446*4882a593Smuzhiyun 		 * and then output the rest of the string.
447*4882a593Smuzhiyun 		 */
448*4882a593Smuzhiyun 		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
449*4882a593Smuzhiyun 		sclp_vt220_emit_current();
450*4882a593Smuzhiyun 		spin_lock_irqsave(&sclp_vt220_lock, flags);
451*4882a593Smuzhiyun 		buf += written;
452*4882a593Smuzhiyun 		count -= written;
453*4882a593Smuzhiyun 	} while (count > 0);
454*4882a593Smuzhiyun 	/* Setup timer to output current console buffer after some time */
455*4882a593Smuzhiyun 	if (sclp_vt220_current_request != NULL &&
456*4882a593Smuzhiyun 	    !timer_pending(&sclp_vt220_timer) && do_schedule) {
457*4882a593Smuzhiyun 		sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
458*4882a593Smuzhiyun 		add_timer(&sclp_vt220_timer);
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun out:
461*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
462*4882a593Smuzhiyun 	return overall_written;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /*
466*4882a593Smuzhiyun  * This routine is called by the kernel to write a series of
467*4882a593Smuzhiyun  * characters to the tty device.  The characters may come from
468*4882a593Smuzhiyun  * user space or kernel space.  This routine will return the
469*4882a593Smuzhiyun  * number of characters actually accepted for writing.
470*4882a593Smuzhiyun  */
471*4882a593Smuzhiyun static int
sclp_vt220_write(struct tty_struct * tty,const unsigned char * buf,int count)472*4882a593Smuzhiyun sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	return __sclp_vt220_write(buf, count, 1, 0, 1);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun #define SCLP_VT220_SESSION_ENDED	0x01
478*4882a593Smuzhiyun #define	SCLP_VT220_SESSION_STARTED	0x80
479*4882a593Smuzhiyun #define SCLP_VT220_SESSION_DATA		0x00
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun #ifdef CONFIG_MAGIC_SYSRQ
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun static int sysrq_pressed;
484*4882a593Smuzhiyun static struct sysrq_work sysrq;
485*4882a593Smuzhiyun 
sclp_vt220_reset_session(void)486*4882a593Smuzhiyun static void sclp_vt220_reset_session(void)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	sysrq_pressed = 0;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
sclp_vt220_handle_input(const char * buffer,unsigned int count)491*4882a593Smuzhiyun static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	int i;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	for (i = 0; i < count; i++) {
496*4882a593Smuzhiyun 		/* Handle magic sys request */
497*4882a593Smuzhiyun 		if (buffer[i] == ('O' ^ 0100)) { /* CTRL-O */
498*4882a593Smuzhiyun 			/*
499*4882a593Smuzhiyun 			 * If pressed again, reset sysrq_pressed
500*4882a593Smuzhiyun 			 * and flip CTRL-O character
501*4882a593Smuzhiyun 			 */
502*4882a593Smuzhiyun 			sysrq_pressed = !sysrq_pressed;
503*4882a593Smuzhiyun 			if (sysrq_pressed)
504*4882a593Smuzhiyun 				continue;
505*4882a593Smuzhiyun 		} else if (sysrq_pressed) {
506*4882a593Smuzhiyun 			sysrq.key = buffer[i];
507*4882a593Smuzhiyun 			schedule_sysrq_work(&sysrq);
508*4882a593Smuzhiyun 			sysrq_pressed = 0;
509*4882a593Smuzhiyun 			continue;
510*4882a593Smuzhiyun 		}
511*4882a593Smuzhiyun 		tty_insert_flip_char(&sclp_vt220_port, buffer[i], 0);
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun #else
516*4882a593Smuzhiyun 
sclp_vt220_reset_session(void)517*4882a593Smuzhiyun static void sclp_vt220_reset_session(void)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
sclp_vt220_handle_input(const char * buffer,unsigned int count)521*4882a593Smuzhiyun static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	tty_insert_flip_string(&sclp_vt220_port, buffer, count);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun #endif
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun /*
529*4882a593Smuzhiyun  * Called by the SCLP to report incoming event buffers.
530*4882a593Smuzhiyun  */
531*4882a593Smuzhiyun static void
sclp_vt220_receiver_fn(struct evbuf_header * evbuf)532*4882a593Smuzhiyun sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	char *buffer;
535*4882a593Smuzhiyun 	unsigned int count;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
538*4882a593Smuzhiyun 	count = evbuf->length - sizeof(struct evbuf_header);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	switch (*buffer) {
541*4882a593Smuzhiyun 	case SCLP_VT220_SESSION_ENDED:
542*4882a593Smuzhiyun 	case SCLP_VT220_SESSION_STARTED:
543*4882a593Smuzhiyun 		sclp_vt220_reset_session();
544*4882a593Smuzhiyun 		break;
545*4882a593Smuzhiyun 	case SCLP_VT220_SESSION_DATA:
546*4882a593Smuzhiyun 		/* Send input to line discipline */
547*4882a593Smuzhiyun 		buffer++;
548*4882a593Smuzhiyun 		count--;
549*4882a593Smuzhiyun 		sclp_vt220_handle_input(buffer, count);
550*4882a593Smuzhiyun 		tty_flip_buffer_push(&sclp_vt220_port);
551*4882a593Smuzhiyun 		break;
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /*
556*4882a593Smuzhiyun  * This routine is called when a particular tty device is opened.
557*4882a593Smuzhiyun  */
558*4882a593Smuzhiyun static int
sclp_vt220_open(struct tty_struct * tty,struct file * filp)559*4882a593Smuzhiyun sclp_vt220_open(struct tty_struct *tty, struct file *filp)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	if (tty->count == 1) {
562*4882a593Smuzhiyun 		tty_port_tty_set(&sclp_vt220_port, tty);
563*4882a593Smuzhiyun 		sclp_vt220_port.low_latency = 0;
564*4882a593Smuzhiyun 		if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
565*4882a593Smuzhiyun 			tty->winsize.ws_row = 24;
566*4882a593Smuzhiyun 			tty->winsize.ws_col = 80;
567*4882a593Smuzhiyun 		}
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 	return 0;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun  * This routine is called when a particular tty device is closed.
574*4882a593Smuzhiyun  */
575*4882a593Smuzhiyun static void
sclp_vt220_close(struct tty_struct * tty,struct file * filp)576*4882a593Smuzhiyun sclp_vt220_close(struct tty_struct *tty, struct file *filp)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	if (tty->count == 1)
579*4882a593Smuzhiyun 		tty_port_tty_set(&sclp_vt220_port, NULL);
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun /*
583*4882a593Smuzhiyun  * This routine is called by the kernel to write a single
584*4882a593Smuzhiyun  * character to the tty device.  If the kernel uses this routine,
585*4882a593Smuzhiyun  * it must call the flush_chars() routine (if defined) when it is
586*4882a593Smuzhiyun  * done stuffing characters into the driver.
587*4882a593Smuzhiyun  */
588*4882a593Smuzhiyun static int
sclp_vt220_put_char(struct tty_struct * tty,unsigned char ch)589*4882a593Smuzhiyun sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	return __sclp_vt220_write(&ch, 1, 0, 0, 1);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /*
595*4882a593Smuzhiyun  * This routine is called by the kernel after it has written a
596*4882a593Smuzhiyun  * series of characters to the tty device using put_char().
597*4882a593Smuzhiyun  */
598*4882a593Smuzhiyun static void
sclp_vt220_flush_chars(struct tty_struct * tty)599*4882a593Smuzhiyun sclp_vt220_flush_chars(struct tty_struct *tty)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun 	if (!sclp_vt220_queue_running)
602*4882a593Smuzhiyun 		sclp_vt220_emit_current();
603*4882a593Smuzhiyun 	else
604*4882a593Smuzhiyun 		sclp_vt220_flush_later = 1;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun  * This routine returns the numbers of characters the tty driver
609*4882a593Smuzhiyun  * will accept for queuing to be written.  This number is subject
610*4882a593Smuzhiyun  * to change as output buffers get emptied, or if the output flow
611*4882a593Smuzhiyun  * control is acted.
612*4882a593Smuzhiyun  */
613*4882a593Smuzhiyun static int
sclp_vt220_write_room(struct tty_struct * tty)614*4882a593Smuzhiyun sclp_vt220_write_room(struct tty_struct *tty)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	unsigned long flags;
617*4882a593Smuzhiyun 	struct list_head *l;
618*4882a593Smuzhiyun 	int count;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
621*4882a593Smuzhiyun 	count = 0;
622*4882a593Smuzhiyun 	if (sclp_vt220_current_request != NULL)
623*4882a593Smuzhiyun 		count = sclp_vt220_space_left(sclp_vt220_current_request);
624*4882a593Smuzhiyun 	list_for_each(l, &sclp_vt220_empty)
625*4882a593Smuzhiyun 		count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
626*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
627*4882a593Smuzhiyun 	return count;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun /*
631*4882a593Smuzhiyun  * Return number of buffered chars.
632*4882a593Smuzhiyun  */
633*4882a593Smuzhiyun static int
sclp_vt220_chars_in_buffer(struct tty_struct * tty)634*4882a593Smuzhiyun sclp_vt220_chars_in_buffer(struct tty_struct *tty)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun 	unsigned long flags;
637*4882a593Smuzhiyun 	struct list_head *l;
638*4882a593Smuzhiyun 	struct sclp_vt220_request *r;
639*4882a593Smuzhiyun 	int count;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
642*4882a593Smuzhiyun 	count = 0;
643*4882a593Smuzhiyun 	if (sclp_vt220_current_request != NULL)
644*4882a593Smuzhiyun 		count = sclp_vt220_chars_stored(sclp_vt220_current_request);
645*4882a593Smuzhiyun 	list_for_each(l, &sclp_vt220_outqueue) {
646*4882a593Smuzhiyun 		r = list_entry(l, struct sclp_vt220_request, list);
647*4882a593Smuzhiyun 		count += sclp_vt220_chars_stored(r);
648*4882a593Smuzhiyun 	}
649*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
650*4882a593Smuzhiyun 	return count;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun /*
654*4882a593Smuzhiyun  * Pass on all buffers to the hardware. Return only when there are no more
655*4882a593Smuzhiyun  * buffers pending.
656*4882a593Smuzhiyun  */
657*4882a593Smuzhiyun static void
sclp_vt220_flush_buffer(struct tty_struct * tty)658*4882a593Smuzhiyun sclp_vt220_flush_buffer(struct tty_struct *tty)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	sclp_vt220_emit_current();
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun /* Release allocated pages. */
__sclp_vt220_free_pages(void)664*4882a593Smuzhiyun static void __init __sclp_vt220_free_pages(void)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	struct list_head *page, *p;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	list_for_each_safe(page, p, &sclp_vt220_empty) {
669*4882a593Smuzhiyun 		list_del(page);
670*4882a593Smuzhiyun 		free_page((unsigned long) page);
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun /* Release memory and unregister from sclp core. Controlled by init counting -
675*4882a593Smuzhiyun  * only the last invoker will actually perform these actions. */
__sclp_vt220_cleanup(void)676*4882a593Smuzhiyun static void __init __sclp_vt220_cleanup(void)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	sclp_vt220_init_count--;
679*4882a593Smuzhiyun 	if (sclp_vt220_init_count != 0)
680*4882a593Smuzhiyun 		return;
681*4882a593Smuzhiyun 	sclp_unregister(&sclp_vt220_register);
682*4882a593Smuzhiyun 	__sclp_vt220_free_pages();
683*4882a593Smuzhiyun 	tty_port_destroy(&sclp_vt220_port);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun /* Allocate buffer pages and register with sclp core. Controlled by init
687*4882a593Smuzhiyun  * counting - only the first invoker will actually perform these actions. */
__sclp_vt220_init(int num_pages)688*4882a593Smuzhiyun static int __init __sclp_vt220_init(int num_pages)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun 	void *page;
691*4882a593Smuzhiyun 	int i;
692*4882a593Smuzhiyun 	int rc;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	sclp_vt220_init_count++;
695*4882a593Smuzhiyun 	if (sclp_vt220_init_count != 1)
696*4882a593Smuzhiyun 		return 0;
697*4882a593Smuzhiyun 	spin_lock_init(&sclp_vt220_lock);
698*4882a593Smuzhiyun 	INIT_LIST_HEAD(&sclp_vt220_empty);
699*4882a593Smuzhiyun 	INIT_LIST_HEAD(&sclp_vt220_outqueue);
700*4882a593Smuzhiyun 	timer_setup(&sclp_vt220_timer, sclp_vt220_timeout, 0);
701*4882a593Smuzhiyun 	tty_port_init(&sclp_vt220_port);
702*4882a593Smuzhiyun 	sclp_vt220_current_request = NULL;
703*4882a593Smuzhiyun 	sclp_vt220_buffered_chars = 0;
704*4882a593Smuzhiyun 	sclp_vt220_flush_later = 0;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	/* Allocate pages for output buffering */
707*4882a593Smuzhiyun 	rc = -ENOMEM;
708*4882a593Smuzhiyun 	for (i = 0; i < num_pages; i++) {
709*4882a593Smuzhiyun 		page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
710*4882a593Smuzhiyun 		if (!page)
711*4882a593Smuzhiyun 			goto out;
712*4882a593Smuzhiyun 		list_add_tail(page, &sclp_vt220_empty);
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 	rc = sclp_register(&sclp_vt220_register);
715*4882a593Smuzhiyun out:
716*4882a593Smuzhiyun 	if (rc) {
717*4882a593Smuzhiyun 		__sclp_vt220_free_pages();
718*4882a593Smuzhiyun 		sclp_vt220_init_count--;
719*4882a593Smuzhiyun 		tty_port_destroy(&sclp_vt220_port);
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun 	return rc;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun static const struct tty_operations sclp_vt220_ops = {
725*4882a593Smuzhiyun 	.open = sclp_vt220_open,
726*4882a593Smuzhiyun 	.close = sclp_vt220_close,
727*4882a593Smuzhiyun 	.write = sclp_vt220_write,
728*4882a593Smuzhiyun 	.put_char = sclp_vt220_put_char,
729*4882a593Smuzhiyun 	.flush_chars = sclp_vt220_flush_chars,
730*4882a593Smuzhiyun 	.write_room = sclp_vt220_write_room,
731*4882a593Smuzhiyun 	.chars_in_buffer = sclp_vt220_chars_in_buffer,
732*4882a593Smuzhiyun 	.flush_buffer = sclp_vt220_flush_buffer,
733*4882a593Smuzhiyun };
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun /*
736*4882a593Smuzhiyun  * Register driver with SCLP and Linux and initialize internal tty structures.
737*4882a593Smuzhiyun  */
sclp_vt220_tty_init(void)738*4882a593Smuzhiyun static int __init sclp_vt220_tty_init(void)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	struct tty_driver *driver;
741*4882a593Smuzhiyun 	int rc;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
744*4882a593Smuzhiyun 	 * symmetry between VM and LPAR systems regarding ttyS1. */
745*4882a593Smuzhiyun 	driver = alloc_tty_driver(1);
746*4882a593Smuzhiyun 	if (!driver)
747*4882a593Smuzhiyun 		return -ENOMEM;
748*4882a593Smuzhiyun 	rc = __sclp_vt220_init(MAX_KMEM_PAGES);
749*4882a593Smuzhiyun 	if (rc)
750*4882a593Smuzhiyun 		goto out_driver;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	driver->driver_name = SCLP_VT220_DRIVER_NAME;
753*4882a593Smuzhiyun 	driver->name = SCLP_VT220_DEVICE_NAME;
754*4882a593Smuzhiyun 	driver->major = SCLP_VT220_MAJOR;
755*4882a593Smuzhiyun 	driver->minor_start = SCLP_VT220_MINOR;
756*4882a593Smuzhiyun 	driver->type = TTY_DRIVER_TYPE_SYSTEM;
757*4882a593Smuzhiyun 	driver->subtype = SYSTEM_TYPE_TTY;
758*4882a593Smuzhiyun 	driver->init_termios = tty_std_termios;
759*4882a593Smuzhiyun 	driver->flags = TTY_DRIVER_REAL_RAW;
760*4882a593Smuzhiyun 	tty_set_operations(driver, &sclp_vt220_ops);
761*4882a593Smuzhiyun 	tty_port_link_device(&sclp_vt220_port, driver, 0);
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	rc = tty_register_driver(driver);
764*4882a593Smuzhiyun 	if (rc)
765*4882a593Smuzhiyun 		goto out_init;
766*4882a593Smuzhiyun 	rc = sclp_register(&sclp_vt220_register_input);
767*4882a593Smuzhiyun 	if (rc)
768*4882a593Smuzhiyun 		goto out_reg;
769*4882a593Smuzhiyun 	sclp_vt220_driver = driver;
770*4882a593Smuzhiyun 	return 0;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun out_reg:
773*4882a593Smuzhiyun 	tty_unregister_driver(driver);
774*4882a593Smuzhiyun out_init:
775*4882a593Smuzhiyun 	__sclp_vt220_cleanup();
776*4882a593Smuzhiyun out_driver:
777*4882a593Smuzhiyun 	put_tty_driver(driver);
778*4882a593Smuzhiyun 	return rc;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun __initcall(sclp_vt220_tty_init);
781*4882a593Smuzhiyun 
__sclp_vt220_flush_buffer(void)782*4882a593Smuzhiyun static void __sclp_vt220_flush_buffer(void)
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun 	unsigned long flags;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	sclp_vt220_emit_current();
787*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
788*4882a593Smuzhiyun 	if (timer_pending(&sclp_vt220_timer))
789*4882a593Smuzhiyun 		del_timer(&sclp_vt220_timer);
790*4882a593Smuzhiyun 	while (sclp_vt220_queue_running) {
791*4882a593Smuzhiyun 		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
792*4882a593Smuzhiyun 		sclp_sync_wait();
793*4882a593Smuzhiyun 		spin_lock_irqsave(&sclp_vt220_lock, flags);
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /*
799*4882a593Smuzhiyun  * Resume console: If there are cached messages, emit them.
800*4882a593Smuzhiyun  */
sclp_vt220_resume(void)801*4882a593Smuzhiyun static void sclp_vt220_resume(void)
802*4882a593Smuzhiyun {
803*4882a593Smuzhiyun 	unsigned long flags;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
806*4882a593Smuzhiyun 	sclp_vt220_suspended = 0;
807*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
808*4882a593Smuzhiyun 	sclp_vt220_emit_current();
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun /*
812*4882a593Smuzhiyun  * Suspend console: Set suspend flag and flush console
813*4882a593Smuzhiyun  */
sclp_vt220_suspend(void)814*4882a593Smuzhiyun static void sclp_vt220_suspend(void)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun 	unsigned long flags;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	spin_lock_irqsave(&sclp_vt220_lock, flags);
819*4882a593Smuzhiyun 	sclp_vt220_suspended = 1;
820*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
821*4882a593Smuzhiyun 	__sclp_vt220_flush_buffer();
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun 
sclp_vt220_pm_event_fn(struct sclp_register * reg,enum sclp_pm_event sclp_pm_event)824*4882a593Smuzhiyun static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
825*4882a593Smuzhiyun 				   enum sclp_pm_event sclp_pm_event)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	switch (sclp_pm_event) {
828*4882a593Smuzhiyun 	case SCLP_PM_EVENT_FREEZE:
829*4882a593Smuzhiyun 		sclp_vt220_suspend();
830*4882a593Smuzhiyun 		break;
831*4882a593Smuzhiyun 	case SCLP_PM_EVENT_RESTORE:
832*4882a593Smuzhiyun 	case SCLP_PM_EVENT_THAW:
833*4882a593Smuzhiyun 		sclp_vt220_resume();
834*4882a593Smuzhiyun 		break;
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun #ifdef CONFIG_SCLP_VT220_CONSOLE
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun static void
sclp_vt220_con_write(struct console * con,const char * buf,unsigned int count)841*4882a593Smuzhiyun sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
842*4882a593Smuzhiyun {
843*4882a593Smuzhiyun 	__sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun static struct tty_driver *
sclp_vt220_con_device(struct console * c,int * index)847*4882a593Smuzhiyun sclp_vt220_con_device(struct console *c, int *index)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	*index = 0;
850*4882a593Smuzhiyun 	return sclp_vt220_driver;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun static int
sclp_vt220_notify(struct notifier_block * self,unsigned long event,void * data)854*4882a593Smuzhiyun sclp_vt220_notify(struct notifier_block *self,
855*4882a593Smuzhiyun 			  unsigned long event, void *data)
856*4882a593Smuzhiyun {
857*4882a593Smuzhiyun 	__sclp_vt220_flush_buffer();
858*4882a593Smuzhiyun 	return NOTIFY_OK;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun static struct notifier_block on_panic_nb = {
862*4882a593Smuzhiyun 	.notifier_call = sclp_vt220_notify,
863*4882a593Smuzhiyun 	.priority = 1,
864*4882a593Smuzhiyun };
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun static struct notifier_block on_reboot_nb = {
867*4882a593Smuzhiyun 	.notifier_call = sclp_vt220_notify,
868*4882a593Smuzhiyun 	.priority = 1,
869*4882a593Smuzhiyun };
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun /* Structure needed to register with printk */
872*4882a593Smuzhiyun static struct console sclp_vt220_console =
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	.name = SCLP_VT220_CONSOLE_NAME,
875*4882a593Smuzhiyun 	.write = sclp_vt220_con_write,
876*4882a593Smuzhiyun 	.device = sclp_vt220_con_device,
877*4882a593Smuzhiyun 	.flags = CON_PRINTBUFFER,
878*4882a593Smuzhiyun 	.index = SCLP_VT220_CONSOLE_INDEX
879*4882a593Smuzhiyun };
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun static int __init
sclp_vt220_con_init(void)882*4882a593Smuzhiyun sclp_vt220_con_init(void)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun 	int rc;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	rc = __sclp_vt220_init(sclp_console_pages);
887*4882a593Smuzhiyun 	if (rc)
888*4882a593Smuzhiyun 		return rc;
889*4882a593Smuzhiyun 	/* Attach linux console */
890*4882a593Smuzhiyun 	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
891*4882a593Smuzhiyun 	register_reboot_notifier(&on_reboot_nb);
892*4882a593Smuzhiyun 	register_console(&sclp_vt220_console);
893*4882a593Smuzhiyun 	return 0;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun console_initcall(sclp_vt220_con_init);
897*4882a593Smuzhiyun #endif /* CONFIG_SCLP_VT220_CONSOLE */
898*4882a593Smuzhiyun 
899