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