Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Device probing and sysfs code.
0004  *
0005  * Copyright (C) 2005-2006  Kristian Hoegsberg <krh@bitplanet.net>
0006  */
0007 
0008 #include <linux/bug.h>
0009 #include <linux/ctype.h>
0010 #include <linux/delay.h>
0011 #include <linux/device.h>
0012 #include <linux/errno.h>
0013 #include <linux/firewire.h>
0014 #include <linux/firewire-constants.h>
0015 #include <linux/idr.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/kobject.h>
0018 #include <linux/list.h>
0019 #include <linux/mod_devicetable.h>
0020 #include <linux/module.h>
0021 #include <linux/mutex.h>
0022 #include <linux/random.h>
0023 #include <linux/rwsem.h>
0024 #include <linux/slab.h>
0025 #include <linux/spinlock.h>
0026 #include <linux/string.h>
0027 #include <linux/workqueue.h>
0028 
0029 #include <linux/atomic.h>
0030 #include <asm/byteorder.h>
0031 
0032 #include "core.h"
0033 
0034 void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p)
0035 {
0036     ci->p = p + 1;
0037     ci->end = ci->p + (p[0] >> 16);
0038 }
0039 EXPORT_SYMBOL(fw_csr_iterator_init);
0040 
0041 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
0042 {
0043     *key = *ci->p >> 24;
0044     *value = *ci->p & 0xffffff;
0045 
0046     return ci->p++ < ci->end;
0047 }
0048 EXPORT_SYMBOL(fw_csr_iterator_next);
0049 
0050 static const u32 *search_leaf(const u32 *directory, int search_key)
0051 {
0052     struct fw_csr_iterator ci;
0053     int last_key = 0, key, value;
0054 
0055     fw_csr_iterator_init(&ci, directory);
0056     while (fw_csr_iterator_next(&ci, &key, &value)) {
0057         if (last_key == search_key &&
0058             key == (CSR_DESCRIPTOR | CSR_LEAF))
0059             return ci.p - 1 + value;
0060 
0061         last_key = key;
0062     }
0063 
0064     return NULL;
0065 }
0066 
0067 static int textual_leaf_to_string(const u32 *block, char *buf, size_t size)
0068 {
0069     unsigned int quadlets, i;
0070     char c;
0071 
0072     if (!size || !buf)
0073         return -EINVAL;
0074 
0075     quadlets = min(block[0] >> 16, 256U);
0076     if (quadlets < 2)
0077         return -ENODATA;
0078 
0079     if (block[1] != 0 || block[2] != 0)
0080         /* unknown language/character set */
0081         return -ENODATA;
0082 
0083     block += 3;
0084     quadlets -= 2;
0085     for (i = 0; i < quadlets * 4 && i < size - 1; i++) {
0086         c = block[i / 4] >> (24 - 8 * (i % 4));
0087         if (c == '\0')
0088             break;
0089         buf[i] = c;
0090     }
0091     buf[i] = '\0';
0092 
0093     return i;
0094 }
0095 
0096 /**
0097  * fw_csr_string() - reads a string from the configuration ROM
0098  * @directory:  e.g. root directory or unit directory
0099  * @key:    the key of the preceding directory entry
0100  * @buf:    where to put the string
0101  * @size:   size of @buf, in bytes
0102  *
0103  * The string is taken from a minimal ASCII text descriptor leaf after
0104  * the immediate entry with @key.  The string is zero-terminated.
0105  * An overlong string is silently truncated such that it and the
0106  * zero byte fit into @size.
0107  *
0108  * Returns strlen(buf) or a negative error code.
0109  */
0110 int fw_csr_string(const u32 *directory, int key, char *buf, size_t size)
0111 {
0112     const u32 *leaf = search_leaf(directory, key);
0113     if (!leaf)
0114         return -ENOENT;
0115 
0116     return textual_leaf_to_string(leaf, buf, size);
0117 }
0118 EXPORT_SYMBOL(fw_csr_string);
0119 
0120 static void get_ids(const u32 *directory, int *id)
0121 {
0122     struct fw_csr_iterator ci;
0123     int key, value;
0124 
0125     fw_csr_iterator_init(&ci, directory);
0126     while (fw_csr_iterator_next(&ci, &key, &value)) {
0127         switch (key) {
0128         case CSR_VENDOR:    id[0] = value; break;
0129         case CSR_MODEL:     id[1] = value; break;
0130         case CSR_SPECIFIER_ID:  id[2] = value; break;
0131         case CSR_VERSION:   id[3] = value; break;
0132         }
0133     }
0134 }
0135 
0136 static void get_modalias_ids(struct fw_unit *unit, int *id)
0137 {
0138     get_ids(&fw_parent_device(unit)->config_rom[5], id);
0139     get_ids(unit->directory, id);
0140 }
0141 
0142 static bool match_ids(const struct ieee1394_device_id *id_table, int *id)
0143 {
0144     int match = 0;
0145 
0146     if (id[0] == id_table->vendor_id)
0147         match |= IEEE1394_MATCH_VENDOR_ID;
0148     if (id[1] == id_table->model_id)
0149         match |= IEEE1394_MATCH_MODEL_ID;
0150     if (id[2] == id_table->specifier_id)
0151         match |= IEEE1394_MATCH_SPECIFIER_ID;
0152     if (id[3] == id_table->version)
0153         match |= IEEE1394_MATCH_VERSION;
0154 
0155     return (match & id_table->match_flags) == id_table->match_flags;
0156 }
0157 
0158 static const struct ieee1394_device_id *unit_match(struct device *dev,
0159                            struct device_driver *drv)
0160 {
0161     const struct ieee1394_device_id *id_table =
0162             container_of(drv, struct fw_driver, driver)->id_table;
0163     int id[] = {0, 0, 0, 0};
0164 
0165     get_modalias_ids(fw_unit(dev), id);
0166 
0167     for (; id_table->match_flags != 0; id_table++)
0168         if (match_ids(id_table, id))
0169             return id_table;
0170 
0171     return NULL;
0172 }
0173 
0174 static bool is_fw_unit(struct device *dev);
0175 
0176 static int fw_unit_match(struct device *dev, struct device_driver *drv)
0177 {
0178     /* We only allow binding to fw_units. */
0179     return is_fw_unit(dev) && unit_match(dev, drv) != NULL;
0180 }
0181 
0182 static int fw_unit_probe(struct device *dev)
0183 {
0184     struct fw_driver *driver =
0185             container_of(dev->driver, struct fw_driver, driver);
0186 
0187     return driver->probe(fw_unit(dev), unit_match(dev, dev->driver));
0188 }
0189 
0190 static void fw_unit_remove(struct device *dev)
0191 {
0192     struct fw_driver *driver =
0193             container_of(dev->driver, struct fw_driver, driver);
0194 
0195     driver->remove(fw_unit(dev));
0196 }
0197 
0198 static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
0199 {
0200     int id[] = {0, 0, 0, 0};
0201 
0202     get_modalias_ids(unit, id);
0203 
0204     return snprintf(buffer, buffer_size,
0205             "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
0206             id[0], id[1], id[2], id[3]);
0207 }
0208 
0209 static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
0210 {
0211     struct fw_unit *unit = fw_unit(dev);
0212     char modalias[64];
0213 
0214     get_modalias(unit, modalias, sizeof(modalias));
0215 
0216     if (add_uevent_var(env, "MODALIAS=%s", modalias))
0217         return -ENOMEM;
0218 
0219     return 0;
0220 }
0221 
0222 struct bus_type fw_bus_type = {
0223     .name = "firewire",
0224     .match = fw_unit_match,
0225     .probe = fw_unit_probe,
0226     .remove = fw_unit_remove,
0227 };
0228 EXPORT_SYMBOL(fw_bus_type);
0229 
0230 int fw_device_enable_phys_dma(struct fw_device *device)
0231 {
0232     int generation = device->generation;
0233 
0234     /* device->node_id, accessed below, must not be older than generation */
0235     smp_rmb();
0236 
0237     return device->card->driver->enable_phys_dma(device->card,
0238                              device->node_id,
0239                              generation);
0240 }
0241 EXPORT_SYMBOL(fw_device_enable_phys_dma);
0242 
0243 struct config_rom_attribute {
0244     struct device_attribute attr;
0245     u32 key;
0246 };
0247 
0248 static ssize_t show_immediate(struct device *dev,
0249                   struct device_attribute *dattr, char *buf)
0250 {
0251     struct config_rom_attribute *attr =
0252         container_of(dattr, struct config_rom_attribute, attr);
0253     struct fw_csr_iterator ci;
0254     const u32 *dir;
0255     int key, value, ret = -ENOENT;
0256 
0257     down_read(&fw_device_rwsem);
0258 
0259     if (is_fw_unit(dev))
0260         dir = fw_unit(dev)->directory;
0261     else
0262         dir = fw_device(dev)->config_rom + 5;
0263 
0264     fw_csr_iterator_init(&ci, dir);
0265     while (fw_csr_iterator_next(&ci, &key, &value))
0266         if (attr->key == key) {
0267             ret = snprintf(buf, buf ? PAGE_SIZE : 0,
0268                        "0x%06x\n", value);
0269             break;
0270         }
0271 
0272     up_read(&fw_device_rwsem);
0273 
0274     return ret;
0275 }
0276 
0277 #define IMMEDIATE_ATTR(name, key)               \
0278     { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
0279 
0280 static ssize_t show_text_leaf(struct device *dev,
0281                   struct device_attribute *dattr, char *buf)
0282 {
0283     struct config_rom_attribute *attr =
0284         container_of(dattr, struct config_rom_attribute, attr);
0285     const u32 *dir;
0286     size_t bufsize;
0287     char dummy_buf[2];
0288     int ret;
0289 
0290     down_read(&fw_device_rwsem);
0291 
0292     if (is_fw_unit(dev))
0293         dir = fw_unit(dev)->directory;
0294     else
0295         dir = fw_device(dev)->config_rom + 5;
0296 
0297     if (buf) {
0298         bufsize = PAGE_SIZE - 1;
0299     } else {
0300         buf = dummy_buf;
0301         bufsize = 1;
0302     }
0303 
0304     ret = fw_csr_string(dir, attr->key, buf, bufsize);
0305 
0306     if (ret >= 0) {
0307         /* Strip trailing whitespace and add newline. */
0308         while (ret > 0 && isspace(buf[ret - 1]))
0309             ret--;
0310         strcpy(buf + ret, "\n");
0311         ret++;
0312     }
0313 
0314     up_read(&fw_device_rwsem);
0315 
0316     return ret;
0317 }
0318 
0319 #define TEXT_LEAF_ATTR(name, key)               \
0320     { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
0321 
0322 static struct config_rom_attribute config_rom_attributes[] = {
0323     IMMEDIATE_ATTR(vendor, CSR_VENDOR),
0324     IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
0325     IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
0326     IMMEDIATE_ATTR(version, CSR_VERSION),
0327     IMMEDIATE_ATTR(model, CSR_MODEL),
0328     TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
0329     TEXT_LEAF_ATTR(model_name, CSR_MODEL),
0330     TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
0331 };
0332 
0333 static void init_fw_attribute_group(struct device *dev,
0334                     struct device_attribute *attrs,
0335                     struct fw_attribute_group *group)
0336 {
0337     struct device_attribute *attr;
0338     int i, j;
0339 
0340     for (j = 0; attrs[j].attr.name != NULL; j++)
0341         group->attrs[j] = &attrs[j].attr;
0342 
0343     for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
0344         attr = &config_rom_attributes[i].attr;
0345         if (attr->show(dev, attr, NULL) < 0)
0346             continue;
0347         group->attrs[j++] = &attr->attr;
0348     }
0349 
0350     group->attrs[j] = NULL;
0351     group->groups[0] = &group->group;
0352     group->groups[1] = NULL;
0353     group->group.attrs = group->attrs;
0354     dev->groups = (const struct attribute_group **) group->groups;
0355 }
0356 
0357 static ssize_t modalias_show(struct device *dev,
0358                  struct device_attribute *attr, char *buf)
0359 {
0360     struct fw_unit *unit = fw_unit(dev);
0361     int length;
0362 
0363     length = get_modalias(unit, buf, PAGE_SIZE);
0364     strcpy(buf + length, "\n");
0365 
0366     return length + 1;
0367 }
0368 
0369 static ssize_t rom_index_show(struct device *dev,
0370                   struct device_attribute *attr, char *buf)
0371 {
0372     struct fw_device *device = fw_device(dev->parent);
0373     struct fw_unit *unit = fw_unit(dev);
0374 
0375     return sysfs_emit(buf, "%td\n", unit->directory - device->config_rom);
0376 }
0377 
0378 static struct device_attribute fw_unit_attributes[] = {
0379     __ATTR_RO(modalias),
0380     __ATTR_RO(rom_index),
0381     __ATTR_NULL,
0382 };
0383 
0384 static ssize_t config_rom_show(struct device *dev,
0385                    struct device_attribute *attr, char *buf)
0386 {
0387     struct fw_device *device = fw_device(dev);
0388     size_t length;
0389 
0390     down_read(&fw_device_rwsem);
0391     length = device->config_rom_length * 4;
0392     memcpy(buf, device->config_rom, length);
0393     up_read(&fw_device_rwsem);
0394 
0395     return length;
0396 }
0397 
0398 static ssize_t guid_show(struct device *dev,
0399              struct device_attribute *attr, char *buf)
0400 {
0401     struct fw_device *device = fw_device(dev);
0402     int ret;
0403 
0404     down_read(&fw_device_rwsem);
0405     ret = sysfs_emit(buf, "0x%08x%08x\n", device->config_rom[3], device->config_rom[4]);
0406     up_read(&fw_device_rwsem);
0407 
0408     return ret;
0409 }
0410 
0411 static ssize_t is_local_show(struct device *dev,
0412                  struct device_attribute *attr, char *buf)
0413 {
0414     struct fw_device *device = fw_device(dev);
0415 
0416     return sprintf(buf, "%u\n", device->is_local);
0417 }
0418 
0419 static int units_sprintf(char *buf, const u32 *directory)
0420 {
0421     struct fw_csr_iterator ci;
0422     int key, value;
0423     int specifier_id = 0;
0424     int version = 0;
0425 
0426     fw_csr_iterator_init(&ci, directory);
0427     while (fw_csr_iterator_next(&ci, &key, &value)) {
0428         switch (key) {
0429         case CSR_SPECIFIER_ID:
0430             specifier_id = value;
0431             break;
0432         case CSR_VERSION:
0433             version = value;
0434             break;
0435         }
0436     }
0437 
0438     return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
0439 }
0440 
0441 static ssize_t units_show(struct device *dev,
0442               struct device_attribute *attr, char *buf)
0443 {
0444     struct fw_device *device = fw_device(dev);
0445     struct fw_csr_iterator ci;
0446     int key, value, i = 0;
0447 
0448     down_read(&fw_device_rwsem);
0449     fw_csr_iterator_init(&ci, &device->config_rom[5]);
0450     while (fw_csr_iterator_next(&ci, &key, &value)) {
0451         if (key != (CSR_UNIT | CSR_DIRECTORY))
0452             continue;
0453         i += units_sprintf(&buf[i], ci.p + value - 1);
0454         if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
0455             break;
0456     }
0457     up_read(&fw_device_rwsem);
0458 
0459     if (i)
0460         buf[i - 1] = '\n';
0461 
0462     return i;
0463 }
0464 
0465 static struct device_attribute fw_device_attributes[] = {
0466     __ATTR_RO(config_rom),
0467     __ATTR_RO(guid),
0468     __ATTR_RO(is_local),
0469     __ATTR_RO(units),
0470     __ATTR_NULL,
0471 };
0472 
0473 static int read_rom(struct fw_device *device,
0474             int generation, int index, u32 *data)
0475 {
0476     u64 offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;
0477     int i, rcode;
0478 
0479     /* device->node_id, accessed below, must not be older than generation */
0480     smp_rmb();
0481 
0482     for (i = 10; i < 100; i += 10) {
0483         rcode = fw_run_transaction(device->card,
0484                 TCODE_READ_QUADLET_REQUEST, device->node_id,
0485                 generation, device->max_speed, offset, data, 4);
0486         if (rcode != RCODE_BUSY)
0487             break;
0488         msleep(i);
0489     }
0490     be32_to_cpus(data);
0491 
0492     return rcode;
0493 }
0494 
0495 #define MAX_CONFIG_ROM_SIZE 256
0496 
0497 /*
0498  * Read the bus info block, perform a speed probe, and read all of the rest of
0499  * the config ROM.  We do all this with a cached bus generation.  If the bus
0500  * generation changes under us, read_config_rom will fail and get retried.
0501  * It's better to start all over in this case because the node from which we
0502  * are reading the ROM may have changed the ROM during the reset.
0503  * Returns either a result code or a negative error code.
0504  */
0505 static int read_config_rom(struct fw_device *device, int generation)
0506 {
0507     struct fw_card *card = device->card;
0508     const u32 *old_rom, *new_rom;
0509     u32 *rom, *stack;
0510     u32 sp, key;
0511     int i, end, length, ret;
0512 
0513     rom = kmalloc(sizeof(*rom) * MAX_CONFIG_ROM_SIZE +
0514               sizeof(*stack) * MAX_CONFIG_ROM_SIZE, GFP_KERNEL);
0515     if (rom == NULL)
0516         return -ENOMEM;
0517 
0518     stack = &rom[MAX_CONFIG_ROM_SIZE];
0519     memset(rom, 0, sizeof(*rom) * MAX_CONFIG_ROM_SIZE);
0520 
0521     device->max_speed = SCODE_100;
0522 
0523     /* First read the bus info block. */
0524     for (i = 0; i < 5; i++) {
0525         ret = read_rom(device, generation, i, &rom[i]);
0526         if (ret != RCODE_COMPLETE)
0527             goto out;
0528         /*
0529          * As per IEEE1212 7.2, during initialization, devices can
0530          * reply with a 0 for the first quadlet of the config
0531          * rom to indicate that they are booting (for example,
0532          * if the firmware is on the disk of a external
0533          * harddisk).  In that case we just fail, and the
0534          * retry mechanism will try again later.
0535          */
0536         if (i == 0 && rom[i] == 0) {
0537             ret = RCODE_BUSY;
0538             goto out;
0539         }
0540     }
0541 
0542     device->max_speed = device->node->max_speed;
0543 
0544     /*
0545      * Determine the speed of
0546      *   - devices with link speed less than PHY speed,
0547      *   - devices with 1394b PHY (unless only connected to 1394a PHYs),
0548      *   - all devices if there are 1394b repeaters.
0549      * Note, we cannot use the bus info block's link_spd as starting point
0550      * because some buggy firmwares set it lower than necessary and because
0551      * 1394-1995 nodes do not have the field.
0552      */
0553     if ((rom[2] & 0x7) < device->max_speed ||
0554         device->max_speed == SCODE_BETA ||
0555         card->beta_repeaters_present) {
0556         u32 dummy;
0557 
0558         /* for S1600 and S3200 */
0559         if (device->max_speed == SCODE_BETA)
0560             device->max_speed = card->link_speed;
0561 
0562         while (device->max_speed > SCODE_100) {
0563             if (read_rom(device, generation, 0, &dummy) ==
0564                 RCODE_COMPLETE)
0565                 break;
0566             device->max_speed--;
0567         }
0568     }
0569 
0570     /*
0571      * Now parse the config rom.  The config rom is a recursive
0572      * directory structure so we parse it using a stack of
0573      * references to the blocks that make up the structure.  We
0574      * push a reference to the root directory on the stack to
0575      * start things off.
0576      */
0577     length = i;
0578     sp = 0;
0579     stack[sp++] = 0xc0000005;
0580     while (sp > 0) {
0581         /*
0582          * Pop the next block reference of the stack.  The
0583          * lower 24 bits is the offset into the config rom,
0584          * the upper 8 bits are the type of the reference the
0585          * block.
0586          */
0587         key = stack[--sp];
0588         i = key & 0xffffff;
0589         if (WARN_ON(i >= MAX_CONFIG_ROM_SIZE)) {
0590             ret = -ENXIO;
0591             goto out;
0592         }
0593 
0594         /* Read header quadlet for the block to get the length. */
0595         ret = read_rom(device, generation, i, &rom[i]);
0596         if (ret != RCODE_COMPLETE)
0597             goto out;
0598         end = i + (rom[i] >> 16) + 1;
0599         if (end > MAX_CONFIG_ROM_SIZE) {
0600             /*
0601              * This block extends outside the config ROM which is
0602              * a firmware bug.  Ignore this whole block, i.e.
0603              * simply set a fake block length of 0.
0604              */
0605             fw_err(card, "skipped invalid ROM block %x at %llx\n",
0606                    rom[i],
0607                    i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
0608             rom[i] = 0;
0609             end = i;
0610         }
0611         i++;
0612 
0613         /*
0614          * Now read in the block.  If this is a directory
0615          * block, check the entries as we read them to see if
0616          * it references another block, and push it in that case.
0617          */
0618         for (; i < end; i++) {
0619             ret = read_rom(device, generation, i, &rom[i]);
0620             if (ret != RCODE_COMPLETE)
0621                 goto out;
0622 
0623             if ((key >> 30) != 3 || (rom[i] >> 30) < 2)
0624                 continue;
0625             /*
0626              * Offset points outside the ROM.  May be a firmware
0627              * bug or an Extended ROM entry (IEEE 1212-2001 clause
0628              * 7.7.18).  Simply overwrite this pointer here by a
0629              * fake immediate entry so that later iterators over
0630              * the ROM don't have to check offsets all the time.
0631              */
0632             if (i + (rom[i] & 0xffffff) >= MAX_CONFIG_ROM_SIZE) {
0633                 fw_err(card,
0634                        "skipped unsupported ROM entry %x at %llx\n",
0635                        rom[i],
0636                        i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
0637                 rom[i] = 0;
0638                 continue;
0639             }
0640             stack[sp++] = i + rom[i];
0641         }
0642         if (length < i)
0643             length = i;
0644     }
0645 
0646     old_rom = device->config_rom;
0647     new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
0648     if (new_rom == NULL) {
0649         ret = -ENOMEM;
0650         goto out;
0651     }
0652 
0653     down_write(&fw_device_rwsem);
0654     device->config_rom = new_rom;
0655     device->config_rom_length = length;
0656     up_write(&fw_device_rwsem);
0657 
0658     kfree(old_rom);
0659     ret = RCODE_COMPLETE;
0660     device->max_rec = rom[2] >> 12 & 0xf;
0661     device->cmc = rom[2] >> 30 & 1;
0662     device->irmc    = rom[2] >> 31 & 1;
0663  out:
0664     kfree(rom);
0665 
0666     return ret;
0667 }
0668 
0669 static void fw_unit_release(struct device *dev)
0670 {
0671     struct fw_unit *unit = fw_unit(dev);
0672 
0673     fw_device_put(fw_parent_device(unit));
0674     kfree(unit);
0675 }
0676 
0677 static struct device_type fw_unit_type = {
0678     .uevent     = fw_unit_uevent,
0679     .release    = fw_unit_release,
0680 };
0681 
0682 static bool is_fw_unit(struct device *dev)
0683 {
0684     return dev->type == &fw_unit_type;
0685 }
0686 
0687 static void create_units(struct fw_device *device)
0688 {
0689     struct fw_csr_iterator ci;
0690     struct fw_unit *unit;
0691     int key, value, i;
0692 
0693     i = 0;
0694     fw_csr_iterator_init(&ci, &device->config_rom[5]);
0695     while (fw_csr_iterator_next(&ci, &key, &value)) {
0696         if (key != (CSR_UNIT | CSR_DIRECTORY))
0697             continue;
0698 
0699         /*
0700          * Get the address of the unit directory and try to
0701          * match the drivers id_tables against it.
0702          */
0703         unit = kzalloc(sizeof(*unit), GFP_KERNEL);
0704         if (unit == NULL)
0705             continue;
0706 
0707         unit->directory = ci.p + value - 1;
0708         unit->device.bus = &fw_bus_type;
0709         unit->device.type = &fw_unit_type;
0710         unit->device.parent = &device->device;
0711         dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
0712 
0713         BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
0714                 ARRAY_SIZE(fw_unit_attributes) +
0715                 ARRAY_SIZE(config_rom_attributes));
0716         init_fw_attribute_group(&unit->device,
0717                     fw_unit_attributes,
0718                     &unit->attribute_group);
0719 
0720         if (device_register(&unit->device) < 0)
0721             goto skip_unit;
0722 
0723         fw_device_get(device);
0724         continue;
0725 
0726     skip_unit:
0727         kfree(unit);
0728     }
0729 }
0730 
0731 static int shutdown_unit(struct device *device, void *data)
0732 {
0733     device_unregister(device);
0734 
0735     return 0;
0736 }
0737 
0738 /*
0739  * fw_device_rwsem acts as dual purpose mutex:
0740  *   - serializes accesses to fw_device_idr,
0741  *   - serializes accesses to fw_device.config_rom/.config_rom_length and
0742  *     fw_unit.directory, unless those accesses happen at safe occasions
0743  */
0744 DECLARE_RWSEM(fw_device_rwsem);
0745 
0746 DEFINE_IDR(fw_device_idr);
0747 int fw_cdev_major;
0748 
0749 struct fw_device *fw_device_get_by_devt(dev_t devt)
0750 {
0751     struct fw_device *device;
0752 
0753     down_read(&fw_device_rwsem);
0754     device = idr_find(&fw_device_idr, MINOR(devt));
0755     if (device)
0756         fw_device_get(device);
0757     up_read(&fw_device_rwsem);
0758 
0759     return device;
0760 }
0761 
0762 struct workqueue_struct *fw_workqueue;
0763 EXPORT_SYMBOL(fw_workqueue);
0764 
0765 static void fw_schedule_device_work(struct fw_device *device,
0766                     unsigned long delay)
0767 {
0768     queue_delayed_work(fw_workqueue, &device->work, delay);
0769 }
0770 
0771 /*
0772  * These defines control the retry behavior for reading the config
0773  * rom.  It shouldn't be necessary to tweak these; if the device
0774  * doesn't respond to a config rom read within 10 seconds, it's not
0775  * going to respond at all.  As for the initial delay, a lot of
0776  * devices will be able to respond within half a second after bus
0777  * reset.  On the other hand, it's not really worth being more
0778  * aggressive than that, since it scales pretty well; if 10 devices
0779  * are plugged in, they're all getting read within one second.
0780  */
0781 
0782 #define MAX_RETRIES 10
0783 #define RETRY_DELAY (3 * HZ)
0784 #define INITIAL_DELAY   (HZ / 2)
0785 #define SHUTDOWN_DELAY  (2 * HZ)
0786 
0787 static void fw_device_shutdown(struct work_struct *work)
0788 {
0789     struct fw_device *device =
0790         container_of(work, struct fw_device, work.work);
0791     int minor = MINOR(device->device.devt);
0792 
0793     if (time_before64(get_jiffies_64(),
0794               device->card->reset_jiffies + SHUTDOWN_DELAY)
0795         && !list_empty(&device->card->link)) {
0796         fw_schedule_device_work(device, SHUTDOWN_DELAY);
0797         return;
0798     }
0799 
0800     if (atomic_cmpxchg(&device->state,
0801                FW_DEVICE_GONE,
0802                FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
0803         return;
0804 
0805     fw_device_cdev_remove(device);
0806     device_for_each_child(&device->device, NULL, shutdown_unit);
0807     device_unregister(&device->device);
0808 
0809     down_write(&fw_device_rwsem);
0810     idr_remove(&fw_device_idr, minor);
0811     up_write(&fw_device_rwsem);
0812 
0813     fw_device_put(device);
0814 }
0815 
0816 static void fw_device_release(struct device *dev)
0817 {
0818     struct fw_device *device = fw_device(dev);
0819     struct fw_card *card = device->card;
0820     unsigned long flags;
0821 
0822     /*
0823      * Take the card lock so we don't set this to NULL while a
0824      * FW_NODE_UPDATED callback is being handled or while the
0825      * bus manager work looks at this node.
0826      */
0827     spin_lock_irqsave(&card->lock, flags);
0828     device->node->data = NULL;
0829     spin_unlock_irqrestore(&card->lock, flags);
0830 
0831     fw_node_put(device->node);
0832     kfree(device->config_rom);
0833     kfree(device);
0834     fw_card_put(card);
0835 }
0836 
0837 static struct device_type fw_device_type = {
0838     .release = fw_device_release,
0839 };
0840 
0841 static bool is_fw_device(struct device *dev)
0842 {
0843     return dev->type == &fw_device_type;
0844 }
0845 
0846 static int update_unit(struct device *dev, void *data)
0847 {
0848     struct fw_unit *unit = fw_unit(dev);
0849     struct fw_driver *driver = (struct fw_driver *)dev->driver;
0850 
0851     if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
0852         device_lock(dev);
0853         driver->update(unit);
0854         device_unlock(dev);
0855     }
0856 
0857     return 0;
0858 }
0859 
0860 static void fw_device_update(struct work_struct *work)
0861 {
0862     struct fw_device *device =
0863         container_of(work, struct fw_device, work.work);
0864 
0865     fw_device_cdev_update(device);
0866     device_for_each_child(&device->device, NULL, update_unit);
0867 }
0868 
0869 /*
0870  * If a device was pending for deletion because its node went away but its
0871  * bus info block and root directory header matches that of a newly discovered
0872  * device, revive the existing fw_device.
0873  * The newly allocated fw_device becomes obsolete instead.
0874  */
0875 static int lookup_existing_device(struct device *dev, void *data)
0876 {
0877     struct fw_device *old = fw_device(dev);
0878     struct fw_device *new = data;
0879     struct fw_card *card = new->card;
0880     int match = 0;
0881 
0882     if (!is_fw_device(dev))
0883         return 0;
0884 
0885     down_read(&fw_device_rwsem); /* serialize config_rom access */
0886     spin_lock_irq(&card->lock);  /* serialize node access */
0887 
0888     if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
0889         atomic_cmpxchg(&old->state,
0890                FW_DEVICE_GONE,
0891                FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
0892         struct fw_node *current_node = new->node;
0893         struct fw_node *obsolete_node = old->node;
0894 
0895         new->node = obsolete_node;
0896         new->node->data = new;
0897         old->node = current_node;
0898         old->node->data = old;
0899 
0900         old->max_speed = new->max_speed;
0901         old->node_id = current_node->node_id;
0902         smp_wmb();  /* update node_id before generation */
0903         old->generation = card->generation;
0904         old->config_rom_retries = 0;
0905         fw_notice(card, "rediscovered device %s\n", dev_name(dev));
0906 
0907         old->workfn = fw_device_update;
0908         fw_schedule_device_work(old, 0);
0909 
0910         if (current_node == card->root_node)
0911             fw_schedule_bm_work(card, 0);
0912 
0913         match = 1;
0914     }
0915 
0916     spin_unlock_irq(&card->lock);
0917     up_read(&fw_device_rwsem);
0918 
0919     return match;
0920 }
0921 
0922 enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
0923 
0924 static void set_broadcast_channel(struct fw_device *device, int generation)
0925 {
0926     struct fw_card *card = device->card;
0927     __be32 data;
0928     int rcode;
0929 
0930     if (!card->broadcast_channel_allocated)
0931         return;
0932 
0933     /*
0934      * The Broadcast_Channel Valid bit is required by nodes which want to
0935      * transmit on this channel.  Such transmissions are practically
0936      * exclusive to IP over 1394 (RFC 2734).  IP capable nodes are required
0937      * to be IRM capable and have a max_rec of 8 or more.  We use this fact
0938      * to narrow down to which nodes we send Broadcast_Channel updates.
0939      */
0940     if (!device->irmc || device->max_rec < 8)
0941         return;
0942 
0943     /*
0944      * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
0945      * Perform a read test first.
0946      */
0947     if (device->bc_implemented == BC_UNKNOWN) {
0948         rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
0949                 device->node_id, generation, device->max_speed,
0950                 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
0951                 &data, 4);
0952         switch (rcode) {
0953         case RCODE_COMPLETE:
0954             if (data & cpu_to_be32(1 << 31)) {
0955                 device->bc_implemented = BC_IMPLEMENTED;
0956                 break;
0957             }
0958             fallthrough;    /* to case address error */
0959         case RCODE_ADDRESS_ERROR:
0960             device->bc_implemented = BC_UNIMPLEMENTED;
0961         }
0962     }
0963 
0964     if (device->bc_implemented == BC_IMPLEMENTED) {
0965         data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
0966                    BROADCAST_CHANNEL_VALID);
0967         fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
0968                 device->node_id, generation, device->max_speed,
0969                 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
0970                 &data, 4);
0971     }
0972 }
0973 
0974 int fw_device_set_broadcast_channel(struct device *dev, void *gen)
0975 {
0976     if (is_fw_device(dev))
0977         set_broadcast_channel(fw_device(dev), (long)gen);
0978 
0979     return 0;
0980 }
0981 
0982 static void fw_device_init(struct work_struct *work)
0983 {
0984     struct fw_device *device =
0985         container_of(work, struct fw_device, work.work);
0986     struct fw_card *card = device->card;
0987     struct device *revived_dev;
0988     int minor, ret;
0989 
0990     /*
0991      * All failure paths here set node->data to NULL, so that we
0992      * don't try to do device_for_each_child() on a kfree()'d
0993      * device.
0994      */
0995 
0996     ret = read_config_rom(device, device->generation);
0997     if (ret != RCODE_COMPLETE) {
0998         if (device->config_rom_retries < MAX_RETRIES &&
0999             atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1000             device->config_rom_retries++;
1001             fw_schedule_device_work(device, RETRY_DELAY);
1002         } else {
1003             if (device->node->link_on)
1004                 fw_notice(card, "giving up on node %x: reading config rom failed: %s\n",
1005                       device->node_id,
1006                       fw_rcode_string(ret));
1007             if (device->node == card->root_node)
1008                 fw_schedule_bm_work(card, 0);
1009             fw_device_release(&device->device);
1010         }
1011         return;
1012     }
1013 
1014     revived_dev = device_find_child(card->device,
1015                     device, lookup_existing_device);
1016     if (revived_dev) {
1017         put_device(revived_dev);
1018         fw_device_release(&device->device);
1019 
1020         return;
1021     }
1022 
1023     device_initialize(&device->device);
1024 
1025     fw_device_get(device);
1026     down_write(&fw_device_rwsem);
1027     minor = idr_alloc(&fw_device_idr, device, 0, 1 << MINORBITS,
1028             GFP_KERNEL);
1029     up_write(&fw_device_rwsem);
1030 
1031     if (minor < 0)
1032         goto error;
1033 
1034     device->device.bus = &fw_bus_type;
1035     device->device.type = &fw_device_type;
1036     device->device.parent = card->device;
1037     device->device.devt = MKDEV(fw_cdev_major, minor);
1038     dev_set_name(&device->device, "fw%d", minor);
1039 
1040     BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
1041             ARRAY_SIZE(fw_device_attributes) +
1042             ARRAY_SIZE(config_rom_attributes));
1043     init_fw_attribute_group(&device->device,
1044                 fw_device_attributes,
1045                 &device->attribute_group);
1046 
1047     if (device_add(&device->device)) {
1048         fw_err(card, "failed to add device\n");
1049         goto error_with_cdev;
1050     }
1051 
1052     create_units(device);
1053 
1054     /*
1055      * Transition the device to running state.  If it got pulled
1056      * out from under us while we did the initialization work, we
1057      * have to shut down the device again here.  Normally, though,
1058      * fw_node_event will be responsible for shutting it down when
1059      * necessary.  We have to use the atomic cmpxchg here to avoid
1060      * racing with the FW_NODE_DESTROYED case in
1061      * fw_node_event().
1062      */
1063     if (atomic_cmpxchg(&device->state,
1064                FW_DEVICE_INITIALIZING,
1065                FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
1066         device->workfn = fw_device_shutdown;
1067         fw_schedule_device_work(device, SHUTDOWN_DELAY);
1068     } else {
1069         fw_notice(card, "created device %s: GUID %08x%08x, S%d00\n",
1070               dev_name(&device->device),
1071               device->config_rom[3], device->config_rom[4],
1072               1 << device->max_speed);
1073         device->config_rom_retries = 0;
1074 
1075         set_broadcast_channel(device, device->generation);
1076 
1077         add_device_randomness(&device->config_rom[3], 8);
1078     }
1079 
1080     /*
1081      * Reschedule the IRM work if we just finished reading the
1082      * root node config rom.  If this races with a bus reset we
1083      * just end up running the IRM work a couple of extra times -
1084      * pretty harmless.
1085      */
1086     if (device->node == card->root_node)
1087         fw_schedule_bm_work(card, 0);
1088 
1089     return;
1090 
1091  error_with_cdev:
1092     down_write(&fw_device_rwsem);
1093     idr_remove(&fw_device_idr, minor);
1094     up_write(&fw_device_rwsem);
1095  error:
1096     fw_device_put(device);      /* fw_device_idr's reference */
1097 
1098     put_device(&device->device);    /* our reference */
1099 }
1100 
1101 /* Reread and compare bus info block and header of root directory */
1102 static int reread_config_rom(struct fw_device *device, int generation,
1103                  bool *changed)
1104 {
1105     u32 q;
1106     int i, rcode;
1107 
1108     for (i = 0; i < 6; i++) {
1109         rcode = read_rom(device, generation, i, &q);
1110         if (rcode != RCODE_COMPLETE)
1111             return rcode;
1112 
1113         if (i == 0 && q == 0)
1114             /* inaccessible (see read_config_rom); retry later */
1115             return RCODE_BUSY;
1116 
1117         if (q != device->config_rom[i]) {
1118             *changed = true;
1119             return RCODE_COMPLETE;
1120         }
1121     }
1122 
1123     *changed = false;
1124     return RCODE_COMPLETE;
1125 }
1126 
1127 static void fw_device_refresh(struct work_struct *work)
1128 {
1129     struct fw_device *device =
1130         container_of(work, struct fw_device, work.work);
1131     struct fw_card *card = device->card;
1132     int ret, node_id = device->node_id;
1133     bool changed;
1134 
1135     ret = reread_config_rom(device, device->generation, &changed);
1136     if (ret != RCODE_COMPLETE)
1137         goto failed_config_rom;
1138 
1139     if (!changed) {
1140         if (atomic_cmpxchg(&device->state,
1141                    FW_DEVICE_INITIALIZING,
1142                    FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1143             goto gone;
1144 
1145         fw_device_update(work);
1146         device->config_rom_retries = 0;
1147         goto out;
1148     }
1149 
1150     /*
1151      * Something changed.  We keep things simple and don't investigate
1152      * further.  We just destroy all previous units and create new ones.
1153      */
1154     device_for_each_child(&device->device, NULL, shutdown_unit);
1155 
1156     ret = read_config_rom(device, device->generation);
1157     if (ret != RCODE_COMPLETE)
1158         goto failed_config_rom;
1159 
1160     fw_device_cdev_update(device);
1161     create_units(device);
1162 
1163     /* Userspace may want to re-read attributes. */
1164     kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1165 
1166     if (atomic_cmpxchg(&device->state,
1167                FW_DEVICE_INITIALIZING,
1168                FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1169         goto gone;
1170 
1171     fw_notice(card, "refreshed device %s\n", dev_name(&device->device));
1172     device->config_rom_retries = 0;
1173     goto out;
1174 
1175  failed_config_rom:
1176     if (device->config_rom_retries < MAX_RETRIES &&
1177         atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1178         device->config_rom_retries++;
1179         fw_schedule_device_work(device, RETRY_DELAY);
1180         return;
1181     }
1182 
1183     fw_notice(card, "giving up on refresh of device %s: %s\n",
1184           dev_name(&device->device), fw_rcode_string(ret));
1185  gone:
1186     atomic_set(&device->state, FW_DEVICE_GONE);
1187     device->workfn = fw_device_shutdown;
1188     fw_schedule_device_work(device, SHUTDOWN_DELAY);
1189  out:
1190     if (node_id == card->root_node->node_id)
1191         fw_schedule_bm_work(card, 0);
1192 }
1193 
1194 static void fw_device_workfn(struct work_struct *work)
1195 {
1196     struct fw_device *device = container_of(to_delayed_work(work),
1197                         struct fw_device, work);
1198     device->workfn(work);
1199 }
1200 
1201 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1202 {
1203     struct fw_device *device;
1204 
1205     switch (event) {
1206     case FW_NODE_CREATED:
1207         /*
1208          * Attempt to scan the node, regardless whether its self ID has
1209          * the L (link active) flag set or not.  Some broken devices
1210          * send L=0 but have an up-and-running link; others send L=1
1211          * without actually having a link.
1212          */
1213  create:
1214         device = kzalloc(sizeof(*device), GFP_ATOMIC);
1215         if (device == NULL)
1216             break;
1217 
1218         /*
1219          * Do minimal initialization of the device here, the
1220          * rest will happen in fw_device_init().
1221          *
1222          * Attention:  A lot of things, even fw_device_get(),
1223          * cannot be done before fw_device_init() finished!
1224          * You can basically just check device->state and
1225          * schedule work until then, but only while holding
1226          * card->lock.
1227          */
1228         atomic_set(&device->state, FW_DEVICE_INITIALIZING);
1229         device->card = fw_card_get(card);
1230         device->node = fw_node_get(node);
1231         device->node_id = node->node_id;
1232         device->generation = card->generation;
1233         device->is_local = node == card->local_node;
1234         mutex_init(&device->client_list_mutex);
1235         INIT_LIST_HEAD(&device->client_list);
1236 
1237         /*
1238          * Set the node data to point back to this device so
1239          * FW_NODE_UPDATED callbacks can update the node_id
1240          * and generation for the device.
1241          */
1242         node->data = device;
1243 
1244         /*
1245          * Many devices are slow to respond after bus resets,
1246          * especially if they are bus powered and go through
1247          * power-up after getting plugged in.  We schedule the
1248          * first config rom scan half a second after bus reset.
1249          */
1250         device->workfn = fw_device_init;
1251         INIT_DELAYED_WORK(&device->work, fw_device_workfn);
1252         fw_schedule_device_work(device, INITIAL_DELAY);
1253         break;
1254 
1255     case FW_NODE_INITIATED_RESET:
1256     case FW_NODE_LINK_ON:
1257         device = node->data;
1258         if (device == NULL)
1259             goto create;
1260 
1261         device->node_id = node->node_id;
1262         smp_wmb();  /* update node_id before generation */
1263         device->generation = card->generation;
1264         if (atomic_cmpxchg(&device->state,
1265                 FW_DEVICE_RUNNING,
1266                 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1267             device->workfn = fw_device_refresh;
1268             fw_schedule_device_work(device,
1269                 device->is_local ? 0 : INITIAL_DELAY);
1270         }
1271         break;
1272 
1273     case FW_NODE_UPDATED:
1274         device = node->data;
1275         if (device == NULL)
1276             break;
1277 
1278         device->node_id = node->node_id;
1279         smp_wmb();  /* update node_id before generation */
1280         device->generation = card->generation;
1281         if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1282             device->workfn = fw_device_update;
1283             fw_schedule_device_work(device, 0);
1284         }
1285         break;
1286 
1287     case FW_NODE_DESTROYED:
1288     case FW_NODE_LINK_OFF:
1289         if (!node->data)
1290             break;
1291 
1292         /*
1293          * Destroy the device associated with the node.  There
1294          * are two cases here: either the device is fully
1295          * initialized (FW_DEVICE_RUNNING) or we're in the
1296          * process of reading its config rom
1297          * (FW_DEVICE_INITIALIZING).  If it is fully
1298          * initialized we can reuse device->work to schedule a
1299          * full fw_device_shutdown().  If not, there's work
1300          * scheduled to read it's config rom, and we just put
1301          * the device in shutdown state to have that code fail
1302          * to create the device.
1303          */
1304         device = node->data;
1305         if (atomic_xchg(&device->state,
1306                 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
1307             device->workfn = fw_device_shutdown;
1308             fw_schedule_device_work(device,
1309                 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
1310         }
1311         break;
1312     }
1313 }