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