blob: ae67386c03d3b8301a581c58a68a028d70f22fbf [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
29#include "sclp.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define SCLP_VT220_MAJOR TTY_MAJOR
32#define SCLP_VT220_MINOR 65
33#define SCLP_VT220_DRIVER_NAME "sclp_vt220"
34#define SCLP_VT220_DEVICE_NAME "ttysclp"
35#define SCLP_VT220_CONSOLE_NAME "ttyS"
36#define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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
Jiri Slaby092f7372012-04-02 13:54:13 +020058static struct tty_port sclp_vt220_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* Lock to protect internal data from concurrent access */
61static spinlock_t sclp_vt220_lock;
62
63/* List of empty pages to be used as write request buffers */
64static struct list_head sclp_vt220_empty;
65
66/* List of pending requests */
67static struct list_head sclp_vt220_outqueue;
68
Michael Holzheu62b749422009-06-16 10:30:40 +020069/* Suspend mode flag */
70static int sclp_vt220_suspended;
71
72/* Flag that output queue is currently running */
73static int sclp_vt220_queue_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
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);
Michael Holzheu62b749422009-06-16 10:30:40 +020095static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
96 enum sclp_pm_event sclp_pm_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int __sclp_vt220_emit(struct sclp_vt220_request *request);
98static void sclp_vt220_emit_current(void);
99
Peter Oberparleitere106e4e2014-04-09 13:10:05 +0200100/* Registration structure for SCLP output event buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct sclp_register sclp_vt220_register = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200102 .send_mask = EVTYP_VT220MSG_MASK,
Michael Holzheu62b749422009-06-16 10:30:40 +0200103 .pm_event_fn = sclp_vt220_pm_event_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Peter Oberparleitere106e4e2014-04-09 13:10:05 +0200106/* Registration structure for SCLP input event buffers */
107static struct sclp_register sclp_vt220_register_input = {
108 .receive_mask = EVTYP_VT220MSG_MASK,
109 .receiver_fn = sclp_vt220_receiver_fn,
110};
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
114 * Put provided request buffer back into queue and check emit pending
115 * buffers if necessary.
116 */
117static void
118sclp_vt220_process_queue(struct sclp_vt220_request *request)
119{
120 unsigned long flags;
121 void *page;
122
123 do {
124 /* Put buffer back to list of empty buffers */
125 page = request->sclp_req.sccb;
126 spin_lock_irqsave(&sclp_vt220_lock, flags);
127 /* Move request from outqueue to empty queue */
128 list_del(&request->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
130 /* Check if there is a pending buffer on the out queue. */
131 request = NULL;
132 if (!list_empty(&sclp_vt220_outqueue))
133 request = list_entry(sclp_vt220_outqueue.next,
134 struct sclp_vt220_request, list);
Michael Holzheu62b749422009-06-16 10:30:40 +0200135 if (!request || sclp_vt220_suspended) {
136 sclp_vt220_queue_running = 0;
137 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
138 break;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200141 } while (__sclp_vt220_emit(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (request == NULL && sclp_vt220_flush_later)
143 sclp_vt220_emit_current();
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100144 tty_port_tty_wakeup(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147#define SCLP_BUFFER_MAX_RETRY 1
148
149/*
150 * Callback through which the result of a write request is reported by the
151 * SCLP.
152 */
153static void
154sclp_vt220_callback(struct sclp_req *request, void *data)
155{
156 struct sclp_vt220_request *vt220_request;
157 struct sclp_vt220_sccb *sccb;
158
159 vt220_request = (struct sclp_vt220_request *) data;
160 if (request->status == SCLP_REQ_FAILED) {
161 sclp_vt220_process_queue(vt220_request);
162 return;
163 }
164 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
165
166 /* Check SCLP response code and choose suitable action */
167 switch (sccb->header.response_code) {
168 case 0x0020 :
169 break;
170
171 case 0x05f0: /* Target resource in improper state */
172 break;
173
174 case 0x0340: /* Contained SCLP equipment check */
175 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
176 break;
177 /* Remove processed buffers and requeue rest */
178 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
179 /* Not all buffers were processed */
180 sccb->header.response_code = 0x0000;
181 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
182 if (sclp_add_request(request) == 0)
183 return;
184 }
185 break;
186
187 case 0x0040: /* SCLP equipment check */
188 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
189 break;
190 sccb->header.response_code = 0x0000;
191 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
192 if (sclp_add_request(request) == 0)
193 return;
194 break;
195
196 default:
197 break;
198 }
199 sclp_vt220_process_queue(vt220_request);
200}
201
202/*
203 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
204 * otherwise.
205 */
206static int
207__sclp_vt220_emit(struct sclp_vt220_request *request)
208{
Heiko Carstensab14de62007-02-05 21:18:37 +0100209 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 request->sclp_req.status = SCLP_REQ_FILLED;
211 request->sclp_req.callback = sclp_vt220_callback;
212 request->sclp_req.callback_data = (void *) request;
213
214 return sclp_add_request(&request->sclp_req);
215}
216
217/*
Michael Holzheu62b749422009-06-16 10:30:40 +0200218 * Queue and emit current request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
220static void
221sclp_vt220_emit_current(void)
222{
223 unsigned long flags;
224 struct sclp_vt220_request *request;
225 struct sclp_vt220_sccb *sccb;
226
227 spin_lock_irqsave(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200228 if (sclp_vt220_current_request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 sccb = (struct sclp_vt220_sccb *)
230 sclp_vt220_current_request->sclp_req.sccb;
231 /* Only emit buffers with content */
232 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
Michael Holzheu62b749422009-06-16 10:30:40 +0200233 list_add_tail(&sclp_vt220_current_request->list,
234 &sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 sclp_vt220_current_request = NULL;
236 if (timer_pending(&sclp_vt220_timer))
237 del_timer(&sclp_vt220_timer);
238 }
239 sclp_vt220_flush_later = 0;
240 }
Michael Holzheu62b749422009-06-16 10:30:40 +0200241 if (sclp_vt220_queue_running || sclp_vt220_suspended)
242 goto out_unlock;
243 if (list_empty(&sclp_vt220_outqueue))
244 goto out_unlock;
245 request = list_first_entry(&sclp_vt220_outqueue,
246 struct sclp_vt220_request, list);
247 sclp_vt220_queue_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200249
250 if (__sclp_vt220_emit(request))
251 sclp_vt220_process_queue(request);
252 return;
253out_unlock:
254 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257#define SCLP_NORMAL_WRITE 0x00
258
259/*
260 * Helper function to initialize a page with the sclp request structure.
261 */
262static struct sclp_vt220_request *
263sclp_vt220_initialize_page(void *page)
264{
265 struct sclp_vt220_request *request;
266 struct sclp_vt220_sccb *sccb;
267
268 /* Place request structure at end of page */
269 request = ((struct sclp_vt220_request *)
270 ((addr_t) page + PAGE_SIZE)) - 1;
271 request->retry_count = 0;
272 request->sclp_req.sccb = page;
273 /* SCCB goes at start of page */
274 sccb = (struct sclp_vt220_sccb *) page;
275 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
276 sccb->header.length = sizeof(struct sclp_vt220_sccb);
277 sccb->header.function_code = SCLP_NORMAL_WRITE;
278 sccb->header.response_code = 0x0000;
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200279 sccb->evbuf.type = EVTYP_VT220MSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sccb->evbuf.length = sizeof(struct evbuf_header);
281
282 return request;
283}
284
285static inline unsigned int
286sclp_vt220_space_left(struct sclp_vt220_request *request)
287{
288 struct sclp_vt220_sccb *sccb;
289 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
290 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
291 sccb->header.length;
292}
293
294static inline unsigned int
295sclp_vt220_chars_stored(struct sclp_vt220_request *request)
296{
297 struct sclp_vt220_sccb *sccb;
298 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
299 return sccb->evbuf.length - sizeof(struct evbuf_header);
300}
301
302/*
303 * Add msg to buffer associated with request. Return the number of characters
304 * added.
305 */
306static int
307sclp_vt220_add_msg(struct sclp_vt220_request *request,
308 const unsigned char *msg, int count, int convertlf)
309{
310 struct sclp_vt220_sccb *sccb;
311 void *buffer;
312 unsigned char c;
313 int from;
314 int to;
315
316 if (count > sclp_vt220_space_left(request))
317 count = sclp_vt220_space_left(request);
318 if (count <= 0)
319 return 0;
320
321 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
322 buffer = (void *) ((addr_t) sccb + sccb->header.length);
323
324 if (convertlf) {
325 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
326 for (from=0, to=0;
327 (from < count) && (to < sclp_vt220_space_left(request));
328 from++) {
329 /* Retrieve character */
330 c = msg[from];
331 /* Perform conversion */
332 if (c == 0x0a) {
333 if (to + 1 < sclp_vt220_space_left(request)) {
334 ((unsigned char *) buffer)[to++] = c;
335 ((unsigned char *) buffer)[to++] = 0x0d;
336 } else
337 break;
338
339 } else
340 ((unsigned char *) buffer)[to++] = c;
341 }
342 sccb->header.length += to;
343 sccb->evbuf.length += to;
344 return from;
345 } else {
346 memcpy(buffer, (const void *) msg, count);
347 sccb->header.length += count;
348 sccb->evbuf.length += count;
349 return count;
350 }
351}
352
353/*
354 * Emit buffer after having waited long enough for more data to arrive.
355 */
356static void
357sclp_vt220_timeout(unsigned long data)
358{
359 sclp_vt220_emit_current();
360}
361
Christian Borntraegerfa331ff2008-03-05 12:37:12 +0100362#define BUFFER_MAX_DELAY HZ/20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Martin Schwidefsky25b41a72013-05-24 12:30:03 +0200364/*
365 * Drop oldest console buffer if sclp_con_drop is set
366 */
367static int
368sclp_vt220_drop_buffer(void)
369{
370 struct list_head *list;
371 struct sclp_vt220_request *request;
372 void *page;
373
374 if (!sclp_console_drop)
375 return 0;
376 list = sclp_vt220_outqueue.next;
377 if (sclp_vt220_queue_running)
378 /* The first element is in I/O */
379 list = list->next;
380 if (list == &sclp_vt220_outqueue)
381 return 0;
382 list_del(list);
383 request = list_entry(list, struct sclp_vt220_request, list);
384 page = request->sclp_req.sccb;
385 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
386 return 1;
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * Internal implementation of the write function. Write COUNT bytes of data
391 * from memory at BUF
392 * to the SCLP interface. In case that the data does not fit into the current
393 * write buffer, emit the current one and allocate a new one. If there are no
394 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
395 * is non-zero, the buffer will be scheduled for emitting after a timeout -
396 * otherwise the user has to explicitly call the flush function.
397 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
398 * buffer should be converted to 0x0a 0x0d. After completion, return the number
399 * of bytes written.
400 */
401static int
402__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
Heiko Carstensd4820e42008-05-30 10:03:30 +0200403 int convertlf, int may_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 unsigned long flags;
406 void *page;
407 int written;
408 int overall_written;
409
410 if (count <= 0)
411 return 0;
412 overall_written = 0;
413 spin_lock_irqsave(&sclp_vt220_lock, flags);
414 do {
Heiko Carstensd4820e42008-05-30 10:03:30 +0200415 /* Create an sclp output buffer if none exists yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (sclp_vt220_current_request == NULL) {
Martin Schwidefsky25b41a72013-05-24 12:30:03 +0200417 if (list_empty(&sclp_vt220_empty))
418 sclp_console_full++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 while (list_empty(&sclp_vt220_empty)) {
Michael Holzheu62b749422009-06-16 10:30:40 +0200420 if (may_fail || sclp_vt220_suspended)
Heiko Carstensd4820e42008-05-30 10:03:30 +0200421 goto out;
Martin Schwidefsky25b41a72013-05-24 12:30:03 +0200422 if (sclp_vt220_drop_buffer())
423 break;
424 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
425
426 sclp_sync_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 spin_lock_irqsave(&sclp_vt220_lock, flags);
428 }
429 page = (void *) sclp_vt220_empty.next;
430 list_del((struct list_head *) page);
431 sclp_vt220_current_request =
432 sclp_vt220_initialize_page(page);
433 }
434 /* Try to write the string to the current request buffer */
435 written = sclp_vt220_add_msg(sclp_vt220_current_request,
436 buf, count, convertlf);
437 overall_written += written;
438 if (written == count)
439 break;
440 /*
441 * Not all characters could be written to the current
442 * output buffer. Emit the buffer, create a new buffer
443 * and then output the rest of the string.
444 */
445 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
446 sclp_vt220_emit_current();
447 spin_lock_irqsave(&sclp_vt220_lock, flags);
448 buf += written;
449 count -= written;
450 } while (count > 0);
451 /* Setup timer to output current console buffer after some time */
452 if (sclp_vt220_current_request != NULL &&
453 !timer_pending(&sclp_vt220_timer) && do_schedule) {
454 sclp_vt220_timer.function = sclp_vt220_timeout;
455 sclp_vt220_timer.data = 0UL;
456 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
457 add_timer(&sclp_vt220_timer);
458 }
Heiko Carstensd4820e42008-05-30 10:03:30 +0200459out:
Martin Schwidefsky25b41a72013-05-24 12:30:03 +0200460 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return overall_written;
462}
463
464/*
465 * This routine is called by the kernel to write a series of
466 * characters to the tty device. The characters may come from
467 * user space or kernel space. This routine will return the
468 * number of characters actually accepted for writing.
469 */
470static int
471sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
472{
Heiko Carstensd1e23372008-04-17 07:46:02 +0200473 return __sclp_vt220_write(buf, count, 1, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476#define SCLP_VT220_SESSION_ENDED 0x01
477#define SCLP_VT220_SESSION_STARTED 0x80
478#define SCLP_VT220_SESSION_DATA 0x00
479
480/*
481 * Called by the SCLP to report incoming event buffers.
482 */
483static void
484sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
485{
486 char *buffer;
487 unsigned int count;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
490 count = evbuf->length - sizeof(struct evbuf_header);
491
492 switch (*buffer) {
493 case SCLP_VT220_SESSION_ENDED:
494 case SCLP_VT220_SESSION_STARTED:
495 break;
496 case SCLP_VT220_SESSION_DATA:
497 /* Send input to line discipline */
498 buffer++;
499 count--;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100500 tty_insert_flip_string(&sclp_vt220_port, buffer, count);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100501 tty_flip_buffer_push(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 }
504}
505
506/*
507 * This routine is called when a particular tty device is opened.
508 */
509static int
510sclp_vt220_open(struct tty_struct *tty, struct file *filp)
511{
512 if (tty->count == 1) {
Jiri Slaby092f7372012-04-02 13:54:13 +0200513 tty_port_tty_set(&sclp_vt220_port, tty);
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100514 sclp_vt220_port.low_latency = 0;
Hendrik Brueckner0b665d72010-01-27 10:12:38 +0100515 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
516 tty->winsize.ws_row = 24;
517 tty->winsize.ws_col = 80;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 return 0;
521}
522
523/*
524 * This routine is called when a particular tty device is closed.
525 */
526static void
527sclp_vt220_close(struct tty_struct *tty, struct file *filp)
528{
Jiri Slabyb538c4e2012-04-02 13:54:14 +0200529 if (tty->count == 1)
Jiri Slaby092f7372012-04-02 13:54:13 +0200530 tty_port_tty_set(&sclp_vt220_port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
534 * This routine is called by the kernel to write a single
535 * character to the tty device. If the kernel uses this routine,
536 * it must call the flush_chars() routine (if defined) when it is
537 * done stuffing characters into the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700539static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
541{
Heiko Carstensd4820e42008-05-30 10:03:30 +0200542 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/*
546 * This routine is called by the kernel after it has written a
547 * series of characters to the tty device using put_char().
548 */
549static void
550sclp_vt220_flush_chars(struct tty_struct *tty)
551{
Michael Holzheu62b749422009-06-16 10:30:40 +0200552 if (!sclp_vt220_queue_running)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 sclp_vt220_emit_current();
554 else
555 sclp_vt220_flush_later = 1;
556}
557
558/*
559 * This routine returns the numbers of characters the tty driver
560 * will accept for queuing to be written. This number is subject
561 * to change as output buffers get emptied, or if the output flow
562 * control is acted.
563 */
564static int
565sclp_vt220_write_room(struct tty_struct *tty)
566{
567 unsigned long flags;
568 struct list_head *l;
569 int count;
570
571 spin_lock_irqsave(&sclp_vt220_lock, flags);
572 count = 0;
573 if (sclp_vt220_current_request != NULL)
574 count = sclp_vt220_space_left(sclp_vt220_current_request);
575 list_for_each(l, &sclp_vt220_empty)
576 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
577 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
578 return count;
579}
580
581/*
582 * Return number of buffered chars.
583 */
584static int
585sclp_vt220_chars_in_buffer(struct tty_struct *tty)
586{
587 unsigned long flags;
588 struct list_head *l;
589 struct sclp_vt220_request *r;
590 int count;
591
592 spin_lock_irqsave(&sclp_vt220_lock, flags);
593 count = 0;
594 if (sclp_vt220_current_request != NULL)
595 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
596 list_for_each(l, &sclp_vt220_outqueue) {
597 r = list_entry(l, struct sclp_vt220_request, list);
598 count += sclp_vt220_chars_stored(r);
599 }
600 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
601 return count;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
605 * Pass on all buffers to the hardware. Return only when there are no more
606 * buffers pending.
607 */
608static void
609sclp_vt220_flush_buffer(struct tty_struct *tty)
610{
611 sclp_vt220_emit_current();
612}
613
Peter Oberparleiterad211792008-07-14 09:59:07 +0200614/* Release allocated pages. */
615static void __init __sclp_vt220_free_pages(void)
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200616{
617 struct list_head *page, *p;
618
619 list_for_each_safe(page, p, &sclp_vt220_empty) {
620 list_del(page);
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200621 free_page((unsigned long) page);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200622 }
623}
624
Peter Oberparleiterad211792008-07-14 09:59:07 +0200625/* Release memory and unregister from sclp core. Controlled by init counting -
626 * only the last invoker will actually perform these actions. */
627static void __init __sclp_vt220_cleanup(void)
628{
629 sclp_vt220_init_count--;
630 if (sclp_vt220_init_count != 0)
631 return;
632 sclp_unregister(&sclp_vt220_register);
633 __sclp_vt220_free_pages();
Jiri Slaby191c5f12012-11-15 09:49:56 +0100634 tty_port_destroy(&sclp_vt220_port);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200635}
636
637/* Allocate buffer pages and register with sclp core. Controlled by init
638 * counting - only the first invoker will actually perform these actions. */
639static int __init __sclp_vt220_init(int num_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 void *page;
642 int i;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100643 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Peter Oberparleiterad211792008-07-14 09:59:07 +0200645 sclp_vt220_init_count++;
646 if (sclp_vt220_init_count != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_lock_init(&sclp_vt220_lock);
649 INIT_LIST_HEAD(&sclp_vt220_empty);
650 INIT_LIST_HEAD(&sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 init_timer(&sclp_vt220_timer);
Jiri Slaby092f7372012-04-02 13:54:13 +0200652 tty_port_init(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 sclp_vt220_current_request = NULL;
654 sclp_vt220_buffered_chars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 sclp_vt220_flush_later = 0;
656
657 /* Allocate pages for output buffering */
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200658 rc = -ENOMEM;
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200659 for (i = 0; i < num_pages; i++) {
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200660 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
661 if (!page)
Peter Oberparleiterad211792008-07-14 09:59:07 +0200662 goto out;
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200663 list_add_tail(page, &sclp_vt220_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100665 rc = sclp_register(&sclp_vt220_register);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200666out:
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100667 if (rc) {
Peter Oberparleiterad211792008-07-14 09:59:07 +0200668 __sclp_vt220_free_pages();
669 sclp_vt220_init_count--;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100670 tty_port_destroy(&sclp_vt220_port);
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100671 }
672 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700675static const struct tty_operations sclp_vt220_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .open = sclp_vt220_open,
677 .close = sclp_vt220_close,
678 .write = sclp_vt220_write,
679 .put_char = sclp_vt220_put_char,
680 .flush_chars = sclp_vt220_flush_chars,
681 .write_room = sclp_vt220_write_room,
682 .chars_in_buffer = sclp_vt220_chars_in_buffer,
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200683 .flush_buffer = sclp_vt220_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684};
685
686/*
687 * Register driver with SCLP and Linux and initialize internal tty structures.
688 */
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200689static int __init sclp_vt220_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct tty_driver *driver;
692 int rc;
693
694 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
695 * symmetry between VM and LPAR systems regarding ttyS1. */
696 driver = alloc_tty_driver(1);
697 if (!driver)
698 return -ENOMEM;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200699 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200700 if (rc)
701 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 driver->driver_name = SCLP_VT220_DRIVER_NAME;
704 driver->name = SCLP_VT220_DEVICE_NAME;
705 driver->major = SCLP_VT220_MAJOR;
706 driver->minor_start = SCLP_VT220_MINOR;
707 driver->type = TTY_DRIVER_TYPE_SYSTEM;
708 driver->subtype = SYSTEM_TYPE_TTY;
709 driver->init_termios = tty_std_termios;
710 driver->flags = TTY_DRIVER_REAL_RAW;
711 tty_set_operations(driver, &sclp_vt220_ops);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200712 tty_port_link_device(&sclp_vt220_port, driver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 rc = tty_register_driver(driver);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200715 if (rc)
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100716 goto out_init;
Peter Oberparleitere106e4e2014-04-09 13:10:05 +0200717 rc = sclp_register(&sclp_vt220_register_input);
718 if (rc)
719 goto out_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sclp_vt220_driver = driver;
721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Peter Oberparleitere106e4e2014-04-09 13:10:05 +0200723out_reg:
724 tty_unregister_driver(driver);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200725out_init:
Peter Oberparleiterad211792008-07-14 09:59:07 +0200726 __sclp_vt220_cleanup();
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200727out_driver:
728 put_tty_driver(driver);
729 return rc;
730}
731__initcall(sclp_vt220_tty_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100733static void __sclp_vt220_flush_buffer(void)
734{
735 unsigned long flags;
736
737 sclp_vt220_emit_current();
738 spin_lock_irqsave(&sclp_vt220_lock, flags);
739 if (timer_pending(&sclp_vt220_timer))
740 del_timer(&sclp_vt220_timer);
Michael Holzheu62b749422009-06-16 10:30:40 +0200741 while (sclp_vt220_queue_running) {
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100742 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
743 sclp_sync_wait();
744 spin_lock_irqsave(&sclp_vt220_lock, flags);
745 }
746 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
747}
748
Michael Holzheu62b749422009-06-16 10:30:40 +0200749/*
750 * Resume console: If there are cached messages, emit them.
751 */
752static void sclp_vt220_resume(void)
753{
754 unsigned long flags;
755
756 spin_lock_irqsave(&sclp_vt220_lock, flags);
757 sclp_vt220_suspended = 0;
758 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
759 sclp_vt220_emit_current();
760}
761
762/*
763 * Suspend console: Set suspend flag and flush console
764 */
765static void sclp_vt220_suspend(void)
766{
767 unsigned long flags;
768
769 spin_lock_irqsave(&sclp_vt220_lock, flags);
770 sclp_vt220_suspended = 1;
771 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
772 __sclp_vt220_flush_buffer();
773}
774
775static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
776 enum sclp_pm_event sclp_pm_event)
777{
778 switch (sclp_pm_event) {
779 case SCLP_PM_EVENT_FREEZE:
780 sclp_vt220_suspend();
781 break;
782 case SCLP_PM_EVENT_RESTORE:
783 case SCLP_PM_EVENT_THAW:
784 sclp_vt220_resume();
785 break;
786 }
787}
788
Michael Holzheuac522b62009-10-14 12:43:51 +0200789#ifdef CONFIG_SCLP_VT220_CONSOLE
790
791static void
792sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
793{
794 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
795}
796
797static struct tty_driver *
798sclp_vt220_con_device(struct console *c, int *index)
799{
800 *index = 0;
801 return sclp_vt220_driver;
802}
803
Holger Smolinski2332ce12008-10-10 21:33:27 +0200804static int
805sclp_vt220_notify(struct notifier_block *self,
806 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 __sclp_vt220_flush_buffer();
Holger Smolinski2332ce12008-10-10 21:33:27 +0200809 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Holger Smolinski2332ce12008-10-10 21:33:27 +0200812static struct notifier_block on_panic_nb = {
813 .notifier_call = sclp_vt220_notify,
814 .priority = 1,
815};
816
817static struct notifier_block on_reboot_nb = {
818 .notifier_call = sclp_vt220_notify,
819 .priority = 1,
820};
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/* Structure needed to register with printk */
823static struct console sclp_vt220_console =
824{
825 .name = SCLP_VT220_CONSOLE_NAME,
826 .write = sclp_vt220_con_write,
827 .device = sclp_vt220_con_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 .flags = CON_PRINTBUFFER,
829 .index = SCLP_VT220_CONSOLE_INDEX
830};
831
832static int __init
833sclp_vt220_con_init(void)
834{
835 int rc;
836
Martin Schwidefsky25b41a72013-05-24 12:30:03 +0200837 rc = __sclp_vt220_init(sclp_console_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (rc)
839 return rc;
840 /* Attach linux console */
Holger Smolinski2332ce12008-10-10 21:33:27 +0200841 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
842 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 register_console(&sclp_vt220_console);
844 return 0;
845}
846
847console_initcall(sclp_vt220_con_init);
848#endif /* CONFIG_SCLP_VT220_CONSOLE */
849