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