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