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 | |
Stefan Richter | 41f321c | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 22 | #include <linux/delay.h> |
Stefan Richter | 41f321c | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 23 | #include <linux/device.h> |
| 24 | #include <linux/errno.h> |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 25 | #include <linux/idr.h> |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 26 | #include <linux/jiffies.h> |
Stefan Richter | 41f321c | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 27 | #include <linux/kobject.h> |
| 28 | #include <linux/list.h> |
Stefan Richter | b3b2988 | 2009-02-15 23:12:34 +0100 | [diff] [blame] | 29 | #include <linux/mod_devicetable.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 | 41f321c | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 34 | #include <linux/string.h> |
| 35 | #include <linux/workqueue.h> |
| 36 | |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 37 | #include <asm/system.h> |
Stefan Richter | 41f321c | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 38 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 39 | #include "fw-device.h" |
Stefan Richter | 41f321c | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 40 | #include "fw-topology.h" |
| 41 | #include "fw-transaction.h" |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 42 | |
| 43 | void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p) |
| 44 | { |
| 45 | ci->p = p + 1; |
| 46 | ci->end = ci->p + (p[0] >> 16); |
| 47 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 48 | EXPORT_SYMBOL(fw_csr_iterator_init); |
| 49 | |
| 50 | int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value) |
| 51 | { |
| 52 | *key = *ci->p >> 24; |
| 53 | *value = *ci->p & 0xffffff; |
| 54 | |
| 55 | return ci->p++ < ci->end; |
| 56 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 57 | EXPORT_SYMBOL(fw_csr_iterator_next); |
| 58 | |
| 59 | static int is_fw_unit(struct device *dev); |
| 60 | |
Stefan Richter | e41f8d7 | 2009-02-16 00:22:05 +0100 | [diff] [blame^] | 61 | static int match_unit_directory(u32 *directory, u32 match_flags, |
Stefan Richter | b3b2988 | 2009-02-15 23:12:34 +0100 | [diff] [blame] | 62 | const struct ieee1394_device_id *id) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 63 | { |
| 64 | struct fw_csr_iterator ci; |
| 65 | int key, value, match; |
| 66 | |
| 67 | match = 0; |
| 68 | fw_csr_iterator_init(&ci, directory); |
| 69 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
Stefan Richter | b3b2988 | 2009-02-15 23:12:34 +0100 | [diff] [blame] | 70 | if (key == CSR_VENDOR && value == id->vendor_id) |
| 71 | match |= IEEE1394_MATCH_VENDOR_ID; |
| 72 | if (key == CSR_MODEL && value == id->model_id) |
| 73 | match |= IEEE1394_MATCH_MODEL_ID; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 74 | if (key == CSR_SPECIFIER_ID && value == id->specifier_id) |
Stefan Richter | b3b2988 | 2009-02-15 23:12:34 +0100 | [diff] [blame] | 75 | match |= IEEE1394_MATCH_SPECIFIER_ID; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 76 | if (key == CSR_VERSION && value == id->version) |
Stefan Richter | b3b2988 | 2009-02-15 23:12:34 +0100 | [diff] [blame] | 77 | match |= IEEE1394_MATCH_VERSION; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Stefan Richter | e41f8d7 | 2009-02-16 00:22:05 +0100 | [diff] [blame^] | 80 | return (match & match_flags) == match_flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static int fw_unit_match(struct device *dev, struct device_driver *drv) |
| 84 | { |
| 85 | struct fw_unit *unit = fw_unit(dev); |
Stefan Richter | e41f8d7 | 2009-02-16 00:22:05 +0100 | [diff] [blame^] | 86 | struct fw_device *device; |
| 87 | const struct ieee1394_device_id *id; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 88 | |
| 89 | /* We only allow binding to fw_units. */ |
| 90 | if (!is_fw_unit(dev)) |
| 91 | return 0; |
| 92 | |
Stefan Richter | e41f8d7 | 2009-02-16 00:22:05 +0100 | [diff] [blame^] | 93 | device = fw_device(unit->device.parent); |
| 94 | |
| 95 | for (id = fw_driver(drv)->id_table; id->match_flags != 0; id++) { |
| 96 | if (match_unit_directory(unit->directory, id->match_flags, id)) |
| 97 | return 1; |
| 98 | |
| 99 | /* Also check vendor ID in the root directory. */ |
| 100 | if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) && |
| 101 | match_unit_directory(&device->config_rom[5], |
| 102 | IEEE1394_MATCH_VENDOR_ID, id) && |
| 103 | match_unit_directory(unit->directory, id->match_flags |
| 104 | & ~IEEE1394_MATCH_VENDOR_ID, id)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size) |
| 112 | { |
| 113 | struct fw_device *device = fw_device(unit->device.parent); |
| 114 | struct fw_csr_iterator ci; |
| 115 | |
| 116 | int key, value; |
| 117 | int vendor = 0; |
| 118 | int model = 0; |
| 119 | int specifier_id = 0; |
| 120 | int version = 0; |
| 121 | |
| 122 | fw_csr_iterator_init(&ci, &device->config_rom[5]); |
| 123 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 124 | switch (key) { |
| 125 | case CSR_VENDOR: |
| 126 | vendor = value; |
| 127 | break; |
| 128 | case CSR_MODEL: |
| 129 | model = value; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fw_csr_iterator_init(&ci, unit->directory); |
| 135 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 136 | switch (key) { |
| 137 | case CSR_SPECIFIER_ID: |
| 138 | specifier_id = value; |
| 139 | break; |
| 140 | case CSR_VERSION: |
| 141 | version = value; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return snprintf(buffer, buffer_size, |
| 147 | "ieee1394:ven%08Xmo%08Xsp%08Xver%08X", |
| 148 | vendor, model, specifier_id, version); |
| 149 | } |
| 150 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 151 | static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 152 | { |
| 153 | struct fw_unit *unit = fw_unit(dev); |
| 154 | char modalias[64]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 155 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 156 | get_modalias(unit, modalias, sizeof(modalias)); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 157 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 158 | if (add_uevent_var(env, "MODALIAS=%s", modalias)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 159 | return -ENOMEM; |
| 160 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | struct bus_type fw_bus_type = { |
Kristian Høgsberg | 362c2c8 | 2007-02-06 14:49:37 -0500 | [diff] [blame] | 165 | .name = "firewire", |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 166 | .match = fw_unit_match, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 167 | }; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 168 | EXPORT_SYMBOL(fw_bus_type); |
| 169 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 170 | int fw_device_enable_phys_dma(struct fw_device *device) |
| 171 | { |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 172 | int generation = device->generation; |
| 173 | |
| 174 | /* device->node_id, accessed below, must not be older than generation */ |
| 175 | smp_rmb(); |
| 176 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 177 | return device->card->driver->enable_phys_dma(device->card, |
| 178 | device->node_id, |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 179 | generation); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 180 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 181 | EXPORT_SYMBOL(fw_device_enable_phys_dma); |
| 182 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 183 | struct config_rom_attribute { |
| 184 | struct device_attribute attr; |
| 185 | u32 key; |
| 186 | }; |
| 187 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 188 | static ssize_t show_immediate(struct device *dev, |
| 189 | struct device_attribute *dattr, char *buf) |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 190 | { |
| 191 | struct config_rom_attribute *attr = |
| 192 | container_of(dattr, struct config_rom_attribute, attr); |
| 193 | struct fw_csr_iterator ci; |
| 194 | u32 *dir; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 195 | int key, value, ret = -ENOENT; |
| 196 | |
| 197 | down_read(&fw_device_rwsem); |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 198 | |
| 199 | if (is_fw_unit(dev)) |
| 200 | dir = fw_unit(dev)->directory; |
| 201 | else |
| 202 | dir = fw_device(dev)->config_rom + 5; |
| 203 | |
| 204 | fw_csr_iterator_init(&ci, dir); |
| 205 | while (fw_csr_iterator_next(&ci, &key, &value)) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 206 | if (attr->key == key) { |
| 207 | ret = snprintf(buf, buf ? PAGE_SIZE : 0, |
| 208 | "0x%06x\n", value); |
| 209 | break; |
| 210 | } |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 211 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 212 | up_read(&fw_device_rwsem); |
| 213 | |
| 214 | return ret; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | #define IMMEDIATE_ATTR(name, key) \ |
| 218 | { __ATTR(name, S_IRUGO, show_immediate, NULL), key } |
| 219 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 220 | static ssize_t show_text_leaf(struct device *dev, |
| 221 | struct device_attribute *dattr, char *buf) |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 222 | { |
| 223 | struct config_rom_attribute *attr = |
| 224 | container_of(dattr, struct config_rom_attribute, attr); |
| 225 | struct fw_csr_iterator ci; |
| 226 | u32 *dir, *block = NULL, *p, *end; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 227 | int length, key, value, last_key = 0, ret = -ENOENT; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 228 | char *b; |
| 229 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 230 | down_read(&fw_device_rwsem); |
| 231 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 232 | if (is_fw_unit(dev)) |
| 233 | dir = fw_unit(dev)->directory; |
| 234 | else |
| 235 | dir = fw_device(dev)->config_rom + 5; |
| 236 | |
| 237 | fw_csr_iterator_init(&ci, dir); |
| 238 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 239 | if (attr->key == last_key && |
| 240 | key == (CSR_DESCRIPTOR | CSR_LEAF)) |
| 241 | block = ci.p - 1 + value; |
| 242 | last_key = key; |
| 243 | } |
| 244 | |
| 245 | if (block == NULL) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 246 | goto out; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 247 | |
| 248 | length = min(block[0] >> 16, 256U); |
| 249 | if (length < 3) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 250 | goto out; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 251 | |
| 252 | if (block[1] != 0 || block[2] != 0) |
| 253 | /* Unknown encoding. */ |
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 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 256 | if (buf == NULL) { |
| 257 | ret = length * 4; |
| 258 | goto out; |
| 259 | } |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 260 | |
| 261 | b = buf; |
| 262 | end = &block[length + 1]; |
| 263 | for (p = &block[3]; p < end; p++, b += 4) |
| 264 | * (u32 *) b = (__force u32) __cpu_to_be32(*p); |
| 265 | |
| 266 | /* Strip trailing whitespace and add newline. */ |
| 267 | while (b--, (isspace(*b) || *b == '\0') && b > buf); |
| 268 | strcpy(b + 1, "\n"); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 269 | ret = b + 2 - buf; |
| 270 | out: |
| 271 | up_read(&fw_device_rwsem); |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 272 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 273 | return ret; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | #define TEXT_LEAF_ATTR(name, key) \ |
| 277 | { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key } |
| 278 | |
| 279 | static struct config_rom_attribute config_rom_attributes[] = { |
| 280 | IMMEDIATE_ATTR(vendor, CSR_VENDOR), |
| 281 | IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION), |
| 282 | IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID), |
| 283 | IMMEDIATE_ATTR(version, CSR_VERSION), |
| 284 | IMMEDIATE_ATTR(model, CSR_MODEL), |
| 285 | TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR), |
| 286 | TEXT_LEAF_ATTR(model_name, CSR_MODEL), |
| 287 | TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION), |
| 288 | }; |
| 289 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 290 | static void init_fw_attribute_group(struct device *dev, |
| 291 | struct device_attribute *attrs, |
| 292 | struct fw_attribute_group *group) |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 293 | { |
| 294 | struct device_attribute *attr; |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 295 | int i, j; |
| 296 | |
| 297 | for (j = 0; attrs[j].attr.name != NULL; j++) |
| 298 | group->attrs[j] = &attrs[j].attr; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 299 | |
| 300 | for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) { |
| 301 | attr = &config_rom_attributes[i].attr; |
| 302 | if (attr->show(dev, attr, NULL) < 0) |
| 303 | continue; |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 304 | group->attrs[j++] = &attr->attr; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Stefan Richter | e5333db | 2009-05-22 23:16:27 +0200 | [diff] [blame] | 307 | group->attrs[j] = NULL; |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 308 | group->groups[0] = &group->group; |
| 309 | group->groups[1] = NULL; |
| 310 | group->group.attrs = group->attrs; |
| 311 | dev->groups = group->groups; |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 314 | static ssize_t modalias_show(struct device *dev, |
| 315 | struct device_attribute *attr, char *buf) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 316 | { |
| 317 | struct fw_unit *unit = fw_unit(dev); |
| 318 | int length; |
| 319 | |
| 320 | length = get_modalias(unit, buf, PAGE_SIZE); |
| 321 | strcpy(buf + length, "\n"); |
| 322 | |
| 323 | return length + 1; |
| 324 | } |
| 325 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 326 | static ssize_t rom_index_show(struct device *dev, |
| 327 | struct device_attribute *attr, char *buf) |
Kristian Høgsberg | 048961e | 2007-03-07 12:12:46 -0500 | [diff] [blame] | 328 | { |
| 329 | struct fw_device *device = fw_device(dev->parent); |
| 330 | struct fw_unit *unit = fw_unit(dev); |
| 331 | |
| 332 | return snprintf(buf, PAGE_SIZE, "%d\n", |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 333 | (int)(unit->directory - device->config_rom)); |
Kristian Høgsberg | 048961e | 2007-03-07 12:12:46 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 336 | static struct device_attribute fw_unit_attributes[] = { |
| 337 | __ATTR_RO(modalias), |
| 338 | __ATTR_RO(rom_index), |
| 339 | __ATTR_NULL, |
| 340 | }; |
| 341 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 342 | static ssize_t config_rom_show(struct device *dev, |
| 343 | struct device_attribute *attr, char *buf) |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 344 | { |
| 345 | struct fw_device *device = fw_device(dev); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 346 | size_t length; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 347 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 348 | down_read(&fw_device_rwsem); |
| 349 | length = device->config_rom_length * 4; |
| 350 | memcpy(buf, device->config_rom, length); |
| 351 | up_read(&fw_device_rwsem); |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 352 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 353 | return length; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 356 | static ssize_t guid_show(struct device *dev, |
| 357 | struct device_attribute *attr, char *buf) |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 358 | { |
| 359 | struct fw_device *device = fw_device(dev); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 360 | int ret; |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 361 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 362 | down_read(&fw_device_rwsem); |
| 363 | ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n", |
| 364 | device->config_rom[3], device->config_rom[4]); |
| 365 | up_read(&fw_device_rwsem); |
| 366 | |
| 367 | return ret; |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 368 | } |
| 369 | |
Stefan Richter | 0210b66 | 2009-05-23 00:03:29 +0200 | [diff] [blame] | 370 | static int units_sprintf(char *buf, u32 *directory) |
| 371 | { |
| 372 | struct fw_csr_iterator ci; |
| 373 | int key, value; |
| 374 | int specifier_id = 0; |
| 375 | int version = 0; |
| 376 | |
| 377 | fw_csr_iterator_init(&ci, directory); |
| 378 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 379 | switch (key) { |
| 380 | case CSR_SPECIFIER_ID: |
| 381 | specifier_id = value; |
| 382 | break; |
| 383 | case CSR_VERSION: |
| 384 | version = value; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version); |
| 390 | } |
| 391 | |
| 392 | static ssize_t units_show(struct device *dev, |
| 393 | struct device_attribute *attr, char *buf) |
| 394 | { |
| 395 | struct fw_device *device = fw_device(dev); |
| 396 | struct fw_csr_iterator ci; |
| 397 | int key, value, i = 0; |
| 398 | |
| 399 | down_read(&fw_device_rwsem); |
| 400 | fw_csr_iterator_init(&ci, &device->config_rom[5]); |
| 401 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 402 | if (key != (CSR_UNIT | CSR_DIRECTORY)) |
| 403 | continue; |
| 404 | i += units_sprintf(&buf[i], ci.p + value - 1); |
| 405 | if (i >= PAGE_SIZE - (8 + 1 + 8 + 1)) |
| 406 | break; |
| 407 | } |
| 408 | up_read(&fw_device_rwsem); |
| 409 | |
| 410 | if (i) |
| 411 | buf[i - 1] = '\n'; |
| 412 | |
| 413 | return i; |
| 414 | } |
| 415 | |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 416 | static struct device_attribute fw_device_attributes[] = { |
| 417 | __ATTR_RO(config_rom), |
Kristian Høgsberg | bbd1494 | 2007-03-20 20:58:35 -0400 | [diff] [blame] | 418 | __ATTR_RO(guid), |
Stefan Richter | 0210b66 | 2009-05-23 00:03:29 +0200 | [diff] [blame] | 419 | __ATTR_RO(units), |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 420 | __ATTR_NULL, |
Kristian Høgsberg | 048961e | 2007-03-07 12:12:46 -0500 | [diff] [blame] | 421 | }; |
| 422 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 423 | static int read_rom(struct fw_device *device, |
| 424 | int generation, int index, u32 *data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 425 | { |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 426 | int rcode; |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 427 | |
| 428 | /* device->node_id, accessed below, must not be older than generation */ |
| 429 | smp_rmb(); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 430 | |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 431 | rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST, |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 432 | device->node_id, generation, device->max_speed, |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 433 | (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4, |
| 434 | data, 4); |
| 435 | be32_to_cpus(data); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 436 | |
Jay Fenlason | 1e119fa | 2008-07-20 14:20:53 +0200 | [diff] [blame] | 437 | return rcode; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 438 | } |
| 439 | |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 440 | #define READ_BIB_ROM_SIZE 256 |
| 441 | #define READ_BIB_STACK_SIZE 16 |
| 442 | |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 443 | /* |
| 444 | * Read the bus info block, perform a speed probe, and read all of the rest of |
| 445 | * the config ROM. We do all this with a cached bus generation. If the bus |
| 446 | * generation changes under us, read_bus_info_block will fail and get retried. |
| 447 | * It's better to start all over in this case because the node from which we |
| 448 | * are reading the ROM may have changed the ROM during the reset. |
| 449 | */ |
| 450 | 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] | 451 | { |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 452 | u32 *rom, *stack, *old_rom, *new_rom; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 453 | u32 sp, key; |
| 454 | int i, end, length, ret = -1; |
| 455 | |
| 456 | rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE + |
| 457 | sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL); |
| 458 | if (rom == NULL) |
| 459 | return -ENOMEM; |
| 460 | |
| 461 | stack = &rom[READ_BIB_ROM_SIZE]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 462 | |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 463 | device->max_speed = SCODE_100; |
| 464 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 465 | /* First read the bus info block. */ |
| 466 | for (i = 0; i < 5; i++) { |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 467 | if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 468 | goto out; |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 469 | /* |
| 470 | * As per IEEE1212 7.2, during power-up, devices can |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 471 | * reply with a 0 for the first quadlet of the config |
| 472 | * rom to indicate that they are booting (for example, |
| 473 | * if the firmware is on the disk of a external |
| 474 | * harddisk). In that case we just fail, and the |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 475 | * retry mechanism will try again later. |
| 476 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 477 | if (i == 0 && rom[i] == 0) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 478 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 481 | device->max_speed = device->node->max_speed; |
| 482 | |
| 483 | /* |
| 484 | * Determine the speed of |
| 485 | * - devices with link speed less than PHY speed, |
| 486 | * - devices with 1394b PHY (unless only connected to 1394a PHYs), |
| 487 | * - all devices if there are 1394b repeaters. |
| 488 | * Note, we cannot use the bus info block's link_spd as starting point |
| 489 | * because some buggy firmwares set it lower than necessary and because |
| 490 | * 1394-1995 nodes do not have the field. |
| 491 | */ |
| 492 | if ((rom[2] & 0x7) < device->max_speed || |
| 493 | device->max_speed == SCODE_BETA || |
| 494 | device->card->beta_repeaters_present) { |
| 495 | u32 dummy; |
| 496 | |
| 497 | /* for S1600 and S3200 */ |
| 498 | if (device->max_speed == SCODE_BETA) |
| 499 | device->max_speed = device->card->link_speed; |
| 500 | |
| 501 | while (device->max_speed > SCODE_100) { |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 502 | if (read_rom(device, generation, 0, &dummy) == |
| 503 | RCODE_COMPLETE) |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 504 | break; |
| 505 | device->max_speed--; |
| 506 | } |
| 507 | } |
| 508 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 509 | /* |
| 510 | * Now parse the config rom. The config rom is a recursive |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 511 | * directory structure so we parse it using a stack of |
| 512 | * references to the blocks that make up the structure. We |
| 513 | * push a reference to the root directory on the stack to |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 514 | * start things off. |
| 515 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 516 | length = i; |
| 517 | sp = 0; |
| 518 | stack[sp++] = 0xc0000005; |
| 519 | while (sp > 0) { |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 520 | /* |
| 521 | * Pop the next block reference of the stack. The |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 522 | * lower 24 bits is the offset into the config rom, |
| 523 | * the upper 8 bits are the type of the reference the |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 524 | * block. |
| 525 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 526 | key = stack[--sp]; |
| 527 | i = key & 0xffffff; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 528 | if (i >= READ_BIB_ROM_SIZE) |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 529 | /* |
| 530 | * The reference points outside the standard |
| 531 | * config rom area, something's fishy. |
| 532 | */ |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 533 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 534 | |
| 535 | /* Read header quadlet for the block to get the length. */ |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 536 | if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 537 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 538 | end = i + (rom[i] >> 16) + 1; |
| 539 | i++; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 540 | if (end > READ_BIB_ROM_SIZE) |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 541 | /* |
| 542 | * This block extends outside standard config |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 543 | * area (and the array we're reading it |
| 544 | * into). That's broken, so ignore this |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 545 | * device. |
| 546 | */ |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 547 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 548 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 549 | /* |
| 550 | * Now read in the block. If this is a directory |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 551 | * block, check the entries as we read them to see if |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 552 | * it references another block, and push it in that case. |
| 553 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 554 | while (i < end) { |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 555 | if (read_rom(device, generation, i, &rom[i]) != |
| 556 | RCODE_COMPLETE) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 557 | goto out; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 558 | if ((key >> 30) == 3 && (rom[i] >> 30) > 1 && |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 559 | sp < READ_BIB_STACK_SIZE) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 560 | stack[sp++] = i + rom[i]; |
| 561 | i++; |
| 562 | } |
| 563 | if (length < i) |
| 564 | length = i; |
| 565 | } |
| 566 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 567 | old_rom = device->config_rom; |
| 568 | new_rom = kmemdup(rom, length * 4, GFP_KERNEL); |
| 569 | if (new_rom == NULL) |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 570 | goto out; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 571 | |
| 572 | down_write(&fw_device_rwsem); |
| 573 | device->config_rom = new_rom; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 574 | device->config_rom_length = length; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 575 | up_write(&fw_device_rwsem); |
| 576 | |
| 577 | kfree(old_rom); |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 578 | ret = 0; |
Stefan Richter | 7889b60 | 2009-03-10 21:09:28 +0100 | [diff] [blame] | 579 | device->cmc = rom[2] >> 30 & 1; |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 580 | out: |
| 581 | kfree(rom); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 582 | |
Stefan Richter | 1dadff7 | 2008-03-02 19:35:42 +0100 | [diff] [blame] | 583 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void fw_unit_release(struct device *dev) |
| 587 | { |
| 588 | struct fw_unit *unit = fw_unit(dev); |
| 589 | |
| 590 | kfree(unit); |
| 591 | } |
| 592 | |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 593 | static struct device_type fw_unit_type = { |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 594 | .uevent = fw_unit_uevent, |
| 595 | .release = fw_unit_release, |
| 596 | }; |
| 597 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 598 | static int is_fw_unit(struct device *dev) |
| 599 | { |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 600 | return dev->type == &fw_unit_type; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static void create_units(struct fw_device *device) |
| 604 | { |
| 605 | struct fw_csr_iterator ci; |
| 606 | struct fw_unit *unit; |
| 607 | int key, value, i; |
| 608 | |
| 609 | i = 0; |
| 610 | fw_csr_iterator_init(&ci, &device->config_rom[5]); |
| 611 | while (fw_csr_iterator_next(&ci, &key, &value)) { |
| 612 | if (key != (CSR_UNIT | CSR_DIRECTORY)) |
| 613 | continue; |
| 614 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 615 | /* |
| 616 | * Get the address of the unit directory and try to |
| 617 | * match the drivers id_tables against it. |
| 618 | */ |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 619 | unit = kzalloc(sizeof(*unit), GFP_KERNEL); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 620 | if (unit == NULL) { |
| 621 | fw_error("failed to allocate memory for unit\n"); |
| 622 | continue; |
| 623 | } |
| 624 | |
| 625 | unit->directory = ci.p + value - 1; |
| 626 | unit->device.bus = &fw_bus_type; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 627 | unit->device.type = &fw_unit_type; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 628 | unit->device.parent = &device->device; |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 629 | 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] | 630 | |
Stefan Richter | e5333db | 2009-05-22 23:16:27 +0200 | [diff] [blame] | 631 | BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) < |
| 632 | ARRAY_SIZE(fw_unit_attributes) + |
| 633 | ARRAY_SIZE(config_rom_attributes)); |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 634 | init_fw_attribute_group(&unit->device, |
| 635 | fw_unit_attributes, |
| 636 | &unit->attribute_group); |
Stefan Richter | e5333db | 2009-05-22 23:16:27 +0200 | [diff] [blame] | 637 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 638 | if (device_register(&unit->device) < 0) |
| 639 | goto skip_unit; |
| 640 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 641 | continue; |
| 642 | |
Kristian Høgsberg | 7feb9cc | 2007-03-21 10:55:19 -0400 | [diff] [blame] | 643 | skip_unit: |
| 644 | kfree(unit); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | static int shutdown_unit(struct device *device, void *data) |
| 649 | { |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 650 | device_unregister(device); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 655 | /* |
| 656 | * fw_device_rwsem acts as dual purpose mutex: |
| 657 | * - serializes accesses to fw_device_idr, |
| 658 | * - serializes accesses to fw_device.config_rom/.config_rom_length and |
| 659 | * fw_unit.directory, unless those accesses happen at safe occasions |
| 660 | */ |
| 661 | DECLARE_RWSEM(fw_device_rwsem); |
| 662 | |
Stefan Richter | d6053e0 | 2008-11-24 20:40:00 +0100 | [diff] [blame] | 663 | DEFINE_IDR(fw_device_idr); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 664 | int fw_cdev_major; |
| 665 | |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 666 | struct fw_device *fw_device_get_by_devt(dev_t devt) |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 667 | { |
| 668 | struct fw_device *device; |
| 669 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 670 | down_read(&fw_device_rwsem); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 671 | device = idr_find(&fw_device_idr, MINOR(devt)); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 672 | if (device) |
| 673 | fw_device_get(device); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 674 | up_read(&fw_device_rwsem); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 675 | |
| 676 | return device; |
| 677 | } |
| 678 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 679 | /* |
| 680 | * These defines control the retry behavior for reading the config |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 681 | * rom. It shouldn't be necessary to tweak these; if the device |
| 682 | * doesn't respond to a config rom read within 10 seconds, it's not |
| 683 | * going to respond at all. As for the initial delay, a lot of |
| 684 | * devices will be able to respond within half a second after bus |
| 685 | * reset. On the other hand, it's not really worth being more |
| 686 | * aggressive than that, since it scales pretty well; if 10 devices |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 687 | * are plugged in, they're all getting read within one second. |
| 688 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 689 | |
Kristian Høgsberg | c5dfd0a | 2007-03-27 01:43:43 -0400 | [diff] [blame] | 690 | #define MAX_RETRIES 10 |
| 691 | #define RETRY_DELAY (3 * HZ) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 692 | #define INITIAL_DELAY (HZ / 2) |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 693 | #define SHUTDOWN_DELAY (2 * HZ) |
| 694 | |
| 695 | static void fw_device_shutdown(struct work_struct *work) |
| 696 | { |
| 697 | struct fw_device *device = |
| 698 | container_of(work, struct fw_device, work.work); |
| 699 | int minor = MINOR(device->device.devt); |
| 700 | |
Stefan Richter | e747a5c | 2009-01-24 20:35:38 +0100 | [diff] [blame] | 701 | if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY) |
| 702 | && !list_empty(&device->card->link)) { |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 703 | schedule_delayed_work(&device->work, SHUTDOWN_DELAY); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | if (atomic_cmpxchg(&device->state, |
| 708 | FW_DEVICE_GONE, |
| 709 | FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE) |
| 710 | return; |
| 711 | |
| 712 | fw_device_cdev_remove(device); |
| 713 | device_for_each_child(&device->device, NULL, shutdown_unit); |
| 714 | device_unregister(&device->device); |
| 715 | |
| 716 | down_write(&fw_device_rwsem); |
| 717 | idr_remove(&fw_device_idr, minor); |
| 718 | up_write(&fw_device_rwsem); |
| 719 | |
| 720 | fw_device_put(device); |
| 721 | } |
| 722 | |
Stefan Richter | aed8089 | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 723 | static void fw_device_release(struct device *dev) |
| 724 | { |
| 725 | struct fw_device *device = fw_device(dev); |
| 726 | struct fw_card *card = device->card; |
| 727 | unsigned long flags; |
| 728 | |
| 729 | /* |
| 730 | * Take the card lock so we don't set this to NULL while a |
| 731 | * FW_NODE_UPDATED callback is being handled or while the |
| 732 | * bus manager work looks at this node. |
| 733 | */ |
| 734 | spin_lock_irqsave(&card->lock, flags); |
| 735 | device->node->data = NULL; |
| 736 | spin_unlock_irqrestore(&card->lock, flags); |
| 737 | |
| 738 | fw_node_put(device->node); |
| 739 | kfree(device->config_rom); |
| 740 | kfree(device); |
| 741 | fw_card_put(card); |
| 742 | } |
| 743 | |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 744 | static struct device_type fw_device_type = { |
Stefan Richter | aed8089 | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 745 | .release = fw_device_release, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 746 | }; |
| 747 | |
Stefan Richter | aed8089 | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 748 | static int update_unit(struct device *dev, void *data) |
| 749 | { |
| 750 | struct fw_unit *unit = fw_unit(dev); |
| 751 | struct fw_driver *driver = (struct fw_driver *)dev->driver; |
| 752 | |
| 753 | if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) { |
| 754 | down(&dev->sem); |
| 755 | driver->update(unit); |
| 756 | up(&dev->sem); |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static void fw_device_update(struct work_struct *work) |
| 763 | { |
| 764 | struct fw_device *device = |
| 765 | container_of(work, struct fw_device, work.work); |
| 766 | |
| 767 | fw_device_cdev_update(device); |
| 768 | device_for_each_child(&device->device, NULL, update_unit); |
| 769 | } |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * If a device was pending for deletion because its node went away but its |
| 773 | * bus info block and root directory header matches that of a newly discovered |
| 774 | * device, revive the existing fw_device. |
| 775 | * The newly allocated fw_device becomes obsolete instead. |
| 776 | */ |
| 777 | static int lookup_existing_device(struct device *dev, void *data) |
| 778 | { |
| 779 | struct fw_device *old = fw_device(dev); |
| 780 | struct fw_device *new = data; |
| 781 | struct fw_card *card = new->card; |
| 782 | int match = 0; |
| 783 | |
| 784 | down_read(&fw_device_rwsem); /* serialize config_rom access */ |
| 785 | spin_lock_irq(&card->lock); /* serialize node access */ |
| 786 | |
| 787 | if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 && |
| 788 | atomic_cmpxchg(&old->state, |
| 789 | FW_DEVICE_GONE, |
| 790 | FW_DEVICE_RUNNING) == FW_DEVICE_GONE) { |
| 791 | struct fw_node *current_node = new->node; |
| 792 | struct fw_node *obsolete_node = old->node; |
| 793 | |
| 794 | new->node = obsolete_node; |
| 795 | new->node->data = new; |
| 796 | old->node = current_node; |
| 797 | old->node->data = old; |
| 798 | |
| 799 | old->max_speed = new->max_speed; |
| 800 | old->node_id = current_node->node_id; |
| 801 | smp_wmb(); /* update node_id before generation */ |
| 802 | old->generation = card->generation; |
| 803 | old->config_rom_retries = 0; |
| 804 | fw_notify("rediscovered device %s\n", dev_name(dev)); |
| 805 | |
| 806 | PREPARE_DELAYED_WORK(&old->work, fw_device_update); |
| 807 | schedule_delayed_work(&old->work, 0); |
| 808 | |
| 809 | if (current_node == card->root_node) |
| 810 | fw_schedule_bm_work(card, 0); |
| 811 | |
| 812 | match = 1; |
| 813 | } |
| 814 | |
| 815 | spin_unlock_irq(&card->lock); |
| 816 | up_read(&fw_device_rwsem); |
| 817 | |
| 818 | return match; |
| 819 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 820 | |
Stefan Richter | 7889b60 | 2009-03-10 21:09:28 +0100 | [diff] [blame] | 821 | enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, }; |
| 822 | |
| 823 | void fw_device_set_broadcast_channel(struct fw_device *device, int generation) |
| 824 | { |
| 825 | struct fw_card *card = device->card; |
| 826 | __be32 data; |
| 827 | int rcode; |
| 828 | |
| 829 | if (!card->broadcast_channel_allocated) |
| 830 | return; |
| 831 | |
| 832 | if (device->bc_implemented == BC_UNKNOWN) { |
| 833 | rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST, |
| 834 | device->node_id, generation, device->max_speed, |
| 835 | CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL, |
| 836 | &data, 4); |
| 837 | switch (rcode) { |
| 838 | case RCODE_COMPLETE: |
| 839 | if (data & cpu_to_be32(1 << 31)) { |
| 840 | device->bc_implemented = BC_IMPLEMENTED; |
| 841 | break; |
| 842 | } |
| 843 | /* else fall through to case address error */ |
| 844 | case RCODE_ADDRESS_ERROR: |
| 845 | device->bc_implemented = BC_UNIMPLEMENTED; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if (device->bc_implemented == BC_IMPLEMENTED) { |
| 850 | data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL | |
| 851 | BROADCAST_CHANNEL_VALID); |
| 852 | fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST, |
| 853 | device->node_id, generation, device->max_speed, |
| 854 | CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL, |
| 855 | &data, 4); |
| 856 | } |
| 857 | } |
| 858 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 859 | static void fw_device_init(struct work_struct *work) |
| 860 | { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 861 | struct fw_device *device = |
| 862 | container_of(work, struct fw_device, work.work); |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 863 | struct device *revived_dev; |
Stefan Richter | e1eff7a | 2009-02-03 17:55:19 +0100 | [diff] [blame] | 864 | int minor, ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 865 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 866 | /* |
| 867 | * 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] | 868 | * 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] | 869 | * device. |
| 870 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 871 | |
Stefan Richter | f8d2dc3 | 2008-01-25 17:53:49 +0100 | [diff] [blame] | 872 | if (read_bus_info_block(device, device->generation) < 0) { |
Stefan Richter | 855c603 | 2008-02-27 22:14:27 +0100 | [diff] [blame] | 873 | if (device->config_rom_retries < MAX_RETRIES && |
| 874 | atomic_read(&device->state) == FW_DEVICE_INITIALIZING) { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 875 | device->config_rom_retries++; |
| 876 | schedule_delayed_work(&device->work, RETRY_DELAY); |
| 877 | } else { |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 878 | 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] | 879 | device->node_id); |
Kristian Høgsberg | 931c483 | 2007-01-26 00:38:45 -0500 | [diff] [blame] | 880 | if (device->node == device->card->root_node) |
Jay Fenlason | 0fa1986f | 2008-11-29 17:44:57 +0100 | [diff] [blame] | 881 | fw_schedule_bm_work(device->card, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 882 | fw_device_release(&device->device); |
| 883 | } |
| 884 | return; |
| 885 | } |
| 886 | |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 887 | revived_dev = device_find_child(device->card->device, |
| 888 | device, lookup_existing_device); |
| 889 | if (revived_dev) { |
| 890 | put_device(revived_dev); |
| 891 | fw_device_release(&device->device); |
| 892 | |
| 893 | return; |
| 894 | } |
| 895 | |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 896 | device_initialize(&device->device); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 897 | |
| 898 | fw_device_get(device); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 899 | down_write(&fw_device_rwsem); |
Stefan Richter | e1eff7a | 2009-02-03 17:55:19 +0100 | [diff] [blame] | 900 | ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ? |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 901 | idr_get_new(&fw_device_idr, device, &minor) : |
| 902 | -ENOMEM; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 903 | up_write(&fw_device_rwsem); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 904 | |
Stefan Richter | e1eff7a | 2009-02-03 17:55:19 +0100 | [diff] [blame] | 905 | if (ret < 0) |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 906 | goto error; |
| 907 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 908 | device->device.bus = &fw_bus_type; |
Kristian Høgsberg | 21351db | 2007-03-20 20:58:33 -0400 | [diff] [blame] | 909 | device->device.type = &fw_device_type; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 910 | device->device.parent = device->card->device; |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 911 | device->device.devt = MKDEV(fw_cdev_major, minor); |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 912 | dev_set_name(&device->device, "fw%d", minor); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 913 | |
Stefan Richter | e5333db | 2009-05-22 23:16:27 +0200 | [diff] [blame] | 914 | BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) < |
| 915 | ARRAY_SIZE(fw_device_attributes) + |
| 916 | ARRAY_SIZE(config_rom_attributes)); |
Kristian Høgsberg | 6f2e53d | 2007-03-27 19:35:13 -0400 | [diff] [blame] | 917 | init_fw_attribute_group(&device->device, |
| 918 | fw_device_attributes, |
| 919 | &device->attribute_group); |
Stefan Richter | e5333db | 2009-05-22 23:16:27 +0200 | [diff] [blame] | 920 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 921 | if (device_add(&device->device)) { |
| 922 | fw_error("Failed to add device.\n"); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 923 | goto error_with_cdev; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 924 | } |
| 925 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 926 | create_units(device); |
| 927 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 928 | /* |
| 929 | * Transition the device to running state. If it got pulled |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 930 | * out from under us while we did the intialization work, we |
| 931 | * have to shut down the device again here. Normally, though, |
| 932 | * fw_node_event will be responsible for shutting it down when |
| 933 | * necessary. We have to use the atomic cmpxchg here to avoid |
| 934 | * racing with the FW_NODE_DESTROYED case in |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 935 | * fw_node_event(). |
| 936 | */ |
Stefan Richter | 641f879 | 2007-01-27 10:34:55 +0100 | [diff] [blame] | 937 | if (atomic_cmpxchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 938 | FW_DEVICE_INITIALIZING, |
| 939 | FW_DEVICE_RUNNING) == FW_DEVICE_GONE) { |
| 940 | PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown); |
| 941 | schedule_delayed_work(&device->work, SHUTDOWN_DELAY); |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 942 | } else { |
| 943 | if (device->config_rom_retries) |
| 944 | fw_notify("created device %s: GUID %08x%08x, S%d00, " |
| 945 | "%d config ROM retries\n", |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 946 | dev_name(&device->device), |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 947 | device->config_rom[3], device->config_rom[4], |
| 948 | 1 << device->max_speed, |
| 949 | device->config_rom_retries); |
| 950 | else |
| 951 | fw_notify("created device %s: GUID %08x%08x, S%d00\n", |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 952 | dev_name(&device->device), |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 953 | device->config_rom[3], device->config_rom[4], |
| 954 | 1 << device->max_speed); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 955 | device->config_rom_retries = 0; |
Stefan Richter | 7889b60 | 2009-03-10 21:09:28 +0100 | [diff] [blame] | 956 | |
| 957 | fw_device_set_broadcast_channel(device, device->generation); |
Stefan Richter | fa6e697 | 2008-02-03 23:03:00 +0100 | [diff] [blame] | 958 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 959 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 960 | /* |
| 961 | * Reschedule the IRM work if we just finished reading the |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 962 | * root node config rom. If this races with a bus reset we |
| 963 | * 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] | 964 | * pretty harmless. |
| 965 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 966 | if (device->node == device->card->root_node) |
Jay Fenlason | 0fa1986f | 2008-11-29 17:44:57 +0100 | [diff] [blame] | 967 | fw_schedule_bm_work(device->card, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 968 | |
| 969 | return; |
| 970 | |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 971 | error_with_cdev: |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 972 | down_write(&fw_device_rwsem); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 973 | idr_remove(&fw_device_idr, minor); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 974 | up_write(&fw_device_rwsem); |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 975 | error: |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 976 | fw_device_put(device); /* fw_device_idr's reference */ |
| 977 | |
| 978 | put_device(&device->device); /* our reference */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 979 | } |
| 980 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 981 | enum { |
| 982 | REREAD_BIB_ERROR, |
| 983 | REREAD_BIB_GONE, |
| 984 | REREAD_BIB_UNCHANGED, |
| 985 | REREAD_BIB_CHANGED, |
| 986 | }; |
| 987 | |
| 988 | /* Reread and compare bus info block and header of root directory */ |
| 989 | static int reread_bus_info_block(struct fw_device *device, int generation) |
| 990 | { |
| 991 | u32 q; |
| 992 | int i; |
| 993 | |
| 994 | for (i = 0; i < 6; i++) { |
| 995 | if (read_rom(device, generation, i, &q) != RCODE_COMPLETE) |
| 996 | return REREAD_BIB_ERROR; |
| 997 | |
| 998 | if (i == 0 && q == 0) |
| 999 | return REREAD_BIB_GONE; |
| 1000 | |
Stefan Richter | d01b017 | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 1001 | if (q != device->config_rom[i]) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1002 | return REREAD_BIB_CHANGED; |
| 1003 | } |
| 1004 | |
| 1005 | return REREAD_BIB_UNCHANGED; |
| 1006 | } |
| 1007 | |
| 1008 | static void fw_device_refresh(struct work_struct *work) |
| 1009 | { |
| 1010 | struct fw_device *device = |
| 1011 | container_of(work, struct fw_device, work.work); |
| 1012 | struct fw_card *card = device->card; |
| 1013 | int node_id = device->node_id; |
| 1014 | |
| 1015 | switch (reread_bus_info_block(device, device->generation)) { |
| 1016 | case REREAD_BIB_ERROR: |
| 1017 | if (device->config_rom_retries < MAX_RETRIES / 2 && |
| 1018 | atomic_read(&device->state) == FW_DEVICE_INITIALIZING) { |
| 1019 | device->config_rom_retries++; |
| 1020 | schedule_delayed_work(&device->work, RETRY_DELAY / 2); |
| 1021 | |
| 1022 | return; |
| 1023 | } |
| 1024 | goto give_up; |
| 1025 | |
| 1026 | case REREAD_BIB_GONE: |
| 1027 | goto gone; |
| 1028 | |
| 1029 | case REREAD_BIB_UNCHANGED: |
| 1030 | if (atomic_cmpxchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 1031 | FW_DEVICE_INITIALIZING, |
| 1032 | FW_DEVICE_RUNNING) == FW_DEVICE_GONE) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1033 | goto gone; |
| 1034 | |
| 1035 | fw_device_update(work); |
| 1036 | device->config_rom_retries = 0; |
| 1037 | goto out; |
| 1038 | |
| 1039 | case REREAD_BIB_CHANGED: |
| 1040 | break; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * Something changed. We keep things simple and don't investigate |
| 1045 | * further. We just destroy all previous units and create new ones. |
| 1046 | */ |
| 1047 | device_for_each_child(&device->device, NULL, shutdown_unit); |
| 1048 | |
| 1049 | if (read_bus_info_block(device, device->generation) < 0) { |
| 1050 | if (device->config_rom_retries < MAX_RETRIES && |
| 1051 | atomic_read(&device->state) == FW_DEVICE_INITIALIZING) { |
| 1052 | device->config_rom_retries++; |
| 1053 | schedule_delayed_work(&device->work, RETRY_DELAY); |
| 1054 | |
| 1055 | return; |
| 1056 | } |
| 1057 | goto give_up; |
| 1058 | } |
| 1059 | |
| 1060 | create_units(device); |
| 1061 | |
Stefan Richter | 0210b66 | 2009-05-23 00:03:29 +0200 | [diff] [blame] | 1062 | /* Userspace may want to re-read attributes. */ |
| 1063 | kobject_uevent(&device->device.kobj, KOBJ_CHANGE); |
| 1064 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1065 | if (atomic_cmpxchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 1066 | FW_DEVICE_INITIALIZING, |
| 1067 | FW_DEVICE_RUNNING) == FW_DEVICE_GONE) |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1068 | goto gone; |
| 1069 | |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 1070 | fw_notify("refreshed device %s\n", dev_name(&device->device)); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1071 | device->config_rom_retries = 0; |
| 1072 | goto out; |
| 1073 | |
| 1074 | give_up: |
Kay Sievers | a1f6481 | 2008-10-30 01:41:56 +0100 | [diff] [blame] | 1075 | 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] | 1076 | gone: |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 1077 | atomic_set(&device->state, FW_DEVICE_GONE); |
| 1078 | PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown); |
| 1079 | schedule_delayed_work(&device->work, SHUTDOWN_DELAY); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1080 | out: |
| 1081 | if (node_id == card->root_node->node_id) |
Jay Fenlason | 0fa1986f | 2008-11-29 17:44:57 +0100 | [diff] [blame] | 1082 | fw_schedule_bm_work(card, 0); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1085 | void fw_node_event(struct fw_card *card, struct fw_node *node, int event) |
| 1086 | { |
| 1087 | struct fw_device *device; |
| 1088 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1089 | switch (event) { |
| 1090 | case FW_NODE_CREATED: |
| 1091 | case FW_NODE_LINK_ON: |
| 1092 | if (!node->link_on) |
| 1093 | break; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1094 | create: |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1095 | device = kzalloc(sizeof(*device), GFP_ATOMIC); |
| 1096 | if (device == NULL) |
| 1097 | break; |
| 1098 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1099 | /* |
| 1100 | * Do minimal intialization of the device here, the |
Stefan Richter | 6230582 | 2009-01-09 20:49:37 +0100 | [diff] [blame] | 1101 | * rest will happen in fw_device_init(). |
| 1102 | * |
| 1103 | * Attention: A lot of things, even fw_device_get(), |
| 1104 | * cannot be done before fw_device_init() finished! |
| 1105 | * You can basically just check device->state and |
| 1106 | * schedule work until then, but only while holding |
| 1107 | * card->lock. |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1108 | */ |
Stefan Richter | 641f879 | 2007-01-27 10:34:55 +0100 | [diff] [blame] | 1109 | atomic_set(&device->state, FW_DEVICE_INITIALIZING); |
Stefan Richter | 459f792 | 2008-05-24 16:50:22 +0200 | [diff] [blame] | 1110 | device->card = fw_card_get(card); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1111 | device->node = fw_node_get(node); |
| 1112 | device->node_id = node->node_id; |
| 1113 | device->generation = card->generation; |
Stefan Richter | 9236889 | 2009-05-13 21:42:14 +0200 | [diff] [blame] | 1114 | device->is_local = node == card->local_node; |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame] | 1115 | mutex_init(&device->client_list_mutex); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 1116 | INIT_LIST_HEAD(&device->client_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1117 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1118 | /* |
| 1119 | * Set the node data to point back to this device so |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1120 | * FW_NODE_UPDATED callbacks can update the node_id |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1121 | * and generation for the device. |
| 1122 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1123 | node->data = device; |
| 1124 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1125 | /* |
| 1126 | * Many devices are slow to respond after bus resets, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1127 | * especially if they are bus powered and go through |
| 1128 | * power-up after getting plugged in. We schedule the |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1129 | * first config rom scan half a second after bus reset. |
| 1130 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1131 | INIT_DELAYED_WORK(&device->work, fw_device_init); |
| 1132 | schedule_delayed_work(&device->work, INITIAL_DELAY); |
| 1133 | break; |
| 1134 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1135 | case FW_NODE_INITIATED_RESET: |
| 1136 | device = node->data; |
| 1137 | if (device == NULL) |
| 1138 | goto create; |
| 1139 | |
| 1140 | device->node_id = node->node_id; |
| 1141 | smp_wmb(); /* update node_id before generation */ |
| 1142 | device->generation = card->generation; |
| 1143 | if (atomic_cmpxchg(&device->state, |
| 1144 | FW_DEVICE_RUNNING, |
| 1145 | FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) { |
| 1146 | PREPARE_DELAYED_WORK(&device->work, fw_device_refresh); |
| 1147 | schedule_delayed_work(&device->work, |
Stefan Richter | 9236889 | 2009-05-13 21:42:14 +0200 | [diff] [blame] | 1148 | device->is_local ? 0 : INITIAL_DELAY); |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 1149 | } |
| 1150 | break; |
| 1151 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1152 | case FW_NODE_UPDATED: |
| 1153 | if (!node->link_on || node->data == NULL) |
| 1154 | break; |
| 1155 | |
| 1156 | device = node->data; |
| 1157 | device->node_id = node->node_id; |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 1158 | smp_wmb(); /* update node_id before generation */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1159 | device->generation = card->generation; |
Kristian Høgsberg | 5f48047 | 2007-03-07 12:12:39 -0500 | [diff] [blame] | 1160 | if (atomic_read(&device->state) == FW_DEVICE_RUNNING) { |
| 1161 | PREPARE_DELAYED_WORK(&device->work, fw_device_update); |
| 1162 | schedule_delayed_work(&device->work, 0); |
| 1163 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1164 | break; |
| 1165 | |
| 1166 | case FW_NODE_DESTROYED: |
| 1167 | case FW_NODE_LINK_OFF: |
| 1168 | if (!node->data) |
| 1169 | break; |
| 1170 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1171 | /* |
| 1172 | * Destroy the device associated with the node. There |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1173 | * are two cases here: either the device is fully |
| 1174 | * initialized (FW_DEVICE_RUNNING) or we're in the |
| 1175 | * process of reading its config rom |
| 1176 | * (FW_DEVICE_INITIALIZING). If it is fully |
| 1177 | * initialized we can reuse device->work to schedule a |
| 1178 | * full fw_device_shutdown(). If not, there's work |
| 1179 | * scheduled to read it's config rom, and we just put |
| 1180 | * the device in shutdown state to have that code fail |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1181 | * to create the device. |
| 1182 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1183 | device = node->data; |
Stefan Richter | 641f879 | 2007-01-27 10:34:55 +0100 | [diff] [blame] | 1184 | if (atomic_xchg(&device->state, |
Stefan Richter | 3d36a0d | 2009-01-17 22:45:54 +0100 | [diff] [blame] | 1185 | FW_DEVICE_GONE) == FW_DEVICE_RUNNING) { |
Kristian Høgsberg | 5f48047 | 2007-03-07 12:12:39 -0500 | [diff] [blame] | 1186 | PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown); |
Stefan Richter | e747a5c | 2009-01-24 20:35:38 +0100 | [diff] [blame] | 1187 | schedule_delayed_work(&device->work, |
| 1188 | list_empty(&card->link) ? 0 : SHUTDOWN_DELAY); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1189 | } |
| 1190 | break; |
| 1191 | } |
| 1192 | } |