1 /*
2  * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3  * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
4  * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
5  * Copyright (C) 2004 IBM Corporation
6  *
7  * Additional Author(s):
8  *  Ryan S. Arnold <rsa@us.ibm.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 
25 #include <linux/console.h>
26 #include <linux/cpumask.h>
27 #include <linux/init.h>
28 #include <linux/kbd_kern.h>
29 #include <linux/kernel.h>
30 #include <linux/kthread.h>
31 #include <linux/list.h>
32 #include <linux/module.h>
33 #include <linux/major.h>
34 #include <linux/atomic.h>
35 #include <linux/sysrq.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
38 #include <linux/sched.h>
39 #include <linux/spinlock.h>
40 #include <linux/delay.h>
41 #include <linux/freezer.h>
42 #include <linux/slab.h>
43 #include <linux/serial_core.h>
44 
45 #include <asm/uaccess.h>
46 
47 #include "hvc_console.h"
48 
49 #define HVC_MAJOR	229
50 #define HVC_MINOR	0
51 
52 /*
53  * Wait this long per iteration while trying to push buffered data to the
54  * hypervisor before allowing the tty to complete a close operation.
55  */
56 #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
57 
58 /*
59  * These sizes are most efficient for vio, because they are the
60  * native transfer size. We could make them selectable in the
61  * future to better deal with backends that want other buffer sizes.
62  */
63 #define N_OUTBUF	16
64 #define N_INBUF		16
65 
66 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
67 
68 static struct tty_driver *hvc_driver;
69 static struct task_struct *hvc_task;
70 
71 /* Picks up late kicks after list walk but before schedule() */
72 static int hvc_kicked;
73 
74 /* hvc_init is triggered from hvc_alloc, i.e. only when actually used */
75 static atomic_t hvc_needs_init __read_mostly = ATOMIC_INIT(-1);
76 
77 static int hvc_init(void);
78 
79 #ifdef CONFIG_MAGIC_SYSRQ
80 static int sysrq_pressed;
81 #endif
82 
83 /* dynamic list of hvc_struct instances */
84 static LIST_HEAD(hvc_structs);
85 
86 /*
87  * Protect the list of hvc_struct instances from inserts and removals during
88  * list traversal.
89  */
90 static DEFINE_SPINLOCK(hvc_structs_lock);
91 
92 /*
93  * This value is used to assign a tty->index value to a hvc_struct based
94  * upon order of exposure via hvc_probe(), when we can not match it to
95  * a console candidate registered with hvc_instantiate().
96  */
97 static int last_hvc = -1;
98 
99 /*
100  * Do not call this function with either the hvc_structs_lock or the hvc_struct
101  * lock held.  If successful, this function increments the kref reference
102  * count against the target hvc_struct so it should be released when finished.
103  */
hvc_get_by_index(int index)104 static struct hvc_struct *hvc_get_by_index(int index)
105 {
106 	struct hvc_struct *hp;
107 	unsigned long flags;
108 
109 	spin_lock(&hvc_structs_lock);
110 
111 	list_for_each_entry(hp, &hvc_structs, next) {
112 		spin_lock_irqsave(&hp->lock, flags);
113 		if (hp->index == index) {
114 			kref_get(&hp->kref);
115 			spin_unlock_irqrestore(&hp->lock, flags);
116 			spin_unlock(&hvc_structs_lock);
117 			return hp;
118 		}
119 		spin_unlock_irqrestore(&hp->lock, flags);
120 	}
121 	hp = NULL;
122 
123 	spin_unlock(&hvc_structs_lock);
124 	return hp;
125 }
126 
127 
128 /*
129  * Initial console vtermnos for console API usage prior to full console
130  * initialization.  Any vty adapter outside this range will not have usable
131  * console interfaces but can still be used as a tty device.  This has to be
132  * static because kmalloc will not work during early console init.
133  */
134 static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
135 static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
136 	{[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
137 
138 /*
139  * Console APIs, NOT TTY.  These APIs are available immediately when
140  * hvc_console_setup() finds adapters.
141  */
142 
hvc_console_print(struct console * co,const char * b,unsigned count)143 static void hvc_console_print(struct console *co, const char *b,
144 			      unsigned count)
145 {
146 	char c[N_OUTBUF] __ALIGNED__;
147 	unsigned i = 0, n = 0;
148 	int r, donecr = 0, index = co->index;
149 
150 	/* Console access attempt outside of acceptable console range. */
151 	if (index >= MAX_NR_HVC_CONSOLES)
152 		return;
153 
154 	/* This console adapter was removed so it is not usable. */
155 	if (vtermnos[index] == -1)
156 		return;
157 
158 	while (count > 0 || i > 0) {
159 		if (count > 0 && i < sizeof(c)) {
160 			if (b[n] == '\n' && !donecr) {
161 				c[i++] = '\r';
162 				donecr = 1;
163 			} else {
164 				c[i++] = b[n++];
165 				donecr = 0;
166 				--count;
167 			}
168 		} else {
169 			r = cons_ops[index]->put_chars(vtermnos[index], c, i);
170 			if (r <= 0) {
171 				/* throw away characters on error
172 				 * but spin in case of -EAGAIN */
173 				if (r != -EAGAIN)
174 					i = 0;
175 			} else if (r > 0) {
176 				i -= r;
177 				if (i > 0)
178 					memmove(c, c+r, i);
179 			}
180 		}
181 	}
182 }
183 
hvc_console_device(struct console * c,int * index)184 static struct tty_driver *hvc_console_device(struct console *c, int *index)
185 {
186 	if (vtermnos[c->index] == -1)
187 		return NULL;
188 
189 	*index = c->index;
190 	return hvc_driver;
191 }
192 
hvc_console_setup(struct console * co,char * options)193 static int hvc_console_setup(struct console *co, char *options)
194 {
195 	if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
196 		return -ENODEV;
197 
198 	if (vtermnos[co->index] == -1)
199 		return -ENODEV;
200 
201 	return 0;
202 }
203 
204 static struct console hvc_console = {
205 	.name		= "hvc",
206 	.write		= hvc_console_print,
207 	.device		= hvc_console_device,
208 	.setup		= hvc_console_setup,
209 	.flags		= CON_PRINTBUFFER,
210 	.index		= -1,
211 };
212 
213 /*
214  * Early console initialization.  Precedes driver initialization.
215  *
216  * (1) we are first, and the user specified another driver
217  * -- index will remain -1
218  * (2) we are first and the user specified no driver
219  * -- index will be set to 0, then we will fail setup.
220  * (3)  we are first and the user specified our driver
221  * -- index will be set to user specified driver, and we will fail
222  * (4) we are after driver, and this initcall will register us
223  * -- if the user didn't specify a driver then the console will match
224  *
225  * Note that for cases 2 and 3, we will match later when the io driver
226  * calls hvc_instantiate() and call register again.
227  */
hvc_console_init(void)228 static int __init hvc_console_init(void)
229 {
230 	register_console(&hvc_console);
231 	return 0;
232 }
233 console_initcall(hvc_console_init);
234 
235 /* callback when the kboject ref count reaches zero. */
destroy_hvc_struct(struct kref * kref)236 static void destroy_hvc_struct(struct kref *kref)
237 {
238 	struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
239 	unsigned long flags;
240 
241 	spin_lock(&hvc_structs_lock);
242 
243 	spin_lock_irqsave(&hp->lock, flags);
244 	list_del(&(hp->next));
245 	spin_unlock_irqrestore(&hp->lock, flags);
246 
247 	spin_unlock(&hvc_structs_lock);
248 
249 	kfree(hp);
250 }
251 
252 /*
253  * hvc_instantiate() is an early console discovery method which locates
254  * consoles * prior to the vio subsystem discovering them.  Hotplugged
255  * vty adapters do NOT get an hvc_instantiate() callback since they
256  * appear after early console init.
257  */
hvc_instantiate(uint32_t vtermno,int index,const struct hv_ops * ops)258 int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
259 {
260 	struct hvc_struct *hp;
261 
262 	if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
263 		return -1;
264 
265 	if (vtermnos[index] != -1)
266 		return -1;
267 
268 	/* make sure no no tty has been registered in this index */
269 	hp = hvc_get_by_index(index);
270 	if (hp) {
271 		kref_put(&hp->kref, destroy_hvc_struct);
272 		return -1;
273 	}
274 
275 	vtermnos[index] = vtermno;
276 	cons_ops[index] = ops;
277 
278 	/* reserve all indices up to and including this index */
279 	if (last_hvc < index)
280 		last_hvc = index;
281 
282 	/* if this index is what the user requested, then register
283 	 * now (setup won't fail at this point).  It's ok to just
284 	 * call register again if previously .setup failed.
285 	 */
286 	if (index == hvc_console.index)
287 		register_console(&hvc_console);
288 
289 	return 0;
290 }
291 EXPORT_SYMBOL_GPL(hvc_instantiate);
292 
293 /* Wake the sleeping khvcd */
hvc_kick(void)294 void hvc_kick(void)
295 {
296 	hvc_kicked = 1;
297 	wake_up_process(hvc_task);
298 }
299 EXPORT_SYMBOL_GPL(hvc_kick);
300 
hvc_unthrottle(struct tty_struct * tty)301 static void hvc_unthrottle(struct tty_struct *tty)
302 {
303 	hvc_kick();
304 }
305 
306 /*
307  * The TTY interface won't be used until after the vio layer has exposed the vty
308  * adapter to the kernel.
309  */
hvc_open(struct tty_struct * tty,struct file * filp)310 static int hvc_open(struct tty_struct *tty, struct file * filp)
311 {
312 	struct hvc_struct *hp;
313 	unsigned long flags;
314 	int rc = 0;
315 
316 	/* Auto increments kref reference if found. */
317 	if (!(hp = hvc_get_by_index(tty->index)))
318 		return -ENODEV;
319 
320 	spin_lock_irqsave(&hp->lock, flags);
321 	/* Check and then increment for fast path open. */
322 	if (hp->count++ > 0) {
323 		tty_kref_get(tty);
324 		spin_unlock_irqrestore(&hp->lock, flags);
325 		hvc_kick();
326 		return 0;
327 	} /* else count == 0 */
328 
329 	tty->driver_data = hp;
330 
331 	hp->tty = tty_kref_get(tty);
332 
333 	spin_unlock_irqrestore(&hp->lock, flags);
334 
335 	if (hp->ops->notifier_add)
336 		rc = hp->ops->notifier_add(hp, hp->data);
337 
338 	/*
339 	 * If the notifier fails we return an error.  The tty layer
340 	 * will call hvc_close() after a failed open but we don't want to clean
341 	 * up there so we'll clean up here and clear out the previously set
342 	 * tty fields and return the kref reference.
343 	 */
344 	if (rc) {
345 		spin_lock_irqsave(&hp->lock, flags);
346 		hp->tty = NULL;
347 		spin_unlock_irqrestore(&hp->lock, flags);
348 		tty_kref_put(tty);
349 		tty->driver_data = NULL;
350 		kref_put(&hp->kref, destroy_hvc_struct);
351 		printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
352 	}
353 	/* Force wakeup of the polling thread */
354 	hvc_kick();
355 
356 	return rc;
357 }
358 
hvc_close(struct tty_struct * tty,struct file * filp)359 static void hvc_close(struct tty_struct *tty, struct file * filp)
360 {
361 	struct hvc_struct *hp;
362 	unsigned long flags;
363 
364 	if (tty_hung_up_p(filp))
365 		return;
366 
367 	/*
368 	 * No driver_data means that this close was issued after a failed
369 	 * hvc_open by the tty layer's release_dev() function and we can just
370 	 * exit cleanly because the kref reference wasn't made.
371 	 */
372 	if (!tty->driver_data)
373 		return;
374 
375 	hp = tty->driver_data;
376 
377 	spin_lock_irqsave(&hp->lock, flags);
378 
379 	if (--hp->count == 0) {
380 		/* We are done with the tty pointer now. */
381 		hp->tty = NULL;
382 		spin_unlock_irqrestore(&hp->lock, flags);
383 
384 		if (hp->ops->notifier_del)
385 			hp->ops->notifier_del(hp, hp->data);
386 
387 		/* cancel pending tty resize work */
388 		cancel_work_sync(&hp->tty_resize);
389 
390 		/*
391 		 * Chain calls chars_in_buffer() and returns immediately if
392 		 * there is no buffered data otherwise sleeps on a wait queue
393 		 * waking periodically to check chars_in_buffer().
394 		 */
395 		tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT);
396 	} else {
397 		if (hp->count < 0)
398 			printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
399 				hp->vtermno, hp->count);
400 		spin_unlock_irqrestore(&hp->lock, flags);
401 	}
402 
403 	tty_kref_put(tty);
404 	kref_put(&hp->kref, destroy_hvc_struct);
405 }
406 
hvc_hangup(struct tty_struct * tty)407 static void hvc_hangup(struct tty_struct *tty)
408 {
409 	struct hvc_struct *hp = tty->driver_data;
410 	unsigned long flags;
411 	int temp_open_count;
412 
413 	if (!hp)
414 		return;
415 
416 	/* cancel pending tty resize work */
417 	cancel_work_sync(&hp->tty_resize);
418 
419 	spin_lock_irqsave(&hp->lock, flags);
420 
421 	/*
422 	 * The N_TTY line discipline has problems such that in a close vs
423 	 * open->hangup case this can be called after the final close so prevent
424 	 * that from happening for now.
425 	 */
426 	if (hp->count <= 0) {
427 		spin_unlock_irqrestore(&hp->lock, flags);
428 		return;
429 	}
430 
431 	temp_open_count = hp->count;
432 	hp->count = 0;
433 	hp->n_outbuf = 0;
434 	hp->tty = NULL;
435 
436 	spin_unlock_irqrestore(&hp->lock, flags);
437 
438 	if (hp->ops->notifier_hangup)
439 		hp->ops->notifier_hangup(hp, hp->data);
440 
441 	while(temp_open_count) {
442 		--temp_open_count;
443 		tty_kref_put(tty);
444 		kref_put(&hp->kref, destroy_hvc_struct);
445 	}
446 }
447 
448 /*
449  * Push buffered characters whether they were just recently buffered or waiting
450  * on a blocked hypervisor.  Call this function with hp->lock held.
451  */
hvc_push(struct hvc_struct * hp)452 static int hvc_push(struct hvc_struct *hp)
453 {
454 	int n;
455 
456 	n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
457 	if (n <= 0) {
458 		if (n == 0 || n == -EAGAIN) {
459 			hp->do_wakeup = 1;
460 			return 0;
461 		}
462 		/* throw away output on error; this happens when
463 		   there is no session connected to the vterm. */
464 		hp->n_outbuf = 0;
465 	} else
466 		hp->n_outbuf -= n;
467 	if (hp->n_outbuf > 0)
468 		memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
469 	else
470 		hp->do_wakeup = 1;
471 
472 	return n;
473 }
474 
hvc_write(struct tty_struct * tty,const unsigned char * buf,int count)475 static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
476 {
477 	struct hvc_struct *hp = tty->driver_data;
478 	unsigned long flags;
479 	int rsize, written = 0;
480 
481 	/* This write was probably executed during a tty close. */
482 	if (!hp)
483 		return -EPIPE;
484 
485 	if (hp->count <= 0)
486 		return -EIO;
487 
488 	spin_lock_irqsave(&hp->lock, flags);
489 
490 	/* Push pending writes */
491 	if (hp->n_outbuf > 0)
492 		hvc_push(hp);
493 
494 	while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
495 		if (rsize > count)
496 			rsize = count;
497 		memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
498 		count -= rsize;
499 		buf += rsize;
500 		hp->n_outbuf += rsize;
501 		written += rsize;
502 		hvc_push(hp);
503 	}
504 	spin_unlock_irqrestore(&hp->lock, flags);
505 
506 	/*
507 	 * Racy, but harmless, kick thread if there is still pending data.
508 	 */
509 	if (hp->n_outbuf)
510 		hvc_kick();
511 
512 	return written;
513 }
514 
515 /**
516  * hvc_set_winsz() - Resize the hvc tty terminal window.
517  * @work:	work structure.
518  *
519  * The routine shall not be called within an atomic context because it
520  * might sleep.
521  *
522  * Locking:	hp->lock
523  */
hvc_set_winsz(struct work_struct * work)524 static void hvc_set_winsz(struct work_struct *work)
525 {
526 	struct hvc_struct *hp;
527 	unsigned long hvc_flags;
528 	struct tty_struct *tty;
529 	struct winsize ws;
530 
531 	hp = container_of(work, struct hvc_struct, tty_resize);
532 
533 	spin_lock_irqsave(&hp->lock, hvc_flags);
534 	if (!hp->tty) {
535 		spin_unlock_irqrestore(&hp->lock, hvc_flags);
536 		return;
537 	}
538 	ws  = hp->ws;
539 	tty = tty_kref_get(hp->tty);
540 	spin_unlock_irqrestore(&hp->lock, hvc_flags);
541 
542 	tty_do_resize(tty, &ws);
543 	tty_kref_put(tty);
544 }
545 
546 /*
547  * This is actually a contract between the driver and the tty layer outlining
548  * how much write room the driver can guarantee will be sent OR BUFFERED.  This
549  * driver MUST honor the return value.
550  */
hvc_write_room(struct tty_struct * tty)551 static int hvc_write_room(struct tty_struct *tty)
552 {
553 	struct hvc_struct *hp = tty->driver_data;
554 
555 	if (!hp)
556 		return -1;
557 
558 	return hp->outbuf_size - hp->n_outbuf;
559 }
560 
hvc_chars_in_buffer(struct tty_struct * tty)561 static int hvc_chars_in_buffer(struct tty_struct *tty)
562 {
563 	struct hvc_struct *hp = tty->driver_data;
564 
565 	if (!hp)
566 		return 0;
567 	return hp->n_outbuf;
568 }
569 
570 /*
571  * timeout will vary between the MIN and MAX values defined here.  By default
572  * and during console activity we will use a default MIN_TIMEOUT of 10.  When
573  * the console is idle, we increase the timeout value on each pass through
574  * msleep until we reach the max.  This may be noticeable as a brief (average
575  * one second) delay on the console before the console responds to input when
576  * there has been no input for some time.
577  */
578 #define MIN_TIMEOUT		(10)
579 #define MAX_TIMEOUT		(2000)
580 static u32 timeout = MIN_TIMEOUT;
581 
582 #define HVC_POLL_READ	0x00000001
583 #define HVC_POLL_WRITE	0x00000002
584 
hvc_poll(struct hvc_struct * hp)585 int hvc_poll(struct hvc_struct *hp)
586 {
587 	struct tty_struct *tty;
588 	int i, n, poll_mask = 0;
589 	char buf[N_INBUF] __ALIGNED__;
590 	unsigned long flags;
591 	int read_total = 0;
592 	int written_total = 0;
593 
594 	spin_lock_irqsave(&hp->lock, flags);
595 
596 	/* Push pending writes */
597 	if (hp->n_outbuf > 0)
598 		written_total = hvc_push(hp);
599 
600 	/* Reschedule us if still some write pending */
601 	if (hp->n_outbuf > 0) {
602 		poll_mask |= HVC_POLL_WRITE;
603 		/* If hvc_push() was not able to write, sleep a few msecs */
604 		timeout = (written_total) ? 0 : MIN_TIMEOUT;
605 	}
606 
607 	/* No tty attached, just skip */
608 	tty = tty_kref_get(hp->tty);
609 	if (tty == NULL)
610 		goto bail;
611 
612 	/* Now check if we can get data (are we throttled ?) */
613 	if (test_bit(TTY_THROTTLED, &tty->flags))
614 		goto throttled;
615 
616 	/* If we aren't notifier driven and aren't throttled, we always
617 	 * request a reschedule
618 	 */
619 	if (!hp->irq_requested)
620 		poll_mask |= HVC_POLL_READ;
621 
622 	/* Read data if any */
623 	for (;;) {
624 		int count = tty_buffer_request_room(tty, N_INBUF);
625 
626 		/* If flip is full, just reschedule a later read */
627 		if (count == 0) {
628 			poll_mask |= HVC_POLL_READ;
629 			break;
630 		}
631 
632 		n = hp->ops->get_chars(hp->vtermno, buf, count);
633 		if (n <= 0) {
634 			/* Hangup the tty when disconnected from host */
635 			if (n == -EPIPE) {
636 				spin_unlock_irqrestore(&hp->lock, flags);
637 				tty_hangup(tty);
638 				spin_lock_irqsave(&hp->lock, flags);
639 			} else if ( n == -EAGAIN ) {
640 				/*
641 				 * Some back-ends can only ensure a certain min
642 				 * num of bytes read, which may be > 'count'.
643 				 * Let the tty clear the flip buff to make room.
644 				 */
645 				poll_mask |= HVC_POLL_READ;
646 			}
647 			break;
648 		}
649 		for (i = 0; i < n; ++i) {
650 #ifdef CONFIG_MAGIC_SYSRQ
651 			if (hp->index == hvc_console.index) {
652 				/* Handle the SysRq Hack */
653 				/* XXX should support a sequence */
654 				if (buf[i] == '\x0f') {	/* ^O */
655 					/* if ^O is pressed again, reset
656 					 * sysrq_pressed and flip ^O char */
657 					sysrq_pressed = !sysrq_pressed;
658 					if (sysrq_pressed)
659 						continue;
660 				} else if (sysrq_pressed) {
661 					handle_sysrq(buf[i]);
662 					sysrq_pressed = 0;
663 					continue;
664 				}
665 			}
666 #endif /* CONFIG_MAGIC_SYSRQ */
667 			tty_insert_flip_char(tty, buf[i], 0);
668 		}
669 
670 		read_total += n;
671 	}
672  throttled:
673 	/* Wakeup write queue if necessary */
674 	if (hp->do_wakeup) {
675 		hp->do_wakeup = 0;
676 		tty_wakeup(tty);
677 	}
678  bail:
679 	spin_unlock_irqrestore(&hp->lock, flags);
680 
681 	if (read_total) {
682 		/* Activity is occurring, so reset the polling backoff value to
683 		   a minimum for performance. */
684 		timeout = MIN_TIMEOUT;
685 
686 		tty_flip_buffer_push(tty);
687 	}
688 	if (tty)
689 		tty_kref_put(tty);
690 
691 	return poll_mask;
692 }
693 EXPORT_SYMBOL_GPL(hvc_poll);
694 
695 /**
696  * __hvc_resize() - Update terminal window size information.
697  * @hp:		HVC console pointer
698  * @ws:		Terminal window size structure
699  *
700  * Stores the specified window size information in the hvc structure of @hp.
701  * The function schedule the tty resize update.
702  *
703  * Locking:	Locking free; the function MUST be called holding hp->lock
704  */
__hvc_resize(struct hvc_struct * hp,struct winsize ws)705 void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
706 {
707 	hp->ws = ws;
708 	schedule_work(&hp->tty_resize);
709 }
710 EXPORT_SYMBOL_GPL(__hvc_resize);
711 
712 /*
713  * This kthread is either polling or interrupt driven.  This is determined by
714  * calling hvc_poll() who determines whether a console adapter support
715  * interrupts.
716  */
khvcd(void * unused)717 static int khvcd(void *unused)
718 {
719 	int poll_mask;
720 	struct hvc_struct *hp;
721 
722 	set_freezable();
723 	do {
724 		poll_mask = 0;
725 		hvc_kicked = 0;
726 		try_to_freeze();
727 		wmb();
728 		if (!cpus_are_in_xmon()) {
729 			spin_lock(&hvc_structs_lock);
730 			list_for_each_entry(hp, &hvc_structs, next) {
731 				poll_mask |= hvc_poll(hp);
732 			}
733 			spin_unlock(&hvc_structs_lock);
734 		} else
735 			poll_mask |= HVC_POLL_READ;
736 		if (hvc_kicked)
737 			continue;
738 		set_current_state(TASK_INTERRUPTIBLE);
739 		if (!hvc_kicked) {
740 			if (poll_mask == 0)
741 				schedule();
742 			else {
743 				if (timeout < MAX_TIMEOUT)
744 					timeout += (timeout >> 6) + 1;
745 
746 				msleep_interruptible(timeout);
747 			}
748 		}
749 		__set_current_state(TASK_RUNNING);
750 	} while (!kthread_should_stop());
751 
752 	return 0;
753 }
754 
hvc_tiocmget(struct tty_struct * tty)755 static int hvc_tiocmget(struct tty_struct *tty)
756 {
757 	struct hvc_struct *hp = tty->driver_data;
758 
759 	if (!hp || !hp->ops->tiocmget)
760 		return -EINVAL;
761 	return hp->ops->tiocmget(hp);
762 }
763 
hvc_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)764 static int hvc_tiocmset(struct tty_struct *tty,
765 			unsigned int set, unsigned int clear)
766 {
767 	struct hvc_struct *hp = tty->driver_data;
768 
769 	if (!hp || !hp->ops->tiocmset)
770 		return -EINVAL;
771 	return hp->ops->tiocmset(hp, set, clear);
772 }
773 
774 #ifdef CONFIG_CONSOLE_POLL
hvc_poll_init(struct tty_driver * driver,int line,char * options)775 int hvc_poll_init(struct tty_driver *driver, int line, char *options)
776 {
777 	return 0;
778 }
779 
hvc_poll_get_char(struct tty_driver * driver,int line)780 static int hvc_poll_get_char(struct tty_driver *driver, int line)
781 {
782 	struct tty_struct *tty = driver->ttys[0];
783 	struct hvc_struct *hp = tty->driver_data;
784 	int n;
785 	char ch;
786 
787 	n = hp->ops->get_chars(hp->vtermno, &ch, 1);
788 
789 	if (n == 0)
790 		return NO_POLL_CHAR;
791 
792 	return ch;
793 }
794 
hvc_poll_put_char(struct tty_driver * driver,int line,char ch)795 static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
796 {
797 	struct tty_struct *tty = driver->ttys[0];
798 	struct hvc_struct *hp = tty->driver_data;
799 	int n;
800 
801 	do {
802 		n = hp->ops->put_chars(hp->vtermno, &ch, 1);
803 	} while (n <= 0);
804 }
805 #endif
806 
807 static const struct tty_operations hvc_ops = {
808 	.open = hvc_open,
809 	.close = hvc_close,
810 	.write = hvc_write,
811 	.hangup = hvc_hangup,
812 	.unthrottle = hvc_unthrottle,
813 	.write_room = hvc_write_room,
814 	.chars_in_buffer = hvc_chars_in_buffer,
815 	.tiocmget = hvc_tiocmget,
816 	.tiocmset = hvc_tiocmset,
817 #ifdef CONFIG_CONSOLE_POLL
818 	.poll_init = hvc_poll_init,
819 	.poll_get_char = hvc_poll_get_char,
820 	.poll_put_char = hvc_poll_put_char,
821 #endif
822 };
823 
hvc_alloc(uint32_t vtermno,int data,const struct hv_ops * ops,int outbuf_size)824 struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
825 			     const struct hv_ops *ops,
826 			     int outbuf_size)
827 {
828 	struct hvc_struct *hp;
829 	int i;
830 
831 	/* We wait until a driver actually comes along */
832 	if (atomic_inc_not_zero(&hvc_needs_init)) {
833 		int err = hvc_init();
834 		if (err)
835 			return ERR_PTR(err);
836 	}
837 
838 	hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
839 			GFP_KERNEL);
840 	if (!hp)
841 		return ERR_PTR(-ENOMEM);
842 
843 	hp->vtermno = vtermno;
844 	hp->data = data;
845 	hp->ops = ops;
846 	hp->outbuf_size = outbuf_size;
847 	hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
848 
849 	kref_init(&hp->kref);
850 
851 	INIT_WORK(&hp->tty_resize, hvc_set_winsz);
852 	spin_lock_init(&hp->lock);
853 	spin_lock(&hvc_structs_lock);
854 
855 	/*
856 	 * find index to use:
857 	 * see if this vterm id matches one registered for console.
858 	 */
859 	for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
860 		if (vtermnos[i] == hp->vtermno &&
861 		    cons_ops[i] == hp->ops)
862 			break;
863 
864 	/* no matching slot, just use a counter */
865 	if (i >= MAX_NR_HVC_CONSOLES)
866 		i = ++last_hvc;
867 
868 	hp->index = i;
869 
870 	list_add_tail(&(hp->next), &hvc_structs);
871 	spin_unlock(&hvc_structs_lock);
872 
873 	return hp;
874 }
875 EXPORT_SYMBOL_GPL(hvc_alloc);
876 
hvc_remove(struct hvc_struct * hp)877 int hvc_remove(struct hvc_struct *hp)
878 {
879 	unsigned long flags;
880 	struct tty_struct *tty;
881 
882 	spin_lock_irqsave(&hp->lock, flags);
883 	tty = tty_kref_get(hp->tty);
884 
885 	if (hp->index < MAX_NR_HVC_CONSOLES)
886 		vtermnos[hp->index] = -1;
887 
888 	/* Don't whack hp->irq because tty_hangup() will need to free the irq. */
889 
890 	spin_unlock_irqrestore(&hp->lock, flags);
891 
892 	/*
893 	 * We 'put' the instance that was grabbed when the kref instance
894 	 * was initialized using kref_init().  Let the last holder of this
895 	 * kref cause it to be removed, which will probably be the tty_vhangup
896 	 * below.
897 	 */
898 	kref_put(&hp->kref, destroy_hvc_struct);
899 
900 	/*
901 	 * This function call will auto chain call hvc_hangup.
902 	 */
903 	if (tty) {
904 		tty_vhangup(tty);
905 		tty_kref_put(tty);
906 	}
907 	return 0;
908 }
909 EXPORT_SYMBOL_GPL(hvc_remove);
910 
911 /* Driver initialization: called as soon as someone uses hvc_alloc(). */
hvc_init(void)912 static int hvc_init(void)
913 {
914 	struct tty_driver *drv;
915 	int err;
916 
917 	/* We need more than hvc_count adapters due to hotplug additions. */
918 	drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
919 	if (!drv) {
920 		err = -ENOMEM;
921 		goto out;
922 	}
923 
924 	drv->driver_name = "hvc";
925 	drv->name = "hvc";
926 	drv->major = HVC_MAJOR;
927 	drv->minor_start = HVC_MINOR;
928 	drv->type = TTY_DRIVER_TYPE_SYSTEM;
929 	drv->init_termios = tty_std_termios;
930 	drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
931 	tty_set_operations(drv, &hvc_ops);
932 
933 	/* Always start the kthread because there can be hotplug vty adapters
934 	 * added later. */
935 	hvc_task = kthread_run(khvcd, NULL, "khvcd");
936 	if (IS_ERR(hvc_task)) {
937 		printk(KERN_ERR "Couldn't create kthread for console.\n");
938 		err = PTR_ERR(hvc_task);
939 		goto put_tty;
940 	}
941 
942 	err = tty_register_driver(drv);
943 	if (err) {
944 		printk(KERN_ERR "Couldn't register hvc console driver\n");
945 		goto stop_thread;
946 	}
947 
948 	/*
949 	 * Make sure tty is fully registered before allowing it to be
950 	 * found by hvc_console_device.
951 	 */
952 	smp_mb();
953 	hvc_driver = drv;
954 	return 0;
955 
956 stop_thread:
957 	kthread_stop(hvc_task);
958 	hvc_task = NULL;
959 put_tty:
960 	put_tty_driver(drv);
961 out:
962 	return err;
963 }
964 
965 /* This isn't particularly necessary due to this being a console driver
966  * but it is nice to be thorough.
967  */
hvc_exit(void)968 static void __exit hvc_exit(void)
969 {
970 	if (hvc_driver) {
971 		kthread_stop(hvc_task);
972 
973 		tty_unregister_driver(hvc_driver);
974 		/* return tty_struct instances allocated in hvc_init(). */
975 		put_tty_driver(hvc_driver);
976 		unregister_console(&hvc_console);
977 	}
978 }
979 module_exit(hvc_exit);
980