include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / firmware / dmi_scan.c
index 1412d7b..d464672 100644 (file)
@@ -5,15 +5,23 @@
 #include <linux/dmi.h>
 #include <linux/efi.h>
 #include <linux/bootmem.h>
-#include <linux/slab.h>
 #include <asm/dmi.h>
 
+/*
+ * DMI stands for "Desktop Management Interface".  It is part
+ * of and an antecedent to, SMBIOS, which stands for System
+ * Management BIOS.  See further: http://www.dmtf.org/standards
+ */
 static char dmi_empty_string[] = "        ";
 
-static char * __init dmi_string(const struct dmi_header *dm, u8 s)
+/*
+ * Catch too early calls to dmi_check_system():
+ */
+static int dmi_initialized;
+
+static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
 {
        const u8 *bp = ((u8 *) dm) + dm->length;
-       char *str = "";
 
        if (s) {
                s--;
@@ -28,14 +36,29 @@ static char * __init dmi_string(const struct dmi_header *dm, u8 s)
 
                        if (!memcmp(bp, dmi_empty_string, cmp_len))
                                return dmi_empty_string;
-                       str = dmi_alloc(len);
-                       if (str != NULL)
-                               strcpy(str, bp);
-                       else
-                               printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
+                       return bp;
                }
        }
 
+       return "";
+}
+
+static char * __init dmi_string(const struct dmi_header *dm, u8 s)
+{
+       const char *bp = dmi_string_nosave(dm, s);
+       char *str;
+       size_t len;
+
+       if (bp == dmi_empty_string)
+               return dmi_empty_string;
+
+       len = strlen(bp) + 1;
+       str = dmi_alloc(len);
+       if (str != NULL)
+               strcpy(str, bp);
+       else
+               printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
+
        return str;
 }
 
