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