69aed79f32cb73827a58730ef43856c8eebabd0d
[linux-2.6.git] / drivers / s390 / char / sclp_vt220.c
1 /*
2  *  drivers/s390/char/sclp_vt220.c
3  *    SCLP VT220 terminal driver.
4  *
5  *  S390 version
6  *    Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
8  */
9
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/timer.h>
16 #include <linux/kernel.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/sched.h>
20 #include <linux/errno.h>
21 #include <linux/mm.h>
22 #include <linux/major.h>
23 #include <linux/console.h>
24 #include <linux/kdev_t.h>
25 #include <linux/bootmem.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <asm/uaccess.h>
29 #include "sclp.h"
30
31 #define SCLP_VT220_PRINT_HEADER         "sclp vt220 tty driver: "
32 #define SCLP_VT220_MAJOR                TTY_MAJOR
33 #define SCLP_VT220_MINOR                65
34 #define SCLP_VT220_DRIVER_NAME          "sclp_vt220"
35 #define SCLP_VT220_DEVICE_NAME          "ttysclp"
36 #define SCLP_VT220_CONSOLE_NAME         "ttyS"
37 #define SCLP_VT220_CONSOLE_INDEX        1       /* console=ttyS1 */
38 #define SCLP_VT220_BUF_SIZE             80
39
40 /* Representation of a single write request */
41 struct sclp_vt220_request {
42         struct list_head list;
43         struct sclp_req sclp_req;
44         int retry_count;
45         struct timer_list retry_timer;
46 };
47
48 /* VT220 SCCB */
49 struct sclp_vt220_sccb {
50         struct sccb_header header;
51         struct evbuf_header evbuf;
52 };
53
54 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
55                                          sizeof(struct sclp_vt220_request) - \
56                                          sizeof(struct sclp_vt220_sccb))
57
58 /* Structures and data needed to register tty driver */
59 static struct tty_driver *sclp_vt220_driver;
60
61 /* The tty_struct that the kernel associated with us */
62 static struct tty_struct *sclp_vt220_tty;
63
64 /* Lock to protect internal data from concurrent access */
65 static spinlock_t sclp_vt220_lock;
66
67 /* List of empty pages to be used as write request buffers */
68 static struct list_head sclp_vt220_empty;
69
70 /* List of pending requests */
71 static struct list_head sclp_vt220_outqueue;
72
73 /* Number of requests in outqueue */
74 static int sclp_vt220_outqueue_count;
75
76 /* Wait queue used to delay write requests while we've run out of buffers */
77 static wait_queue_head_t sclp_vt220_waitq;
78
79 /* Timer used for delaying write requests to merge subsequent messages into
80  * a single buffer */
81 static struct timer_list sclp_vt220_timer;
82
83 /* Pointer to current request buffer which has been partially filled but not
84  * yet sent */
85 static struct sclp_vt220_request *sclp_vt220_current_request;
86
87 /* Number of characters in current request buffer */
88 static int sclp_vt220_buffered_chars;
89
90 /* Flag indicating whether this driver has already been initialized */
91 static int sclp_vt220_initialized = 0;
92
93 /* Flag indicating that sclp_vt220_current_request should really
94  * have been already queued but wasn't because the SCLP was processing
95  * another buffer */
96 static int sclp_vt220_flush_later;
97
98 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
99 static void __sclp_vt220_emit(struct sclp_vt220_request *request);
100 static void sclp_vt220_emit_current(void);
101
102 /* Registration structure for our interest in SCLP event buffers */
103 static struct sclp_register sclp_vt220_register = {
104         .send_mask              = EvTyp_VT220Msg_Mask,
105         .receive_mask           = EvTyp_VT220Msg_Mask,
106         .state_change_fn        = NULL,
107         .receiver_fn            = sclp_vt220_receiver_fn
108 };
109
110
111 /*
112  * Put provided request buffer back into queue and check emit pending
113  * buffers if necessary.
114  */
115 static void
116 sclp_vt220_process_queue(struct sclp_vt220_request *request)
117 {
118         unsigned long flags;
119         struct sclp_vt220_request *next;
120         void *page;
121
122         /* Put buffer back to list of empty buffers */
123         page = request->sclp_req.sccb;
124         spin_lock_irqsave(&sclp_vt220_lock, flags);
125         /* Move request from outqueue to empty queue */
126         list_del(&request->list);
127         sclp_vt220_outqueue_count--;
128         list_add_tail((struct list_head *) page, &sclp_vt220_empty);
129         /* Check if there is a pending buffer on the out queue. */
130         next = NULL;
131         if (!list_empty(&sclp_vt220_outqueue))
132                 next = list_entry(sclp_vt220_outqueue.next,
133                                   struct sclp_vt220_request, list);
134         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
135         if (next != NULL)
136                 __sclp_vt220_emit(next);
137         else if (sclp_vt220_flush_later)
138                 sclp_vt220_emit_current();
139         wake_up(&sclp_vt220_waitq);
140         /* Check if the tty needs a wake up call */
141         if (sclp_vt220_tty != NULL) {
142                 tty_wakeup(sclp_vt220_tty);
143         }
144 }
145
146 /*
147  * Retry sclp write request after waiting some time for an sclp equipment
148  * check to pass.
149  */
150 static void
151 sclp_vt220_retry(unsigned long data)
152 {
153         struct sclp_vt220_request *request;
154         struct sclp_vt220_sccb *sccb;
155
156         request = (struct sclp_vt220_request *) data;
157         request->sclp_req.status = SCLP_REQ_FILLED;
158         sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
159         sccb->header.response_code = 0x0000;
160         sclp_add_request(&request->sclp_req);
161 }
162
163 #define SCLP_BUFFER_MAX_RETRY           5
164 #define SCLP_BUFFER_RETRY_INTERVAL      2
165
166 /*
167  * Callback through which the result of a write request is reported by the
168  * SCLP.
169  */
170 static void
171 sclp_vt220_callback(struct sclp_req *request, void *data)
172 {
173         struct sclp_vt220_request *vt220_request;
174         struct sclp_vt220_sccb *sccb;
175
176         vt220_request = (struct sclp_vt220_request *) data;
177         if (request->status == SCLP_REQ_FAILED) {
178                 sclp_vt220_process_queue(vt220_request);
179                 return;
180         }
181         sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
182
183         /* Check SCLP response code and choose suitable action  */
184         switch (sccb->header.response_code) {
185         case 0x0020 :
186                 break;
187
188         case 0x05f0: /* Target resource in improper state */
189                 break;
190
191         case 0x0340: /* Contained SCLP equipment check */
192                 if (vt220_request->retry_count++ > SCLP_BUFFER_MAX_RETRY)
193                         break;
194                 /* Remove processed buffers and requeue rest */
195                 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
196                         /* Not all buffers were processed */
197                         sccb->header.response_code = 0x0000;
198                         vt220_request->sclp_req.status = SCLP_REQ_FILLED;
199                         sclp_add_request(request);
200                         return;
201                 }
202                 break;
203
204         case 0x0040: /* SCLP equipment check */
205                 if (vt220_request->retry_count++ > SCLP_BUFFER_MAX_RETRY)
206                         break;
207                 /* Wait some time, then retry request */
208                 vt220_request->retry_timer.function = sclp_vt220_retry;
209                 vt220_request->retry_timer.data =
210                         (unsigned long) vt220_request;
211                 vt220_request->retry_timer.expires =
212                         jiffies + SCLP_BUFFER_RETRY_INTERVAL*HZ;
213                 add_timer(&vt220_request->retry_timer);
214                 return;
215
216         default:
217                 break;
218         }
219         sclp_vt220_process_queue(vt220_request);
220 }
221
222 /*
223  * Emit vt220 request buffer to SCLP.
224  */
225 static void
226 __sclp_vt220_emit(struct sclp_vt220_request *request)
227 {
228         if (!(sclp_vt220_register.sclp_send_mask & EvTyp_VT220Msg_Mask)) {
229                 request->sclp_req.status = SCLP_REQ_FAILED;
230                 sclp_vt220_callback(&request->sclp_req, (void *) request);
231                 return;
232         }
233         request->sclp_req.command = SCLP_CMDW_WRITEDATA;
234         request->sclp_req.status = SCLP_REQ_FILLED;
235         request->sclp_req.callback = sclp_vt220_callback;
236         request->sclp_req.callback_data = (void *) request;
237
238         sclp_add_request(&request->sclp_req);
239 }
240
241 /*
242  * Queue and emit given request.
243  */
244 static void
245 sclp_vt220_emit(struct sclp_vt220_request *request)
246 {
247         unsigned long flags;
248         int count;
249
250         spin_lock_irqsave(&sclp_vt220_lock, flags);
251         list_add_tail(&request->list, &sclp_vt220_outqueue);
252         count = sclp_vt220_outqueue_count++;
253         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
254         /* Emit only the first buffer immediately - callback takes care of
255          * the rest */
256         if (count == 0)
257                 __sclp_vt220_emit(request);
258 }
259
260 /*
261  * Queue and emit current request.
262  */
263 static void
264 sclp_vt220_emit_current(void)
265 {
266         unsigned long flags;
267         struct sclp_vt220_request *request;
268         struct sclp_vt220_sccb *sccb;
269
270         spin_lock_irqsave(&sclp_vt220_lock, flags);
271         request = NULL;
272         if (sclp_vt220_current_request != NULL) {
273                 sccb = (struct sclp_vt220_sccb *) 
274                                 sclp_vt220_current_request->sclp_req.sccb;
275                 /* Only emit buffers with content */
276                 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
277                         request = sclp_vt220_current_request;
278                         sclp_vt220_current_request = NULL;
279                         if (timer_pending(&sclp_vt220_timer))
280                                 del_timer(&sclp_vt220_timer);
281                 }
282                 sclp_vt220_flush_later = 0;
283         }
284         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
285         if (request != NULL)
286                 sclp_vt220_emit(request);
287 }
288
289 #define SCLP_NORMAL_WRITE       0x00
290
291 /*
292  * Helper function to initialize a page with the sclp request structure.
293  */
294 static struct sclp_vt220_request *
295 sclp_vt220_initialize_page(void *page)
296 {
297         struct sclp_vt220_request *request;
298         struct sclp_vt220_sccb *sccb;
299
300         /* Place request structure at end of page */
301         request = ((struct sclp_vt220_request *)
302                         ((addr_t) page + PAGE_SIZE)) - 1;
303         init_timer(&request->retry_timer);
304         request->retry_count = 0;
305         request->sclp_req.sccb = page;
306         /* SCCB goes at start of page */
307         sccb = (struct sclp_vt220_sccb *) page;
308         memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
309         sccb->header.length = sizeof(struct sclp_vt220_sccb);
310         sccb->header.function_code = SCLP_NORMAL_WRITE;
311         sccb->header.response_code = 0x0000;
312         sccb->evbuf.type = EvTyp_VT220Msg;
313         sccb->evbuf.length = sizeof(struct evbuf_header);
314
315         return request;
316 }
317
318 static inline unsigned int
319 sclp_vt220_space_left(struct sclp_vt220_request *request)
320 {
321         struct sclp_vt220_sccb *sccb;
322         sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
323         return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
324                sccb->header.length;
325 }
326
327 static inline unsigned int
328 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
329 {
330         struct sclp_vt220_sccb *sccb;
331         sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
332         return sccb->evbuf.length - sizeof(struct evbuf_header);
333 }
334
335 /*
336  * Add msg to buffer associated with request. Return the number of characters
337  * added.
338  */
339 static int
340 sclp_vt220_add_msg(struct sclp_vt220_request *request,
341                    const unsigned char *msg, int count, int convertlf)
342 {
343         struct sclp_vt220_sccb *sccb;
344         void *buffer;
345         unsigned char c;
346         int from;
347         int to;
348
349         if (count > sclp_vt220_space_left(request))
350                 count = sclp_vt220_space_left(request);
351         if (count <= 0)
352                 return 0;
353
354         sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
355         buffer = (void *) ((addr_t) sccb + sccb->header.length);
356
357         if (convertlf) {
358                 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
359                 for (from=0, to=0;
360                      (from < count) && (to < sclp_vt220_space_left(request));
361                      from++) {
362                         /* Retrieve character */
363                         c = msg[from];
364                         /* Perform conversion */
365                         if (c == 0x0a) {
366                                 if (to + 1 < sclp_vt220_space_left(request)) {
367                                         ((unsigned char *) buffer)[to++] = c;
368                                         ((unsigned char *) buffer)[to++] = 0x0d;
369                                 } else
370                                         break;
371
372                         } else
373                                 ((unsigned char *) buffer)[to++] = c;
374                 }
375                 sccb->header.length += to;
376                 sccb->evbuf.length += to;
377                 return from;
378         } else {
379                 memcpy(buffer, (const void *) msg, count);
380                 sccb->header.length += count;
381                 sccb->evbuf.length += count;
382                 return count;
383         }
384 }
385
386 /*
387  * Emit buffer after having waited long enough for more data to arrive.
388  */
389 static void
390 sclp_vt220_timeout(unsigned long data)
391 {
392         sclp_vt220_emit_current();
393 }
394
395 #define BUFFER_MAX_DELAY        HZ/2
396
397 /* 
398  * Internal implementation of the write function. Write COUNT bytes of data
399  * from memory at BUF
400  * to the SCLP interface. In case that the data does not fit into the current
401  * write buffer, emit the current one and allocate a new one. If there are no
402  * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
403  * is non-zero, the buffer will be scheduled for emitting after a timeout -
404  * otherwise the user has to explicitly call the flush function.
405  * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
406  * buffer should be converted to 0x0a 0x0d. After completion, return the number
407  * of bytes written.
408  */
409 static int
410 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
411                    int convertlf)
412 {
413         unsigned long flags;
414         void *page;
415         int written;
416         int overall_written;
417
418         if (count <= 0)
419                 return 0;
420         overall_written = 0;
421         spin_lock_irqsave(&sclp_vt220_lock, flags);
422         do {
423                 /* Create a sclp output buffer if none exists yet */
424                 if (sclp_vt220_current_request == NULL) {
425                         while (list_empty(&sclp_vt220_empty)) {
426                                 spin_unlock_irqrestore(&sclp_vt220_lock,
427                                                        flags);
428                                 if (in_interrupt())
429                                         sclp_sync_wait();
430                                 else
431                                         wait_event(sclp_vt220_waitq,
432                                                 !list_empty(&sclp_vt220_empty));
433                                 spin_lock_irqsave(&sclp_vt220_lock, flags);
434                         }
435                         page = (void *) sclp_vt220_empty.next;
436                         list_del((struct list_head *) page);
437                         sclp_vt220_current_request =
438                                 sclp_vt220_initialize_page(page);
439                 }
440                 /* Try to write the string to the current request buffer */
441                 written = sclp_vt220_add_msg(sclp_vt220_current_request,
442                                              buf, count, convertlf);
443                 overall_written += written;
444                 if (written == count)
445                         break;
446                 /*
447                  * Not all characters could be written to the current
448                  * output buffer. Emit the buffer, create a new buffer
449                  * and then output the rest of the string.
450                  */
451                 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
452                 sclp_vt220_emit_current();
453                 spin_lock_irqsave(&sclp_vt220_lock, flags);
454                 buf += written;
455                 count -= written;
456         } while (count > 0);
457         /* Setup timer to output current console buffer after some time */
458         if (sclp_vt220_current_request != NULL &&
459             !timer_pending(&sclp_vt220_timer) && do_schedule) {
460                 sclp_vt220_timer.function = sclp_vt220_timeout;
461                 sclp_vt220_timer.data = 0UL;
462                 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
463                 add_timer(&sclp_vt220_timer);
464         }
465         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
466         return overall_written;
467 }
468
469 /*
470  * This routine is called by the kernel to write a series of
471  * characters to the tty device.  The characters may come from
472  * user space or kernel space.  This routine will return the
473  * number of characters actually accepted for writing.
474  */
475 static int
476 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
477 {
478         return __sclp_vt220_write(buf, count, 1, 0);
479 }
480
481 #define SCLP_VT220_SESSION_ENDED        0x01
482 #define SCLP_VT220_SESSION_STARTED      0x80
483 #define SCLP_VT220_SESSION_DATA         0x00
484
485 /*
486  * Called by the SCLP to report incoming event buffers.
487  */
488 static void
489 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
490 {
491         char *buffer;
492         unsigned int count;
493
494         /* Ignore input if device is not open */
495         if (sclp_vt220_tty == NULL)
496                 return;
497
498         buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
499         count = evbuf->length - sizeof(struct evbuf_header);
500
501         switch (*buffer) {
502         case SCLP_VT220_SESSION_ENDED:
503         case SCLP_VT220_SESSION_STARTED:
504                 break;
505         case SCLP_VT220_SESSION_DATA:
506                 /* Send input to line discipline */
507                 buffer++;
508                 count--;
509                 /* Prevent buffer overrun by discarding input. Note that
510                  * because buffer_push works asynchronously, we cannot wait
511                  * for the buffer to be emptied. */
512                 if (count + sclp_vt220_tty->flip.count > TTY_FLIPBUF_SIZE)
513                         count = TTY_FLIPBUF_SIZE - sclp_vt220_tty->flip.count;
514                 memcpy(sclp_vt220_tty->flip.char_buf_ptr, buffer, count);
515                 memset(sclp_vt220_tty->flip.flag_buf_ptr, TTY_NORMAL, count);
516                 sclp_vt220_tty->flip.char_buf_ptr += count;
517                 sclp_vt220_tty->flip.flag_buf_ptr += count;
518                 sclp_vt220_tty->flip.count += count;
519                 tty_flip_buffer_push(sclp_vt220_tty);
520                 break;
521         }
522 }
523
524 /*
525  * This routine is called when a particular tty device is opened.
526  */
527 static int
528 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
529 {
530         if (tty->count == 1) {
531                 sclp_vt220_tty = tty;
532                 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
533                 if (tty->driver_data == NULL)
534                         return -ENOMEM;
535                 tty->low_latency = 0;
536         }
537         return 0;
538 }
539
540 /*
541  * This routine is called when a particular tty device is closed.
542  */
543 static void
544 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
545 {
546         if (tty->count == 1) {
547                 sclp_vt220_tty = NULL;
548                 kfree(tty->driver_data);
549                 tty->driver_data = NULL;
550         }
551 }
552
553 /*
554  * This routine is called by the kernel to write a single
555  * character to the tty device.  If the kernel uses this routine,
556  * it must call the flush_chars() routine (if defined) when it is
557  * done stuffing characters into the driver.
558  *
559  * NOTE: include/linux/tty_driver.h specifies that a character should be
560  * ignored if there is no room in the queue. This driver implements a different
561  * semantic in that it will block when there is no more room left.
562  */
563 static void
564 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
565 {
566         __sclp_vt220_write(&ch, 1, 0, 0);
567 }
568
569 /*
570  * This routine is called by the kernel after it has written a
571  * series of characters to the tty device using put_char().  
572  */
573 static void
574 sclp_vt220_flush_chars(struct tty_struct *tty)
575 {
576         if (sclp_vt220_outqueue_count == 0)
577                 sclp_vt220_emit_current();
578         else
579                 sclp_vt220_flush_later = 1;
580 }
581
582 /*
583  * This routine returns the numbers of characters the tty driver
584  * will accept for queuing to be written.  This number is subject
585  * to change as output buffers get emptied, or if the output flow
586  * control is acted.
587  */
588 static int
589 sclp_vt220_write_room(struct tty_struct *tty)
590 {
591         unsigned long flags;
592         struct list_head *l;
593         int count;
594
595         spin_lock_irqsave(&sclp_vt220_lock, flags);
596         count = 0;
597         if (sclp_vt220_current_request != NULL)
598                 count = sclp_vt220_space_left(sclp_vt220_current_request);
599         list_for_each(l, &sclp_vt220_empty)
600                 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
601         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
602         return count;
603 }
604
605 /*
606  * Return number of buffered chars.
607  */
608 static int
609 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
610 {
611         unsigned long flags;
612         struct list_head *l;
613         struct sclp_vt220_request *r;
614         int count;
615
616         spin_lock_irqsave(&sclp_vt220_lock, flags);
617         count = 0;
618         if (sclp_vt220_current_request != NULL)
619                 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
620         list_for_each(l, &sclp_vt220_outqueue) {
621                 r = list_entry(l, struct sclp_vt220_request, list);
622                 count += sclp_vt220_chars_stored(r);
623         }
624         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
625         return count;
626 }
627
628 static void
629 __sclp_vt220_flush_buffer(void)
630 {
631         unsigned long flags;
632
633         sclp_vt220_emit_current();
634         spin_lock_irqsave(&sclp_vt220_lock, flags);
635         if (timer_pending(&sclp_vt220_timer))
636                 del_timer(&sclp_vt220_timer);
637         while (sclp_vt220_outqueue_count > 0) {
638                 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
639                 sclp_sync_wait();
640                 spin_lock_irqsave(&sclp_vt220_lock, flags);
641         }
642         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
643 }
644
645 /*
646  * Pass on all buffers to the hardware. Return only when there are no more
647  * buffers pending.
648  */
649 static void
650 sclp_vt220_flush_buffer(struct tty_struct *tty)
651 {
652         sclp_vt220_emit_current();
653 }
654
655 /*
656  * Initialize all relevant components and register driver with system.
657  */
658 static int
659 __sclp_vt220_init(int early)
660 {
661         void *page;
662         int i;
663
664         if (sclp_vt220_initialized)
665                 return 0;
666         sclp_vt220_initialized = 1;
667         spin_lock_init(&sclp_vt220_lock);
668         INIT_LIST_HEAD(&sclp_vt220_empty);
669         INIT_LIST_HEAD(&sclp_vt220_outqueue);
670         init_waitqueue_head(&sclp_vt220_waitq);
671         init_timer(&sclp_vt220_timer);
672         sclp_vt220_current_request = NULL;
673         sclp_vt220_buffered_chars = 0;
674         sclp_vt220_outqueue_count = 0;
675         sclp_vt220_tty = NULL;
676         sclp_vt220_flush_later = 0;
677
678         /* Allocate pages for output buffering */
679         for (i = 0; i < (early ? MAX_CONSOLE_PAGES : MAX_KMEM_PAGES); i++) {
680                 if (early)
681                         page = alloc_bootmem_low_pages(PAGE_SIZE);
682                 else
683                         page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
684                 if (!page)
685                         return -ENOMEM;
686                 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
687         }
688         return 0;
689 }
690
691 static struct tty_operations sclp_vt220_ops = {
692         .open = sclp_vt220_open,
693         .close = sclp_vt220_close,
694         .write = sclp_vt220_write,
695         .put_char = sclp_vt220_put_char,
696         .flush_chars = sclp_vt220_flush_chars,
697         .write_room = sclp_vt220_write_room,
698         .chars_in_buffer = sclp_vt220_chars_in_buffer,
699         .flush_buffer = sclp_vt220_flush_buffer
700 };
701
702 /*
703  * Register driver with SCLP and Linux and initialize internal tty structures.
704  */
705 int __init
706 sclp_vt220_tty_init(void)
707 {
708         struct tty_driver *driver;
709         int rc;
710
711         /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
712          * symmetry between VM and LPAR systems regarding ttyS1. */
713         driver = alloc_tty_driver(1);
714         if (!driver)
715                 return -ENOMEM;
716         rc = __sclp_vt220_init(0);
717         if (rc) {
718                 put_tty_driver(driver);
719                 return rc;
720         }
721         rc = sclp_register(&sclp_vt220_register);
722         if (rc) {
723                 printk(KERN_ERR SCLP_VT220_PRINT_HEADER
724                        "could not register tty - "
725                        "sclp_register returned %d\n", rc);
726                 put_tty_driver(driver);
727                 return rc;
728         }
729
730         driver->owner = THIS_MODULE;
731         driver->driver_name = SCLP_VT220_DRIVER_NAME;
732         driver->name = SCLP_VT220_DEVICE_NAME;
733         driver->major = SCLP_VT220_MAJOR;
734         driver->minor_start = SCLP_VT220_MINOR;
735         driver->type = TTY_DRIVER_TYPE_SYSTEM;
736         driver->subtype = SYSTEM_TYPE_TTY;
737         driver->init_termios = tty_std_termios;
738         driver->flags = TTY_DRIVER_REAL_RAW;
739         tty_set_operations(driver, &sclp_vt220_ops);
740
741         rc = tty_register_driver(driver);
742         if (rc) {
743                 printk(KERN_ERR SCLP_VT220_PRINT_HEADER
744                        "could not register tty - "
745                        "tty_register_driver returned %d\n", rc);
746                 put_tty_driver(driver);
747                 return rc;
748         }
749         sclp_vt220_driver = driver;
750         return 0;
751 }
752
753 module_init(sclp_vt220_tty_init);
754
755 #ifdef CONFIG_SCLP_VT220_CONSOLE
756
757 static void
758 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
759 {
760         __sclp_vt220_write((const unsigned char *) buf, count, 1, 1);
761 }
762
763 static struct tty_driver *
764 sclp_vt220_con_device(struct console *c, int *index)
765 {
766         *index = 0;
767         return sclp_vt220_driver;
768 }
769
770 /*
771  * This routine is called from panic when the kernel is going to give up.
772  * We have to make sure that all buffers will be flushed to the SCLP.
773  * Note that this function may be called from within an interrupt context.
774  */
775 static void
776 sclp_vt220_con_unblank(void)
777 {
778         __sclp_vt220_flush_buffer();
779 }
780
781 /* Structure needed to register with printk */
782 static struct console sclp_vt220_console =
783 {
784         .name = SCLP_VT220_CONSOLE_NAME,
785         .write = sclp_vt220_con_write,
786         .device = sclp_vt220_con_device,
787         .unblank = sclp_vt220_con_unblank,
788         .flags = CON_PRINTBUFFER,
789         .index = SCLP_VT220_CONSOLE_INDEX
790 };
791
792 static int __init
793 sclp_vt220_con_init(void)
794 {
795         int rc;
796
797         if (!CONSOLE_IS_SCLP)
798                 return 0;
799         rc = __sclp_vt220_init(1);
800         if (rc)
801                 return rc;
802         /* Attach linux console */
803         register_console(&sclp_vt220_console);
804         return 0;
805 }
806
807 console_initcall(sclp_vt220_con_init);
808 #endif /* CONFIG_SCLP_VT220_CONSOLE */
809