cifs: fix another memleak, in cifs_root_iget
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / firewire / core-device.c
blob17e2b1740202a3921edde10eeeec757cdaedd5e5
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/ctype.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/firewire.h>
26 #include <linux/firewire-constants.h>
27 #include <linux/idr.h>
28 #include <linux/jiffies.h>
29 #include <linux/kobject.h>
30 #include <linux/list.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/module.h>
33 #include <linux/mutex.h>
34 #include <linux/rwsem.h>
35 #include <linux/semaphore.h>
36 #include <linux/spinlock.h>
37 #include <linux/string.h>
38 #include <linux/workqueue.h>
40 #include <asm/atomic.h>
41 #include <asm/byteorder.h>
42 #include <asm/system.h>
44 #include "core.h"
46 void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
48 ci->p = p + 1;
49 ci->end = ci->p + (p[0] >> 16);
51 EXPORT_SYMBOL(fw_csr_iterator_init);
53 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
55 *key = *ci->p >> 24;
56 *value = *ci->p & 0xffffff;
58 return ci->p++ < ci->end;
60 EXPORT_SYMBOL(fw_csr_iterator_next);
62 static bool is_fw_unit(struct device *dev);
64 static int match_unit_directory(u32 *directory, u32 match_flags,
65 const struct ieee1394_device_id *id)
67 struct fw_csr_iterator ci;
68 int key, value, match;
70 match = 0;
71 fw_csr_iterator_init(&ci, directory);
72 while (fw_csr_iterator_next(&ci, &key, &value)) {
73 if (key == CSR_VENDOR && value == id->vendor_id)
74 match |= IEEE1394_MATCH_VENDOR_ID;
75 if (key == CSR_MODEL && value == id->model_id)
76 match |= IEEE1394_MATCH_MODEL_ID;
77 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
78 match |= IEEE1394_MATCH_SPECIFIER_ID;
79 if (key == CSR_VERSION && value == id->version)
80 match |= IEEE1394_MATCH_VERSION;
83 return (match & match_flags) == match_flags;
86 static int fw_unit_match(struct device *dev, struct device_driver *drv)
88 struct fw_unit *unit = fw_unit(dev);
89 struct fw_device *device;
90 const struct ieee1394_device_id *id;
92 /* We only allow binding to fw_units. */
93 if (!is_fw_unit(dev))
94 return 0;
96 device = fw_parent_device(unit);
97 id = container_of(drv, struct fw_driver, driver)->id_table;
99 for (; id->match_flags != 0; id++) {
100 if (match_unit_directory(unit->directory, id->match_flags, id))
101 return 1;
103 /* Also check vendor ID in the root directory. */
104 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
105 match_unit_directory(&device->config_rom[5],
106 IEEE1394_MATCH_VENDOR_ID, id) &&
107 match_unit_directory(unit->directory, id->match_flags
108 & ~IEEE1394_MATCH_VENDOR_ID, id))
109 return 1;
112 return 0;
115 static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
117 struct fw_device *device = fw_parent_device(unit);
118 struct fw_csr_iterator ci;
120 int key, value;
121 int vendor = 0;
122 int model = 0;
123 int specifier_id = 0;
124 int version = 0;
126 fw_csr_iterator_init(&ci, &device->config_rom[5]);
127 while (fw_csr_iterator_next(&ci, &key, &value)) {
128 switch (key) {
129 case CSR_VENDOR:
130 vendor = value;
131 break;
132 case CSR_MODEL:
133 model = value;
134 break;
138 fw_csr_iterator_init(&ci, unit->directory);
139 while (fw_csr_iterator_next(&ci, &key, &value)) {
140 switch (key) {
141 case CSR_SPECIFIER_ID:
142 specifier_id = value;
143 break;
144 case CSR_VERSION:
145 version = value;
146 break;
150 return snprintf(buffer, buffer_size,
151 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
152 vendor, model, specifier_id, version);
155 static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
157 struct fw_unit *unit = fw_unit(dev);
158 char modalias[64];
160 get_modalias(unit, modalias, sizeof(modalias));
162 if (add_uevent_var(env, "MODALIAS=%s", modalias))
163 return -ENOMEM;
165 return 0;
168 struct bus_type fw_bus_type = {
169 .name = "firewire",
170 .match = fw_unit_match,
172 EXPORT_SYMBOL(fw_bus_type);
174 int fw_device_enable_phys_dma(struct fw_device *device)
176 int generation = device->generation;
178 /* device->node_id, accessed below, must not be older than generation */
179 smp_rmb();
181 return device->card->driver->enable_phys_dma(device->card,
182 device->node_id,
183 generation);
185 EXPORT_SYMBOL(fw_device_enable_phys_dma);
187 struct config_rom_attribute {
188 struct device_attribute attr;
189 u32 key;
192 static ssize_t show_immediate(struct device *dev,
193 struct device_attribute *dattr, char *buf)
195 struct config_rom_attribute *attr =
196 container_of(dattr, struct config_rom_attribute, attr);
197 struct fw_csr_iterator ci;
198 u32 *dir;
199 int key, value, ret = -ENOENT;
201 down_read(&fw_device_rwsem);
203 if (is_fw_unit(dev))
204 dir = fw_unit(dev)->directory;
205 else
206 dir = fw_device(dev)->config_rom + 5;
208 fw_csr_iterator_init(&ci, dir);
209 while (fw_csr_iterator_next(&ci, &key, &value))
210 if (attr->key == key) {
211 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
212 "0x%06x\n", value);
213 break;
216 up_read(&fw_device_rwsem);
218 return ret;
221 #define IMMEDIATE_ATTR(name, key) \
222 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
224 static ssize_t show_text_leaf(struct device *dev,
225 struct device_attribute *dattr, char *buf)
227 struct config_rom_attribute *attr =
228 container_of(dattr, struct config_rom_attribute, attr);
229 struct fw_csr_iterator ci;
230 u32 *dir, *block = NULL, *p, *end;
231 int length, key, value, last_key = 0, ret = -ENOENT;
232 char *b;
234 down_read(&fw_device_rwsem);
236 if (is_fw_unit(dev))
237 dir = fw_unit(dev)->directory;
238 else
239 dir = fw_device(dev)->config_rom + 5;
241 fw_csr_iterator_init(&ci, dir);
242 while (fw_csr_iterator_next(&ci, &key, &value)) {
243 if (attr->key == last_key &&
244 key == (CSR_DESCRIPTOR | CSR_LEAF))
245 block = ci.p - 1 + value;
246 last_key = key;
249 if (block == NULL)
250 goto out;
252 length = min(block[0] >> 16, 256U);
253 if (length < 3)
254 goto out;
256 if (block[1] != 0 || block[2] != 0)
257 /* Unknown encoding. */
258 goto out;
260 if (buf == NULL) {
261 ret = length * 4;
262 goto out;
265 b = buf;
266 end = &block[length + 1];
267 for (p = &block[3]; p < end; p++, b += 4)
268 * (u32 *) b = (__force u32) __cpu_to_be32(*p);
270 /* Strip trailing whitespace and add newline. */
271 while (b--, (isspace(*b) || *b == '\0') && b > buf);
272 strcpy(b + 1, "\n");
273 ret = b + 2 - buf;
274 out:
275 up_read(&fw_device_rwsem);
277 return ret;
280 #define TEXT_LEAF_ATTR(name, key) \
281 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
283 static struct config_rom_attribute config_rom_attributes[] = {
284 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
285 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
286 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
287 IMMEDIATE_ATTR(version, CSR_VERSION),
288 IMMEDIATE_ATTR(model, CSR_MODEL),
289 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
290 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
291 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
294 static void init_fw_attribute_group(struct device *dev,
295 struct device_attribute *attrs,
296 struct fw_attribute_group *group)
298 struct device_attribute *attr;
299 int i, j;
301 for (j = 0; attrs[j].attr.name != NULL; j++)
302 group->attrs[j] = &attrs[j].attr;
304 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
305 attr = &config_rom_attributes[i].attr;
306 if (attr->show(dev, attr, NULL) < 0)
307 continue;
308 group->attrs[j++] = &attr->attr;
311 group->attrs[j] = NULL;
312 group->groups[0] = &group->group;
313 group->groups[1] = NULL;
314 group->group.attrs = group->attrs;
315 dev->groups = (const struct attribute_group **) group->groups;
318 static ssize_t modalias_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct fw_unit *unit = fw_unit(dev);
322 int length;
324 length = get_modalias(unit, buf, PAGE_SIZE);
325 strcpy(buf + length, "\n");
327 return length + 1;
330 static ssize_t rom_index_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
333 struct fw_device *device = fw_device(dev->parent);
334 struct fw_unit *unit = fw_unit(dev);
336 return snprintf(buf, PAGE_SIZE, "%d\n",
337 (int)(unit->directory - device->config_rom));
340 static struct device_attribute fw_unit_attributes[] = {
341 __ATTR_RO(modalias),
342 __ATTR_RO(rom_index),
343 __ATTR_NULL,
346 static ssize_t config_rom_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 struct fw_device *device = fw_device(dev);
350 size_t length;
352 down_read(&fw_device_rwsem);
353 length = device->config_rom_length * 4;
354 memcpy(buf, device->config_rom, length);
355 up_read(&fw_device_rwsem);
357 return length;
360 static ssize_t guid_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
363 struct fw_device *device = fw_device(dev);
364 int ret;
366 down_read(&fw_device_rwsem);
367 ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
368 device->config_rom[3], device->config_rom[4]);
369 up_read(&fw_device_rwsem);
371 return ret;
374 static int units_sprintf(char *buf, u32 *directory)
376 struct fw_csr_iterator ci;
377 int key, value;
378 int specifier_id = 0;
379 int version = 0;
381 fw_csr_iterator_init(&ci, directory);
382 while (fw_csr_iterator_next(&ci, &key, &value)) {
383 switch (key) {
384 case CSR_SPECIFIER_ID:
385 specifier_id = value;
386 break;
387 case CSR_VERSION:
388 version = value;
389 break;
393 return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
396 static ssize_t units_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
399 struct fw_device *device = fw_device(dev);
400 struct fw_csr_iterator ci;
401 int key, value, i = 0;
403 down_read(&fw_device_rwsem);
404 fw_csr_iterator_init(&ci, &device->config_rom[5]);
405 while (fw_csr_iterator_next(&ci, &key, &value)) {
406 if (key != (CSR_UNIT | CSR_DIRECTORY))
407 continue;
408 i += units_sprintf(&buf[i], ci.p + value - 1);
409 if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
410 break;
412 up_read(&fw_device_rwsem);
414 if (i)
415 buf[i - 1] = '\n';
417 return i;
420 static struct device_attribute fw_device_attributes[] = {
421 __ATTR_RO(config_rom),
422 __ATTR_RO(guid),
423 __ATTR_RO(units),
424 __ATTR_NULL,
427 static int read_rom(struct fw_device *device,
428 int generation, int index, u32 *data)
430 int rcode;
432 /* device->node_id, accessed below, must not be older than generation */
433 smp_rmb();
435 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
436 device->node_id, generation, device->max_speed,
437 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
438 data, 4);
439 be32_to_cpus(data);
441 return rcode;
444 #define READ_BIB_ROM_SIZE 256
445 #define READ_BIB_STACK_SIZE 16
448 * Read the bus info block, perform a speed probe, and read all of the rest of
449 * the config ROM. We do all this with a cached bus generation. If the bus
450 * generation changes under us, read_bus_info_block will fail and get retried.
451 * It's better to start all over in this case because the node from which we
452 * are reading the ROM may have changed the ROM during the reset.
454 static int read_bus_info_block(struct fw_device *device, int generation)
456 u32 *rom, *stack, *old_rom, *new_rom;
457 u32 sp, key;
458 int i, end, length, ret = -1;
460 rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
461 sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
462 if (rom == NULL)
463 return -ENOMEM;
465 stack = &rom[READ_BIB_ROM_SIZE];
466 memset(rom, 0, sizeof(*rom) * READ_BIB_ROM_SIZE);
468 device->max_speed = SCODE_100;
470 /* First read the bus info block. */
471 for (i = 0; i < 5; i++) {
472 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
473 goto out;
475 * As per IEEE1212 7.2, during power-up, devices can
476 * reply with a 0 for the first quadlet of the config
477 * rom to indicate that they are booting (for example,
478 * if the firmware is on the disk of a external
479 * harddisk). In that case we just fail, and the
480 * retry mechanism will try again later.
482 if (i == 0 && rom[i] == 0)
483 goto out;
486 device->max_speed = device->node->max_speed;
489 * Determine the speed of
490 * - devices with link speed less than PHY speed,
491 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
492 * - all devices if there are 1394b repeaters.
493 * Note, we cannot use the bus info block's link_spd as starting point
494 * because some buggy firmwares set it lower than necessary and because
495 * 1394-1995 nodes do not have the field.
497 if ((rom[2] & 0x7) < device->max_speed ||
498 device->max_speed == SCODE_BETA ||
499 device->card->beta_repeaters_present) {
500 u32 dummy;
502 /* for S1600 and S3200 */
503 if (device->max_speed == SCODE_BETA)
504 device->max_speed = device->card->link_speed;
506 while (device->max_speed > SCODE_100) {
507 if (read_rom(device, generation, 0, &dummy) ==
508 RCODE_COMPLETE)
509 break;
510 device->max_speed--;
515 * Now parse the config rom. The config rom is a recursive
516 * directory structure so we parse it using a stack of
517 * references to the blocks that make up the structure. We
518 * push a reference to the root directory on the stack to
519 * start things off.
521 length = i;
522 sp = 0;
523 stack[sp++] = 0xc0000005;
524 while (sp > 0) {
526 * Pop the next block reference of the stack. The
527 * lower 24 bits is the offset into the config rom,
528 * the upper 8 bits are the type of the reference the
529 * block.
531 key = stack[--sp];
532 i = key & 0xffffff;
533 if (i >= READ_BIB_ROM_SIZE)
535 * The reference points outside the standard
536 * config rom area, something's fishy.
538 goto out;
540 /* Read header quadlet for the block to get the length. */
541 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
542 goto out;
543 end = i + (rom[i] >> 16) + 1;
544 i++;
545 if (end > READ_BIB_ROM_SIZE)
547 * This block extends outside standard config
548 * area (and the array we're reading it
549 * into). That's broken, so ignore this
550 * device.
552 goto out;
555 * Now read in the block. If this is a directory
556 * block, check the entries as we read them to see if
557 * it references another block, and push it in that case.
559 while (i < end) {
560 if (read_rom(device, generation, i, &rom[i]) !=
561 RCODE_COMPLETE)
562 goto out;
563 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
564 sp < READ_BIB_STACK_SIZE)
565 stack[sp++] = i + rom[i];
566 i++;
568 if (length < i)
569 length = i;
572 old_rom = device->config_rom;
573 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
574 if (new_rom == NULL)
575 goto out;
577 down_write(&fw_device_rwsem);
578 device->config_rom = new_rom;
579 device->config_rom_length = length;
580 up_write(&fw_device_rwsem);
582 kfree(old_rom);
583 ret = 0;
584 device->max_rec = rom[2] >> 12 & 0xf;
585 device->cmc = rom[2] >> 30 & 1;
586 device->irmc = rom[2] >> 31 & 1;
587 out:
588 kfree(rom);
590 return ret;
593 static void fw_unit_release(struct device *dev)
595 struct fw_unit *unit = fw_unit(dev);
597 kfree(unit);
600 static struct device_type fw_unit_type = {
601 .uevent = fw_unit_uevent,
602 .release = fw_unit_release,
605 static bool is_fw_unit(struct device *dev)
607 return dev->type == &fw_unit_type;
610 static void create_units(struct fw_device *device)
612 struct fw_csr_iterator ci;
613 struct fw_unit *unit;
614 int key, value, i;
616 i = 0;
617 fw_csr_iterator_init(&ci, &device->config_rom[5]);
618 while (fw_csr_iterator_next(&ci, &key, &value)) {
619 if (key != (CSR_UNIT | CSR_DIRECTORY))
620 continue;
623 * Get the address of the unit directory and try to
624 * match the drivers id_tables against it.
626 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
627 if (unit == NULL) {
628 fw_error("failed to allocate memory for unit\n");
629 continue;
632 unit->directory = ci.p + value - 1;
633 unit->device.bus = &fw_bus_type;
634 unit->device.type = &fw_unit_type;
635 unit->device.parent = &device->device;
636 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
638 BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
639 ARRAY_SIZE(fw_unit_attributes) +
640 ARRAY_SIZE(config_rom_attributes));
641 init_fw_attribute_group(&unit->device,
642 fw_unit_attributes,
643 &unit->attribute_group);
645 if (device_register(&unit->device) < 0)
646 goto skip_unit;
648 continue;
650 skip_unit:
651 kfree(unit);
655 static int shutdown_unit(struct device *device, void *data)
657 device_unregister(device);
659 return 0;
663 * fw_device_rwsem acts as dual purpose mutex:
664 * - serializes accesses to fw_device_idr,
665 * - serializes accesses to fw_device.config_rom/.config_rom_length and
666 * fw_unit.directory, unless those accesses happen at safe occasions
668 DECLARE_RWSEM(fw_device_rwsem);
670 DEFINE_IDR(fw_device_idr);
671 int fw_cdev_major;
673 struct fw_device *fw_device_get_by_devt(dev_t devt)
675 struct fw_device *device;
677 down_read(&fw_device_rwsem);
678 device = idr_find(&fw_device_idr, MINOR(devt));
679 if (device)
680 fw_device_get(device);
681 up_read(&fw_device_rwsem);
683 return device;
687 * These defines control the retry behavior for reading the config
688 * rom. It shouldn't be necessary to tweak these; if the device
689 * doesn't respond to a config rom read within 10 seconds, it's not
690 * going to respond at all. As for the initial delay, a lot of
691 * devices will be able to respond within half a second after bus
692 * reset. On the other hand, it's not really worth being more
693 * aggressive than that, since it scales pretty well; if 10 devices
694 * are plugged in, they're all getting read within one second.
697 #define MAX_RETRIES 10
698 #define RETRY_DELAY (3 * HZ)
699 #define INITIAL_DELAY (HZ / 2)
700 #define SHUTDOWN_DELAY (2 * HZ)
702 static void fw_device_shutdown(struct work_struct *work)
704 struct fw_device *device =
705 container_of(work, struct fw_device, work.work);
706 int minor = MINOR(device->device.devt);
708 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
709 && !list_empty(&device->card->link)) {
710 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
711 return;
714 if (atomic_cmpxchg(&device->state,
715 FW_DEVICE_GONE,
716 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
717 return;
719 fw_device_cdev_remove(device);
720 device_for_each_child(&device->device, NULL, shutdown_unit);
721 device_unregister(&device->device);
723 down_write(&fw_device_rwsem);
724 idr_remove(&fw_device_idr, minor);
725 up_write(&fw_device_rwsem);
727 fw_device_put(device);
730 static void fw_device_release(struct device *dev)
732 struct fw_device *device = fw_device(dev);
733 struct fw_card *card = device->card;
734 unsigned long flags;
737 * Take the card lock so we don't set this to NULL while a
738 * FW_NODE_UPDATED callback is being handled or while the
739 * bus manager work looks at this node.
741 spin_lock_irqsave(&card->lock, flags);
742 device->node->data = NULL;
743 spin_unlock_irqrestore(&card->lock, flags);
745 fw_node_put(device->node);
746 kfree(device->config_rom);
747 kfree(device);
748 fw_card_put(card);
751 static struct device_type fw_device_type = {
752 .release = fw_device_release,
755 static bool is_fw_device(struct device *dev)
757 return dev->type == &fw_device_type;
760 static int update_unit(struct device *dev, void *data)
762 struct fw_unit *unit = fw_unit(dev);
763 struct fw_driver *driver = (struct fw_driver *)dev->driver;
765 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
766 down(&dev->sem);
767 driver->update(unit);
768 up(&dev->sem);
771 return 0;
774 static void fw_device_update(struct work_struct *work)
776 struct fw_device *device =
777 container_of(work, struct fw_device, work.work);
779 fw_device_cdev_update(device);
780 device_for_each_child(&device->device, NULL, update_unit);
784 * If a device was pending for deletion because its node went away but its
785 * bus info block and root directory header matches that of a newly discovered
786 * device, revive the existing fw_device.
787 * The newly allocated fw_device becomes obsolete instead.
789 static int lookup_existing_device(struct device *dev, void *data)
791 struct fw_device *old = fw_device(dev);
792 struct fw_device *new = data;
793 struct fw_card *card = new->card;
794 int match = 0;
796 if (!is_fw_device(dev))
797 return 0;
799 down_read(&fw_device_rwsem); /* serialize config_rom access */
800 spin_lock_irq(&card->lock); /* serialize node access */
802 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
803 atomic_cmpxchg(&old->state,
804 FW_DEVICE_GONE,
805 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
806 struct fw_node *current_node = new->node;
807 struct fw_node *obsolete_node = old->node;
809 new->node = obsolete_node;
810 new->node->data = new;
811 old->node = current_node;
812 old->node->data = old;
814 old->max_speed = new->max_speed;
815 old->node_id = current_node->node_id;
816 smp_wmb(); /* update node_id before generation */
817 old->generation = card->generation;
818 old->config_rom_retries = 0;
819 fw_notify("rediscovered device %s\n", dev_name(dev));
821 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
822 schedule_delayed_work(&old->work, 0);
824 if (current_node == card->root_node)
825 fw_schedule_bm_work(card, 0);
827 match = 1;
830 spin_unlock_irq(&card->lock);
831 up_read(&fw_device_rwsem);
833 return match;
836 enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
838 static void set_broadcast_channel(struct fw_device *device, int generation)
840 struct fw_card *card = device->card;
841 __be32 data;
842 int rcode;
844 if (!card->broadcast_channel_allocated)
845 return;
848 * The Broadcast_Channel Valid bit is required by nodes which want to
849 * transmit on this channel. Such transmissions are practically
850 * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
851 * to be IRM capable and have a max_rec of 8 or more. We use this fact
852 * to narrow down to which nodes we send Broadcast_Channel updates.
854 if (!device->irmc || device->max_rec < 8)
855 return;
858 * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
859 * Perform a read test first.
861 if (device->bc_implemented == BC_UNKNOWN) {
862 rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
863 device->node_id, generation, device->max_speed,
864 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
865 &data, 4);
866 switch (rcode) {
867 case RCODE_COMPLETE:
868 if (data & cpu_to_be32(1 << 31)) {
869 device->bc_implemented = BC_IMPLEMENTED;
870 break;
872 /* else fall through to case address error */
873 case RCODE_ADDRESS_ERROR:
874 device->bc_implemented = BC_UNIMPLEMENTED;
878 if (device->bc_implemented == BC_IMPLEMENTED) {
879 data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
880 BROADCAST_CHANNEL_VALID);
881 fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
882 device->node_id, generation, device->max_speed,
883 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
884 &data, 4);
888 int fw_device_set_broadcast_channel(struct device *dev, void *gen)
890 if (is_fw_device(dev))
891 set_broadcast_channel(fw_device(dev), (long)gen);
893 return 0;
896 static void fw_device_init(struct work_struct *work)
898 struct fw_device *device =
899 container_of(work, struct fw_device, work.work);
900 struct device *revived_dev;
901 int minor, ret;
904 * All failure paths here set node->data to NULL, so that we
905 * don't try to do device_for_each_child() on a kfree()'d
906 * device.
909 if (read_bus_info_block(device, device->generation) < 0) {
910 if (device->config_rom_retries < MAX_RETRIES &&
911 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
912 device->config_rom_retries++;
913 schedule_delayed_work(&device->work, RETRY_DELAY);
914 } else {
915 fw_notify("giving up on config rom for node id %x\n",
916 device->node_id);
917 if (device->node == device->card->root_node)
918 fw_schedule_bm_work(device->card, 0);
919 fw_device_release(&device->device);
921 return;
924 revived_dev = device_find_child(device->card->device,
925 device, lookup_existing_device);
926 if (revived_dev) {
927 put_device(revived_dev);
928 fw_device_release(&device->device);
930 return;
933 device_initialize(&device->device);
935 fw_device_get(device);
936 down_write(&fw_device_rwsem);
937 ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
938 idr_get_new(&fw_device_idr, device, &minor) :
939 -ENOMEM;
940 up_write(&fw_device_rwsem);
942 if (ret < 0)
943 goto error;
945 device->device.bus = &fw_bus_type;
946 device->device.type = &fw_device_type;
947 device->device.parent = device->card->device;
948 device->device.devt = MKDEV(fw_cdev_major, minor);
949 dev_set_name(&device->device, "fw%d", minor);
951 BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
952 ARRAY_SIZE(fw_device_attributes) +
953 ARRAY_SIZE(config_rom_attributes));
954 init_fw_attribute_group(&device->device,
955 fw_device_attributes,
956 &device->attribute_group);
958 if (device_add(&device->device)) {
959 fw_error("Failed to add device.\n");
960 goto error_with_cdev;
963 create_units(device);
966 * Transition the device to running state. If it got pulled
967 * out from under us while we did the intialization work, we
968 * have to shut down the device again here. Normally, though,
969 * fw_node_event will be responsible for shutting it down when
970 * necessary. We have to use the atomic cmpxchg here to avoid
971 * racing with the FW_NODE_DESTROYED case in
972 * fw_node_event().
974 if (atomic_cmpxchg(&device->state,
975 FW_DEVICE_INITIALIZING,
976 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
977 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
978 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
979 } else {
980 if (device->config_rom_retries)
981 fw_notify("created device %s: GUID %08x%08x, S%d00, "
982 "%d config ROM retries\n",
983 dev_name(&device->device),
984 device->config_rom[3], device->config_rom[4],
985 1 << device->max_speed,
986 device->config_rom_retries);
987 else
988 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
989 dev_name(&device->device),
990 device->config_rom[3], device->config_rom[4],
991 1 << device->max_speed);
992 device->config_rom_retries = 0;
994 set_broadcast_channel(device, device->generation);
998 * Reschedule the IRM work if we just finished reading the
999 * root node config rom. If this races with a bus reset we
1000 * just end up running the IRM work a couple of extra times -
1001 * pretty harmless.
1003 if (device->node == device->card->root_node)
1004 fw_schedule_bm_work(device->card, 0);
1006 return;
1008 error_with_cdev:
1009 down_write(&fw_device_rwsem);
1010 idr_remove(&fw_device_idr, minor);
1011 up_write(&fw_device_rwsem);
1012 error:
1013 fw_device_put(device); /* fw_device_idr's reference */
1015 put_device(&device->device); /* our reference */
1018 enum {
1019 REREAD_BIB_ERROR,
1020 REREAD_BIB_GONE,
1021 REREAD_BIB_UNCHANGED,
1022 REREAD_BIB_CHANGED,
1025 /* Reread and compare bus info block and header of root directory */
1026 static int reread_bus_info_block(struct fw_device *device, int generation)
1028 u32 q;
1029 int i;
1031 for (i = 0; i < 6; i++) {
1032 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
1033 return REREAD_BIB_ERROR;
1035 if (i == 0 && q == 0)
1036 return REREAD_BIB_GONE;
1038 if (q != device->config_rom[i])
1039 return REREAD_BIB_CHANGED;
1042 return REREAD_BIB_UNCHANGED;
1045 static void fw_device_refresh(struct work_struct *work)
1047 struct fw_device *device =
1048 container_of(work, struct fw_device, work.work);
1049 struct fw_card *card = device->card;
1050 int node_id = device->node_id;
1052 switch (reread_bus_info_block(device, device->generation)) {
1053 case REREAD_BIB_ERROR:
1054 if (device->config_rom_retries < MAX_RETRIES / 2 &&
1055 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1056 device->config_rom_retries++;
1057 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
1059 return;
1061 goto give_up;
1063 case REREAD_BIB_GONE:
1064 goto gone;
1066 case REREAD_BIB_UNCHANGED:
1067 if (atomic_cmpxchg(&device->state,
1068 FW_DEVICE_INITIALIZING,
1069 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1070 goto gone;
1072 fw_device_update(work);
1073 device->config_rom_retries = 0;
1074 goto out;
1076 case REREAD_BIB_CHANGED:
1077 break;
1081 * Something changed. We keep things simple and don't investigate
1082 * further. We just destroy all previous units and create new ones.
1084 device_for_each_child(&device->device, NULL, shutdown_unit);
1086 if (read_bus_info_block(device, device->generation) < 0) {
1087 if (device->config_rom_retries < MAX_RETRIES &&
1088 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1089 device->config_rom_retries++;
1090 schedule_delayed_work(&device->work, RETRY_DELAY);
1092 return;
1094 goto give_up;
1097 create_units(device);
1099 /* Userspace may want to re-read attributes. */
1100 kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1102 if (atomic_cmpxchg(&device->state,
1103 FW_DEVICE_INITIALIZING,
1104 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1105 goto gone;
1107 fw_notify("refreshed device %s\n", dev_name(&device->device));
1108 device->config_rom_retries = 0;
1109 goto out;
1111 give_up:
1112 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
1113 gone:
1114 atomic_set(&device->state, FW_DEVICE_GONE);
1115 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1116 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
1117 out:
1118 if (node_id == card->root_node->node_id)
1119 fw_schedule_bm_work(card, 0);
1122 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1124 struct fw_device *device;
1126 switch (event) {
1127 case FW_NODE_CREATED:
1128 case FW_NODE_LINK_ON:
1129 if (!node->link_on)
1130 break;
1131 create:
1132 device = kzalloc(sizeof(*device), GFP_ATOMIC);
1133 if (device == NULL)
1134 break;
1137 * Do minimal intialization of the device here, the
1138 * rest will happen in fw_device_init().
1140 * Attention: A lot of things, even fw_device_get(),
1141 * cannot be done before fw_device_init() finished!
1142 * You can basically just check device->state and
1143 * schedule work until then, but only while holding
1144 * card->lock.
1146 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
1147 device->card = fw_card_get(card);
1148 device->node = fw_node_get(node);
1149 device->node_id = node->node_id;
1150 device->generation = card->generation;
1151 device->is_local = node == card->local_node;
1152 mutex_init(&device->client_list_mutex);
1153 INIT_LIST_HEAD(&device->client_list);
1156 * Set the node data to point back to this device so
1157 * FW_NODE_UPDATED callbacks can update the node_id
1158 * and generation for the device.
1160 node->data = device;
1163 * Many devices are slow to respond after bus resets,
1164 * especially if they are bus powered and go through
1165 * power-up after getting plugged in. We schedule the
1166 * first config rom scan half a second after bus reset.
1168 INIT_DELAYED_WORK(&device->work, fw_device_init);
1169 schedule_delayed_work(&device->work, INITIAL_DELAY);
1170 break;
1172 case FW_NODE_INITIATED_RESET:
1173 device = node->data;
1174 if (device == NULL)
1175 goto create;
1177 device->node_id = node->node_id;
1178 smp_wmb(); /* update node_id before generation */
1179 device->generation = card->generation;
1180 if (atomic_cmpxchg(&device->state,
1181 FW_DEVICE_RUNNING,
1182 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1183 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1184 schedule_delayed_work(&device->work,
1185 device->is_local ? 0 : INITIAL_DELAY);
1187 break;
1189 case FW_NODE_UPDATED:
1190 if (!node->link_on || node->data == NULL)
1191 break;
1193 device = node->data;
1194 device->node_id = node->node_id;
1195 smp_wmb(); /* update node_id before generation */
1196 device->generation = card->generation;
1197 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1198 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1199 schedule_delayed_work(&device->work, 0);
1201 break;
1203 case FW_NODE_DESTROYED:
1204 case FW_NODE_LINK_OFF:
1205 if (!node->data)
1206 break;
1209 * Destroy the device associated with the node. There
1210 * are two cases here: either the device is fully
1211 * initialized (FW_DEVICE_RUNNING) or we're in the
1212 * process of reading its config rom
1213 * (FW_DEVICE_INITIALIZING). If it is fully
1214 * initialized we can reuse device->work to schedule a
1215 * full fw_device_shutdown(). If not, there's work
1216 * scheduled to read it's config rom, and we just put
1217 * the device in shutdown state to have that code fail
1218 * to create the device.
1220 device = node->data;
1221 if (atomic_xchg(&device->state,
1222 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
1223 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1224 schedule_delayed_work(&device->work,
1225 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
1227 break;