1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2728674a7SGreg Kroah-Hartman /*
3728674a7SGreg Kroah-Hartman * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
4728674a7SGreg Kroah-Hartman * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
5728674a7SGreg Kroah-Hartman * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
6728674a7SGreg Kroah-Hartman * Copyright (C) 2004 IBM Corporation
7728674a7SGreg Kroah-Hartman *
8728674a7SGreg Kroah-Hartman * Additional Author(s):
9728674a7SGreg Kroah-Hartman * Ryan S. Arnold <rsa@us.ibm.com>
10728674a7SGreg Kroah-Hartman */
11728674a7SGreg Kroah-Hartman
12728674a7SGreg Kroah-Hartman #include <linux/console.h>
13728674a7SGreg Kroah-Hartman #include <linux/cpumask.h>
14728674a7SGreg Kroah-Hartman #include <linux/init.h>
15728674a7SGreg Kroah-Hartman #include <linux/kbd_kern.h>
16728674a7SGreg Kroah-Hartman #include <linux/kernel.h>
17728674a7SGreg Kroah-Hartman #include <linux/kthread.h>
18728674a7SGreg Kroah-Hartman #include <linux/list.h>
19728674a7SGreg Kroah-Hartman #include <linux/major.h>
20f76a1cbeSPaul Gortmaker #include <linux/atomic.h>
21728674a7SGreg Kroah-Hartman #include <linux/sysrq.h>
22728674a7SGreg Kroah-Hartman #include <linux/tty.h>
23728674a7SGreg Kroah-Hartman #include <linux/tty_flip.h>
24728674a7SGreg Kroah-Hartman #include <linux/sched.h>
25728674a7SGreg Kroah-Hartman #include <linux/spinlock.h>
26728674a7SGreg Kroah-Hartman #include <linux/delay.h>
27728674a7SGreg Kroah-Hartman #include <linux/freezer.h>
28728674a7SGreg Kroah-Hartman #include <linux/slab.h>
29762e77aeSAnton Blanchard #include <linux/serial_core.h>
30728674a7SGreg Kroah-Hartman
317c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
32728674a7SGreg Kroah-Hartman
33728674a7SGreg Kroah-Hartman #include "hvc_console.h"
34728674a7SGreg Kroah-Hartman
35728674a7SGreg Kroah-Hartman #define HVC_MAJOR 229
36728674a7SGreg Kroah-Hartman #define HVC_MINOR 0
37728674a7SGreg Kroah-Hartman
38728674a7SGreg Kroah-Hartman /*
39728674a7SGreg Kroah-Hartman * Wait this long per iteration while trying to push buffered data to the
40728674a7SGreg Kroah-Hartman * hypervisor before allowing the tty to complete a close operation.
41728674a7SGreg Kroah-Hartman */
42728674a7SGreg Kroah-Hartman #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
43728674a7SGreg Kroah-Hartman
4460f41e84SGreg Kroah-Hartman /*
4560f41e84SGreg Kroah-Hartman * These sizes are most efficient for vio, because they are the
4660f41e84SGreg Kroah-Hartman * native transfer size. We could make them selectable in the
4760f41e84SGreg Kroah-Hartman * future to better deal with backends that want other buffer sizes.
4860f41e84SGreg Kroah-Hartman */
4960f41e84SGreg Kroah-Hartman #define N_OUTBUF 16
5060f41e84SGreg Kroah-Hartman #define N_INBUF 16
5160f41e84SGreg Kroah-Hartman
5260f41e84SGreg Kroah-Hartman #define __ALIGNED__ __attribute__((__aligned__(L1_CACHE_BYTES)))
5360f41e84SGreg Kroah-Hartman
54728674a7SGreg Kroah-Hartman static struct tty_driver *hvc_driver;
55728674a7SGreg Kroah-Hartman static struct task_struct *hvc_task;
56728674a7SGreg Kroah-Hartman
57728674a7SGreg Kroah-Hartman /* Picks up late kicks after list walk but before schedule() */
58728674a7SGreg Kroah-Hartman static int hvc_kicked;
59728674a7SGreg Kroah-Hartman
60f76a1cbeSPaul Gortmaker /* hvc_init is triggered from hvc_alloc, i.e. only when actually used */
61f76a1cbeSPaul Gortmaker static atomic_t hvc_needs_init __read_mostly = ATOMIC_INIT(-1);
62f76a1cbeSPaul Gortmaker
63728674a7SGreg Kroah-Hartman static int hvc_init(void);
64728674a7SGreg Kroah-Hartman
65728674a7SGreg Kroah-Hartman #ifdef CONFIG_MAGIC_SYSRQ
66728674a7SGreg Kroah-Hartman static int sysrq_pressed;
67728674a7SGreg Kroah-Hartman #endif
68728674a7SGreg Kroah-Hartman
69728674a7SGreg Kroah-Hartman /* dynamic list of hvc_struct instances */
70728674a7SGreg Kroah-Hartman static LIST_HEAD(hvc_structs);
71728674a7SGreg Kroah-Hartman
72728674a7SGreg Kroah-Hartman /*
73728674a7SGreg Kroah-Hartman * Protect the list of hvc_struct instances from inserts and removals during
74728674a7SGreg Kroah-Hartman * list traversal.
75728674a7SGreg Kroah-Hartman */
76a9bf5c8aSNicholas Piggin static DEFINE_MUTEX(hvc_structs_mutex);
77728674a7SGreg Kroah-Hartman
78728674a7SGreg Kroah-Hartman /*
79728674a7SGreg Kroah-Hartman * This value is used to assign a tty->index value to a hvc_struct based
80728674a7SGreg Kroah-Hartman * upon order of exposure via hvc_probe(), when we can not match it to
81728674a7SGreg Kroah-Hartman * a console candidate registered with hvc_instantiate().
82728674a7SGreg Kroah-Hartman */
83728674a7SGreg Kroah-Hartman static int last_hvc = -1;
84728674a7SGreg Kroah-Hartman
85728674a7SGreg Kroah-Hartman /*
86a9bf5c8aSNicholas Piggin * Do not call this function with either the hvc_structs_mutex or the hvc_struct
87728674a7SGreg Kroah-Hartman * lock held. If successful, this function increments the kref reference
88728674a7SGreg Kroah-Hartman * count against the target hvc_struct so it should be released when finished.
89728674a7SGreg Kroah-Hartman */
hvc_get_by_index(int index)90728674a7SGreg Kroah-Hartman static struct hvc_struct *hvc_get_by_index(int index)
91728674a7SGreg Kroah-Hartman {
92728674a7SGreg Kroah-Hartman struct hvc_struct *hp;
93728674a7SGreg Kroah-Hartman unsigned long flags;
94728674a7SGreg Kroah-Hartman
95a9bf5c8aSNicholas Piggin mutex_lock(&hvc_structs_mutex);
96728674a7SGreg Kroah-Hartman
97728674a7SGreg Kroah-Hartman list_for_each_entry(hp, &hvc_structs, next) {
98728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags);
99728674a7SGreg Kroah-Hartman if (hp->index == index) {
100f3d9f250SJiri Slaby tty_port_get(&hp->port);
101728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
102a9bf5c8aSNicholas Piggin mutex_unlock(&hvc_structs_mutex);
103728674a7SGreg Kroah-Hartman return hp;
104728674a7SGreg Kroah-Hartman }
105728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
106728674a7SGreg Kroah-Hartman }
107728674a7SGreg Kroah-Hartman hp = NULL;
108a9bf5c8aSNicholas Piggin mutex_unlock(&hvc_structs_mutex);
109728674a7SGreg Kroah-Hartman
110728674a7SGreg Kroah-Hartman return hp;
111728674a7SGreg Kroah-Hartman }
112728674a7SGreg Kroah-Hartman
__hvc_flush(const struct hv_ops * ops,uint32_t vtermno,bool wait)1139f65b81fSNicholas Piggin static int __hvc_flush(const struct hv_ops *ops, uint32_t vtermno, bool wait)
1149f65b81fSNicholas Piggin {
1159f65b81fSNicholas Piggin if (wait)
1169f65b81fSNicholas Piggin might_sleep();
1179f65b81fSNicholas Piggin
1189f65b81fSNicholas Piggin if (ops->flush)
1199f65b81fSNicholas Piggin return ops->flush(vtermno, wait);
1209f65b81fSNicholas Piggin return 0;
1219f65b81fSNicholas Piggin }
1229f65b81fSNicholas Piggin
hvc_console_flush(const struct hv_ops * ops,uint32_t vtermno)1239f65b81fSNicholas Piggin static int hvc_console_flush(const struct hv_ops *ops, uint32_t vtermno)
1249f65b81fSNicholas Piggin {
1259f65b81fSNicholas Piggin return __hvc_flush(ops, vtermno, false);
1269f65b81fSNicholas Piggin }
1279f65b81fSNicholas Piggin
1289f65b81fSNicholas Piggin /*
1299f65b81fSNicholas Piggin * Wait for the console to flush before writing more to it. This sleeps.
1309f65b81fSNicholas Piggin */
hvc_flush(struct hvc_struct * hp)1319f65b81fSNicholas Piggin static int hvc_flush(struct hvc_struct *hp)
1329f65b81fSNicholas Piggin {
1339f65b81fSNicholas Piggin return __hvc_flush(hp->ops, hp->vtermno, true);
1349f65b81fSNicholas Piggin }
1359f65b81fSNicholas Piggin
136728674a7SGreg Kroah-Hartman /*
137728674a7SGreg Kroah-Hartman * Initial console vtermnos for console API usage prior to full console
138728674a7SGreg Kroah-Hartman * initialization. Any vty adapter outside this range will not have usable
139728674a7SGreg Kroah-Hartman * console interfaces but can still be used as a tty device. This has to be
140728674a7SGreg Kroah-Hartman * static because kmalloc will not work during early console init.
141728674a7SGreg Kroah-Hartman */
142728674a7SGreg Kroah-Hartman static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
143728674a7SGreg Kroah-Hartman static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
144728674a7SGreg Kroah-Hartman {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
145728674a7SGreg Kroah-Hartman
146728674a7SGreg Kroah-Hartman /*
147728674a7SGreg Kroah-Hartman * Console APIs, NOT TTY. These APIs are available immediately when
148728674a7SGreg Kroah-Hartman * hvc_console_setup() finds adapters.
149728674a7SGreg Kroah-Hartman */
150728674a7SGreg Kroah-Hartman
hvc_console_print(struct console * co,const char * b,unsigned count)151728674a7SGreg Kroah-Hartman static void hvc_console_print(struct console *co, const char *b,
152728674a7SGreg Kroah-Hartman unsigned count)
153728674a7SGreg Kroah-Hartman {
15460f41e84SGreg Kroah-Hartman char c[N_OUTBUF] __ALIGNED__;
155728674a7SGreg Kroah-Hartman unsigned i = 0, n = 0;
156728674a7SGreg Kroah-Hartman int r, donecr = 0, index = co->index;
157728674a7SGreg Kroah-Hartman
158728674a7SGreg Kroah-Hartman /* Console access attempt outside of acceptable console range. */
159728674a7SGreg Kroah-Hartman if (index >= MAX_NR_HVC_CONSOLES)
160728674a7SGreg Kroah-Hartman return;
161728674a7SGreg Kroah-Hartman
162728674a7SGreg Kroah-Hartman /* This console adapter was removed so it is not usable. */
163728674a7SGreg Kroah-Hartman if (vtermnos[index] == -1)
164728674a7SGreg Kroah-Hartman return;
165728674a7SGreg Kroah-Hartman
166728674a7SGreg Kroah-Hartman while (count > 0 || i > 0) {
167728674a7SGreg Kroah-Hartman if (count > 0 && i < sizeof(c)) {
168728674a7SGreg Kroah-Hartman if (b[n] == '\n' && !donecr) {
169728674a7SGreg Kroah-Hartman c[i++] = '\r';
170728674a7SGreg Kroah-Hartman donecr = 1;
171728674a7SGreg Kroah-Hartman } else {
172728674a7SGreg Kroah-Hartman c[i++] = b[n++];
173728674a7SGreg Kroah-Hartman donecr = 0;
174728674a7SGreg Kroah-Hartman --count;
175728674a7SGreg Kroah-Hartman }
176728674a7SGreg Kroah-Hartman } else {
177728674a7SGreg Kroah-Hartman r = cons_ops[index]->put_chars(vtermnos[index], c, i);
178728674a7SGreg Kroah-Hartman if (r <= 0) {
1798c2381afSHendrik Brueckner /* throw away characters on error
1808c2381afSHendrik Brueckner * but spin in case of -EAGAIN */
1819f65b81fSNicholas Piggin if (r != -EAGAIN) {
182728674a7SGreg Kroah-Hartman i = 0;
1839f65b81fSNicholas Piggin } else {
1849f65b81fSNicholas Piggin hvc_console_flush(cons_ops[index],
1859f65b81fSNicholas Piggin vtermnos[index]);
1869f65b81fSNicholas Piggin }
187728674a7SGreg Kroah-Hartman } else if (r > 0) {
188728674a7SGreg Kroah-Hartman i -= r;
189728674a7SGreg Kroah-Hartman if (i > 0)
190728674a7SGreg Kroah-Hartman memmove(c, c+r, i);
191728674a7SGreg Kroah-Hartman }
192728674a7SGreg Kroah-Hartman }
193728674a7SGreg Kroah-Hartman }
1949f65b81fSNicholas Piggin hvc_console_flush(cons_ops[index], vtermnos[index]);
195728674a7SGreg Kroah-Hartman }
196728674a7SGreg Kroah-Hartman
hvc_console_device(struct console * c,int * index)197728674a7SGreg Kroah-Hartman static struct tty_driver *hvc_console_device(struct console *c, int *index)
198728674a7SGreg Kroah-Hartman {
199728674a7SGreg Kroah-Hartman if (vtermnos[c->index] == -1)
200728674a7SGreg Kroah-Hartman return NULL;
201728674a7SGreg Kroah-Hartman
202728674a7SGreg Kroah-Hartman *index = c->index;
203728674a7SGreg Kroah-Hartman return hvc_driver;
204728674a7SGreg Kroah-Hartman }
205728674a7SGreg Kroah-Hartman
hvc_console_setup(struct console * co,char * options)206501fed45STomoki Sekiyama static int hvc_console_setup(struct console *co, char *options)
207728674a7SGreg Kroah-Hartman {
208728674a7SGreg Kroah-Hartman if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
209728674a7SGreg Kroah-Hartman return -ENODEV;
210728674a7SGreg Kroah-Hartman
211728674a7SGreg Kroah-Hartman if (vtermnos[co->index] == -1)
212728674a7SGreg Kroah-Hartman return -ENODEV;
213728674a7SGreg Kroah-Hartman
214728674a7SGreg Kroah-Hartman return 0;
215728674a7SGreg Kroah-Hartman }
216728674a7SGreg Kroah-Hartman
217728674a7SGreg Kroah-Hartman static struct console hvc_console = {
218728674a7SGreg Kroah-Hartman .name = "hvc",
219728674a7SGreg Kroah-Hartman .write = hvc_console_print,
220728674a7SGreg Kroah-Hartman .device = hvc_console_device,
221728674a7SGreg Kroah-Hartman .setup = hvc_console_setup,
222728674a7SGreg Kroah-Hartman .flags = CON_PRINTBUFFER,
223728674a7SGreg Kroah-Hartman .index = -1,
224728674a7SGreg Kroah-Hartman };
225728674a7SGreg Kroah-Hartman
226728674a7SGreg Kroah-Hartman /*
227728674a7SGreg Kroah-Hartman * Early console initialization. Precedes driver initialization.
228728674a7SGreg Kroah-Hartman *
229728674a7SGreg Kroah-Hartman * (1) we are first, and the user specified another driver
230728674a7SGreg Kroah-Hartman * -- index will remain -1
231728674a7SGreg Kroah-Hartman * (2) we are first and the user specified no driver
232728674a7SGreg Kroah-Hartman * -- index will be set to 0, then we will fail setup.
233728674a7SGreg Kroah-Hartman * (3) we are first and the user specified our driver
234728674a7SGreg Kroah-Hartman * -- index will be set to user specified driver, and we will fail
235728674a7SGreg Kroah-Hartman * (4) we are after driver, and this initcall will register us
236728674a7SGreg Kroah-Hartman * -- if the user didn't specify a driver then the console will match
237728674a7SGreg Kroah-Hartman *
238728674a7SGreg Kroah-Hartman * Note that for cases 2 and 3, we will match later when the io driver
239728674a7SGreg Kroah-Hartman * calls hvc_instantiate() and call register again.
240728674a7SGreg Kroah-Hartman */
hvc_console_init(void)241728674a7SGreg Kroah-Hartman static int __init hvc_console_init(void)
242728674a7SGreg Kroah-Hartman {
243728674a7SGreg Kroah-Hartman register_console(&hvc_console);
244728674a7SGreg Kroah-Hartman return 0;
245728674a7SGreg Kroah-Hartman }
246728674a7SGreg Kroah-Hartman console_initcall(hvc_console_init);
247728674a7SGreg Kroah-Hartman
248728674a7SGreg Kroah-Hartman /* callback when the kboject ref count reaches zero. */
hvc_port_destruct(struct tty_port * port)249f3d9f250SJiri Slaby static void hvc_port_destruct(struct tty_port *port)
250728674a7SGreg Kroah-Hartman {
251f3d9f250SJiri Slaby struct hvc_struct *hp = container_of(port, struct hvc_struct, port);
252728674a7SGreg Kroah-Hartman unsigned long flags;
253728674a7SGreg Kroah-Hartman
254a9bf5c8aSNicholas Piggin mutex_lock(&hvc_structs_mutex);
255728674a7SGreg Kroah-Hartman
256728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags);
257728674a7SGreg Kroah-Hartman list_del(&(hp->next));
258728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
259728674a7SGreg Kroah-Hartman
260a9bf5c8aSNicholas Piggin mutex_unlock(&hvc_structs_mutex);
261728674a7SGreg Kroah-Hartman
262728674a7SGreg Kroah-Hartman kfree(hp);
263728674a7SGreg Kroah-Hartman }
264728674a7SGreg Kroah-Hartman
hvc_check_console(int index)26592057a49SBenjamin Herrenschmidt static void hvc_check_console(int index)
26692057a49SBenjamin Herrenschmidt {
26734d9541eSJohn Ogness /* Already registered, bail out */
26834d9541eSJohn Ogness if (console_is_registered(&hvc_console))
26992057a49SBenjamin Herrenschmidt return;
27092057a49SBenjamin Herrenschmidt
27192057a49SBenjamin Herrenschmidt /* If this index is what the user requested, then register
27292057a49SBenjamin Herrenschmidt * now (setup won't fail at this point). It's ok to just
27392057a49SBenjamin Herrenschmidt * call register again if previously .setup failed.
27492057a49SBenjamin Herrenschmidt */
27592057a49SBenjamin Herrenschmidt if (index == hvc_console.index)
27692057a49SBenjamin Herrenschmidt register_console(&hvc_console);
27792057a49SBenjamin Herrenschmidt }
27892057a49SBenjamin Herrenschmidt
279728674a7SGreg Kroah-Hartman /*
280728674a7SGreg Kroah-Hartman * hvc_instantiate() is an early console discovery method which locates
281728674a7SGreg Kroah-Hartman * consoles * prior to the vio subsystem discovering them. Hotplugged
282728674a7SGreg Kroah-Hartman * vty adapters do NOT get an hvc_instantiate() callback since they
283728674a7SGreg Kroah-Hartman * appear after early console init.
284728674a7SGreg Kroah-Hartman */
hvc_instantiate(uint32_t vtermno,int index,const struct hv_ops * ops)285728674a7SGreg Kroah-Hartman int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
286728674a7SGreg Kroah-Hartman {
287728674a7SGreg Kroah-Hartman struct hvc_struct *hp;
288728674a7SGreg Kroah-Hartman
289728674a7SGreg Kroah-Hartman if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
290728674a7SGreg Kroah-Hartman return -1;
291728674a7SGreg Kroah-Hartman
292728674a7SGreg Kroah-Hartman if (vtermnos[index] != -1)
293728674a7SGreg Kroah-Hartman return -1;
294728674a7SGreg Kroah-Hartman
2952ac62268SXiaofei Tan /* make sure no tty has been registered in this index */
296728674a7SGreg Kroah-Hartman hp = hvc_get_by_index(index);
297728674a7SGreg Kroah-Hartman if (hp) {
298f3d9f250SJiri Slaby tty_port_put(&hp->port);
299728674a7SGreg Kroah-Hartman return -1;
300728674a7SGreg Kroah-Hartman }
301728674a7SGreg Kroah-Hartman
302728674a7SGreg Kroah-Hartman vtermnos[index] = vtermno;
303728674a7SGreg Kroah-Hartman cons_ops[index] = ops;
304728674a7SGreg Kroah-Hartman
30592057a49SBenjamin Herrenschmidt /* check if we need to re-register the kernel console */
30692057a49SBenjamin Herrenschmidt hvc_check_console(index);
307728674a7SGreg Kroah-Hartman
308728674a7SGreg Kroah-Hartman return 0;
309728674a7SGreg Kroah-Hartman }
310728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_instantiate);
311728674a7SGreg Kroah-Hartman
312728674a7SGreg Kroah-Hartman /* Wake the sleeping khvcd */
hvc_kick(void)313728674a7SGreg Kroah-Hartman void hvc_kick(void)
314728674a7SGreg Kroah-Hartman {
315728674a7SGreg Kroah-Hartman hvc_kicked = 1;
316728674a7SGreg Kroah-Hartman wake_up_process(hvc_task);
317728674a7SGreg Kroah-Hartman }
318728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_kick);
319728674a7SGreg Kroah-Hartman
hvc_unthrottle(struct tty_struct * tty)320728674a7SGreg Kroah-Hartman static void hvc_unthrottle(struct tty_struct *tty)
321728674a7SGreg Kroah-Hartman {
322728674a7SGreg Kroah-Hartman hvc_kick();
323728674a7SGreg Kroah-Hartman }
324728674a7SGreg Kroah-Hartman
hvc_install(struct tty_driver * driver,struct tty_struct * tty)325bdb498c2SJiri Slaby static int hvc_install(struct tty_driver *driver, struct tty_struct *tty)
326bdb498c2SJiri Slaby {
327bdb498c2SJiri Slaby struct hvc_struct *hp;
328bdb498c2SJiri Slaby int rc;
329bdb498c2SJiri Slaby
330bdb498c2SJiri Slaby /* Auto increments kref reference if found. */
331b8dccc1bSGreg Kroah-Hartman hp = hvc_get_by_index(tty->index);
332b8dccc1bSGreg Kroah-Hartman if (!hp)
333bdb498c2SJiri Slaby return -ENODEV;
334bdb498c2SJiri Slaby
335bdb498c2SJiri Slaby tty->driver_data = hp;
336bdb498c2SJiri Slaby
337bdb498c2SJiri Slaby rc = tty_port_install(&hp->port, driver, tty);
338bdb498c2SJiri Slaby if (rc)
339bdb498c2SJiri Slaby tty_port_put(&hp->port);
340bdb498c2SJiri Slaby return rc;
341bdb498c2SJiri Slaby }
342bdb498c2SJiri Slaby
343728674a7SGreg Kroah-Hartman /*
344728674a7SGreg Kroah-Hartman * The TTY interface won't be used until after the vio layer has exposed the vty
345728674a7SGreg Kroah-Hartman * adapter to the kernel.
346728674a7SGreg Kroah-Hartman */
hvc_open(struct tty_struct * tty,struct file * filp)347728674a7SGreg Kroah-Hartman static int hvc_open(struct tty_struct *tty, struct file * filp)
348728674a7SGreg Kroah-Hartman {
349cf9c9445SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data;
350728674a7SGreg Kroah-Hartman unsigned long flags;
351728674a7SGreg Kroah-Hartman int rc = 0;
352728674a7SGreg Kroah-Hartman
3530146b693SJiri Slaby spin_lock_irqsave(&hp->port.lock, flags);
354728674a7SGreg Kroah-Hartman /* Check and then increment for fast path open. */
3550146b693SJiri Slaby if (hp->port.count++ > 0) {
3560146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags);
357728674a7SGreg Kroah-Hartman hvc_kick();
358cf9c9445SGreg Kroah-Hartman return 0;
359728674a7SGreg Kroah-Hartman } /* else count == 0 */
3600146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags);
361728674a7SGreg Kroah-Hartman
36285bbc003SJiri Slaby tty_port_tty_set(&hp->port, tty);
36385bbc003SJiri Slaby
364728674a7SGreg Kroah-Hartman if (hp->ops->notifier_add)
365728674a7SGreg Kroah-Hartman rc = hp->ops->notifier_add(hp, hp->data);
366728674a7SGreg Kroah-Hartman
367728674a7SGreg Kroah-Hartman /*
368728674a7SGreg Kroah-Hartman * If the notifier fails we return an error. The tty layer
369728674a7SGreg Kroah-Hartman * will call hvc_close() after a failed open but we don't want to clean
370728674a7SGreg Kroah-Hartman * up there so we'll clean up here and clear out the previously set
371728674a7SGreg Kroah-Hartman * tty fields and return the kref reference.
372728674a7SGreg Kroah-Hartman */
373728674a7SGreg Kroah-Hartman if (rc) {
374728674a7SGreg Kroah-Hartman printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
37524eb2377SJiri Slaby } else {
37633e745a1SHendrik Brueckner /* We are ready... raise DTR/RTS */
37733e745a1SHendrik Brueckner if (C_BAUD(tty))
37833e745a1SHendrik Brueckner if (hp->ops->dtr_rts)
3795d420399SIlpo Järvinen hp->ops->dtr_rts(hp, true);
38024eb2377SJiri Slaby tty_port_set_initialized(&hp->port, true);
38124eb2377SJiri Slaby }
38233e745a1SHendrik Brueckner
383728674a7SGreg Kroah-Hartman /* Force wakeup of the polling thread */
384728674a7SGreg Kroah-Hartman hvc_kick();
385728674a7SGreg Kroah-Hartman
386728674a7SGreg Kroah-Hartman return rc;
387728674a7SGreg Kroah-Hartman }
388728674a7SGreg Kroah-Hartman
hvc_close(struct tty_struct * tty,struct file * filp)389728674a7SGreg Kroah-Hartman static void hvc_close(struct tty_struct *tty, struct file * filp)
390728674a7SGreg Kroah-Hartman {
39124eb2377SJiri Slaby struct hvc_struct *hp = tty->driver_data;
392728674a7SGreg Kroah-Hartman unsigned long flags;
393728674a7SGreg Kroah-Hartman
394728674a7SGreg Kroah-Hartman if (tty_hung_up_p(filp))
395728674a7SGreg Kroah-Hartman return;
396728674a7SGreg Kroah-Hartman
3970146b693SJiri Slaby spin_lock_irqsave(&hp->port.lock, flags);
398728674a7SGreg Kroah-Hartman
3990146b693SJiri Slaby if (--hp->port.count == 0) {
4000146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags);
40185bbc003SJiri Slaby /* We are done with the tty pointer now. */
40285bbc003SJiri Slaby tty_port_tty_set(&hp->port, NULL);
403728674a7SGreg Kroah-Hartman
40424eb2377SJiri Slaby if (!tty_port_initialized(&hp->port))
40524eb2377SJiri Slaby return;
40624eb2377SJiri Slaby
40733e745a1SHendrik Brueckner if (C_HUPCL(tty))
40833e745a1SHendrik Brueckner if (hp->ops->dtr_rts)
4095d420399SIlpo Järvinen hp->ops->dtr_rts(hp, false);
41033e745a1SHendrik Brueckner
411728674a7SGreg Kroah-Hartman if (hp->ops->notifier_del)
412728674a7SGreg Kroah-Hartman hp->ops->notifier_del(hp, hp->data);
413728674a7SGreg Kroah-Hartman
414728674a7SGreg Kroah-Hartman /* cancel pending tty resize work */
415728674a7SGreg Kroah-Hartman cancel_work_sync(&hp->tty_resize);
416728674a7SGreg Kroah-Hartman
417728674a7SGreg Kroah-Hartman /*
418728674a7SGreg Kroah-Hartman * Chain calls chars_in_buffer() and returns immediately if
419728674a7SGreg Kroah-Hartman * there is no buffered data otherwise sleeps on a wait queue
420728674a7SGreg Kroah-Hartman * waking periodically to check chars_in_buffer().
421728674a7SGreg Kroah-Hartman */
42279c1faa4SPeter Hurley tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
42324eb2377SJiri Slaby tty_port_set_initialized(&hp->port, false);
424728674a7SGreg Kroah-Hartman } else {
4250146b693SJiri Slaby if (hp->port.count < 0)
426728674a7SGreg Kroah-Hartman printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
4270146b693SJiri Slaby hp->vtermno, hp->port.count);
4280146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags);
429728674a7SGreg Kroah-Hartman }
430bdb498c2SJiri Slaby }
431bdb498c2SJiri Slaby
hvc_cleanup(struct tty_struct * tty)432bdb498c2SJiri Slaby static void hvc_cleanup(struct tty_struct *tty)
433bdb498c2SJiri Slaby {
434bdb498c2SJiri Slaby struct hvc_struct *hp = tty->driver_data;
435728674a7SGreg Kroah-Hartman
436f3d9f250SJiri Slaby tty_port_put(&hp->port);
437728674a7SGreg Kroah-Hartman }
438728674a7SGreg Kroah-Hartman
hvc_hangup(struct tty_struct * tty)439728674a7SGreg Kroah-Hartman static void hvc_hangup(struct tty_struct *tty)
440728674a7SGreg Kroah-Hartman {
441728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data;
442728674a7SGreg Kroah-Hartman unsigned long flags;
443728674a7SGreg Kroah-Hartman
444728674a7SGreg Kroah-Hartman if (!hp)
445728674a7SGreg Kroah-Hartman return;
446728674a7SGreg Kroah-Hartman
447728674a7SGreg Kroah-Hartman /* cancel pending tty resize work */
448728674a7SGreg Kroah-Hartman cancel_work_sync(&hp->tty_resize);
449728674a7SGreg Kroah-Hartman
4500146b693SJiri Slaby spin_lock_irqsave(&hp->port.lock, flags);
451728674a7SGreg Kroah-Hartman
452728674a7SGreg Kroah-Hartman /*
453728674a7SGreg Kroah-Hartman * The N_TTY line discipline has problems such that in a close vs
454728674a7SGreg Kroah-Hartman * open->hangup case this can be called after the final close so prevent
455728674a7SGreg Kroah-Hartman * that from happening for now.
456728674a7SGreg Kroah-Hartman */
4570146b693SJiri Slaby if (hp->port.count <= 0) {
4580146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags);
459728674a7SGreg Kroah-Hartman return;
460728674a7SGreg Kroah-Hartman }
461728674a7SGreg Kroah-Hartman
4620146b693SJiri Slaby hp->port.count = 0;
4630146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags);
46485bbc003SJiri Slaby tty_port_tty_set(&hp->port, NULL);
465728674a7SGreg Kroah-Hartman
4660146b693SJiri Slaby hp->n_outbuf = 0;
4670146b693SJiri Slaby
468728674a7SGreg Kroah-Hartman if (hp->ops->notifier_hangup)
469728674a7SGreg Kroah-Hartman hp->ops->notifier_hangup(hp, hp->data);
470728674a7SGreg Kroah-Hartman }
471728674a7SGreg Kroah-Hartman
472728674a7SGreg Kroah-Hartman /*
473728674a7SGreg Kroah-Hartman * Push buffered characters whether they were just recently buffered or waiting
474728674a7SGreg Kroah-Hartman * on a blocked hypervisor. Call this function with hp->lock held.
475728674a7SGreg Kroah-Hartman */
hvc_push(struct hvc_struct * hp)476728674a7SGreg Kroah-Hartman static int hvc_push(struct hvc_struct *hp)
477728674a7SGreg Kroah-Hartman {
478728674a7SGreg Kroah-Hartman int n;
479728674a7SGreg Kroah-Hartman
480728674a7SGreg Kroah-Hartman n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
481728674a7SGreg Kroah-Hartman if (n <= 0) {
4828c2381afSHendrik Brueckner if (n == 0 || n == -EAGAIN) {
483728674a7SGreg Kroah-Hartman hp->do_wakeup = 1;
484728674a7SGreg Kroah-Hartman return 0;
485728674a7SGreg Kroah-Hartman }
486728674a7SGreg Kroah-Hartman /* throw away output on error; this happens when
487728674a7SGreg Kroah-Hartman there is no session connected to the vterm. */
488728674a7SGreg Kroah-Hartman hp->n_outbuf = 0;
489728674a7SGreg Kroah-Hartman } else
490728674a7SGreg Kroah-Hartman hp->n_outbuf -= n;
491728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0)
492728674a7SGreg Kroah-Hartman memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
493728674a7SGreg Kroah-Hartman else
494728674a7SGreg Kroah-Hartman hp->do_wakeup = 1;
495728674a7SGreg Kroah-Hartman
496728674a7SGreg Kroah-Hartman return n;
497728674a7SGreg Kroah-Hartman }
498728674a7SGreg Kroah-Hartman
hvc_write(struct tty_struct * tty,const u8 * buf,size_t count)49995713967SJiri Slaby (SUSE) static ssize_t hvc_write(struct tty_struct *tty, const u8 *buf, size_t count)
500728674a7SGreg Kroah-Hartman {
501728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data;
502728674a7SGreg Kroah-Hartman unsigned long flags;
503*8428e522SJiri Slaby (SUSE) size_t rsize, written = 0;
504728674a7SGreg Kroah-Hartman
505728674a7SGreg Kroah-Hartman /* This write was probably executed during a tty close. */
506728674a7SGreg Kroah-Hartman if (!hp)
507728674a7SGreg Kroah-Hartman return -EPIPE;
508728674a7SGreg Kroah-Hartman
5090146b693SJiri Slaby /* FIXME what's this (unprotected) check for? */
5100146b693SJiri Slaby if (hp->port.count <= 0)
511728674a7SGreg Kroah-Hartman return -EIO;
512728674a7SGreg Kroah-Hartman
513550ddadcSNicholas Piggin while (count > 0) {
5147f2bf784SNicholas Piggin int ret = 0;
5157f2bf784SNicholas Piggin
516728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags);
517728674a7SGreg Kroah-Hartman
518550ddadcSNicholas Piggin rsize = hp->outbuf_size - hp->n_outbuf;
519728674a7SGreg Kroah-Hartman
520550ddadcSNicholas Piggin if (rsize) {
521728674a7SGreg Kroah-Hartman if (rsize > count)
522728674a7SGreg Kroah-Hartman rsize = count;
523728674a7SGreg Kroah-Hartman memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
524728674a7SGreg Kroah-Hartman count -= rsize;
525728674a7SGreg Kroah-Hartman buf += rsize;
526728674a7SGreg Kroah-Hartman hp->n_outbuf += rsize;
527728674a7SGreg Kroah-Hartman written += rsize;
528728674a7SGreg Kroah-Hartman }
529550ddadcSNicholas Piggin
530550ddadcSNicholas Piggin if (hp->n_outbuf > 0)
5317f2bf784SNicholas Piggin ret = hvc_push(hp);
532550ddadcSNicholas Piggin
533728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
534728674a7SGreg Kroah-Hartman
5357f2bf784SNicholas Piggin if (!ret)
5367f2bf784SNicholas Piggin break;
5377f2bf784SNicholas Piggin
5389f65b81fSNicholas Piggin if (count) {
5399f65b81fSNicholas Piggin if (hp->n_outbuf > 0)
5409f65b81fSNicholas Piggin hvc_flush(hp);
541550ddadcSNicholas Piggin cond_resched();
542550ddadcSNicholas Piggin }
5439f65b81fSNicholas Piggin }
544550ddadcSNicholas Piggin
545728674a7SGreg Kroah-Hartman /*
546728674a7SGreg Kroah-Hartman * Racy, but harmless, kick thread if there is still pending data.
547728674a7SGreg Kroah-Hartman */
548728674a7SGreg Kroah-Hartman if (hp->n_outbuf)
549728674a7SGreg Kroah-Hartman hvc_kick();
550728674a7SGreg Kroah-Hartman
551728674a7SGreg Kroah-Hartman return written;
552728674a7SGreg Kroah-Hartman }
553728674a7SGreg Kroah-Hartman
554728674a7SGreg Kroah-Hartman /**
555728674a7SGreg Kroah-Hartman * hvc_set_winsz() - Resize the hvc tty terminal window.
556728674a7SGreg Kroah-Hartman * @work: work structure.
557728674a7SGreg Kroah-Hartman *
558728674a7SGreg Kroah-Hartman * The routine shall not be called within an atomic context because it
559728674a7SGreg Kroah-Hartman * might sleep.
560728674a7SGreg Kroah-Hartman *
561728674a7SGreg Kroah-Hartman * Locking: hp->lock
562728674a7SGreg Kroah-Hartman */
hvc_set_winsz(struct work_struct * work)563728674a7SGreg Kroah-Hartman static void hvc_set_winsz(struct work_struct *work)
564728674a7SGreg Kroah-Hartman {
565728674a7SGreg Kroah-Hartman struct hvc_struct *hp;
566728674a7SGreg Kroah-Hartman unsigned long hvc_flags;
567728674a7SGreg Kroah-Hartman struct tty_struct *tty;
568728674a7SGreg Kroah-Hartman struct winsize ws;
569728674a7SGreg Kroah-Hartman
570728674a7SGreg Kroah-Hartman hp = container_of(work, struct hvc_struct, tty_resize);
571728674a7SGreg Kroah-Hartman
57285bbc003SJiri Slaby tty = tty_port_tty_get(&hp->port);
57385bbc003SJiri Slaby if (!tty)
574728674a7SGreg Kroah-Hartman return;
57585bbc003SJiri Slaby
57685bbc003SJiri Slaby spin_lock_irqsave(&hp->lock, hvc_flags);
577728674a7SGreg Kroah-Hartman ws = hp->ws;
578728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, hvc_flags);
579728674a7SGreg Kroah-Hartman
580728674a7SGreg Kroah-Hartman tty_do_resize(tty, &ws);
581728674a7SGreg Kroah-Hartman tty_kref_put(tty);
582728674a7SGreg Kroah-Hartman }
583728674a7SGreg Kroah-Hartman
584728674a7SGreg Kroah-Hartman /*
585728674a7SGreg Kroah-Hartman * This is actually a contract between the driver and the tty layer outlining
586728674a7SGreg Kroah-Hartman * how much write room the driver can guarantee will be sent OR BUFFERED. This
587728674a7SGreg Kroah-Hartman * driver MUST honor the return value.
588728674a7SGreg Kroah-Hartman */
hvc_write_room(struct tty_struct * tty)58903b3b1a2SJiri Slaby static unsigned int hvc_write_room(struct tty_struct *tty)
590728674a7SGreg Kroah-Hartman {
591728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data;
592728674a7SGreg Kroah-Hartman
593728674a7SGreg Kroah-Hartman if (!hp)
594d83114e9SAlan Cox return 0;
595728674a7SGreg Kroah-Hartman
596728674a7SGreg Kroah-Hartman return hp->outbuf_size - hp->n_outbuf;
597728674a7SGreg Kroah-Hartman }
598728674a7SGreg Kroah-Hartman
hvc_chars_in_buffer(struct tty_struct * tty)599fff4ef17SJiri Slaby static unsigned int hvc_chars_in_buffer(struct tty_struct *tty)
600728674a7SGreg Kroah-Hartman {
601728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data;
602728674a7SGreg Kroah-Hartman
603728674a7SGreg Kroah-Hartman if (!hp)
604728674a7SGreg Kroah-Hartman return 0;
605728674a7SGreg Kroah-Hartman return hp->n_outbuf;
606728674a7SGreg Kroah-Hartman }
607728674a7SGreg Kroah-Hartman
608728674a7SGreg Kroah-Hartman /*
609728674a7SGreg Kroah-Hartman * timeout will vary between the MIN and MAX values defined here. By default
610728674a7SGreg Kroah-Hartman * and during console activity we will use a default MIN_TIMEOUT of 10. When
611728674a7SGreg Kroah-Hartman * the console is idle, we increase the timeout value on each pass through
612728674a7SGreg Kroah-Hartman * msleep until we reach the max. This may be noticeable as a brief (average
613728674a7SGreg Kroah-Hartman * one second) delay on the console before the console responds to input when
614728674a7SGreg Kroah-Hartman * there has been no input for some time.
615728674a7SGreg Kroah-Hartman */
616728674a7SGreg Kroah-Hartman #define MIN_TIMEOUT (10)
617728674a7SGreg Kroah-Hartman #define MAX_TIMEOUT (2000)
618728674a7SGreg Kroah-Hartman static u32 timeout = MIN_TIMEOUT;
619728674a7SGreg Kroah-Hartman
62068b2fc71SNicholas Piggin /*
62168b2fc71SNicholas Piggin * Maximum number of bytes to get from the console driver if hvc_poll is
62268b2fc71SNicholas Piggin * called from driver (and can't sleep). Any more than this and we break
6232ac62268SXiaofei Tan * and start polling with khvcd. This value was derived from an OpenBMC
62468b2fc71SNicholas Piggin * console with the OPAL driver that results in about 0.25ms interrupts off
62568b2fc71SNicholas Piggin * latency.
62668b2fc71SNicholas Piggin */
62768b2fc71SNicholas Piggin #define HVC_ATOMIC_READ_MAX 128
62868b2fc71SNicholas Piggin
629728674a7SGreg Kroah-Hartman #define HVC_POLL_READ 0x00000001
630728674a7SGreg Kroah-Hartman #define HVC_POLL_WRITE 0x00000002
631728674a7SGreg Kroah-Hartman
__hvc_poll(struct hvc_struct * hp,bool may_sleep)632cfb5946bSNicholas Piggin static int __hvc_poll(struct hvc_struct *hp, bool may_sleep)
633728674a7SGreg Kroah-Hartman {
634728674a7SGreg Kroah-Hartman struct tty_struct *tty;
635ec97eaadSNicholas Piggin int i, n, count, poll_mask = 0;
636728674a7SGreg Kroah-Hartman char buf[N_INBUF] __ALIGNED__;
637728674a7SGreg Kroah-Hartman unsigned long flags;
638728674a7SGreg Kroah-Hartman int read_total = 0;
639728674a7SGreg Kroah-Hartman int written_total = 0;
640728674a7SGreg Kroah-Hartman
641728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags);
642728674a7SGreg Kroah-Hartman
643728674a7SGreg Kroah-Hartman /* Push pending writes */
644728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0)
645728674a7SGreg Kroah-Hartman written_total = hvc_push(hp);
646728674a7SGreg Kroah-Hartman
647728674a7SGreg Kroah-Hartman /* Reschedule us if still some write pending */
648728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0) {
649728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_WRITE;
650728674a7SGreg Kroah-Hartman /* If hvc_push() was not able to write, sleep a few msecs */
651728674a7SGreg Kroah-Hartman timeout = (written_total) ? 0 : MIN_TIMEOUT;
652728674a7SGreg Kroah-Hartman }
653728674a7SGreg Kroah-Hartman
654cfb5946bSNicholas Piggin if (may_sleep) {
655cfb5946bSNicholas Piggin spin_unlock_irqrestore(&hp->lock, flags);
656cfb5946bSNicholas Piggin cond_resched();
657cfb5946bSNicholas Piggin spin_lock_irqsave(&hp->lock, flags);
658cfb5946bSNicholas Piggin }
659cfb5946bSNicholas Piggin
660728674a7SGreg Kroah-Hartman /* No tty attached, just skip */
66185bbc003SJiri Slaby tty = tty_port_tty_get(&hp->port);
662728674a7SGreg Kroah-Hartman if (tty == NULL)
663728674a7SGreg Kroah-Hartman goto bail;
664728674a7SGreg Kroah-Hartman
665728674a7SGreg Kroah-Hartman /* Now check if we can get data (are we throttled ?) */
66697ef38b8SPeter Hurley if (tty_throttled(tty))
667ec97eaadSNicholas Piggin goto out;
668728674a7SGreg Kroah-Hartman
669728674a7SGreg Kroah-Hartman /* If we aren't notifier driven and aren't throttled, we always
670728674a7SGreg Kroah-Hartman * request a reschedule
671728674a7SGreg Kroah-Hartman */
672728674a7SGreg Kroah-Hartman if (!hp->irq_requested)
673728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ;
674728674a7SGreg Kroah-Hartman
67568b2fc71SNicholas Piggin read_again:
676728674a7SGreg Kroah-Hartman /* Read data if any */
677ec97eaadSNicholas Piggin count = tty_buffer_request_room(&hp->port, N_INBUF);
678728674a7SGreg Kroah-Hartman
679728674a7SGreg Kroah-Hartman /* If flip is full, just reschedule a later read */
680728674a7SGreg Kroah-Hartman if (count == 0) {
681728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ;
682ec97eaadSNicholas Piggin goto out;
683728674a7SGreg Kroah-Hartman }
684728674a7SGreg Kroah-Hartman
685728674a7SGreg Kroah-Hartman n = hp->ops->get_chars(hp->vtermno, buf, count);
686728674a7SGreg Kroah-Hartman if (n <= 0) {
687728674a7SGreg Kroah-Hartman /* Hangup the tty when disconnected from host */
688728674a7SGreg Kroah-Hartman if (n == -EPIPE) {
689728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
690728674a7SGreg Kroah-Hartman tty_hangup(tty);
691728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags);
692728674a7SGreg Kroah-Hartman } else if ( n == -EAGAIN ) {
693728674a7SGreg Kroah-Hartman /*
694728674a7SGreg Kroah-Hartman * Some back-ends can only ensure a certain min
695728674a7SGreg Kroah-Hartman * num of bytes read, which may be > 'count'.
696728674a7SGreg Kroah-Hartman * Let the tty clear the flip buff to make room.
697728674a7SGreg Kroah-Hartman */
698728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ;
699728674a7SGreg Kroah-Hartman }
700ec97eaadSNicholas Piggin goto out;
701728674a7SGreg Kroah-Hartman }
702ec97eaadSNicholas Piggin
703728674a7SGreg Kroah-Hartman for (i = 0; i < n; ++i) {
704728674a7SGreg Kroah-Hartman #ifdef CONFIG_MAGIC_SYSRQ
705728674a7SGreg Kroah-Hartman if (hp->index == hvc_console.index) {
706728674a7SGreg Kroah-Hartman /* Handle the SysRq Hack */
707728674a7SGreg Kroah-Hartman /* XXX should support a sequence */
708728674a7SGreg Kroah-Hartman if (buf[i] == '\x0f') { /* ^O */
709728674a7SGreg Kroah-Hartman /* if ^O is pressed again, reset
710728674a7SGreg Kroah-Hartman * sysrq_pressed and flip ^O char */
711728674a7SGreg Kroah-Hartman sysrq_pressed = !sysrq_pressed;
712728674a7SGreg Kroah-Hartman if (sysrq_pressed)
713728674a7SGreg Kroah-Hartman continue;
714728674a7SGreg Kroah-Hartman } else if (sysrq_pressed) {
715728674a7SGreg Kroah-Hartman handle_sysrq(buf[i]);
716728674a7SGreg Kroah-Hartman sysrq_pressed = 0;
717728674a7SGreg Kroah-Hartman continue;
718728674a7SGreg Kroah-Hartman }
719728674a7SGreg Kroah-Hartman }
720728674a7SGreg Kroah-Hartman #endif /* CONFIG_MAGIC_SYSRQ */
72192a19f9cSJiri Slaby tty_insert_flip_char(&hp->port, buf[i], 0);
722728674a7SGreg Kroah-Hartman }
72368b2fc71SNicholas Piggin read_total += n;
72468b2fc71SNicholas Piggin
72568b2fc71SNicholas Piggin if (may_sleep) {
72668b2fc71SNicholas Piggin /* Keep going until the flip is full */
72768b2fc71SNicholas Piggin spin_unlock_irqrestore(&hp->lock, flags);
72868b2fc71SNicholas Piggin cond_resched();
72968b2fc71SNicholas Piggin spin_lock_irqsave(&hp->lock, flags);
73068b2fc71SNicholas Piggin goto read_again;
73168b2fc71SNicholas Piggin } else if (read_total < HVC_ATOMIC_READ_MAX) {
73268b2fc71SNicholas Piggin /* Break and defer if it's a large read in atomic */
73368b2fc71SNicholas Piggin goto read_again;
73468b2fc71SNicholas Piggin }
735728674a7SGreg Kroah-Hartman
7366e7f6b82SNicholas Piggin /*
7376e7f6b82SNicholas Piggin * Latency break, schedule another poll immediately.
7386e7f6b82SNicholas Piggin */
7396e7f6b82SNicholas Piggin poll_mask |= HVC_POLL_READ;
7406e7f6b82SNicholas Piggin
741ec97eaadSNicholas Piggin out:
742728674a7SGreg Kroah-Hartman /* Wakeup write queue if necessary */
743728674a7SGreg Kroah-Hartman if (hp->do_wakeup) {
744728674a7SGreg Kroah-Hartman hp->do_wakeup = 0;
745728674a7SGreg Kroah-Hartman tty_wakeup(tty);
746728674a7SGreg Kroah-Hartman }
747728674a7SGreg Kroah-Hartman bail:
748728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
749728674a7SGreg Kroah-Hartman
750728674a7SGreg Kroah-Hartman if (read_total) {
751728674a7SGreg Kroah-Hartman /* Activity is occurring, so reset the polling backoff value to
752728674a7SGreg Kroah-Hartman a minimum for performance. */
753728674a7SGreg Kroah-Hartman timeout = MIN_TIMEOUT;
754728674a7SGreg Kroah-Hartman
7552e124b4aSJiri Slaby tty_flip_buffer_push(&hp->port);
756728674a7SGreg Kroah-Hartman }
757728674a7SGreg Kroah-Hartman tty_kref_put(tty);
758728674a7SGreg Kroah-Hartman
759728674a7SGreg Kroah-Hartman return poll_mask;
760728674a7SGreg Kroah-Hartman }
761cfb5946bSNicholas Piggin
hvc_poll(struct hvc_struct * hp)762cfb5946bSNicholas Piggin int hvc_poll(struct hvc_struct *hp)
763cfb5946bSNicholas Piggin {
764cfb5946bSNicholas Piggin return __hvc_poll(hp, false);
765cfb5946bSNicholas Piggin }
766728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_poll);
767728674a7SGreg Kroah-Hartman
768728674a7SGreg Kroah-Hartman /**
769728674a7SGreg Kroah-Hartman * __hvc_resize() - Update terminal window size information.
770728674a7SGreg Kroah-Hartman * @hp: HVC console pointer
771728674a7SGreg Kroah-Hartman * @ws: Terminal window size structure
772728674a7SGreg Kroah-Hartman *
773728674a7SGreg Kroah-Hartman * Stores the specified window size information in the hvc structure of @hp.
774728674a7SGreg Kroah-Hartman * The function schedule the tty resize update.
775728674a7SGreg Kroah-Hartman *
776728674a7SGreg Kroah-Hartman * Locking: Locking free; the function MUST be called holding hp->lock
777728674a7SGreg Kroah-Hartman */
__hvc_resize(struct hvc_struct * hp,struct winsize ws)778728674a7SGreg Kroah-Hartman void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
779728674a7SGreg Kroah-Hartman {
780728674a7SGreg Kroah-Hartman hp->ws = ws;
781728674a7SGreg Kroah-Hartman schedule_work(&hp->tty_resize);
782728674a7SGreg Kroah-Hartman }
783728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(__hvc_resize);
784728674a7SGreg Kroah-Hartman
785728674a7SGreg Kroah-Hartman /*
786728674a7SGreg Kroah-Hartman * This kthread is either polling or interrupt driven. This is determined by
787728674a7SGreg Kroah-Hartman * calling hvc_poll() who determines whether a console adapter support
788728674a7SGreg Kroah-Hartman * interrupts.
789728674a7SGreg Kroah-Hartman */
khvcd(void * unused)790728674a7SGreg Kroah-Hartman static int khvcd(void *unused)
791728674a7SGreg Kroah-Hartman {
792728674a7SGreg Kroah-Hartman int poll_mask;
793728674a7SGreg Kroah-Hartman struct hvc_struct *hp;
794728674a7SGreg Kroah-Hartman
795728674a7SGreg Kroah-Hartman set_freezable();
796728674a7SGreg Kroah-Hartman do {
797728674a7SGreg Kroah-Hartman poll_mask = 0;
798728674a7SGreg Kroah-Hartman hvc_kicked = 0;
799728674a7SGreg Kroah-Hartman try_to_freeze();
800728674a7SGreg Kroah-Hartman wmb();
801728674a7SGreg Kroah-Hartman if (!cpus_are_in_xmon()) {
802a9bf5c8aSNicholas Piggin mutex_lock(&hvc_structs_mutex);
803728674a7SGreg Kroah-Hartman list_for_each_entry(hp, &hvc_structs, next) {
804cfb5946bSNicholas Piggin poll_mask |= __hvc_poll(hp, true);
805cfb5946bSNicholas Piggin cond_resched();
806728674a7SGreg Kroah-Hartman }
807a9bf5c8aSNicholas Piggin mutex_unlock(&hvc_structs_mutex);
808728674a7SGreg Kroah-Hartman } else
809728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ;
810728674a7SGreg Kroah-Hartman if (hvc_kicked)
811728674a7SGreg Kroah-Hartman continue;
812728674a7SGreg Kroah-Hartman set_current_state(TASK_INTERRUPTIBLE);
813728674a7SGreg Kroah-Hartman if (!hvc_kicked) {
814728674a7SGreg Kroah-Hartman if (poll_mask == 0)
815728674a7SGreg Kroah-Hartman schedule();
816728674a7SGreg Kroah-Hartman else {
81715a27431SBenjamin Herrenschmidt unsigned long j_timeout;
81815a27431SBenjamin Herrenschmidt
819728674a7SGreg Kroah-Hartman if (timeout < MAX_TIMEOUT)
820728674a7SGreg Kroah-Hartman timeout += (timeout >> 6) + 1;
821728674a7SGreg Kroah-Hartman
82215a27431SBenjamin Herrenschmidt /*
82315a27431SBenjamin Herrenschmidt * We don't use msleep_interruptible otherwise
82415a27431SBenjamin Herrenschmidt * "kick" will fail to wake us up
82515a27431SBenjamin Herrenschmidt */
82615a27431SBenjamin Herrenschmidt j_timeout = msecs_to_jiffies(timeout) + 1;
82715a27431SBenjamin Herrenschmidt schedule_timeout_interruptible(j_timeout);
828728674a7SGreg Kroah-Hartman }
829728674a7SGreg Kroah-Hartman }
830728674a7SGreg Kroah-Hartman __set_current_state(TASK_RUNNING);
831728674a7SGreg Kroah-Hartman } while (!kthread_should_stop());
832728674a7SGreg Kroah-Hartman
833728674a7SGreg Kroah-Hartman return 0;
834728674a7SGreg Kroah-Hartman }
835728674a7SGreg Kroah-Hartman
hvc_tiocmget(struct tty_struct * tty)8364d2bb3f5SBenjamin Herrenschmidt static int hvc_tiocmget(struct tty_struct *tty)
8374d2bb3f5SBenjamin Herrenschmidt {
8384d2bb3f5SBenjamin Herrenschmidt struct hvc_struct *hp = tty->driver_data;
8394d2bb3f5SBenjamin Herrenschmidt
8404d2bb3f5SBenjamin Herrenschmidt if (!hp || !hp->ops->tiocmget)
8414d2bb3f5SBenjamin Herrenschmidt return -EINVAL;
8424d2bb3f5SBenjamin Herrenschmidt return hp->ops->tiocmget(hp);
8434d2bb3f5SBenjamin Herrenschmidt }
8444d2bb3f5SBenjamin Herrenschmidt
hvc_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)8454d2bb3f5SBenjamin Herrenschmidt static int hvc_tiocmset(struct tty_struct *tty,
8464d2bb3f5SBenjamin Herrenschmidt unsigned int set, unsigned int clear)
8474d2bb3f5SBenjamin Herrenschmidt {
8484d2bb3f5SBenjamin Herrenschmidt struct hvc_struct *hp = tty->driver_data;
8494d2bb3f5SBenjamin Herrenschmidt
8504d2bb3f5SBenjamin Herrenschmidt if (!hp || !hp->ops->tiocmset)
8514d2bb3f5SBenjamin Herrenschmidt return -EINVAL;
8524d2bb3f5SBenjamin Herrenschmidt return hp->ops->tiocmset(hp, set, clear);
8534d2bb3f5SBenjamin Herrenschmidt }
8544d2bb3f5SBenjamin Herrenschmidt
855762e77aeSAnton Blanchard #ifdef CONFIG_CONSOLE_POLL
hvc_poll_init(struct tty_driver * driver,int line,char * options)8565855b210SRashika Kheria static int hvc_poll_init(struct tty_driver *driver, int line, char *options)
857762e77aeSAnton Blanchard {
858762e77aeSAnton Blanchard return 0;
859762e77aeSAnton Blanchard }
860762e77aeSAnton Blanchard
hvc_poll_get_char(struct tty_driver * driver,int line)861762e77aeSAnton Blanchard static int hvc_poll_get_char(struct tty_driver *driver, int line)
862762e77aeSAnton Blanchard {
863762e77aeSAnton Blanchard struct tty_struct *tty = driver->ttys[0];
864762e77aeSAnton Blanchard struct hvc_struct *hp = tty->driver_data;
865762e77aeSAnton Blanchard int n;
866762e77aeSAnton Blanchard char ch;
867762e77aeSAnton Blanchard
868762e77aeSAnton Blanchard n = hp->ops->get_chars(hp->vtermno, &ch, 1);
869762e77aeSAnton Blanchard
870fab794aeSDan Carpenter if (n <= 0)
871762e77aeSAnton Blanchard return NO_POLL_CHAR;
872762e77aeSAnton Blanchard
873762e77aeSAnton Blanchard return ch;
874762e77aeSAnton Blanchard }
875762e77aeSAnton Blanchard
hvc_poll_put_char(struct tty_driver * driver,int line,char ch)876762e77aeSAnton Blanchard static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
877762e77aeSAnton Blanchard {
878762e77aeSAnton Blanchard struct tty_struct *tty = driver->ttys[0];
879762e77aeSAnton Blanchard struct hvc_struct *hp = tty->driver_data;
880762e77aeSAnton Blanchard int n;
881762e77aeSAnton Blanchard
882762e77aeSAnton Blanchard do {
88360f41e84SGreg Kroah-Hartman n = hp->ops->put_chars(hp->vtermno, &ch, 1);
884762e77aeSAnton Blanchard } while (n <= 0);
885762e77aeSAnton Blanchard }
886762e77aeSAnton Blanchard #endif
887762e77aeSAnton Blanchard
888728674a7SGreg Kroah-Hartman static const struct tty_operations hvc_ops = {
889bdb498c2SJiri Slaby .install = hvc_install,
890728674a7SGreg Kroah-Hartman .open = hvc_open,
891728674a7SGreg Kroah-Hartman .close = hvc_close,
892bdb498c2SJiri Slaby .cleanup = hvc_cleanup,
893728674a7SGreg Kroah-Hartman .write = hvc_write,
894728674a7SGreg Kroah-Hartman .hangup = hvc_hangup,
895728674a7SGreg Kroah-Hartman .unthrottle = hvc_unthrottle,
896728674a7SGreg Kroah-Hartman .write_room = hvc_write_room,
897728674a7SGreg Kroah-Hartman .chars_in_buffer = hvc_chars_in_buffer,
8984d2bb3f5SBenjamin Herrenschmidt .tiocmget = hvc_tiocmget,
8994d2bb3f5SBenjamin Herrenschmidt .tiocmset = hvc_tiocmset,
900762e77aeSAnton Blanchard #ifdef CONFIG_CONSOLE_POLL
901762e77aeSAnton Blanchard .poll_init = hvc_poll_init,
902762e77aeSAnton Blanchard .poll_get_char = hvc_poll_get_char,
903762e77aeSAnton Blanchard .poll_put_char = hvc_poll_put_char,
904762e77aeSAnton Blanchard #endif
905728674a7SGreg Kroah-Hartman };
906728674a7SGreg Kroah-Hartman
907f3d9f250SJiri Slaby static const struct tty_port_operations hvc_port_ops = {
908f3d9f250SJiri Slaby .destruct = hvc_port_destruct,
909f3d9f250SJiri Slaby };
910f3d9f250SJiri Slaby
hvc_alloc(uint32_t vtermno,int data,const struct hv_ops * ops,int outbuf_size)911728674a7SGreg Kroah-Hartman struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
912728674a7SGreg Kroah-Hartman const struct hv_ops *ops,
913728674a7SGreg Kroah-Hartman int outbuf_size)
914728674a7SGreg Kroah-Hartman {
915728674a7SGreg Kroah-Hartman struct hvc_struct *hp;
916728674a7SGreg Kroah-Hartman int i;
917728674a7SGreg Kroah-Hartman
918728674a7SGreg Kroah-Hartman /* We wait until a driver actually comes along */
919f76a1cbeSPaul Gortmaker if (atomic_inc_not_zero(&hvc_needs_init)) {
920728674a7SGreg Kroah-Hartman int err = hvc_init();
921728674a7SGreg Kroah-Hartman if (err)
922728674a7SGreg Kroah-Hartman return ERR_PTR(err);
923728674a7SGreg Kroah-Hartman }
924728674a7SGreg Kroah-Hartman
92560f41e84SGreg Kroah-Hartman hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
92660f41e84SGreg Kroah-Hartman GFP_KERNEL);
927728674a7SGreg Kroah-Hartman if (!hp)
928728674a7SGreg Kroah-Hartman return ERR_PTR(-ENOMEM);
929728674a7SGreg Kroah-Hartman
930728674a7SGreg Kroah-Hartman hp->vtermno = vtermno;
931728674a7SGreg Kroah-Hartman hp->data = data;
932728674a7SGreg Kroah-Hartman hp->ops = ops;
933728674a7SGreg Kroah-Hartman hp->outbuf_size = outbuf_size;
93460f41e84SGreg Kroah-Hartman hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
935728674a7SGreg Kroah-Hartman
936f3d9f250SJiri Slaby tty_port_init(&hp->port);
937f3d9f250SJiri Slaby hp->port.ops = &hvc_port_ops;
938728674a7SGreg Kroah-Hartman
939728674a7SGreg Kroah-Hartman INIT_WORK(&hp->tty_resize, hvc_set_winsz);
940728674a7SGreg Kroah-Hartman spin_lock_init(&hp->lock);
941a9bf5c8aSNicholas Piggin mutex_lock(&hvc_structs_mutex);
942728674a7SGreg Kroah-Hartman
943728674a7SGreg Kroah-Hartman /*
944728674a7SGreg Kroah-Hartman * find index to use:
945728674a7SGreg Kroah-Hartman * see if this vterm id matches one registered for console.
946728674a7SGreg Kroah-Hartman */
947728674a7SGreg Kroah-Hartman for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
948728674a7SGreg Kroah-Hartman if (vtermnos[i] == hp->vtermno &&
949728674a7SGreg Kroah-Hartman cons_ops[i] == hp->ops)
950728674a7SGreg Kroah-Hartman break;
951728674a7SGreg Kroah-Hartman
9529a9fc42bSAndrew Melnychenko if (i >= MAX_NR_HVC_CONSOLES) {
9539a9fc42bSAndrew Melnychenko
9549a9fc42bSAndrew Melnychenko /* find 'empty' slot for console */
9559a9fc42bSAndrew Melnychenko for (i = 0; i < MAX_NR_HVC_CONSOLES && vtermnos[i] != -1; i++) {
9569a9fc42bSAndrew Melnychenko }
9579a9fc42bSAndrew Melnychenko
958728674a7SGreg Kroah-Hartman /* no matching slot, just use a counter */
9599a9fc42bSAndrew Melnychenko if (i == MAX_NR_HVC_CONSOLES)
9609a9fc42bSAndrew Melnychenko i = ++last_hvc + MAX_NR_HVC_CONSOLES;
9619a9fc42bSAndrew Melnychenko }
962728674a7SGreg Kroah-Hartman
963728674a7SGreg Kroah-Hartman hp->index = i;
9649a9fc42bSAndrew Melnychenko if (i < MAX_NR_HVC_CONSOLES) {
96592057a49SBenjamin Herrenschmidt cons_ops[i] = ops;
96692057a49SBenjamin Herrenschmidt vtermnos[i] = vtermno;
9679a9fc42bSAndrew Melnychenko }
968728674a7SGreg Kroah-Hartman
969728674a7SGreg Kroah-Hartman list_add_tail(&(hp->next), &hvc_structs);
970a9bf5c8aSNicholas Piggin mutex_unlock(&hvc_structs_mutex);
971728674a7SGreg Kroah-Hartman
97292057a49SBenjamin Herrenschmidt /* check if we need to re-register the kernel console */
97392057a49SBenjamin Herrenschmidt hvc_check_console(i);
97492057a49SBenjamin Herrenschmidt
975728674a7SGreg Kroah-Hartman return hp;
976728674a7SGreg Kroah-Hartman }
977728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_alloc);
978728674a7SGreg Kroah-Hartman
hvc_remove(struct hvc_struct * hp)979728674a7SGreg Kroah-Hartman int hvc_remove(struct hvc_struct *hp)
980728674a7SGreg Kroah-Hartman {
981728674a7SGreg Kroah-Hartman unsigned long flags;
982728674a7SGreg Kroah-Hartman struct tty_struct *tty;
983728674a7SGreg Kroah-Hartman
98485bbc003SJiri Slaby tty = tty_port_tty_get(&hp->port);
985728674a7SGreg Kroah-Hartman
98619fa6e60SDenis Kirjanov console_lock();
98785bbc003SJiri Slaby spin_lock_irqsave(&hp->lock, flags);
98892057a49SBenjamin Herrenschmidt if (hp->index < MAX_NR_HVC_CONSOLES) {
989728674a7SGreg Kroah-Hartman vtermnos[hp->index] = -1;
99092057a49SBenjamin Herrenschmidt cons_ops[hp->index] = NULL;
99192057a49SBenjamin Herrenschmidt }
992728674a7SGreg Kroah-Hartman
993728674a7SGreg Kroah-Hartman /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
994728674a7SGreg Kroah-Hartman
995728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags);
99619fa6e60SDenis Kirjanov console_unlock();
997728674a7SGreg Kroah-Hartman
998728674a7SGreg Kroah-Hartman /*
999728674a7SGreg Kroah-Hartman * We 'put' the instance that was grabbed when the kref instance
1000728674a7SGreg Kroah-Hartman * was initialized using kref_init(). Let the last holder of this
1001728674a7SGreg Kroah-Hartman * kref cause it to be removed, which will probably be the tty_vhangup
1002728674a7SGreg Kroah-Hartman * below.
1003728674a7SGreg Kroah-Hartman */
1004f3d9f250SJiri Slaby tty_port_put(&hp->port);
1005728674a7SGreg Kroah-Hartman
1006728674a7SGreg Kroah-Hartman /*
1007728674a7SGreg Kroah-Hartman * This function call will auto chain call hvc_hangup.
1008728674a7SGreg Kroah-Hartman */
1009728674a7SGreg Kroah-Hartman if (tty) {
1010728674a7SGreg Kroah-Hartman tty_vhangup(tty);
1011728674a7SGreg Kroah-Hartman tty_kref_put(tty);
1012728674a7SGreg Kroah-Hartman }
1013728674a7SGreg Kroah-Hartman return 0;
1014728674a7SGreg Kroah-Hartman }
1015728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_remove);
1016728674a7SGreg Kroah-Hartman
1017728674a7SGreg Kroah-Hartman /* Driver initialization: called as soon as someone uses hvc_alloc(). */
hvc_init(void)1018728674a7SGreg Kroah-Hartman static int hvc_init(void)
1019728674a7SGreg Kroah-Hartman {
1020728674a7SGreg Kroah-Hartman struct tty_driver *drv;
1021728674a7SGreg Kroah-Hartman int err;
1022728674a7SGreg Kroah-Hartman
1023728674a7SGreg Kroah-Hartman /* We need more than hvc_count adapters due to hotplug additions. */
102439b7b42bSJiri Slaby drv = tty_alloc_driver(HVC_ALLOC_TTY_ADAPTERS, TTY_DRIVER_REAL_RAW |
102539b7b42bSJiri Slaby TTY_DRIVER_RESET_TERMIOS);
102639b7b42bSJiri Slaby if (IS_ERR(drv)) {
102739b7b42bSJiri Slaby err = PTR_ERR(drv);
1028728674a7SGreg Kroah-Hartman goto out;
1029728674a7SGreg Kroah-Hartman }
1030728674a7SGreg Kroah-Hartman
1031728674a7SGreg Kroah-Hartman drv->driver_name = "hvc";
1032728674a7SGreg Kroah-Hartman drv->name = "hvc";
1033728674a7SGreg Kroah-Hartman drv->major = HVC_MAJOR;
1034728674a7SGreg Kroah-Hartman drv->minor_start = HVC_MINOR;
1035728674a7SGreg Kroah-Hartman drv->type = TTY_DRIVER_TYPE_SYSTEM;
1036728674a7SGreg Kroah-Hartman drv->init_termios = tty_std_termios;
1037728674a7SGreg Kroah-Hartman tty_set_operations(drv, &hvc_ops);
1038728674a7SGreg Kroah-Hartman
1039728674a7SGreg Kroah-Hartman /* Always start the kthread because there can be hotplug vty adapters
1040728674a7SGreg Kroah-Hartman * added later. */
1041728674a7SGreg Kroah-Hartman hvc_task = kthread_run(khvcd, NULL, "khvcd");
1042728674a7SGreg Kroah-Hartman if (IS_ERR(hvc_task)) {
1043728674a7SGreg Kroah-Hartman printk(KERN_ERR "Couldn't create kthread for console.\n");
1044728674a7SGreg Kroah-Hartman err = PTR_ERR(hvc_task);
1045728674a7SGreg Kroah-Hartman goto put_tty;
1046728674a7SGreg Kroah-Hartman }
1047728674a7SGreg Kroah-Hartman
1048728674a7SGreg Kroah-Hartman err = tty_register_driver(drv);
1049728674a7SGreg Kroah-Hartman if (err) {
1050728674a7SGreg Kroah-Hartman printk(KERN_ERR "Couldn't register hvc console driver\n");
1051728674a7SGreg Kroah-Hartman goto stop_thread;
1052728674a7SGreg Kroah-Hartman }
1053728674a7SGreg Kroah-Hartman
1054728674a7SGreg Kroah-Hartman /*
1055728674a7SGreg Kroah-Hartman * Make sure tty is fully registered before allowing it to be
1056728674a7SGreg Kroah-Hartman * found by hvc_console_device.
1057728674a7SGreg Kroah-Hartman */
1058728674a7SGreg Kroah-Hartman smp_mb();
1059728674a7SGreg Kroah-Hartman hvc_driver = drv;
1060728674a7SGreg Kroah-Hartman return 0;
1061728674a7SGreg Kroah-Hartman
1062728674a7SGreg Kroah-Hartman stop_thread:
1063728674a7SGreg Kroah-Hartman kthread_stop(hvc_task);
1064728674a7SGreg Kroah-Hartman hvc_task = NULL;
1065728674a7SGreg Kroah-Hartman put_tty:
10669f90a4ddSJiri Slaby tty_driver_kref_put(drv);
1067728674a7SGreg Kroah-Hartman out:
1068728674a7SGreg Kroah-Hartman return err;
1069728674a7SGreg Kroah-Hartman }
1070