Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Device probing and sysfs code. |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 19 | */ |
| 20 | |
| 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> |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 27 | #include <linux/idr.h> |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 28 | #include <linux/jiffies.h> |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 29 | #include <linux/string.h> |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame^] | 30 | #include <linux/mutex.h> |
Matthew Wilcox | 6188e10 | 2008-04-18 22:21:05 -0400 | [diff] [blame] | 31 | #include <linux/rwsem.h> |
| 32 | #include <linux/semaphore.h> |
Jay Fenlason | cf417e54 | 2008-10-03 11:19:09 -0400 | [diff] [blame] | 33 | #include <linux/spinlock.h> |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 34 | #include <asm/system.h> |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 35 | #include <linux/ctype.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 36 | #include "fw-transaction.h" |
| 37 | #include "fw-topology.h" |
| 38 | #include "fw-device.h" |
| 39 | |
| 40 | void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p) |
| 41 | { |
| 42 | ci->p = p + 1; |
| 43 | ci->end = ci->p + (p[0] >> 16); |
| 44 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 45 | EXPORT_SYMBOL(fw_csr_iterator_init); |
| 46 | |
| 47 | int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value) |
| 48 | { |
| 49 | *key = *ci->p >> 24; |
| 50 | *value = *ci->p & 0xffffff; |
| 51 | |
| 52 | return ci->p++ < ci->end; |
| 53 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 54 | EXPORT_SYMBOL(fw_csr_iterator_next); |
| 55 | |
| 56 | static int is_fw_unit(struct device *dev); |
| 57 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 58 | static int match_unit_directory(u32 * directory, const struct fw_device_id *id) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 59 | { |
| 60 | struct fw_csr_iterator ci; |
| 61 | int key, value, match; |
| 62 | |
| 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; |
| 74 | } |
| 75 | |
| 76 | return (match & id->match_flags) == id->match_flags; |
| 77 | } |
| 78 | |
| 79 | static int fw_unit_match(struct device *dev, struct device_driver *drv) |
| 80 | { |
| 81 | struct fw_unit *unit = fw_unit(dev); |
| 82 | struct fw_driver *driver = fw_driver(drv); |
| 83 | int i; |
| 84 | |
| 85 | /* We only allow binding to fw_units. */ |
| 86 | if (!is_fw_unit(dev)) |
| 87 | return 0; |
| 88 | |
| 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; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size) |
| 98 | { |
| 99 | struct fw_device *device = fw_device(unit->device.parent); |
| 100 | struct fw_csr_iterator ci; |
| 101 | |
| 102 | int key, value; |
| 103 | int vendor = 0; |
| 104 | int model = 0; |
| 105 | int specifier_id = 0; |
| 106 | int version = 0; |
| 107 | |
| 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; |
| 117 | } |
| 118 | } |
| 119 | |
| 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; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return snprintf(buffer, buffer_size, |
| 133 | "ieee1394:ven%08Xmo%08Xsp%08Xver%08X", |
| 134 | vendor, model, specifier_id, version); |
| 135 | } |
| 136 | |
| 137 | static int |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 138 | fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 139 | { |
| 140 | struct fw_unit *unit = fw_unit(dev); |
| 141 | char modalias[64]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 142 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 143 | get_modalias(unit, modalias, sizeof(modalias)); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 144 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 145 | if (add_uevent_var(env, "MODALIAS=%s", modalias)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 146 | return -ENOMEM; |
| 147 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | struct bus_type fw_bus_type = { |
Kristian Høgsberg | 362c2c8 | 2007-02-06 14:49:37 -0500 | [diff] [blame] | 152 | .name = "firewire", |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 153 | .match = fw_unit_match, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 154 | }; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 155 | EXPORT_SYMBOL(fw_bus_type); |
| 156 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 157 | static void fw_device_release(struct device *dev) |
| 158 | { |
| 159 | struct fw_device *device = fw_device(dev); |
Stefan Richter | 855c603 | 2008-02-27 22:14:27 +0100 | [diff] [blame] | 160 | struct fw_card *card = device->card; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 161 | unsigned long flags; |
| 162 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 163 | /* |
| 164 | * Take the card lock so we don't set this to NULL while a |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 165 | * FW_NODE_UPDATED callback is being handled or while the |
| 166 | * bus manager work looks at this node. |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 167 | */ |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 168 | spin_lock_irqsave(&card->lock, flags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 169 | device->node->data = NULL; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 170 | spin_unlock_irqrestore(&card->lock, flags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 171 | |
| 172 | fw_node_put(device->node); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 173 | kfree(device->config_rom); |
| 174 | kfree(device); |
Stefan Richter | 459f792 | 2008-05-24 16:50:22 +0200 | [diff] [blame] | 175 | fw_card_put(card); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int fw_device_enable_phys_dma(struct fw_device *device) |
| 179 | { |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 180 | int generation = device->generation; |
| 181 | |
| 182 | /* device->node_id, accessed below, must not be older than generation */ |
| 183 | smp_rmb(); |
| 184 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 185 | return device->card->driver->enable_phys_dma(device->card, |
| 186 | device->node_id, |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 187 | generation); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 188 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 189 | EXPORT_SYMBOL(fw_device_enable_phys_dma); |
| 190 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 191 | struct config_rom_attribute { |
| 192 | struct device_attribute attr; |
| 193 | u32 key; |
| 194 | }; |
| 195 | |
| 196 | static ssize_t |
| 197 | show_immediate(struct device *dev, struct device_attribute *dattr, char *buf) |
| 198 | { |
| 199 | struct config_rom_attribute *attr = |
| 200 | container_of(dattr, struct config_rom_attribute, attr); |
| 201 | struct fw_csr_iterator ci; |
| 202 | u32 *dir; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 203 | int key, value, ret = -ENOENT; |
| 204 | |
| 205 | down_read(&fw_device_rwsem); |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 206 | |
| 207 | if (is_fw_unit(dev)) |
| 208 | dir = fw_unit(dev)->directory; |
| 209 | else |
| 210 | dir = fw_device(dev)->config_rom + 5; |
| 211 | |
| 212 | fw_csr_iterator_init(&ci, dir); |
| 213 | while (fw_csr_iterator_next(&ci, &key, &value)) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 214 | if (attr->key == key) { |
| 215 | ret = snprintf(buf, buf ? PAGE_SIZE : 0, |
| 216 | "0x%06x\n", value); |
| 217 | break; |
| 218 | } |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 219 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 220 | up_read(&fw_device_rwsem); |
| 221 | |
| 222 | return ret; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | #define IMMEDIATE_ATTR(name, key) \ |
| 226 | { __ATTR(name, S_IRUGO, show_immediate, NULL), key } |
| 227 | |
| 228 | static ssize_t |
| 229 | show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf) |
| 230 | { |
| 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; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 235 | int length, key, value, last_key = 0, ret = -ENOENT; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 236 | char *b; |
| 237 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 238 | down_read(&fw_device_rwsem); |
| 239 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 240 | if (is_fw_unit(dev)) |
| 241 | dir = fw_unit(dev)->directory; |
| 242 | else |
| 243 | dir = fw_device(dev)->config_rom + 5; |
| 244 | |
| 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; |
| 251 | } |
| 252 | |
| 253 | if (block == NULL) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 254 | goto out; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 255 | |
| 256 | length = min(block[0] >> 16, 256U); |
| 257 | if (length < 3) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 258 | goto out; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 259 | |
| 260 | if (block[1] != 0 || block[2] != 0) |
| 261 | /* Unknown encoding. */ |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 262 | goto out; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 263 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 264 | if (buf == NULL) { |
| 265 | ret = length * 4; |
| 266 | goto out; |
| 267 | } |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 268 | |
| 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); |
| 273 | |
| 274 | /* Strip trailing whitespace and add newline. */ |
| 275 | while (b--, (isspace(*b) || *b == '\0') && b > buf); |
| 276 | strcpy(b + 1, "\n"); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 277 | ret = b + 2 - buf; |
| 278 | out: |
| 279 | up_read(&fw_device_rwsem); |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 280 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 281 | return ret; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #define TEXT_LEAF_ATTR(name, key) \ |
| 285 | { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key } |
| 286 | |
| 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), |
| 296 | }; |
| 297 | |
| 298 | static void |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 299 | init_fw_attribute_group(struct device *dev, |
| 300 | struct device_attribute *attrs, |
| 301 | struct fw_attribute_group *group) |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 302 | { |
| 303 | struct device_attribute *attr; |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 304 | int i, j; |
| 305 | |
| 306 | for (j = 0; attrs[j].attr.name != NULL; j++) |
| 307 | group->attrs[j] = &attrs[j].attr; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 308 | |
| 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; |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 313 | group->attrs[j++] = &attr->attr; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 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; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 324 | static ssize_t |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 325 | modalias_show(struct device *dev, |
| 326 | struct device_attribute *attr, char *buf) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 327 | { |
| 328 | struct fw_unit *unit = fw_unit(dev); |
| 329 | int length; |
| 330 | |
| 331 | length = get_modalias(unit, buf, PAGE_SIZE); |
| 332 | strcpy(buf + length, "\n"); |
| 333 | |
| 334 | return length + 1; |
| 335 | } |
| 336 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 337 | static ssize_t |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 338 | rom_index_show(struct device *dev, |
| 339 | struct device_attribute *attr, char *buf) |
Kristian Høgsberg | 048961e | 2007-03-07 12:12:46 -0500 | [diff] [blame] | 340 | { |
| 341 | struct fw_device *device = fw_device(dev->parent); |
| 342 | struct fw_unit *unit = fw_unit(dev); |
| 343 | |
| 344 | return snprintf(buf, PAGE_SIZE, "%d\n", |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 345 | (int)(unit->directory - device->config_rom)); |
Kristian Høgsberg | 048961e | 2007-03-07 12:12:46 -0500 | [diff] [blame] | 346 | } |
| 347 | |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 348 | static struct device_attribute fw_unit_attributes[] = { |
| 349 | __ATTR_RO(modalias), |
| 350 | __ATTR_RO(rom_index), |
| 351 | __ATTR_NULL, |
| 352 | }; |
| 353 | |
| 354 | static ssize_t |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 355 | config_rom_show(struct device *dev, struct device_attribute *attr, char *buf) |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 356 | { |
| 357 | struct fw_device *device = fw_device(dev); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 358 | size_t length; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 359 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 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); |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 364 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 365 | return length; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 368 | static ssize_t |
| 369 | guid_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 370 | { |
| 371 | struct fw_device *device = fw_device(dev); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 372 | int ret; |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 373 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 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); |
| 378 | |
| 379 | return ret; |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 382 | static struct device_attribute fw_device_attributes[] = { |
| 383 | __ATTR_RO(config_rom), |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 384 | __ATTR_RO(guid), |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 385 | __ATTR_NULL, |
Kristian Høgsberg | 048961e | 2007-03-07 12:12:46 -0500 | [diff] [blame] | 386 | }; |
| 387 | |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 388 | static int |
| 389 | read_rom(struct fw_device *device, int generation, int index, u32 *data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 390 | { |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 391 | int rcode; |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 392 | |
| 393 | /* device->node_id, accessed below, must not be older than generation */ |
| 394 | smp_rmb(); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 395 | |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 396 | rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST, |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 397 | device->node_id, generation, device->max_speed, |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 398 | (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4, |
| 399 | data, 4); |
| 400 | be32_to_cpus(data); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 401 | |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 402 | return rcode; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 405 | #define READ_BIB_ROM_SIZE 256 |
| 406 | #define READ_BIB_STACK_SIZE 16 |
| 407 | |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 408 | /* |
| 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. |
| 414 | */ |
| 415 | static int read_bus_info_block(struct fw_device *device, int generation) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 416 | { |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 417 | u32 *rom, *stack, *old_rom, *new_rom; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 418 | u32 sp, key; |
| 419 | int i, end, length, ret = -1; |
| 420 | |
| 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; |
| 425 | |
| 426 | stack = &rom[READ_BIB_ROM_SIZE]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 427 | |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 428 | device->max_speed = SCODE_100; |
| 429 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 430 | /* First read the bus info block. */ |
| 431 | for (i = 0; i < 5; i++) { |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 432 | if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 433 | goto out; |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 434 | /* |
| 435 | * As per IEEE1212 7.2, during power-up, devices can |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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 |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 440 | * retry mechanism will try again later. |
| 441 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 442 | if (i == 0 && rom[i] == 0) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 443 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 444 | } |
| 445 | |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 446 | device->max_speed = device->node->max_speed; |
| 447 | |
| 448 | /* |
| 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. |
| 456 | */ |
| 457 | if ((rom[2] & 0x7) < device->max_speed || |
| 458 | device->max_speed == SCODE_BETA || |
| 459 | device->card->beta_repeaters_present) { |
| 460 | u32 dummy; |
| 461 | |
| 462 | /* for S1600 and S3200 */ |
| 463 | if (device->max_speed == SCODE_BETA) |
| 464 | device->max_speed = device->card->link_speed; |
| 465 | |
| 466 | while (device->max_speed > SCODE_100) { |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 467 | if (read_rom(device, generation, 0, &dummy) == |
| 468 | RCODE_COMPLETE) |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 469 | break; |
| 470 | device->max_speed--; |
| 471 | } |
| 472 | } |
| 473 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 474 | /* |
| 475 | * Now parse the config rom. The config rom is a recursive |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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 |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 479 | * start things off. |
| 480 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 481 | length = i; |
| 482 | sp = 0; |
| 483 | stack[sp++] = 0xc0000005; |
| 484 | while (sp > 0) { |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 485 | /* |
| 486 | * Pop the next block reference of the stack. The |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 487 | * lower 24 bits is the offset into the config rom, |
| 488 | * the upper 8 bits are the type of the reference the |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 489 | * block. |
| 490 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 491 | key = stack[--sp]; |
| 492 | i = key & 0xffffff; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 493 | if (i >= READ_BIB_ROM_SIZE) |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 494 | /* |
| 495 | * The reference points outside the standard |
| 496 | * config rom area, something's fishy. |
| 497 | */ |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 498 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 499 | |
| 500 | /* Read header quadlet for the block to get the length. */ |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 501 | if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 502 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 503 | end = i + (rom[i] >> 16) + 1; |
| 504 | i++; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 505 | if (end > READ_BIB_ROM_SIZE) |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 506 | /* |
| 507 | * This block extends outside standard config |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 508 | * area (and the array we're reading it |
| 509 | * into). That's broken, so ignore this |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 510 | * device. |
| 511 | */ |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 512 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 513 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 514 | /* |
| 515 | * Now read in the block. If this is a directory |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 516 | * block, check the entries as we read them to see if |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 517 | * it references another block, and push it in that case. |
| 518 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 519 | while (i < end) { |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 520 | if (read_rom(device, generation, i, &rom[i]) != |
| 521 | RCODE_COMPLETE) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 522 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 523 | if ((key >> 30) == 3 && (rom[i] >> 30) > 1 && |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 524 | sp < READ_BIB_STACK_SIZE) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 525 | stack[sp++] = i + rom[i]; |
| 526 | i++; |
| 527 | } |
| 528 | if (length < i) |
| 529 | length = i; |
| 530 | } |
| 531 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 532 | old_rom = device->config_rom; |
| 533 | new_rom = kmemdup(rom, length * 4, GFP_KERNEL); |
| 534 | if (new_rom == NULL) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 535 | goto out; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 536 | |
| 537 | down_write(&fw_device_rwsem); |
| 538 | device->config_rom = new_rom; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 539 | device->config_rom_length = length; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 540 | up_write(&fw_device_rwsem); |
| 541 | |
| 542 | kfree(old_rom); |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 543 | ret = 0; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 544 | device->cmc = rom[2] & 1 << 30; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 545 | out: |
| 546 | kfree(rom); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 547 | |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 548 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static void fw_unit_release(struct device *dev) |
| 552 | { |
| 553 | struct fw_unit *unit = fw_unit(dev); |
| 554 | |
| 555 | kfree(unit); |
| 556 | } |
| 557 | |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 558 | static struct device_type fw_unit_type = { |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 559 | .uevent = fw_unit_uevent, |
| 560 | .release = fw_unit_release, |
| 561 | }; |
| 562 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 563 | static int is_fw_unit(struct device *dev) |
| 564 | { |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 565 | return dev->type == &fw_unit_type; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void create_units(struct fw_device *device) |
| 569 | { |
| 570 | struct fw_csr_iterator ci; |
| 571 | struct fw_unit *unit; |
| 572 | int key, value, i; |
| 573 | |
| 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; |
| 579 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 580 | /* |
| 581 | * Get the address of the unit directory and try to |
| 582 | * match the drivers id_tables against it. |
| 583 | */ |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 584 | unit = kzalloc(sizeof(*unit), GFP_KERNEL); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 585 | if (unit == NULL) { |
| 586 | fw_error("failed to allocate memory for unit\n"); |
| 587 | continue; |
| 588 | } |
| 589 | |
| 590 | unit->directory = ci.p + value - 1; |
| 591 | unit->device.bus = &fw_bus_type; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 592 | unit->device.type = &fw_unit_type; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 593 | unit->device.parent = &device->device; |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 594 | dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 595 | |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 596 | init_fw_attribute_group(&unit->device, |
| 597 | fw_unit_attributes, |
| 598 | &unit->attribute_group); |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 599 | if (device_register(&unit->device) < 0) |
| 600 | goto skip_unit; |
| 601 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 602 | continue; |
| 603 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 604 | skip_unit: |
| 605 | kfree(unit); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | static int shutdown_unit(struct device *device, void *data) |
| 610 | { |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 611 | device_unregister(device); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 616 | /* |
| 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 |
| 621 | */ |
| 622 | DECLARE_RWSEM(fw_device_rwsem); |
| 623 | |
Stefan Richter | d6053e0 | 2008-11-24 20:40:00 +0100 | [diff] [blame] | 624 | DEFINE_IDR(fw_device_idr); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 625 | int fw_cdev_major; |
| 626 | |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 627 | struct fw_device *fw_device_get_by_devt(dev_t devt) |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 628 | { |
| 629 | struct fw_device *device; |
| 630 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 631 | down_read(&fw_device_rwsem); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 632 | device = idr_find(&fw_device_idr, MINOR(devt)); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 633 | if (device) |
| 634 | fw_device_get(device); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 635 | up_read(&fw_device_rwsem); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 636 | |
| 637 | return device; |
| 638 | } |
| 639 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 640 | /* |
| 641 | * These defines control the retry behavior for reading the config |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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 |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 648 | * are plugged in, they're all getting read within one second. |
| 649 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 650 | |
Kristian Høgsberg | c5dfd0a | 2007-03-27 01:43:43 -0400 | [diff] [blame] | 651 | #define MAX_RETRIES 10 |
| 652 | #define RETRY_DELAY (3 * HZ) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 653 | #define INITIAL_DELAY (HZ / 2) |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 654 | #define SHUTDOWN_DELAY (2 * HZ) |
| 655 | |
| 656 | static void fw_device_shutdown(struct work_struct *work) |
| 657 | { |
| 658 | struct fw_device *device = |
| 659 | container_of(work, struct fw_device, work.work); |
| 660 | int minor = MINOR(device->device.devt); |
| 661 | |
Stefan Richter | e747a5c | 2009-01-24 20:35:38 +0100 | [diff] [blame] | 662 | if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY) |
| 663 | && !list_empty(&device->card->link)) { |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 664 | schedule_delayed_work(&device->work, SHUTDOWN_DELAY); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | if (atomic_cmpxchg(&device->state, |
| 669 | FW_DEVICE_GONE, |
| 670 | FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE) |
| 671 | return; |
| 672 | |
| 673 | fw_device_cdev_remove(device); |
| 674 | device_for_each_child(&device->device, NULL, shutdown_unit); |
| 675 | device_unregister(&device->device); |
| 676 | |
| 677 | down_write(&fw_device_rwsem); |
| 678 | idr_remove(&fw_device_idr, minor); |
| 679 | up_write(&fw_device_rwsem); |
| 680 | |
| 681 | fw_device_put(device); |
| 682 | } |
| 683 | |
| 684 | static struct device_type fw_device_type = { |
| 685 | .release = fw_device_release, |
| 686 | }; |
| 687 | |
| 688 | static void fw_device_update(struct work_struct *work); |
| 689 | |
| 690 | /* |
| 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. |
| 695 | */ |
| 696 | static int lookup_existing_device(struct device *dev, void *data) |
| 697 | { |
| 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; |
| 702 | |
| 703 | down_read(&fw_device_rwsem); /* serialize config_rom access */ |
| 704 | spin_lock_irq(&card->lock); /* serialize node access */ |
| 705 | |
| 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; |
| 712 | |
| 713 | new->node = obsolete_node; |
| 714 | new->node->data = new; |
| 715 | old->node = current_node; |
| 716 | old->node->data = old; |
| 717 | |
| 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)); |
| 724 | |
| 725 | PREPARE_DELAYED_WORK(&old->work, fw_device_update); |
| 726 | schedule_delayed_work(&old->work, 0); |
| 727 | |
| 728 | if (current_node == card->root_node) |
| 729 | fw_schedule_bm_work(card, 0); |
| 730 | |
| 731 | match = 1; |
| 732 | } |
| 733 | |
| 734 | spin_unlock_irq(&card->lock); |
| 735 | up_read(&fw_device_rwsem); |
| 736 | |
| 737 | return match; |
| 738 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 739 | |
| 740 | static void fw_device_init(struct work_struct *work) |
| 741 | { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 742 | struct fw_device *device = |
| 743 | container_of(work, struct fw_device, work.work); |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 744 | struct device *revived_dev; |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 745 | int minor, err; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 746 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 747 | /* |
| 748 | * All failure paths here set node->data to NULL, so that we |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 749 | * don't try to do device_for_each_child() on a kfree()'d |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 750 | * device. |
| 751 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 752 | |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 753 | if (read_bus_info_block(device, device->generation) < 0) { |
Stefan Richter | 855c603 | 2008-02-27 22:14:27 +0100 | [diff] [blame] | 754 | if (device->config_rom_retries < MAX_RETRIES && |
| 755 | atomic_read(&device->state) == FW_DEVICE_INITIALIZING) { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 756 | device->config_rom_retries++; |
| 757 | schedule_delayed_work(&device->work, RETRY_DELAY); |
| 758 | } else { |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 759 | fw_notify("giving up on config rom for node id %x\n", |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 760 | device->node_id); |
Kristian Høgsberg | 931c483 | 2007-01-26 00:38:45 -0500 | [diff] [blame] | 761 | if (device->node == device->card->root_node) |
Jay Fenlason | 0fa1986f | 2008-11-29 17:44:57 +0100 | [diff] [blame] | 762 | fw_schedule_bm_work(device->card, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 763 | fw_device_release(&device->device); |
| 764 | } |
| 765 | return; |
| 766 | } |
| 767 | |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 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); |
| 773 | |
| 774 | return; |
| 775 | } |
| 776 | |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 777 | device_initialize(&device->device); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 778 | |
| 779 | fw_device_get(device); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 780 | down_write(&fw_device_rwsem); |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 781 | err = idr_pre_get(&fw_device_idr, GFP_KERNEL) ? |
| 782 | idr_get_new(&fw_device_idr, device, &minor) : |
| 783 | -ENOMEM; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 784 | up_write(&fw_device_rwsem); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 785 | |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 786 | if (err < 0) |
| 787 | goto error; |
| 788 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 789 | device->device.bus = &fw_bus_type; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 790 | device->device.type = &fw_device_type; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 791 | device->device.parent = device->card->device; |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 792 | device->device.devt = MKDEV(fw_cdev_major, minor); |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 793 | dev_set_name(&device->device, "fw%d", minor); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 794 | |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 795 | init_fw_attribute_group(&device->device, |
| 796 | fw_device_attributes, |
| 797 | &device->attribute_group); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 798 | if (device_add(&device->device)) { |
| 799 | fw_error("Failed to add device.\n"); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 800 | goto error_with_cdev; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 801 | } |
| 802 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 803 | create_units(device); |
| 804 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 805 | /* |
| 806 | * Transition the device to running state. If it got pulled |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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 |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 812 | * fw_node_event(). |
| 813 | */ |
Stefan Richter | 641f879 | 2007-01-27 10:34:55 +0100 | [diff] [blame] | 814 | if (atomic_cmpxchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 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); |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 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", |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 823 | dev_name(&device->device), |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 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", |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 829 | dev_name(&device->device), |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 830 | device->config_rom[3], device->config_rom[4], |
| 831 | 1 << device->max_speed); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 832 | device->config_rom_retries = 0; |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 833 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 834 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 835 | /* |
| 836 | * Reschedule the IRM work if we just finished reading the |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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 - |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 839 | * pretty harmless. |
| 840 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 841 | if (device->node == device->card->root_node) |
Jay Fenlason | 0fa1986f | 2008-11-29 17:44:57 +0100 | [diff] [blame] | 842 | fw_schedule_bm_work(device->card, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 843 | |
| 844 | return; |
| 845 | |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 846 | error_with_cdev: |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 847 | down_write(&fw_device_rwsem); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 848 | idr_remove(&fw_device_idr, minor); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 849 | up_write(&fw_device_rwsem); |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 850 | error: |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 851 | fw_device_put(device); /* fw_device_idr's reference */ |
| 852 | |
| 853 | put_device(&device->device); /* our reference */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static int update_unit(struct device *dev, void *data) |
| 857 | { |
| 858 | struct fw_unit *unit = fw_unit(dev); |
| 859 | struct fw_driver *driver = (struct fw_driver *)dev->driver; |
| 860 | |
Kristian Høgsberg | 015b066 | 2007-03-19 11:37:16 -0400 | [diff] [blame] | 861 | if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) { |
| 862 | down(&dev->sem); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 863 | driver->update(unit); |
Kristian Høgsberg | 015b066 | 2007-03-19 11:37:16 -0400 | [diff] [blame] | 864 | up(&dev->sem); |
| 865 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
Kristian Høgsberg | 5f48047 | 2007-03-07 12:12:39 -0500 | [diff] [blame] | 870 | static void fw_device_update(struct work_struct *work) |
| 871 | { |
| 872 | struct fw_device *device = |
| 873 | container_of(work, struct fw_device, work.work); |
| 874 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 875 | fw_device_cdev_update(device); |
Kristian Høgsberg | 5f48047 | 2007-03-07 12:12:39 -0500 | [diff] [blame] | 876 | device_for_each_child(&device->device, NULL, update_unit); |
| 877 | } |
| 878 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 879 | enum { |
| 880 | REREAD_BIB_ERROR, |
| 881 | REREAD_BIB_GONE, |
| 882 | REREAD_BIB_UNCHANGED, |
| 883 | REREAD_BIB_CHANGED, |
| 884 | }; |
| 885 | |
| 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) |
| 888 | { |
| 889 | u32 q; |
| 890 | int i; |
| 891 | |
| 892 | for (i = 0; i < 6; i++) { |
| 893 | if (read_rom(device, generation, i, &q) != RCODE_COMPLETE) |
| 894 | return REREAD_BIB_ERROR; |
| 895 | |
| 896 | if (i == 0 && q == 0) |
| 897 | return REREAD_BIB_GONE; |
| 898 | |
| 899 | if (i > device->config_rom_length || q != device->config_rom[i]) |
| 900 | return REREAD_BIB_CHANGED; |
| 901 | } |
| 902 | |
| 903 | return REREAD_BIB_UNCHANGED; |
| 904 | } |
| 905 | |
| 906 | static void fw_device_refresh(struct work_struct *work) |
| 907 | { |
| 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; |
| 912 | |
| 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); |
| 919 | |
| 920 | return; |
| 921 | } |
| 922 | goto give_up; |
| 923 | |
| 924 | case REREAD_BIB_GONE: |
| 925 | goto gone; |
| 926 | |
| 927 | case REREAD_BIB_UNCHANGED: |
| 928 | if (atomic_cmpxchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 929 | FW_DEVICE_INITIALIZING, |
| 930 | FW_DEVICE_RUNNING) == FW_DEVICE_GONE) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 931 | goto gone; |
| 932 | |
| 933 | fw_device_update(work); |
| 934 | device->config_rom_retries = 0; |
| 935 | goto out; |
| 936 | |
| 937 | case REREAD_BIB_CHANGED: |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Something changed. We keep things simple and don't investigate |
| 943 | * further. We just destroy all previous units and create new ones. |
| 944 | */ |
| 945 | device_for_each_child(&device->device, NULL, shutdown_unit); |
| 946 | |
| 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); |
| 952 | |
| 953 | return; |
| 954 | } |
| 955 | goto give_up; |
| 956 | } |
| 957 | |
| 958 | create_units(device); |
| 959 | |
| 960 | if (atomic_cmpxchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 961 | FW_DEVICE_INITIALIZING, |
| 962 | FW_DEVICE_RUNNING) == FW_DEVICE_GONE) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 963 | goto gone; |
| 964 | |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 965 | fw_notify("refreshed device %s\n", dev_name(&device->device)); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 966 | device->config_rom_retries = 0; |
| 967 | goto out; |
| 968 | |
| 969 | give_up: |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 970 | fw_notify("giving up on refresh of device %s\n", dev_name(&device->device)); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 971 | gone: |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 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); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 975 | out: |
| 976 | if (node_id == card->root_node->node_id) |
Jay Fenlason | 0fa1986f | 2008-11-29 17:44:57 +0100 | [diff] [blame] | 977 | fw_schedule_bm_work(card, 0); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 980 | void fw_node_event(struct fw_card *card, struct fw_node *node, int event) |
| 981 | { |
| 982 | struct fw_device *device; |
| 983 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 984 | switch (event) { |
| 985 | case FW_NODE_CREATED: |
| 986 | case FW_NODE_LINK_ON: |
| 987 | if (!node->link_on) |
| 988 | break; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 989 | create: |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 990 | device = kzalloc(sizeof(*device), GFP_ATOMIC); |
| 991 | if (device == NULL) |
| 992 | break; |
| 993 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 994 | /* |
| 995 | * Do minimal intialization of the device here, the |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 996 | * rest will happen in fw_device_init(). |
| 997 | * |
| 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. |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1003 | */ |
Stefan Richter | 641f879 | 2007-01-27 10:34:55 +0100 | [diff] [blame] | 1004 | atomic_set(&device->state, FW_DEVICE_INITIALIZING); |
Stefan Richter | 459f792 | 2008-05-24 16:50:22 +0200 | [diff] [blame] | 1005 | device->card = fw_card_get(card); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1006 | device->node = fw_node_get(node); |
| 1007 | device->node_id = node->node_id; |
| 1008 | device->generation = card->generation; |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame^] | 1009 | mutex_init(&device->client_list_mutex); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 1010 | INIT_LIST_HEAD(&device->client_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1011 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1012 | /* |
| 1013 | * Set the node data to point back to this device so |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1014 | * FW_NODE_UPDATED callbacks can update the node_id |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1015 | * and generation for the device. |
| 1016 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1017 | node->data = device; |
| 1018 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1019 | /* |
| 1020 | * Many devices are slow to respond after bus resets, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1021 | * especially if they are bus powered and go through |
| 1022 | * power-up after getting plugged in. We schedule the |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1023 | * first config rom scan half a second after bus reset. |
| 1024 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1025 | INIT_DELAYED_WORK(&device->work, fw_device_init); |
| 1026 | schedule_delayed_work(&device->work, INITIAL_DELAY); |
| 1027 | break; |
| 1028 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1029 | case FW_NODE_INITIATED_RESET: |
| 1030 | device = node->data; |
| 1031 | if (device == NULL) |
| 1032 | goto create; |
| 1033 | |
| 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); |
| 1043 | } |
| 1044 | break; |
| 1045 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1046 | case FW_NODE_UPDATED: |
| 1047 | if (!node->link_on || node->data == NULL) |
| 1048 | break; |
| 1049 | |
| 1050 | device = node->data; |
| 1051 | device->node_id = node->node_id; |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 1052 | smp_wmb(); /* update node_id before generation */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1053 | device->generation = card->generation; |
Kristian Høgsberg | 5f48047 | 2007-03-07 12:12:39 -0500 | [diff] [blame] | 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); |
| 1057 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1058 | break; |
| 1059 | |
| 1060 | case FW_NODE_DESTROYED: |
| 1061 | case FW_NODE_LINK_OFF: |
| 1062 | if (!node->data) |
| 1063 | break; |
| 1064 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1065 | /* |
| 1066 | * Destroy the device associated with the node. There |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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 |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1075 | * to create the device. |
| 1076 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1077 | device = node->data; |
Stefan Richter | 641f879 | 2007-01-27 10:34:55 +0100 | [diff] [blame] | 1078 | if (atomic_xchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 1079 | FW_DEVICE_GONE) == FW_DEVICE_RUNNING) { |
Kristian Høgsberg | 5f48047 | 2007-03-07 12:12:39 -0500 | [diff] [blame] | 1080 | PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown); |
Stefan Richter | e747a5c | 2009-01-24 20:35:38 +0100 | [diff] [blame] | 1081 | schedule_delayed_work(&device->work, |
| 1082 | list_empty(&card->link) ? 0 : SHUTDOWN_DELAY); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1083 | } |
| 1084 | break; |
| 1085 | } |
| 1086 | } |