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