blob: a839aa531d7c0f011cbe6e135ce3bac897333d7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/sclp_vt220.c
3 * SCLP VT220 terminal driver.
4 *
5 * S390 version
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +01006 * Copyright IBM Corp. 2003,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/spinlock.h>
12#include <linux/list.h>
13#include <linux/wait.h>
14#include <linux/timer.h>
15#include <linux/kernel.h>
16#include <linux/tty.h>
17#include <linux/tty_driver.h>
Alan Cox33f0f882006-01-09 20:54:13 -080018#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/mm.h>
21#include <linux/major.h>
22#include <linux/console.h>
23#include <linux/kdev_t.h>
24#include <linux/bootmem.h>
25#include <linux/interrupt.h>
26#include <linux/init.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020027#include <linux/reboot.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include "sclp.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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 */
41struct sclp_vt220_request {
42 struct list_head list;
43 struct sclp_req sclp_req;
44 int retry_count;
45};
46
47/* VT220 SCCB */
48struct sclp_vt220_sccb {
49 struct sccb_header header;
50 struct evbuf_header evbuf;
51};
52
53#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
54 sizeof(struct sclp_vt220_request) - \
55 sizeof(struct sclp_vt220_sccb))
56
57/* Structures and data needed to register tty driver */
58static struct tty_driver *sclp_vt220_driver;
59
60/* The tty_struct that the kernel associated with us */
61static struct tty_struct *sclp_vt220_tty;
62
63/* Lock to protect internal data from concurrent access */
64static spinlock_t sclp_vt220_lock;
65
66/* List of empty pages to be used as write request buffers */
67static struct list_head sclp_vt220_empty;
68
69/* List of pending requests */
70static struct list_head sclp_vt220_outqueue;
71
72/* Number of requests in outqueue */
73static int sclp_vt220_outqueue_count;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Timer used for delaying write requests to merge subsequent messages into
76 * a single buffer */
77static struct timer_list sclp_vt220_timer;
78
79/* Pointer to current request buffer which has been partially filled but not
80 * yet sent */
81static struct sclp_vt220_request *sclp_vt220_current_request;
82
83/* Number of characters in current request buffer */
84static int sclp_vt220_buffered_chars;
85
Peter Oberparleiterad211792008-07-14 09:59:07 +020086/* Counter controlling core driver initialization. */
87static int __initdata sclp_vt220_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* Flag indicating that sclp_vt220_current_request should really
90 * have been already queued but wasn't because the SCLP was processing
91 * another buffer */
92static int sclp_vt220_flush_later;
93
94static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
95static int __sclp_vt220_emit(struct sclp_vt220_request *request);
96static void sclp_vt220_emit_current(void);
97
98/* Registration structure for our interest in SCLP event buffers */
99static struct sclp_register sclp_vt220_register = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200100 .send_mask = EVTYP_VT220MSG_MASK,
101 .receive_mask = EVTYP_VT220MSG_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .state_change_fn = NULL,
103 .receiver_fn = sclp_vt220_receiver_fn
104};
105
106
107/*
108 * Put provided request buffer back into queue and check emit pending
109 * buffers if necessary.
110 */
111static void
112sclp_vt220_process_queue(struct sclp_vt220_request *request)
113{
114 unsigned long flags;
115 void *page;
116
117 do {
118 /* Put buffer back to list of empty buffers */
119 page = request->sclp_req.sccb;
120 spin_lock_irqsave(&sclp_vt220_lock, flags);
121 /* Move request from outqueue to empty queue */
122 list_del(&request->list);
123 sclp_vt220_outqueue_count--;
124 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
125 /* Check if there is a pending buffer on the out queue. */
126 request = NULL;
127 if (!list_empty(&sclp_vt220_outqueue))
128 request = list_entry(sclp_vt220_outqueue.next,
129 struct sclp_vt220_request, list);
130 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
131 } while (request && __sclp_vt220_emit(request));
132 if (request == NULL && sclp_vt220_flush_later)
133 sclp_vt220_emit_current();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Check if the tty needs a wake up call */
135 if (sclp_vt220_tty != NULL) {
136 tty_wakeup(sclp_vt220_tty);
137 }
138}
139
140#define SCLP_BUFFER_MAX_RETRY 1
141
142/*
143 * Callback through which the result of a write request is reported by the
144 * SCLP.
145 */
146static void
147sclp_vt220_callback(struct sclp_req *request, void *data)
148{
149 struct sclp_vt220_request *vt220_request;
150 struct sclp_vt220_sccb *sccb;
151
152 vt220_request = (struct sclp_vt220_request *) data;
153 if (request->status == SCLP_REQ_FAILED) {
154 sclp_vt220_process_queue(vt220_request);
155 return;
156 }
157 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
158
159 /* Check SCLP response code and choose suitable action */
160 switch (sccb->header.response_code) {
161 case 0x0020 :
162 break;
163
164 case 0x05f0: /* Target resource in improper state */
165 break;
166
167 case 0x0340: /* Contained SCLP equipment check */
168 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
169 break;
170 /* Remove processed buffers and requeue rest */
171 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
172 /* Not all buffers were processed */
173 sccb->header.response_code = 0x0000;
174 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
175 if (sclp_add_request(request) == 0)
176 return;
177 }
178 break;
179
180 case 0x0040: /* SCLP equipment check */
181 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
182 break;
183 sccb->header.response_code = 0x0000;
184 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
185 if (sclp_add_request(request) == 0)
186 return;
187 break;
188
189 default:
190 break;
191 }
192 sclp_vt220_process_queue(vt220_request);
193}
194
195/*
196 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
197 * otherwise.
198 */
199static int
200__sclp_vt220_emit(struct sclp_vt220_request *request)
201{
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100202 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 request->sclp_req.status = SCLP_REQ_FAILED;
204 return -EIO;
205 }
Heiko Carstensab14de62007-02-05 21:18:37 +0100206 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 request->sclp_req.status = SCLP_REQ_FILLED;
208 request->sclp_req.callback = sclp_vt220_callback;
209 request->sclp_req.callback_data = (void *) request;
210
211 return sclp_add_request(&request->sclp_req);
212}
213
214/*
215 * Queue and emit given request.
216 */
217static void
218sclp_vt220_emit(struct sclp_vt220_request *request)
219{
220 unsigned long flags;
221 int count;
222
223 spin_lock_irqsave(&sclp_vt220_lock, flags);
224 list_add_tail(&request->list, &sclp_vt220_outqueue);
225 count = sclp_vt220_outqueue_count++;
226 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
227 /* Emit only the first buffer immediately - callback takes care of
228 * the rest */
229 if (count == 0 && __sclp_vt220_emit(request))
230 sclp_vt220_process_queue(request);
231}
232
233/*
234 * Queue and emit current request. Return zero on success, non-zero otherwise.
235 */
236static void
237sclp_vt220_emit_current(void)
238{
239 unsigned long flags;
240 struct sclp_vt220_request *request;
241 struct sclp_vt220_sccb *sccb;
242
243 spin_lock_irqsave(&sclp_vt220_lock, flags);
244 request = NULL;
245 if (sclp_vt220_current_request != NULL) {
246 sccb = (struct sclp_vt220_sccb *)
247 sclp_vt220_current_request->sclp_req.sccb;
248 /* Only emit buffers with content */
249 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
250 request = sclp_vt220_current_request;
251 sclp_vt220_current_request = NULL;
252 if (timer_pending(&sclp_vt220_timer))
253 del_timer(&sclp_vt220_timer);
254 }
255 sclp_vt220_flush_later = 0;
256 }
257 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
258 if (request != NULL)
259 sclp_vt220_emit(request);
260}
261
262#define SCLP_NORMAL_WRITE 0x00
263
264/*
265 * Helper function to initialize a page with the sclp request structure.
266 */
267static struct sclp_vt220_request *
268sclp_vt220_initialize_page(void *page)
269{
270 struct sclp_vt220_request *request;
271 struct sclp_vt220_sccb *sccb;
272
273 /* Place request structure at end of page */
274 request = ((struct sclp_vt220_request *)
275 ((addr_t) page + PAGE_SIZE)) - 1;
276 request->retry_count = 0;
277 request->sclp_req.sccb = page;
278 /* SCCB goes at start of page */
279 sccb = (struct sclp_vt220_sccb *) page;
280 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
281 sccb->header.length = sizeof(struct sclp_vt220_sccb);
282 sccb->header.function_code = SCLP_NORMAL_WRITE;
283 sccb->header.response_code = 0x0000;
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200284 sccb->evbuf.type = EVTYP_VT220MSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sccb->evbuf.length = sizeof(struct evbuf_header);
286
287 return request;
288}
289
290static inline unsigned int
291sclp_vt220_space_left(struct sclp_vt220_request *request)
292{
293 struct sclp_vt220_sccb *sccb;
294 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
295 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
296 sccb->header.length;
297}
298
299static inline unsigned int
300sclp_vt220_chars_stored(struct sclp_vt220_request *request)
301{
302 struct sclp_vt220_sccb *sccb;
303 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
304 return sccb->evbuf.length - sizeof(struct evbuf_header);
305}
306
307/*
308 * Add msg to buffer associated with request. Return the number of characters
309 * added.
310 */
311static int
312sclp_vt220_add_msg(struct sclp_vt220_request *request,
313 const unsigned char *msg, int count, int convertlf)
314{
315 struct sclp_vt220_sccb *sccb;
316 void *buffer;
317 unsigned char c;
318 int from;
319 int to;
320
321 if (count > sclp_vt220_space_left(request))
322 count = sclp_vt220_space_left(request);
323 if (count <= 0)
324 return 0;
325
326 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
327 buffer = (void *) ((addr_t) sccb + sccb->header.length);
328
329 if (convertlf) {
330 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
331 for (from=0, to=0;
332 (from < count) && (to < sclp_vt220_space_left(request));
333 from++) {
334 /* Retrieve character */
335 c = msg[from];
336 /* Perform conversion */
337 if (c == 0x0a) {
338 if (to + 1 < sclp_vt220_space_left(request)) {
339 ((unsigned char *) buffer)[to++] = c;
340 ((unsigned char *) buffer)[to++] = 0x0d;
341 } else
342 break;
343
344 } else
345 ((unsigned char *) buffer)[to++] = c;
346 }
347 sccb->header.length += to;
348 sccb->evbuf.length += to;
349 return from;
350 } else {
351 memcpy(buffer, (const void *) msg, count);
352 sccb->header.length += count;
353 sccb->evbuf.length += count;
354 return count;
355 }
356}
357
358/*
359 * Emit buffer after having waited long enough for more data to arrive.
360 */
361static void
362sclp_vt220_timeout(unsigned long data)
363{
364 sclp_vt220_emit_current();
365}
366
Christian Borntraegerfa331ff2008-03-05 12:37:12 +0100367#define BUFFER_MAX_DELAY HZ/20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369/*
370 * Internal implementation of the write function. Write COUNT bytes of data
371 * from memory at BUF
372 * to the SCLP interface. In case that the data does not fit into the current
373 * write buffer, emit the current one and allocate a new one. If there are no
374 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
375 * is non-zero, the buffer will be scheduled for emitting after a timeout -
376 * otherwise the user has to explicitly call the flush function.
377 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
378 * buffer should be converted to 0x0a 0x0d. After completion, return the number
379 * of bytes written.
380 */
381static int
382__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
Heiko Carstensd4820e42008-05-30 10:03:30 +0200383 int convertlf, int may_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 unsigned long flags;
386 void *page;
387 int written;
388 int overall_written;
389
390 if (count <= 0)
391 return 0;
392 overall_written = 0;
393 spin_lock_irqsave(&sclp_vt220_lock, flags);
394 do {
Heiko Carstensd4820e42008-05-30 10:03:30 +0200395 /* Create an sclp output buffer if none exists yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (sclp_vt220_current_request == NULL) {
397 while (list_empty(&sclp_vt220_empty)) {
Heiko Carstensd1e23372008-04-17 07:46:02 +0200398 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Heiko Carstensd4820e42008-05-30 10:03:30 +0200399 if (may_fail)
400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 else
Heiko Carstensd4820e42008-05-30 10:03:30 +0200402 sclp_sync_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 spin_lock_irqsave(&sclp_vt220_lock, flags);
404 }
405 page = (void *) sclp_vt220_empty.next;
406 list_del((struct list_head *) page);
407 sclp_vt220_current_request =
408 sclp_vt220_initialize_page(page);
409 }
410 /* Try to write the string to the current request buffer */
411 written = sclp_vt220_add_msg(sclp_vt220_current_request,
412 buf, count, convertlf);
413 overall_written += written;
414 if (written == count)
415 break;
416 /*
417 * Not all characters could be written to the current
418 * output buffer. Emit the buffer, create a new buffer
419 * and then output the rest of the string.
420 */
421 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
422 sclp_vt220_emit_current();
423 spin_lock_irqsave(&sclp_vt220_lock, flags);
424 buf += written;
425 count -= written;
426 } while (count > 0);
427 /* Setup timer to output current console buffer after some time */
428 if (sclp_vt220_current_request != NULL &&
429 !timer_pending(&sclp_vt220_timer) && do_schedule) {
430 sclp_vt220_timer.function = sclp_vt220_timeout;
431 sclp_vt220_timer.data = 0UL;
432 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
433 add_timer(&sclp_vt220_timer);
434 }
435 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Heiko Carstensd4820e42008-05-30 10:03:30 +0200436out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return overall_written;
438}
439
440/*
441 * This routine is called by the kernel to write a series of
442 * characters to the tty device. The characters may come from
443 * user space or kernel space. This routine will return the
444 * number of characters actually accepted for writing.
445 */
446static int
447sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
448{
Heiko Carstensd1e23372008-04-17 07:46:02 +0200449 return __sclp_vt220_write(buf, count, 1, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452#define SCLP_VT220_SESSION_ENDED 0x01
453#define SCLP_VT220_SESSION_STARTED 0x80
454#define SCLP_VT220_SESSION_DATA 0x00
455
456/*
457 * Called by the SCLP to report incoming event buffers.
458 */
459static void
460sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
461{
462 char *buffer;
463 unsigned int count;
464
465 /* Ignore input if device is not open */
466 if (sclp_vt220_tty == NULL)
467 return;
468
469 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
470 count = evbuf->length - sizeof(struct evbuf_header);
471
472 switch (*buffer) {
473 case SCLP_VT220_SESSION_ENDED:
474 case SCLP_VT220_SESSION_STARTED:
475 break;
476 case SCLP_VT220_SESSION_DATA:
477 /* Send input to line discipline */
478 buffer++;
479 count--;
Alan Cox33f0f882006-01-09 20:54:13 -0800480 tty_insert_flip_string(sclp_vt220_tty, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tty_flip_buffer_push(sclp_vt220_tty);
482 break;
483 }
484}
485
486/*
487 * This routine is called when a particular tty device is opened.
488 */
489static int
490sclp_vt220_open(struct tty_struct *tty, struct file *filp)
491{
492 if (tty->count == 1) {
493 sclp_vt220_tty = tty;
494 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
495 if (tty->driver_data == NULL)
496 return -ENOMEM;
497 tty->low_latency = 0;
498 }
499 return 0;
500}
501
502/*
503 * This routine is called when a particular tty device is closed.
504 */
505static void
506sclp_vt220_close(struct tty_struct *tty, struct file *filp)
507{
508 if (tty->count == 1) {
509 sclp_vt220_tty = NULL;
510 kfree(tty->driver_data);
511 tty->driver_data = NULL;
512 }
513}
514
515/*
516 * This routine is called by the kernel to write a single
517 * character to the tty device. If the kernel uses this routine,
518 * it must call the flush_chars() routine (if defined) when it is
519 * done stuffing characters into the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700521static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
523{
Heiko Carstensd4820e42008-05-30 10:03:30 +0200524 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
528 * This routine is called by the kernel after it has written a
529 * series of characters to the tty device using put_char().
530 */
531static void
532sclp_vt220_flush_chars(struct tty_struct *tty)
533{
534 if (sclp_vt220_outqueue_count == 0)
535 sclp_vt220_emit_current();
536 else
537 sclp_vt220_flush_later = 1;
538}
539
540/*
541 * This routine returns the numbers of characters the tty driver
542 * will accept for queuing to be written. This number is subject
543 * to change as output buffers get emptied, or if the output flow
544 * control is acted.
545 */
546static int
547sclp_vt220_write_room(struct tty_struct *tty)
548{
549 unsigned long flags;
550 struct list_head *l;
551 int count;
552
553 spin_lock_irqsave(&sclp_vt220_lock, flags);
554 count = 0;
555 if (sclp_vt220_current_request != NULL)
556 count = sclp_vt220_space_left(sclp_vt220_current_request);
557 list_for_each(l, &sclp_vt220_empty)
558 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
559 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
560 return count;
561}
562
563/*
564 * Return number of buffered chars.
565 */
566static int
567sclp_vt220_chars_in_buffer(struct tty_struct *tty)
568{
569 unsigned long flags;
570 struct list_head *l;
571 struct sclp_vt220_request *r;
572 int count;
573
574 spin_lock_irqsave(&sclp_vt220_lock, flags);
575 count = 0;
576 if (sclp_vt220_current_request != NULL)
577 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
578 list_for_each(l, &sclp_vt220_outqueue) {
579 r = list_entry(l, struct sclp_vt220_request, list);
580 count += sclp_vt220_chars_stored(r);
581 }
582 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
583 return count;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Pass on all buffers to the hardware. Return only when there are no more
588 * buffers pending.
589 */
590static void
591sclp_vt220_flush_buffer(struct tty_struct *tty)
592{
593 sclp_vt220_emit_current();
594}
595
Peter Oberparleiterad211792008-07-14 09:59:07 +0200596/* Release allocated pages. */
597static void __init __sclp_vt220_free_pages(void)
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200598{
599 struct list_head *page, *p;
600
601 list_for_each_safe(page, p, &sclp_vt220_empty) {
602 list_del(page);
603 if (slab_is_available())
604 free_page((unsigned long) page);
605 else
606 free_bootmem((unsigned long) page, PAGE_SIZE);
607 }
608}
609
Peter Oberparleiterad211792008-07-14 09:59:07 +0200610/* Release memory and unregister from sclp core. Controlled by init counting -
611 * only the last invoker will actually perform these actions. */
612static void __init __sclp_vt220_cleanup(void)
613{
614 sclp_vt220_init_count--;
615 if (sclp_vt220_init_count != 0)
616 return;
617 sclp_unregister(&sclp_vt220_register);
618 __sclp_vt220_free_pages();
619}
620
621/* Allocate buffer pages and register with sclp core. Controlled by init
622 * counting - only the first invoker will actually perform these actions. */
623static int __init __sclp_vt220_init(int num_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 void *page;
626 int i;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100627 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Peter Oberparleiterad211792008-07-14 09:59:07 +0200629 sclp_vt220_init_count++;
630 if (sclp_vt220_init_count != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 spin_lock_init(&sclp_vt220_lock);
633 INIT_LIST_HEAD(&sclp_vt220_empty);
634 INIT_LIST_HEAD(&sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 init_timer(&sclp_vt220_timer);
636 sclp_vt220_current_request = NULL;
637 sclp_vt220_buffered_chars = 0;
638 sclp_vt220_outqueue_count = 0;
639 sclp_vt220_tty = NULL;
640 sclp_vt220_flush_later = 0;
641
642 /* Allocate pages for output buffering */
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200643 for (i = 0; i < num_pages; i++) {
644 if (slab_is_available())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200646 else
647 page = alloc_bootmem_low_pages(PAGE_SIZE);
648 if (!page) {
Peter Oberparleiterad211792008-07-14 09:59:07 +0200649 rc = -ENOMEM;
650 goto out;
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
653 }
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100654 rc = sclp_register(&sclp_vt220_register);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200655out:
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100656 if (rc) {
Peter Oberparleiterad211792008-07-14 09:59:07 +0200657 __sclp_vt220_free_pages();
658 sclp_vt220_init_count--;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100659 }
660 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700663static const struct tty_operations sclp_vt220_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .open = sclp_vt220_open,
665 .close = sclp_vt220_close,
666 .write = sclp_vt220_write,
667 .put_char = sclp_vt220_put_char,
668 .flush_chars = sclp_vt220_flush_chars,
669 .write_room = sclp_vt220_write_room,
670 .chars_in_buffer = sclp_vt220_chars_in_buffer,
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200671 .flush_buffer = sclp_vt220_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
674/*
675 * Register driver with SCLP and Linux and initialize internal tty structures.
676 */
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200677static int __init sclp_vt220_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct tty_driver *driver;
680 int rc;
681
682 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
683 * symmetry between VM and LPAR systems regarding ttyS1. */
684 driver = alloc_tty_driver(1);
685 if (!driver)
686 return -ENOMEM;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200687 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200688 if (rc)
689 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 driver->owner = THIS_MODULE;
692 driver->driver_name = SCLP_VT220_DRIVER_NAME;
693 driver->name = SCLP_VT220_DEVICE_NAME;
694 driver->major = SCLP_VT220_MAJOR;
695 driver->minor_start = SCLP_VT220_MINOR;
696 driver->type = TTY_DRIVER_TYPE_SYSTEM;
697 driver->subtype = SYSTEM_TYPE_TTY;
698 driver->init_termios = tty_std_termios;
699 driver->flags = TTY_DRIVER_REAL_RAW;
700 tty_set_operations(driver, &sclp_vt220_ops);
701
702 rc = tty_register_driver(driver);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200703 if (rc)
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100704 goto out_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sclp_vt220_driver = driver;
706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200708out_init:
Peter Oberparleiterad211792008-07-14 09:59:07 +0200709 __sclp_vt220_cleanup();
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200710out_driver:
711 put_tty_driver(driver);
712 return rc;
713}
714__initcall(sclp_vt220_tty_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716#ifdef CONFIG_SCLP_VT220_CONSOLE
717
718static void
719sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
720{
Heiko Carstensd1e23372008-04-17 07:46:02 +0200721 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724static struct tty_driver *
725sclp_vt220_con_device(struct console *c, int *index)
726{
727 *index = 0;
728 return sclp_vt220_driver;
729}
730
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100731static void __sclp_vt220_flush_buffer(void)
732{
733 unsigned long flags;
734
735 sclp_vt220_emit_current();
736 spin_lock_irqsave(&sclp_vt220_lock, flags);
737 if (timer_pending(&sclp_vt220_timer))
738 del_timer(&sclp_vt220_timer);
739 while (sclp_vt220_outqueue_count > 0) {
740 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
741 sclp_sync_wait();
742 spin_lock_irqsave(&sclp_vt220_lock, flags);
743 }
744 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
745}
746
Holger Smolinski2332ce12008-10-10 21:33:27 +0200747static int
748sclp_vt220_notify(struct notifier_block *self,
749 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 __sclp_vt220_flush_buffer();
Holger Smolinski2332ce12008-10-10 21:33:27 +0200752 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Holger Smolinski2332ce12008-10-10 21:33:27 +0200755static struct notifier_block on_panic_nb = {
756 .notifier_call = sclp_vt220_notify,
757 .priority = 1,
758};
759
760static struct notifier_block on_reboot_nb = {
761 .notifier_call = sclp_vt220_notify,
762 .priority = 1,
763};
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/* Structure needed to register with printk */
766static struct console sclp_vt220_console =
767{
768 .name = SCLP_VT220_CONSOLE_NAME,
769 .write = sclp_vt220_con_write,
770 .device = sclp_vt220_con_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 .flags = CON_PRINTBUFFER,
772 .index = SCLP_VT220_CONSOLE_INDEX
773};
774
775static int __init
776sclp_vt220_con_init(void)
777{
778 int rc;
779
780 if (!CONSOLE_IS_SCLP)
781 return 0;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200782 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (rc)
784 return rc;
785 /* Attach linux console */
Holger Smolinski2332ce12008-10-10 21:33:27 +0200786 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
787 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 register_console(&sclp_vt220_console);
789 return 0;
790}
791
792console_initcall(sclp_vt220_con_init);
793#endif /* CONFIG_SCLP_VT220_CONSOLE */
794