blob: 3796ffdb847995c89ff74acf9bb5078f28a7eff5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Michael Holzheu62b749422009-06-16 10:30:40 +02002 * SCLP VT220 terminal driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Michael Holzheu62b749422009-06-16 10:30:40 +02004 * Copyright IBM Corp. 2003, 2009
5 *
6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/spinlock.h>
11#include <linux/list.h>
12#include <linux/wait.h>
13#include <linux/timer.h>
14#include <linux/kernel.h>
15#include <linux/tty.h>
16#include <linux/tty_driver.h>
Alan Cox33f0f882006-01-09 20:54:13 -080017#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
19#include <linux/mm.h>
20#include <linux/major.h>
21#include <linux/console.h>
22#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/interrupt.h>
24#include <linux/init.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020025#include <linux/reboot.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/uaccess.h>
28#include "sclp.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define SCLP_VT220_MAJOR TTY_MAJOR
31#define SCLP_VT220_MINOR 65
32#define SCLP_VT220_DRIVER_NAME "sclp_vt220"
33#define SCLP_VT220_DEVICE_NAME "ttysclp"
34#define SCLP_VT220_CONSOLE_NAME "ttyS"
35#define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
36#define SCLP_VT220_BUF_SIZE 80
37
38/* Representation of a single write request */
39struct sclp_vt220_request {
40 struct list_head list;
41 struct sclp_req sclp_req;
42 int retry_count;
43};
44
45/* VT220 SCCB */
46struct sclp_vt220_sccb {
47 struct sccb_header header;
48 struct evbuf_header evbuf;
49};
50
51#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
52 sizeof(struct sclp_vt220_request) - \
53 sizeof(struct sclp_vt220_sccb))
54
55/* Structures and data needed to register tty driver */
56static struct tty_driver *sclp_vt220_driver;
57
58/* The tty_struct that the kernel associated with us */
59static struct tty_struct *sclp_vt220_tty;
60
61/* Lock to protect internal data from concurrent access */
62static spinlock_t sclp_vt220_lock;
63
64/* List of empty pages to be used as write request buffers */
65static struct list_head sclp_vt220_empty;
66
67/* List of pending requests */
68static struct list_head sclp_vt220_outqueue;
69
Michael Holzheu62b749422009-06-16 10:30:40 +020070/* Suspend mode flag */
71static int sclp_vt220_suspended;
72
73/* Flag that output queue is currently running */
74static int sclp_vt220_queue_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Timer used for delaying write requests to merge subsequent messages into
77 * a single buffer */
78static struct timer_list sclp_vt220_timer;
79
80/* Pointer to current request buffer which has been partially filled but not
81 * yet sent */
82static struct sclp_vt220_request *sclp_vt220_current_request;
83
84/* Number of characters in current request buffer */
85static int sclp_vt220_buffered_chars;
86
Peter Oberparleiterad211792008-07-14 09:59:07 +020087/* Counter controlling core driver initialization. */
88static int __initdata sclp_vt220_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* Flag indicating that sclp_vt220_current_request should really
91 * have been already queued but wasn't because the SCLP was processing
92 * another buffer */
93static int sclp_vt220_flush_later;
94
95static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
Michael Holzheu62b749422009-06-16 10:30:40 +020096static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
97 enum sclp_pm_event sclp_pm_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int __sclp_vt220_emit(struct sclp_vt220_request *request);
99static void sclp_vt220_emit_current(void);
100
101/* Registration structure for our interest in SCLP event buffers */
102static struct sclp_register sclp_vt220_register = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200103 .send_mask = EVTYP_VT220MSG_MASK,
104 .receive_mask = EVTYP_VT220MSG_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .state_change_fn = NULL,
Michael Holzheu62b749422009-06-16 10:30:40 +0200106 .receiver_fn = sclp_vt220_receiver_fn,
107 .pm_event_fn = sclp_vt220_pm_event_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110
111/*
112 * Put provided request buffer back into queue and check emit pending
113 * buffers if necessary.
114 */
115static void
116sclp_vt220_process_queue(struct sclp_vt220_request *request)
117{
118 unsigned long flags;
119 void *page;
120
121 do {
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
128 /* Check if there is a pending buffer on the out queue. */
129 request = NULL;
130 if (!list_empty(&sclp_vt220_outqueue))
131 request = list_entry(sclp_vt220_outqueue.next,
132 struct sclp_vt220_request, list);
Michael Holzheu62b749422009-06-16 10:30:40 +0200133 if (!request || sclp_vt220_suspended) {
134 sclp_vt220_queue_running = 0;
135 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
136 break;
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200139 } while (__sclp_vt220_emit(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (request == NULL && sclp_vt220_flush_later)
141 sclp_vt220_emit_current();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Check if the tty needs a wake up call */
143 if (sclp_vt220_tty != NULL) {
144 tty_wakeup(sclp_vt220_tty);
145 }
146}
147
148#define SCLP_BUFFER_MAX_RETRY 1
149
150/*
151 * Callback through which the result of a write request is reported by the
152 * SCLP.
153 */
154static void
155sclp_vt220_callback(struct sclp_req *request, void *data)
156{
157 struct sclp_vt220_request *vt220_request;
158 struct sclp_vt220_sccb *sccb;
159
160 vt220_request = (struct sclp_vt220_request *) data;
161 if (request->status == SCLP_REQ_FAILED) {
162 sclp_vt220_process_queue(vt220_request);
163 return;
164 }
165 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
166
167 /* Check SCLP response code and choose suitable action */
168 switch (sccb->header.response_code) {
169 case 0x0020 :
170 break;
171
172 case 0x05f0: /* Target resource in improper state */
173 break;
174
175 case 0x0340: /* Contained SCLP equipment check */
176 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
177 break;
178 /* Remove processed buffers and requeue rest */
179 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
180 /* Not all buffers were processed */
181 sccb->header.response_code = 0x0000;
182 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
183 if (sclp_add_request(request) == 0)
184 return;
185 }
186 break;
187
188 case 0x0040: /* SCLP equipment check */
189 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
190 break;
191 sccb->header.response_code = 0x0000;
192 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
193 if (sclp_add_request(request) == 0)
194 return;
195 break;
196
197 default:
198 break;
199 }
200 sclp_vt220_process_queue(vt220_request);
201}
202
203/*
204 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
205 * otherwise.
206 */
207static int
208__sclp_vt220_emit(struct sclp_vt220_request *request)
209{
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100210 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 request->sclp_req.status = SCLP_REQ_FAILED;
212 return -EIO;
213 }
Heiko Carstensab14de62007-02-05 21:18:37 +0100214 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 request->sclp_req.status = SCLP_REQ_FILLED;
216 request->sclp_req.callback = sclp_vt220_callback;
217 request->sclp_req.callback_data = (void *) request;
218
219 return sclp_add_request(&request->sclp_req);
220}
221
222/*
Michael Holzheu62b749422009-06-16 10:30:40 +0200223 * Queue and emit current request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225static void
226sclp_vt220_emit_current(void)
227{
228 unsigned long flags;
229 struct sclp_vt220_request *request;
230 struct sclp_vt220_sccb *sccb;
231
232 spin_lock_irqsave(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200233 if (sclp_vt220_current_request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sccb = (struct sclp_vt220_sccb *)
235 sclp_vt220_current_request->sclp_req.sccb;
236 /* Only emit buffers with content */
237 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
Michael Holzheu62b749422009-06-16 10:30:40 +0200238 list_add_tail(&sclp_vt220_current_request->list,
239 &sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 sclp_vt220_current_request = NULL;
241 if (timer_pending(&sclp_vt220_timer))
242 del_timer(&sclp_vt220_timer);
243 }
244 sclp_vt220_flush_later = 0;
245 }
Michael Holzheu62b749422009-06-16 10:30:40 +0200246 if (sclp_vt220_queue_running || sclp_vt220_suspended)
247 goto out_unlock;
248 if (list_empty(&sclp_vt220_outqueue))
249 goto out_unlock;
250 request = list_first_entry(&sclp_vt220_outqueue,
251 struct sclp_vt220_request, list);
252 sclp_vt220_queue_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200254
255 if (__sclp_vt220_emit(request))
256 sclp_vt220_process_queue(request);
257 return;
258out_unlock:
259 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
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);
Michael Holzheu62b749422009-06-16 10:30:40 +0200399 if (may_fail || sclp_vt220_suspended)
Heiko Carstensd4820e42008-05-30 10:03:30 +0200400 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;
Hendrik Brueckner0b665d72010-01-27 10:12:38 +0100498 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
499 tty->winsize.ws_row = 24;
500 tty->winsize.ws_col = 80;
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 return 0;
504}
505
506/*
507 * This routine is called when a particular tty device is closed.
508 */
509static void
510sclp_vt220_close(struct tty_struct *tty, struct file *filp)
511{
512 if (tty->count == 1) {
513 sclp_vt220_tty = NULL;
514 kfree(tty->driver_data);
515 tty->driver_data = NULL;
516 }
517}
518
519/*
520 * This routine is called by the kernel to write a single
521 * character to the tty device. If the kernel uses this routine,
522 * it must call the flush_chars() routine (if defined) when it is
523 * done stuffing characters into the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700525static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
527{
Heiko Carstensd4820e42008-05-30 10:03:30 +0200528 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * This routine is called by the kernel after it has written a
533 * series of characters to the tty device using put_char().
534 */
535static void
536sclp_vt220_flush_chars(struct tty_struct *tty)
537{
Michael Holzheu62b749422009-06-16 10:30:40 +0200538 if (!sclp_vt220_queue_running)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 sclp_vt220_emit_current();
540 else
541 sclp_vt220_flush_later = 1;
542}
543
544/*
545 * This routine returns the numbers of characters the tty driver
546 * will accept for queuing to be written. This number is subject
547 * to change as output buffers get emptied, or if the output flow
548 * control is acted.
549 */
550static int
551sclp_vt220_write_room(struct tty_struct *tty)
552{
553 unsigned long flags;
554 struct list_head *l;
555 int count;
556
557 spin_lock_irqsave(&sclp_vt220_lock, flags);
558 count = 0;
559 if (sclp_vt220_current_request != NULL)
560 count = sclp_vt220_space_left(sclp_vt220_current_request);
561 list_for_each(l, &sclp_vt220_empty)
562 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
563 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
564 return count;
565}
566
567/*
568 * Return number of buffered chars.
569 */
570static int
571sclp_vt220_chars_in_buffer(struct tty_struct *tty)
572{
573 unsigned long flags;
574 struct list_head *l;
575 struct sclp_vt220_request *r;
576 int count;
577
578 spin_lock_irqsave(&sclp_vt220_lock, flags);
579 count = 0;
580 if (sclp_vt220_current_request != NULL)
581 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
582 list_for_each(l, &sclp_vt220_outqueue) {
583 r = list_entry(l, struct sclp_vt220_request, list);
584 count += sclp_vt220_chars_stored(r);
585 }
586 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
587 return count;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/*
591 * Pass on all buffers to the hardware. Return only when there are no more
592 * buffers pending.
593 */
594static void
595sclp_vt220_flush_buffer(struct tty_struct *tty)
596{
597 sclp_vt220_emit_current();
598}
599
Peter Oberparleiterad211792008-07-14 09:59:07 +0200600/* Release allocated pages. */
601static void __init __sclp_vt220_free_pages(void)
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200602{
603 struct list_head *page, *p;
604
605 list_for_each_safe(page, p, &sclp_vt220_empty) {
606 list_del(page);
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200607 free_page((unsigned long) page);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200608 }
609}
610
Peter Oberparleiterad211792008-07-14 09:59:07 +0200611/* Release memory and unregister from sclp core. Controlled by init counting -
612 * only the last invoker will actually perform these actions. */
613static void __init __sclp_vt220_cleanup(void)
614{
615 sclp_vt220_init_count--;
616 if (sclp_vt220_init_count != 0)
617 return;
618 sclp_unregister(&sclp_vt220_register);
619 __sclp_vt220_free_pages();
620}
621
622/* Allocate buffer pages and register with sclp core. Controlled by init
623 * counting - only the first invoker will actually perform these actions. */
624static int __init __sclp_vt220_init(int num_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 void *page;
627 int i;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100628 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Peter Oberparleiterad211792008-07-14 09:59:07 +0200630 sclp_vt220_init_count++;
631 if (sclp_vt220_init_count != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_lock_init(&sclp_vt220_lock);
634 INIT_LIST_HEAD(&sclp_vt220_empty);
635 INIT_LIST_HEAD(&sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 init_timer(&sclp_vt220_timer);
637 sclp_vt220_current_request = NULL;
638 sclp_vt220_buffered_chars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 sclp_vt220_tty = NULL;
640 sclp_vt220_flush_later = 0;
641
642 /* Allocate pages for output buffering */
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200643 rc = -ENOMEM;
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200644 for (i = 0; i < num_pages; i++) {
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200645 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
646 if (!page)
Peter Oberparleiterad211792008-07-14 09:59:07 +0200647 goto out;
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200648 list_add_tail(page, &sclp_vt220_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100650 rc = sclp_register(&sclp_vt220_register);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200651out:
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100652 if (rc) {
Peter Oberparleiterad211792008-07-14 09:59:07 +0200653 __sclp_vt220_free_pages();
654 sclp_vt220_init_count--;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100655 }
656 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700659static const struct tty_operations sclp_vt220_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 .open = sclp_vt220_open,
661 .close = sclp_vt220_close,
662 .write = sclp_vt220_write,
663 .put_char = sclp_vt220_put_char,
664 .flush_chars = sclp_vt220_flush_chars,
665 .write_room = sclp_vt220_write_room,
666 .chars_in_buffer = sclp_vt220_chars_in_buffer,
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200667 .flush_buffer = sclp_vt220_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668};
669
670/*
671 * Register driver with SCLP and Linux and initialize internal tty structures.
672 */
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200673static int __init sclp_vt220_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct tty_driver *driver;
676 int rc;
677
678 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
679 * symmetry between VM and LPAR systems regarding ttyS1. */
680 driver = alloc_tty_driver(1);
681 if (!driver)
682 return -ENOMEM;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200683 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200684 if (rc)
685 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 driver->owner = THIS_MODULE;
688 driver->driver_name = SCLP_VT220_DRIVER_NAME;
689 driver->name = SCLP_VT220_DEVICE_NAME;
690 driver->major = SCLP_VT220_MAJOR;
691 driver->minor_start = SCLP_VT220_MINOR;
692 driver->type = TTY_DRIVER_TYPE_SYSTEM;
693 driver->subtype = SYSTEM_TYPE_TTY;
694 driver->init_termios = tty_std_termios;
695 driver->flags = TTY_DRIVER_REAL_RAW;
696 tty_set_operations(driver, &sclp_vt220_ops);
697
698 rc = tty_register_driver(driver);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200699 if (rc)
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100700 goto out_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 sclp_vt220_driver = driver;
702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200704out_init:
Peter Oberparleiterad211792008-07-14 09:59:07 +0200705 __sclp_vt220_cleanup();
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200706out_driver:
707 put_tty_driver(driver);
708 return rc;
709}
710__initcall(sclp_vt220_tty_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100712static void __sclp_vt220_flush_buffer(void)
713{
714 unsigned long flags;
715
716 sclp_vt220_emit_current();
717 spin_lock_irqsave(&sclp_vt220_lock, flags);
718 if (timer_pending(&sclp_vt220_timer))
719 del_timer(&sclp_vt220_timer);
Michael Holzheu62b749422009-06-16 10:30:40 +0200720 while (sclp_vt220_queue_running) {
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100721 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
722 sclp_sync_wait();
723 spin_lock_irqsave(&sclp_vt220_lock, flags);
724 }
725 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
726}
727
Michael Holzheu62b749422009-06-16 10:30:40 +0200728/*
729 * Resume console: If there are cached messages, emit them.
730 */
731static void sclp_vt220_resume(void)
732{
733 unsigned long flags;
734
735 spin_lock_irqsave(&sclp_vt220_lock, flags);
736 sclp_vt220_suspended = 0;
737 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
738 sclp_vt220_emit_current();
739}
740
741/*
742 * Suspend console: Set suspend flag and flush console
743 */
744static void sclp_vt220_suspend(void)
745{
746 unsigned long flags;
747
748 spin_lock_irqsave(&sclp_vt220_lock, flags);
749 sclp_vt220_suspended = 1;
750 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
751 __sclp_vt220_flush_buffer();
752}
753
754static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
755 enum sclp_pm_event sclp_pm_event)
756{
757 switch (sclp_pm_event) {
758 case SCLP_PM_EVENT_FREEZE:
759 sclp_vt220_suspend();
760 break;
761 case SCLP_PM_EVENT_RESTORE:
762 case SCLP_PM_EVENT_THAW:
763 sclp_vt220_resume();
764 break;
765 }
766}
767
Michael Holzheuac522b62009-10-14 12:43:51 +0200768#ifdef CONFIG_SCLP_VT220_CONSOLE
769
770static void
771sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
772{
773 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
774}
775
776static struct tty_driver *
777sclp_vt220_con_device(struct console *c, int *index)
778{
779 *index = 0;
780 return sclp_vt220_driver;
781}
782
Holger Smolinski2332ce12008-10-10 21:33:27 +0200783static int
784sclp_vt220_notify(struct notifier_block *self,
785 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 __sclp_vt220_flush_buffer();
Holger Smolinski2332ce12008-10-10 21:33:27 +0200788 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Holger Smolinski2332ce12008-10-10 21:33:27 +0200791static struct notifier_block on_panic_nb = {
792 .notifier_call = sclp_vt220_notify,
793 .priority = 1,
794};
795
796static struct notifier_block on_reboot_nb = {
797 .notifier_call = sclp_vt220_notify,
798 .priority = 1,
799};
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/* Structure needed to register with printk */
802static struct console sclp_vt220_console =
803{
804 .name = SCLP_VT220_CONSOLE_NAME,
805 .write = sclp_vt220_con_write,
806 .device = sclp_vt220_con_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .flags = CON_PRINTBUFFER,
808 .index = SCLP_VT220_CONSOLE_INDEX
809};
810
811static int __init
812sclp_vt220_con_init(void)
813{
814 int rc;
815
816 if (!CONSOLE_IS_SCLP)
817 return 0;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200818 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (rc)
820 return rc;
821 /* Attach linux console */
Holger Smolinski2332ce12008-10-10 21:33:27 +0200822 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
823 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 register_console(&sclp_vt220_console);
825 return 0;
826}
827
828console_initcall(sclp_vt220_con_init);
829#endif /* CONFIG_SCLP_VT220_CONSOLE */
830