1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Device probing and sysfs code.
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
9 #include <linux/ctype.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/firewire.h>
14 #include <linux/firewire-constants.h>
15 #include <linux/idr.h>
16 #include <linux/jiffies.h>
17 #include <linux/kobject.h>
18 #include <linux/list.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/random.h>
23 #include <linux/rwsem.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <linux/workqueue.h>
29 #include <linux/atomic.h>
30 #include <asm/byteorder.h>
34 #define ROOT_DIR_OFFSET 5
36 void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p)
39 ci->end = ci->p + (p[0] >> 16);
41 EXPORT_SYMBOL(fw_csr_iterator_init);
43 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
46 *value = *ci->p & 0xffffff;
48 return ci->p++ < ci->end;
50 EXPORT_SYMBOL(fw_csr_iterator_next);
52 static const u32 *search_directory(const u32 *directory, int search_key)
54 struct fw_csr_iterator ci;
57 search_key |= CSR_DIRECTORY;
59 fw_csr_iterator_init(&ci, directory);
60 while (fw_csr_iterator_next(&ci, &key, &value)) {
61 if (key == search_key)
62 return ci.p - 1 + value;
68 static const u32 *search_leaf(const u32 *directory, int search_key)
70 struct fw_csr_iterator ci;
71 int last_key = 0, key, value;
73 fw_csr_iterator_init(&ci, directory);
74 while (fw_csr_iterator_next(&ci, &key, &value)) {
75 if (last_key == search_key &&
76 key == (CSR_DESCRIPTOR | CSR_LEAF))
77 return ci.p - 1 + value;
85 static int textual_leaf_to_string(const u32 *block, char *buf, size_t size)
87 unsigned int quadlets, i;
93 quadlets = min(block[0] >> 16, 256U);
97 if (block[1] != 0 || block[2] != 0)
98 /* unknown language/character set */
103 for (i = 0; i < quadlets * 4 && i < size - 1; i++) {
104 c = block[i / 4] >> (24 - 8 * (i % 4));
115 * fw_csr_string() - reads a string from the configuration ROM
116 * @directory: e.g. root directory or unit directory
117 * @key: the key of the preceding directory entry
118 * @buf: where to put the string
119 * @size: size of @buf, in bytes
121 * The string is taken from a minimal ASCII text descriptor leaf after
122 * the immediate entry with @key. The string is zero-terminated.
123 * An overlong string is silently truncated such that it and the
124 * zero byte fit into @size.
126 * Returns strlen(buf) or a negative error code.
128 int fw_csr_string(const u32 *directory, int key, char *buf, size_t size)
130 const u32 *leaf = search_leaf(directory, key);
134 return textual_leaf_to_string(leaf, buf, size);
136 EXPORT_SYMBOL(fw_csr_string);
138 static void get_ids(const u32 *directory, int *id)
140 struct fw_csr_iterator ci;
143 fw_csr_iterator_init(&ci, directory);
144 while (fw_csr_iterator_next(&ci, &key, &value)) {
146 case CSR_VENDOR: id[0] = value; break;
147 case CSR_MODEL: id[1] = value; break;
148 case CSR_SPECIFIER_ID: id[2] = value; break;
149 case CSR_VERSION: id[3] = value; break;
154 static void get_modalias_ids(const struct fw_unit *unit, int *id)
156 const u32 *root_directory = &fw_parent_device(unit)->config_rom[ROOT_DIR_OFFSET];
157 const u32 *directories[] = {NULL, NULL, NULL};
158 const u32 *vendor_directory;
161 directories[0] = root_directory;
163 // Legacy layout of configuration ROM described in Annex 1 of 'Configuration ROM for AV/C
164 // Devices 1.0 (December 12, 2000, 1394 Trading Association, TA Document 1999027)'.
165 vendor_directory = search_directory(root_directory, CSR_VENDOR);
166 if (!vendor_directory) {
167 directories[1] = unit->directory;
169 directories[1] = vendor_directory;
170 directories[2] = unit->directory;
173 for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i)
174 get_ids(directories[i], id);
177 static bool match_ids(const struct ieee1394_device_id *id_table, int *id)
181 if (id[0] == id_table->vendor_id)
182 match |= IEEE1394_MATCH_VENDOR_ID;
183 if (id[1] == id_table->model_id)
184 match |= IEEE1394_MATCH_MODEL_ID;
185 if (id[2] == id_table->specifier_id)
186 match |= IEEE1394_MATCH_SPECIFIER_ID;
187 if (id[3] == id_table->version)
188 match |= IEEE1394_MATCH_VERSION;
190 return (match & id_table->match_flags) == id_table->match_flags;
193 static const struct ieee1394_device_id *unit_match(struct device *dev,
194 struct device_driver *drv)
196 const struct ieee1394_device_id *id_table =
197 container_of(drv, struct fw_driver, driver)->id_table;
198 int id[] = {0, 0, 0, 0};
200 get_modalias_ids(fw_unit(dev), id);
202 for (; id_table->match_flags != 0; id_table++)
203 if (match_ids(id_table, id))
209 static bool is_fw_unit(const struct device *dev);
211 static int fw_unit_match(struct device *dev, struct device_driver *drv)
213 /* We only allow binding to fw_units. */
214 return is_fw_unit(dev) && unit_match(dev, drv) != NULL;
217 static int fw_unit_probe(struct device *dev)
219 struct fw_driver *driver =
220 container_of(dev->driver, struct fw_driver, driver);
222 return driver->probe(fw_unit(dev), unit_match(dev, dev->driver));
225 static void fw_unit_remove(struct device *dev)
227 struct fw_driver *driver =
228 container_of(dev->driver, struct fw_driver, driver);
230 driver->remove(fw_unit(dev));
233 static int get_modalias(const struct fw_unit *unit, char *buffer, size_t buffer_size)
235 int id[] = {0, 0, 0, 0};
237 get_modalias_ids(unit, id);
239 return snprintf(buffer, buffer_size,
240 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
241 id[0], id[1], id[2], id[3]);
244 static int fw_unit_uevent(const struct device *dev, struct kobj_uevent_env *env)
246 const struct fw_unit *unit = fw_unit(dev);
249 get_modalias(unit, modalias, sizeof(modalias));
251 if (add_uevent_var(env, "MODALIAS=%s", modalias))
257 const struct bus_type fw_bus_type = {
259 .match = fw_unit_match,
260 .probe = fw_unit_probe,
261 .remove = fw_unit_remove,
263 EXPORT_SYMBOL(fw_bus_type);
265 int fw_device_enable_phys_dma(struct fw_device *device)
267 int generation = device->generation;
269 /* device->node_id, accessed below, must not be older than generation */
272 return device->card->driver->enable_phys_dma(device->card,
276 EXPORT_SYMBOL(fw_device_enable_phys_dma);
278 struct config_rom_attribute {
279 struct device_attribute attr;
283 static ssize_t show_immediate(struct device *dev,
284 struct device_attribute *dattr, char *buf)
286 struct config_rom_attribute *attr =
287 container_of(dattr, struct config_rom_attribute, attr);
288 struct fw_csr_iterator ci;
289 const u32 *directories[] = {NULL, NULL};
292 down_read(&fw_device_rwsem);
294 if (is_fw_unit(dev)) {
295 directories[0] = fw_unit(dev)->directory;
297 const u32 *root_directory = fw_device(dev)->config_rom + ROOT_DIR_OFFSET;
298 const u32 *vendor_directory = search_directory(root_directory, CSR_VENDOR);
300 if (!vendor_directory) {
301 directories[0] = root_directory;
303 // Legacy layout of configuration ROM described in Annex 1 of
304 // 'Configuration ROM for AV/C Devices 1.0 (December 12, 2000, 1394 Trading
305 // Association, TA Document 1999027)'.
306 directories[0] = vendor_directory;
307 directories[1] = root_directory;
311 for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i) {
314 fw_csr_iterator_init(&ci, directories[i]);
315 while (fw_csr_iterator_next(&ci, &key, &val)) {
316 if (attr->key == key)
321 up_read(&fw_device_rwsem);
326 return snprintf(buf, buf ? PAGE_SIZE : 0, "0x%06x\n", value);
329 #define IMMEDIATE_ATTR(name, key) \
330 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
332 static ssize_t show_text_leaf(struct device *dev,
333 struct device_attribute *dattr, char *buf)
335 struct config_rom_attribute *attr =
336 container_of(dattr, struct config_rom_attribute, attr);
337 const u32 *directories[] = {NULL, NULL};
340 int i, ret = -ENOENT;
342 down_read(&fw_device_rwsem);
344 if (is_fw_unit(dev)) {
345 directories[0] = fw_unit(dev)->directory;
347 const u32 *root_directory = fw_device(dev)->config_rom + ROOT_DIR_OFFSET;
348 const u32 *vendor_directory = search_directory(root_directory, CSR_VENDOR);
350 if (!vendor_directory) {
351 directories[0] = root_directory;
353 // Legacy layout of configuration ROM described in Annex 1 of
354 // 'Configuration ROM for AV/C Devices 1.0 (December 12, 2000, 1394
355 // Trading Association, TA Document 1999027)'.
356 directories[0] = root_directory;
357 directories[1] = vendor_directory;
362 bufsize = PAGE_SIZE - 1;
368 for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i) {
369 int result = fw_csr_string(directories[i], attr->key, buf, bufsize);
376 /* Strip trailing whitespace and add newline. */
377 while (ret > 0 && isspace(buf[ret - 1]))
379 strcpy(buf + ret, "\n");
383 up_read(&fw_device_rwsem);
388 #define TEXT_LEAF_ATTR(name, key) \
389 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
391 static struct config_rom_attribute config_rom_attributes[] = {
392 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
393 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
394 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
395 IMMEDIATE_ATTR(version, CSR_VERSION),
396 IMMEDIATE_ATTR(model, CSR_MODEL),
397 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
398 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
399 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
402 static void init_fw_attribute_group(struct device *dev,
403 struct device_attribute *attrs,
404 struct fw_attribute_group *group)
406 struct device_attribute *attr;
409 for (j = 0; attrs[j].attr.name != NULL; j++)
410 group->attrs[j] = &attrs[j].attr;
412 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
413 attr = &config_rom_attributes[i].attr;
414 if (attr->show(dev, attr, NULL) < 0)
416 group->attrs[j++] = &attr->attr;
419 group->attrs[j] = NULL;
420 group->groups[0] = &group->group;
421 group->groups[1] = NULL;
422 group->group.attrs = group->attrs;
423 dev->groups = (const struct attribute_group **) group->groups;
426 static ssize_t modalias_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 struct fw_unit *unit = fw_unit(dev);
432 length = get_modalias(unit, buf, PAGE_SIZE);
433 strcpy(buf + length, "\n");
438 static ssize_t rom_index_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct fw_device *device = fw_device(dev->parent);
442 struct fw_unit *unit = fw_unit(dev);
444 return sysfs_emit(buf, "%td\n", unit->directory - device->config_rom);
447 static struct device_attribute fw_unit_attributes[] = {
449 __ATTR_RO(rom_index),
453 static ssize_t config_rom_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct fw_device *device = fw_device(dev);
459 down_read(&fw_device_rwsem);
460 length = device->config_rom_length * 4;
461 memcpy(buf, device->config_rom, length);
462 up_read(&fw_device_rwsem);
467 static ssize_t guid_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 struct fw_device *device = fw_device(dev);
473 down_read(&fw_device_rwsem);
474 ret = sysfs_emit(buf, "0x%08x%08x\n", device->config_rom[3], device->config_rom[4]);
475 up_read(&fw_device_rwsem);
480 static ssize_t is_local_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
483 struct fw_device *device = fw_device(dev);
485 return sprintf(buf, "%u\n", device->is_local);
488 static int units_sprintf(char *buf, const u32 *directory)
490 struct fw_csr_iterator ci;
492 int specifier_id = 0;
495 fw_csr_iterator_init(&ci, directory);
496 while (fw_csr_iterator_next(&ci, &key, &value)) {
498 case CSR_SPECIFIER_ID:
499 specifier_id = value;
507 return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
510 static ssize_t units_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 struct fw_device *device = fw_device(dev);
514 struct fw_csr_iterator ci;
515 int key, value, i = 0;
517 down_read(&fw_device_rwsem);
518 fw_csr_iterator_init(&ci, &device->config_rom[ROOT_DIR_OFFSET]);
519 while (fw_csr_iterator_next(&ci, &key, &value)) {
520 if (key != (CSR_UNIT | CSR_DIRECTORY))
522 i += units_sprintf(&buf[i], ci.p + value - 1);
523 if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
526 up_read(&fw_device_rwsem);
534 static struct device_attribute fw_device_attributes[] = {
535 __ATTR_RO(config_rom),
542 static int read_rom(struct fw_device *device,
543 int generation, int index, u32 *data)
545 u64 offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;
548 /* device->node_id, accessed below, must not be older than generation */
551 for (i = 10; i < 100; i += 10) {
552 rcode = fw_run_transaction(device->card,
553 TCODE_READ_QUADLET_REQUEST, device->node_id,
554 generation, device->max_speed, offset, data, 4);
555 if (rcode != RCODE_BUSY)
564 #define MAX_CONFIG_ROM_SIZE 256
567 * Read the bus info block, perform a speed probe, and read all of the rest of
568 * the config ROM. We do all this with a cached bus generation. If the bus
569 * generation changes under us, read_config_rom will fail and get retried.
570 * It's better to start all over in this case because the node from which we
571 * are reading the ROM may have changed the ROM during the reset.
572 * Returns either a result code or a negative error code.
574 static int read_config_rom(struct fw_device *device, int generation)
576 struct fw_card *card = device->card;
577 const u32 *old_rom, *new_rom;
580 int i, end, length, ret;
582 rom = kmalloc(sizeof(*rom) * MAX_CONFIG_ROM_SIZE +
583 sizeof(*stack) * MAX_CONFIG_ROM_SIZE, GFP_KERNEL);
587 stack = &rom[MAX_CONFIG_ROM_SIZE];
588 memset(rom, 0, sizeof(*rom) * MAX_CONFIG_ROM_SIZE);
590 device->max_speed = SCODE_100;
592 /* First read the bus info block. */
593 for (i = 0; i < 5; i++) {
594 ret = read_rom(device, generation, i, &rom[i]);
595 if (ret != RCODE_COMPLETE)
598 * As per IEEE1212 7.2, during initialization, devices can
599 * reply with a 0 for the first quadlet of the config
600 * rom to indicate that they are booting (for example,
601 * if the firmware is on the disk of a external
602 * harddisk). In that case we just fail, and the
603 * retry mechanism will try again later.
605 if (i == 0 && rom[i] == 0) {
611 device->max_speed = device->node->max_speed;
614 * Determine the speed of
615 * - devices with link speed less than PHY speed,
616 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
617 * - all devices if there are 1394b repeaters.
618 * Note, we cannot use the bus info block's link_spd as starting point
619 * because some buggy firmwares set it lower than necessary and because
620 * 1394-1995 nodes do not have the field.
622 if ((rom[2] & 0x7) < device->max_speed ||
623 device->max_speed == SCODE_BETA ||
624 card->beta_repeaters_present) {
627 /* for S1600 and S3200 */
628 if (device->max_speed == SCODE_BETA)
629 device->max_speed = card->link_speed;
631 while (device->max_speed > SCODE_100) {
632 if (read_rom(device, generation, 0, &dummy) ==
640 * Now parse the config rom. The config rom is a recursive
641 * directory structure so we parse it using a stack of
642 * references to the blocks that make up the structure. We
643 * push a reference to the root directory on the stack to
648 stack[sp++] = 0xc0000005;
651 * Pop the next block reference of the stack. The
652 * lower 24 bits is the offset into the config rom,
653 * the upper 8 bits are the type of the reference the
658 if (WARN_ON(i >= MAX_CONFIG_ROM_SIZE)) {
663 /* Read header quadlet for the block to get the length. */
664 ret = read_rom(device, generation, i, &rom[i]);
665 if (ret != RCODE_COMPLETE)
667 end = i + (rom[i] >> 16) + 1;
668 if (end > MAX_CONFIG_ROM_SIZE) {
670 * This block extends outside the config ROM which is
671 * a firmware bug. Ignore this whole block, i.e.
672 * simply set a fake block length of 0.
674 fw_err(card, "skipped invalid ROM block %x at %llx\n",
676 i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
683 * Now read in the block. If this is a directory
684 * block, check the entries as we read them to see if
685 * it references another block, and push it in that case.
687 for (; i < end; i++) {
688 ret = read_rom(device, generation, i, &rom[i]);
689 if (ret != RCODE_COMPLETE)
692 if ((key >> 30) != 3 || (rom[i] >> 30) < 2)
695 * Offset points outside the ROM. May be a firmware
696 * bug or an Extended ROM entry (IEEE 1212-2001 clause
697 * 7.7.18). Simply overwrite this pointer here by a
698 * fake immediate entry so that later iterators over
699 * the ROM don't have to check offsets all the time.
701 if (i + (rom[i] & 0xffffff) >= MAX_CONFIG_ROM_SIZE) {
703 "skipped unsupported ROM entry %x at %llx\n",
705 i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
709 stack[sp++] = i + rom[i];
715 old_rom = device->config_rom;
716 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
717 if (new_rom == NULL) {
722 down_write(&fw_device_rwsem);
723 device->config_rom = new_rom;
724 device->config_rom_length = length;
725 up_write(&fw_device_rwsem);
728 ret = RCODE_COMPLETE;
729 device->max_rec = rom[2] >> 12 & 0xf;
730 device->cmc = rom[2] >> 30 & 1;
731 device->irmc = rom[2] >> 31 & 1;
738 static void fw_unit_release(struct device *dev)
740 struct fw_unit *unit = fw_unit(dev);
742 fw_device_put(fw_parent_device(unit));
746 static struct device_type fw_unit_type = {
747 .uevent = fw_unit_uevent,
748 .release = fw_unit_release,
751 static bool is_fw_unit(const struct device *dev)
753 return dev->type == &fw_unit_type;
756 static void create_units(struct fw_device *device)
758 struct fw_csr_iterator ci;
759 struct fw_unit *unit;
763 fw_csr_iterator_init(&ci, &device->config_rom[ROOT_DIR_OFFSET]);
764 while (fw_csr_iterator_next(&ci, &key, &value)) {
765 if (key != (CSR_UNIT | CSR_DIRECTORY))
769 * Get the address of the unit directory and try to
770 * match the drivers id_tables against it.
772 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
776 unit->directory = ci.p + value - 1;
777 unit->device.bus = &fw_bus_type;
778 unit->device.type = &fw_unit_type;
779 unit->device.parent = &device->device;
780 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
782 BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
783 ARRAY_SIZE(fw_unit_attributes) +
784 ARRAY_SIZE(config_rom_attributes));
785 init_fw_attribute_group(&unit->device,
787 &unit->attribute_group);
789 fw_device_get(device);
790 if (device_register(&unit->device) < 0) {
791 put_device(&unit->device);
797 static int shutdown_unit(struct device *device, void *data)
799 device_unregister(device);
805 * fw_device_rwsem acts as dual purpose mutex:
806 * - serializes accesses to fw_device_idr,
807 * - serializes accesses to fw_device.config_rom/.config_rom_length and
808 * fw_unit.directory, unless those accesses happen at safe occasions
810 DECLARE_RWSEM(fw_device_rwsem);
812 DEFINE_IDR(fw_device_idr);
815 struct fw_device *fw_device_get_by_devt(dev_t devt)
817 struct fw_device *device;
819 down_read(&fw_device_rwsem);
820 device = idr_find(&fw_device_idr, MINOR(devt));
822 fw_device_get(device);
823 up_read(&fw_device_rwsem);
828 struct workqueue_struct *fw_workqueue;
829 EXPORT_SYMBOL(fw_workqueue);
831 static void fw_schedule_device_work(struct fw_device *device,
834 queue_delayed_work(fw_workqueue, &device->work, delay);
838 * These defines control the retry behavior for reading the config
839 * rom. It shouldn't be necessary to tweak these; if the device
840 * doesn't respond to a config rom read within 10 seconds, it's not
841 * going to respond at all. As for the initial delay, a lot of
842 * devices will be able to respond within half a second after bus
843 * reset. On the other hand, it's not really worth being more
844 * aggressive than that, since it scales pretty well; if 10 devices
845 * are plugged in, they're all getting read within one second.
848 #define MAX_RETRIES 10
849 #define RETRY_DELAY (3 * HZ)
850 #define INITIAL_DELAY (HZ / 2)
851 #define SHUTDOWN_DELAY (2 * HZ)
853 static void fw_device_shutdown(struct work_struct *work)
855 struct fw_device *device =
856 container_of(work, struct fw_device, work.work);
857 int minor = MINOR(device->device.devt);
859 if (time_before64(get_jiffies_64(),
860 device->card->reset_jiffies + SHUTDOWN_DELAY)
861 && !list_empty(&device->card->link)) {
862 fw_schedule_device_work(device, SHUTDOWN_DELAY);
866 if (atomic_cmpxchg(&device->state,
868 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
871 fw_device_cdev_remove(device);
872 device_for_each_child(&device->device, NULL, shutdown_unit);
873 device_unregister(&device->device);
875 down_write(&fw_device_rwsem);
876 idr_remove(&fw_device_idr, minor);
877 up_write(&fw_device_rwsem);
879 fw_device_put(device);
882 static void fw_device_release(struct device *dev)
884 struct fw_device *device = fw_device(dev);
885 struct fw_card *card = device->card;
889 * Take the card lock so we don't set this to NULL while a
890 * FW_NODE_UPDATED callback is being handled or while the
891 * bus manager work looks at this node.
893 spin_lock_irqsave(&card->lock, flags);
894 device->node->data = NULL;
895 spin_unlock_irqrestore(&card->lock, flags);
897 fw_node_put(device->node);
898 kfree(device->config_rom);
903 static struct device_type fw_device_type = {
904 .release = fw_device_release,
907 static bool is_fw_device(const struct device *dev)
909 return dev->type == &fw_device_type;
912 static int update_unit(struct device *dev, void *data)
914 struct fw_unit *unit = fw_unit(dev);
915 struct fw_driver *driver = (struct fw_driver *)dev->driver;
917 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
919 driver->update(unit);
926 static void fw_device_update(struct work_struct *work)
928 struct fw_device *device =
929 container_of(work, struct fw_device, work.work);
931 fw_device_cdev_update(device);
932 device_for_each_child(&device->device, NULL, update_unit);
936 * If a device was pending for deletion because its node went away but its
937 * bus info block and root directory header matches that of a newly discovered
938 * device, revive the existing fw_device.
939 * The newly allocated fw_device becomes obsolete instead.
941 static int lookup_existing_device(struct device *dev, void *data)
943 struct fw_device *old = fw_device(dev);
944 struct fw_device *new = data;
945 struct fw_card *card = new->card;
948 if (!is_fw_device(dev))
951 down_read(&fw_device_rwsem); /* serialize config_rom access */
952 spin_lock_irq(&card->lock); /* serialize node access */
954 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
955 atomic_cmpxchg(&old->state,
957 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
958 struct fw_node *current_node = new->node;
959 struct fw_node *obsolete_node = old->node;
961 new->node = obsolete_node;
962 new->node->data = new;
963 old->node = current_node;
964 old->node->data = old;
966 old->max_speed = new->max_speed;
967 old->node_id = current_node->node_id;
968 smp_wmb(); /* update node_id before generation */
969 old->generation = card->generation;
970 old->config_rom_retries = 0;
971 fw_notice(card, "rediscovered device %s\n", dev_name(dev));
973 old->workfn = fw_device_update;
974 fw_schedule_device_work(old, 0);
976 if (current_node == card->root_node)
977 fw_schedule_bm_work(card, 0);
982 spin_unlock_irq(&card->lock);
983 up_read(&fw_device_rwsem);
988 enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
990 static void set_broadcast_channel(struct fw_device *device, int generation)
992 struct fw_card *card = device->card;
996 if (!card->broadcast_channel_allocated)
1000 * The Broadcast_Channel Valid bit is required by nodes which want to
1001 * transmit on this channel. Such transmissions are practically
1002 * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
1003 * to be IRM capable and have a max_rec of 8 or more. We use this fact
1004 * to narrow down to which nodes we send Broadcast_Channel updates.
1006 if (!device->irmc || device->max_rec < 8)
1010 * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
1011 * Perform a read test first.
1013 if (device->bc_implemented == BC_UNKNOWN) {
1014 rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
1015 device->node_id, generation, device->max_speed,
1016 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
1019 case RCODE_COMPLETE:
1020 if (data & cpu_to_be32(1 << 31)) {
1021 device->bc_implemented = BC_IMPLEMENTED;
1024 fallthrough; /* to case address error */
1025 case RCODE_ADDRESS_ERROR:
1026 device->bc_implemented = BC_UNIMPLEMENTED;
1030 if (device->bc_implemented == BC_IMPLEMENTED) {
1031 data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
1032 BROADCAST_CHANNEL_VALID);
1033 fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
1034 device->node_id, generation, device->max_speed,
1035 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
1040 int fw_device_set_broadcast_channel(struct device *dev, void *gen)
1042 if (is_fw_device(dev))
1043 set_broadcast_channel(fw_device(dev), (long)gen);
1048 static void fw_device_init(struct work_struct *work)
1050 struct fw_device *device =
1051 container_of(work, struct fw_device, work.work);
1052 struct fw_card *card = device->card;
1053 struct device *revived_dev;
1057 * All failure paths here set node->data to NULL, so that we
1058 * don't try to do device_for_each_child() on a kfree()'d
1062 ret = read_config_rom(device, device->generation);
1063 if (ret != RCODE_COMPLETE) {
1064 if (device->config_rom_retries < MAX_RETRIES &&
1065 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1066 device->config_rom_retries++;
1067 fw_schedule_device_work(device, RETRY_DELAY);
1069 if (device->node->link_on)
1070 fw_notice(card, "giving up on node %x: reading config rom failed: %s\n",
1072 fw_rcode_string(ret));
1073 if (device->node == card->root_node)
1074 fw_schedule_bm_work(card, 0);
1075 fw_device_release(&device->device);
1080 revived_dev = device_find_child(card->device,
1081 device, lookup_existing_device);
1083 put_device(revived_dev);
1084 fw_device_release(&device->device);
1089 device_initialize(&device->device);
1091 fw_device_get(device);
1092 down_write(&fw_device_rwsem);
1093 minor = idr_alloc(&fw_device_idr, device, 0, 1 << MINORBITS,
1095 up_write(&fw_device_rwsem);
1100 device->device.bus = &fw_bus_type;
1101 device->device.type = &fw_device_type;
1102 device->device.parent = card->device;
1103 device->device.devt = MKDEV(fw_cdev_major, minor);
1104 dev_set_name(&device->device, "fw%d", minor);
1106 BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
1107 ARRAY_SIZE(fw_device_attributes) +
1108 ARRAY_SIZE(config_rom_attributes));
1109 init_fw_attribute_group(&device->device,
1110 fw_device_attributes,
1111 &device->attribute_group);
1113 if (device_add(&device->device)) {
1114 fw_err(card, "failed to add device\n");
1115 goto error_with_cdev;
1118 create_units(device);
1121 * Transition the device to running state. If it got pulled
1122 * out from under us while we did the initialization work, we
1123 * have to shut down the device again here. Normally, though,
1124 * fw_node_event will be responsible for shutting it down when
1125 * necessary. We have to use the atomic cmpxchg here to avoid
1126 * racing with the FW_NODE_DESTROYED case in
1129 if (atomic_cmpxchg(&device->state,
1130 FW_DEVICE_INITIALIZING,
1131 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
1132 device->workfn = fw_device_shutdown;
1133 fw_schedule_device_work(device, SHUTDOWN_DELAY);
1135 fw_notice(card, "created device %s: GUID %08x%08x, S%d00\n",
1136 dev_name(&device->device),
1137 device->config_rom[3], device->config_rom[4],
1138 1 << device->max_speed);
1139 device->config_rom_retries = 0;
1141 set_broadcast_channel(device, device->generation);
1143 add_device_randomness(&device->config_rom[3], 8);
1147 * Reschedule the IRM work if we just finished reading the
1148 * root node config rom. If this races with a bus reset we
1149 * just end up running the IRM work a couple of extra times -
1152 if (device->node == card->root_node)
1153 fw_schedule_bm_work(card, 0);
1158 down_write(&fw_device_rwsem);
1159 idr_remove(&fw_device_idr, minor);
1160 up_write(&fw_device_rwsem);
1162 fw_device_put(device); /* fw_device_idr's reference */
1164 put_device(&device->device); /* our reference */
1167 /* Reread and compare bus info block and header of root directory */
1168 static int reread_config_rom(struct fw_device *device, int generation,
1174 for (i = 0; i < 6; i++) {
1175 rcode = read_rom(device, generation, i, &q);
1176 if (rcode != RCODE_COMPLETE)
1179 if (i == 0 && q == 0)
1180 /* inaccessible (see read_config_rom); retry later */
1183 if (q != device->config_rom[i]) {
1185 return RCODE_COMPLETE;
1190 return RCODE_COMPLETE;
1193 static void fw_device_refresh(struct work_struct *work)
1195 struct fw_device *device =
1196 container_of(work, struct fw_device, work.work);
1197 struct fw_card *card = device->card;
1198 int ret, node_id = device->node_id;
1201 ret = reread_config_rom(device, device->generation, &changed);
1202 if (ret != RCODE_COMPLETE)
1203 goto failed_config_rom;
1206 if (atomic_cmpxchg(&device->state,
1207 FW_DEVICE_INITIALIZING,
1208 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1211 fw_device_update(work);
1212 device->config_rom_retries = 0;
1217 * Something changed. We keep things simple and don't investigate
1218 * further. We just destroy all previous units and create new ones.
1220 device_for_each_child(&device->device, NULL, shutdown_unit);
1222 ret = read_config_rom(device, device->generation);
1223 if (ret != RCODE_COMPLETE)
1224 goto failed_config_rom;
1226 fw_device_cdev_update(device);
1227 create_units(device);
1229 /* Userspace may want to re-read attributes. */
1230 kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1232 if (atomic_cmpxchg(&device->state,
1233 FW_DEVICE_INITIALIZING,
1234 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1237 fw_notice(card, "refreshed device %s\n", dev_name(&device->device));
1238 device->config_rom_retries = 0;
1242 if (device->config_rom_retries < MAX_RETRIES &&
1243 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1244 device->config_rom_retries++;
1245 fw_schedule_device_work(device, RETRY_DELAY);
1249 fw_notice(card, "giving up on refresh of device %s: %s\n",
1250 dev_name(&device->device), fw_rcode_string(ret));
1252 atomic_set(&device->state, FW_DEVICE_GONE);
1253 device->workfn = fw_device_shutdown;
1254 fw_schedule_device_work(device, SHUTDOWN_DELAY);
1256 if (node_id == card->root_node->node_id)
1257 fw_schedule_bm_work(card, 0);
1260 static void fw_device_workfn(struct work_struct *work)
1262 struct fw_device *device = container_of(to_delayed_work(work),
1263 struct fw_device, work);
1264 device->workfn(work);
1267 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1269 struct fw_device *device;
1272 case FW_NODE_CREATED:
1274 * Attempt to scan the node, regardless whether its self ID has
1275 * the L (link active) flag set or not. Some broken devices
1276 * send L=0 but have an up-and-running link; others send L=1
1277 * without actually having a link.
1280 device = kzalloc(sizeof(*device), GFP_ATOMIC);
1285 * Do minimal initialization of the device here, the
1286 * rest will happen in fw_device_init().
1288 * Attention: A lot of things, even fw_device_get(),
1289 * cannot be done before fw_device_init() finished!
1290 * You can basically just check device->state and
1291 * schedule work until then, but only while holding
1294 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
1295 device->card = fw_card_get(card);
1296 device->node = fw_node_get(node);
1297 device->node_id = node->node_id;
1298 device->generation = card->generation;
1299 device->is_local = node == card->local_node;
1300 mutex_init(&device->client_list_mutex);
1301 INIT_LIST_HEAD(&device->client_list);
1304 * Set the node data to point back to this device so
1305 * FW_NODE_UPDATED callbacks can update the node_id
1306 * and generation for the device.
1308 node->data = device;
1311 * Many devices are slow to respond after bus resets,
1312 * especially if they are bus powered and go through
1313 * power-up after getting plugged in. We schedule the
1314 * first config rom scan half a second after bus reset.
1316 device->workfn = fw_device_init;
1317 INIT_DELAYED_WORK(&device->work, fw_device_workfn);
1318 fw_schedule_device_work(device, INITIAL_DELAY);
1321 case FW_NODE_INITIATED_RESET:
1322 case FW_NODE_LINK_ON:
1323 device = node->data;
1327 device->node_id = node->node_id;
1328 smp_wmb(); /* update node_id before generation */
1329 device->generation = card->generation;
1330 if (atomic_cmpxchg(&device->state,
1332 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1333 device->workfn = fw_device_refresh;
1334 fw_schedule_device_work(device,
1335 device->is_local ? 0 : INITIAL_DELAY);
1339 case FW_NODE_UPDATED:
1340 device = node->data;
1344 device->node_id = node->node_id;
1345 smp_wmb(); /* update node_id before generation */
1346 device->generation = card->generation;
1347 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1348 device->workfn = fw_device_update;
1349 fw_schedule_device_work(device, 0);
1353 case FW_NODE_DESTROYED:
1354 case FW_NODE_LINK_OFF:
1359 * Destroy the device associated with the node. There
1360 * are two cases here: either the device is fully
1361 * initialized (FW_DEVICE_RUNNING) or we're in the
1362 * process of reading its config rom
1363 * (FW_DEVICE_INITIALIZING). If it is fully
1364 * initialized we can reuse device->work to schedule a
1365 * full fw_device_shutdown(). If not, there's work
1366 * scheduled to read it's config rom, and we just put
1367 * the device in shutdown state to have that code fail
1368 * to create the device.
1370 device = node->data;
1371 if (atomic_xchg(&device->state,
1372 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
1373 device->workfn = fw_device_shutdown;
1374 fw_schedule_device_work(device,
1375 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
1381 #ifdef CONFIG_FIREWIRE_KUNIT_DEVICE_ATTRIBUTE_TEST
1382 #include "device-attribute-test.c"