1728674a7SGreg Kroah-Hartman /* 2728674a7SGreg Kroah-Hartman * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM 3728674a7SGreg Kroah-Hartman * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM 4728674a7SGreg Kroah-Hartman * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp. 5728674a7SGreg Kroah-Hartman * Copyright (C) 2004 IBM Corporation 6728674a7SGreg Kroah-Hartman * 7728674a7SGreg Kroah-Hartman * Additional Author(s): 8728674a7SGreg Kroah-Hartman * Ryan S. Arnold <rsa@us.ibm.com> 9728674a7SGreg Kroah-Hartman * 10728674a7SGreg Kroah-Hartman * This program is free software; you can redistribute it and/or modify 11728674a7SGreg Kroah-Hartman * it under the terms of the GNU General Public License as published by 12728674a7SGreg Kroah-Hartman * the Free Software Foundation; either version 2 of the License, or 13728674a7SGreg Kroah-Hartman * (at your option) any later version. 14728674a7SGreg Kroah-Hartman * 15728674a7SGreg Kroah-Hartman * This program is distributed in the hope that it will be useful, 16728674a7SGreg Kroah-Hartman * but WITHOUT ANY WARRANTY; without even the implied warranty of 17728674a7SGreg Kroah-Hartman * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18728674a7SGreg Kroah-Hartman * GNU General Public License for more details. 19728674a7SGreg Kroah-Hartman * 20728674a7SGreg Kroah-Hartman * You should have received a copy of the GNU General Public License 21728674a7SGreg Kroah-Hartman * along with this program; if not, write to the Free Software 22728674a7SGreg Kroah-Hartman * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23728674a7SGreg Kroah-Hartman */ 24728674a7SGreg Kroah-Hartman 25728674a7SGreg Kroah-Hartman #include <linux/console.h> 26728674a7SGreg Kroah-Hartman #include <linux/cpumask.h> 27728674a7SGreg Kroah-Hartman #include <linux/init.h> 28728674a7SGreg Kroah-Hartman #include <linux/kbd_kern.h> 29728674a7SGreg Kroah-Hartman #include <linux/kernel.h> 30728674a7SGreg Kroah-Hartman #include <linux/kthread.h> 31728674a7SGreg Kroah-Hartman #include <linux/list.h> 32728674a7SGreg Kroah-Hartman #include <linux/module.h> 33728674a7SGreg Kroah-Hartman #include <linux/major.h> 34728674a7SGreg Kroah-Hartman #include <linux/sysrq.h> 35728674a7SGreg Kroah-Hartman #include <linux/tty.h> 36728674a7SGreg Kroah-Hartman #include <linux/tty_flip.h> 37728674a7SGreg Kroah-Hartman #include <linux/sched.h> 38728674a7SGreg Kroah-Hartman #include <linux/spinlock.h> 39728674a7SGreg Kroah-Hartman #include <linux/delay.h> 40728674a7SGreg Kroah-Hartman #include <linux/freezer.h> 41728674a7SGreg Kroah-Hartman #include <linux/slab.h> 42762e77aeSAnton Blanchard #include <linux/serial_core.h> 43728674a7SGreg Kroah-Hartman 44728674a7SGreg Kroah-Hartman #include <asm/uaccess.h> 45728674a7SGreg Kroah-Hartman 46728674a7SGreg Kroah-Hartman #include "hvc_console.h" 47728674a7SGreg Kroah-Hartman 48728674a7SGreg Kroah-Hartman #define HVC_MAJOR 229 49728674a7SGreg Kroah-Hartman #define HVC_MINOR 0 50728674a7SGreg Kroah-Hartman 51728674a7SGreg Kroah-Hartman /* 52728674a7SGreg Kroah-Hartman * Wait this long per iteration while trying to push buffered data to the 53728674a7SGreg Kroah-Hartman * hypervisor before allowing the tty to complete a close operation. 54728674a7SGreg Kroah-Hartman */ 55728674a7SGreg Kroah-Hartman #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */ 56728674a7SGreg Kroah-Hartman 57728674a7SGreg Kroah-Hartman /* 58728674a7SGreg Kroah-Hartman * These sizes are most efficient for vio, because they are the 59728674a7SGreg Kroah-Hartman * native transfer size. We could make them selectable in the 60728674a7SGreg Kroah-Hartman * future to better deal with backends that want other buffer sizes. 61728674a7SGreg Kroah-Hartman */ 62728674a7SGreg Kroah-Hartman #define N_OUTBUF 16 63728674a7SGreg Kroah-Hartman #define N_INBUF 16 64728674a7SGreg Kroah-Hartman 65728674a7SGreg Kroah-Hartman #define __ALIGNED__ __attribute__((__aligned__(sizeof(long)))) 66728674a7SGreg Kroah-Hartman 67728674a7SGreg Kroah-Hartman static struct tty_driver *hvc_driver; 68728674a7SGreg Kroah-Hartman static struct task_struct *hvc_task; 69728674a7SGreg Kroah-Hartman 70728674a7SGreg Kroah-Hartman /* Picks up late kicks after list walk but before schedule() */ 71728674a7SGreg Kroah-Hartman static int hvc_kicked; 72728674a7SGreg Kroah-Hartman 73728674a7SGreg Kroah-Hartman static int hvc_init(void); 74728674a7SGreg Kroah-Hartman 75728674a7SGreg Kroah-Hartman #ifdef CONFIG_MAGIC_SYSRQ 76728674a7SGreg Kroah-Hartman static int sysrq_pressed; 77728674a7SGreg Kroah-Hartman #endif 78728674a7SGreg Kroah-Hartman 79728674a7SGreg Kroah-Hartman /* dynamic list of hvc_struct instances */ 80728674a7SGreg Kroah-Hartman static LIST_HEAD(hvc_structs); 81728674a7SGreg Kroah-Hartman 82728674a7SGreg Kroah-Hartman /* 83728674a7SGreg Kroah-Hartman * Protect the list of hvc_struct instances from inserts and removals during 84728674a7SGreg Kroah-Hartman * list traversal. 85728674a7SGreg Kroah-Hartman */ 86728674a7SGreg Kroah-Hartman static DEFINE_SPINLOCK(hvc_structs_lock); 87728674a7SGreg Kroah-Hartman 88728674a7SGreg Kroah-Hartman /* 89728674a7SGreg Kroah-Hartman * This value is used to assign a tty->index value to a hvc_struct based 90728674a7SGreg Kroah-Hartman * upon order of exposure via hvc_probe(), when we can not match it to 91728674a7SGreg Kroah-Hartman * a console candidate registered with hvc_instantiate(). 92728674a7SGreg Kroah-Hartman */ 93728674a7SGreg Kroah-Hartman static int last_hvc = -1; 94728674a7SGreg Kroah-Hartman 95728674a7SGreg Kroah-Hartman /* 96728674a7SGreg Kroah-Hartman * Do not call this function with either the hvc_structs_lock or the hvc_struct 97728674a7SGreg Kroah-Hartman * lock held. If successful, this function increments the kref reference 98728674a7SGreg Kroah-Hartman * count against the target hvc_struct so it should be released when finished. 99728674a7SGreg Kroah-Hartman */ 100728674a7SGreg Kroah-Hartman static struct hvc_struct *hvc_get_by_index(int index) 101728674a7SGreg Kroah-Hartman { 102728674a7SGreg Kroah-Hartman struct hvc_struct *hp; 103728674a7SGreg Kroah-Hartman unsigned long flags; 104728674a7SGreg Kroah-Hartman 105728674a7SGreg Kroah-Hartman spin_lock(&hvc_structs_lock); 106728674a7SGreg Kroah-Hartman 107728674a7SGreg Kroah-Hartman list_for_each_entry(hp, &hvc_structs, next) { 108728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags); 109728674a7SGreg Kroah-Hartman if (hp->index == index) { 110f3d9f250SJiri Slaby tty_port_get(&hp->port); 111728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 112728674a7SGreg Kroah-Hartman spin_unlock(&hvc_structs_lock); 113728674a7SGreg Kroah-Hartman return hp; 114728674a7SGreg Kroah-Hartman } 115728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 116728674a7SGreg Kroah-Hartman } 117728674a7SGreg Kroah-Hartman hp = NULL; 118728674a7SGreg Kroah-Hartman 119728674a7SGreg Kroah-Hartman spin_unlock(&hvc_structs_lock); 120728674a7SGreg Kroah-Hartman return hp; 121728674a7SGreg Kroah-Hartman } 122728674a7SGreg Kroah-Hartman 123728674a7SGreg Kroah-Hartman 124728674a7SGreg Kroah-Hartman /* 125728674a7SGreg Kroah-Hartman * Initial console vtermnos for console API usage prior to full console 126728674a7SGreg Kroah-Hartman * initialization. Any vty adapter outside this range will not have usable 127728674a7SGreg Kroah-Hartman * console interfaces but can still be used as a tty device. This has to be 128728674a7SGreg Kroah-Hartman * static because kmalloc will not work during early console init. 129728674a7SGreg Kroah-Hartman */ 130728674a7SGreg Kroah-Hartman static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES]; 131728674a7SGreg Kroah-Hartman static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] = 132728674a7SGreg Kroah-Hartman {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1}; 133728674a7SGreg Kroah-Hartman 134728674a7SGreg Kroah-Hartman /* 135728674a7SGreg Kroah-Hartman * Console APIs, NOT TTY. These APIs are available immediately when 136728674a7SGreg Kroah-Hartman * hvc_console_setup() finds adapters. 137728674a7SGreg Kroah-Hartman */ 138728674a7SGreg Kroah-Hartman 139728674a7SGreg Kroah-Hartman static void hvc_console_print(struct console *co, const char *b, 140728674a7SGreg Kroah-Hartman unsigned count) 141728674a7SGreg Kroah-Hartman { 142728674a7SGreg Kroah-Hartman char c[N_OUTBUF] __ALIGNED__; 143728674a7SGreg Kroah-Hartman unsigned i = 0, n = 0; 144728674a7SGreg Kroah-Hartman int r, donecr = 0, index = co->index; 145728674a7SGreg Kroah-Hartman 146728674a7SGreg Kroah-Hartman /* Console access attempt outside of acceptable console range. */ 147728674a7SGreg Kroah-Hartman if (index >= MAX_NR_HVC_CONSOLES) 148728674a7SGreg Kroah-Hartman return; 149728674a7SGreg Kroah-Hartman 150728674a7SGreg Kroah-Hartman /* This console adapter was removed so it is not usable. */ 151728674a7SGreg Kroah-Hartman if (vtermnos[index] == -1) 152728674a7SGreg Kroah-Hartman return; 153728674a7SGreg Kroah-Hartman 154728674a7SGreg Kroah-Hartman while (count > 0 || i > 0) { 155728674a7SGreg Kroah-Hartman if (count > 0 && i < sizeof(c)) { 156728674a7SGreg Kroah-Hartman if (b[n] == '\n' && !donecr) { 157728674a7SGreg Kroah-Hartman c[i++] = '\r'; 158728674a7SGreg Kroah-Hartman donecr = 1; 159728674a7SGreg Kroah-Hartman } else { 160728674a7SGreg Kroah-Hartman c[i++] = b[n++]; 161728674a7SGreg Kroah-Hartman donecr = 0; 162728674a7SGreg Kroah-Hartman --count; 163728674a7SGreg Kroah-Hartman } 164728674a7SGreg Kroah-Hartman } else { 165728674a7SGreg Kroah-Hartman r = cons_ops[index]->put_chars(vtermnos[index], c, i); 166728674a7SGreg Kroah-Hartman if (r <= 0) { 1678c2381afSHendrik Brueckner /* throw away characters on error 1688c2381afSHendrik Brueckner * but spin in case of -EAGAIN */ 1698c2381afSHendrik Brueckner if (r != -EAGAIN) 170728674a7SGreg Kroah-Hartman i = 0; 171728674a7SGreg Kroah-Hartman } else if (r > 0) { 172728674a7SGreg Kroah-Hartman i -= r; 173728674a7SGreg Kroah-Hartman if (i > 0) 174728674a7SGreg Kroah-Hartman memmove(c, c+r, i); 175728674a7SGreg Kroah-Hartman } 176728674a7SGreg Kroah-Hartman } 177728674a7SGreg Kroah-Hartman } 178728674a7SGreg Kroah-Hartman } 179728674a7SGreg Kroah-Hartman 180728674a7SGreg Kroah-Hartman static struct tty_driver *hvc_console_device(struct console *c, int *index) 181728674a7SGreg Kroah-Hartman { 182728674a7SGreg Kroah-Hartman if (vtermnos[c->index] == -1) 183728674a7SGreg Kroah-Hartman return NULL; 184728674a7SGreg Kroah-Hartman 185728674a7SGreg Kroah-Hartman *index = c->index; 186728674a7SGreg Kroah-Hartman return hvc_driver; 187728674a7SGreg Kroah-Hartman } 188728674a7SGreg Kroah-Hartman 189728674a7SGreg Kroah-Hartman static int __init hvc_console_setup(struct console *co, char *options) 190728674a7SGreg Kroah-Hartman { 191728674a7SGreg Kroah-Hartman if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES) 192728674a7SGreg Kroah-Hartman return -ENODEV; 193728674a7SGreg Kroah-Hartman 194728674a7SGreg Kroah-Hartman if (vtermnos[co->index] == -1) 195728674a7SGreg Kroah-Hartman return -ENODEV; 196728674a7SGreg Kroah-Hartman 197728674a7SGreg Kroah-Hartman return 0; 198728674a7SGreg Kroah-Hartman } 199728674a7SGreg Kroah-Hartman 200728674a7SGreg Kroah-Hartman static struct console hvc_console = { 201728674a7SGreg Kroah-Hartman .name = "hvc", 202728674a7SGreg Kroah-Hartman .write = hvc_console_print, 203728674a7SGreg Kroah-Hartman .device = hvc_console_device, 204728674a7SGreg Kroah-Hartman .setup = hvc_console_setup, 205728674a7SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 206728674a7SGreg Kroah-Hartman .index = -1, 207728674a7SGreg Kroah-Hartman }; 208728674a7SGreg Kroah-Hartman 209728674a7SGreg Kroah-Hartman /* 210728674a7SGreg Kroah-Hartman * Early console initialization. Precedes driver initialization. 211728674a7SGreg Kroah-Hartman * 212728674a7SGreg Kroah-Hartman * (1) we are first, and the user specified another driver 213728674a7SGreg Kroah-Hartman * -- index will remain -1 214728674a7SGreg Kroah-Hartman * (2) we are first and the user specified no driver 215728674a7SGreg Kroah-Hartman * -- index will be set to 0, then we will fail setup. 216728674a7SGreg Kroah-Hartman * (3) we are first and the user specified our driver 217728674a7SGreg Kroah-Hartman * -- index will be set to user specified driver, and we will fail 218728674a7SGreg Kroah-Hartman * (4) we are after driver, and this initcall will register us 219728674a7SGreg Kroah-Hartman * -- if the user didn't specify a driver then the console will match 220728674a7SGreg Kroah-Hartman * 221728674a7SGreg Kroah-Hartman * Note that for cases 2 and 3, we will match later when the io driver 222728674a7SGreg Kroah-Hartman * calls hvc_instantiate() and call register again. 223728674a7SGreg Kroah-Hartman */ 224728674a7SGreg Kroah-Hartman static int __init hvc_console_init(void) 225728674a7SGreg Kroah-Hartman { 226728674a7SGreg Kroah-Hartman register_console(&hvc_console); 227728674a7SGreg Kroah-Hartman return 0; 228728674a7SGreg Kroah-Hartman } 229728674a7SGreg Kroah-Hartman console_initcall(hvc_console_init); 230728674a7SGreg Kroah-Hartman 231728674a7SGreg Kroah-Hartman /* callback when the kboject ref count reaches zero. */ 232f3d9f250SJiri Slaby static void hvc_port_destruct(struct tty_port *port) 233728674a7SGreg Kroah-Hartman { 234f3d9f250SJiri Slaby struct hvc_struct *hp = container_of(port, struct hvc_struct, port); 235728674a7SGreg Kroah-Hartman unsigned long flags; 236728674a7SGreg Kroah-Hartman 237728674a7SGreg Kroah-Hartman spin_lock(&hvc_structs_lock); 238728674a7SGreg Kroah-Hartman 239728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags); 240728674a7SGreg Kroah-Hartman list_del(&(hp->next)); 241728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 242728674a7SGreg Kroah-Hartman 243728674a7SGreg Kroah-Hartman spin_unlock(&hvc_structs_lock); 244728674a7SGreg Kroah-Hartman 245728674a7SGreg Kroah-Hartman kfree(hp); 246728674a7SGreg Kroah-Hartman } 247728674a7SGreg Kroah-Hartman 24892057a49SBenjamin Herrenschmidt static void hvc_check_console(int index) 24992057a49SBenjamin Herrenschmidt { 25092057a49SBenjamin Herrenschmidt /* Already enabled, bail out */ 25192057a49SBenjamin Herrenschmidt if (hvc_console.flags & CON_ENABLED) 25292057a49SBenjamin Herrenschmidt return; 25392057a49SBenjamin Herrenschmidt 25492057a49SBenjamin Herrenschmidt /* If this index is what the user requested, then register 25592057a49SBenjamin Herrenschmidt * now (setup won't fail at this point). It's ok to just 25692057a49SBenjamin Herrenschmidt * call register again if previously .setup failed. 25792057a49SBenjamin Herrenschmidt */ 25892057a49SBenjamin Herrenschmidt if (index == hvc_console.index) 25992057a49SBenjamin Herrenschmidt register_console(&hvc_console); 26092057a49SBenjamin Herrenschmidt } 26192057a49SBenjamin Herrenschmidt 262728674a7SGreg Kroah-Hartman /* 263728674a7SGreg Kroah-Hartman * hvc_instantiate() is an early console discovery method which locates 264728674a7SGreg Kroah-Hartman * consoles * prior to the vio subsystem discovering them. Hotplugged 265728674a7SGreg Kroah-Hartman * vty adapters do NOT get an hvc_instantiate() callback since they 266728674a7SGreg Kroah-Hartman * appear after early console init. 267728674a7SGreg Kroah-Hartman */ 268728674a7SGreg Kroah-Hartman int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops) 269728674a7SGreg Kroah-Hartman { 270728674a7SGreg Kroah-Hartman struct hvc_struct *hp; 271728674a7SGreg Kroah-Hartman 272728674a7SGreg Kroah-Hartman if (index < 0 || index >= MAX_NR_HVC_CONSOLES) 273728674a7SGreg Kroah-Hartman return -1; 274728674a7SGreg Kroah-Hartman 275728674a7SGreg Kroah-Hartman if (vtermnos[index] != -1) 276728674a7SGreg Kroah-Hartman return -1; 277728674a7SGreg Kroah-Hartman 278728674a7SGreg Kroah-Hartman /* make sure no no tty has been registered in this index */ 279728674a7SGreg Kroah-Hartman hp = hvc_get_by_index(index); 280728674a7SGreg Kroah-Hartman if (hp) { 281f3d9f250SJiri Slaby tty_port_put(&hp->port); 282728674a7SGreg Kroah-Hartman return -1; 283728674a7SGreg Kroah-Hartman } 284728674a7SGreg Kroah-Hartman 285728674a7SGreg Kroah-Hartman vtermnos[index] = vtermno; 286728674a7SGreg Kroah-Hartman cons_ops[index] = ops; 287728674a7SGreg Kroah-Hartman 288728674a7SGreg Kroah-Hartman /* reserve all indices up to and including this index */ 289728674a7SGreg Kroah-Hartman if (last_hvc < index) 290728674a7SGreg Kroah-Hartman last_hvc = index; 291728674a7SGreg Kroah-Hartman 29292057a49SBenjamin Herrenschmidt /* check if we need to re-register the kernel console */ 29392057a49SBenjamin Herrenschmidt hvc_check_console(index); 294728674a7SGreg Kroah-Hartman 295728674a7SGreg Kroah-Hartman return 0; 296728674a7SGreg Kroah-Hartman } 297728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_instantiate); 298728674a7SGreg Kroah-Hartman 299728674a7SGreg Kroah-Hartman /* Wake the sleeping khvcd */ 300728674a7SGreg Kroah-Hartman void hvc_kick(void) 301728674a7SGreg Kroah-Hartman { 302728674a7SGreg Kroah-Hartman hvc_kicked = 1; 303728674a7SGreg Kroah-Hartman wake_up_process(hvc_task); 304728674a7SGreg Kroah-Hartman } 305728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_kick); 306728674a7SGreg Kroah-Hartman 307728674a7SGreg Kroah-Hartman static void hvc_unthrottle(struct tty_struct *tty) 308728674a7SGreg Kroah-Hartman { 309728674a7SGreg Kroah-Hartman hvc_kick(); 310728674a7SGreg Kroah-Hartman } 311728674a7SGreg Kroah-Hartman 312bdb498c2SJiri Slaby static int hvc_install(struct tty_driver *driver, struct tty_struct *tty) 313bdb498c2SJiri Slaby { 314bdb498c2SJiri Slaby struct hvc_struct *hp; 315bdb498c2SJiri Slaby int rc; 316bdb498c2SJiri Slaby 317bdb498c2SJiri Slaby /* Auto increments kref reference if found. */ 318bdb498c2SJiri Slaby if (!(hp = hvc_get_by_index(tty->index))) 319bdb498c2SJiri Slaby return -ENODEV; 320bdb498c2SJiri Slaby 321bdb498c2SJiri Slaby tty->driver_data = hp; 322bdb498c2SJiri Slaby 323bdb498c2SJiri Slaby rc = tty_port_install(&hp->port, driver, tty); 324bdb498c2SJiri Slaby if (rc) 325bdb498c2SJiri Slaby tty_port_put(&hp->port); 326bdb498c2SJiri Slaby return rc; 327bdb498c2SJiri Slaby } 328bdb498c2SJiri Slaby 329728674a7SGreg Kroah-Hartman /* 330728674a7SGreg Kroah-Hartman * The TTY interface won't be used until after the vio layer has exposed the vty 331728674a7SGreg Kroah-Hartman * adapter to the kernel. 332728674a7SGreg Kroah-Hartman */ 333728674a7SGreg Kroah-Hartman static int hvc_open(struct tty_struct *tty, struct file * filp) 334728674a7SGreg Kroah-Hartman { 335bdb498c2SJiri Slaby struct hvc_struct *hp = tty->driver_data; 336728674a7SGreg Kroah-Hartman unsigned long flags; 337728674a7SGreg Kroah-Hartman int rc = 0; 338728674a7SGreg Kroah-Hartman 3390146b693SJiri Slaby spin_lock_irqsave(&hp->port.lock, flags); 340728674a7SGreg Kroah-Hartman /* Check and then increment for fast path open. */ 3410146b693SJiri Slaby if (hp->port.count++ > 0) { 3420146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags); 343728674a7SGreg Kroah-Hartman hvc_kick(); 344728674a7SGreg Kroah-Hartman return 0; 345728674a7SGreg Kroah-Hartman } /* else count == 0 */ 3460146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags); 347728674a7SGreg Kroah-Hartman 34885bbc003SJiri Slaby tty_port_tty_set(&hp->port, tty); 34985bbc003SJiri Slaby 350728674a7SGreg Kroah-Hartman if (hp->ops->notifier_add) 351728674a7SGreg Kroah-Hartman rc = hp->ops->notifier_add(hp, hp->data); 352728674a7SGreg Kroah-Hartman 353728674a7SGreg Kroah-Hartman /* 354728674a7SGreg Kroah-Hartman * If the notifier fails we return an error. The tty layer 355728674a7SGreg Kroah-Hartman * will call hvc_close() after a failed open but we don't want to clean 356728674a7SGreg Kroah-Hartman * up there so we'll clean up here and clear out the previously set 357728674a7SGreg Kroah-Hartman * tty fields and return the kref reference. 358728674a7SGreg Kroah-Hartman */ 359728674a7SGreg Kroah-Hartman if (rc) { 36085bbc003SJiri Slaby tty_port_tty_set(&hp->port, NULL); 361728674a7SGreg Kroah-Hartman tty->driver_data = NULL; 362f3d9f250SJiri Slaby tty_port_put(&hp->port); 363728674a7SGreg Kroah-Hartman printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc); 364*33e745a1SHendrik Brueckner } else 365*33e745a1SHendrik Brueckner /* We are ready... raise DTR/RTS */ 366*33e745a1SHendrik Brueckner if (C_BAUD(tty)) 367*33e745a1SHendrik Brueckner if (hp->ops->dtr_rts) 368*33e745a1SHendrik Brueckner hp->ops->dtr_rts(hp, 1); 369*33e745a1SHendrik Brueckner 370728674a7SGreg Kroah-Hartman /* Force wakeup of the polling thread */ 371728674a7SGreg Kroah-Hartman hvc_kick(); 372728674a7SGreg Kroah-Hartman 373728674a7SGreg Kroah-Hartman return rc; 374728674a7SGreg Kroah-Hartman } 375728674a7SGreg Kroah-Hartman 376728674a7SGreg Kroah-Hartman static void hvc_close(struct tty_struct *tty, struct file * filp) 377728674a7SGreg Kroah-Hartman { 378728674a7SGreg Kroah-Hartman struct hvc_struct *hp; 379728674a7SGreg Kroah-Hartman unsigned long flags; 380728674a7SGreg Kroah-Hartman 381728674a7SGreg Kroah-Hartman if (tty_hung_up_p(filp)) 382728674a7SGreg Kroah-Hartman return; 383728674a7SGreg Kroah-Hartman 384728674a7SGreg Kroah-Hartman /* 385728674a7SGreg Kroah-Hartman * No driver_data means that this close was issued after a failed 386728674a7SGreg Kroah-Hartman * hvc_open by the tty layer's release_dev() function and we can just 387728674a7SGreg Kroah-Hartman * exit cleanly because the kref reference wasn't made. 388728674a7SGreg Kroah-Hartman */ 389728674a7SGreg Kroah-Hartman if (!tty->driver_data) 390728674a7SGreg Kroah-Hartman return; 391728674a7SGreg Kroah-Hartman 392728674a7SGreg Kroah-Hartman hp = tty->driver_data; 393728674a7SGreg Kroah-Hartman 3940146b693SJiri Slaby spin_lock_irqsave(&hp->port.lock, flags); 395728674a7SGreg Kroah-Hartman 3960146b693SJiri Slaby if (--hp->port.count == 0) { 3970146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags); 39885bbc003SJiri Slaby /* We are done with the tty pointer now. */ 39985bbc003SJiri Slaby tty_port_tty_set(&hp->port, NULL); 400728674a7SGreg Kroah-Hartman 401*33e745a1SHendrik Brueckner if (C_HUPCL(tty)) 402*33e745a1SHendrik Brueckner if (hp->ops->dtr_rts) 403*33e745a1SHendrik Brueckner hp->ops->dtr_rts(hp, 0); 404*33e745a1SHendrik Brueckner 405728674a7SGreg Kroah-Hartman if (hp->ops->notifier_del) 406728674a7SGreg Kroah-Hartman hp->ops->notifier_del(hp, hp->data); 407728674a7SGreg Kroah-Hartman 408728674a7SGreg Kroah-Hartman /* cancel pending tty resize work */ 409728674a7SGreg Kroah-Hartman cancel_work_sync(&hp->tty_resize); 410728674a7SGreg Kroah-Hartman 411728674a7SGreg Kroah-Hartman /* 412728674a7SGreg Kroah-Hartman * Chain calls chars_in_buffer() and returns immediately if 413728674a7SGreg Kroah-Hartman * there is no buffered data otherwise sleeps on a wait queue 414728674a7SGreg Kroah-Hartman * waking periodically to check chars_in_buffer(). 415728674a7SGreg Kroah-Hartman */ 4160b058353SJiri Slaby tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT); 417728674a7SGreg Kroah-Hartman } else { 4180146b693SJiri Slaby if (hp->port.count < 0) 419728674a7SGreg Kroah-Hartman printk(KERN_ERR "hvc_close %X: oops, count is %d\n", 4200146b693SJiri Slaby hp->vtermno, hp->port.count); 4210146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags); 422728674a7SGreg Kroah-Hartman } 423bdb498c2SJiri Slaby } 424bdb498c2SJiri Slaby 425bdb498c2SJiri Slaby static void hvc_cleanup(struct tty_struct *tty) 426bdb498c2SJiri Slaby { 427bdb498c2SJiri Slaby struct hvc_struct *hp = tty->driver_data; 428728674a7SGreg Kroah-Hartman 429f3d9f250SJiri Slaby tty_port_put(&hp->port); 430728674a7SGreg Kroah-Hartman } 431728674a7SGreg Kroah-Hartman 432728674a7SGreg Kroah-Hartman static void hvc_hangup(struct tty_struct *tty) 433728674a7SGreg Kroah-Hartman { 434728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data; 435728674a7SGreg Kroah-Hartman unsigned long flags; 436728674a7SGreg Kroah-Hartman 437728674a7SGreg Kroah-Hartman if (!hp) 438728674a7SGreg Kroah-Hartman return; 439728674a7SGreg Kroah-Hartman 440728674a7SGreg Kroah-Hartman /* cancel pending tty resize work */ 441728674a7SGreg Kroah-Hartman cancel_work_sync(&hp->tty_resize); 442728674a7SGreg Kroah-Hartman 4430146b693SJiri Slaby spin_lock_irqsave(&hp->port.lock, flags); 444728674a7SGreg Kroah-Hartman 445728674a7SGreg Kroah-Hartman /* 446728674a7SGreg Kroah-Hartman * The N_TTY line discipline has problems such that in a close vs 447728674a7SGreg Kroah-Hartman * open->hangup case this can be called after the final close so prevent 448728674a7SGreg Kroah-Hartman * that from happening for now. 449728674a7SGreg Kroah-Hartman */ 4500146b693SJiri Slaby if (hp->port.count <= 0) { 4510146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags); 452728674a7SGreg Kroah-Hartman return; 453728674a7SGreg Kroah-Hartman } 454728674a7SGreg Kroah-Hartman 4550146b693SJiri Slaby hp->port.count = 0; 4560146b693SJiri Slaby spin_unlock_irqrestore(&hp->port.lock, flags); 45785bbc003SJiri Slaby tty_port_tty_set(&hp->port, NULL); 458728674a7SGreg Kroah-Hartman 4590146b693SJiri Slaby hp->n_outbuf = 0; 4600146b693SJiri Slaby 461728674a7SGreg Kroah-Hartman if (hp->ops->notifier_hangup) 462728674a7SGreg Kroah-Hartman hp->ops->notifier_hangup(hp, hp->data); 463728674a7SGreg Kroah-Hartman } 464728674a7SGreg Kroah-Hartman 465728674a7SGreg Kroah-Hartman /* 466728674a7SGreg Kroah-Hartman * Push buffered characters whether they were just recently buffered or waiting 467728674a7SGreg Kroah-Hartman * on a blocked hypervisor. Call this function with hp->lock held. 468728674a7SGreg Kroah-Hartman */ 469728674a7SGreg Kroah-Hartman static int hvc_push(struct hvc_struct *hp) 470728674a7SGreg Kroah-Hartman { 471728674a7SGreg Kroah-Hartman int n; 472728674a7SGreg Kroah-Hartman 473728674a7SGreg Kroah-Hartman n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf); 474728674a7SGreg Kroah-Hartman if (n <= 0) { 4758c2381afSHendrik Brueckner if (n == 0 || n == -EAGAIN) { 476728674a7SGreg Kroah-Hartman hp->do_wakeup = 1; 477728674a7SGreg Kroah-Hartman return 0; 478728674a7SGreg Kroah-Hartman } 479728674a7SGreg Kroah-Hartman /* throw away output on error; this happens when 480728674a7SGreg Kroah-Hartman there is no session connected to the vterm. */ 481728674a7SGreg Kroah-Hartman hp->n_outbuf = 0; 482728674a7SGreg Kroah-Hartman } else 483728674a7SGreg Kroah-Hartman hp->n_outbuf -= n; 484728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0) 485728674a7SGreg Kroah-Hartman memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf); 486728674a7SGreg Kroah-Hartman else 487728674a7SGreg Kroah-Hartman hp->do_wakeup = 1; 488728674a7SGreg Kroah-Hartman 489728674a7SGreg Kroah-Hartman return n; 490728674a7SGreg Kroah-Hartman } 491728674a7SGreg Kroah-Hartman 492728674a7SGreg Kroah-Hartman static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count) 493728674a7SGreg Kroah-Hartman { 494728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data; 495728674a7SGreg Kroah-Hartman unsigned long flags; 496728674a7SGreg Kroah-Hartman int rsize, written = 0; 497728674a7SGreg Kroah-Hartman 498728674a7SGreg Kroah-Hartman /* This write was probably executed during a tty close. */ 499728674a7SGreg Kroah-Hartman if (!hp) 500728674a7SGreg Kroah-Hartman return -EPIPE; 501728674a7SGreg Kroah-Hartman 5020146b693SJiri Slaby /* FIXME what's this (unprotected) check for? */ 5030146b693SJiri Slaby if (hp->port.count <= 0) 504728674a7SGreg Kroah-Hartman return -EIO; 505728674a7SGreg Kroah-Hartman 506728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags); 507728674a7SGreg Kroah-Hartman 508728674a7SGreg Kroah-Hartman /* Push pending writes */ 509728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0) 510728674a7SGreg Kroah-Hartman hvc_push(hp); 511728674a7SGreg Kroah-Hartman 512728674a7SGreg Kroah-Hartman while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) { 513728674a7SGreg Kroah-Hartman if (rsize > count) 514728674a7SGreg Kroah-Hartman rsize = count; 515728674a7SGreg Kroah-Hartman memcpy(hp->outbuf + hp->n_outbuf, buf, rsize); 516728674a7SGreg Kroah-Hartman count -= rsize; 517728674a7SGreg Kroah-Hartman buf += rsize; 518728674a7SGreg Kroah-Hartman hp->n_outbuf += rsize; 519728674a7SGreg Kroah-Hartman written += rsize; 520728674a7SGreg Kroah-Hartman hvc_push(hp); 521728674a7SGreg Kroah-Hartman } 522728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 523728674a7SGreg Kroah-Hartman 524728674a7SGreg Kroah-Hartman /* 525728674a7SGreg Kroah-Hartman * Racy, but harmless, kick thread if there is still pending data. 526728674a7SGreg Kroah-Hartman */ 527728674a7SGreg Kroah-Hartman if (hp->n_outbuf) 528728674a7SGreg Kroah-Hartman hvc_kick(); 529728674a7SGreg Kroah-Hartman 530728674a7SGreg Kroah-Hartman return written; 531728674a7SGreg Kroah-Hartman } 532728674a7SGreg Kroah-Hartman 533728674a7SGreg Kroah-Hartman /** 534728674a7SGreg Kroah-Hartman * hvc_set_winsz() - Resize the hvc tty terminal window. 535728674a7SGreg Kroah-Hartman * @work: work structure. 536728674a7SGreg Kroah-Hartman * 537728674a7SGreg Kroah-Hartman * The routine shall not be called within an atomic context because it 538728674a7SGreg Kroah-Hartman * might sleep. 539728674a7SGreg Kroah-Hartman * 540728674a7SGreg Kroah-Hartman * Locking: hp->lock 541728674a7SGreg Kroah-Hartman */ 542728674a7SGreg Kroah-Hartman static void hvc_set_winsz(struct work_struct *work) 543728674a7SGreg Kroah-Hartman { 544728674a7SGreg Kroah-Hartman struct hvc_struct *hp; 545728674a7SGreg Kroah-Hartman unsigned long hvc_flags; 546728674a7SGreg Kroah-Hartman struct tty_struct *tty; 547728674a7SGreg Kroah-Hartman struct winsize ws; 548728674a7SGreg Kroah-Hartman 549728674a7SGreg Kroah-Hartman hp = container_of(work, struct hvc_struct, tty_resize); 550728674a7SGreg Kroah-Hartman 55185bbc003SJiri Slaby tty = tty_port_tty_get(&hp->port); 55285bbc003SJiri Slaby if (!tty) 553728674a7SGreg Kroah-Hartman return; 55485bbc003SJiri Slaby 55585bbc003SJiri Slaby spin_lock_irqsave(&hp->lock, hvc_flags); 556728674a7SGreg Kroah-Hartman ws = hp->ws; 557728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, hvc_flags); 558728674a7SGreg Kroah-Hartman 559728674a7SGreg Kroah-Hartman tty_do_resize(tty, &ws); 560728674a7SGreg Kroah-Hartman tty_kref_put(tty); 561728674a7SGreg Kroah-Hartman } 562728674a7SGreg Kroah-Hartman 563728674a7SGreg Kroah-Hartman /* 564728674a7SGreg Kroah-Hartman * This is actually a contract between the driver and the tty layer outlining 565728674a7SGreg Kroah-Hartman * how much write room the driver can guarantee will be sent OR BUFFERED. This 566728674a7SGreg Kroah-Hartman * driver MUST honor the return value. 567728674a7SGreg Kroah-Hartman */ 568728674a7SGreg Kroah-Hartman static int hvc_write_room(struct tty_struct *tty) 569728674a7SGreg Kroah-Hartman { 570728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data; 571728674a7SGreg Kroah-Hartman 572728674a7SGreg Kroah-Hartman if (!hp) 573d83114e9SAlan Cox return 0; 574728674a7SGreg Kroah-Hartman 575728674a7SGreg Kroah-Hartman return hp->outbuf_size - hp->n_outbuf; 576728674a7SGreg Kroah-Hartman } 577728674a7SGreg Kroah-Hartman 578728674a7SGreg Kroah-Hartman static int hvc_chars_in_buffer(struct tty_struct *tty) 579728674a7SGreg Kroah-Hartman { 580728674a7SGreg Kroah-Hartman struct hvc_struct *hp = tty->driver_data; 581728674a7SGreg Kroah-Hartman 582728674a7SGreg Kroah-Hartman if (!hp) 583728674a7SGreg Kroah-Hartman return 0; 584728674a7SGreg Kroah-Hartman return hp->n_outbuf; 585728674a7SGreg Kroah-Hartman } 586728674a7SGreg Kroah-Hartman 587728674a7SGreg Kroah-Hartman /* 588728674a7SGreg Kroah-Hartman * timeout will vary between the MIN and MAX values defined here. By default 589728674a7SGreg Kroah-Hartman * and during console activity we will use a default MIN_TIMEOUT of 10. When 590728674a7SGreg Kroah-Hartman * the console is idle, we increase the timeout value on each pass through 591728674a7SGreg Kroah-Hartman * msleep until we reach the max. This may be noticeable as a brief (average 592728674a7SGreg Kroah-Hartman * one second) delay on the console before the console responds to input when 593728674a7SGreg Kroah-Hartman * there has been no input for some time. 594728674a7SGreg Kroah-Hartman */ 595728674a7SGreg Kroah-Hartman #define MIN_TIMEOUT (10) 596728674a7SGreg Kroah-Hartman #define MAX_TIMEOUT (2000) 597728674a7SGreg Kroah-Hartman static u32 timeout = MIN_TIMEOUT; 598728674a7SGreg Kroah-Hartman 599728674a7SGreg Kroah-Hartman #define HVC_POLL_READ 0x00000001 600728674a7SGreg Kroah-Hartman #define HVC_POLL_WRITE 0x00000002 601728674a7SGreg Kroah-Hartman 602728674a7SGreg Kroah-Hartman int hvc_poll(struct hvc_struct *hp) 603728674a7SGreg Kroah-Hartman { 604728674a7SGreg Kroah-Hartman struct tty_struct *tty; 605728674a7SGreg Kroah-Hartman int i, n, poll_mask = 0; 606728674a7SGreg Kroah-Hartman char buf[N_INBUF] __ALIGNED__; 607728674a7SGreg Kroah-Hartman unsigned long flags; 608728674a7SGreg Kroah-Hartman int read_total = 0; 609728674a7SGreg Kroah-Hartman int written_total = 0; 610728674a7SGreg Kroah-Hartman 611728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags); 612728674a7SGreg Kroah-Hartman 613728674a7SGreg Kroah-Hartman /* Push pending writes */ 614728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0) 615728674a7SGreg Kroah-Hartman written_total = hvc_push(hp); 616728674a7SGreg Kroah-Hartman 617728674a7SGreg Kroah-Hartman /* Reschedule us if still some write pending */ 618728674a7SGreg Kroah-Hartman if (hp->n_outbuf > 0) { 619728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_WRITE; 620728674a7SGreg Kroah-Hartman /* If hvc_push() was not able to write, sleep a few msecs */ 621728674a7SGreg Kroah-Hartman timeout = (written_total) ? 0 : MIN_TIMEOUT; 622728674a7SGreg Kroah-Hartman } 623728674a7SGreg Kroah-Hartman 624728674a7SGreg Kroah-Hartman /* No tty attached, just skip */ 62585bbc003SJiri Slaby tty = tty_port_tty_get(&hp->port); 626728674a7SGreg Kroah-Hartman if (tty == NULL) 627728674a7SGreg Kroah-Hartman goto bail; 628728674a7SGreg Kroah-Hartman 629728674a7SGreg Kroah-Hartman /* Now check if we can get data (are we throttled ?) */ 630728674a7SGreg Kroah-Hartman if (test_bit(TTY_THROTTLED, &tty->flags)) 631728674a7SGreg Kroah-Hartman goto throttled; 632728674a7SGreg Kroah-Hartman 633728674a7SGreg Kroah-Hartman /* If we aren't notifier driven and aren't throttled, we always 634728674a7SGreg Kroah-Hartman * request a reschedule 635728674a7SGreg Kroah-Hartman */ 636728674a7SGreg Kroah-Hartman if (!hp->irq_requested) 637728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ; 638728674a7SGreg Kroah-Hartman 639728674a7SGreg Kroah-Hartman /* Read data if any */ 640728674a7SGreg Kroah-Hartman for (;;) { 641227434f8SJiri Slaby int count = tty_buffer_request_room(&hp->port, N_INBUF); 642728674a7SGreg Kroah-Hartman 643728674a7SGreg Kroah-Hartman /* If flip is full, just reschedule a later read */ 644728674a7SGreg Kroah-Hartman if (count == 0) { 645728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ; 646728674a7SGreg Kroah-Hartman break; 647728674a7SGreg Kroah-Hartman } 648728674a7SGreg Kroah-Hartman 649728674a7SGreg Kroah-Hartman n = hp->ops->get_chars(hp->vtermno, buf, count); 650728674a7SGreg Kroah-Hartman if (n <= 0) { 651728674a7SGreg Kroah-Hartman /* Hangup the tty when disconnected from host */ 652728674a7SGreg Kroah-Hartman if (n == -EPIPE) { 653728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 654728674a7SGreg Kroah-Hartman tty_hangup(tty); 655728674a7SGreg Kroah-Hartman spin_lock_irqsave(&hp->lock, flags); 656728674a7SGreg Kroah-Hartman } else if ( n == -EAGAIN ) { 657728674a7SGreg Kroah-Hartman /* 658728674a7SGreg Kroah-Hartman * Some back-ends can only ensure a certain min 659728674a7SGreg Kroah-Hartman * num of bytes read, which may be > 'count'. 660728674a7SGreg Kroah-Hartman * Let the tty clear the flip buff to make room. 661728674a7SGreg Kroah-Hartman */ 662728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ; 663728674a7SGreg Kroah-Hartman } 664728674a7SGreg Kroah-Hartman break; 665728674a7SGreg Kroah-Hartman } 666728674a7SGreg Kroah-Hartman for (i = 0; i < n; ++i) { 667728674a7SGreg Kroah-Hartman #ifdef CONFIG_MAGIC_SYSRQ 668728674a7SGreg Kroah-Hartman if (hp->index == hvc_console.index) { 669728674a7SGreg Kroah-Hartman /* Handle the SysRq Hack */ 670728674a7SGreg Kroah-Hartman /* XXX should support a sequence */ 671728674a7SGreg Kroah-Hartman if (buf[i] == '\x0f') { /* ^O */ 672728674a7SGreg Kroah-Hartman /* if ^O is pressed again, reset 673728674a7SGreg Kroah-Hartman * sysrq_pressed and flip ^O char */ 674728674a7SGreg Kroah-Hartman sysrq_pressed = !sysrq_pressed; 675728674a7SGreg Kroah-Hartman if (sysrq_pressed) 676728674a7SGreg Kroah-Hartman continue; 677728674a7SGreg Kroah-Hartman } else if (sysrq_pressed) { 678728674a7SGreg Kroah-Hartman handle_sysrq(buf[i]); 679728674a7SGreg Kroah-Hartman sysrq_pressed = 0; 680728674a7SGreg Kroah-Hartman continue; 681728674a7SGreg Kroah-Hartman } 682728674a7SGreg Kroah-Hartman } 683728674a7SGreg Kroah-Hartman #endif /* CONFIG_MAGIC_SYSRQ */ 68492a19f9cSJiri Slaby tty_insert_flip_char(&hp->port, buf[i], 0); 685728674a7SGreg Kroah-Hartman } 686728674a7SGreg Kroah-Hartman 687728674a7SGreg Kroah-Hartman read_total += n; 688728674a7SGreg Kroah-Hartman } 689728674a7SGreg Kroah-Hartman throttled: 690728674a7SGreg Kroah-Hartman /* Wakeup write queue if necessary */ 691728674a7SGreg Kroah-Hartman if (hp->do_wakeup) { 692728674a7SGreg Kroah-Hartman hp->do_wakeup = 0; 693728674a7SGreg Kroah-Hartman tty_wakeup(tty); 694728674a7SGreg Kroah-Hartman } 695728674a7SGreg Kroah-Hartman bail: 696728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 697728674a7SGreg Kroah-Hartman 698728674a7SGreg Kroah-Hartman if (read_total) { 699728674a7SGreg Kroah-Hartman /* Activity is occurring, so reset the polling backoff value to 700728674a7SGreg Kroah-Hartman a minimum for performance. */ 701728674a7SGreg Kroah-Hartman timeout = MIN_TIMEOUT; 702728674a7SGreg Kroah-Hartman 7032e124b4aSJiri Slaby tty_flip_buffer_push(&hp->port); 704728674a7SGreg Kroah-Hartman } 705728674a7SGreg Kroah-Hartman tty_kref_put(tty); 706728674a7SGreg Kroah-Hartman 707728674a7SGreg Kroah-Hartman return poll_mask; 708728674a7SGreg Kroah-Hartman } 709728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_poll); 710728674a7SGreg Kroah-Hartman 711728674a7SGreg Kroah-Hartman /** 712728674a7SGreg Kroah-Hartman * __hvc_resize() - Update terminal window size information. 713728674a7SGreg Kroah-Hartman * @hp: HVC console pointer 714728674a7SGreg Kroah-Hartman * @ws: Terminal window size structure 715728674a7SGreg Kroah-Hartman * 716728674a7SGreg Kroah-Hartman * Stores the specified window size information in the hvc structure of @hp. 717728674a7SGreg Kroah-Hartman * The function schedule the tty resize update. 718728674a7SGreg Kroah-Hartman * 719728674a7SGreg Kroah-Hartman * Locking: Locking free; the function MUST be called holding hp->lock 720728674a7SGreg Kroah-Hartman */ 721728674a7SGreg Kroah-Hartman void __hvc_resize(struct hvc_struct *hp, struct winsize ws) 722728674a7SGreg Kroah-Hartman { 723728674a7SGreg Kroah-Hartman hp->ws = ws; 724728674a7SGreg Kroah-Hartman schedule_work(&hp->tty_resize); 725728674a7SGreg Kroah-Hartman } 726728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(__hvc_resize); 727728674a7SGreg Kroah-Hartman 728728674a7SGreg Kroah-Hartman /* 729728674a7SGreg Kroah-Hartman * This kthread is either polling or interrupt driven. This is determined by 730728674a7SGreg Kroah-Hartman * calling hvc_poll() who determines whether a console adapter support 731728674a7SGreg Kroah-Hartman * interrupts. 732728674a7SGreg Kroah-Hartman */ 733728674a7SGreg Kroah-Hartman static int khvcd(void *unused) 734728674a7SGreg Kroah-Hartman { 735728674a7SGreg Kroah-Hartman int poll_mask; 736728674a7SGreg Kroah-Hartman struct hvc_struct *hp; 737728674a7SGreg Kroah-Hartman 738728674a7SGreg Kroah-Hartman set_freezable(); 739728674a7SGreg Kroah-Hartman do { 740728674a7SGreg Kroah-Hartman poll_mask = 0; 741728674a7SGreg Kroah-Hartman hvc_kicked = 0; 742728674a7SGreg Kroah-Hartman try_to_freeze(); 743728674a7SGreg Kroah-Hartman wmb(); 744728674a7SGreg Kroah-Hartman if (!cpus_are_in_xmon()) { 745728674a7SGreg Kroah-Hartman spin_lock(&hvc_structs_lock); 746728674a7SGreg Kroah-Hartman list_for_each_entry(hp, &hvc_structs, next) { 747728674a7SGreg Kroah-Hartman poll_mask |= hvc_poll(hp); 748728674a7SGreg Kroah-Hartman } 749728674a7SGreg Kroah-Hartman spin_unlock(&hvc_structs_lock); 750728674a7SGreg Kroah-Hartman } else 751728674a7SGreg Kroah-Hartman poll_mask |= HVC_POLL_READ; 752728674a7SGreg Kroah-Hartman if (hvc_kicked) 753728674a7SGreg Kroah-Hartman continue; 754728674a7SGreg Kroah-Hartman set_current_state(TASK_INTERRUPTIBLE); 755728674a7SGreg Kroah-Hartman if (!hvc_kicked) { 756728674a7SGreg Kroah-Hartman if (poll_mask == 0) 757728674a7SGreg Kroah-Hartman schedule(); 758728674a7SGreg Kroah-Hartman else { 759728674a7SGreg Kroah-Hartman if (timeout < MAX_TIMEOUT) 760728674a7SGreg Kroah-Hartman timeout += (timeout >> 6) + 1; 761728674a7SGreg Kroah-Hartman 762728674a7SGreg Kroah-Hartman msleep_interruptible(timeout); 763728674a7SGreg Kroah-Hartman } 764728674a7SGreg Kroah-Hartman } 765728674a7SGreg Kroah-Hartman __set_current_state(TASK_RUNNING); 766728674a7SGreg Kroah-Hartman } while (!kthread_should_stop()); 767728674a7SGreg Kroah-Hartman 768728674a7SGreg Kroah-Hartman return 0; 769728674a7SGreg Kroah-Hartman } 770728674a7SGreg Kroah-Hartman 7714d2bb3f5SBenjamin Herrenschmidt static int hvc_tiocmget(struct tty_struct *tty) 7724d2bb3f5SBenjamin Herrenschmidt { 7734d2bb3f5SBenjamin Herrenschmidt struct hvc_struct *hp = tty->driver_data; 7744d2bb3f5SBenjamin Herrenschmidt 7754d2bb3f5SBenjamin Herrenschmidt if (!hp || !hp->ops->tiocmget) 7764d2bb3f5SBenjamin Herrenschmidt return -EINVAL; 7774d2bb3f5SBenjamin Herrenschmidt return hp->ops->tiocmget(hp); 7784d2bb3f5SBenjamin Herrenschmidt } 7794d2bb3f5SBenjamin Herrenschmidt 7804d2bb3f5SBenjamin Herrenschmidt static int hvc_tiocmset(struct tty_struct *tty, 7814d2bb3f5SBenjamin Herrenschmidt unsigned int set, unsigned int clear) 7824d2bb3f5SBenjamin Herrenschmidt { 7834d2bb3f5SBenjamin Herrenschmidt struct hvc_struct *hp = tty->driver_data; 7844d2bb3f5SBenjamin Herrenschmidt 7854d2bb3f5SBenjamin Herrenschmidt if (!hp || !hp->ops->tiocmset) 7864d2bb3f5SBenjamin Herrenschmidt return -EINVAL; 7874d2bb3f5SBenjamin Herrenschmidt return hp->ops->tiocmset(hp, set, clear); 7884d2bb3f5SBenjamin Herrenschmidt } 7894d2bb3f5SBenjamin Herrenschmidt 790762e77aeSAnton Blanchard #ifdef CONFIG_CONSOLE_POLL 791762e77aeSAnton Blanchard int hvc_poll_init(struct tty_driver *driver, int line, char *options) 792762e77aeSAnton Blanchard { 793762e77aeSAnton Blanchard return 0; 794762e77aeSAnton Blanchard } 795762e77aeSAnton Blanchard 796762e77aeSAnton Blanchard static int hvc_poll_get_char(struct tty_driver *driver, int line) 797762e77aeSAnton Blanchard { 798762e77aeSAnton Blanchard struct tty_struct *tty = driver->ttys[0]; 799762e77aeSAnton Blanchard struct hvc_struct *hp = tty->driver_data; 800762e77aeSAnton Blanchard int n; 801762e77aeSAnton Blanchard char ch; 802762e77aeSAnton Blanchard 803762e77aeSAnton Blanchard n = hp->ops->get_chars(hp->vtermno, &ch, 1); 804762e77aeSAnton Blanchard 805762e77aeSAnton Blanchard if (n == 0) 806762e77aeSAnton Blanchard return NO_POLL_CHAR; 807762e77aeSAnton Blanchard 808762e77aeSAnton Blanchard return ch; 809762e77aeSAnton Blanchard } 810762e77aeSAnton Blanchard 811762e77aeSAnton Blanchard static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch) 812762e77aeSAnton Blanchard { 813762e77aeSAnton Blanchard struct tty_struct *tty = driver->ttys[0]; 814762e77aeSAnton Blanchard struct hvc_struct *hp = tty->driver_data; 815762e77aeSAnton Blanchard int n; 816762e77aeSAnton Blanchard 817762e77aeSAnton Blanchard do { 818762e77aeSAnton Blanchard n = hp->ops->put_chars(hp->vtermno, &ch, 1); 819762e77aeSAnton Blanchard } while (n <= 0); 820762e77aeSAnton Blanchard } 821762e77aeSAnton Blanchard #endif 822762e77aeSAnton Blanchard 823728674a7SGreg Kroah-Hartman static const struct tty_operations hvc_ops = { 824bdb498c2SJiri Slaby .install = hvc_install, 825728674a7SGreg Kroah-Hartman .open = hvc_open, 826728674a7SGreg Kroah-Hartman .close = hvc_close, 827bdb498c2SJiri Slaby .cleanup = hvc_cleanup, 828728674a7SGreg Kroah-Hartman .write = hvc_write, 829728674a7SGreg Kroah-Hartman .hangup = hvc_hangup, 830728674a7SGreg Kroah-Hartman .unthrottle = hvc_unthrottle, 831728674a7SGreg Kroah-Hartman .write_room = hvc_write_room, 832728674a7SGreg Kroah-Hartman .chars_in_buffer = hvc_chars_in_buffer, 8334d2bb3f5SBenjamin Herrenschmidt .tiocmget = hvc_tiocmget, 8344d2bb3f5SBenjamin Herrenschmidt .tiocmset = hvc_tiocmset, 835762e77aeSAnton Blanchard #ifdef CONFIG_CONSOLE_POLL 836762e77aeSAnton Blanchard .poll_init = hvc_poll_init, 837762e77aeSAnton Blanchard .poll_get_char = hvc_poll_get_char, 838762e77aeSAnton Blanchard .poll_put_char = hvc_poll_put_char, 839762e77aeSAnton Blanchard #endif 840728674a7SGreg Kroah-Hartman }; 841728674a7SGreg Kroah-Hartman 842f3d9f250SJiri Slaby static const struct tty_port_operations hvc_port_ops = { 843f3d9f250SJiri Slaby .destruct = hvc_port_destruct, 844f3d9f250SJiri Slaby }; 845f3d9f250SJiri Slaby 846728674a7SGreg Kroah-Hartman struct hvc_struct *hvc_alloc(uint32_t vtermno, int data, 847728674a7SGreg Kroah-Hartman const struct hv_ops *ops, 848728674a7SGreg Kroah-Hartman int outbuf_size) 849728674a7SGreg Kroah-Hartman { 850728674a7SGreg Kroah-Hartman struct hvc_struct *hp; 851728674a7SGreg Kroah-Hartman int i; 852728674a7SGreg Kroah-Hartman 853728674a7SGreg Kroah-Hartman /* We wait until a driver actually comes along */ 854728674a7SGreg Kroah-Hartman if (!hvc_driver) { 855728674a7SGreg Kroah-Hartman int err = hvc_init(); 856728674a7SGreg Kroah-Hartman if (err) 857728674a7SGreg Kroah-Hartman return ERR_PTR(err); 858728674a7SGreg Kroah-Hartman } 859728674a7SGreg Kroah-Hartman 860728674a7SGreg Kroah-Hartman hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size, 861728674a7SGreg Kroah-Hartman GFP_KERNEL); 862728674a7SGreg Kroah-Hartman if (!hp) 863728674a7SGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 864728674a7SGreg Kroah-Hartman 865728674a7SGreg Kroah-Hartman hp->vtermno = vtermno; 866728674a7SGreg Kroah-Hartman hp->data = data; 867728674a7SGreg Kroah-Hartman hp->ops = ops; 868728674a7SGreg Kroah-Hartman hp->outbuf_size = outbuf_size; 869728674a7SGreg Kroah-Hartman hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))]; 870728674a7SGreg Kroah-Hartman 871f3d9f250SJiri Slaby tty_port_init(&hp->port); 872f3d9f250SJiri Slaby hp->port.ops = &hvc_port_ops; 873728674a7SGreg Kroah-Hartman 874728674a7SGreg Kroah-Hartman INIT_WORK(&hp->tty_resize, hvc_set_winsz); 875728674a7SGreg Kroah-Hartman spin_lock_init(&hp->lock); 876728674a7SGreg Kroah-Hartman spin_lock(&hvc_structs_lock); 877728674a7SGreg Kroah-Hartman 878728674a7SGreg Kroah-Hartman /* 879728674a7SGreg Kroah-Hartman * find index to use: 880728674a7SGreg Kroah-Hartman * see if this vterm id matches one registered for console. 881728674a7SGreg Kroah-Hartman */ 882728674a7SGreg Kroah-Hartman for (i=0; i < MAX_NR_HVC_CONSOLES; i++) 883728674a7SGreg Kroah-Hartman if (vtermnos[i] == hp->vtermno && 884728674a7SGreg Kroah-Hartman cons_ops[i] == hp->ops) 885728674a7SGreg Kroah-Hartman break; 886728674a7SGreg Kroah-Hartman 887728674a7SGreg Kroah-Hartman /* no matching slot, just use a counter */ 888728674a7SGreg Kroah-Hartman if (i >= MAX_NR_HVC_CONSOLES) 889728674a7SGreg Kroah-Hartman i = ++last_hvc; 890728674a7SGreg Kroah-Hartman 891728674a7SGreg Kroah-Hartman hp->index = i; 89292057a49SBenjamin Herrenschmidt cons_ops[i] = ops; 89392057a49SBenjamin Herrenschmidt vtermnos[i] = vtermno; 894728674a7SGreg Kroah-Hartman 895728674a7SGreg Kroah-Hartman list_add_tail(&(hp->next), &hvc_structs); 896728674a7SGreg Kroah-Hartman spin_unlock(&hvc_structs_lock); 897728674a7SGreg Kroah-Hartman 89892057a49SBenjamin Herrenschmidt /* check if we need to re-register the kernel console */ 89992057a49SBenjamin Herrenschmidt hvc_check_console(i); 90092057a49SBenjamin Herrenschmidt 901728674a7SGreg Kroah-Hartman return hp; 902728674a7SGreg Kroah-Hartman } 903728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_alloc); 904728674a7SGreg Kroah-Hartman 905728674a7SGreg Kroah-Hartman int hvc_remove(struct hvc_struct *hp) 906728674a7SGreg Kroah-Hartman { 907728674a7SGreg Kroah-Hartman unsigned long flags; 908728674a7SGreg Kroah-Hartman struct tty_struct *tty; 909728674a7SGreg Kroah-Hartman 91085bbc003SJiri Slaby tty = tty_port_tty_get(&hp->port); 911728674a7SGreg Kroah-Hartman 91285bbc003SJiri Slaby spin_lock_irqsave(&hp->lock, flags); 91392057a49SBenjamin Herrenschmidt if (hp->index < MAX_NR_HVC_CONSOLES) { 91492057a49SBenjamin Herrenschmidt console_lock(); 915728674a7SGreg Kroah-Hartman vtermnos[hp->index] = -1; 91692057a49SBenjamin Herrenschmidt cons_ops[hp->index] = NULL; 91792057a49SBenjamin Herrenschmidt console_unlock(); 91892057a49SBenjamin Herrenschmidt } 919728674a7SGreg Kroah-Hartman 920728674a7SGreg Kroah-Hartman /* Don't whack hp->irq because tty_hangup() will need to free the irq. */ 921728674a7SGreg Kroah-Hartman 922728674a7SGreg Kroah-Hartman spin_unlock_irqrestore(&hp->lock, flags); 923728674a7SGreg Kroah-Hartman 924728674a7SGreg Kroah-Hartman /* 925728674a7SGreg Kroah-Hartman * We 'put' the instance that was grabbed when the kref instance 926728674a7SGreg Kroah-Hartman * was initialized using kref_init(). Let the last holder of this 927728674a7SGreg Kroah-Hartman * kref cause it to be removed, which will probably be the tty_vhangup 928728674a7SGreg Kroah-Hartman * below. 929728674a7SGreg Kroah-Hartman */ 930f3d9f250SJiri Slaby tty_port_put(&hp->port); 931728674a7SGreg Kroah-Hartman 932728674a7SGreg Kroah-Hartman /* 933728674a7SGreg Kroah-Hartman * This function call will auto chain call hvc_hangup. 934728674a7SGreg Kroah-Hartman */ 935728674a7SGreg Kroah-Hartman if (tty) { 936728674a7SGreg Kroah-Hartman tty_vhangup(tty); 937728674a7SGreg Kroah-Hartman tty_kref_put(tty); 938728674a7SGreg Kroah-Hartman } 939728674a7SGreg Kroah-Hartman return 0; 940728674a7SGreg Kroah-Hartman } 941728674a7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(hvc_remove); 942728674a7SGreg Kroah-Hartman 943728674a7SGreg Kroah-Hartman /* Driver initialization: called as soon as someone uses hvc_alloc(). */ 944728674a7SGreg Kroah-Hartman static int hvc_init(void) 945728674a7SGreg Kroah-Hartman { 946728674a7SGreg Kroah-Hartman struct tty_driver *drv; 947728674a7SGreg Kroah-Hartman int err; 948728674a7SGreg Kroah-Hartman 949728674a7SGreg Kroah-Hartman /* We need more than hvc_count adapters due to hotplug additions. */ 950728674a7SGreg Kroah-Hartman drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS); 951728674a7SGreg Kroah-Hartman if (!drv) { 952728674a7SGreg Kroah-Hartman err = -ENOMEM; 953728674a7SGreg Kroah-Hartman goto out; 954728674a7SGreg Kroah-Hartman } 955728674a7SGreg Kroah-Hartman 956728674a7SGreg Kroah-Hartman drv->driver_name = "hvc"; 957728674a7SGreg Kroah-Hartman drv->name = "hvc"; 958728674a7SGreg Kroah-Hartman drv->major = HVC_MAJOR; 959728674a7SGreg Kroah-Hartman drv->minor_start = HVC_MINOR; 960728674a7SGreg Kroah-Hartman drv->type = TTY_DRIVER_TYPE_SYSTEM; 961728674a7SGreg Kroah-Hartman drv->init_termios = tty_std_termios; 962728674a7SGreg Kroah-Hartman drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS; 963728674a7SGreg Kroah-Hartman tty_set_operations(drv, &hvc_ops); 964728674a7SGreg Kroah-Hartman 965728674a7SGreg Kroah-Hartman /* Always start the kthread because there can be hotplug vty adapters 966728674a7SGreg Kroah-Hartman * added later. */ 967728674a7SGreg Kroah-Hartman hvc_task = kthread_run(khvcd, NULL, "khvcd"); 968728674a7SGreg Kroah-Hartman if (IS_ERR(hvc_task)) { 969728674a7SGreg Kroah-Hartman printk(KERN_ERR "Couldn't create kthread for console.\n"); 970728674a7SGreg Kroah-Hartman err = PTR_ERR(hvc_task); 971728674a7SGreg Kroah-Hartman goto put_tty; 972728674a7SGreg Kroah-Hartman } 973728674a7SGreg Kroah-Hartman 974728674a7SGreg Kroah-Hartman err = tty_register_driver(drv); 975728674a7SGreg Kroah-Hartman if (err) { 976728674a7SGreg Kroah-Hartman printk(KERN_ERR "Couldn't register hvc console driver\n"); 977728674a7SGreg Kroah-Hartman goto stop_thread; 978728674a7SGreg Kroah-Hartman } 979728674a7SGreg Kroah-Hartman 980728674a7SGreg Kroah-Hartman /* 981728674a7SGreg Kroah-Hartman * Make sure tty is fully registered before allowing it to be 982728674a7SGreg Kroah-Hartman * found by hvc_console_device. 983728674a7SGreg Kroah-Hartman */ 984728674a7SGreg Kroah-Hartman smp_mb(); 985728674a7SGreg Kroah-Hartman hvc_driver = drv; 986728674a7SGreg Kroah-Hartman return 0; 987728674a7SGreg Kroah-Hartman 988728674a7SGreg Kroah-Hartman stop_thread: 989728674a7SGreg Kroah-Hartman kthread_stop(hvc_task); 990728674a7SGreg Kroah-Hartman hvc_task = NULL; 991728674a7SGreg Kroah-Hartman put_tty: 992728674a7SGreg Kroah-Hartman put_tty_driver(drv); 993728674a7SGreg Kroah-Hartman out: 994728674a7SGreg Kroah-Hartman return err; 995728674a7SGreg Kroah-Hartman } 996728674a7SGreg Kroah-Hartman 997728674a7SGreg Kroah-Hartman /* This isn't particularly necessary due to this being a console driver 998728674a7SGreg Kroah-Hartman * but it is nice to be thorough. 999728674a7SGreg Kroah-Hartman */ 1000728674a7SGreg Kroah-Hartman static void __exit hvc_exit(void) 1001728674a7SGreg Kroah-Hartman { 1002728674a7SGreg Kroah-Hartman if (hvc_driver) { 1003728674a7SGreg Kroah-Hartman kthread_stop(hvc_task); 1004728674a7SGreg Kroah-Hartman 1005728674a7SGreg Kroah-Hartman tty_unregister_driver(hvc_driver); 1006728674a7SGreg Kroah-Hartman /* return tty_struct instances allocated in hvc_init(). */ 1007728674a7SGreg Kroah-Hartman put_tty_driver(hvc_driver); 1008728674a7SGreg Kroah-Hartman unregister_console(&hvc_console); 1009728674a7SGreg Kroah-Hartman } 1010728674a7SGreg Kroah-Hartman } 1011728674a7SGreg Kroah-Hartman module_exit(hvc_exit); 1012