blob: f179033eaa3ec7c70819a4123815bd43296857bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
4 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
5 * Copyright (C) 2004 IBM Corporation
6 *
7 * Additional Author(s):
8 * Ryan S. Arnold <rsa@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/console.h>
26#include <linux/cpumask.h>
27#include <linux/init.h>
28#include <linux/kbd_kern.h>
29#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/kthread.h>
31#include <linux/list.h>
32#include <linux/module.h>
33#include <linux/major.h>
Paul Gortmaker94804de2014-01-14 16:03:37 -050034#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/sysrq.h>
36#include <linux/tty.h>
37#include <linux/tty_flip.h>
38#include <linux/sched.h>
39#include <linux/spinlock.h>
40#include <linux/delay.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080041#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Anton Blanchard762e77a2011-07-12 19:44:05 +000043#include <linux/serial_core.h>
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020046
47#include "hvc_console.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define HVC_MAJOR 229
50#define HVC_MINOR 0
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Wait this long per iteration while trying to push buffered data to the
54 * hypervisor before allowing the tty to complete a close operation.
55 */
56#define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
57
58/*
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020059 * These sizes are most efficient for vio, because they are the
60 * native transfer size. We could make them selectable in the
61 * future to better deal with backends that want other buffer sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define N_OUTBUF 16
64#define N_INBUF 16
65
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020066#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Milton Miller837dcfa2005-07-07 17:56:16 -070068static struct tty_driver *hvc_driver;
69static struct task_struct *hvc_task;
70
71/* Picks up late kicks after list walk but before schedule() */
72static int hvc_kicked;
73
Paul Gortmaker94804de2014-01-14 16:03:37 -050074/* hvc_init is triggered from hvc_alloc, i.e. only when actually used */
75static atomic_t hvc_needs_init __read_mostly = ATOMIC_INIT(-1);
76
Rusty Russell3e6c6f62007-10-16 23:30:13 -070077static int hvc_init(void);
78
Milton Miller837dcfa2005-07-07 17:56:16 -070079#ifdef CONFIG_MAGIC_SYSRQ
80static int sysrq_pressed;
81#endif
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* dynamic list of hvc_struct instances */
Denis Chengbed97592008-02-06 01:37:35 -080084static LIST_HEAD(hvc_structs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Protect the list of hvc_struct instances from inserts and removals during
88 * list traversal.
89 */
90static DEFINE_SPINLOCK(hvc_structs_lock);
91
92/*
Milton Miller6f248082005-07-07 17:56:17 -070093 * This value is used to assign a tty->index value to a hvc_struct based
94 * upon order of exposure via hvc_probe(), when we can not match it to
will schmidtc3ea6922007-04-18 00:54:51 +100095 * a console candidate registered with hvc_instantiate().
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Milton Miller6f248082005-07-07 17:56:17 -070097static int last_hvc = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
will schmidtc3ea6922007-04-18 00:54:51 +1000100 * Do not call this function with either the hvc_structs_lock or the hvc_struct
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800101 * lock held. If successful, this function increments the kref reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * count against the target hvc_struct so it should be released when finished.
103 */
Adrian Bunk4fa156e2007-05-08 00:24:24 -0700104static struct hvc_struct *hvc_get_by_index(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct hvc_struct *hp;
107 unsigned long flags;
108
109 spin_lock(&hvc_structs_lock);
110
111 list_for_each_entry(hp, &hvc_structs, next) {
112 spin_lock_irqsave(&hp->lock, flags);
113 if (hp->index == index) {
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200114 tty_port_get(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_unlock_irqrestore(&hp->lock, flags);
116 spin_unlock(&hvc_structs_lock);
117 return hp;
118 }
119 spin_unlock_irqrestore(&hp->lock, flags);
120 }
121 hp = NULL;
122
123 spin_unlock(&hvc_structs_lock);
124 return hp;
125}
126
Milton Miller837dcfa2005-07-07 17:56:16 -0700127
128/*
129 * Initial console vtermnos for console API usage prior to full console
130 * initialization. Any vty adapter outside this range will not have usable
131 * console interfaces but can still be used as a tty device. This has to be
132 * static because kmalloc will not work during early console init.
133 */
Rusty Russellb5113062010-01-18 03:44:57 +0000134static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
Milton Miller64e4da52005-07-07 17:56:22 -0700135static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
136 {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
Milton Miller837dcfa2005-07-07 17:56:16 -0700137
138/*
139 * Console APIs, NOT TTY. These APIs are available immediately when
140 * hvc_console_setup() finds adapters.
141 */
142
Adrian Bunk4fa156e2007-05-08 00:24:24 -0700143static void hvc_console_print(struct console *co, const char *b,
144 unsigned count)
Milton Miller837dcfa2005-07-07 17:56:16 -0700145{
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200146 char c[N_OUTBUF] __ALIGNED__;
Milton Miller837dcfa2005-07-07 17:56:16 -0700147 unsigned i = 0, n = 0;
Milton Miller030ffad2005-07-07 17:56:25 -0700148 int r, donecr = 0, index = co->index;
Milton Miller837dcfa2005-07-07 17:56:16 -0700149
150 /* Console access attempt outside of acceptable console range. */
Milton Miller030ffad2005-07-07 17:56:25 -0700151 if (index >= MAX_NR_HVC_CONSOLES)
Milton Miller837dcfa2005-07-07 17:56:16 -0700152 return;
153
will schmidtc3ea6922007-04-18 00:54:51 +1000154 /* This console adapter was removed so it is not usable. */
Roel Kluinecfcbee2009-12-09 12:34:16 -0800155 if (vtermnos[index] == -1)
Milton Miller837dcfa2005-07-07 17:56:16 -0700156 return;
157
158 while (count > 0 || i > 0) {
159 if (count > 0 && i < sizeof(c)) {
160 if (b[n] == '\n' && !donecr) {
161 c[i++] = '\r';
162 donecr = 1;
163 } else {
164 c[i++] = b[n++];
165 donecr = 0;
166 --count;
167 }
168 } else {
Milton Miller030ffad2005-07-07 17:56:25 -0700169 r = cons_ops[index]->put_chars(vtermnos[index], c, i);
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000170 if (r <= 0) {
Hendrik Brueckner8c2381a2011-07-05 21:50:18 +0000171 /* throw away characters on error
172 * but spin in case of -EAGAIN */
173 if (r != -EAGAIN)
174 i = 0;
Milton Miller837dcfa2005-07-07 17:56:16 -0700175 } else if (r > 0) {
176 i -= r;
177 if (i > 0)
178 memmove(c, c+r, i);
179 }
180 }
181 }
182}
183
184static struct tty_driver *hvc_console_device(struct console *c, int *index)
185{
Milton Miller7805b1b2005-07-07 17:56:23 -0700186 if (vtermnos[c->index] == -1)
187 return NULL;
188
Milton Miller837dcfa2005-07-07 17:56:16 -0700189 *index = c->index;
190 return hvc_driver;
191}
192
Tomoki Sekiyamaeebf62b2014-05-02 18:58:24 -0400193static int hvc_console_setup(struct console *co, char *options)
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000194{
Milton Miller7805b1b2005-07-07 17:56:23 -0700195 if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
196 return -ENODEV;
197
198 if (vtermnos[co->index] == -1)
199 return -ENODEV;
200
Milton Miller837dcfa2005-07-07 17:56:16 -0700201 return 0;
202}
203
Chris Metcalf4455b112010-07-06 08:03:16 +0000204static struct console hvc_console = {
Milton Miller837dcfa2005-07-07 17:56:16 -0700205 .name = "hvc",
206 .write = hvc_console_print,
207 .device = hvc_console_device,
208 .setup = hvc_console_setup,
209 .flags = CON_PRINTBUFFER,
210 .index = -1,
211};
212
Milton Millerd5ee2572005-07-07 17:56:24 -0700213/*
will schmidtc3ea6922007-04-18 00:54:51 +1000214 * Early console initialization. Precedes driver initialization.
Milton Millerd5ee2572005-07-07 17:56:24 -0700215 *
216 * (1) we are first, and the user specified another driver
217 * -- index will remain -1
218 * (2) we are first and the user specified no driver
219 * -- index will be set to 0, then we will fail setup.
220 * (3) we are first and the user specified our driver
221 * -- index will be set to user specified driver, and we will fail
222 * (4) we are after driver, and this initcall will register us
223 * -- if the user didn't specify a driver then the console will match
224 *
225 * Note that for cases 2 and 3, we will match later when the io driver
226 * calls hvc_instantiate() and call register again.
227 */
Milton Miller837dcfa2005-07-07 17:56:16 -0700228static int __init hvc_console_init(void)
229{
Chris Metcalf4455b112010-07-06 08:03:16 +0000230 register_console(&hvc_console);
Milton Miller837dcfa2005-07-07 17:56:16 -0700231 return 0;
232}
233console_initcall(hvc_console_init);
234
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800235/* callback when the kboject ref count reaches zero. */
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200236static void hvc_port_destruct(struct tty_port *port)
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800237{
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200238 struct hvc_struct *hp = container_of(port, struct hvc_struct, port);
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800239 unsigned long flags;
240
241 spin_lock(&hvc_structs_lock);
242
243 spin_lock_irqsave(&hp->lock, flags);
244 list_del(&(hp->next));
245 spin_unlock_irqrestore(&hp->lock, flags);
246
247 spin_unlock(&hvc_structs_lock);
248
249 kfree(hp);
250}
251
Benjamin Herrenschmidt92057a42012-07-23 21:33:13 +0000252static void hvc_check_console(int index)
253{
254 /* Already enabled, bail out */
255 if (hvc_console.flags & CON_ENABLED)
256 return;
257
258 /* If this index is what the user requested, then register
259 * now (setup won't fail at this point). It's ok to just
260 * call register again if previously .setup failed.
261 */
262 if (index == hvc_console.index)
263 register_console(&hvc_console);
264}
265
Milton Miller837dcfa2005-07-07 17:56:16 -0700266/*
Milton Miller6f248082005-07-07 17:56:17 -0700267 * hvc_instantiate() is an early console discovery method which locates
268 * consoles * prior to the vio subsystem discovering them. Hotplugged
269 * vty adapters do NOT get an hvc_instantiate() callback since they
270 * appear after early console init.
Milton Miller837dcfa2005-07-07 17:56:16 -0700271 */
Rusty Russellb5113062010-01-18 03:44:57 +0000272int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
Milton Miller837dcfa2005-07-07 17:56:16 -0700273{
Milton Miller7805b1b2005-07-07 17:56:23 -0700274 struct hvc_struct *hp;
275
Milton Miller837dcfa2005-07-07 17:56:16 -0700276 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
277 return -1;
278
279 if (vtermnos[index] != -1)
280 return -1;
281
will schmidtc3ea6922007-04-18 00:54:51 +1000282 /* make sure no no tty has been registered in this index */
Milton Miller7805b1b2005-07-07 17:56:23 -0700283 hp = hvc_get_by_index(index);
284 if (hp) {
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200285 tty_port_put(&hp->port);
Milton Miller7805b1b2005-07-07 17:56:23 -0700286 return -1;
287 }
288
Milton Miller837dcfa2005-07-07 17:56:16 -0700289 vtermnos[index] = vtermno;
Milton Miller030ffad2005-07-07 17:56:25 -0700290 cons_ops[index] = ops;
Milton Miller6f248082005-07-07 17:56:17 -0700291
will schmidtc3ea6922007-04-18 00:54:51 +1000292 /* reserve all indices up to and including this index */
Milton Miller6f248082005-07-07 17:56:17 -0700293 if (last_hvc < index)
294 last_hvc = index;
295
Benjamin Herrenschmidt92057a42012-07-23 21:33:13 +0000296 /* check if we need to re-register the kernel console */
297 hvc_check_console(index);
Milton Millerd5ee2572005-07-07 17:56:24 -0700298
Milton Miller837dcfa2005-07-07 17:56:16 -0700299 return 0;
300}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500301EXPORT_SYMBOL_GPL(hvc_instantiate);
Milton Miller837dcfa2005-07-07 17:56:16 -0700302
303/* Wake the sleeping khvcd */
Christian Borntraeger611e0972008-06-20 15:24:08 +0200304void hvc_kick(void)
Milton Miller837dcfa2005-07-07 17:56:16 -0700305{
306 hvc_kicked = 1;
307 wake_up_process(hvc_task);
308}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500309EXPORT_SYMBOL_GPL(hvc_kick);
Milton Miller837dcfa2005-07-07 17:56:16 -0700310
Milton Miller837dcfa2005-07-07 17:56:16 -0700311static void hvc_unthrottle(struct tty_struct *tty)
312{
313 hvc_kick();
314}
315
Jiri Slabybdb498c2012-08-07 21:48:04 +0200316static int hvc_install(struct tty_driver *driver, struct tty_struct *tty)
317{
318 struct hvc_struct *hp;
319 int rc;
320
321 /* Auto increments kref reference if found. */
322 if (!(hp = hvc_get_by_index(tty->index)))
323 return -ENODEV;
324
325 tty->driver_data = hp;
326
327 rc = tty_port_install(&hp->port, driver, tty);
328 if (rc)
329 tty_port_put(&hp->port);
330 return rc;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*
334 * The TTY interface won't be used until after the vio layer has exposed the vty
335 * adapter to the kernel.
336 */
337static int hvc_open(struct tty_struct *tty, struct file * filp)
338{
Jiri Slabybdb498c2012-08-07 21:48:04 +0200339 struct hvc_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jiri Slaby0146b692012-04-02 13:54:23 +0200343 spin_lock_irqsave(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Check and then increment for fast path open. */
Jiri Slaby0146b692012-04-02 13:54:23 +0200345 if (hp->port.count++ > 0) {
346 spin_unlock_irqrestore(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 hvc_kick();
348 return 0;
349 } /* else count == 0 */
Jiri Slaby0146b692012-04-02 13:54:23 +0200350 spin_unlock_irqrestore(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jiri Slaby85bbc002012-04-02 13:54:22 +0200352 tty_port_tty_set(&hp->port, tty);
353
Christian Borntraegerb1b135c2008-08-07 09:18:34 +0200354 if (hp->ops->notifier_add)
355 rc = hp->ops->notifier_add(hp, hp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /*
Christian Borntraeger611e0972008-06-20 15:24:08 +0200358 * If the notifier fails we return an error. The tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * will call hvc_close() after a failed open but we don't want to clean
360 * up there so we'll clean up here and clear out the previously set
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800361 * tty fields and return the kref reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 if (rc) {
Jiri Slaby85bbc002012-04-02 13:54:22 +0200364 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 tty->driver_data = NULL;
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200366 tty_port_put(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
368 }
369 /* Force wakeup of the polling thread */
370 hvc_kick();
371
372 return rc;
373}
374
375static void hvc_close(struct tty_struct *tty, struct file * filp)
376{
377 struct hvc_struct *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 unsigned long flags;
379
380 if (tty_hung_up_p(filp))
381 return;
382
383 /*
384 * No driver_data means that this close was issued after a failed
385 * hvc_open by the tty layer's release_dev() function and we can just
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800386 * exit cleanly because the kref reference wasn't made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388 if (!tty->driver_data)
389 return;
390
391 hp = tty->driver_data;
Amit Shahe74d0982010-03-12 11:53:15 +0530392
Jiri Slaby0146b692012-04-02 13:54:23 +0200393 spin_lock_irqsave(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jiri Slaby0146b692012-04-02 13:54:23 +0200395 if (--hp->port.count == 0) {
396 spin_unlock_irqrestore(&hp->port.lock, flags);
Jiri Slaby85bbc002012-04-02 13:54:22 +0200397 /* We are done with the tty pointer now. */
398 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Christian Borntraegereef26222008-10-12 21:51:31 +0000400 if (hp->ops->notifier_del)
401 hp->ops->notifier_del(hp, hp->data);
402
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000403 /* cancel pending tty resize work */
404 cancel_work_sync(&hp->tty_resize);
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * Chain calls chars_in_buffer() and returns immediately if
408 * there is no buffered data otherwise sleeps on a wait queue
409 * waking periodically to check chars_in_buffer().
410 */
Jiri Slaby0b058352011-08-25 15:12:08 +0200411 tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } else {
Jiri Slaby0146b692012-04-02 13:54:23 +0200413 if (hp->port.count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
Jiri Slaby0146b692012-04-02 13:54:23 +0200415 hp->vtermno, hp->port.count);
416 spin_unlock_irqrestore(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Jiri Slabybdb498c2012-08-07 21:48:04 +0200418}
419
420static void hvc_cleanup(struct tty_struct *tty)
421{
422 struct hvc_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200424 tty_port_put(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static void hvc_hangup(struct tty_struct *tty)
428{
429 struct hvc_struct *hp = tty->driver_data;
430 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!hp)
433 return;
434
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000435 /* cancel pending tty resize work */
436 cancel_work_sync(&hp->tty_resize);
437
Jiri Slaby0146b692012-04-02 13:54:23 +0200438 spin_lock_irqsave(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * The N_TTY line discipline has problems such that in a close vs
442 * open->hangup case this can be called after the final close so prevent
443 * that from happening for now.
444 */
Jiri Slaby0146b692012-04-02 13:54:23 +0200445 if (hp->port.count <= 0) {
446 spin_unlock_irqrestore(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return;
448 }
449
Jiri Slaby0146b692012-04-02 13:54:23 +0200450 hp->port.count = 0;
451 spin_unlock_irqrestore(&hp->port.lock, flags);
Jiri Slaby85bbc002012-04-02 13:54:22 +0200452 tty_port_tty_set(&hp->port, NULL);
Christian Borntraegereef26222008-10-12 21:51:31 +0000453
Jiri Slaby0146b692012-04-02 13:54:23 +0200454 hp->n_outbuf = 0;
455
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000456 if (hp->ops->notifier_hangup)
Amit Shahe74d0982010-03-12 11:53:15 +0530457 hp->ops->notifier_hangup(hp, hp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Push buffered characters whether they were just recently buffered or waiting
462 * on a blocked hypervisor. Call this function with hp->lock held.
463 */
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000464static int hvc_push(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 int n;
467
Milton Miller030ffad2005-07-07 17:56:25 -0700468 n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (n <= 0) {
Hendrik Brueckner8c2381a2011-07-05 21:50:18 +0000470 if (n == 0 || n == -EAGAIN) {
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200471 hp->do_wakeup = 1;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000472 return 0;
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* throw away output on error; this happens when
475 there is no session connected to the vterm. */
476 hp->n_outbuf = 0;
477 } else
478 hp->n_outbuf -= n;
479 if (hp->n_outbuf > 0)
480 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
481 else
482 hp->do_wakeup = 1;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000483
484 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200487static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200489 struct hvc_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 unsigned long flags;
491 int rsize, written = 0;
492
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200493 /* This write was probably executed during a tty close. */
494 if (!hp)
495 return -EPIPE;
496
Jiri Slaby0146b692012-04-02 13:54:23 +0200497 /* FIXME what's this (unprotected) check for? */
498 if (hp->port.count <= 0)
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200499 return -EIO;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_lock_irqsave(&hp->lock, flags);
502
503 /* Push pending writes */
504 if (hp->n_outbuf > 0)
505 hvc_push(hp);
506
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000507 while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (rsize > count)
509 rsize = count;
510 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
511 count -= rsize;
512 buf += rsize;
513 hp->n_outbuf += rsize;
514 written += rsize;
515 hvc_push(hp);
516 }
517 spin_unlock_irqrestore(&hp->lock, flags);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * Racy, but harmless, kick thread if there is still pending data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
522 if (hp->n_outbuf)
523 hvc_kick();
524
525 return written;
526}
527
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000528/**
529 * hvc_set_winsz() - Resize the hvc tty terminal window.
530 * @work: work structure.
531 *
532 * The routine shall not be called within an atomic context because it
533 * might sleep.
534 *
535 * Locking: hp->lock
536 */
537static void hvc_set_winsz(struct work_struct *work)
538{
539 struct hvc_struct *hp;
540 unsigned long hvc_flags;
541 struct tty_struct *tty;
542 struct winsize ws;
543
544 hp = container_of(work, struct hvc_struct, tty_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000545
Jiri Slaby85bbc002012-04-02 13:54:22 +0200546 tty = tty_port_tty_get(&hp->port);
547 if (!tty)
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000548 return;
Jiri Slaby85bbc002012-04-02 13:54:22 +0200549
550 spin_lock_irqsave(&hp->lock, hvc_flags);
551 ws = hp->ws;
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000552 spin_unlock_irqrestore(&hp->lock, hvc_flags);
553
Alan Coxfc6f6232009-01-02 13:43:17 +0000554 tty_do_resize(tty, &ws);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000555 tty_kref_put(tty);
556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * This is actually a contract between the driver and the tty layer outlining
will schmidtc3ea6922007-04-18 00:54:51 +1000560 * how much write room the driver can guarantee will be sent OR BUFFERED. This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * driver MUST honor the return value.
562 */
563static int hvc_write_room(struct tty_struct *tty)
564{
565 struct hvc_struct *hp = tty->driver_data;
566
567 if (!hp)
Alan Coxd83114e2012-09-17 12:03:39 +0100568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000570 return hp->outbuf_size - hp->n_outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573static int hvc_chars_in_buffer(struct tty_struct *tty)
574{
575 struct hvc_struct *hp = tty->driver_data;
576
577 if (!hp)
Alan Cox23198fd2009-07-20 16:05:27 +0100578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return hp->n_outbuf;
580}
581
Will Schmidtb7910722007-04-18 00:44:46 +1000582/*
583 * timeout will vary between the MIN and MAX values defined here. By default
584 * and during console activity we will use a default MIN_TIMEOUT of 10. When
585 * the console is idle, we increase the timeout value on each pass through
586 * msleep until we reach the max. This may be noticeable as a brief (average
587 * one second) delay on the console before the console responds to input when
588 * there has been no input for some time.
589 */
590#define MIN_TIMEOUT (10)
591#define MAX_TIMEOUT (2000)
592static u32 timeout = MIN_TIMEOUT;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#define HVC_POLL_READ 0x00000001
595#define HVC_POLL_WRITE 0x00000002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Christian Borntraeger611e0972008-06-20 15:24:08 +0200597int hvc_poll(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct tty_struct *tty;
600 int i, n, poll_mask = 0;
601 char buf[N_INBUF] __ALIGNED__;
602 unsigned long flags;
603 int read_total = 0;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000604 int written_total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 spin_lock_irqsave(&hp->lock, flags);
607
608 /* Push pending writes */
609 if (hp->n_outbuf > 0)
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000610 written_total = hvc_push(hp);
Michael Ellermanb5374462006-06-07 17:10:03 +1000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Reschedule us if still some write pending */
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000613 if (hp->n_outbuf > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 poll_mask |= HVC_POLL_WRITE;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000615 /* If hvc_push() was not able to write, sleep a few msecs */
616 timeout = (written_total) ? 0 : MIN_TIMEOUT;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* No tty attached, just skip */
Jiri Slaby85bbc002012-04-02 13:54:22 +0200620 tty = tty_port_tty_get(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (tty == NULL)
622 goto bail;
623
624 /* Now check if we can get data (are we throttled ?) */
625 if (test_bit(TTY_THROTTLED, &tty->flags))
626 goto throttled;
627
Christian Borntraeger611e0972008-06-20 15:24:08 +0200628 /* If we aren't notifier driven and aren't throttled, we always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * request a reschedule
630 */
Christian Borntraeger611e0972008-06-20 15:24:08 +0200631 if (!hp->irq_requested)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 poll_mask |= HVC_POLL_READ;
633
634 /* Read data if any */
635 for (;;) {
Jiri Slaby227434f2013-01-03 15:53:01 +0100636 int count = tty_buffer_request_room(&hp->port, N_INBUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* If flip is full, just reschedule a later read */
639 if (count == 0) {
640 poll_mask |= HVC_POLL_READ;
641 break;
642 }
643
Milton Miller030ffad2005-07-07 17:56:25 -0700644 n = hp->ops->get_chars(hp->vtermno, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (n <= 0) {
646 /* Hangup the tty when disconnected from host */
647 if (n == -EPIPE) {
648 spin_unlock_irqrestore(&hp->lock, flags);
649 tty_hangup(tty);
650 spin_lock_irqsave(&hp->lock, flags);
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200651 } else if ( n == -EAGAIN ) {
652 /*
653 * Some back-ends can only ensure a certain min
654 * num of bytes read, which may be > 'count'.
655 * Let the tty clear the flip buff to make room.
656 */
657 poll_mask |= HVC_POLL_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 break;
660 }
661 for (i = 0; i < n; ++i) {
662#ifdef CONFIG_MAGIC_SYSRQ
Chris Metcalf4455b112010-07-06 08:03:16 +0000663 if (hp->index == hvc_console.index) {
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700664 /* Handle the SysRq Hack */
665 /* XXX should support a sequence */
666 if (buf[i] == '\x0f') { /* ^O */
Hendrik Brueckner368c1e32008-12-16 00:09:38 +0000667 /* if ^O is pressed again, reset
668 * sysrq_pressed and flip ^O char */
669 sysrq_pressed = !sysrq_pressed;
670 if (sysrq_pressed)
671 continue;
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700672 } else if (sysrq_pressed) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700673 handle_sysrq(buf[i]);
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700674 sysrq_pressed = 0;
675 continue;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678#endif /* CONFIG_MAGIC_SYSRQ */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100679 tty_insert_flip_char(&hp->port, buf[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 read_total += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 throttled:
685 /* Wakeup write queue if necessary */
686 if (hp->do_wakeup) {
687 hp->do_wakeup = 0;
688 tty_wakeup(tty);
689 }
690 bail:
691 spin_unlock_irqrestore(&hp->lock, flags);
692
Will Schmidtb7910722007-04-18 00:44:46 +1000693 if (read_total) {
694 /* Activity is occurring, so reset the polling backoff value to
695 a minimum for performance. */
696 timeout = MIN_TIMEOUT;
697
Jiri Slaby2e124b42013-01-03 15:53:06 +0100698 tty_flip_buffer_push(&hp->port);
Will Schmidtb7910722007-04-18 00:44:46 +1000699 }
Jiri Slaby85bbc002012-04-02 13:54:22 +0200700 tty_kref_put(tty);
Will Schmidtb7910722007-04-18 00:44:46 +1000701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return poll_mask;
703}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500704EXPORT_SYMBOL_GPL(hvc_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000706/**
Hendrik Brueckner254be492009-08-27 01:45:39 +0000707 * __hvc_resize() - Update terminal window size information.
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000708 * @hp: HVC console pointer
709 * @ws: Terminal window size structure
710 *
711 * Stores the specified window size information in the hvc structure of @hp.
712 * The function schedule the tty resize update.
713 *
714 * Locking: Locking free; the function MUST be called holding hp->lock
715 */
Hendrik Brueckner254be492009-08-27 01:45:39 +0000716void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000717{
Hendrik Brueckner7947cf02008-11-18 01:28:28 +0000718 hp->ws = ws;
719 schedule_work(&hp->tty_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000720}
Hendrik Brueckner254be492009-08-27 01:45:39 +0000721EXPORT_SYMBOL_GPL(__hvc_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/*
724 * This kthread is either polling or interrupt driven. This is determined by
725 * calling hvc_poll() who determines whether a console adapter support
726 * interrupts.
727 */
Adrian Bunk5605d4d2007-07-09 11:37:38 -0700728static int khvcd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 int poll_mask;
731 struct hvc_struct *hp;
732
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700733 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 do {
735 poll_mask = 0;
736 hvc_kicked = 0;
Andrew Mortonb64074e2006-09-16 12:15:38 -0700737 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 wmb();
Michael Ellerman1c8950f2008-05-08 14:27:17 +1000739 if (!cpus_are_in_xmon()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock(&hvc_structs_lock);
741 list_for_each_entry(hp, &hvc_structs, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 poll_mask |= hvc_poll(hp);
743 }
744 spin_unlock(&hvc_structs_lock);
745 } else
746 poll_mask |= HVC_POLL_READ;
747 if (hvc_kicked)
748 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 set_current_state(TASK_INTERRUPTIBLE);
750 if (!hvc_kicked) {
751 if (poll_mask == 0)
752 schedule();
Will Schmidtb7910722007-04-18 00:44:46 +1000753 else {
754 if (timeout < MAX_TIMEOUT)
755 timeout += (timeout >> 6) + 1;
756
757 msleep_interruptible(timeout);
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 __set_current_state(TASK_RUNNING);
761 } while (!kthread_should_stop());
762
763 return 0;
764}
765
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000766static int hvc_tiocmget(struct tty_struct *tty)
767{
768 struct hvc_struct *hp = tty->driver_data;
769
770 if (!hp || !hp->ops->tiocmget)
771 return -EINVAL;
772 return hp->ops->tiocmget(hp);
773}
774
775static int hvc_tiocmset(struct tty_struct *tty,
776 unsigned int set, unsigned int clear)
777{
778 struct hvc_struct *hp = tty->driver_data;
779
780 if (!hp || !hp->ops->tiocmset)
781 return -EINVAL;
782 return hp->ops->tiocmset(hp, set, clear);
783}
784
Anton Blanchard762e77a2011-07-12 19:44:05 +0000785#ifdef CONFIG_CONSOLE_POLL
786int hvc_poll_init(struct tty_driver *driver, int line, char *options)
787{
788 return 0;
789}
790
791static int hvc_poll_get_char(struct tty_driver *driver, int line)
792{
793 struct tty_struct *tty = driver->ttys[0];
794 struct hvc_struct *hp = tty->driver_data;
795 int n;
796 char ch;
797
798 n = hp->ops->get_chars(hp->vtermno, &ch, 1);
799
800 if (n == 0)
801 return NO_POLL_CHAR;
802
803 return ch;
804}
805
806static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
807{
808 struct tty_struct *tty = driver->ttys[0];
809 struct hvc_struct *hp = tty->driver_data;
810 int n;
811
812 do {
813 n = hp->ops->put_chars(hp->vtermno, &ch, 1);
814 } while (n <= 0);
815}
816#endif
817
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700818static const struct tty_operations hvc_ops = {
Jiri Slabybdb498c2012-08-07 21:48:04 +0200819 .install = hvc_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .open = hvc_open,
821 .close = hvc_close,
Jiri Slabybdb498c2012-08-07 21:48:04 +0200822 .cleanup = hvc_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .write = hvc_write,
824 .hangup = hvc_hangup,
825 .unthrottle = hvc_unthrottle,
826 .write_room = hvc_write_room,
827 .chars_in_buffer = hvc_chars_in_buffer,
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000828 .tiocmget = hvc_tiocmget,
829 .tiocmset = hvc_tiocmset,
Anton Blanchard762e77a2011-07-12 19:44:05 +0000830#ifdef CONFIG_CONSOLE_POLL
831 .poll_init = hvc_poll_init,
832 .poll_get_char = hvc_poll_get_char,
833 .poll_put_char = hvc_poll_put_char,
834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835};
836
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200837static const struct tty_port_operations hvc_port_ops = {
838 .destruct = hvc_port_destruct,
839};
840
Amit Shah119ea102010-01-18 03:44:58 +0000841struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
842 const struct hv_ops *ops,
843 int outbuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct hvc_struct *hp;
Milton Miller6f248082005-07-07 17:56:17 -0700846 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700848 /* We wait until a driver actually comes along */
Paul Gortmaker94804de2014-01-14 16:03:37 -0500849 if (atomic_inc_not_zero(&hvc_needs_init)) {
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700850 int err = hvc_init();
851 if (err)
852 return ERR_PTR(err);
853 }
854
Milton Miller2da75822009-01-08 02:14:28 +0000855 hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000856 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!hp)
Milton Milleracad9552005-07-07 17:56:24 -0700858 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Milton Milleracad9552005-07-07 17:56:24 -0700860 hp->vtermno = vtermno;
Christian Borntraeger611e0972008-06-20 15:24:08 +0200861 hp->data = data;
Milton Miller030ffad2005-07-07 17:56:25 -0700862 hp->ops = ops;
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000863 hp->outbuf_size = outbuf_size;
864 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200866 tty_port_init(&hp->port);
867 hp->port.ops = &hvc_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000869 INIT_WORK(&hp->tty_resize, hvc_set_winsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 spin_lock_init(&hp->lock);
871 spin_lock(&hvc_structs_lock);
Milton Miller6f248082005-07-07 17:56:17 -0700872
873 /*
874 * find index to use:
875 * see if this vterm id matches one registered for console.
876 */
Linus Torvalds31555212011-11-06 22:22:16 -0800877 for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200878 if (vtermnos[i] == hp->vtermno &&
879 cons_ops[i] == hp->ops)
Milton Miller6f248082005-07-07 17:56:17 -0700880 break;
881
882 /* no matching slot, just use a counter */
883 if (i >= MAX_NR_HVC_CONSOLES)
884 i = ++last_hvc;
885
886 hp->index = i;
Benjamin Herrenschmidt92057a42012-07-23 21:33:13 +0000887 cons_ops[i] = ops;
888 vtermnos[i] = vtermno;
Milton Miller6f248082005-07-07 17:56:17 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 list_add_tail(&(hp->next), &hvc_structs);
891 spin_unlock(&hvc_structs_lock);
892
Benjamin Herrenschmidt92057a42012-07-23 21:33:13 +0000893 /* check if we need to re-register the kernel console */
894 hvc_check_console(i);
895
Milton Milleracad9552005-07-07 17:56:24 -0700896 return hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500898EXPORT_SYMBOL_GPL(hvc_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Hendrik Brueckner934752d2008-10-13 23:12:52 +0000900int hvc_remove(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct tty_struct *tty;
904
Jiri Slaby85bbc002012-04-02 13:54:22 +0200905 tty = tty_port_tty_get(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Jiri Slaby85bbc002012-04-02 13:54:22 +0200907 spin_lock_irqsave(&hp->lock, flags);
Benjamin Herrenschmidt92057a42012-07-23 21:33:13 +0000908 if (hp->index < MAX_NR_HVC_CONSOLES) {
909 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 vtermnos[hp->index] = -1;
Benjamin Herrenschmidt92057a42012-07-23 21:33:13 +0000911 cons_ops[hp->index] = NULL;
912 console_unlock();
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
916
917 spin_unlock_irqrestore(&hp->lock, flags);
918
919 /*
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800920 * We 'put' the instance that was grabbed when the kref instance
921 * was initialized using kref_init(). Let the last holder of this
Amit Shahe74d0982010-03-12 11:53:15 +0530922 * kref cause it to be removed, which will probably be the tty_vhangup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * below.
924 */
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200925 tty_port_put(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /*
Amit Shahe74d0982010-03-12 11:53:15 +0530928 * This function call will auto chain call hvc_hangup.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 */
Amit Shahe74d0982010-03-12 11:53:15 +0530930 if (tty) {
931 tty_vhangup(tty);
932 tty_kref_put(tty);
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
Amit Shahc0cefeb2009-11-27 20:50:49 +0000936EXPORT_SYMBOL_GPL(hvc_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700938/* Driver initialization: called as soon as someone uses hvc_alloc(). */
939static int hvc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Michael Neuling55aab8c2006-03-25 17:30:00 +1100941 struct tty_driver *drv;
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700942 int err;
Michael Neuling55aab8c2006-03-25 17:30:00 +1100943
Milton Miller5f6d9c02005-07-07 17:56:21 -0700944 /* We need more than hvc_count adapters due to hotplug additions. */
Michael Neuling55aab8c2006-03-25 17:30:00 +1100945 drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700946 if (!drv) {
947 err = -ENOMEM;
948 goto out;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Michael Neuling55aab8c2006-03-25 17:30:00 +1100951 drv->driver_name = "hvc";
952 drv->name = "hvc";
953 drv->major = HVC_MAJOR;
954 drv->minor_start = HVC_MINOR;
955 drv->type = TTY_DRIVER_TYPE_SYSTEM;
956 drv->init_termios = tty_std_termios;
Hendrik Brueckner56ce9e22008-10-13 23:12:49 +0000957 drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
Michael Neuling55aab8c2006-03-25 17:30:00 +1100958 tty_set_operations(drv, &hvc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* Always start the kthread because there can be hotplug vty adapters
961 * added later. */
962 hvc_task = kthread_run(khvcd, NULL, "khvcd");
963 if (IS_ERR(hvc_task)) {
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700964 printk(KERN_ERR "Couldn't create kthread for console.\n");
965 err = PTR_ERR(hvc_task);
966 goto put_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700969 err = tty_register_driver(drv);
970 if (err) {
971 printk(KERN_ERR "Couldn't register hvc console driver\n");
972 goto stop_thread;
973 }
Anton Blanchardef4cbee2005-09-14 14:19:18 -0700974
Milton Miller9fef3d22009-01-08 02:14:18 +0000975 /*
976 * Make sure tty is fully registered before allowing it to be
977 * found by hvc_console_device.
978 */
979 smp_mb();
Michael Neuling55aab8c2006-03-25 17:30:00 +1100980 hvc_driver = drv;
Milton Milleracad9552005-07-07 17:56:24 -0700981 return 0;
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700982
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700983stop_thread:
984 kthread_stop(hvc_task);
985 hvc_task = NULL;
Julia Lawall2571cd6a2008-10-13 10:31:49 +0100986put_tty:
987 put_tty_driver(drv);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700988out:
989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
will schmidtc3ea6922007-04-18 00:54:51 +1000992/* This isn't particularly necessary due to this being a console driver
Milton Miller320da0d2005-07-07 17:56:20 -0700993 * but it is nice to be thorough.
994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static void __exit hvc_exit(void)
996{
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700997 if (hvc_driver) {
998 kthread_stop(hvc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Rusty Russell3e6c6f62007-10-16 23:30:13 -07001000 tty_unregister_driver(hvc_driver);
1001 /* return tty_struct instances allocated in hvc_init(). */
1002 put_tty_driver(hvc_driver);
Chris Metcalf4455b112010-07-06 08:03:16 +00001003 unregister_console(&hvc_console);
Rusty Russell3e6c6f62007-10-16 23:30:13 -07001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006module_exit(hvc_exit);