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