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