blob: 0ff7fda0742f4326113cf82f8a72cb355c840319 [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 Gortmakerf76a1cb2014-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 Gortmakerf76a1cb2014-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 Sekiyama501fed42014-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 Herrenschmidt92057a492012-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 Herrenschmidt92057a492012-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);
Hendrik Brueckner33e745a2013-07-02 17:07:14 +0200368 } else
369 /* We are ready... raise DTR/RTS */
370 if (C_BAUD(tty))
371 if (hp->ops->dtr_rts)
372 hp->ops->dtr_rts(hp, 1);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Force wakeup of the polling thread */
375 hvc_kick();
376
377 return rc;
378}
379
380static void hvc_close(struct tty_struct *tty, struct file * filp)
381{
382 struct hvc_struct *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned long flags;
384
385 if (tty_hung_up_p(filp))
386 return;
387
388 /*
389 * No driver_data means that this close was issued after a failed
390 * hvc_open by the tty layer's release_dev() function and we can just
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800391 * exit cleanly because the kref reference wasn't made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
393 if (!tty->driver_data)
394 return;
395
396 hp = tty->driver_data;
Amit Shahe74d0982010-03-12 11:53:15 +0530397
Jiri Slaby0146b692012-04-02 13:54:23 +0200398 spin_lock_irqsave(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jiri Slaby0146b692012-04-02 13:54:23 +0200400 if (--hp->port.count == 0) {
401 spin_unlock_irqrestore(&hp->port.lock, flags);
Jiri Slaby85bbc002012-04-02 13:54:22 +0200402 /* We are done with the tty pointer now. */
403 tty_port_tty_set(&hp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Hendrik Brueckner33e745a2013-07-02 17:07:14 +0200405 if (C_HUPCL(tty))
406 if (hp->ops->dtr_rts)
407 hp->ops->dtr_rts(hp, 0);
408
Christian Borntraegereef26222008-10-12 21:51:31 +0000409 if (hp->ops->notifier_del)
410 hp->ops->notifier_del(hp, hp->data);
411
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000412 /* cancel pending tty resize work */
413 cancel_work_sync(&hp->tty_resize);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Chain calls chars_in_buffer() and returns immediately if
417 * there is no buffered data otherwise sleeps on a wait queue
418 * waking periodically to check chars_in_buffer().
419 */
Jiri Slaby0b058352011-08-25 15:12:08 +0200420 tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 } else {
Jiri Slaby0146b692012-04-02 13:54:23 +0200422 if (hp->port.count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
Jiri Slaby0146b692012-04-02 13:54:23 +0200424 hp->vtermno, hp->port.count);
425 spin_unlock_irqrestore(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Jiri Slabybdb498c2012-08-07 21:48:04 +0200427}
428
429static void hvc_cleanup(struct tty_struct *tty)
430{
431 struct hvc_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200433 tty_port_put(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static void hvc_hangup(struct tty_struct *tty)
437{
438 struct hvc_struct *hp = tty->driver_data;
439 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!hp)
442 return;
443
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000444 /* cancel pending tty resize work */
445 cancel_work_sync(&hp->tty_resize);
446
Jiri Slaby0146b692012-04-02 13:54:23 +0200447 spin_lock_irqsave(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /*
450 * The N_TTY line discipline has problems such that in a close vs
451 * open->hangup case this can be called after the final close so prevent
452 * that from happening for now.
453 */
Jiri Slaby0146b692012-04-02 13:54:23 +0200454 if (hp->port.count <= 0) {
455 spin_unlock_irqrestore(&hp->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
457 }
458
Jiri Slaby0146b692012-04-02 13:54:23 +0200459 hp->port.count = 0;
460 spin_unlock_irqrestore(&hp->port.lock, flags);
Jiri Slaby85bbc002012-04-02 13:54:22 +0200461 tty_port_tty_set(&hp->port, NULL);
Christian Borntraegereef26222008-10-12 21:51:31 +0000462
Jiri Slaby0146b692012-04-02 13:54:23 +0200463 hp->n_outbuf = 0;
464
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000465 if (hp->ops->notifier_hangup)
Amit Shahe74d0982010-03-12 11:53:15 +0530466 hp->ops->notifier_hangup(hp, hp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
470 * Push buffered characters whether they were just recently buffered or waiting
471 * on a blocked hypervisor. Call this function with hp->lock held.
472 */
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000473static int hvc_push(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int n;
476
Milton Miller030ffad2005-07-07 17:56:25 -0700477 n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (n <= 0) {
Hendrik Brueckner8c2381a2011-07-05 21:50:18 +0000479 if (n == 0 || n == -EAGAIN) {
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200480 hp->do_wakeup = 1;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000481 return 0;
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* throw away output on error; this happens when
484 there is no session connected to the vterm. */
485 hp->n_outbuf = 0;
486 } else
487 hp->n_outbuf -= n;
488 if (hp->n_outbuf > 0)
489 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
490 else
491 hp->do_wakeup = 1;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000492
493 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200496static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200498 struct hvc_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 unsigned long flags;
500 int rsize, written = 0;
501
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200502 /* This write was probably executed during a tty close. */
503 if (!hp)
504 return -EPIPE;
505
Jiri Slaby0146b692012-04-02 13:54:23 +0200506 /* FIXME what's this (unprotected) check for? */
507 if (hp->port.count <= 0)
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200508 return -EIO;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 spin_lock_irqsave(&hp->lock, flags);
511
512 /* Push pending writes */
513 if (hp->n_outbuf > 0)
514 hvc_push(hp);
515
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000516 while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (rsize > count)
518 rsize = count;
519 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
520 count -= rsize;
521 buf += rsize;
522 hp->n_outbuf += rsize;
523 written += rsize;
524 hvc_push(hp);
525 }
526 spin_unlock_irqrestore(&hp->lock, flags);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /*
529 * Racy, but harmless, kick thread if there is still pending data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
531 if (hp->n_outbuf)
532 hvc_kick();
533
534 return written;
535}
536
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000537/**
538 * hvc_set_winsz() - Resize the hvc tty terminal window.
539 * @work: work structure.
540 *
541 * The routine shall not be called within an atomic context because it
542 * might sleep.
543 *
544 * Locking: hp->lock
545 */
546static void hvc_set_winsz(struct work_struct *work)
547{
548 struct hvc_struct *hp;
549 unsigned long hvc_flags;
550 struct tty_struct *tty;
551 struct winsize ws;
552
553 hp = container_of(work, struct hvc_struct, tty_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000554
Jiri Slaby85bbc002012-04-02 13:54:22 +0200555 tty = tty_port_tty_get(&hp->port);
556 if (!tty)
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000557 return;
Jiri Slaby85bbc002012-04-02 13:54:22 +0200558
559 spin_lock_irqsave(&hp->lock, hvc_flags);
560 ws = hp->ws;
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000561 spin_unlock_irqrestore(&hp->lock, hvc_flags);
562
Alan Coxfc6f6232009-01-02 13:43:17 +0000563 tty_do_resize(tty, &ws);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000564 tty_kref_put(tty);
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
568 * This is actually a contract between the driver and the tty layer outlining
will schmidtc3ea6922007-04-18 00:54:51 +1000569 * how much write room the driver can guarantee will be sent OR BUFFERED. This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * driver MUST honor the return value.
571 */
572static int hvc_write_room(struct tty_struct *tty)
573{
574 struct hvc_struct *hp = tty->driver_data;
575
576 if (!hp)
Alan Coxd83114e2012-09-17 12:03:39 +0100577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000579 return hp->outbuf_size - hp->n_outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582static int hvc_chars_in_buffer(struct tty_struct *tty)
583{
584 struct hvc_struct *hp = tty->driver_data;
585
586 if (!hp)
Alan Cox23198fd2009-07-20 16:05:27 +0100587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return hp->n_outbuf;
589}
590
Will Schmidtb7910722007-04-18 00:44:46 +1000591/*
592 * timeout will vary between the MIN and MAX values defined here. By default
593 * and during console activity we will use a default MIN_TIMEOUT of 10. When
594 * the console is idle, we increase the timeout value on each pass through
595 * msleep until we reach the max. This may be noticeable as a brief (average
596 * one second) delay on the console before the console responds to input when
597 * there has been no input for some time.
598 */
599#define MIN_TIMEOUT (10)
600#define MAX_TIMEOUT (2000)
601static u32 timeout = MIN_TIMEOUT;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603#define HVC_POLL_READ 0x00000001
604#define HVC_POLL_WRITE 0x00000002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christian Borntraeger611e0972008-06-20 15:24:08 +0200606int hvc_poll(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct tty_struct *tty;
609 int i, n, poll_mask = 0;
610 char buf[N_INBUF] __ALIGNED__;
611 unsigned long flags;
612 int read_total = 0;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000613 int written_total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 spin_lock_irqsave(&hp->lock, flags);
616
617 /* Push pending writes */
618 if (hp->n_outbuf > 0)
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000619 written_total = hvc_push(hp);
Michael Ellermanb5374462006-06-07 17:10:03 +1000620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Reschedule us if still some write pending */
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000622 if (hp->n_outbuf > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 poll_mask |= HVC_POLL_WRITE;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000624 /* If hvc_push() was not able to write, sleep a few msecs */
625 timeout = (written_total) ? 0 : MIN_TIMEOUT;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* No tty attached, just skip */
Jiri Slaby85bbc002012-04-02 13:54:22 +0200629 tty = tty_port_tty_get(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (tty == NULL)
631 goto bail;
632
633 /* Now check if we can get data (are we throttled ?) */
634 if (test_bit(TTY_THROTTLED, &tty->flags))
635 goto throttled;
636
Christian Borntraeger611e0972008-06-20 15:24:08 +0200637 /* If we aren't notifier driven and aren't throttled, we always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * request a reschedule
639 */
Christian Borntraeger611e0972008-06-20 15:24:08 +0200640 if (!hp->irq_requested)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 poll_mask |= HVC_POLL_READ;
642
643 /* Read data if any */
644 for (;;) {
Jiri Slaby227434f2013-01-03 15:53:01 +0100645 int count = tty_buffer_request_room(&hp->port, N_INBUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* If flip is full, just reschedule a later read */
648 if (count == 0) {
649 poll_mask |= HVC_POLL_READ;
650 break;
651 }
652
Milton Miller030ffad2005-07-07 17:56:25 -0700653 n = hp->ops->get_chars(hp->vtermno, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (n <= 0) {
655 /* Hangup the tty when disconnected from host */
656 if (n == -EPIPE) {
657 spin_unlock_irqrestore(&hp->lock, flags);
658 tty_hangup(tty);
659 spin_lock_irqsave(&hp->lock, flags);
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200660 } else if ( n == -EAGAIN ) {
661 /*
662 * Some back-ends can only ensure a certain min
663 * num of bytes read, which may be > 'count'.
664 * Let the tty clear the flip buff to make room.
665 */
666 poll_mask |= HVC_POLL_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 break;
669 }
670 for (i = 0; i < n; ++i) {
671#ifdef CONFIG_MAGIC_SYSRQ
Chris Metcalf4455b112010-07-06 08:03:16 +0000672 if (hp->index == hvc_console.index) {
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700673 /* Handle the SysRq Hack */
674 /* XXX should support a sequence */
675 if (buf[i] == '\x0f') { /* ^O */
Hendrik Brueckner368c1e32008-12-16 00:09:38 +0000676 /* if ^O is pressed again, reset
677 * sysrq_pressed and flip ^O char */
678 sysrq_pressed = !sysrq_pressed;
679 if (sysrq_pressed)
680 continue;
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700681 } else if (sysrq_pressed) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700682 handle_sysrq(buf[i]);
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700683 sysrq_pressed = 0;
684 continue;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687#endif /* CONFIG_MAGIC_SYSRQ */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100688 tty_insert_flip_char(&hp->port, buf[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 read_total += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 throttled:
694 /* Wakeup write queue if necessary */
695 if (hp->do_wakeup) {
696 hp->do_wakeup = 0;
697 tty_wakeup(tty);
698 }
699 bail:
700 spin_unlock_irqrestore(&hp->lock, flags);
701
Will Schmidtb7910722007-04-18 00:44:46 +1000702 if (read_total) {
703 /* Activity is occurring, so reset the polling backoff value to
704 a minimum for performance. */
705 timeout = MIN_TIMEOUT;
706
Jiri Slaby2e124b42013-01-03 15:53:06 +0100707 tty_flip_buffer_push(&hp->port);
Will Schmidtb7910722007-04-18 00:44:46 +1000708 }
Jiri Slaby85bbc002012-04-02 13:54:22 +0200709 tty_kref_put(tty);
Will Schmidtb7910722007-04-18 00:44:46 +1000710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return poll_mask;
712}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500713EXPORT_SYMBOL_GPL(hvc_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000715/**
Hendrik Brueckner254be492009-08-27 01:45:39 +0000716 * __hvc_resize() - Update terminal window size information.
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000717 * @hp: HVC console pointer
718 * @ws: Terminal window size structure
719 *
720 * Stores the specified window size information in the hvc structure of @hp.
721 * The function schedule the tty resize update.
722 *
723 * Locking: Locking free; the function MUST be called holding hp->lock
724 */
Hendrik Brueckner254be492009-08-27 01:45:39 +0000725void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000726{
Hendrik Brueckner7947cf02008-11-18 01:28:28 +0000727 hp->ws = ws;
728 schedule_work(&hp->tty_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000729}
Hendrik Brueckner254be492009-08-27 01:45:39 +0000730EXPORT_SYMBOL_GPL(__hvc_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * This kthread is either polling or interrupt driven. This is determined by
734 * calling hvc_poll() who determines whether a console adapter support
735 * interrupts.
736 */
Adrian Bunk5605d4d2007-07-09 11:37:38 -0700737static int khvcd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 int poll_mask;
740 struct hvc_struct *hp;
741
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700742 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 do {
744 poll_mask = 0;
745 hvc_kicked = 0;
Andrew Mortonb64074e2006-09-16 12:15:38 -0700746 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 wmb();
Michael Ellerman1c8950f2008-05-08 14:27:17 +1000748 if (!cpus_are_in_xmon()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_lock(&hvc_structs_lock);
750 list_for_each_entry(hp, &hvc_structs, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 poll_mask |= hvc_poll(hp);
752 }
753 spin_unlock(&hvc_structs_lock);
754 } else
755 poll_mask |= HVC_POLL_READ;
756 if (hvc_kicked)
757 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 set_current_state(TASK_INTERRUPTIBLE);
759 if (!hvc_kicked) {
760 if (poll_mask == 0)
761 schedule();
Will Schmidtb7910722007-04-18 00:44:46 +1000762 else {
763 if (timeout < MAX_TIMEOUT)
764 timeout += (timeout >> 6) + 1;
765
766 msleep_interruptible(timeout);
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 __set_current_state(TASK_RUNNING);
770 } while (!kthread_should_stop());
771
772 return 0;
773}
774
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000775static int hvc_tiocmget(struct tty_struct *tty)
776{
777 struct hvc_struct *hp = tty->driver_data;
778
779 if (!hp || !hp->ops->tiocmget)
780 return -EINVAL;
781 return hp->ops->tiocmget(hp);
782}
783
784static int hvc_tiocmset(struct tty_struct *tty,
785 unsigned int set, unsigned int clear)
786{
787 struct hvc_struct *hp = tty->driver_data;
788
789 if (!hp || !hp->ops->tiocmset)
790 return -EINVAL;
791 return hp->ops->tiocmset(hp, set, clear);
792}
793
Anton Blanchard762e77a2011-07-12 19:44:05 +0000794#ifdef CONFIG_CONSOLE_POLL
Rashika Kheria5855b212013-12-16 16:31:28 +0530795static int hvc_poll_init(struct tty_driver *driver, int line, char *options)
Anton Blanchard762e77a2011-07-12 19:44:05 +0000796{
797 return 0;
798}
799
800static int hvc_poll_get_char(struct tty_driver *driver, int line)
801{
802 struct tty_struct *tty = driver->ttys[0];
803 struct hvc_struct *hp = tty->driver_data;
804 int n;
805 char ch;
806
807 n = hp->ops->get_chars(hp->vtermno, &ch, 1);
808
809 if (n == 0)
810 return NO_POLL_CHAR;
811
812 return ch;
813}
814
815static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
816{
817 struct tty_struct *tty = driver->ttys[0];
818 struct hvc_struct *hp = tty->driver_data;
819 int n;
820
821 do {
822 n = hp->ops->put_chars(hp->vtermno, &ch, 1);
823 } while (n <= 0);
824}
825#endif
826
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700827static const struct tty_operations hvc_ops = {
Jiri Slabybdb498c2012-08-07 21:48:04 +0200828 .install = hvc_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 .open = hvc_open,
830 .close = hvc_close,
Jiri Slabybdb498c2012-08-07 21:48:04 +0200831 .cleanup = hvc_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .write = hvc_write,
833 .hangup = hvc_hangup,
834 .unthrottle = hvc_unthrottle,
835 .write_room = hvc_write_room,
836 .chars_in_buffer = hvc_chars_in_buffer,
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000837 .tiocmget = hvc_tiocmget,
838 .tiocmset = hvc_tiocmset,
Anton Blanchard762e77a2011-07-12 19:44:05 +0000839#ifdef CONFIG_CONSOLE_POLL
840 .poll_init = hvc_poll_init,
841 .poll_get_char = hvc_poll_get_char,
842 .poll_put_char = hvc_poll_put_char,
843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844};
845
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200846static const struct tty_port_operations hvc_port_ops = {
847 .destruct = hvc_port_destruct,
848};
849
Amit Shah119ea102010-01-18 03:44:58 +0000850struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
851 const struct hv_ops *ops,
852 int outbuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct hvc_struct *hp;
Milton Miller6f248082005-07-07 17:56:17 -0700855 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700857 /* We wait until a driver actually comes along */
Paul Gortmakerf76a1cb2014-01-14 16:03:37 -0500858 if (atomic_inc_not_zero(&hvc_needs_init)) {
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700859 int err = hvc_init();
860 if (err)
861 return ERR_PTR(err);
862 }
863
Milton Miller2da75822009-01-08 02:14:28 +0000864 hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000865 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!hp)
Milton Milleracad9552005-07-07 17:56:24 -0700867 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Milton Milleracad9552005-07-07 17:56:24 -0700869 hp->vtermno = vtermno;
Christian Borntraeger611e0972008-06-20 15:24:08 +0200870 hp->data = data;
Milton Miller030ffad2005-07-07 17:56:25 -0700871 hp->ops = ops;
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000872 hp->outbuf_size = outbuf_size;
873 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200875 tty_port_init(&hp->port);
876 hp->port.ops = &hvc_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000878 INIT_WORK(&hp->tty_resize, hvc_set_winsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 spin_lock_init(&hp->lock);
880 spin_lock(&hvc_structs_lock);
Milton Miller6f248082005-07-07 17:56:17 -0700881
882 /*
883 * find index to use:
884 * see if this vterm id matches one registered for console.
885 */
Linus Torvalds31555212011-11-06 22:22:16 -0800886 for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200887 if (vtermnos[i] == hp->vtermno &&
888 cons_ops[i] == hp->ops)
Milton Miller6f248082005-07-07 17:56:17 -0700889 break;
890
891 /* no matching slot, just use a counter */
892 if (i >= MAX_NR_HVC_CONSOLES)
893 i = ++last_hvc;
894
895 hp->index = i;
Benjamin Herrenschmidt92057a492012-07-23 21:33:13 +0000896 cons_ops[i] = ops;
897 vtermnos[i] = vtermno;
Milton Miller6f248082005-07-07 17:56:17 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 list_add_tail(&(hp->next), &hvc_structs);
900 spin_unlock(&hvc_structs_lock);
901
Benjamin Herrenschmidt92057a492012-07-23 21:33:13 +0000902 /* check if we need to re-register the kernel console */
903 hvc_check_console(i);
904
Milton Milleracad9552005-07-07 17:56:24 -0700905 return hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500907EXPORT_SYMBOL_GPL(hvc_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Hendrik Brueckner934752d2008-10-13 23:12:52 +0000909int hvc_remove(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct tty_struct *tty;
913
Jiri Slaby85bbc002012-04-02 13:54:22 +0200914 tty = tty_port_tty_get(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Jiri Slaby85bbc002012-04-02 13:54:22 +0200916 spin_lock_irqsave(&hp->lock, flags);
Benjamin Herrenschmidt92057a492012-07-23 21:33:13 +0000917 if (hp->index < MAX_NR_HVC_CONSOLES) {
918 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 vtermnos[hp->index] = -1;
Benjamin Herrenschmidt92057a492012-07-23 21:33:13 +0000920 cons_ops[hp->index] = NULL;
921 console_unlock();
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
925
926 spin_unlock_irqrestore(&hp->lock, flags);
927
928 /*
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800929 * We 'put' the instance that was grabbed when the kref instance
930 * was initialized using kref_init(). Let the last holder of this
Amit Shahe74d0982010-03-12 11:53:15 +0530931 * kref cause it to be removed, which will probably be the tty_vhangup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * below.
933 */
Jiri Slabyf3d9f252012-04-02 13:54:21 +0200934 tty_port_put(&hp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /*
Amit Shahe74d0982010-03-12 11:53:15 +0530937 * This function call will auto chain call hvc_hangup.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
Amit Shahe74d0982010-03-12 11:53:15 +0530939 if (tty) {
940 tty_vhangup(tty);
941 tty_kref_put(tty);
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944}
Amit Shahc0cefeb2009-11-27 20:50:49 +0000945EXPORT_SYMBOL_GPL(hvc_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700947/* Driver initialization: called as soon as someone uses hvc_alloc(). */
948static int hvc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Michael Neuling55aab8c2006-03-25 17:30:00 +1100950 struct tty_driver *drv;
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700951 int err;
Michael Neuling55aab8c2006-03-25 17:30:00 +1100952
Milton Miller5f6d9c02005-07-07 17:56:21 -0700953 /* We need more than hvc_count adapters due to hotplug additions. */
Michael Neuling55aab8c2006-03-25 17:30:00 +1100954 drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700955 if (!drv) {
956 err = -ENOMEM;
957 goto out;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Michael Neuling55aab8c2006-03-25 17:30:00 +1100960 drv->driver_name = "hvc";
961 drv->name = "hvc";
962 drv->major = HVC_MAJOR;
963 drv->minor_start = HVC_MINOR;
964 drv->type = TTY_DRIVER_TYPE_SYSTEM;
965 drv->init_termios = tty_std_termios;
Hendrik Brueckner56ce9e22008-10-13 23:12:49 +0000966 drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
Michael Neuling55aab8c2006-03-25 17:30:00 +1100967 tty_set_operations(drv, &hvc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* Always start the kthread because there can be hotplug vty adapters
970 * added later. */
971 hvc_task = kthread_run(khvcd, NULL, "khvcd");
972 if (IS_ERR(hvc_task)) {
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700973 printk(KERN_ERR "Couldn't create kthread for console.\n");
974 err = PTR_ERR(hvc_task);
975 goto put_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700978 err = tty_register_driver(drv);
979 if (err) {
980 printk(KERN_ERR "Couldn't register hvc console driver\n");
981 goto stop_thread;
982 }
Anton Blanchardef4cbee2005-09-14 14:19:18 -0700983
Milton Miller9fef3d22009-01-08 02:14:18 +0000984 /*
985 * Make sure tty is fully registered before allowing it to be
986 * found by hvc_console_device.
987 */
988 smp_mb();
Michael Neuling55aab8c2006-03-25 17:30:00 +1100989 hvc_driver = drv;
Milton Milleracad9552005-07-07 17:56:24 -0700990 return 0;
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700991
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700992stop_thread:
993 kthread_stop(hvc_task);
994 hvc_task = NULL;
Julia Lawall2571cd6a2008-10-13 10:31:49 +0100995put_tty:
996 put_tty_driver(drv);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700997out:
998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
will schmidtc3ea6922007-04-18 00:54:51 +10001001/* This isn't particularly necessary due to this being a console driver
Milton Miller320da0d2005-07-07 17:56:20 -07001002 * but it is nice to be thorough.
1003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static void __exit hvc_exit(void)
1005{
Rusty Russell3e6c6f62007-10-16 23:30:13 -07001006 if (hvc_driver) {
1007 kthread_stop(hvc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Rusty Russell3e6c6f62007-10-16 23:30:13 -07001009 tty_unregister_driver(hvc_driver);
1010 /* return tty_struct instances allocated in hvc_init(). */
1011 put_tty_driver(hvc_driver);
Chris Metcalf4455b112010-07-06 08:03:16 +00001012 unregister_console(&hvc_console);
Rusty Russell3e6c6f62007-10-16 23:30:13 -07001013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015module_exit(hvc_exit);