Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1 | /* -*- c-basic-offset: 8 -*- |
| 2 | * |
| 3 | * fw-device.c - Device probing and sysfs code. |
| 4 | * |
| 5 | * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software Foundation, |
| 19 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/wait.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/kthread.h> |
| 26 | #include <linux/device.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include "fw-transaction.h" |
| 29 | #include "fw-topology.h" |
| 30 | #include "fw-device.h" |
| 31 | |
| 32 | void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p) |
| 33 | { |
| 34 | ci->p = p + 1; |
| 35 | ci->end = ci->p + (p[0] >> 16); |
| 36 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 37 | EXPORT_SYMBOL(fw_csr_iterator_init); |
| 38 | |
| 39 | int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value) |
| 40 | { |
| 41 | *key = *ci->p >> 24; |
| 42 | *value = *ci->p & 0xffffff; |
| 43 | |
| 44 | return ci->p++ < ci->end; |
| 45 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 46 | EXPORT_SYMBOL(fw_csr_iterator_next); |
| 47 | |
| 48 | static int is_fw_unit(struct device *dev); |
| 49 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 50 | 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] | 51 | { |
| 52 | struct fw_csr_iterator ci; |
| 53 | int key, value, match; |
| 54 | |
| 55 | match = 0; |
| 56 | fw_csr_iterator_init(&ci, directory); |
| 57 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 58 | if (key == CSR_VENDOR && value == id->vendor) |
| 59 | match |= FW_MATCH_VENDOR; |
| 60 | if (key == CSR_MODEL && value == id->model) |
| 61 | match |= FW_MATCH_MODEL; |
| 62 | if (key == CSR_SPECIFIER_ID && value == id->specifier_id) |
| 63 | match |= FW_MATCH_SPECIFIER_ID; |
| 64 | if (key == CSR_VERSION && value == id->version) |
| 65 | match |= FW_MATCH_VERSION; |
| 66 | } |
| 67 | |
| 68 | return (match & id->match_flags) == id->match_flags; |
| 69 | } |
| 70 | |
| 71 | static int fw_unit_match(struct device *dev, struct device_driver *drv) |
| 72 | { |
| 73 | struct fw_unit *unit = fw_unit(dev); |
| 74 | struct fw_driver *driver = fw_driver(drv); |
| 75 | int i; |
| 76 | |
| 77 | /* We only allow binding to fw_units. */ |
| 78 | if (!is_fw_unit(dev)) |
| 79 | return 0; |
| 80 | |
| 81 | for (i = 0; driver->id_table[i].match_flags != 0; i++) { |
| 82 | if (match_unit_directory(unit->directory, &driver->id_table[i])) |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size) |
| 90 | { |
| 91 | struct fw_device *device = fw_device(unit->device.parent); |
| 92 | struct fw_csr_iterator ci; |
| 93 | |
| 94 | int key, value; |
| 95 | int vendor = 0; |
| 96 | int model = 0; |
| 97 | int specifier_id = 0; |
| 98 | int version = 0; |
| 99 | |
| 100 | fw_csr_iterator_init(&ci, &device->config_rom[5]); |
| 101 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 102 | switch (key) { |
| 103 | case CSR_VENDOR: |
| 104 | vendor = value; |
| 105 | break; |
| 106 | case CSR_MODEL: |
| 107 | model = value; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fw_csr_iterator_init(&ci, unit->directory); |
| 113 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 114 | switch (key) { |
| 115 | case CSR_SPECIFIER_ID: |
| 116 | specifier_id = value; |
| 117 | break; |
| 118 | case CSR_VERSION: |
| 119 | version = value; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return snprintf(buffer, buffer_size, |
| 125 | "ieee1394:ven%08Xmo%08Xsp%08Xver%08X", |
| 126 | vendor, model, specifier_id, version); |
| 127 | } |
| 128 | |
| 129 | static int |
| 130 | fw_unit_uevent(struct device *dev, char **envp, int num_envp, |
| 131 | char *buffer, int buffer_size) |
| 132 | { |
| 133 | struct fw_unit *unit = fw_unit(dev); |
| 134 | char modalias[64]; |
| 135 | int length = 0; |
| 136 | int i = 0; |
| 137 | |
| 138 | if (!is_fw_unit(dev)) |
| 139 | goto out; |
| 140 | |
| 141 | get_modalias(unit, modalias, sizeof modalias); |
| 142 | |
| 143 | if (add_uevent_var(envp, num_envp, &i, |
| 144 | buffer, buffer_size, &length, |
| 145 | "MODALIAS=%s", modalias)) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | out: |
| 149 | envp[i] = NULL; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | struct bus_type fw_bus_type = { |
| 155 | .name = "fw", |
| 156 | .match = fw_unit_match, |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 157 | .uevent = fw_unit_uevent, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 158 | }; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 159 | EXPORT_SYMBOL(fw_bus_type); |
| 160 | |
| 161 | extern struct fw_device *fw_device_get(struct fw_device *device) |
| 162 | { |
| 163 | get_device(&device->device); |
| 164 | |
| 165 | return device; |
| 166 | } |
| 167 | |
| 168 | extern void fw_device_put(struct fw_device *device) |
| 169 | { |
| 170 | put_device(&device->device); |
| 171 | } |
| 172 | |
| 173 | static void fw_device_release(struct device *dev) |
| 174 | { |
| 175 | struct fw_device *device = fw_device(dev); |
| 176 | unsigned long flags; |
| 177 | |
| 178 | /* Take the card lock so we don't set this to NULL while a |
| 179 | * FW_NODE_UPDATED callback is being handled. */ |
| 180 | spin_lock_irqsave(&device->card->lock, flags); |
| 181 | device->node->data = NULL; |
| 182 | spin_unlock_irqrestore(&device->card->lock, flags); |
| 183 | |
| 184 | fw_node_put(device->node); |
| 185 | fw_card_put(device->card); |
| 186 | kfree(device->config_rom); |
| 187 | kfree(device); |
| 188 | } |
| 189 | |
| 190 | int fw_device_enable_phys_dma(struct fw_device *device) |
| 191 | { |
| 192 | return device->card->driver->enable_phys_dma(device->card, |
| 193 | device->node_id, |
| 194 | device->generation); |
| 195 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 196 | EXPORT_SYMBOL(fw_device_enable_phys_dma); |
| 197 | |
| 198 | static ssize_t |
| 199 | show_modalias_attribute(struct device *dev, |
| 200 | struct device_attribute *attr, char *buf) |
| 201 | { |
| 202 | struct fw_unit *unit = fw_unit(dev); |
| 203 | int length; |
| 204 | |
| 205 | length = get_modalias(unit, buf, PAGE_SIZE); |
| 206 | strcpy(buf + length, "\n"); |
| 207 | |
| 208 | return length + 1; |
| 209 | } |
| 210 | |
| 211 | static struct device_attribute modalias_attribute = { |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 212 | .attr = { .name = "modalias", .mode = S_IRUGO, }, |
| 213 | .show = show_modalias_attribute, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | static ssize_t |
| 217 | show_config_rom_attribute(struct device *dev, |
| 218 | struct device_attribute *attr, char *buf) |
| 219 | { |
| 220 | struct fw_device *device = fw_device(dev); |
| 221 | |
| 222 | memcpy(buf, device->config_rom, device->config_rom_length * 4); |
| 223 | |
| 224 | return device->config_rom_length * 4; |
| 225 | } |
| 226 | |
| 227 | static struct device_attribute config_rom_attribute = { |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 228 | .attr = {.name = "config_rom", .mode = S_IRUGO,}, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 229 | .show = show_config_rom_attribute, |
| 230 | }; |
| 231 | |
| 232 | struct read_quadlet_callback_data { |
| 233 | struct completion done; |
| 234 | int rcode; |
| 235 | u32 data; |
| 236 | }; |
| 237 | |
| 238 | static void |
| 239 | complete_transaction(struct fw_card *card, int rcode, |
| 240 | void *payload, size_t length, void *data) |
| 241 | { |
| 242 | struct read_quadlet_callback_data *callback_data = data; |
| 243 | |
| 244 | if (rcode == RCODE_COMPLETE) |
| 245 | callback_data->data = be32_to_cpu(*(__be32 *)payload); |
| 246 | callback_data->rcode = rcode; |
| 247 | complete(&callback_data->done); |
| 248 | } |
| 249 | |
| 250 | static int read_rom(struct fw_device *device, int index, u32 * data) |
| 251 | { |
| 252 | struct read_quadlet_callback_data callback_data; |
| 253 | struct fw_transaction t; |
| 254 | u64 offset; |
| 255 | |
| 256 | init_completion(&callback_data.done); |
| 257 | |
| 258 | offset = 0xfffff0000400ULL + index * 4; |
| 259 | fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST, |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 260 | device->node_id, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 261 | device->generation, SCODE_100, |
| 262 | offset, NULL, 4, complete_transaction, &callback_data); |
| 263 | |
| 264 | wait_for_completion(&callback_data.done); |
| 265 | |
| 266 | *data = callback_data.data; |
| 267 | |
| 268 | return callback_data.rcode; |
| 269 | } |
| 270 | |
| 271 | static int read_bus_info_block(struct fw_device *device) |
| 272 | { |
| 273 | static u32 rom[256]; |
| 274 | u32 stack[16], sp, key; |
| 275 | int i, end, length; |
| 276 | |
| 277 | /* First read the bus info block. */ |
| 278 | for (i = 0; i < 5; i++) { |
| 279 | if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE) |
| 280 | return -1; |
| 281 | /* As per IEEE1212 7.2, during power-up, devices can |
| 282 | * reply with a 0 for the first quadlet of the config |
| 283 | * rom to indicate that they are booting (for example, |
| 284 | * if the firmware is on the disk of a external |
| 285 | * harddisk). In that case we just fail, and the |
| 286 | * retry mechanism will try again later. */ |
| 287 | if (i == 0 && rom[i] == 0) |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | /* Now parse the config rom. The config rom is a recursive |
| 292 | * directory structure so we parse it using a stack of |
| 293 | * references to the blocks that make up the structure. We |
| 294 | * push a reference to the root directory on the stack to |
| 295 | * start things off. */ |
| 296 | length = i; |
| 297 | sp = 0; |
| 298 | stack[sp++] = 0xc0000005; |
| 299 | while (sp > 0) { |
| 300 | /* Pop the next block reference of the stack. The |
| 301 | * lower 24 bits is the offset into the config rom, |
| 302 | * the upper 8 bits are the type of the reference the |
| 303 | * block. */ |
| 304 | key = stack[--sp]; |
| 305 | i = key & 0xffffff; |
| 306 | if (i >= ARRAY_SIZE(rom)) |
| 307 | /* The reference points outside the standard |
| 308 | * config rom area, something's fishy. */ |
| 309 | return -1; |
| 310 | |
| 311 | /* Read header quadlet for the block to get the length. */ |
| 312 | if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE) |
| 313 | return -1; |
| 314 | end = i + (rom[i] >> 16) + 1; |
| 315 | i++; |
| 316 | if (end > ARRAY_SIZE(rom)) |
| 317 | /* This block extends outside standard config |
| 318 | * area (and the array we're reading it |
| 319 | * into). That's broken, so ignore this |
| 320 | * device. */ |
| 321 | return -1; |
| 322 | |
| 323 | /* Now read in the block. If this is a directory |
| 324 | * block, check the entries as we read them to see if |
| 325 | * it references another block, and push it in that case. */ |
| 326 | while (i < end) { |
| 327 | if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE) |
| 328 | return -1; |
| 329 | if ((key >> 30) == 3 && (rom[i] >> 30) > 1 && |
| 330 | sp < ARRAY_SIZE(stack)) |
| 331 | stack[sp++] = i + rom[i]; |
| 332 | i++; |
| 333 | } |
| 334 | if (length < i) |
| 335 | length = i; |
| 336 | } |
| 337 | |
| 338 | device->config_rom = kmalloc(length * 4, GFP_KERNEL); |
| 339 | if (device->config_rom == NULL) |
| 340 | return -1; |
| 341 | memcpy(device->config_rom, rom, length * 4); |
| 342 | device->config_rom_length = length; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static void fw_unit_release(struct device *dev) |
| 348 | { |
| 349 | struct fw_unit *unit = fw_unit(dev); |
| 350 | |
| 351 | kfree(unit); |
| 352 | } |
| 353 | |
| 354 | static int is_fw_unit(struct device *dev) |
| 355 | { |
| 356 | return dev->release == fw_unit_release; |
| 357 | } |
| 358 | |
| 359 | static void create_units(struct fw_device *device) |
| 360 | { |
| 361 | struct fw_csr_iterator ci; |
| 362 | struct fw_unit *unit; |
| 363 | int key, value, i; |
| 364 | |
| 365 | i = 0; |
| 366 | fw_csr_iterator_init(&ci, &device->config_rom[5]); |
| 367 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 368 | if (key != (CSR_UNIT | CSR_DIRECTORY)) |
| 369 | continue; |
| 370 | |
| 371 | /* Get the address of the unit directory and try to |
| 372 | * match the drivers id_tables against it. */ |
| 373 | unit = kzalloc(sizeof *unit, GFP_KERNEL); |
| 374 | if (unit == NULL) { |
| 375 | fw_error("failed to allocate memory for unit\n"); |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | unit->directory = ci.p + value - 1; |
| 380 | unit->device.bus = &fw_bus_type; |
| 381 | unit->device.release = fw_unit_release; |
| 382 | unit->device.parent = &device->device; |
| 383 | snprintf(unit->device.bus_id, sizeof unit->device.bus_id, |
| 384 | "%s.%d", device->device.bus_id, i++); |
| 385 | |
| 386 | if (device_register(&unit->device) < 0) { |
| 387 | kfree(unit); |
| 388 | continue; |
| 389 | } |
| 390 | |
| 391 | if (device_create_file(&unit->device, &modalias_attribute) < 0) { |
| 392 | device_unregister(&unit->device); |
| 393 | kfree(unit); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static int shutdown_unit(struct device *device, void *data) |
| 399 | { |
| 400 | struct fw_unit *unit = fw_unit(device); |
| 401 | |
| 402 | if (is_fw_unit(device)) { |
| 403 | device_remove_file(&unit->device, &modalias_attribute); |
| 404 | device_unregister(&unit->device); |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static void fw_device_shutdown(struct work_struct *work) |
| 411 | { |
| 412 | struct fw_device *device = |
| 413 | container_of(work, struct fw_device, work.work); |
| 414 | |
| 415 | device_remove_file(&device->device, &config_rom_attribute); |
| 416 | cdev_del(&device->cdev); |
| 417 | unregister_chrdev_region(device->device.devt, 1); |
| 418 | device_for_each_child(&device->device, NULL, shutdown_unit); |
| 419 | device_unregister(&device->device); |
| 420 | } |
| 421 | |
| 422 | /* These defines control the retry behavior for reading the config |
| 423 | * rom. It shouldn't be necessary to tweak these; if the device |
| 424 | * doesn't respond to a config rom read within 10 seconds, it's not |
| 425 | * going to respond at all. As for the initial delay, a lot of |
| 426 | * devices will be able to respond within half a second after bus |
| 427 | * reset. On the other hand, it's not really worth being more |
| 428 | * aggressive than that, since it scales pretty well; if 10 devices |
| 429 | * are plugged in, they're all getting read within one second. */ |
| 430 | |
| 431 | #define MAX_RETRIES 5 |
| 432 | #define RETRY_DELAY (2 * HZ) |
| 433 | #define INITIAL_DELAY (HZ / 2) |
| 434 | |
| 435 | static void fw_device_init(struct work_struct *work) |
| 436 | { |
| 437 | static int serial; |
| 438 | struct fw_device *device = |
| 439 | container_of(work, struct fw_device, work.work); |
| 440 | |
| 441 | /* All failure paths here set node->data to NULL, so that we |
| 442 | * don't try to do device_for_each_child() on a kfree()'d |
| 443 | * device. */ |
| 444 | |
| 445 | if (read_bus_info_block(device) < 0) { |
| 446 | if (device->config_rom_retries < MAX_RETRIES) { |
| 447 | device->config_rom_retries++; |
| 448 | schedule_delayed_work(&device->work, RETRY_DELAY); |
| 449 | } else { |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 450 | 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] | 451 | device->node_id); |
Kristian Høgsberg | 931c483 | 2007-01-26 00:38:45 -0500 | [diff] [blame^] | 452 | if (device->node == device->card->root_node) |
| 453 | schedule_delayed_work(&device->card->work, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 454 | fw_device_release(&device->device); |
| 455 | } |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | device->device.bus = &fw_bus_type; |
| 460 | device->device.release = fw_device_release; |
| 461 | device->device.parent = device->card->device; |
| 462 | snprintf(device->device.bus_id, sizeof device->device.bus_id, |
| 463 | "fw%d", serial++); |
| 464 | |
| 465 | if (alloc_chrdev_region(&device->device.devt, 0, 1, "fw")) { |
| 466 | fw_error("Failed to register char device region.\n"); |
| 467 | goto error; |
| 468 | } |
| 469 | |
| 470 | cdev_init(&device->cdev, &fw_device_ops); |
| 471 | device->cdev.owner = THIS_MODULE; |
| 472 | kobject_set_name(&device->cdev.kobj, device->device.bus_id); |
| 473 | if (cdev_add(&device->cdev, device->device.devt, 1)) { |
| 474 | fw_error("Failed to register char device.\n"); |
| 475 | goto error; |
| 476 | } |
| 477 | |
| 478 | if (device_add(&device->device)) { |
| 479 | fw_error("Failed to add device.\n"); |
| 480 | goto error; |
| 481 | } |
| 482 | |
| 483 | if (device_create_file(&device->device, &config_rom_attribute) < 0) { |
| 484 | fw_error("Failed to create config rom file.\n"); |
| 485 | goto error_with_device; |
| 486 | } |
| 487 | |
| 488 | create_units(device); |
| 489 | |
| 490 | /* Transition the device to running state. If it got pulled |
| 491 | * out from under us while we did the intialization work, we |
| 492 | * have to shut down the device again here. Normally, though, |
| 493 | * fw_node_event will be responsible for shutting it down when |
| 494 | * necessary. We have to use the atomic cmpxchg here to avoid |
| 495 | * racing with the FW_NODE_DESTROYED case in |
| 496 | * fw_node_event(). */ |
| 497 | if (cmpxchg(&device->state, |
| 498 | FW_DEVICE_INITIALIZING, |
| 499 | FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN) |
| 500 | fw_device_shutdown(&device->work.work); |
| 501 | else |
| 502 | fw_notify("created new fw device %s (%d config rom retries)\n", |
| 503 | device->device.bus_id, device->config_rom_retries); |
| 504 | |
| 505 | /* Reschedule the IRM work if we just finished reading the |
| 506 | * root node config rom. If this races with a bus reset we |
| 507 | * just end up running the IRM work a couple of extra times - |
| 508 | * pretty harmless. */ |
| 509 | if (device->node == device->card->root_node) |
| 510 | schedule_delayed_work(&device->card->work, 0); |
| 511 | |
| 512 | return; |
| 513 | |
| 514 | error_with_device: |
| 515 | device_del(&device->device); |
| 516 | error: |
| 517 | cdev_del(&device->cdev); |
| 518 | unregister_chrdev_region(device->device.devt, 1); |
| 519 | put_device(&device->device); |
| 520 | } |
| 521 | |
| 522 | static int update_unit(struct device *dev, void *data) |
| 523 | { |
| 524 | struct fw_unit *unit = fw_unit(dev); |
| 525 | struct fw_driver *driver = (struct fw_driver *)dev->driver; |
| 526 | |
| 527 | if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) |
| 528 | driver->update(unit); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | void fw_node_event(struct fw_card *card, struct fw_node *node, int event) |
| 534 | { |
| 535 | struct fw_device *device; |
| 536 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 537 | switch (event) { |
| 538 | case FW_NODE_CREATED: |
| 539 | case FW_NODE_LINK_ON: |
| 540 | if (!node->link_on) |
| 541 | break; |
| 542 | |
| 543 | device = kzalloc(sizeof(*device), GFP_ATOMIC); |
| 544 | if (device == NULL) |
| 545 | break; |
| 546 | |
| 547 | /* Do minimal intialization of the device here, the |
| 548 | * rest will happen in fw_device_init(). We need the |
| 549 | * card and node so we can read the config rom and we |
| 550 | * need to do device_initialize() now so |
| 551 | * device_for_each_child() in FW_NODE_UPDATED is |
| 552 | * doesn't freak out. */ |
| 553 | device_initialize(&device->device); |
| 554 | device->state = FW_DEVICE_INITIALIZING; |
| 555 | device->card = fw_card_get(card); |
| 556 | device->node = fw_node_get(node); |
| 557 | device->node_id = node->node_id; |
| 558 | device->generation = card->generation; |
| 559 | |
| 560 | /* Set the node data to point back to this device so |
| 561 | * FW_NODE_UPDATED callbacks can update the node_id |
| 562 | * and generation for the device. */ |
| 563 | node->data = device; |
| 564 | |
| 565 | /* Many devices are slow to respond after bus resets, |
| 566 | * especially if they are bus powered and go through |
| 567 | * power-up after getting plugged in. We schedule the |
| 568 | * first config rom scan half a second after bus reset. */ |
| 569 | INIT_DELAYED_WORK(&device->work, fw_device_init); |
| 570 | schedule_delayed_work(&device->work, INITIAL_DELAY); |
| 571 | break; |
| 572 | |
| 573 | case FW_NODE_UPDATED: |
| 574 | if (!node->link_on || node->data == NULL) |
| 575 | break; |
| 576 | |
| 577 | device = node->data; |
| 578 | device->node_id = node->node_id; |
| 579 | device->generation = card->generation; |
| 580 | device_for_each_child(&device->device, NULL, update_unit); |
| 581 | break; |
| 582 | |
| 583 | case FW_NODE_DESTROYED: |
| 584 | case FW_NODE_LINK_OFF: |
| 585 | if (!node->data) |
| 586 | break; |
| 587 | |
| 588 | /* Destroy the device associated with the node. There |
| 589 | * are two cases here: either the device is fully |
| 590 | * initialized (FW_DEVICE_RUNNING) or we're in the |
| 591 | * process of reading its config rom |
| 592 | * (FW_DEVICE_INITIALIZING). If it is fully |
| 593 | * initialized we can reuse device->work to schedule a |
| 594 | * full fw_device_shutdown(). If not, there's work |
| 595 | * scheduled to read it's config rom, and we just put |
| 596 | * the device in shutdown state to have that code fail |
| 597 | * to create the device. */ |
| 598 | device = node->data; |
| 599 | if (xchg(&device->state, |
| 600 | FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) { |
| 601 | INIT_DELAYED_WORK(&device->work, fw_device_shutdown); |
| 602 | schedule_delayed_work(&device->work, 0); |
| 603 | } |
| 604 | break; |
| 605 | } |
| 606 | } |