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