ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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, buf, length);
494                 if (length == 0) {
495                         if (!ret)
496                                 return -EFAULT;
497                         break;
498                 }
499                 length = __sclp_vt220_write(tty->driver_data, length, 1, 0);
500                 buf += length;
501                 count -= length;
502                 ret += length;
503         }
504         return ret;
505 }
506
507 #define SCLP_VT220_SESSION_ENDED        0x01
508 #define SCLP_VT220_SESSION_STARTED      0x80
509 #define SCLP_VT220_SESSION_DATA         0x00
510
511 /*
512  * Called by the SCLP to report incoming event buffers.
513  */
514 static void
515 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
516 {
517         char *buffer;
518         unsigned int count;
519
520         /* Ignore input if device is not open */
521         if (sclp_vt220_tty == NULL)
522                 return;
523
524         buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
525         count = evbuf->length - sizeof(struct evbuf_header);
526
527         switch (*buffer) {
528         case SCLP_VT220_SESSION_ENDED:
529         case SCLP_VT220_SESSION_STARTED:
530                 break;
531         case SCLP_VT220_SESSION_DATA:
532                 /* Send input to line discipline */
533                 buffer++;
534                 count--;
535                 /* Prevent buffer overrun by discarding input. Note that
536                  * because buffer_push works asynchronously, we cannot wait
537                  * for the buffer to be emptied. */
538                 if (count + sclp_vt220_tty->flip.count > TTY_FLIPBUF_SIZE)
539                         count = TTY_FLIPBUF_SIZE - sclp_vt220_tty->flip.count;
540                 memcpy(sclp_vt220_tty->flip.char_buf_ptr, buffer, count);
541                 memset(sclp_vt220_tty->flip.flag_buf_ptr, TTY_NORMAL, count);
542                 sclp_vt220_tty->flip.char_buf_ptr += count;
543                 sclp_vt220_tty->flip.flag_buf_ptr += count;
544                 sclp_vt220_tty->flip.count += count;
545                 tty_flip_buffer_push(sclp_vt220_tty);
546                 break;
547         }
548 }
549
550 /*
551  * This routine is called when a particular tty device is opened.
552  */
553 static int
554 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
555 {
556         if (tty->count == 1) {
557                 sclp_vt220_tty = tty;
558                 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
559                 if (tty->driver_data == NULL)
560                         return -ENOMEM;
561                 tty->low_latency = 0;
562         }
563         return 0;
564 }
565
566 /*
567  * This routine is called when a particular tty device is closed.
568  */
569 static void
570 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
571 {
572         if (tty->count == 1) {
573                 sclp_vt220_tty = NULL;
574                 kfree(tty->driver_data);
575                 tty->driver_data = NULL;
576         }
577 }
578
579 /*
580  * This routine is called by the kernel to write a single
581  * character to the tty device.  If the kernel uses this routine,
582  * it must call the flush_chars() routine (if defined) when it is
583  * done stuffing characters into the driver.
584  *
585  * NOTE: include/linux/tty_driver.h specifies that a character should be
586  * ignored if there is no room in the queue. This driver implements a different
587  * semantic in that it will block when there is no more room left.
588  */
589 static void
590 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
591 {
592         __sclp_vt220_write(&ch, 1, 0, 0);
593 }
594
595 /*
596  * This routine is called by the kernel after it has written a
597  * series of characters to the tty device using put_char().  
598  */
599 static void
600 sclp_vt220_flush_chars(struct tty_struct *tty)
601 {
602         if (sclp_vt220_outqueue_count == 0)
603                 sclp_vt220_emit_current();
604         else
605                 sclp_vt220_flush_later = 1;
606 }
607
608 /*
609  * This routine returns the numbers of characters the tty driver
610  * will accept for queuing to be written.  This number is subject
611  * to change as output buffers get emptied, or if the output flow
612  * control is acted.
613  */
614 static int
615 sclp_vt220_write_room(struct tty_struct *tty)
616 {
617         unsigned long flags;
618         struct list_head *l;
619         int count;
620
621         spin_lock_irqsave(&sclp_vt220_lock, flags);
622         count = 0;
623         if (sclp_vt220_current_request != NULL)
624                 count = sclp_vt220_space_left(sclp_vt220_current_request);
625         list_for_each(l, &sclp_vt220_empty)
626                 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
627         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
628         return count;
629 }
630
631 /*
632  * Return number of buffered chars.
633  */
634 static int
635 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
636 {
637         unsigned long flags;
638         struct list_head *l;
639         struct sclp_vt220_request *r;
640         int count;
641
642         spin_lock_irqsave(&sclp_vt220_lock, flags);
643         count = 0;
644         if (sclp_vt220_current_request != NULL)
645                 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
646         list_for_each(l, &sclp_vt220_outqueue) {
647                 r = list_entry(l, struct sclp_vt220_request, list);
648                 count += sclp_vt220_chars_stored(r);
649         }
650         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
651         return count;
652 }
653
654 static void
655 __sclp_vt220_flush_buffer(void)
656 {
657         unsigned long flags;
658
659         sclp_vt220_emit_current();
660         spin_lock_irqsave(&sclp_vt220_lock, flags);
661         if (timer_pending(&sclp_vt220_timer))
662                 del_timer(&sclp_vt220_timer);
663         while (sclp_vt220_outqueue_count > 0) {
664                 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
665                 sclp_sync_wait();
666                 spin_lock_irqsave(&sclp_vt220_lock, flags);
667         }
668         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
669 }
670
671 /*
672  * Pass on all buffers to the hardware. Return only when there are no more
673  * buffers pending.
674  */
675 static void
676 sclp_vt220_flush_buffer(struct tty_struct *tty)
677 {
678         sclp_vt220_emit_current();
679 }
680
681 /*
682  * Initialize all relevant components and register driver with system.
683  */
684 static int
685 __sclp_vt220_init(int early)
686 {
687         void *page;
688         int i;
689
690         if (sclp_vt220_initialized)
691                 return 0;
692         sclp_vt220_initialized = 1;
693         spin_lock_init(&sclp_vt220_lock);
694         INIT_LIST_HEAD(&sclp_vt220_empty);
695         INIT_LIST_HEAD(&sclp_vt220_outqueue);
696         init_waitqueue_head(&sclp_vt220_waitq);
697         init_timer(&sclp_vt220_timer);
698         sclp_vt220_current_request = NULL;
699         sclp_vt220_buffered_chars = 0;
700         sclp_vt220_outqueue_count = 0;
701         sclp_vt220_tty = NULL;
702         sclp_vt220_flush_later = 0;
703
704         /* Allocate pages for output buffering */
705         for (i = 0; i < (early ? MAX_CONSOLE_PAGES : MAX_KMEM_PAGES); i++) {
706                 if (early)
707                         page = alloc_bootmem_low_pages(PAGE_SIZE);
708                 else
709                         page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
710                 if (!page)
711                         return -ENOMEM;
712                 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
713         }
714         return 0;
715 }
716
717 static struct tty_operations sclp_vt220_ops = {
718         .open = sclp_vt220_open,
719         .close = sclp_vt220_close,
720         .write = sclp_vt220_write,
721         .put_char = sclp_vt220_put_char,
722         .flush_chars = sclp_vt220_flush_chars,
723         .write_room = sclp_vt220_write_room,
724         .chars_in_buffer = sclp_vt220_chars_in_buffer,
725         .flush_buffer = sclp_vt220_flush_buffer
726 };
727
728 /*
729  * Register driver with SCLP and Linux and initialize internal tty structures.
730  */
731 int __init
732 sclp_vt220_tty_init(void)
733 {
734         struct tty_driver *driver;
735         int rc;
736
737         /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
738          * symmetry between VM and LPAR systems regarding ttyS1. */
739         driver = alloc_tty_driver(1);
740         if (!driver)
741                 return -ENOMEM;
742         rc = __sclp_vt220_init(0);
743         if (rc) {
744                 put_tty_driver(driver);
745                 return rc;
746         }
747         rc = sclp_register(&sclp_vt220_register);
748         if (rc) {
749                 printk(KERN_ERR SCLP_VT220_PRINT_HEADER
750                        "could not register tty - "
751                        "sclp_register returned %d\n", rc);
752                 put_tty_driver(driver);
753                 return rc;
754         }
755
756         driver->owner = THIS_MODULE;
757         driver->driver_name = SCLP_VT220_DRIVER_NAME;
758         driver->name = SCLP_VT220_DEVICE_NAME;
759         driver->major = SCLP_VT220_MAJOR;
760         driver->minor_start = SCLP_VT220_MINOR;
761         driver->type = TTY_DRIVER_TYPE_SYSTEM;
762         driver->subtype = SYSTEM_TYPE_TTY;
763         driver->init_termios = tty_std_termios;
764         driver->flags = TTY_DRIVER_REAL_RAW;
765         tty_set_operations(driver, &sclp_vt220_ops);
766
767         rc = tty_register_driver(driver);
768         if (rc) {
769                 printk(KERN_ERR SCLP_VT220_PRINT_HEADER
770                        "could not register tty - "
771                        "tty_register_driver returned %d\n", rc);
772                 put_tty_driver(driver);
773                 return rc;
774         }
775         sclp_vt220_driver = driver;
776         return 0;
777 }
778
779 module_init(sclp_vt220_tty_init);
780
781 #ifdef CONFIG_SCLP_VT220_CONSOLE
782
783 static void
784 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
785 {
786         __sclp_vt220_write((const unsigned char *) buf, count, 1, 1);
787 }
788
789 static struct tty_driver *
790 sclp_vt220_con_device(struct console *c, int *index)
791 {
792         *index = 0;
793         return sclp_vt220_driver;
794 }
795
796 /*
797  * This routine is called from panic when the kernel is going to give up.
798  * We have to make sure that all buffers will be flushed to the SCLP.
799  * Note that this function may be called from within an interrupt context.
800  */
801 static void
802 sclp_vt220_con_unblank(void)
803 {
804         __sclp_vt220_flush_buffer();
805 }
806
807 /* Structure needed to register with printk */
808 static struct console sclp_vt220_console =
809 {
810         .name = SCLP_VT220_CONSOLE_NAME,
811         .write = sclp_vt220_con_write,
812         .device = sclp_vt220_con_device,
813         .unblank = sclp_vt220_con_unblank,
814         .flags = CON_PRINTBUFFER,
815         .index = SCLP_VT220_CONSOLE_INDEX
816 };
817
818 static int __init
819 sclp_vt220_con_init(void)
820 {
821         int rc;
822
823         if (!CONSOLE_IS_SCLP)
824                 return 0;
825         rc = __sclp_vt220_init(1);
826         if (rc)
827                 return rc;
828         /* Attach linux console */
829         register_console(&sclp_vt220_console);
830         return 0;
831 }
832
833 console_initcall(sclp_vt220_con_init);
834 #endif /* CONFIG_SCLP_VT220_CONSOLE */
835