2 * Device probing and sysfs code.
4 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/ctype.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/idr.h>
26 #include <linux/jiffies.h>
27 #include <linux/kobject.h>
28 #include <linux/list.h>
29 #include <linux/mod_devicetable.h>
30 #include <linux/mutex.h>
31 #include <linux/rwsem.h>
32 #include <linux/semaphore.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/workqueue.h>
37 #include <asm/system.h>
39 #include "fw-device.h"
40 #include "fw-topology.h"
41 #include "fw-transaction.h"
43 void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
46 ci->end = ci->p + (p[0] >> 16);
48 EXPORT_SYMBOL(fw_csr_iterator_init);
50 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
53 *value = *ci->p & 0xffffff;
55 return ci->p++ < ci->end;
57 EXPORT_SYMBOL(fw_csr_iterator_next);
59 static int is_fw_unit(struct device *dev);
61 static int match_unit_directory(u32 *directory,
62 const struct ieee1394_device_id *id)
64 struct fw_csr_iterator ci;
65 int key, value, match;
68 fw_csr_iterator_init(&ci, directory);
69 while (fw_csr_iterator_next(&ci, &key, &value)) {
70 if (key == CSR_VENDOR && value == id->vendor_id)
71 match |= IEEE1394_MATCH_VENDOR_ID;
72 if (key == CSR_MODEL && value == id->model_id)
73 match |= IEEE1394_MATCH_MODEL_ID;
74 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
75 match |= IEEE1394_MATCH_SPECIFIER_ID;
76 if (key == CSR_VERSION && value == id->version)
77 match |= IEEE1394_MATCH_VERSION;
80 return (match & id->match_flags) == id->match_flags;
83 static int fw_unit_match(struct device *dev, struct device_driver *drv)
85 struct fw_unit *unit = fw_unit(dev);
86 struct fw_driver *driver = fw_driver(drv);
89 /* We only allow binding to fw_units. */
93 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
94 if (match_unit_directory(unit->directory, &driver->id_table[i]))
101 static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
103 struct fw_device *device = fw_device(unit->device.parent);
104 struct fw_csr_iterator ci;
109 int specifier_id = 0;
112 fw_csr_iterator_init(&ci, &device->config_rom[5]);
113 while (fw_csr_iterator_next(&ci, &key, &value)) {
124 fw_csr_iterator_init(&ci, unit->directory);
125 while (fw_csr_iterator_next(&ci, &key, &value)) {
127 case CSR_SPECIFIER_ID:
128 specifier_id = value;
136 return snprintf(buffer, buffer_size,
137 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
138 vendor, model, specifier_id, version);
141 static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
143 struct fw_unit *unit = fw_unit(dev);
146 get_modalias(unit, modalias, sizeof(modalias));
148 if (add_uevent_var(env, "MODALIAS=%s", modalias))
154 struct bus_type fw_bus_type = {
156 .match = fw_unit_match,
158 EXPORT_SYMBOL(fw_bus_type);
160 int fw_device_enable_phys_dma(struct fw_device *device)
162 int generation = device->generation;
164 /* device->node_id, accessed below, must not be older than generation */
167 return device->card->driver->enable_phys_dma(device->card,
171 EXPORT_SYMBOL(fw_device_enable_phys_dma);
173 struct config_rom_attribute {
174 struct device_attribute attr;
178 static ssize_t show_immediate(struct device *dev,
179 struct device_attribute *dattr, char *buf)
181 struct config_rom_attribute *attr =
182 container_of(dattr, struct config_rom_attribute, attr);
183 struct fw_csr_iterator ci;
185 int key, value, ret = -ENOENT;
187 down_read(&fw_device_rwsem);
190 dir = fw_unit(dev)->directory;
192 dir = fw_device(dev)->config_rom + 5;
194 fw_csr_iterator_init(&ci, dir);
195 while (fw_csr_iterator_next(&ci, &key, &value))
196 if (attr->key == key) {
197 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
202 up_read(&fw_device_rwsem);
207 #define IMMEDIATE_ATTR(name, key) \
208 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
210 static ssize_t show_text_leaf(struct device *dev,
211 struct device_attribute *dattr, char *buf)
213 struct config_rom_attribute *attr =
214 container_of(dattr, struct config_rom_attribute, attr);
215 struct fw_csr_iterator ci;
216 u32 *dir, *block = NULL, *p, *end;
217 int length, key, value, last_key = 0, ret = -ENOENT;
220 down_read(&fw_device_rwsem);
223 dir = fw_unit(dev)->directory;
225 dir = fw_device(dev)->config_rom + 5;
227 fw_csr_iterator_init(&ci, dir);
228 while (fw_csr_iterator_next(&ci, &key, &value)) {
229 if (attr->key == last_key &&
230 key == (CSR_DESCRIPTOR | CSR_LEAF))
231 block = ci.p - 1 + value;
238 length = min(block[0] >> 16, 256U);
242 if (block[1] != 0 || block[2] != 0)
243 /* Unknown encoding. */
252 end = &block[length + 1];
253 for (p = &block[3]; p < end; p++, b += 4)
254 * (u32 *) b = (__force u32) __cpu_to_be32(*p);
256 /* Strip trailing whitespace and add newline. */
257 while (b--, (isspace(*b) || *b == '\0') && b > buf);
261 up_read(&fw_device_rwsem);
266 #define TEXT_LEAF_ATTR(name, key) \
267 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
269 static struct config_rom_attribute config_rom_attributes[] = {
270 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
271 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
272 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
273 IMMEDIATE_ATTR(version, CSR_VERSION),
274 IMMEDIATE_ATTR(model, CSR_MODEL),
275 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
276 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
277 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
280 static void init_fw_attribute_group(struct device *dev,
281 struct device_attribute *attrs,
282 struct fw_attribute_group *group)
284 struct device_attribute *attr;
287 for (j = 0; attrs[j].attr.name != NULL; j++)
288 group->attrs[j] = &attrs[j].attr;
290 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
291 attr = &config_rom_attributes[i].attr;
292 if (attr->show(dev, attr, NULL) < 0)
294 group->attrs[j++] = &attr->attr;
297 group->attrs[j] = NULL;
298 group->groups[0] = &group->group;
299 group->groups[1] = NULL;
300 group->group.attrs = group->attrs;
301 dev->groups = group->groups;
304 static ssize_t modalias_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct fw_unit *unit = fw_unit(dev);
310 length = get_modalias(unit, buf, PAGE_SIZE);
311 strcpy(buf + length, "\n");
316 static ssize_t rom_index_show(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 struct fw_device *device = fw_device(dev->parent);
320 struct fw_unit *unit = fw_unit(dev);
322 return snprintf(buf, PAGE_SIZE, "%d\n",
323 (int)(unit->directory - device->config_rom));
326 static struct device_attribute fw_unit_attributes[] = {
328 __ATTR_RO(rom_index),
332 static ssize_t config_rom_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
335 struct fw_device *device = fw_device(dev);
338 down_read(&fw_device_rwsem);
339 length = device->config_rom_length * 4;
340 memcpy(buf, device->config_rom, length);
341 up_read(&fw_device_rwsem);
346 static ssize_t guid_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 struct fw_device *device = fw_device(dev);
352 down_read(&fw_device_rwsem);
353 ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
354 device->config_rom[3], device->config_rom[4]);
355 up_read(&fw_device_rwsem);
360 static int units_sprintf(char *buf, u32 *directory)
362 struct fw_csr_iterator ci;
364 int specifier_id = 0;
367 fw_csr_iterator_init(&ci, directory);
368 while (fw_csr_iterator_next(&ci, &key, &value)) {
370 case CSR_SPECIFIER_ID:
371 specifier_id = value;
379 return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
382 static ssize_t units_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 struct fw_device *device = fw_device(dev);
386 struct fw_csr_iterator ci;
387 int key, value, i = 0;
389 down_read(&fw_device_rwsem);
390 fw_csr_iterator_init(&ci, &device->config_rom[5]);
391 while (fw_csr_iterator_next(&ci, &key, &value)) {
392 if (key != (CSR_UNIT | CSR_DIRECTORY))
394 i += units_sprintf(&buf[i], ci.p + value - 1);
395 if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
398 up_read(&fw_device_rwsem);
406 static struct device_attribute fw_device_attributes[] = {
407 __ATTR_RO(config_rom),
413 static int read_rom(struct fw_device *device,
414 int generation, int index, u32 *data)
418 /* device->node_id, accessed below, must not be older than generation */
421 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
422 device->node_id, generation, device->max_speed,
423 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
430 #define READ_BIB_ROM_SIZE 256
431 #define READ_BIB_STACK_SIZE 16
434 * Read the bus info block, perform a speed probe, and read all of the rest of
435 * the config ROM. We do all this with a cached bus generation. If the bus
436 * generation changes under us, read_bus_info_block will fail and get retried.
437 * It's better to start all over in this case because the node from which we
438 * are reading the ROM may have changed the ROM during the reset.
440 static int read_bus_info_block(struct fw_device *device, int generation)
442 u32 *rom, *stack, *old_rom, *new_rom;
444 int i, end, length, ret = -1;
446 rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
447 sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
451 stack = &rom[READ_BIB_ROM_SIZE];
453 device->max_speed = SCODE_100;
455 /* First read the bus info block. */
456 for (i = 0; i < 5; i++) {
457 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
460 * As per IEEE1212 7.2, during power-up, devices can
461 * reply with a 0 for the first quadlet of the config
462 * rom to indicate that they are booting (for example,
463 * if the firmware is on the disk of a external
464 * harddisk). In that case we just fail, and the
465 * retry mechanism will try again later.
467 if (i == 0 && rom[i] == 0)
471 device->max_speed = device->node->max_speed;
474 * Determine the speed of
475 * - devices with link speed less than PHY speed,
476 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
477 * - all devices if there are 1394b repeaters.
478 * Note, we cannot use the bus info block's link_spd as starting point
479 * because some buggy firmwares set it lower than necessary and because
480 * 1394-1995 nodes do not have the field.
482 if ((rom[2] & 0x7) < device->max_speed ||
483 device->max_speed == SCODE_BETA ||
484 device->card->beta_repeaters_present) {
487 /* for S1600 and S3200 */
488 if (device->max_speed == SCODE_BETA)
489 device->max_speed = device->card->link_speed;
491 while (device->max_speed > SCODE_100) {
492 if (read_rom(device, generation, 0, &dummy) ==
500 * Now parse the config rom. The config rom is a recursive
501 * directory structure so we parse it using a stack of
502 * references to the blocks that make up the structure. We
503 * push a reference to the root directory on the stack to
508 stack[sp++] = 0xc0000005;
511 * Pop the next block reference of the stack. The
512 * lower 24 bits is the offset into the config rom,
513 * the upper 8 bits are the type of the reference the
518 if (i >= READ_BIB_ROM_SIZE)
520 * The reference points outside the standard
521 * config rom area, something's fishy.
525 /* Read header quadlet for the block to get the length. */
526 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
528 end = i + (rom[i] >> 16) + 1;
530 if (end > READ_BIB_ROM_SIZE)
532 * This block extends outside standard config
533 * area (and the array we're reading it
534 * into). That's broken, so ignore this
540 * Now read in the block. If this is a directory
541 * block, check the entries as we read them to see if
542 * it references another block, and push it in that case.
545 if (read_rom(device, generation, i, &rom[i]) !=
548 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
549 sp < READ_BIB_STACK_SIZE)
550 stack[sp++] = i + rom[i];
557 old_rom = device->config_rom;
558 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
562 down_write(&fw_device_rwsem);
563 device->config_rom = new_rom;
564 device->config_rom_length = length;
565 up_write(&fw_device_rwsem);
569 device->cmc = rom[2] >> 30 & 1;
576 static void fw_unit_release(struct device *dev)
578 struct fw_unit *unit = fw_unit(dev);
583 static struct device_type fw_unit_type = {
584 .uevent = fw_unit_uevent,
585 .release = fw_unit_release,
588 static int is_fw_unit(struct device *dev)
590 return dev->type == &fw_unit_type;
593 static void create_units(struct fw_device *device)
595 struct fw_csr_iterator ci;
596 struct fw_unit *unit;
600 fw_csr_iterator_init(&ci, &device->config_rom[5]);
601 while (fw_csr_iterator_next(&ci, &key, &value)) {
602 if (key != (CSR_UNIT | CSR_DIRECTORY))
606 * Get the address of the unit directory and try to
607 * match the drivers id_tables against it.
609 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
611 fw_error("failed to allocate memory for unit\n");
615 unit->directory = ci.p + value - 1;
616 unit->device.bus = &fw_bus_type;
617 unit->device.type = &fw_unit_type;
618 unit->device.parent = &device->device;
619 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
621 BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
622 ARRAY_SIZE(fw_unit_attributes) +
623 ARRAY_SIZE(config_rom_attributes));
624 init_fw_attribute_group(&unit->device,
626 &unit->attribute_group);
628 if (device_register(&unit->device) < 0)
638 static int shutdown_unit(struct device *device, void *data)
640 device_unregister(device);
646 * fw_device_rwsem acts as dual purpose mutex:
647 * - serializes accesses to fw_device_idr,
648 * - serializes accesses to fw_device.config_rom/.config_rom_length and
649 * fw_unit.directory, unless those accesses happen at safe occasions
651 DECLARE_RWSEM(fw_device_rwsem);
653 DEFINE_IDR(fw_device_idr);
656 struct fw_device *fw_device_get_by_devt(dev_t devt)
658 struct fw_device *device;
660 down_read(&fw_device_rwsem);
661 device = idr_find(&fw_device_idr, MINOR(devt));
663 fw_device_get(device);
664 up_read(&fw_device_rwsem);
670 * These defines control the retry behavior for reading the config
671 * rom. It shouldn't be necessary to tweak these; if the device
672 * doesn't respond to a config rom read within 10 seconds, it's not
673 * going to respond at all. As for the initial delay, a lot of
674 * devices will be able to respond within half a second after bus
675 * reset. On the other hand, it's not really worth being more
676 * aggressive than that, since it scales pretty well; if 10 devices
677 * are plugged in, they're all getting read within one second.
680 #define MAX_RETRIES 10
681 #define RETRY_DELAY (3 * HZ)
682 #define INITIAL_DELAY (HZ / 2)
683 #define SHUTDOWN_DELAY (2 * HZ)
685 static void fw_device_shutdown(struct work_struct *work)
687 struct fw_device *device =
688 container_of(work, struct fw_device, work.work);
689 int minor = MINOR(device->device.devt);
691 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
692 && !list_empty(&device->card->link)) {
693 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
697 if (atomic_cmpxchg(&device->state,
699 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
702 fw_device_cdev_remove(device);
703 device_for_each_child(&device->device, NULL, shutdown_unit);
704 device_unregister(&device->device);
706 down_write(&fw_device_rwsem);
707 idr_remove(&fw_device_idr, minor);
708 up_write(&fw_device_rwsem);
710 fw_device_put(device);
713 static void fw_device_release(struct device *dev)
715 struct fw_device *device = fw_device(dev);
716 struct fw_card *card = device->card;
720 * Take the card lock so we don't set this to NULL while a
721 * FW_NODE_UPDATED callback is being handled or while the
722 * bus manager work looks at this node.
724 spin_lock_irqsave(&card->lock, flags);
725 device->node->data = NULL;
726 spin_unlock_irqrestore(&card->lock, flags);
728 fw_node_put(device->node);
729 kfree(device->config_rom);
734 static struct device_type fw_device_type = {
735 .release = fw_device_release,
738 static int update_unit(struct device *dev, void *data)
740 struct fw_unit *unit = fw_unit(dev);
741 struct fw_driver *driver = (struct fw_driver *)dev->driver;
743 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
745 driver->update(unit);
752 static void fw_device_update(struct work_struct *work)
754 struct fw_device *device =
755 container_of(work, struct fw_device, work.work);
757 fw_device_cdev_update(device);
758 device_for_each_child(&device->device, NULL, update_unit);
762 * If a device was pending for deletion because its node went away but its
763 * bus info block and root directory header matches that of a newly discovered
764 * device, revive the existing fw_device.
765 * The newly allocated fw_device becomes obsolete instead.
767 static int lookup_existing_device(struct device *dev, void *data)
769 struct fw_device *old = fw_device(dev);
770 struct fw_device *new = data;
771 struct fw_card *card = new->card;
774 down_read(&fw_device_rwsem); /* serialize config_rom access */
775 spin_lock_irq(&card->lock); /* serialize node access */
777 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
778 atomic_cmpxchg(&old->state,
780 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
781 struct fw_node *current_node = new->node;
782 struct fw_node *obsolete_node = old->node;
784 new->node = obsolete_node;
785 new->node->data = new;
786 old->node = current_node;
787 old->node->data = old;
789 old->max_speed = new->max_speed;
790 old->node_id = current_node->node_id;
791 smp_wmb(); /* update node_id before generation */
792 old->generation = card->generation;
793 old->config_rom_retries = 0;
794 fw_notify("rediscovered device %s\n", dev_name(dev));
796 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
797 schedule_delayed_work(&old->work, 0);
799 if (current_node == card->root_node)
800 fw_schedule_bm_work(card, 0);
805 spin_unlock_irq(&card->lock);
806 up_read(&fw_device_rwsem);
811 enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
813 void fw_device_set_broadcast_channel(struct fw_device *device, int generation)
815 struct fw_card *card = device->card;
819 if (!card->broadcast_channel_allocated)
822 if (device->bc_implemented == BC_UNKNOWN) {
823 rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
824 device->node_id, generation, device->max_speed,
825 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
829 if (data & cpu_to_be32(1 << 31)) {
830 device->bc_implemented = BC_IMPLEMENTED;
833 /* else fall through to case address error */
834 case RCODE_ADDRESS_ERROR:
835 device->bc_implemented = BC_UNIMPLEMENTED;
839 if (device->bc_implemented == BC_IMPLEMENTED) {
840 data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
841 BROADCAST_CHANNEL_VALID);
842 fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
843 device->node_id, generation, device->max_speed,
844 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
849 static void fw_device_init(struct work_struct *work)
851 struct fw_device *device =
852 container_of(work, struct fw_device, work.work);
853 struct device *revived_dev;
857 * All failure paths here set node->data to NULL, so that we
858 * don't try to do device_for_each_child() on a kfree()'d
862 if (read_bus_info_block(device, device->generation) < 0) {
863 if (device->config_rom_retries < MAX_RETRIES &&
864 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
865 device->config_rom_retries++;
866 schedule_delayed_work(&device->work, RETRY_DELAY);
868 fw_notify("giving up on config rom for node id %x\n",
870 if (device->node == device->card->root_node)
871 fw_schedule_bm_work(device->card, 0);
872 fw_device_release(&device->device);
877 revived_dev = device_find_child(device->card->device,
878 device, lookup_existing_device);
880 put_device(revived_dev);
881 fw_device_release(&device->device);
886 device_initialize(&device->device);
888 fw_device_get(device);
889 down_write(&fw_device_rwsem);
890 ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
891 idr_get_new(&fw_device_idr, device, &minor) :
893 up_write(&fw_device_rwsem);
898 device->device.bus = &fw_bus_type;
899 device->device.type = &fw_device_type;
900 device->device.parent = device->card->device;
901 device->device.devt = MKDEV(fw_cdev_major, minor);
902 dev_set_name(&device->device, "fw%d", minor);
904 BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
905 ARRAY_SIZE(fw_device_attributes) +
906 ARRAY_SIZE(config_rom_attributes));
907 init_fw_attribute_group(&device->device,
908 fw_device_attributes,
909 &device->attribute_group);
911 if (device_add(&device->device)) {
912 fw_error("Failed to add device.\n");
913 goto error_with_cdev;
916 create_units(device);
919 * Transition the device to running state. If it got pulled
920 * out from under us while we did the intialization work, we
921 * have to shut down the device again here. Normally, though,
922 * fw_node_event will be responsible for shutting it down when
923 * necessary. We have to use the atomic cmpxchg here to avoid
924 * racing with the FW_NODE_DESTROYED case in
927 if (atomic_cmpxchg(&device->state,
928 FW_DEVICE_INITIALIZING,
929 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
930 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
931 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
933 if (device->config_rom_retries)
934 fw_notify("created device %s: GUID %08x%08x, S%d00, "
935 "%d config ROM retries\n",
936 dev_name(&device->device),
937 device->config_rom[3], device->config_rom[4],
938 1 << device->max_speed,
939 device->config_rom_retries);
941 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
942 dev_name(&device->device),
943 device->config_rom[3], device->config_rom[4],
944 1 << device->max_speed);
945 device->config_rom_retries = 0;
947 fw_device_set_broadcast_channel(device, device->generation);
951 * Reschedule the IRM work if we just finished reading the
952 * root node config rom. If this races with a bus reset we
953 * just end up running the IRM work a couple of extra times -
956 if (device->node == device->card->root_node)
957 fw_schedule_bm_work(device->card, 0);
962 down_write(&fw_device_rwsem);
963 idr_remove(&fw_device_idr, minor);
964 up_write(&fw_device_rwsem);
966 fw_device_put(device); /* fw_device_idr's reference */
968 put_device(&device->device); /* our reference */
974 REREAD_BIB_UNCHANGED,
978 /* Reread and compare bus info block and header of root directory */
979 static int reread_bus_info_block(struct fw_device *device, int generation)
984 for (i = 0; i < 6; i++) {
985 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
986 return REREAD_BIB_ERROR;
988 if (i == 0 && q == 0)
989 return REREAD_BIB_GONE;
991 if (q != device->config_rom[i])
992 return REREAD_BIB_CHANGED;
995 return REREAD_BIB_UNCHANGED;
998 static void fw_device_refresh(struct work_struct *work)
1000 struct fw_device *device =
1001 container_of(work, struct fw_device, work.work);
1002 struct fw_card *card = device->card;
1003 int node_id = device->node_id;
1005 switch (reread_bus_info_block(device, device->generation)) {
1006 case REREAD_BIB_ERROR:
1007 if (device->config_rom_retries < MAX_RETRIES / 2 &&
1008 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1009 device->config_rom_retries++;
1010 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
1016 case REREAD_BIB_GONE:
1019 case REREAD_BIB_UNCHANGED:
1020 if (atomic_cmpxchg(&device->state,
1021 FW_DEVICE_INITIALIZING,
1022 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1025 fw_device_update(work);
1026 device->config_rom_retries = 0;
1029 case REREAD_BIB_CHANGED:
1034 * Something changed. We keep things simple and don't investigate
1035 * further. We just destroy all previous units and create new ones.
1037 device_for_each_child(&device->device, NULL, shutdown_unit);
1039 if (read_bus_info_block(device, device->generation) < 0) {
1040 if (device->config_rom_retries < MAX_RETRIES &&
1041 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1042 device->config_rom_retries++;
1043 schedule_delayed_work(&device->work, RETRY_DELAY);
1050 create_units(device);
1052 /* Userspace may want to re-read attributes. */
1053 kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1055 if (atomic_cmpxchg(&device->state,
1056 FW_DEVICE_INITIALIZING,
1057 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1060 fw_notify("refreshed device %s\n", dev_name(&device->device));
1061 device->config_rom_retries = 0;
1065 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
1067 atomic_set(&device->state, FW_DEVICE_GONE);
1068 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1069 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
1071 if (node_id == card->root_node->node_id)
1072 fw_schedule_bm_work(card, 0);
1075 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1077 struct fw_device *device;
1080 case FW_NODE_CREATED:
1081 case FW_NODE_LINK_ON:
1085 device = kzalloc(sizeof(*device), GFP_ATOMIC);
1090 * Do minimal intialization of the device here, the
1091 * rest will happen in fw_device_init().
1093 * Attention: A lot of things, even fw_device_get(),
1094 * cannot be done before fw_device_init() finished!
1095 * You can basically just check device->state and
1096 * schedule work until then, but only while holding
1099 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
1100 device->card = fw_card_get(card);
1101 device->node = fw_node_get(node);
1102 device->node_id = node->node_id;
1103 device->generation = card->generation;
1104 device->is_local = node == card->local_node;
1105 mutex_init(&device->client_list_mutex);
1106 INIT_LIST_HEAD(&device->client_list);
1109 * Set the node data to point back to this device so
1110 * FW_NODE_UPDATED callbacks can update the node_id
1111 * and generation for the device.
1113 node->data = device;
1116 * Many devices are slow to respond after bus resets,
1117 * especially if they are bus powered and go through
1118 * power-up after getting plugged in. We schedule the
1119 * first config rom scan half a second after bus reset.
1121 INIT_DELAYED_WORK(&device->work, fw_device_init);
1122 schedule_delayed_work(&device->work, INITIAL_DELAY);
1125 case FW_NODE_INITIATED_RESET:
1126 device = node->data;
1130 device->node_id = node->node_id;
1131 smp_wmb(); /* update node_id before generation */
1132 device->generation = card->generation;
1133 if (atomic_cmpxchg(&device->state,
1135 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1136 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1137 schedule_delayed_work(&device->work,
1138 device->is_local ? 0 : INITIAL_DELAY);
1142 case FW_NODE_UPDATED:
1143 if (!node->link_on || node->data == NULL)
1146 device = node->data;
1147 device->node_id = node->node_id;
1148 smp_wmb(); /* update node_id before generation */
1149 device->generation = card->generation;
1150 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1151 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1152 schedule_delayed_work(&device->work, 0);
1156 case FW_NODE_DESTROYED:
1157 case FW_NODE_LINK_OFF:
1162 * Destroy the device associated with the node. There
1163 * are two cases here: either the device is fully
1164 * initialized (FW_DEVICE_RUNNING) or we're in the
1165 * process of reading its config rom
1166 * (FW_DEVICE_INITIALIZING). If it is fully
1167 * initialized we can reuse device->work to schedule a
1168 * full fw_device_shutdown(). If not, there's work
1169 * scheduled to read it's config rom, and we just put
1170 * the device in shutdown state to have that code fail
1171 * to create the device.
1173 device = node->data;
1174 if (atomic_xchg(&device->state,
1175 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
1176 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1177 schedule_delayed_work(&device->work,
1178 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);