@@ -44,7 +67,8 @@ static char * __init dmi_string(const struct dmi_header *dm, u8 s)
  *     pointing to completely the wrong place for example
  */
 static void dmi_table(u8 *buf, int len, int num,
-                     void (*decode)(const struct dmi_header *))
+                     void (*decode)(const struct dmi_header *, void *),
+                     void *private_data)
 {
        u8 *data = buf;
        int i = 0;
@@ -57,15 +81,15 @@ static void dmi_table(u8 *buf, int len, int num,
                const struct dmi_header *dm = (const struct dmi_header *)data;
 
                /*
-                *  We want to know the total length (formated area and strings)
-                *  before decoding to make sure we won't run off the table in
-                *  dmi_decode or dmi_string
+                *  We want to know the total length (formatted area and
+                *  strings) before decoding to make sure we won't run off the
+                *  table in dmi_decode or dmi_string
                 */
                data += dm->length;
                while ((data - buf < len - 1) && (data[0] || data[1]))
                        data++;
                if (data - buf < len - 1)
-                       decode(dm);
+                       decode(dm, private_data);
                data += 2;
                i++;
        }
@@ -75,7 +99,8 @@ static u32 dmi_base;
 static u16 dmi_len;
 static u16 dmi_num;
 
-static int __init dmi_walk_early(void (*decode)(const struct dmi_header *))
+static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
+               void *))
 {
        u8 *buf;
 
@@ -83,7 +108,7 @@ static int __init dmi_walk_early(void (*decode)(const struct dmi_header *))
        if (buf == NULL)
                return -1;
 
-       dmi_table(buf, dmi_len, dmi_num, decode);
+       dmi_table(buf, dmi_len, dmi_num, decode, NULL);
 
        dmi_iounmap(buf, dmi_len);
        return 0;
@@ -143,10 +168,7 @@ static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int inde
        if (!s)
                return;
 
-       sprintf(s,
-               "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
-               d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
-               d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
+       sprintf(s, "%pUB", d);
 
         dmi_ident[slot] = s;
 }
@@ -167,10 +189,30 @@ static void __init dmi_save_type(const struct dmi_header *dm, int slot, int inde
        dmi_ident[slot] = s;
 }
 
+static void __init dmi_save_one_device(int type, const char *name)
+{
+       struct dmi_device *dev;
+
+       /* No duplicate device */
+       if (dmi_find_device(type, name, NULL))
+               return;
+
+       dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
+       if (!dev) {
+               printk(KERN_ERR "dmi_save_one_device: out of memory.\n");
+               return;
+       }
+
+       dev->type = type;
+       strcpy((char *)(dev + 1), name);
+       dev->name = (char *)(dev + 1);
+       dev->device_data = NULL;
+       list_add(&dev->list, &dmi_devices);
+}
+
 static void __init dmi_save_devices(const struct dmi_header *dm)
 {
        int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
-       struct dmi_device *dev;
 
        for (i = 0; i < count; i++) {
                const char *d = (char *)(dm + 1) + (i * 2);
@@ -179,23 +221,10 @@ static void __init dmi_save_devices(const struct dmi_header *dm)
                if ((*d & 0x80) == 0)
                        continue;
 
-               dev = dmi_alloc(sizeof(*dev));
-               if (!dev) {
-                       printk(KERN_ERR "dmi_save_devices: out of memory.\n");
-                       break;
-               }
-
-               dev->type = *d++ & 0x7f;
-               dev->name = dmi_string(dm, *d);
-               dev->device_data = NULL;
-               list_add(&dev->list, &dmi_devices);
+               dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
        }
 }
 
-static struct dmi_device empty_oem_string_dev = {
-       .name = dmi_empty_string,
-};
-
 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
 {
        int i, count = *(u8 *)(dm + 1);
@@ -204,10 +233,8 @@ static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
        for (i = 1; i <= count; i++) {
                char *devname = dmi_string(dm, i);
 
-               if (!strcmp(devname, dmi_empty_string)) {
-                       list_add(&empty_oem_string_dev.list, &dmi_devices);
+               if (devname == dmi_empty_string)
                        continue;
-               }
 
                dev = dmi_alloc(sizeof(*dev));
                if (!dev) {
@@ -247,7 +274,18 @@ static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
        dev->name = "IPMI controller";
        dev->device_data = data;
 
-       list_add(&dev->list, &dmi_devices);
+       list_add_tail(&dev->list, &dmi_devices);
+}
+
+static void __init dmi_save_extended_devices(const struct dmi_header *dm)
+{
+       const u8 *d = (u8*) dm + 5;
+
+       /* Skip disabled device */
+       if ((*d & 0x80) == 0)
+               return;
+
+       dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
 }
 
 /*
@@ -255,7 +293,7 @@ static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
  *     and machine entries. For 2.5 we should pull the smbus controller info
  *     out of here.
  */
-static void __init dmi_decode(const struct dmi_header *dm)
+static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
 {
        switch(dm->type) {
        case 0:         /* BIOS Information */
@@ -292,6 +330,9 @@ static void __init dmi_decode(const struct dmi_header *dm)
                break;
        case 38:        /* IPMI Device Information */
                dmi_save_ipmi_device(dm);
+               break;
+       case 41:        /* Onboard Devices Extended Information */
+               dmi_save_extended_devices(dm);
        }
 }
 
@@ -328,7 +369,7 @@ void __init dmi_scan_machine(void)
 
        if (efi_enabled) {
                if (efi.smbios == EFI_INVALID_TABLE_ADDR)
-                       goto out;
+                       goto error;
 
                /* This is called as a core_initcall() because it isn't
                 * needed during early boot.  This also means we can
@@ -336,13 +377,13 @@ void __init dmi_scan_machine(void)
                 */
                p = dmi_ioremap(efi.smbios, 32);
                if (p == NULL)
-                       goto out;
+                       goto error;
 
                rc = dmi_present(p + 0x10); /* offset of _DMI_ string */
                dmi_iounmap(p, 32);
                if (!rc) {
                        dmi_available = 1;
-                       return;
+                       goto out;
                }
        }
        else {
@@ -353,19 +394,54 @@ void __init dmi_scan_machine(void)
                 */
                p = dmi_ioremap(0xF0000, 0x10000);
                if (p == NULL)
-                       goto out;
+                       goto error;
 
                for (q = p; q < p + 0x10000; q += 16) {
                        rc = dmi_present(q);
                        if (!rc) {
                                dmi_available = 1;
                                dmi_iounmap(p, 0x10000);
-                               return;
+                               goto out;
                        }
                }
                dmi_iounmap(p, 0x10000);
        }
- out:  printk(KERN_INFO "DMI not present or invalid.\n");
+ error:
+       printk(KERN_INFO "DMI not present or invalid.\n");
+ out:
+       dmi_initialized = 1;
+}
+
+/**
+ *     dmi_matches - check if dmi_system_id structure matches system DMI data
+ *     @dmi: pointer to the dmi_system_id structure to check
+ */
+static bool dmi_matches(const struct dmi_system_id *dmi)
+{
+       int i;
+
+       WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
+
+       for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
+               int s = dmi->matches[i].slot;
+               if (s == DMI_NONE)
+                       break;
+               if (dmi_ident[s]
+                   && strstr(dmi_ident[s], dmi->matches[i].substr))
+                       continue;
+               /* No match */
+               return false;
+       }
+       return true;
+}
+
+/**
+ *     dmi_is_end_of_table - check for end-of-table marker
+ *     @dmi: pointer to the dmi_system_id structure to check
+ */
+static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
+{
+       return dmi->matches[0].slot == DMI_NONE;
 }
 
 /**
@@ -383,30 +459,45 @@ void __init dmi_scan_machine(void)
  */
 int dmi_check_system(const struct dmi_system_id *list)
 {
-       int i, count = 0;
-       const struct dmi_system_id *d = list;
-
-       while (d->ident) {
-               for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
-                       int s = d->matches[i].slot;
-                       if (s == DMI_NONE)
-                               continue;
-                       if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
-                               continue;
-                       /* No match */
-                       goto fail;
+       int count = 0;
+       const struct dmi_system_id *d;
+
+       for (d = list; !dmi_is_end_of_table(d); d++)
+               if (dmi_matches(d)) {
+                       count++;
+                       if (d->callback && d->callback(d))
+                               break;
                }
-               count++;
-               if (d->callback && d->callback(d))
-                       break;
-fail:          d++;
-       }
 
        return count;
 }
 EXPORT_SYMBOL(dmi_check_system);
 
 /**
+ *     dmi_first_match - find dmi_system_id structure matching system DMI data
+ *     @list: array of dmi_system_id structures to match against
+ *             All non-null elements of the list must match
+ *             their slot's (field index's) data (i.e., each
+ *             list string must be a substring of the specified
+ *             DMI slot's string data) to be considered a
+ *             successful match.
+ *
+ *     Walk the blacklist table until the first match is found.  Return the
+ *     pointer to the matching entry or NULL if there's no match.
+ */
+const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
+{
+       const struct dmi_system_id *d;
+
+       for (d = list; !dmi_is_end_of_table(d); d++)
+               if (dmi_matches(d))
+                       return d;
+
+       return NULL;
+}
+EXPORT_SYMBOL(dmi_first_match);
+
+/**
  *     dmi_get_system_info - return DMI data value
  *     @field: data index (see enum dmi_field)
  *
@@ -419,6 +510,17 @@ const char *dmi_get_system_info(int field)
 }
 EXPORT_SYMBOL(dmi_get_system_info);
 
+/**
+ * dmi_name_in_serial - Check if string is in the DMI product serial information
+ * @str: string to check for
+ */
+int dmi_name_in_serial(const char *str)
+{
+       int f = DMI_PRODUCT_SERIAL;
+       if (dmi_ident[f] && strstr(dmi_ident[f], str))
+               return 1;
+       return 0;
+}
 
 /**
  *     dmi_name_in_vendors - Check if string is anywhere in the DMI vendor information.
@@ -471,42 +573,86 @@ const struct dmi_device * dmi_find_device(int type, const char *name,
 EXPORT_SYMBOL(dmi_find_device);
 
 /**
- *     dmi_get_year - Return year of a DMI date
- *     @field: data index (like dmi_get_system_info)
+ *     dmi_get_date - parse a DMI date
+ *     @field: data index (see enum dmi_field)
+ *     @yearp: optional out parameter for the year
+ *     @monthp: optional out parameter for the month
+ *     @dayp: optional out parameter for the day
+ *
+ *     The date field is assumed to be in the form resembling
+ *     [mm[/dd]]/yy[yy] and the result is stored in the out
+ *     parameters any or all of which can be omitted.
+ *
+ *     If the field doesn't exist, all out parameters are set to zero
+ *     and false is returned.  Otherwise, true is returned with any
+ *     invalid part of date set to zero.
  *
- *     Returns -1 when the field doesn't exist. 0 when it is broken.
+ *     On return, year, month and day are guaranteed to be in the
+ *     range of [0,9999], [0,12] and [0,31] respectively.
  */
-int dmi_get_year(int field)
+bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
 {
-       int year;
-       const char *s = dmi_get_system_info(field);
+       int year = 0, month = 0, day = 0;
+       bool exists;
+       const char *s, *y;
+       char *e;
 
-       if (!s)
-               return -1;
-       if (*s == '\0')
-               return 0;
-       s = strrchr(s, '/');
-       if (!s)
-               return 0;
+       s = dmi_get_system_info(field);
+       exists = s;
+       if (!exists)
+               goto out;
 
-       s += 1;
-       year = simple_strtoul(s, NULL, 0);
-       if (year && year < 100) {       /* 2-digit year */
+       /*
+        * Determine year first.  We assume the date string resembles
+        * mm/dd/yy[yy] but the original code extracted only the year
+        * from the end.  Keep the behavior in the spirit of no
+        * surprises.
+        */
+       y = strrchr(s, '/');
+       if (!y)
+               goto out;
+
+       y++;
+       year = simple_strtoul(y, &e, 10);
+       if (y != e && year < 100) {     /* 2-digit year */
                year += 1900;
                if (year < 1996)        /* no dates < spec 1.0 */
                        year += 100;
        }
+       if (year > 9999)                /* year should fit in %04d */
+               year = 0;
+
+       /* parse the mm and dd */
+       month = simple_strtoul(s, &e, 10);
+       if (s == e || *e != '/' || !month || month > 12) {
+               month = 0;
+               goto out;
+       }
 
-       return year;
+       s = e + 1;
+       day = simple_strtoul(s, &e, 10);
+       if (s == y || s == e || *e != '/' || day > 31)
+               day = 0;
+out:
+       if (yearp)
+               *yearp = year;
+       if (monthp)
+               *monthp = month;
+       if (dayp)
+               *dayp = day;
+       return exists;
 }
+EXPORT_SYMBOL(dmi_get_date);
 
 /**
  *     dmi_walk - Walk the DMI table and get called back for every record
  *     @decode: Callback function
+ *     @private_data: Private data to be passed to the callback function
  *
  *     Returns -1 when the DMI table can't be reached, 0 on success.
  */
-int dmi_walk(void (*decode)(const struct dmi_header *))
+int dmi_walk(void (*decode)(const struct dmi_header *, void *),
+            void *private_data)
 {
        u8 *buf;
 
@@ -517,9 +663,27 @@ int dmi_walk(void (*decode)(const struct dmi_header *))
        if (buf == NULL)
                return -1;
 
-       dmi_table(buf, dmi_len, dmi_num, decode);
+       dmi_table(buf, dmi_len, dmi_num, decode, private_data);
 
        iounmap(buf);
        return 0;
 }
 EXPORT_SYMBOL_GPL(dmi_walk);
+
+/**
+ * dmi_match - compare a string to the dmi field (if exists)
+ * @f: DMI field identifier
+ * @str: string to compare the DMI field to
+ *
+ * Returns true if the requested field equals to the str (including NULL).
+ */
+bool dmi_match(enum dmi_field f, const char *str)
+{
+       const char *info = dmi_get_system_info(f);
+
+       if (info == NULL || str == NULL)
+               return info == str;
+
+       return !strcmp(info, str);
+}
+EXPORT_SYMBOL_GPL(dmi_match);