firewire: get_cycle_timer optimization and cleanup
[safe/jmp/linux-2.6] / drivers / firewire / core-device.c
index d6e54a5..eecd52d 100644 (file)
@@ -43,7 +43,7 @@
 
 #include "core.h"
 
-void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
+void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p)
 {
        ci->p = p + 1;
        ci->end = ci->p + (p[0] >> 16);
@@ -59,9 +59,76 @@ int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
 }
 EXPORT_SYMBOL(fw_csr_iterator_next);
 
+static const u32 *search_leaf(const u32 *directory, int search_key)
+{
+       struct fw_csr_iterator ci;
+       int last_key = 0, key, value;
+
+       fw_csr_iterator_init(&ci, directory);
+       while (fw_csr_iterator_next(&ci, &key, &value)) {
+               if (last_key == search_key &&
+                   key == (CSR_DESCRIPTOR | CSR_LEAF))
+                       return ci.p - 1 + value;
+
+               last_key = key;
+       }
+
+       return NULL;
+}
+
+static int textual_leaf_to_string(const u32 *block, char *buf, size_t size)
+{
+       unsigned int quadlets, i;
+       char c;
+
+       if (!size || !buf)
+               return -EINVAL;
+
+       quadlets = min(block[0] >> 16, 256U);
+       if (quadlets < 2)
+               return -ENODATA;
+
+       if (block[1] != 0 || block[2] != 0)
+               /* unknown language/character set */
+               return -ENODATA;
+
+       block += 3;
+       quadlets -= 2;
+       for (i = 0; i < quadlets * 4 && i < size - 1; i++) {
+               c = block[i / 4] >> (24 - 8 * (i % 4));
+               if (c == '\0')
+                       break;
+               buf[i] = c;
+       }
+       buf[i] = '\0';
+
+       return i;
+}
+
+/**
+ * fw_csr_string - reads a string from the configuration ROM
+ * @directory: e.g. root directory or unit directory
+ * @key: the key of the preceding directory entry
+ * @buf: where to put the string
+ * @size: size of @buf, in bytes
+ *
+ * The string is taken from a minimal ASCII text descriptor leaf after
+ * the immediate entry with @key.  The string is zero-terminated.
+ * Returns strlen(buf) or a negative error code.
+ */
+int fw_csr_string(const u32 *directory, int key, char *buf, size_t size)
+{
+       const u32 *leaf = search_leaf(directory, key);
+       if (!leaf)
+               return -ENOENT;
+
+       return textual_leaf_to_string(leaf, buf, size);
+}
+EXPORT_SYMBOL(fw_csr_string);
+
 static bool is_fw_unit(struct device *dev);
 
