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