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