22 #include <linux/ctype.h>
24 #include <linux/device.h>
25 #include <linux/errno.h>
31 #include <linux/list.h>
33 #include <linux/module.h>
35 #include <linux/random.h>
37 #include <linux/slab.h>
39 #include <linux/string.h>
43 #include <asm/byteorder.h>
50 ci->
end = ci->
p + (p[0] >> 16);
57 *value = *ci->
p & 0xffffff;
59 return ci->
p++ < ci->
end;
63 static const u32 *search_leaf(
const u32 *directory,
int search_key)
70 if (last_key == search_key &&
72 return ci.p - 1 +
value;
80 static int textual_leaf_to_string(
const u32 *
block,
char *
buf,
size_t size)
82 unsigned int quadlets,
i;
88 quadlets =
min(block[0] >> 16, 256
U);
92 if (block[1] != 0 || block[2] != 0)
98 for (i = 0; i < quadlets * 4 && i < size - 1; i++) {
99 c = block[i / 4] >> (24 - 8 * (i % 4));
122 const u32 *
leaf = search_leaf(directory, key);
126 return textual_leaf_to_string(leaf, buf, size);
130 static void get_ids(
const u32 *directory,
int *
id)
146 static void get_modalias_ids(
struct fw_unit *
unit,
int *
id)
148 get_ids(&fw_parent_device(unit)->config_rom[5],
id);
162 if (
id[3] == id_table->
version)
168 static bool is_fw_unit(
struct device *
dev);
174 int id[] = {0, 0, 0, 0};
177 if (!is_fw_unit(dev))
180 get_modalias_ids(
fw_unit(dev),
id);
183 if (match_ids(id_table,
id))
189 static int get_modalias(
struct fw_unit *unit,
char *
buffer,
size_t buffer_size)
191 int id[] = {0, 0, 0, 0};
193 get_modalias_ids(unit,
id);
195 return snprintf(buffer, buffer_size,
196 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
197 id[0],
id[1],
id[2],
id[3]);
205 get_modalias(unit, modalias,
sizeof(modalias));
215 .match = fw_unit_match,
226 return device->
card->driver->enable_phys_dma(device->
card,
255 if (attr->
key == key) {
266 #define IMMEDIATE_ATTR(name, key) \
267 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
297 while (ret > 0 &&
isspace(buf[ret - 1]))
308 #define TEXT_LEAF_ATTR(name, key) \
309 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
322 static void init_fw_attribute_group(
struct device *dev,
329 for (j = 0; attrs[
j].
attr.name !=
NULL; j++)
333 attr = &config_rom_attributes[
i].
attr;
334 if (attr->
show(dev, attr,
NULL) < 0)
352 length = get_modalias(unit, buf,
PAGE_SIZE);
353 strcpy(buf + length,
"\n");
374 static ssize_t config_rom_show(
struct device *dev,
388 static ssize_t guid_show(
struct device *dev,
402 static ssize_t is_local_show(
struct device *dev,
410 static int units_sprintf(
char *buf,
const u32 *directory)
414 int specifier_id = 0;
421 specifier_id =
value;
429 return sprintf(buf,
"0x%06x:0x%06x ", specifier_id, version);
432 static ssize_t units_show(
struct device *dev,
444 i += units_sprintf(&buf[i], ci.p + value - 1);
464 static int read_rom(
struct fw_device *device,
473 for (i = 10; i < 100; i += 10) {
476 generation, device->
max_speed, offset, data, 4);
486 #define MAX_CONFIG_ROM_SIZE 256
496 static int read_config_rom(
struct fw_device *device,
int generation)
499 const u32 *old_rom, *new_rom;
510 memset(rom, 0,
sizeof(*rom) * MAX_CONFIG_ROM_SIZE);
515 for (i = 0; i < 5; i++) {
516 ret = read_rom(device, generation, i, &rom[i]);
527 if (i == 0 && rom[i] == 0) {
544 if ((rom[2] & 0x7) < device->
max_speed ||
554 if (read_rom(device, generation, 0, &dummy) ==
570 stack[sp++] = 0xc0000005;
580 if (
WARN_ON(i >= MAX_CONFIG_ROM_SIZE)) {
586 ret = read_rom(device, generation, i, &rom[i]);
589 end = i + (rom[
i] >> 16) + 1;
590 if (end > MAX_CONFIG_ROM_SIZE) {
596 fw_err(card,
"skipped invalid ROM block %x at %llx\n",
609 for (; i <
end; i++) {
610 ret = read_rom(device, generation, i, &rom[i]);
614 if ((key >> 30) != 3 || (rom[i] >> 30) < 2)
623 if (i + (rom[i] & 0xffffff) >= MAX_CONFIG_ROM_SIZE) {
625 "skipped unsupported ROM entry %x at %llx\n",
631 stack[sp++] = i + rom[
i];
639 if (new_rom ==
NULL) {
651 device->
max_rec = rom[2] >> 12 & 0xf;
652 device->
cmc = rom[2] >> 30 & 1;
653 device->
irmc = rom[2] >> 31 & 1;
660 static void fw_unit_release(
struct device *dev)
664 fw_device_put(fw_parent_device(unit));
669 .uevent = fw_unit_uevent,
670 .release = fw_unit_release,
673 static bool is_fw_unit(
struct device *dev)
675 return dev->
type == &fw_unit_type;
678 static void create_units(
struct fw_device *device)
696 fw_err(device->
card,
"out of memory for unit\n");
702 unit->
device.type = &fw_unit_type;
709 init_fw_attribute_group(&unit->
device,
716 fw_device_get(device);
724 static int shutdown_unit(
struct device *device,
void *data)
749 fw_device_get(device);
758 static void fw_schedule_device_work(
struct fw_device *device,
775 #define MAX_RETRIES 10
776 #define RETRY_DELAY (3 * HZ)
777 #define INITIAL_DELAY (HZ / 2)
778 #define SHUTDOWN_DELAY (2 * HZ)
788 && !list_empty(&device->
card->link)) {
806 fw_device_put(device);
809 static void fw_device_release(
struct device *dev)
822 spin_unlock_irqrestore(&card->
lock, flags);
824 fw_node_put(device->
node);
831 .release = fw_device_release,
834 static bool is_fw_device(
struct device *dev)
836 return dev->
type == &fw_device_type;
839 static int update_unit(
struct device *dev,
void *data)
868 static int lookup_existing_device(
struct device *dev,
void *data)
875 if (!is_fw_device(dev))
879 spin_lock_irq(&card->
lock);
885 struct fw_node *current_node =
new->node;
888 new->node = obsolete_node;
889 new->node->
data =
new;
890 old->
node = current_node;
891 old->
node->data = old;
898 fw_notice(card,
"rediscovered device %s\n", dev_name(dev));
901 fw_schedule_device_work(old, 0);
909 spin_unlock_irq(&card->
lock);
917 static void set_broadcast_channel(
struct fw_device *device,
int generation)
969 if (is_fw_device(dev))
970 set_broadcast_channel(
fw_device(dev), (
long)gen);
980 struct device *revived_dev;
989 ret = read_config_rom(device, device->
generation);
996 if (device->
node->link_on)
997 fw_notice(card,
"giving up on node %x: reading config rom failed: %s\n",
1002 fw_device_release(&device->
device);
1008 device, lookup_existing_device);
1011 fw_device_release(&device->
device);
1018 fw_device_get(device);
1029 device->
device.type = &fw_device_type;
1031 device->
device.devt =
MKDEV(fw_cdev_major, minor);
1037 init_fw_attribute_group(&device->
device,
1038 fw_device_attributes,
1042 fw_err(card,
"failed to add device\n");
1043 goto error_with_cdev;
1046 create_units(device);
1063 fw_notice(card,
"created device %s: GUID %08x%08x, S%d00\n",
1064 dev_name(&device->
device),
1069 set_broadcast_channel(device, device->
generation);
1090 fw_device_put(device);
1096 static int reread_config_rom(
struct fw_device *device,
int generation,
1102 for (i = 0; i < 6; i++) {
1103 rcode = read_rom(device, generation, i, &q);
1107 if (i == 0 && q == 0)
1121 static void fw_device_refresh(
struct work_struct *work)
1129 ret = reread_config_rom(device, device->
generation, &changed);
1131 goto failed_config_rom;
1139 fw_device_update(work);
1150 ret = read_config_rom(device, device->
generation);
1152 goto failed_config_rom;
1155 create_units(device);
1165 fw_notice(card,
"refreshed device %s\n", dev_name(&device->
device));
1177 fw_notice(card,
"giving up on refresh of device %s: %s\n",
1184 if (node_id == card->
root_node->node_id)
1201 device = kzalloc(
sizeof(*device),
GFP_ATOMIC);
1216 device->
card = fw_card_get(card);
1217 device->
node = fw_node_get(node);
1243 device = node->
data;
1254 fw_schedule_device_work(device,
1260 device = node->
data;
1269 fw_schedule_device_work(device, 0);
1290 device = node->
data;
1294 fw_schedule_device_work(device,