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