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