blob: df282cc9a7abaf288018c53c17b75654480b0474 [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
Milton Millere51d8c92005-07-07 17:56:21 -070025#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/console.h>
27#include <linux/cpumask.h>
28#include <linux/init.h>
29#include <linux/kbd_kern.h>
30#include <linux/kernel.h>
31#include <linux/kobject.h>
32#include <linux/kthread.h>
33#include <linux/list.h>
34#include <linux/module.h>
35#include <linux/major.h>
36#include <linux/sysrq.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39#include <linux/sched.h>
40#include <linux/spinlock.h>
41#include <linux/delay.h>
42#include <asm/uaccess.h>
43#include <asm/hvconsole.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define HVC_MAJOR 229
46#define HVC_MINOR 0
47
48#define TIMEOUT (10)
49
50/*
51 * Wait this long per iteration while trying to push buffered data to the
52 * hypervisor before allowing the tty to complete a close operation.
53 */
54#define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
55
56/*
57 * The Linux TTY code does not support dynamic addition of tty derived devices
58 * so we need to know how many tty devices we might need when space is allocated
59 * for the tty device. Since this driver supports hotplug of vty adapters we
60 * need to make sure we have enough allocated.
61 */
62#define HVC_ALLOC_TTY_ADAPTERS 8
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define N_OUTBUF 16
65#define N_INBUF 16
66
67#define __ALIGNED__ __attribute__((__aligned__(8)))
68
Milton Miller837dcfa2005-07-07 17:56:16 -070069static struct tty_driver *hvc_driver;
70static struct task_struct *hvc_task;
71
72/* Picks up late kicks after list walk but before schedule() */
73static int hvc_kicked;
74
75#ifdef CONFIG_MAGIC_SYSRQ
76static int sysrq_pressed;
77#endif
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079struct hvc_struct {
80 spinlock_t lock;
81 int index;
82 struct tty_struct *tty;
83 unsigned int count;
84 int do_wakeup;
85 char outbuf[N_OUTBUF] __ALIGNED__;
86 int n_outbuf;
87 uint32_t vtermno;
88 int irq_requested;
89 int irq;
90 struct list_head next;
91 struct kobject kobj; /* ref count & hvc_struct lifetime */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94/* dynamic list of hvc_struct instances */
95static struct list_head hvc_structs = LIST_HEAD_INIT(hvc_structs);
96
97/*
98 * Protect the list of hvc_struct instances from inserts and removals during
99 * list traversal.
100 */
101static DEFINE_SPINLOCK(hvc_structs_lock);
102
103/*
Milton Miller6f248082005-07-07 17:56:17 -0700104 * This value is used to assign a tty->index value to a hvc_struct based
105 * upon order of exposure via hvc_probe(), when we can not match it to
106 * a console canidate registered with hvc_instantiate().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
Milton Miller6f248082005-07-07 17:56:17 -0700108static int last_hvc = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * Do not call this function with either the hvc_strucst_lock or the hvc_struct
112 * lock held. If successful, this function increments the kobject reference
113 * count against the target hvc_struct so it should be released when finished.
114 */
115struct hvc_struct *hvc_get_by_index(int index)
116{
117 struct hvc_struct *hp;
118 unsigned long flags;
119
120 spin_lock(&hvc_structs_lock);
121
122 list_for_each_entry(hp, &hvc_structs, next) {
123 spin_lock_irqsave(&hp->lock, flags);
124 if (hp->index == index) {
125 kobject_get(&hp->kobj);
126 spin_unlock_irqrestore(&hp->lock, flags);
127 spin_unlock(&hvc_structs_lock);
128 return hp;
129 }
130 spin_unlock_irqrestore(&hp->lock, flags);
131 }
132 hp = NULL;
133
134 spin_unlock(&hvc_structs_lock);
135 return hp;
136}
137
Milton Miller837dcfa2005-07-07 17:56:16 -0700138
139/*
140 * Initial console vtermnos for console API usage prior to full console
141 * initialization. Any vty adapter outside this range will not have usable
142 * console interfaces but can still be used as a tty device. This has to be
143 * static because kmalloc will not work during early console init.
144 */
Milton Miller64e4da52005-07-07 17:56:22 -0700145static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
146 {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
Milton Miller837dcfa2005-07-07 17:56:16 -0700147
148/*
149 * Console APIs, NOT TTY. These APIs are available immediately when
150 * hvc_console_setup() finds adapters.
151 */
152
153void hvc_console_print(struct console *co, const char *b, unsigned count)
154{
155 char c[16] __ALIGNED__;
156 unsigned i = 0, n = 0;
157 int r, donecr = 0;
158
159 /* Console access attempt outside of acceptable console range. */
160 if (co->index >= MAX_NR_HVC_CONSOLES)
161 return;
162
163 /* This console adapter was removed so it is not useable. */
164 if (vtermnos[co->index] < 0)
165 return;
166
167 while (count > 0 || i > 0) {
168 if (count > 0 && i < sizeof(c)) {
169 if (b[n] == '\n' && !donecr) {
170 c[i++] = '\r';
171 donecr = 1;
172 } else {
173 c[i++] = b[n++];
174 donecr = 0;
175 --count;
176 }
177 } else {
178 r = hvc_put_chars(vtermnos[co->index], c, i);
179 if (r < 0) {
180 /* throw away chars on error */
181 i = 0;
182 } else if (r > 0) {
183 i -= r;
184 if (i > 0)
185 memmove(c, c+r, i);
186 }
187 }
188 }
189}
190
191static struct tty_driver *hvc_console_device(struct console *c, int *index)
192{
Milton Miller7805b1b2005-07-07 17:56:23 -0700193 if (vtermnos[c->index] == -1)
194 return NULL;
195
Milton Miller837dcfa2005-07-07 17:56:16 -0700196 *index = c->index;
197 return hvc_driver;
198}
199
200static int __init hvc_console_setup(struct console *co, char *options)
201{
Milton Miller7805b1b2005-07-07 17:56:23 -0700202 if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
203 return -ENODEV;
204
205 if (vtermnos[co->index] == -1)
206 return -ENODEV;
207
Milton Miller837dcfa2005-07-07 17:56:16 -0700208 return 0;
209}
210
211struct console hvc_con_driver = {
212 .name = "hvc",
213 .write = hvc_console_print,
214 .device = hvc_console_device,
215 .setup = hvc_console_setup,
216 .flags = CON_PRINTBUFFER,
217 .index = -1,
218};
219
Milton Millerd5ee2572005-07-07 17:56:24 -0700220/*
221 * Early console initialization. Preceeds driver initialization.
222 *
223 * (1) we are first, and the user specified another driver
224 * -- index will remain -1
225 * (2) we are first and the user specified no driver
226 * -- index will be set to 0, then we will fail setup.
227 * (3) we are first and the user specified our driver
228 * -- index will be set to user specified driver, and we will fail
229 * (4) we are after driver, and this initcall will register us
230 * -- if the user didn't specify a driver then the console will match
231 *
232 * Note that for cases 2 and 3, we will match later when the io driver
233 * calls hvc_instantiate() and call register again.
234 */
Milton Miller837dcfa2005-07-07 17:56:16 -0700235static int __init hvc_console_init(void)
236{
Milton Miller837dcfa2005-07-07 17:56:16 -0700237 register_console(&hvc_con_driver);
238 return 0;
239}
240console_initcall(hvc_console_init);
241
242/*
Milton Miller6f248082005-07-07 17:56:17 -0700243 * hvc_instantiate() is an early console discovery method which locates
244 * consoles * prior to the vio subsystem discovering them. Hotplugged
245 * vty adapters do NOT get an hvc_instantiate() callback since they
246 * appear after early console init.
Milton Miller837dcfa2005-07-07 17:56:16 -0700247 */
248int hvc_instantiate(uint32_t vtermno, int index)
249{
Milton Miller7805b1b2005-07-07 17:56:23 -0700250 struct hvc_struct *hp;
251
Milton Miller837dcfa2005-07-07 17:56:16 -0700252 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
253 return -1;
254
255 if (vtermnos[index] != -1)
256 return -1;
257
Milton Miller7805b1b2005-07-07 17:56:23 -0700258 /* make sure no no tty has been registerd in this index */
259 hp = hvc_get_by_index(index);
260 if (hp) {
261 kobject_put(&hp->kobj);
262 return -1;
263 }
264
Milton Miller837dcfa2005-07-07 17:56:16 -0700265 vtermnos[index] = vtermno;
Milton Miller6f248082005-07-07 17:56:17 -0700266
267 /* reserve all indices upto and including this index */
268 if (last_hvc < index)
269 last_hvc = index;
270
Milton Millerd5ee2572005-07-07 17:56:24 -0700271 /* if this index is what the user requested, then register
272 * now (setup won't fail at this point). It's ok to just
273 * call register again if previously .setup failed.
274 */
275 if (index == hvc_con_driver.index)
276 register_console(&hvc_con_driver);
277
Milton Miller837dcfa2005-07-07 17:56:16 -0700278 return 0;
279}
Milton Milleracad9552005-07-07 17:56:24 -0700280EXPORT_SYMBOL(hvc_instantiate);
Milton Miller837dcfa2005-07-07 17:56:16 -0700281
282/* Wake the sleeping khvcd */
283static void hvc_kick(void)
284{
285 hvc_kicked = 1;
286 wake_up_process(hvc_task);
287}
288
Milton Miller8b67f8c2005-07-07 17:56:18 -0700289static int hvc_poll(struct hvc_struct *hp);
290
Milton Miller837dcfa2005-07-07 17:56:16 -0700291/*
292 * NOTE: This API isn't used if the console adapter doesn't support interrupts.
293 * In this case the console is poll driven.
294 */
295static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
296{
Milton Miller8b67f8c2005-07-07 17:56:18 -0700297 /* if hvc_poll request a repoll, then kick the hvcd thread */
298 if (hvc_poll(dev_instance))
299 hvc_kick();
Milton Miller837dcfa2005-07-07 17:56:16 -0700300 return IRQ_HANDLED;
301}
302
303static void hvc_unthrottle(struct tty_struct *tty)
304{
305 hvc_kick();
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * The TTY interface won't be used until after the vio layer has exposed the vty
310 * adapter to the kernel.
311 */
312static int hvc_open(struct tty_struct *tty, struct file * filp)
313{
314 struct hvc_struct *hp;
315 unsigned long flags;
316 int irq = NO_IRQ;
317 int rc = 0;
318 struct kobject *kobjp;
319
320 /* Auto increments kobject reference if found. */
321 if (!(hp = hvc_get_by_index(tty->index))) {
322 printk(KERN_WARNING "hvc_console: tty open failed, no vty associated with tty.\n");
323 return -ENODEV;
324 }
325
326 spin_lock_irqsave(&hp->lock, flags);
327 /* Check and then increment for fast path open. */
328 if (hp->count++ > 0) {
329 spin_unlock_irqrestore(&hp->lock, flags);
330 hvc_kick();
331 return 0;
332 } /* else count == 0 */
333
334 tty->driver_data = hp;
335 hp->tty = tty;
336 /* Save for request_irq outside of spin_lock. */
337 irq = hp->irq;
338 if (irq != NO_IRQ)
339 hp->irq_requested = 1;
340
341 kobjp = &hp->kobj;
342
343 spin_unlock_irqrestore(&hp->lock, flags);
344 /* check error, fallback to non-irq */
345 if (irq != NO_IRQ)
346 rc = request_irq(irq, hvc_handle_interrupt, SA_INTERRUPT, "hvc_console", hp);
347
348 /*
349 * If the request_irq() fails and we return an error. The tty layer
350 * will call hvc_close() after a failed open but we don't want to clean
351 * up there so we'll clean up here and clear out the previously set
352 * tty fields and return the kobject reference.
353 */
354 if (rc) {
355 spin_lock_irqsave(&hp->lock, flags);
356 hp->tty = NULL;
357 hp->irq_requested = 0;
358 spin_unlock_irqrestore(&hp->lock, flags);
359 tty->driver_data = NULL;
360 kobject_put(kobjp);
361 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
362 }
363 /* Force wakeup of the polling thread */
364 hvc_kick();
365
366 return rc;
367}
368
369static void hvc_close(struct tty_struct *tty, struct file * filp)
370{
371 struct hvc_struct *hp;
372 struct kobject *kobjp;
373 int irq = NO_IRQ;
374 unsigned long flags;
375
376 if (tty_hung_up_p(filp))
377 return;
378
379 /*
380 * No driver_data means that this close was issued after a failed
381 * hvc_open by the tty layer's release_dev() function and we can just
382 * exit cleanly because the kobject reference wasn't made.
383 */
384 if (!tty->driver_data)
385 return;
386
387 hp = tty->driver_data;
388 spin_lock_irqsave(&hp->lock, flags);
389
390 kobjp = &hp->kobj;
391 if (--hp->count == 0) {
392 if (hp->irq_requested)
393 irq = hp->irq;
394 hp->irq_requested = 0;
395
396 /* We are done with the tty pointer now. */
397 hp->tty = NULL;
398 spin_unlock_irqrestore(&hp->lock, flags);
399
400 /*
401 * Chain calls chars_in_buffer() and returns immediately if
402 * there is no buffered data otherwise sleeps on a wait queue
403 * waking periodically to check chars_in_buffer().
404 */
405 tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
406
407 if (irq != NO_IRQ)
408 free_irq(irq, hp);
409
410 } else {
411 if (hp->count < 0)
412 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
413 hp->vtermno, hp->count);
414 spin_unlock_irqrestore(&hp->lock, flags);
415 }
416
417 kobject_put(kobjp);
418}
419
420static void hvc_hangup(struct tty_struct *tty)
421{
422 struct hvc_struct *hp = tty->driver_data;
423 unsigned long flags;
424 int irq = NO_IRQ;
425 int temp_open_count;
426 struct kobject *kobjp;
427
428 if (!hp)
429 return;
430
431 spin_lock_irqsave(&hp->lock, flags);
432
433 /*
434 * The N_TTY line discipline has problems such that in a close vs
435 * open->hangup case this can be called after the final close so prevent
436 * that from happening for now.
437 */
438 if (hp->count <= 0) {
439 spin_unlock_irqrestore(&hp->lock, flags);
440 return;
441 }
442
443 kobjp = &hp->kobj;
444 temp_open_count = hp->count;
445 hp->count = 0;
446 hp->n_outbuf = 0;
447 hp->tty = NULL;
448 if (hp->irq_requested)
449 /* Saved for use outside of spin_lock. */
450 irq = hp->irq;
451 hp->irq_requested = 0;
452 spin_unlock_irqrestore(&hp->lock, flags);
453 if (irq != NO_IRQ)
454 free_irq(irq, hp);
455 while(temp_open_count) {
456 --temp_open_count;
457 kobject_put(kobjp);
458 }
459}
460
461/*
462 * Push buffered characters whether they were just recently buffered or waiting
463 * on a blocked hypervisor. Call this function with hp->lock held.
464 */
465static void hvc_push(struct hvc_struct *hp)
466{
467 int n;
468
469 n = hvc_put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
470 if (n <= 0) {
471 if (n == 0)
472 return;
473 /* throw away output on error; this happens when
474 there is no session connected to the vterm. */
475 hp->n_outbuf = 0;
476 } else
477 hp->n_outbuf -= n;
478 if (hp->n_outbuf > 0)
479 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
480 else
481 hp->do_wakeup = 1;
482}
483
484static inline int __hvc_write_kernel(struct hvc_struct *hp,
485 const unsigned char *buf, int count)
486{
487 unsigned long flags;
488 int rsize, written = 0;
489
490 spin_lock_irqsave(&hp->lock, flags);
491
492 /* Push pending writes */
493 if (hp->n_outbuf > 0)
494 hvc_push(hp);
495
496 while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
497 if (rsize > count)
498 rsize = count;
499 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
500 count -= rsize;
501 buf += rsize;
502 hp->n_outbuf += rsize;
503 written += rsize;
504 hvc_push(hp);
505 }
506 spin_unlock_irqrestore(&hp->lock, flags);
507
508 return written;
509}
510static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
511{
512 struct hvc_struct *hp = tty->driver_data;
513 int written;
514
515 /* This write was probably executed during a tty close. */
516 if (!hp)
517 return -EPIPE;
518
519 if (hp->count <= 0)
520 return -EIO;
521
522 written = __hvc_write_kernel(hp, buf, count);
523
524 /*
525 * Racy, but harmless, kick thread if there is still pending data.
526 * There really is nothing wrong with kicking the thread, even if there
527 * is no buffered data.
528 */
529 if (hp->n_outbuf)
530 hvc_kick();
531
532 return written;
533}
534
535/*
536 * This is actually a contract between the driver and the tty layer outlining
537 * how much write room the driver can guarentee will be sent OR BUFFERED. This
538 * driver MUST honor the return value.
539 */
540static int hvc_write_room(struct tty_struct *tty)
541{
542 struct hvc_struct *hp = tty->driver_data;
543
544 if (!hp)
545 return -1;
546
547 return N_OUTBUF - hp->n_outbuf;
548}
549
550static int hvc_chars_in_buffer(struct tty_struct *tty)
551{
552 struct hvc_struct *hp = tty->driver_data;
553
554 if (!hp)
555 return -1;
556 return hp->n_outbuf;
557}
558
559#define HVC_POLL_READ 0x00000001
560#define HVC_POLL_WRITE 0x00000002
561#define HVC_POLL_QUICK 0x00000004
562
563static int hvc_poll(struct hvc_struct *hp)
564{
565 struct tty_struct *tty;
566 int i, n, poll_mask = 0;
567 char buf[N_INBUF] __ALIGNED__;
568 unsigned long flags;
569 int read_total = 0;
570
571 spin_lock_irqsave(&hp->lock, flags);
572
573 /* Push pending writes */
574 if (hp->n_outbuf > 0)
575 hvc_push(hp);
576 /* Reschedule us if still some write pending */
577 if (hp->n_outbuf > 0)
578 poll_mask |= HVC_POLL_WRITE;
579
580 /* No tty attached, just skip */
581 tty = hp->tty;
582 if (tty == NULL)
583 goto bail;
584
585 /* Now check if we can get data (are we throttled ?) */
586 if (test_bit(TTY_THROTTLED, &tty->flags))
587 goto throttled;
588
589 /* If we aren't interrupt driven and aren't throttled, we always
590 * request a reschedule
591 */
592 if (hp->irq == NO_IRQ)
593 poll_mask |= HVC_POLL_READ;
594
595 /* Read data if any */
596 for (;;) {
597 int count = N_INBUF;
598 if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
599 count = TTY_FLIPBUF_SIZE - tty->flip.count;
600
601 /* If flip is full, just reschedule a later read */
602 if (count == 0) {
603 poll_mask |= HVC_POLL_READ;
604 break;
605 }
606
607 n = hvc_get_chars(hp->vtermno, buf, count);
608 if (n <= 0) {
609 /* Hangup the tty when disconnected from host */
610 if (n == -EPIPE) {
611 spin_unlock_irqrestore(&hp->lock, flags);
612 tty_hangup(tty);
613 spin_lock_irqsave(&hp->lock, flags);
614 }
615 break;
616 }
617 for (i = 0; i < n; ++i) {
618#ifdef CONFIG_MAGIC_SYSRQ
Milton Miller2b9e0ba2005-07-07 17:56:19 -0700619 if (hp->index == hvc_con_driver.index) {
620 /* Handle the SysRq Hack */
621 /* XXX should support a sequence */
622 if (buf[i] == '\x0f') { /* ^O */
623 sysrq_pressed = 1;
624 continue;
625 } else if (sysrq_pressed) {
626 handle_sysrq(buf[i], NULL, tty);
627 sysrq_pressed = 0;
628 continue;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631#endif /* CONFIG_MAGIC_SYSRQ */
632 tty_insert_flip_char(tty, buf[i], 0);
633 }
634
635 if (tty->flip.count)
636 tty_schedule_flip(tty);
637
638 /*
639 * Account for the total amount read in one loop, and if above
Milton Miller8b67f8c2005-07-07 17:56:18 -0700640 * 64 bytes, we do a quick schedule loop to let the tty grok
641 * the data and eventually throttle us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643 read_total += n;
644 if (read_total >= 64) {
645 poll_mask |= HVC_POLL_QUICK;
646 break;
647 }
648 }
649 throttled:
650 /* Wakeup write queue if necessary */
651 if (hp->do_wakeup) {
652 hp->do_wakeup = 0;
653 tty_wakeup(tty);
654 }
655 bail:
656 spin_unlock_irqrestore(&hp->lock, flags);
657
658 return poll_mask;
659}
660
661#if defined(CONFIG_XMON) && defined(CONFIG_SMP)
662extern cpumask_t cpus_in_xmon;
663#else
664static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
665#endif
666
667/*
668 * This kthread is either polling or interrupt driven. This is determined by
669 * calling hvc_poll() who determines whether a console adapter support
670 * interrupts.
671 */
672int khvcd(void *unused)
673{
674 int poll_mask;
675 struct hvc_struct *hp;
676
677 __set_current_state(TASK_RUNNING);
678 do {
679 poll_mask = 0;
680 hvc_kicked = 0;
681 wmb();
682 if (cpus_empty(cpus_in_xmon)) {
683 spin_lock(&hvc_structs_lock);
684 list_for_each_entry(hp, &hvc_structs, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 poll_mask |= hvc_poll(hp);
686 }
687 spin_unlock(&hvc_structs_lock);
688 } else
689 poll_mask |= HVC_POLL_READ;
690 if (hvc_kicked)
691 continue;
692 if (poll_mask & HVC_POLL_QUICK) {
693 yield();
694 continue;
695 }
696 set_current_state(TASK_INTERRUPTIBLE);
697 if (!hvc_kicked) {
698 if (poll_mask == 0)
699 schedule();
700 else
701 msleep_interruptible(TIMEOUT);
702 }
703 __set_current_state(TASK_RUNNING);
704 } while (!kthread_should_stop());
705
706 return 0;
707}
708
709static struct tty_operations hvc_ops = {
710 .open = hvc_open,
711 .close = hvc_close,
712 .write = hvc_write,
713 .hangup = hvc_hangup,
714 .unthrottle = hvc_unthrottle,
715 .write_room = hvc_write_room,
716 .chars_in_buffer = hvc_chars_in_buffer,
717};
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/* callback when the kboject ref count reaches zero. */
720static void destroy_hvc_struct(struct kobject *kobj)
721{
722 struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
723 unsigned long flags;
724
725 spin_lock(&hvc_structs_lock);
726
727 spin_lock_irqsave(&hp->lock, flags);
728 list_del(&(hp->next));
729 spin_unlock_irqrestore(&hp->lock, flags);
730
731 spin_unlock(&hvc_structs_lock);
732
733 kfree(hp);
734}
735
736static struct kobj_type hvc_kobj_type = {
737 .release = destroy_hvc_struct,
738};
739
Milton Milleracad9552005-07-07 17:56:24 -0700740struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 struct hvc_struct *hp;
Milton Miller6f248082005-07-07 17:56:17 -0700743 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 hp = kmalloc(sizeof(*hp), GFP_KERNEL);
746 if (!hp)
Milton Milleracad9552005-07-07 17:56:24 -0700747 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 memset(hp, 0x00, sizeof(*hp));
Milton Milleracad9552005-07-07 17:56:24 -0700750
751 hp->vtermno = vtermno;
752 hp->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 kobject_init(&hp->kobj);
755 hp->kobj.ktype = &hvc_kobj_type;
756
757 spin_lock_init(&hp->lock);
758 spin_lock(&hvc_structs_lock);
Milton Miller6f248082005-07-07 17:56:17 -0700759
760 /*
761 * find index to use:
762 * see if this vterm id matches one registered for console.
763 */
764 for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
765 if (vtermnos[i] == hp->vtermno)
766 break;
767
768 /* no matching slot, just use a counter */
769 if (i >= MAX_NR_HVC_CONSOLES)
770 i = ++last_hvc;
771
772 hp->index = i;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 list_add_tail(&(hp->next), &hvc_structs);
775 spin_unlock(&hvc_structs_lock);
776
Milton Milleracad9552005-07-07 17:56:24 -0700777 return hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
Milton Milleracad9552005-07-07 17:56:24 -0700779EXPORT_SYMBOL(hvc_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Milton Milleracad9552005-07-07 17:56:24 -0700781int __devexit hvc_remove(struct hvc_struct *hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 unsigned long flags;
784 struct kobject *kobjp;
785 struct tty_struct *tty;
786
787 spin_lock_irqsave(&hp->lock, flags);
788 tty = hp->tty;
789 kobjp = &hp->kobj;
790
791 if (hp->index < MAX_NR_HVC_CONSOLES)
792 vtermnos[hp->index] = -1;
793
794 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
795
796 spin_unlock_irqrestore(&hp->lock, flags);
797
798 /*
799 * We 'put' the instance that was grabbed when the kobject instance
800 * was intialized using kobject_init(). Let the last holder of this
801 * kobject cause it to be removed, which will probably be the tty_hangup
802 * below.
803 */
804 kobject_put(kobjp);
805
806 /*
807 * This function call will auto chain call hvc_hangup. The tty should
808 * always be valid at this time unless a simultaneous tty close already
809 * cleaned up the hvc_struct.
810 */
811 if (tty)
812 tty_hangup(tty);
813 return 0;
814}
Milton Milleracad9552005-07-07 17:56:24 -0700815EXPORT_SYMBOL(hvc_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817/* Driver initialization. Follow console initialization. This is where the TTY
818 * interfaces start to become available. */
819int __init hvc_init(void)
820{
Milton Miller5f6d9c02005-07-07 17:56:21 -0700821 /* We need more than hvc_count adapters due to hotplug additions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!hvc_driver)
824 return -ENOMEM;
825
826 hvc_driver->owner = THIS_MODULE;
827 hvc_driver->devfs_name = "hvc/";
828 hvc_driver->driver_name = "hvc";
829 hvc_driver->name = "hvc";
830 hvc_driver->major = HVC_MAJOR;
831 hvc_driver->minor_start = HVC_MINOR;
832 hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
833 hvc_driver->init_termios = tty_std_termios;
834 hvc_driver->flags = TTY_DRIVER_REAL_RAW;
835 tty_set_operations(hvc_driver, &hvc_ops);
836
837 if (tty_register_driver(hvc_driver))
838 panic("Couldn't register hvc console driver\n");
839
840 /* Always start the kthread because there can be hotplug vty adapters
841 * added later. */
842 hvc_task = kthread_run(khvcd, NULL, "khvcd");
843 if (IS_ERR(hvc_task)) {
844 panic("Couldn't create kthread for console.\n");
845 put_tty_driver(hvc_driver);
846 return -EIO;
847 }
848
Milton Milleracad9552005-07-07 17:56:24 -0700849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Milton Miller837dcfa2005-07-07 17:56:16 -0700851module_init(hvc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Milton Miller320da0d2005-07-07 17:56:20 -0700853/* This isn't particularily necessary due to this being a console driver
854 * but it is nice to be thorough.
855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static void __exit hvc_exit(void)
857{
858 kthread_stop(hvc_task);
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 tty_unregister_driver(hvc_driver);
861 /* return tty_struct instances allocated in hvc_init(). */
862 put_tty_driver(hvc_driver);
Milton Miller320da0d2005-07-07 17:56:20 -0700863 unregister_console(&hvc_con_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865module_exit(hvc_exit);