]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/char/hvc_console.c
[PATCH] hvc_console: Add some sanity checks
[linux-2.6-omap-h63xx.git] / drivers / char / hvc_console.c
index 31980994feadee917603b02ed9b5ae7e59a8feaa..7bc65a76dfc44d5c00883be4dcbda6324cf6959c 100644 (file)
@@ -144,10 +144,8 @@ struct hvc_struct *hvc_get_by_index(int index)
  * console interfaces but can still be used as a tty device.  This has to be
  * static because kmalloc will not work during early console init.
  */
-static uint32_t vtermnos[MAX_NR_HVC_CONSOLES];
-
-/* Used for accounting purposes */
-static int num_vterms = 0;
+static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
+       {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
 
 /*
  * Console APIs, NOT TTY.  These APIs are available immediately when
@@ -194,12 +192,21 @@ void hvc_console_print(struct console *co, const char *b, unsigned count)
 
 static struct tty_driver *hvc_console_device(struct console *c, int *index)
 {
+       if (vtermnos[c->index] == -1)
+               return NULL;
+
        *index = c->index;
        return hvc_driver;
 }
 
 static int __init hvc_console_setup(struct console *co, char *options)
 {
+       if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
+               return -ENODEV;
+
+       if (vtermnos[co->index] == -1)
+               return -ENODEV;
+
        return 0;
 }
 
@@ -215,11 +222,7 @@ struct console hvc_con_driver = {
 /* Early console initialization.  Preceeds driver initialization. */
 static int __init hvc_console_init(void)
 {
-       int i;
-
-       for (i=0; i<MAX_NR_HVC_CONSOLES; i++)
-               vtermnos[i] = -1;
-       num_vterms = hvc_find_vtys();
+       hvc_find_vtys();
        register_console(&hvc_con_driver);
        return 0;
 }
@@ -233,12 +236,21 @@ console_initcall(hvc_console_init);
  */
 int hvc_instantiate(uint32_t vtermno, int index)
 {
+       struct hvc_struct *hp;
+
        if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
                return -1;
 
        if (vtermnos[index] != -1)
                return -1;
 
+       /* make sure no no tty has been registerd in this index */
+       hp = hvc_get_by_index(index);
+       if (hp) {
+               kobject_put(&hp->kobj);
+               return -1;
+       }
+
        vtermnos[index] = vtermno;
 
        /* reserve all indices upto and including this index */
@@ -651,7 +663,6 @@ int khvcd(void *unused)
                if (cpus_empty(cpus_in_xmon)) {
                        spin_lock(&hvc_structs_lock);
                        list_for_each_entry(hp, &hvc_structs, next) {
-                               /*hp = list_entry(node, struct hvc_struct, * next); */
                                poll_mask |= hvc_poll(hp);
                        }
                        spin_unlock(&hvc_structs_lock);
@@ -811,9 +822,8 @@ int __init hvc_init(void)
 {
        int rc;
 
-       /* We need more than num_vterms adapters due to hotplug additions. */
+       /* We need more than hvc_count adapters due to hotplug additions. */
        hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
-       /* hvc_driver = alloc_tty_driver(num_vterms); */
        if (!hvc_driver)
                return -ENOMEM;