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