blob: 1da617f1c5882bb8758086a3332cde5b1c825839 [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 Gortmaker10333062014-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 Gortmaker10333062014-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) {
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800114 kref_get(&hp->kref);
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 Sekiyama7c680292014-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. */
236static void destroy_hvc_struct(struct kref *kref)
237{
238 struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
239 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
Milton Miller837dcfa2005-07-07 17:56:16 -0700252/*
Milton Miller6f248082005-07-07 17:56:17 -0700253 * hvc_instantiate() is an early console discovery method which locates
254 * consoles * prior to the vio subsystem discovering them. Hotplugged
255 * vty adapters do NOT get an hvc_instantiate() callback since they
256 * appear after early console init.
Milton Miller837dcfa2005-07-07 17:56:16 -0700257 */
Rusty Russellb5113062010-01-18 03:44:57 +0000258int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
Milton Miller837dcfa2005-07-07 17:56:16 -0700259{
Milton Miller7805b1b2005-07-07 17:56:23 -0700260 struct hvc_struct *hp;
261
Milton Miller837dcfa2005-07-07 17:56:16 -0700262 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
263 return -1;
264
265 if (vtermnos[index] != -1)
266 return -1;
267
will schmidtc3ea6922007-04-18 00:54:51 +1000268 /* make sure no no tty has been registered in this index */
Milton Miller7805b1b2005-07-07 17:56:23 -0700269 hp = hvc_get_by_index(index);
270 if (hp) {
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800271 kref_put(&hp->kref, destroy_hvc_struct);
Milton Miller7805b1b2005-07-07 17:56:23 -0700272 return -1;
273 }
274
Milton Miller837dcfa2005-07-07 17:56:16 -0700275 vtermnos[index] = vtermno;
Milton Miller030ffad2005-07-07 17:56:25 -0700276 cons_ops[index] = ops;
Milton Miller6f248082005-07-07 17:56:17 -0700277
will schmidtc3ea6922007-04-18 00:54:51 +1000278 /* reserve all indices up to and including this index */
Milton Miller6f248082005-07-07 17:56:17 -0700279 if (last_hvc < index)
280 last_hvc = index;
281
Milton Millerd5ee2572005-07-07 17:56:24 -0700282 /* if this index is what the user requested, then register
283 * now (setup won't fail at this point). It's ok to just
284 * call register again if previously .setup failed.
285 */
Chris Metcalf4455b112010-07-06 08:03:16 +0000286 if (index == hvc_console.index)
287 register_console(&hvc_console);
Milton Millerd5ee2572005-07-07 17:56:24 -0700288
Milton Miller837dcfa2005-07-07 17:56:16 -0700289 return 0;
290}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500291EXPORT_SYMBOL_GPL(hvc_instantiate);
Milton Miller837dcfa2005-07-07 17:56:16 -0700292
293/* Wake the sleeping khvcd */
Christian Borntraeger611e0972008-06-20 15:24:08 +0200294void hvc_kick(void)
Milton Miller837dcfa2005-07-07 17:56:16 -0700295{
296 hvc_kicked = 1;
297 wake_up_process(hvc_task);
298}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500299EXPORT_SYMBOL_GPL(hvc_kick);
Milton Miller837dcfa2005-07-07 17:56:16 -0700300
Milton Miller837dcfa2005-07-07 17:56:16 -0700301static void hvc_unthrottle(struct tty_struct *tty)
302{
303 hvc_kick();
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * The TTY interface won't be used until after the vio layer has exposed the vty
308 * adapter to the kernel.
309 */
310static int hvc_open(struct tty_struct *tty, struct file * filp)
311{
312 struct hvc_struct *hp;
313 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800316 /* Auto increments kref reference if found. */
Olof Johansson87fd7722006-09-07 15:18:08 -0500317 if (!(hp = hvc_get_by_index(tty->index)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 spin_lock_irqsave(&hp->lock, flags);
321 /* Check and then increment for fast path open. */
322 if (hp->count++ > 0) {
Amit Shahe74d0982010-03-12 11:53:15 +0530323 tty_kref_get(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 spin_unlock_irqrestore(&hp->lock, flags);
325 hvc_kick();
326 return 0;
327 } /* else count == 0 */
328
329 tty->driver_data = hp;
Michal Ostrowskifb5c5942006-02-18 09:29:59 -0500330
Amit Shahe74d0982010-03-12 11:53:15 +0530331 hp->tty = tty_kref_get(tty);
Christian Borntraeger611e0972008-06-20 15:24:08 +0200332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 spin_unlock_irqrestore(&hp->lock, flags);
Christian Borntraeger611e0972008-06-20 15:24:08 +0200334
Christian Borntraegerb1b135c2008-08-07 09:18:34 +0200335 if (hp->ops->notifier_add)
336 rc = hp->ops->notifier_add(hp, hp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
Christian Borntraeger611e0972008-06-20 15:24:08 +0200339 * If the notifier fails we return an error. The tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * will call hvc_close() after a failed open but we don't want to clean
341 * up there so we'll clean up here and clear out the previously set
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800342 * tty fields and return the kref reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344 if (rc) {
345 spin_lock_irqsave(&hp->lock, flags);
346 hp->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_unlock_irqrestore(&hp->lock, flags);
Amit Shahe74d0982010-03-12 11:53:15 +0530348 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 tty->driver_data = NULL;
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800350 kref_put(&hp->kref, destroy_hvc_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
352 }
353 /* Force wakeup of the polling thread */
354 hvc_kick();
355
356 return rc;
357}
358
359static void hvc_close(struct tty_struct *tty, struct file * filp)
360{
361 struct hvc_struct *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long flags;
363
364 if (tty_hung_up_p(filp))
365 return;
366
367 /*
368 * No driver_data means that this close was issued after a failed
369 * hvc_open by the tty layer's release_dev() function and we can just
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800370 * exit cleanly because the kref reference wasn't made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
372 if (!tty->driver_data)
373 return;
374
375 hp = tty->driver_data;
Amit Shahe74d0982010-03-12 11:53:15 +0530376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 spin_lock_irqsave(&hp->lock, flags);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (--hp->count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* We are done with the tty pointer now. */
381 hp->tty = NULL;
382 spin_unlock_irqrestore(&hp->lock, flags);
383
Christian Borntraegereef26222008-10-12 21:51:31 +0000384 if (hp->ops->notifier_del)
385 hp->ops->notifier_del(hp, hp->data);
386
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000387 /* cancel pending tty resize work */
388 cancel_work_sync(&hp->tty_resize);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
391 * Chain calls chars_in_buffer() and returns immediately if
392 * there is no buffered data otherwise sleeps on a wait queue
393 * waking periodically to check chars_in_buffer().
394 */
Jiri Slaby0b058352011-08-25 15:12:08 +0200395 tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
397 if (hp->count < 0)
398 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
399 hp->vtermno, hp->count);
400 spin_unlock_irqrestore(&hp->lock, flags);
401 }
402
Amit Shahe74d0982010-03-12 11:53:15 +0530403 tty_kref_put(tty);
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800404 kref_put(&hp->kref, destroy_hvc_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407static void hvc_hangup(struct tty_struct *tty)
408{
409 struct hvc_struct *hp = tty->driver_data;
410 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int temp_open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (!hp)
414 return;
415
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000416 /* cancel pending tty resize work */
417 cancel_work_sync(&hp->tty_resize);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_lock_irqsave(&hp->lock, flags);
420
421 /*
422 * The N_TTY line discipline has problems such that in a close vs
423 * open->hangup case this can be called after the final close so prevent
424 * that from happening for now.
425 */
426 if (hp->count <= 0) {
427 spin_unlock_irqrestore(&hp->lock, flags);
428 return;
429 }
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 temp_open_count = hp->count;
432 hp->count = 0;
433 hp->n_outbuf = 0;
434 hp->tty = NULL;
Christian Borntraeger611e0972008-06-20 15:24:08 +0200435
Christian Borntraegereef26222008-10-12 21:51:31 +0000436 spin_unlock_irqrestore(&hp->lock, flags);
437
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000438 if (hp->ops->notifier_hangup)
Amit Shahe74d0982010-03-12 11:53:15 +0530439 hp->ops->notifier_hangup(hp, hp->data);
Christian Borntraeger611e0972008-06-20 15:24:08 +0200440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 while(temp_open_count) {
442 --temp_open_count;
Amit Shahe74d0982010-03-12 11:53:15 +0530443 tty_kref_put(tty);
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800444 kref_put(&hp->kref, destroy_hvc_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446}
447
448/*
449 * Push buffered characters whether they were just recently buffered or waiting
450 * on a blocked hypervisor. Call this function with hp->lock held.
451 */
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000452static int hvc_push(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 int n;
455
Milton Miller030ffad2005-07-07 17:56:25 -0700456 n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (n <= 0) {
Hendrik Brueckner8c2381a2011-07-05 21:50:18 +0000458 if (n == 0 || n == -EAGAIN) {
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200459 hp->do_wakeup = 1;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000460 return 0;
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* throw away output on error; this happens when
463 there is no session connected to the vterm. */
464 hp->n_outbuf = 0;
465 } else
466 hp->n_outbuf -= n;
467 if (hp->n_outbuf > 0)
468 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
469 else
470 hp->do_wakeup = 1;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000471
472 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200475static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200477 struct hvc_struct *hp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned long flags;
479 int rsize, written = 0;
480
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200481 /* This write was probably executed during a tty close. */
482 if (!hp)
483 return -EPIPE;
484
485 if (hp->count <= 0)
486 return -EIO;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 spin_lock_irqsave(&hp->lock, flags);
489
490 /* Push pending writes */
491 if (hp->n_outbuf > 0)
492 hvc_push(hp);
493
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000494 while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (rsize > count)
496 rsize = count;
497 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
498 count -= rsize;
499 buf += rsize;
500 hp->n_outbuf += rsize;
501 written += rsize;
502 hvc_push(hp);
503 }
504 spin_unlock_irqrestore(&hp->lock, flags);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Racy, but harmless, kick thread if there is still pending data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
509 if (hp->n_outbuf)
510 hvc_kick();
511
512 return written;
513}
514
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000515/**
516 * hvc_set_winsz() - Resize the hvc tty terminal window.
517 * @work: work structure.
518 *
519 * The routine shall not be called within an atomic context because it
520 * might sleep.
521 *
522 * Locking: hp->lock
523 */
524static void hvc_set_winsz(struct work_struct *work)
525{
526 struct hvc_struct *hp;
527 unsigned long hvc_flags;
528 struct tty_struct *tty;
529 struct winsize ws;
530
531 hp = container_of(work, struct hvc_struct, tty_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000532
533 spin_lock_irqsave(&hp->lock, hvc_flags);
534 if (!hp->tty) {
535 spin_unlock_irqrestore(&hp->lock, hvc_flags);
536 return;
537 }
538 ws = hp->ws;
539 tty = tty_kref_get(hp->tty);
540 spin_unlock_irqrestore(&hp->lock, hvc_flags);
541
Alan Coxfc6f6232009-01-02 13:43:17 +0000542 tty_do_resize(tty, &ws);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000543 tty_kref_put(tty);
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * This is actually a contract between the driver and the tty layer outlining
will schmidtc3ea6922007-04-18 00:54:51 +1000548 * how much write room the driver can guarantee will be sent OR BUFFERED. This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * driver MUST honor the return value.
550 */
551static int hvc_write_room(struct tty_struct *tty)
552{
553 struct hvc_struct *hp = tty->driver_data;
554
555 if (!hp)
556 return -1;
557
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000558 return hp->outbuf_size - hp->n_outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561static int hvc_chars_in_buffer(struct tty_struct *tty)
562{
563 struct hvc_struct *hp = tty->driver_data;
564
565 if (!hp)
Alan Cox23198fd2009-07-20 16:05:27 +0100566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return hp->n_outbuf;
568}
569
Will Schmidtb7910722007-04-18 00:44:46 +1000570/*
571 * timeout will vary between the MIN and MAX values defined here. By default
572 * and during console activity we will use a default MIN_TIMEOUT of 10. When
573 * the console is idle, we increase the timeout value on each pass through
574 * msleep until we reach the max. This may be noticeable as a brief (average
575 * one second) delay on the console before the console responds to input when
576 * there has been no input for some time.
577 */
578#define MIN_TIMEOUT (10)
579#define MAX_TIMEOUT (2000)
580static u32 timeout = MIN_TIMEOUT;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#define HVC_POLL_READ 0x00000001
583#define HVC_POLL_WRITE 0x00000002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Christian Borntraeger611e0972008-06-20 15:24:08 +0200585int hvc_poll(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct tty_struct *tty;
588 int i, n, poll_mask = 0;
589 char buf[N_INBUF] __ALIGNED__;
590 unsigned long flags;
591 int read_total = 0;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000592 int written_total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 spin_lock_irqsave(&hp->lock, flags);
595
596 /* Push pending writes */
597 if (hp->n_outbuf > 0)
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000598 written_total = hvc_push(hp);
Michael Ellermanb5374462006-06-07 17:10:03 +1000599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* Reschedule us if still some write pending */
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000601 if (hp->n_outbuf > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 poll_mask |= HVC_POLL_WRITE;
Hendrik Brueckner3feebbb2008-10-13 23:12:50 +0000603 /* If hvc_push() was not able to write, sleep a few msecs */
604 timeout = (written_total) ? 0 : MIN_TIMEOUT;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* No tty attached, just skip */
Amit Shahe74d0982010-03-12 11:53:15 +0530608 tty = tty_kref_get(hp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (tty == NULL)
610 goto bail;
611
612 /* Now check if we can get data (are we throttled ?) */
613 if (test_bit(TTY_THROTTLED, &tty->flags))
614 goto throttled;
615
Christian Borntraeger611e0972008-06-20 15:24:08 +0200616 /* If we aren't notifier driven and aren't throttled, we always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * request a reschedule
618 */
Christian Borntraeger611e0972008-06-20 15:24:08 +0200619 if (!hp->irq_requested)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 poll_mask |= HVC_POLL_READ;
621
622 /* Read data if any */
623 for (;;) {
Alan Cox33f0f882006-01-09 20:54:13 -0800624 int count = tty_buffer_request_room(tty, N_INBUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* If flip is full, just reschedule a later read */
627 if (count == 0) {
628 poll_mask |= HVC_POLL_READ;
629 break;
630 }
631
Milton Miller030ffad2005-07-07 17:56:25 -0700632 n = hp->ops->get_chars(hp->vtermno, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (n <= 0) {
634 /* Hangup the tty when disconnected from host */
635 if (n == -EPIPE) {
636 spin_unlock_irqrestore(&hp->lock, flags);
637 tty_hangup(tty);
638 spin_lock_irqsave(&hp->lock, flags);
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200639 } else if ( n == -EAGAIN ) {
640 /*
641 * Some back-ends can only ensure a certain min
642 * num of bytes read, which may be > 'count'.
643 * Let the tty clear the flip buff to make room.
644 */
645 poll_mask |= HVC_POLL_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 break;
648 }
649 for (i = 0; i < n; ++i) {
650#ifdef CONFIG_MAGIC_SYSRQ
Chris Metcalf4455b112010-07-06 08:03:16 +0000651 if (hp->index == hvc_console.index) {
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700652 /* Handle the SysRq Hack */
653 /* XXX should support a sequence */
654 if (buf[i] == '\x0f') { /* ^O */
Hendrik Brueckner368c1e32008-12-16 00:09:38 +0000655 /* if ^O is pressed again, reset
656 * sysrq_pressed and flip ^O char */
657 sysrq_pressed = !sysrq_pressed;
658 if (sysrq_pressed)
659 continue;
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700660 } else if (sysrq_pressed) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700661 handle_sysrq(buf[i]);
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700662 sysrq_pressed = 0;
663 continue;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666#endif /* CONFIG_MAGIC_SYSRQ */
667 tty_insert_flip_char(tty, buf[i], 0);
668 }
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 read_total += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 throttled:
673 /* Wakeup write queue if necessary */
674 if (hp->do_wakeup) {
675 hp->do_wakeup = 0;
676 tty_wakeup(tty);
677 }
678 bail:
679 spin_unlock_irqrestore(&hp->lock, flags);
680
Will Schmidtb7910722007-04-18 00:44:46 +1000681 if (read_total) {
682 /* Activity is occurring, so reset the polling backoff value to
683 a minimum for performance. */
684 timeout = MIN_TIMEOUT;
685
Michal Ostrowskifb5c5942006-02-18 09:29:59 -0500686 tty_flip_buffer_push(tty);
Will Schmidtb7910722007-04-18 00:44:46 +1000687 }
Amit Shahe74d0982010-03-12 11:53:15 +0530688 if (tty)
689 tty_kref_put(tty);
Will Schmidtb7910722007-04-18 00:44:46 +1000690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return poll_mask;
692}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500693EXPORT_SYMBOL_GPL(hvc_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000695/**
Hendrik Brueckner254be492009-08-27 01:45:39 +0000696 * __hvc_resize() - Update terminal window size information.
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000697 * @hp: HVC console pointer
698 * @ws: Terminal window size structure
699 *
700 * Stores the specified window size information in the hvc structure of @hp.
701 * The function schedule the tty resize update.
702 *
703 * Locking: Locking free; the function MUST be called holding hp->lock
704 */
Hendrik Brueckner254be492009-08-27 01:45:39 +0000705void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000706{
Hendrik Brueckner7947cf02008-11-18 01:28:28 +0000707 hp->ws = ws;
708 schedule_work(&hp->tty_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000709}
Hendrik Brueckner254be492009-08-27 01:45:39 +0000710EXPORT_SYMBOL_GPL(__hvc_resize);
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/*
713 * This kthread is either polling or interrupt driven. This is determined by
714 * calling hvc_poll() who determines whether a console adapter support
715 * interrupts.
716 */
Adrian Bunk5605d4d2007-07-09 11:37:38 -0700717static int khvcd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 int poll_mask;
720 struct hvc_struct *hp;
721
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700722 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 do {
724 poll_mask = 0;
725 hvc_kicked = 0;
Andrew Mortonb64074e2006-09-16 12:15:38 -0700726 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 wmb();
Michael Ellerman1c8950f2008-05-08 14:27:17 +1000728 if (!cpus_are_in_xmon()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 spin_lock(&hvc_structs_lock);
730 list_for_each_entry(hp, &hvc_structs, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 poll_mask |= hvc_poll(hp);
732 }
733 spin_unlock(&hvc_structs_lock);
734 } else
735 poll_mask |= HVC_POLL_READ;
736 if (hvc_kicked)
737 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 set_current_state(TASK_INTERRUPTIBLE);
739 if (!hvc_kicked) {
740 if (poll_mask == 0)
741 schedule();
Will Schmidtb7910722007-04-18 00:44:46 +1000742 else {
743 if (timeout < MAX_TIMEOUT)
744 timeout += (timeout >> 6) + 1;
745
746 msleep_interruptible(timeout);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 __set_current_state(TASK_RUNNING);
750 } while (!kthread_should_stop());
751
752 return 0;
753}
754
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000755static int hvc_tiocmget(struct tty_struct *tty)
756{
757 struct hvc_struct *hp = tty->driver_data;
758
759 if (!hp || !hp->ops->tiocmget)
760 return -EINVAL;
761 return hp->ops->tiocmget(hp);
762}
763
764static int hvc_tiocmset(struct tty_struct *tty,
765 unsigned int set, unsigned int clear)
766{
767 struct hvc_struct *hp = tty->driver_data;
768
769 if (!hp || !hp->ops->tiocmset)
770 return -EINVAL;
771 return hp->ops->tiocmset(hp, set, clear);
772}
773
Anton Blanchard762e77a2011-07-12 19:44:05 +0000774#ifdef CONFIG_CONSOLE_POLL
775int hvc_poll_init(struct tty_driver *driver, int line, char *options)
776{
777 return 0;
778}
779
780static int hvc_poll_get_char(struct tty_driver *driver, int line)
781{
782 struct tty_struct *tty = driver->ttys[0];
783 struct hvc_struct *hp = tty->driver_data;
784 int n;
785 char ch;
786
787 n = hp->ops->get_chars(hp->vtermno, &ch, 1);
788
789 if (n == 0)
790 return NO_POLL_CHAR;
791
792 return ch;
793}
794
795static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
796{
797 struct tty_struct *tty = driver->ttys[0];
798 struct hvc_struct *hp = tty->driver_data;
799 int n;
800
801 do {
802 n = hp->ops->put_chars(hp->vtermno, &ch, 1);
803 } while (n <= 0);
804}
805#endif
806
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700807static const struct tty_operations hvc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .open = hvc_open,
809 .close = hvc_close,
810 .write = hvc_write,
811 .hangup = hvc_hangup,
812 .unthrottle = hvc_unthrottle,
813 .write_room = hvc_write_room,
814 .chars_in_buffer = hvc_chars_in_buffer,
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000815 .tiocmget = hvc_tiocmget,
816 .tiocmset = hvc_tiocmset,
Anton Blanchard762e77a2011-07-12 19:44:05 +0000817#ifdef CONFIG_CONSOLE_POLL
818 .poll_init = hvc_poll_init,
819 .poll_get_char = hvc_poll_get_char,
820 .poll_put_char = hvc_poll_put_char,
821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822};
823
Amit Shah119ea102010-01-18 03:44:58 +0000824struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
825 const struct hv_ops *ops,
826 int outbuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct hvc_struct *hp;
Milton Miller6f248082005-07-07 17:56:17 -0700829 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700831 /* We wait until a driver actually comes along */
Paul Gortmaker10333062014-01-14 16:03:37 -0500832 if (atomic_inc_not_zero(&hvc_needs_init)) {
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700833 int err = hvc_init();
834 if (err)
835 return ERR_PTR(err);
836 }
837
Milton Miller2da75822009-01-08 02:14:28 +0000838 hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000839 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!hp)
Milton Milleracad9552005-07-07 17:56:24 -0700841 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Milton Milleracad9552005-07-07 17:56:24 -0700843 hp->vtermno = vtermno;
Christian Borntraeger611e0972008-06-20 15:24:08 +0200844 hp->data = data;
Milton Miller030ffad2005-07-07 17:56:25 -0700845 hp->ops = ops;
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +1000846 hp->outbuf_size = outbuf_size;
847 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800849 kref_init(&hp->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Hendrik Bruecknerfebde372008-10-14 05:02:09 +0000851 INIT_WORK(&hp->tty_resize, hvc_set_winsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_lock_init(&hp->lock);
853 spin_lock(&hvc_structs_lock);
Milton Miller6f248082005-07-07 17:56:17 -0700854
855 /*
856 * find index to use:
857 * see if this vterm id matches one registered for console.
858 */
Linus Torvalds31555212011-11-06 22:22:16 -0800859 for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
Ryan S. Arnold45d607e2006-03-27 21:25:16 +0200860 if (vtermnos[i] == hp->vtermno &&
861 cons_ops[i] == hp->ops)
Milton Miller6f248082005-07-07 17:56:17 -0700862 break;
863
864 /* no matching slot, just use a counter */
865 if (i >= MAX_NR_HVC_CONSOLES)
866 i = ++last_hvc;
867
868 hp->index = i;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 list_add_tail(&(hp->next), &hvc_structs);
871 spin_unlock(&hvc_structs_lock);
872
Milton Milleracad9552005-07-07 17:56:24 -0700873 return hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
Christian Borntraeger7721c492008-07-25 12:06:06 -0500875EXPORT_SYMBOL_GPL(hvc_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Hendrik Brueckner934752d2008-10-13 23:12:52 +0000877int hvc_remove(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct tty_struct *tty;
881
882 spin_lock_irqsave(&hp->lock, flags);
Amit Shahe74d0982010-03-12 11:53:15 +0530883 tty = tty_kref_get(hp->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (hp->index < MAX_NR_HVC_CONSOLES)
886 vtermnos[hp->index] = -1;
887
888 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
889
890 spin_unlock_irqrestore(&hp->lock, flags);
891
892 /*
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800893 * We 'put' the instance that was grabbed when the kref instance
894 * was initialized using kref_init(). Let the last holder of this
Amit Shahe74d0982010-03-12 11:53:15 +0530895 * kref cause it to be removed, which will probably be the tty_vhangup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * below.
897 */
Greg Kroah-Hartman12b20de2007-11-28 10:46:22 -0800898 kref_put(&hp->kref, destroy_hvc_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /*
Amit Shahe74d0982010-03-12 11:53:15 +0530901 * This function call will auto chain call hvc_hangup.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Amit Shahe74d0982010-03-12 11:53:15 +0530903 if (tty) {
904 tty_vhangup(tty);
905 tty_kref_put(tty);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
Amit Shahc0cefeb2009-11-27 20:50:49 +0000909EXPORT_SYMBOL_GPL(hvc_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700911/* Driver initialization: called as soon as someone uses hvc_alloc(). */
912static int hvc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Michael Neuling55aab8c2006-03-25 17:30:00 +1100914 struct tty_driver *drv;
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700915 int err;
Michael Neuling55aab8c2006-03-25 17:30:00 +1100916
Milton Miller5f6d9c02005-07-07 17:56:21 -0700917 /* We need more than hvc_count adapters due to hotplug additions. */
Michael Neuling55aab8c2006-03-25 17:30:00 +1100918 drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700919 if (!drv) {
920 err = -ENOMEM;
921 goto out;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Michael Neuling55aab8c2006-03-25 17:30:00 +1100924 drv->driver_name = "hvc";
925 drv->name = "hvc";
926 drv->major = HVC_MAJOR;
927 drv->minor_start = HVC_MINOR;
928 drv->type = TTY_DRIVER_TYPE_SYSTEM;
929 drv->init_termios = tty_std_termios;
Hendrik Brueckner56ce9e22008-10-13 23:12:49 +0000930 drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
Michael Neuling55aab8c2006-03-25 17:30:00 +1100931 tty_set_operations(drv, &hvc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Always start the kthread because there can be hotplug vty adapters
934 * added later. */
935 hvc_task = kthread_run(khvcd, NULL, "khvcd");
936 if (IS_ERR(hvc_task)) {
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700937 printk(KERN_ERR "Couldn't create kthread for console.\n");
938 err = PTR_ERR(hvc_task);
939 goto put_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700942 err = tty_register_driver(drv);
943 if (err) {
944 printk(KERN_ERR "Couldn't register hvc console driver\n");
945 goto stop_thread;
946 }
Anton Blanchardef4cbee2005-09-14 14:19:18 -0700947
Milton Miller9fef3d22009-01-08 02:14:18 +0000948 /*
949 * Make sure tty is fully registered before allowing it to be
950 * found by hvc_console_device.
951 */
952 smp_mb();
Michael Neuling55aab8c2006-03-25 17:30:00 +1100953 hvc_driver = drv;
Milton Milleracad9552005-07-07 17:56:24 -0700954 return 0;
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700955
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700956stop_thread:
957 kthread_stop(hvc_task);
958 hvc_task = NULL;
Julia Lawall2571cd62008-10-13 10:31:49 +0100959put_tty:
960 put_tty_driver(drv);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700961out:
962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
will schmidtc3ea6922007-04-18 00:54:51 +1000965/* This isn't particularly necessary due to this being a console driver
Milton Miller320da0d2005-07-07 17:56:20 -0700966 * but it is nice to be thorough.
967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static void __exit hvc_exit(void)
969{
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700970 if (hvc_driver) {
971 kthread_stop(hvc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700973 tty_unregister_driver(hvc_driver);
974 /* return tty_struct instances allocated in hvc_init(). */
975 put_tty_driver(hvc_driver);
Chris Metcalf4455b112010-07-06 08:03:16 +0000976 unregister_console(&hvc_console);
Rusty Russell3e6c6f62007-10-16 23:30:13 -0700977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979module_exit(hvc_exit);