-static int match_unit_directory(u32 *directory, u32 match_flags,
+static int match_unit_directory(const u32 *directory, u32 match_flags,
                                const struct ieee1394_device_id *id)
 {
        struct fw_csr_iterator ci;
@@ -195,7 +262,7 @@ static ssize_t show_immediate(struct device *dev,
        struct config_rom_attribute *attr =
                container_of(dattr, struct config_rom_attribute, attr);
        struct fw_csr_iterator ci;
-       u32 *dir;
+       const u32 *dir;
        int key, value, ret = -ENOENT;
 
        down_read(&fw_device_rwsem);
@@ -226,10 +293,10 @@ static ssize_t show_text_leaf(struct device *dev,
 {
        struct config_rom_attribute *attr =
                container_of(dattr, struct config_rom_attribute, attr);
-       struct fw_csr_iterator ci;
-       u32 *dir, *block = NULL, *p, *end;
-       int length, key, value, last_key = 0, ret = -ENOENT;
-       char *b;
+       const u32 *dir;
+       size_t bufsize;
+       char dummy_buf[2];
+       int ret;
 
        down_read(&fw_device_rwsem);
 
@@ -238,40 +305,23 @@ static ssize_t show_text_leaf(struct device *dev,
        else
                dir = fw_device(dev)->config_rom + 5;
 
-       fw_csr_iterator_init(&ci, dir);
-       while (fw_csr_iterator_next(&ci, &key, &value)) {
-               if (attr->key == last_key &&
-                   key == (CSR_DESCRIPTOR | CSR_LEAF))
-                       block = ci.p - 1 + value;
-               last_key = key;
+       if (buf) {
+               bufsize = PAGE_SIZE - 1;
+       } else {
+               buf = dummy_buf;
+               bufsize = 1;
        }
 
-       if (block == NULL)
-               goto out;
-
-       length = min(block[0] >> 16, 256U);
-       if (length < 3)
-               goto out;
+       ret = fw_csr_string(dir, attr->key, buf, bufsize);
 
-       if (block[1] != 0 || block[2] != 0)
-               /* Unknown encoding. */
-               goto out;
-
-       if (buf == NULL) {
-               ret = length * 4;
-               goto out;
+       if (ret >= 0) {
+               /* Strip trailing whitespace and add newline. */
+               while (ret > 0 && isspace(buf[ret - 1]))
+                       ret--;
+               strcpy(buf + ret, "\n");
+               ret++;
        }
 
-       b = buf;
-       end = &block[length + 1];
-       for (p = &block[3]; p < end; p++, b += 4)
-               * (u32 *) b = (__force u32) __cpu_to_be32(*p);
-
-       /* Strip trailing whitespace and add newline. */
-       while (b--, (isspace(*b) || *b == '\0') && b > buf);
-       strcpy(b + 1, "\n");
-       ret = b + 2 - buf;
- out:
        up_read(&fw_device_rwsem);
 
        return ret;
@@ -312,7 +362,7 @@ static void init_fw_attribute_group(struct device *dev,
        group->groups[0] = &group->group;
        group->groups[1] = NULL;
        group->group.attrs = group->attrs;
-       dev->groups = group->groups;
+       dev->groups = (const struct attribute_group **) group->groups;
 }
 
 static ssize_t modalias_show(struct device *dev,
@@ -371,7 +421,7 @@ static ssize_t guid_show(struct device *dev,
        return ret;
 }
 
-static int units_sprintf(char *buf, u32 *directory)
+static int units_sprintf(char *buf, const u32 *directory)
 {
        struct fw_csr_iterator ci;
        int key, value;
@@ -453,7 +503,8 @@ static int read_rom(struct fw_device *device,
  */
 static int read_bus_info_block(struct fw_device *device, int generation)
 {
-       u32 *rom, *stack, *old_rom, *new_rom;
+       const u32 *old_rom, *new_rom;
+       u32 *rom, *stack;
        u32 sp, key;
        int i, end, length, ret = -1;
 
@@ -580,7 +631,9 @@ static int read_bus_info_block(struct fw_device *device, int generation)
 
        kfree(old_rom);
        ret = 0;
-       device->cmc = rom[2] >> 30 & 1;
+       device->max_rec = rom[2] >> 12 & 0xf;
+       device->cmc     = rom[2] >> 30 & 1;
+       device->irmc    = rom[2] >> 31 & 1;
  out:
        kfree(rom);
 
@@ -841,6 +894,20 @@ static void set_broadcast_channel(struct fw_device *device, int generation)
        if (!card->broadcast_channel_allocated)
                return;
 
+       /*
+        * The Broadcast_Channel Valid bit is required by nodes which want to
+        * transmit on this channel.  Such transmissions are practically
+        * exclusive to IP over 1394 (RFC 2734).  IP capable nodes are required
+        * to be IRM capable and have a max_rec of 8 or more.  We use this fact
+        * to narrow down to which nodes we send Broadcast_Channel updates.
+        */
+       if (!device->irmc || device->max_rec < 8)
+               return;
+
+       /*
+        * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
+        * Perform a read test first.
+        */
        if (device->bc_implemented == BC_UNKNOWN) {
                rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
                                device->node_id, generation, device->max_speed,