1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SCLP VT220 terminal driver.
4 *
5 * Copyright IBM Corp. 2003, 2009
6 *
7 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/panic_notifier.h>
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/timer.h>
16 #include <linux/kernel.h>
17 #include <linux/sysrq.h>
18 #include <linux/tty.h>
19 #include <linux/tty_driver.h>
20 #include <linux/tty_flip.h>
21 #include <linux/errno.h>
22 #include <linux/mm.h>
23 #include <linux/major.h>
24 #include <linux/console.h>
25 #include <linux/kdev_t.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/reboot.h>
29 #include <linux/slab.h>
30 #include <linux/workqueue.h>
31
32 #include <linux/uaccess.h>
33 #include "sclp.h"
34 #include "ctrlchar.h"
35
36 #define SCLP_VT220_MAJOR TTY_MAJOR
37 #define SCLP_VT220_MINOR 65
38 #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
39 #define SCLP_VT220_DEVICE_NAME "ttysclp"
40 #define SCLP_VT220_CONSOLE_NAME "ttysclp"
41 #define SCLP_VT220_CONSOLE_INDEX 0 /* console=ttysclp0 */
42
43 /* Representation of a single write request */
44 struct sclp_vt220_request {
45 struct list_head list;
46 struct sclp_req sclp_req;
47 int retry_count;
48 };
49
50 /* VT220 SCCB */
51 struct sclp_vt220_sccb {
52 struct sccb_header header;
53 struct evbuf_header evbuf;
54 };
55
56 struct sclp_vt220_evbuf {
57 struct evbuf_header header;
58 char type;
59 char data[];
60 } __packed;
61
62 struct sclp_vt220_resize_data_t {
63 u16 rows;
64 u16 cols;
65 } __packed;
66
67 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
68 sizeof(struct sclp_vt220_request) - \
69 sizeof(struct sclp_vt220_sccb))
70
71 /* Structures and data needed to register tty driver */
72 static struct tty_driver *sclp_vt220_driver;
73
74 static struct tty_port sclp_vt220_port;
75
76 /* Lock to protect internal data from concurrent access */
77 static DEFINE_SPINLOCK(sclp_vt220_lock);
78
79 /* List of empty pages to be used as write request buffers */
80 static LIST_HEAD(sclp_vt220_empty);
81
82 /* List of pending requests */
83 static LIST_HEAD(sclp_vt220_outqueue);
84
85 /* Flag that output queue is currently running */
86 static int sclp_vt220_queue_running;
87
88 /* Timer used for delaying write requests to merge subsequent messages into
89 * a single buffer */
90 static struct timer_list sclp_vt220_timer;
91
92 /* Pointer to current request buffer which has been partially filled but not
93 * yet sent */
94 static struct sclp_vt220_request *sclp_vt220_current_request;
95
96 /* Counter controlling core driver initialization. */
97 static int __initdata sclp_vt220_init_count;
98
99 /* Flag indicating that sclp_vt220_current_request should really
100 * have been already queued but wasn't because the SCLP was processing
101 * another buffer */
102 static int sclp_vt220_flush_later;
103
104 /* Work struct required for scheduling resize */
105 static struct work_struct sclp_vt220_resize_work;
106
107 /* Current vt220 terminal winsize */
108 static struct winsize sclp_vt220_winsize = {
109 .ws_row = 24,
110 .ws_col = 80,
111 };
112
113 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
114 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
115 static void sclp_vt220_emit_current(void);
116
117 /* Registration structure for SCLP output event buffers */
118 static struct sclp_register sclp_vt220_register = {
119 .send_mask = EVTYP_VT220MSG_MASK,
120 };
121
122 /* Registration structure for SCLP input event buffers */
123 static struct sclp_register sclp_vt220_register_input = {
124 .receive_mask = EVTYP_VT220MSG_MASK,
125 .receiver_fn = sclp_vt220_receiver_fn,
126 };
127
128
129 /*
130 * Put provided request buffer back into queue and check emit pending
131 * buffers if necessary.
132 */
133 static void
sclp_vt220_process_queue(struct sclp_vt220_request * request)134 sclp_vt220_process_queue(struct sclp_vt220_request *request)
135 {
136 unsigned long flags;
137 void *page;
138
139 do {
140 /* Put buffer back to list of empty buffers */
141 page = request->sclp_req.sccb;
142 spin_lock_irqsave(&sclp_vt220_lock, flags);
143 /* Move request from outqueue to empty queue */
144 list_del(&request->list);
145 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
146 /* Check if there is a pending buffer on the out queue. */
147 request = NULL;
148 if (!list_empty(&sclp_vt220_outqueue))
149 request = list_entry(sclp_vt220_outqueue.next,
150 struct sclp_vt220_request, list);
151 if (!request) {
152 sclp_vt220_queue_running = 0;
153 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
154 break;
155 }
156 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
157 } while (__sclp_vt220_emit(request));
158 if (request == NULL && sclp_vt220_flush_later)
159 sclp_vt220_emit_current();
160 tty_port_tty_wakeup(&sclp_vt220_port);
161 }
162
163 #define SCLP_BUFFER_MAX_RETRY 1
164
165 /*
166 * Callback through which the result of a write request is reported by the
167 * SCLP.
168 */
169 static void
sclp_vt220_callback(struct sclp_req * request,void * data)170 sclp_vt220_callback(struct sclp_req *request, void *data)
171 {
172 struct sclp_vt220_request *vt220_request;
173 struct sclp_vt220_sccb *sccb;
174
175 vt220_request = (struct sclp_vt220_request *) data;
176 if (request->status == SCLP_REQ_FAILED) {
177 sclp_vt220_process_queue(vt220_request);
178 return;
179 }
180 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
181
182 /* Check SCLP response code and choose suitable action */
183 switch (sccb->header.response_code) {
184 case 0x0020 :
185 break;
186
187 case 0x05f0: /* Target resource in improper state */
188 break;
189
190 case 0x0340: /* Contained SCLP equipment check */
191 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
192 break;
193 /* Remove processed buffers and requeue rest */
194 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
195 /* Not all buffers were processed */
196 sccb->header.response_code = 0x0000;
197 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
198 if (sclp_add_request(request) == 0)
199 return;
200 }
201 break;
202
203 case 0x0040: /* SCLP equipment check */
204 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
205 break;
206 sccb->header.response_code = 0x0000;
207 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
208 if (sclp_add_request(request) == 0)
209 return;
210 break;
211
212 default:
213 break;
214 }
215 sclp_vt220_process_queue(vt220_request);
216 }
217
218 /*
219 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
220 * otherwise.
221 */
222 static int
__sclp_vt220_emit(struct sclp_vt220_request * request)223 __sclp_vt220_emit(struct sclp_vt220_request *request)
224 {
225 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
226 request->sclp_req.status = SCLP_REQ_FILLED;
227 request->sclp_req.callback = sclp_vt220_callback;
228 request->sclp_req.callback_data = (void *) request;
229
230 return sclp_add_request(&request->sclp_req);
231 }
232
233 /*
234 * Queue and emit current request.
235 */
236 static void
sclp_vt220_emit_current(void)237 sclp_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 if (sclp_vt220_current_request) {
245 sccb = (struct sclp_vt220_sccb *)
246 sclp_vt220_current_request->sclp_req.sccb;
247 /* Only emit buffers with content */
248 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
249 list_add_tail(&sclp_vt220_current_request->list,
250 &sclp_vt220_outqueue);
251 sclp_vt220_current_request = NULL;
252 timer_delete(&sclp_vt220_timer);
253 }
254 sclp_vt220_flush_later = 0;
255 }
256 if (sclp_vt220_queue_running)
257 goto out_unlock;
258 if (list_empty(&sclp_vt220_outqueue))
259 goto out_unlock;
260 request = list_first_entry(&sclp_vt220_outqueue,
261 struct sclp_vt220_request, list);
262 sclp_vt220_queue_running = 1;
263 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
264
265 if (__sclp_vt220_emit(request))
266 sclp_vt220_process_queue(request);
267 return;
268 out_unlock:
269 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
270 }
271
272 #define SCLP_NORMAL_WRITE 0x00
273
274 /*
275 * Helper function to initialize a page with the sclp request structure.
276 */
277 static struct sclp_vt220_request *
sclp_vt220_initialize_page(void * page)278 sclp_vt220_initialize_page(void *page)
279 {
280 struct sclp_vt220_request *request;
281 struct sclp_vt220_sccb *sccb;
282
283 /* Place request structure at end of page */
284 request = ((struct sclp_vt220_request *)
285 ((addr_t) page + PAGE_SIZE)) - 1;
286 request->retry_count = 0;
287 request->sclp_req.sccb = page;
288 /* SCCB goes at start of page */
289 sccb = (struct sclp_vt220_sccb *) page;
290 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
291 sccb->header.length = sizeof(struct sclp_vt220_sccb);
292 sccb->header.function_code = SCLP_NORMAL_WRITE;
293 sccb->header.response_code = 0x0000;
294 sccb->evbuf.type = EVTYP_VT220MSG;
295 sccb->evbuf.length = sizeof(struct evbuf_header);
296
297 return request;
298 }
299
300 static inline unsigned int
sclp_vt220_space_left(struct sclp_vt220_request * request)301 sclp_vt220_space_left(struct sclp_vt220_request *request)
302 {
303 struct sclp_vt220_sccb *sccb;
304 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
305 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
306 sccb->header.length;
307 }
308
309 static inline unsigned int
sclp_vt220_chars_stored(struct sclp_vt220_request * request)310 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
311 {
312 struct sclp_vt220_sccb *sccb;
313 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
314 return sccb->evbuf.length - sizeof(struct evbuf_header);
315 }
316
317 /*
318 * Add msg to buffer associated with request. Return the number of characters
319 * added.
320 */
321 static int
sclp_vt220_add_msg(struct sclp_vt220_request * request,const unsigned char * msg,int count,int convertlf)322 sclp_vt220_add_msg(struct sclp_vt220_request *request,
323 const unsigned char *msg, int count, int convertlf)
324 {
325 struct sclp_vt220_sccb *sccb;
326 void *buffer;
327 unsigned char c;
328 int from;
329 int to;
330
331 if (count > sclp_vt220_space_left(request))
332 count = sclp_vt220_space_left(request);
333 if (count <= 0)
334 return 0;
335
336 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
337 buffer = (void *) ((addr_t) sccb + sccb->header.length);
338
339 if (convertlf) {
340 /* Perform Linefeed conversion (0x0a -> 0x0d 0x0a)*/
341 for (from=0, to=0;
342 (from < count) && (to < sclp_vt220_space_left(request));
343 from++) {
344 /* Retrieve character */
345 c = msg[from];
346 /* Perform conversion */
347 if (c == 0x0a) {
348 if (to + 1 < sclp_vt220_space_left(request)) {
349 ((unsigned char *) buffer)[to++] = 0x0d;
350 ((unsigned char *) buffer)[to++] = c;
351 } else
352 break;
353
354 } else
355 ((unsigned char *) buffer)[to++] = c;
356 }
357 sccb->header.length += to;
358 sccb->evbuf.length += to;
359 return from;
360 } else {
361 memcpy(buffer, (const void *) msg, count);
362 sccb->header.length += count;
363 sccb->evbuf.length += count;
364 return count;
365 }
366 }
367
368 /*
369 * Emit buffer after having waited long enough for more data to arrive.
370 */
371 static void
sclp_vt220_timeout(struct timer_list * unused)372 sclp_vt220_timeout(struct timer_list *unused)
373 {
374 sclp_vt220_emit_current();
375 }
376
377 #define BUFFER_MAX_DELAY HZ/20
378
379 /*
380 * Drop oldest console buffer if sclp_con_drop is set
381 */
382 static int
sclp_vt220_drop_buffer(void)383 sclp_vt220_drop_buffer(void)
384 {
385 struct list_head *list;
386 struct sclp_vt220_request *request;
387 void *page;
388
389 if (!sclp_console_drop)
390 return 0;
391 list = sclp_vt220_outqueue.next;
392 if (sclp_vt220_queue_running)
393 /* The first element is in I/O */
394 list = list->next;
395 if (list == &sclp_vt220_outqueue)
396 return 0;
397 list_del(list);
398 request = list_entry(list, struct sclp_vt220_request, list);
399 page = request->sclp_req.sccb;
400 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
401 return 1;
402 }
403
404 /*
405 * Internal implementation of the write function. Write COUNT bytes of data
406 * from memory at BUF
407 * to the SCLP interface. In case that the data does not fit into the current
408 * write buffer, emit the current one and allocate a new one. If there are no
409 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
410 * is non-zero, the buffer will be scheduled for emitting after a timeout -
411 * otherwise the user has to explicitly call the flush function.
412 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
413 * buffer should be converted to 0x0a 0x0d. After completion, return the number
414 * of bytes written.
415 */
416 static int
__sclp_vt220_write(const unsigned char * buf,int count,int do_schedule,int convertlf,int may_fail)417 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
418 int convertlf, int may_fail)
419 {
420 unsigned long flags;
421 void *page;
422 int written;
423 int overall_written;
424
425 if (count <= 0)
426 return 0;
427 overall_written = 0;
428 spin_lock_irqsave(&sclp_vt220_lock, flags);
429 do {
430 /* Create an sclp output buffer if none exists yet */
431 if (sclp_vt220_current_request == NULL) {
432 if (list_empty(&sclp_vt220_empty))
433 sclp_console_full++;
434 while (list_empty(&sclp_vt220_empty)) {
435 if (may_fail)
436 goto out;
437 if (sclp_vt220_drop_buffer())
438 break;
439 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
440
441 sclp_sync_wait();
442 spin_lock_irqsave(&sclp_vt220_lock, flags);
443 }
444 page = (void *) sclp_vt220_empty.next;
445 list_del((struct list_head *) page);
446 sclp_vt220_current_request =
447 sclp_vt220_initialize_page(page);
448 }
449 /* Try to write the string to the current request buffer */
450 written = sclp_vt220_add_msg(sclp_vt220_current_request,
451 buf, count, convertlf);
452 overall_written += written;
453 if (written == count)
454 break;
455 /*
456 * Not all characters could be written to the current
457 * output buffer. Emit the buffer, create a new buffer
458 * and then output the rest of the string.
459 */
460 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
461 sclp_vt220_emit_current();
462 spin_lock_irqsave(&sclp_vt220_lock, flags);
463 buf += written;
464 count -= written;
465 } while (count > 0);
466 /* Setup timer to output current console buffer after some time */
467 if (sclp_vt220_current_request != NULL &&
468 !timer_pending(&sclp_vt220_timer) && do_schedule) {
469 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
470 add_timer(&sclp_vt220_timer);
471 }
472 out:
473 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
474 return overall_written;
475 }
476
477 /*
478 * This routine is called by the kernel to write a series of
479 * characters to the tty device. The characters may come from
480 * user space or kernel space. This routine will return the
481 * number of characters actually accepted for writing.
482 */
483 static ssize_t
sclp_vt220_write(struct tty_struct * tty,const u8 * buf,size_t count)484 sclp_vt220_write(struct tty_struct *tty, const u8 *buf, size_t count)
485 {
486 return __sclp_vt220_write(buf, count, 1, 0, 1);
487 }
488
489 #define SCLP_VT220_SESSION_ENDED 0x01
490 #define SCLP_VT220_SESSION_STARTED 0x80
491 #define SCLP_VT220_SESSION_DATA 0x00
492 #define SCLP_VT220_SESSION_RESIZE 0x08
493
494 #ifdef CONFIG_MAGIC_SYSRQ
495
496 static int sysrq_pressed;
497 static struct sysrq_work sysrq;
498
sclp_vt220_reset_session(void)499 static void sclp_vt220_reset_session(void)
500 {
501 sysrq_pressed = 0;
502 }
503
sclp_vt220_handle_input(const char * buffer,unsigned int count)504 static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
505 {
506 int i;
507
508 for (i = 0; i < count; i++) {
509 /* Handle magic sys request */
510 if (buffer[i] == ('O' ^ 0100)) { /* CTRL-O */
511 /*
512 * If pressed again, reset sysrq_pressed
513 * and flip CTRL-O character
514 */
515 sysrq_pressed = !sysrq_pressed;
516 if (sysrq_pressed)
517 continue;
518 } else if (sysrq_pressed) {
519 sysrq.key = buffer[i];
520 schedule_sysrq_work(&sysrq);
521 sysrq_pressed = 0;
522 continue;
523 }
524 tty_insert_flip_char(&sclp_vt220_port, buffer[i], 0);
525 }
526 }
527
528 #else
529
sclp_vt220_reset_session(void)530 static void sclp_vt220_reset_session(void)
531 {
532 }
533
sclp_vt220_handle_input(const char * buffer,unsigned int count)534 static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
535 {
536 tty_insert_flip_string(&sclp_vt220_port, buffer, count);
537 }
538
539 #endif
540
sclp_vt220_resize(struct work_struct * work)541 static void sclp_vt220_resize(struct work_struct *work)
542 {
543 struct tty_struct *tty;
544 struct winsize ws;
545
546 spin_lock_irq(&sclp_vt220_lock);
547 ws = sclp_vt220_winsize;
548 spin_unlock_irq(&sclp_vt220_lock);
549
550 tty = tty_port_tty_get(&sclp_vt220_port);
551 if (!tty)
552 return;
553
554 tty_do_resize(tty, &ws);
555 tty_kref_put(tty);
556 }
557
sclp_vt220_resize_sched(void * buffer,unsigned int count)558 static void sclp_vt220_resize_sched(void *buffer, unsigned int count)
559 {
560 struct sclp_vt220_resize_data_t *data = buffer;
561 unsigned long flags;
562
563 if (count < sizeof(*data))
564 return;
565
566 spin_lock_irqsave(&sclp_vt220_lock, flags);
567 sclp_vt220_winsize.ws_row = data->rows;
568 sclp_vt220_winsize.ws_col = data->cols;
569 schedule_work(&sclp_vt220_resize_work);
570 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
571 }
572
573 /*
574 * Called by the SCLP to report incoming event buffers.
575 */
sclp_vt220_receiver_fn(struct evbuf_header * evbuf)576 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
577 {
578 struct sclp_vt220_evbuf *buffer;
579 unsigned int count;
580
581 if (evbuf->length < offsetof(struct sclp_vt220_evbuf, data))
582 return;
583
584 buffer = (struct sclp_vt220_evbuf *)evbuf;
585 count = evbuf->length - offsetof(struct sclp_vt220_evbuf, data);
586
587 switch (buffer->type) {
588 case SCLP_VT220_SESSION_ENDED:
589 case SCLP_VT220_SESSION_STARTED:
590 sclp_vt220_reset_session();
591 break;
592 case SCLP_VT220_SESSION_DATA:
593 /* Send input to line discipline */
594 sclp_vt220_handle_input(buffer->data, count);
595 tty_flip_buffer_push(&sclp_vt220_port);
596 break;
597 case SCLP_VT220_SESSION_RESIZE:
598 sclp_vt220_resize_sched(buffer->data, count);
599 break;
600 }
601 }
602
603 /*
604 * This routine is called when a particular tty device is opened.
605 */
606 static int
sclp_vt220_open(struct tty_struct * tty,struct file * filp)607 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
608 {
609 if (tty->count == 1) {
610 tty_port_tty_set(&sclp_vt220_port, tty);
611
612 mutex_lock(&tty->winsize_mutex);
613 spin_lock_irq(&sclp_vt220_lock);
614 tty->winsize = sclp_vt220_winsize;
615 spin_unlock_irq(&sclp_vt220_lock);
616 mutex_unlock(&tty->winsize_mutex);
617 }
618 return 0;
619 }
620
621 /*
622 * This routine is called when a particular tty device is closed.
623 */
624 static void
sclp_vt220_close(struct tty_struct * tty,struct file * filp)625 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
626 {
627 if (tty->count == 1)
628 tty_port_tty_set(&sclp_vt220_port, NULL);
629 }
630
631 /*
632 * This routine is called by the kernel to write a single
633 * character to the tty device. If the kernel uses this routine,
634 * it must call the flush_chars() routine (if defined) when it is
635 * done stuffing characters into the driver.
636 */
637 static int
sclp_vt220_put_char(struct tty_struct * tty,u8 ch)638 sclp_vt220_put_char(struct tty_struct *tty, u8 ch)
639 {
640 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
641 }
642
643 /*
644 * This routine is called by the kernel after it has written a
645 * series of characters to the tty device using put_char().
646 */
647 static void
sclp_vt220_flush_chars(struct tty_struct * tty)648 sclp_vt220_flush_chars(struct tty_struct *tty)
649 {
650 if (!sclp_vt220_queue_running)
651 sclp_vt220_emit_current();
652 else
653 sclp_vt220_flush_later = 1;
654 }
655
656 /*
657 * This routine returns the numbers of characters the tty driver
658 * will accept for queuing to be written. This number is subject
659 * to change as output buffers get emptied, or if the output flow
660 * control is acted.
661 */
662 static unsigned int
sclp_vt220_write_room(struct tty_struct * tty)663 sclp_vt220_write_room(struct tty_struct *tty)
664 {
665 unsigned long flags;
666 struct list_head *l;
667 unsigned int count;
668
669 spin_lock_irqsave(&sclp_vt220_lock, flags);
670 count = 0;
671 if (sclp_vt220_current_request != NULL)
672 count = sclp_vt220_space_left(sclp_vt220_current_request);
673 list_for_each(l, &sclp_vt220_empty)
674 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
675 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
676 return count;
677 }
678
679 /*
680 * Return number of buffered chars.
681 */
682 static unsigned int
sclp_vt220_chars_in_buffer(struct tty_struct * tty)683 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
684 {
685 unsigned long flags;
686 struct list_head *l;
687 struct sclp_vt220_request *r;
688 unsigned int count = 0;
689
690 spin_lock_irqsave(&sclp_vt220_lock, flags);
691 if (sclp_vt220_current_request != NULL)
692 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
693 list_for_each(l, &sclp_vt220_outqueue) {
694 r = list_entry(l, struct sclp_vt220_request, list);
695 count += sclp_vt220_chars_stored(r);
696 }
697 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
698 return count;
699 }
700
701 /*
702 * Pass on all buffers to the hardware. Return only when there are no more
703 * buffers pending.
704 */
705 static void
sclp_vt220_flush_buffer(struct tty_struct * tty)706 sclp_vt220_flush_buffer(struct tty_struct *tty)
707 {
708 sclp_vt220_emit_current();
709 }
710
711 /* Release allocated pages. */
__sclp_vt220_free_pages(void)712 static void __init __sclp_vt220_free_pages(void)
713 {
714 struct list_head *page, *p;
715
716 list_for_each_safe(page, p, &sclp_vt220_empty) {
717 list_del(page);
718 free_page((unsigned long) page);
719 }
720 }
721
722 /* Release memory and unregister from sclp core. Controlled by init counting -
723 * only the last invoker will actually perform these actions. */
__sclp_vt220_cleanup(void)724 static void __init __sclp_vt220_cleanup(void)
725 {
726 sclp_vt220_init_count--;
727 if (sclp_vt220_init_count != 0)
728 return;
729 sclp_unregister(&sclp_vt220_register);
730 __sclp_vt220_free_pages();
731 tty_port_destroy(&sclp_vt220_port);
732 }
733
734 /* Allocate buffer pages and register with sclp core. Controlled by init
735 * counting - only the first invoker will actually perform these actions. */
__sclp_vt220_init(int num_pages)736 static int __init __sclp_vt220_init(int num_pages)
737 {
738 void *page;
739 int i;
740 int rc;
741
742 sclp_vt220_init_count++;
743 if (sclp_vt220_init_count != 1)
744 return 0;
745 timer_setup(&sclp_vt220_timer, sclp_vt220_timeout, 0);
746 tty_port_init(&sclp_vt220_port);
747 sclp_vt220_current_request = NULL;
748 sclp_vt220_flush_later = 0;
749
750 /* Allocate pages for output buffering */
751 rc = -ENOMEM;
752 for (i = 0; i < num_pages; i++) {
753 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
754 if (!page)
755 goto out;
756 list_add_tail(page, &sclp_vt220_empty);
757 }
758 rc = sclp_register(&sclp_vt220_register);
759 out:
760 if (rc) {
761 __sclp_vt220_free_pages();
762 sclp_vt220_init_count--;
763 tty_port_destroy(&sclp_vt220_port);
764 }
765 return rc;
766 }
767
768 static const struct tty_operations sclp_vt220_ops = {
769 .open = sclp_vt220_open,
770 .close = sclp_vt220_close,
771 .write = sclp_vt220_write,
772 .put_char = sclp_vt220_put_char,
773 .flush_chars = sclp_vt220_flush_chars,
774 .write_room = sclp_vt220_write_room,
775 .chars_in_buffer = sclp_vt220_chars_in_buffer,
776 .flush_buffer = sclp_vt220_flush_buffer,
777 };
778
779 /*
780 * Register driver with SCLP and Linux and initialize internal tty structures.
781 */
sclp_vt220_tty_init(void)782 static int __init sclp_vt220_tty_init(void)
783 {
784 struct tty_driver *driver;
785 int rc;
786
787 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
788 * symmetry between VM and LPAR systems regarding ttyS1. */
789 driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
790 if (IS_ERR(driver))
791 return PTR_ERR(driver);
792 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
793 if (rc)
794 goto out_driver;
795
796 driver->driver_name = SCLP_VT220_DRIVER_NAME;
797 driver->name = SCLP_VT220_DEVICE_NAME;
798 driver->major = SCLP_VT220_MAJOR;
799 driver->minor_start = SCLP_VT220_MINOR;
800 driver->type = TTY_DRIVER_TYPE_SYSTEM;
801 driver->subtype = SYSTEM_TYPE_TTY;
802 driver->init_termios = tty_std_termios;
803 tty_set_operations(driver, &sclp_vt220_ops);
804 tty_port_link_device(&sclp_vt220_port, driver, 0);
805
806 rc = tty_register_driver(driver);
807 if (rc)
808 goto out_init;
809
810 INIT_WORK(&sclp_vt220_resize_work, sclp_vt220_resize);
811 rc = sclp_register(&sclp_vt220_register_input);
812 if (rc)
813 goto out_reg;
814 sclp_vt220_driver = driver;
815 return 0;
816
817 out_reg:
818 tty_unregister_driver(driver);
819 out_init:
820 __sclp_vt220_cleanup();
821 out_driver:
822 tty_driver_kref_put(driver);
823 return rc;
824 }
825 __initcall(sclp_vt220_tty_init);
826
827 #ifdef CONFIG_SCLP_VT220_CONSOLE
828
829 static void
sclp_vt220_con_write(struct console * con,const char * buf,unsigned int count)830 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
831 {
832 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
833 }
834
835 static struct tty_driver *
sclp_vt220_con_device(struct console * c,int * index)836 sclp_vt220_con_device(struct console *c, int *index)
837 {
838 *index = 0;
839 return sclp_vt220_driver;
840 }
841
842 /*
843 * This panic/reboot notifier runs in atomic context, so
844 * locking restrictions apply to prevent potential lockups.
845 */
846 static int
sclp_vt220_notify(struct notifier_block * self,unsigned long event,void * data)847 sclp_vt220_notify(struct notifier_block *self,
848 unsigned long event, void *data)
849 {
850 unsigned long flags;
851
852 if (spin_is_locked(&sclp_vt220_lock))
853 return NOTIFY_DONE;
854
855 sclp_vt220_emit_current();
856
857 spin_lock_irqsave(&sclp_vt220_lock, flags);
858 timer_delete(&sclp_vt220_timer);
859 while (sclp_vt220_queue_running) {
860 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
861 sclp_sync_wait();
862 spin_lock_irqsave(&sclp_vt220_lock, flags);
863 }
864 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
865
866 return NOTIFY_DONE;
867 }
868
869 static struct notifier_block on_panic_nb = {
870 .notifier_call = sclp_vt220_notify,
871 .priority = INT_MIN + 1, /* run the callback late */
872 };
873
874 static struct notifier_block on_reboot_nb = {
875 .notifier_call = sclp_vt220_notify,
876 .priority = INT_MIN + 1, /* run the callback late */
877 };
878
879 /* Structure needed to register with printk */
880 static struct console sclp_vt220_console =
881 {
882 .name = SCLP_VT220_CONSOLE_NAME,
883 .write = sclp_vt220_con_write,
884 .device = sclp_vt220_con_device,
885 .flags = CON_PRINTBUFFER,
886 .index = SCLP_VT220_CONSOLE_INDEX
887 };
888
889 static int __init
sclp_vt220_con_init(void)890 sclp_vt220_con_init(void)
891 {
892 int rc;
893
894 rc = __sclp_vt220_init(sclp_console_pages);
895 if (rc)
896 return rc;
897 /* Attach linux console */
898 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
899 register_reboot_notifier(&on_reboot_nb);
900 register_console(&sclp_vt220_console);
901 return 0;
902 }
903
904 console_initcall(sclp_vt220_con_init);
905 #endif /* CONFIG_SCLP_VT220_CONSOLE */
906
907