firewire: convert client_list_lock to mutex
[linux-2.6/kvm.git] / drivers / firewire / fw-device.c
blob2de3dd5ebc4b5af51bd84436f3583cf6f920e9b1
1 /*
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/module.h>
22 #include <linux/wait.h>
23 #include <linux/errno.h>
24 #include <linux/kthread.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/idr.h>
28 #include <linux/jiffies.h>
29 #include <linux/string.h>
30 #include <linux/mutex.h>
31 #include <linux/rwsem.h>
32 #include <linux/semaphore.h>
33 #include <linux/spinlock.h>
34 #include <asm/system.h>
35 #include <linux/ctype.h>
36 #include "fw-transaction.h"
37 #include "fw-topology.h"
38 #include "fw-device.h"
40 void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
42 ci->p = p + 1;
43 ci->end = ci->p + (p[0] >> 16);
45 EXPORT_SYMBOL(fw_csr_iterator_init);
47 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
49 *key = *ci->p >> 24;
50 *value = *ci->p & 0xffffff;
52 return ci->p++ < ci->end;
54 EXPORT_SYMBOL(fw_csr_iterator_next);
56 static int is_fw_unit(struct device *dev);
58 static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
60 struct fw_csr_iterator ci;
61 int key, value, match;
63 match = 0;
64 fw_csr_iterator_init(&ci, directory);
65 while (fw_csr_iterator_next(&ci, &key, &value)) {
66 if (key == CSR_VENDOR && value == id->vendor)
67 match |= FW_MATCH_VENDOR;
68 if (key == CSR_MODEL && value == id->model)
69 match |= FW_MATCH_MODEL;
70 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
71 match |= FW_MATCH_SPECIFIER_ID;
72 if (key == CSR_VERSION && value == id->version)
73 match |= FW_MATCH_VERSION;
76 return (match & id->match_flags) == id->match_flags;
79 static int fw_unit_match(struct device *dev, struct device_driver *drv)
81 struct fw_unit *unit = fw_unit(dev);
82 struct fw_driver *driver = fw_driver(drv);
83 int i;
85 /* We only allow binding to fw_units. */
86 if (!is_fw_unit(dev))
87 return 0;
89 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
90 if (match_unit_directory(unit->directory, &driver->id_table[i]))
91 return 1;
94 return 0;
97 static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
99 struct fw_device *device = fw_device(unit->device.parent);
100 struct fw_csr_iterator ci;
102 int key, value;
103 int vendor = 0;
104 int model = 0;
105 int specifier_id = 0;
106 int version = 0;
108 fw_csr_iterator_init(&ci, &device->config_rom[5]);
109 while (fw_csr_iterator_next(&ci, &key, &value)) {
110 switch (key) {
111 case CSR_VENDOR:
112 vendor = value;
113 break;
114 case CSR_MODEL:
115 model = value;
116 break;
120 fw_csr_iterator_init(&ci, unit->directory);
121 while (fw_csr_iterator_next(&ci, &key, &value)) {
122 switch (key) {
123 case CSR_SPECIFIER_ID:
124 specifier_id = value;
125 break;
126 case CSR_VERSION:
127 version = value;
128 break;
132 return snprintf(buffer, buffer_size,
133 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
134 vendor, model, specifier_id, version);
137 static int
138 fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
140 struct fw_unit *unit = fw_unit(dev);
141 char modalias[64];
143 get_modalias(unit, modalias, sizeof(modalias));
145 if (add_uevent_var(env, "MODALIAS=%s", modalias))
146 return -ENOMEM;
148 return 0;
151 struct bus_type fw_bus_type = {
152 .name = "firewire",
153 .match = fw_unit_match,
155 EXPORT_SYMBOL(fw_bus_type);
157 static void fw_device_release(struct device *dev)
159 struct fw_device *device = fw_device(dev);
160 struct fw_card *card = device->card;
161 unsigned long flags;
164 * Take the card lock so we don't set this to NULL while a
165 * FW_NODE_UPDATED callback is being handled or while the
166 * bus manager work looks at this node.
168 spin_lock_irqsave(&card->lock, flags);
169 device->node->data = NULL;
170 spin_unlock_irqrestore(&card->lock, flags);
172 fw_node_put(device->node);
173 kfree(device->config_rom);
174 kfree(device);
175 fw_card_put(card);
178 int fw_device_enable_phys_dma(struct fw_device *device)
180 int generation = device->generation;
182 /* device->node_id, accessed below, must not be older than generation */
183 smp_rmb();
185 return device->card->driver->enable_phys_dma(device->card,
186 device->node_id,
187 generation);
189 EXPORT_SYMBOL(fw_device_enable_phys_dma);
191 struct config_rom_attribute {
192 struct device_attribute attr;
193 u32 key;
196 static ssize_t
197 show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
199 struct config_rom_attribute *attr =
200 container_of(dattr, struct config_rom_attribute, attr);
201 struct fw_csr_iterator ci;
202 u32 *dir;
203 int key, value, ret = -ENOENT;
205 down_read(&fw_device_rwsem);
207 if (is_fw_unit(dev))
208 dir = fw_unit(dev)->directory;
209 else
210 dir = fw_device(dev)->config_rom + 5;
212 fw_csr_iterator_init(&ci, dir);
213 while (fw_csr_iterator_next(&ci, &key, &value))
214 if (attr->key == key) {
215 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
216 "0x%06x\n", value);
217 break;
220 up_read(&fw_device_rwsem);
222 return ret;
225 #define IMMEDIATE_ATTR(name, key) \
226 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
228 static ssize_t
229 show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
231 struct config_rom_attribute *attr =
232 container_of(dattr, struct config_rom_attribute, attr);
233 struct fw_csr_iterator ci;
234 u32 *dir, *block = NULL, *p, *end;
235 int length, key, value, last_key = 0, ret = -ENOENT;
236 char *b;
238 down_read(&fw_device_rwsem);
240 if (is_fw_unit(dev))
241 dir = fw_unit(dev)->directory;
242 else
243 dir = fw_device(dev)->config_rom + 5;
245 fw_csr_iterator_init(&ci, dir);
246 while (fw_csr_iterator_next(&ci, &key, &value)) {
247 if (attr->key == last_key &&
248 key == (CSR_DESCRIPTOR | CSR_LEAF))
249 block = ci.p - 1 + value;
250 last_key = key;
253 if (block == NULL)
254 goto out;
256 length = min(block[0] >> 16, 256U);
257 if (length < 3)
258 goto out;
260 if (block[1] != 0 || block[2] != 0)
261 /* Unknown encoding. */
262 goto out;
264 if (buf == NULL) {
265 ret = length * 4;
266 goto out;
269 b = buf;
270 end = &block[length + 1];
271 for (p = &block[3]; p < end; p++, b += 4)
272 * (u32 *) b = (__force u32) __cpu_to_be32(*p);
274 /* Strip trailing whitespace and add newline. */
275 while (b--, (isspace(*b) || *b == '\0') && b > buf);
276 strcpy(b + 1, "\n");
277 ret = b + 2 - buf;
278 out:
279 up_read(&fw_device_rwsem);
281 return ret;
284 #define TEXT_LEAF_ATTR(name, key) \
285 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
287 static struct config_rom_attribute config_rom_attributes[] = {
288 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
289 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
290 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
291 IMMEDIATE_ATTR(version, CSR_VERSION),
292 IMMEDIATE_ATTR(model, CSR_MODEL),
293 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
294 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
295 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
298 static void
299 init_fw_attribute_group(struct device *dev,
300 struct device_attribute *attrs,
301 struct fw_attribute_group *group)
303 struct device_attribute *attr;
304 int i, j;
306 for (j = 0; attrs[j].attr.name != NULL; j++)
307 group->attrs[j] = &attrs[j].attr;
309 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
310 attr = &config_rom_attributes[i].attr;
311 if (attr->show(dev, attr, NULL) < 0)
312 continue;
313 group->attrs[j++] = &attr->attr;
316 BUG_ON(j >= ARRAY_SIZE(group->attrs));
317 group->attrs[j++] = NULL;
318 group->groups[0] = &group->group;
319 group->groups[1] = NULL;
320 group->group.attrs = group->attrs;
321 dev->groups = group->groups;
324 static ssize_t
325 modalias_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 struct fw_unit *unit = fw_unit(dev);
329 int length;
331 length = get_modalias(unit, buf, PAGE_SIZE);
332 strcpy(buf + length, "\n");
334 return length + 1;
337 static ssize_t
338 rom_index_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
341 struct fw_device *device = fw_device(dev->parent);
342 struct fw_unit *unit = fw_unit(dev);
344 return snprintf(buf, PAGE_SIZE, "%d\n",
345 (int)(unit->directory - device->config_rom));
348 static struct device_attribute fw_unit_attributes[] = {
349 __ATTR_RO(modalias),
350 __ATTR_RO(rom_index),
351 __ATTR_NULL,
354 static ssize_t
355 config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
357 struct fw_device *device = fw_device(dev);
358 size_t length;
360 down_read(&fw_device_rwsem);
361 length = device->config_rom_length * 4;
362 memcpy(buf, device->config_rom, length);
363 up_read(&fw_device_rwsem);
365 return length;
368 static ssize_t
369 guid_show(struct device *dev, struct device_attribute *attr, char *buf)
371 struct fw_device *device = fw_device(dev);
372 int ret;
374 down_read(&fw_device_rwsem);
375 ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
376 device->config_rom[3], device->config_rom[4]);
377 up_read(&fw_device_rwsem);
379 return ret;
382 static struct device_attribute fw_device_attributes[] = {
383 __ATTR_RO(config_rom),
384 __ATTR_RO(guid),
385 __ATTR_NULL,
388 static int
389 read_rom(struct fw_device *device, int generation, int index, u32 *data)
391 int rcode;
393 /* device->node_id, accessed below, must not be older than generation */
394 smp_rmb();
396 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
397 device->node_id, generation, device->max_speed,
398 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
399 data, 4);
400 be32_to_cpus(data);
402 return rcode;
405 #define READ_BIB_ROM_SIZE 256
406 #define READ_BIB_STACK_SIZE 16
409 * Read the bus info block, perform a speed probe, and read all of the rest of
410 * the config ROM. We do all this with a cached bus generation. If the bus
411 * generation changes under us, read_bus_info_block will fail and get retried.
412 * It's better to start all over in this case because the node from which we
413 * are reading the ROM may have changed the ROM during the reset.
415 static int read_bus_info_block(struct fw_device *device, int generation)
417 u32 *rom, *stack, *old_rom, *new_rom;
418 u32 sp, key;
419 int i, end, length, ret = -1;
421 rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
422 sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
423 if (rom == NULL)
424 return -ENOMEM;
426 stack = &rom[READ_BIB_ROM_SIZE];
428 device->max_speed = SCODE_100;
430 /* First read the bus info block. */
431 for (i = 0; i < 5; i++) {
432 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
433 goto out;
435 * As per IEEE1212 7.2, during power-up, devices can
436 * reply with a 0 for the first quadlet of the config
437 * rom to indicate that they are booting (for example,
438 * if the firmware is on the disk of a external
439 * harddisk). In that case we just fail, and the
440 * retry mechanism will try again later.
442 if (i == 0 && rom[i] == 0)
443 goto out;
446 device->max_speed = device->node->max_speed;
449 * Determine the speed of
450 * - devices with link speed less than PHY speed,
451 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
452 * - all devices if there are 1394b repeaters.
453 * Note, we cannot use the bus info block's link_spd as starting point
454 * because some buggy firmwares set it lower than necessary and because
455 * 1394-1995 nodes do not have the field.
457 if ((rom[2] & 0x7) < device->max_speed ||
458 device->max_speed == SCODE_BETA ||
459 device->card->beta_repeaters_present) {
460 u32 dummy;
462 /* for S1600 and S3200 */
463 if (device->max_speed == SCODE_BETA)
464 device->max_speed = device->card->link_speed;
466 while (device->max_speed > SCODE_100) {
467 if (read_rom(device, generation, 0, &dummy) ==
468 RCODE_COMPLETE)
469 break;
470 device->max_speed--;
475 * Now parse the config rom. The config rom is a recursive
476 * directory structure so we parse it using a stack of
477 * references to the blocks that make up the structure. We
478 * push a reference to the root directory on the stack to
479 * start things off.
481 length = i;
482 sp = 0;
483 stack[sp++] = 0xc0000005;
484 while (sp > 0) {
486 * Pop the next block reference of the stack. The
487 * lower 24 bits is the offset into the config rom,
488 * the upper 8 bits are the type of the reference the
489 * block.
491 key = stack[--sp];
492 i = key & 0xffffff;
493 if (i >= READ_BIB_ROM_SIZE)
495 * The reference points outside the standard
496 * config rom area, something's fishy.
498 goto out;
500 /* Read header quadlet for the block to get the length. */
501 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
502 goto out;
503 end = i + (rom[i] >> 16) + 1;
504 i++;
505 if (end > READ_BIB_ROM_SIZE)
507 * This block extends outside standard config
508 * area (and the array we're reading it
509 * into). That's broken, so ignore this
510 * device.
512 goto out;
515 * Now read in the block. If this is a directory
516 * block, check the entries as we read them to see if
517 * it references another block, and push it in that case.
519 while (i < end) {
520 if (read_rom(device, generation, i, &rom[i]) !=
521 RCODE_COMPLETE)
522 goto out;
523 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
524 sp < READ_BIB_STACK_SIZE)
525 stack[sp++] = i + rom[i];
526 i++;
528 if (length < i)
529 length = i;
532 old_rom = device->config_rom;
533 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
534 if (new_rom == NULL)
535 goto out;
537 down_write(&fw_device_rwsem);
538 device->config_rom = new_rom;
539 device->config_rom_length = length;
540 up_write(&fw_device_rwsem);
542 kfree(old_rom);
543 ret = 0;
544 device->cmc = rom[2] & 1 << 30;
545 out:
546 kfree(rom);
548 return ret;
551 static void fw_unit_release(struct device *dev)
553 struct fw_unit *unit = fw_unit(dev);
555 kfree(unit);
558 static struct device_type fw_unit_type = {
559 .uevent = fw_unit_uevent,
560 .release = fw_unit_release,
563 static int is_fw_unit(struct device *dev)
565 return dev->type == &fw_unit_type;
568 static void create_units(struct fw_device *device)
570 struct fw_csr_iterator ci;
571 struct fw_unit *unit;
572 int key, value, i;
574 i = 0;
575 fw_csr_iterator_init(&ci, &device->config_rom[5]);
576 while (fw_csr_iterator_next(&ci, &key, &value)) {
577 if (key != (CSR_UNIT | CSR_DIRECTORY))
578 continue;
581 * Get the address of the unit directory and try to
582 * match the drivers id_tables against it.
584 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
585 if (unit == NULL) {
586 fw_error("failed to allocate memory for unit\n");
587 continue;
590 unit->directory = ci.p + value - 1;
591 unit->device.bus = &fw_bus_type;
592 unit->device.type = &fw_unit_type;
593 unit->device.parent = &device->device;
594 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
596 init_fw_attribute_group(&unit->device,
597 fw_unit_attributes,
598 &unit->attribute_group);
599 if (device_register(&unit->device) < 0)
600 goto skip_unit;
602 continue;
604 skip_unit:
605 kfree(unit);
609 static int shutdown_unit(struct device *device, void *data)
611 device_unregister(device);
613 return 0;
617 * fw_device_rwsem acts as dual purpose mutex:
618 * - serializes accesses to fw_device_idr,
619 * - serializes accesses to fw_device.config_rom/.config_rom_length and
620 * fw_unit.directory, unless those accesses happen at safe occasions
622 DECLARE_RWSEM(fw_device_rwsem);
624 DEFINE_IDR(fw_device_idr);
625 int fw_cdev_major;
627 struct fw_device *fw_device_get_by_devt(dev_t devt)
629 struct fw_device *device;
631 down_read(&fw_device_rwsem);
632 device = idr_find(&fw_device_idr, MINOR(devt));
633 if (device)
634 fw_device_get(device);
635 up_read(&fw_device_rwsem);
637 return device;
641 * These defines control the retry behavior for reading the config
642 * rom. It shouldn't be necessary to tweak these; if the device
643 * doesn't respond to a config rom read within 10 seconds, it's not
644 * going to respond at all. As for the initial delay, a lot of
645 * devices will be able to respond within half a second after bus
646 * reset. On the other hand, it's not really worth being more
647 * aggressive than that, since it scales pretty well; if 10 devices
648 * are plugged in, they're all getting read within one second.
651 #define MAX_RETRIES 10
652 #define RETRY_DELAY (3 * HZ)
653 #define INITIAL_DELAY (HZ / 2)
654 #define SHUTDOWN_DELAY (2 * HZ)
656 static void fw_device_shutdown(struct work_struct *work)
658 struct fw_device *device =
659 container_of(work, struct fw_device, work.work);
660 int minor = MINOR(device->device.devt);
662 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
663 && !list_empty(&device->card->link)) {
664 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
665 return;
668 if (atomic_cmpxchg(&device->state,
669 FW_DEVICE_GONE,
670 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
671 return;
673 fw_device_cdev_remove(device);
674 device_for_each_child(&device->device, NULL, shutdown_unit);
675 device_unregister(&device->device);
677 down_write(&fw_device_rwsem);
678 idr_remove(&fw_device_idr, minor);
679 up_write(&fw_device_rwsem);
681 fw_device_put(device);
684 static struct device_type fw_device_type = {
685 .release = fw_device_release,
688 static void fw_device_update(struct work_struct *work);
691 * If a device was pending for deletion because its node went away but its
692 * bus info block and root directory header matches that of a newly discovered
693 * device, revive the existing fw_device.
694 * The newly allocated fw_device becomes obsolete instead.
696 static int lookup_existing_device(struct device *dev, void *data)
698 struct fw_device *old = fw_device(dev);
699 struct fw_device *new = data;
700 struct fw_card *card = new->card;
701 int match = 0;
703 down_read(&fw_device_rwsem); /* serialize config_rom access */
704 spin_lock_irq(&card->lock); /* serialize node access */
706 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
707 atomic_cmpxchg(&old->state,
708 FW_DEVICE_GONE,
709 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
710 struct fw_node *current_node = new->node;
711 struct fw_node *obsolete_node = old->node;
713 new->node = obsolete_node;
714 new->node->data = new;
715 old->node = current_node;
716 old->node->data = old;
718 old->max_speed = new->max_speed;
719 old->node_id = current_node->node_id;
720 smp_wmb(); /* update node_id before generation */
721 old->generation = card->generation;
722 old->config_rom_retries = 0;
723 fw_notify("rediscovered device %s\n", dev_name(dev));
725 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
726 schedule_delayed_work(&old->work, 0);
728 if (current_node == card->root_node)
729 fw_schedule_bm_work(card, 0);
731 match = 1;
734 spin_unlock_irq(&card->lock);
735 up_read(&fw_device_rwsem);
737 return match;
740 static void fw_device_init(struct work_struct *work)
742 struct fw_device *device =
743 container_of(work, struct fw_device, work.work);
744 struct device *revived_dev;
745 int minor, err;
748 * All failure paths here set node->data to NULL, so that we
749 * don't try to do device_for_each_child() on a kfree()'d
750 * device.
753 if (read_bus_info_block(device, device->generation) < 0) {
754 if (device->config_rom_retries < MAX_RETRIES &&
755 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
756 device->config_rom_retries++;
757 schedule_delayed_work(&device->work, RETRY_DELAY);
758 } else {
759 fw_notify("giving up on config rom for node id %x\n",
760 device->node_id);
761 if (device->node == device->card->root_node)
762 fw_schedule_bm_work(device->card, 0);
763 fw_device_release(&device->device);
765 return;
768 revived_dev = device_find_child(device->card->device,
769 device, lookup_existing_device);
770 if (revived_dev) {
771 put_device(revived_dev);
772 fw_device_release(&device->device);
774 return;
777 device_initialize(&device->device);
779 fw_device_get(device);
780 down_write(&fw_device_rwsem);
781 err = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
782 idr_get_new(&fw_device_idr, device, &minor) :
783 -ENOMEM;
784 up_write(&fw_device_rwsem);
786 if (err < 0)
787 goto error;
789 device->device.bus = &fw_bus_type;
790 device->device.type = &fw_device_type;
791 device->device.parent = device->card->device;
792 device->device.devt = MKDEV(fw_cdev_major, minor);
793 dev_set_name(&device->device, "fw%d", minor);
795 init_fw_attribute_group(&device->device,
796 fw_device_attributes,
797 &device->attribute_group);
798 if (device_add(&device->device)) {
799 fw_error("Failed to add device.\n");
800 goto error_with_cdev;
803 create_units(device);
806 * Transition the device to running state. If it got pulled
807 * out from under us while we did the intialization work, we
808 * have to shut down the device again here. Normally, though,
809 * fw_node_event will be responsible for shutting it down when
810 * necessary. We have to use the atomic cmpxchg here to avoid
811 * racing with the FW_NODE_DESTROYED case in
812 * fw_node_event().
814 if (atomic_cmpxchg(&device->state,
815 FW_DEVICE_INITIALIZING,
816 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
817 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
818 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
819 } else {
820 if (device->config_rom_retries)
821 fw_notify("created device %s: GUID %08x%08x, S%d00, "
822 "%d config ROM retries\n",
823 dev_name(&device->device),
824 device->config_rom[3], device->config_rom[4],
825 1 << device->max_speed,
826 device->config_rom_retries);
827 else
828 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
829 dev_name(&device->device),
830 device->config_rom[3], device->config_rom[4],
831 1 << device->max_speed);
832 device->config_rom_retries = 0;
836 * Reschedule the IRM work if we just finished reading the
837 * root node config rom. If this races with a bus reset we
838 * just end up running the IRM work a couple of extra times -
839 * pretty harmless.
841 if (device->node == device->card->root_node)
842 fw_schedule_bm_work(device->card, 0);
844 return;
846 error_with_cdev:
847 down_write(&fw_device_rwsem);
848 idr_remove(&fw_device_idr, minor);
849 up_write(&fw_device_rwsem);
850 error:
851 fw_device_put(device); /* fw_device_idr's reference */
853 put_device(&device->device); /* our reference */
856 static int update_unit(struct device *dev, void *data)
858 struct fw_unit *unit = fw_unit(dev);
859 struct fw_driver *driver = (struct fw_driver *)dev->driver;
861 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
862 down(&dev->sem);
863 driver->update(unit);
864 up(&dev->sem);
867 return 0;
870 static void fw_device_update(struct work_struct *work)
872 struct fw_device *device =
873 container_of(work, struct fw_device, work.work);
875 fw_device_cdev_update(device);
876 device_for_each_child(&device->device, NULL, update_unit);
879 enum {
880 REREAD_BIB_ERROR,
881 REREAD_BIB_GONE,
882 REREAD_BIB_UNCHANGED,
883 REREAD_BIB_CHANGED,
886 /* Reread and compare bus info block and header of root directory */
887 static int reread_bus_info_block(struct fw_device *device, int generation)
889 u32 q;
890 int i;
892 for (i = 0; i < 6; i++) {
893 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
894 return REREAD_BIB_ERROR;
896 if (i == 0 && q == 0)
897 return REREAD_BIB_GONE;
899 if (i > device->config_rom_length || q != device->config_rom[i])
900 return REREAD_BIB_CHANGED;
903 return REREAD_BIB_UNCHANGED;
906 static void fw_device_refresh(struct work_struct *work)
908 struct fw_device *device =
909 container_of(work, struct fw_device, work.work);
910 struct fw_card *card = device->card;
911 int node_id = device->node_id;
913 switch (reread_bus_info_block(device, device->generation)) {
914 case REREAD_BIB_ERROR:
915 if (device->config_rom_retries < MAX_RETRIES / 2 &&
916 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
917 device->config_rom_retries++;
918 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
920 return;
922 goto give_up;
924 case REREAD_BIB_GONE:
925 goto gone;
927 case REREAD_BIB_UNCHANGED:
928 if (atomic_cmpxchg(&device->state,
929 FW_DEVICE_INITIALIZING,
930 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
931 goto gone;
933 fw_device_update(work);
934 device->config_rom_retries = 0;
935 goto out;
937 case REREAD_BIB_CHANGED:
938 break;
942 * Something changed. We keep things simple and don't investigate
943 * further. We just destroy all previous units and create new ones.
945 device_for_each_child(&device->device, NULL, shutdown_unit);
947 if (read_bus_info_block(device, device->generation) < 0) {
948 if (device->config_rom_retries < MAX_RETRIES &&
949 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
950 device->config_rom_retries++;
951 schedule_delayed_work(&device->work, RETRY_DELAY);
953 return;
955 goto give_up;
958 create_units(device);
960 if (atomic_cmpxchg(&device->state,
961 FW_DEVICE_INITIALIZING,
962 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
963 goto gone;
965 fw_notify("refreshed device %s\n", dev_name(&device->device));
966 device->config_rom_retries = 0;
967 goto out;
969 give_up:
970 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
971 gone:
972 atomic_set(&device->state, FW_DEVICE_GONE);
973 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
974 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
975 out:
976 if (node_id == card->root_node->node_id)
977 fw_schedule_bm_work(card, 0);
980 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
982 struct fw_device *device;
984 switch (event) {
985 case FW_NODE_CREATED:
986 case FW_NODE_LINK_ON:
987 if (!node->link_on)
988 break;
989 create:
990 device = kzalloc(sizeof(*device), GFP_ATOMIC);
991 if (device == NULL)
992 break;
995 * Do minimal intialization of the device here, the
996 * rest will happen in fw_device_init().
998 * Attention: A lot of things, even fw_device_get(),
999 * cannot be done before fw_device_init() finished!
1000 * You can basically just check device->state and
1001 * schedule work until then, but only while holding
1002 * card->lock.
1004 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
1005 device->card = fw_card_get(card);
1006 device->node = fw_node_get(node);
1007 device->node_id = node->node_id;
1008 device->generation = card->generation;
1009 mutex_init(&device->client_list_mutex);
1010 INIT_LIST_HEAD(&device->client_list);
1013 * Set the node data to point back to this device so
1014 * FW_NODE_UPDATED callbacks can update the node_id
1015 * and generation for the device.
1017 node->data = device;
1020 * Many devices are slow to respond after bus resets,
1021 * especially if they are bus powered and go through
1022 * power-up after getting plugged in. We schedule the
1023 * first config rom scan half a second after bus reset.
1025 INIT_DELAYED_WORK(&device->work, fw_device_init);
1026 schedule_delayed_work(&device->work, INITIAL_DELAY);
1027 break;
1029 case FW_NODE_INITIATED_RESET:
1030 device = node->data;
1031 if (device == NULL)
1032 goto create;
1034 device->node_id = node->node_id;
1035 smp_wmb(); /* update node_id before generation */
1036 device->generation = card->generation;
1037 if (atomic_cmpxchg(&device->state,
1038 FW_DEVICE_RUNNING,
1039 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1040 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1041 schedule_delayed_work(&device->work,
1042 node == card->local_node ? 0 : INITIAL_DELAY);
1044 break;
1046 case FW_NODE_UPDATED:
1047 if (!node->link_on || node->data == NULL)
1048 break;
1050 device = node->data;
1051 device->node_id = node->node_id;
1052 smp_wmb(); /* update node_id before generation */
1053 device->generation = card->generation;
1054 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1055 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1056 schedule_delayed_work(&device->work, 0);
1058 break;
1060 case FW_NODE_DESTROYED:
1061 case FW_NODE_LINK_OFF:
1062 if (!node->data)
1063 break;
1066 * Destroy the device associated with the node. There
1067 * are two cases here: either the device is fully
1068 * initialized (FW_DEVICE_RUNNING) or we're in the
1069 * process of reading its config rom
1070 * (FW_DEVICE_INITIALIZING). If it is fully
1071 * initialized we can reuse device->work to schedule a
1072 * full fw_device_shutdown(). If not, there's work
1073 * scheduled to read it's config rom, and we just put
1074 * the device in shutdown state to have that code fail
1075 * to create the device.
1077 device = node->data;
1078 if (atomic_xchg(&device->state,
1079 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
1080 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1081 schedule_delayed_work(&device->work,
1082 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
1084 break;