blob: a39e4344cd58579532153e976186ae2e92c68eaf [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Device probing and sysfs code.
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05003 *
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 Richter41f321c2009-01-17 22:45:54 +010021#include <linux/ctype.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050022#include <linux/delay.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010023#include <linux/device.h>
24#include <linux/errno.h>
Stefan Richter77c9a5d2009-06-05 16:26:18 +020025#include <linux/firewire.h>
26#include <linux/firewire-constants.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050027#include <linux/idr.h>
Stefan Richter3d36a0d2009-01-17 22:45:54 +010028#include <linux/jiffies.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010029#include <linux/kobject.h>
30#include <linux/list.h>
Stefan Richterb3b29882009-02-15 23:12:34 +010031#include <linux/mod_devicetable.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020032#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020033#include <linux/mutex.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040034#include <linux/rwsem.h>
35#include <linux/semaphore.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040036#include <linux/spinlock.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010037#include <linux/string.h>
38#include <linux/workqueue.h>
39
Stefan Richtere8ca9702009-06-04 21:09:38 +020040#include <asm/atomic.h>
41#include <asm/byteorder.h>
Stefan Richterb5d2a5e2008-01-25 18:57:41 +010042#include <asm/system.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010043
Stefan Richter77c9a5d2009-06-05 16:26:18 +020044#include "core.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050045
46void 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øgsberg19a15b92006-12-19 19:58:31 -050051EXPORT_SYMBOL(fw_csr_iterator_init);
52
53int 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øgsberg19a15b92006-12-19 19:58:31 -050060EXPORT_SYMBOL(fw_csr_iterator_next);
61
Clemens Ladisch1f8fef72009-12-24 11:59:57 +010062static u32 *search_leaf(u32 *directory, int search_key)
63{
64 struct fw_csr_iterator ci;
65 int last_key = 0, key, value;
66
67 fw_csr_iterator_init(&ci, directory);
68 while (fw_csr_iterator_next(&ci, &key, &value)) {
69 if (last_key == search_key &&
70 key == (CSR_DESCRIPTOR | CSR_LEAF))
71 return ci.p - 1 + value;
72 last_key = key;
73 }
74 return NULL;
75}
76
77static int textual_leaf_to_string(u32 *block, char *buf, size_t size)
78{
79 unsigned int quadlets, length;
80
81 if (!size || !buf)
82 return -EINVAL;
83
84 quadlets = min(block[0] >> 16, 256u);
85 if (quadlets < 2)
86 return -ENODATA;
87
88 if (block[1] != 0 || block[2] != 0)
89 /* unknown language/character set */
90 return -ENODATA;
91
92 block += 3;
93 quadlets -= 2;
94 for (length = 0; length < quadlets * 4 && length + 1 < size; length++) {
95 char c = block[length / 4] >> (24 - 8 * (length % 4));
96 if (c == '\0')
97 break;
98 buf[length] = c;
99 }
100 buf[length] = '\0';
101 return length;
102}
103
104/**
105 * fw_csr_string - reads a string from the configuration ROM
106 * @directory: device or unit directory;
107 * fw_device->config_rom+5 or fw_unit->directory
108 * @key: the key of the preceding directory entry
109 * @buf: where to put the string
110 * @size: size of @buf, in bytes
111 *
112 * Returns string length (>= 0) or error code (< 0).
113 */
114int fw_csr_string(u32 *directory, int key, char *buf, size_t size)
115{
116 u32 *leaf = search_leaf(directory, key);
117 if (!leaf)
118 return -ENOENT;
119 return textual_leaf_to_string(leaf, buf, size);
120}
121EXPORT_SYMBOL(fw_csr_string);
122
Stefan Richter099d5412009-06-06 18:37:25 +0200123static bool is_fw_unit(struct device *dev);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500124
Stefan Richtere41f8d72009-02-16 00:22:05 +0100125static int match_unit_directory(u32 *directory, u32 match_flags,
Stefan Richterb3b29882009-02-15 23:12:34 +0100126 const struct ieee1394_device_id *id)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500127{
128 struct fw_csr_iterator ci;
129 int key, value, match;
130
131 match = 0;
132 fw_csr_iterator_init(&ci, directory);
133 while (fw_csr_iterator_next(&ci, &key, &value)) {
Stefan Richterb3b29882009-02-15 23:12:34 +0100134 if (key == CSR_VENDOR && value == id->vendor_id)
135 match |= IEEE1394_MATCH_VENDOR_ID;
136 if (key == CSR_MODEL && value == id->model_id)
137 match |= IEEE1394_MATCH_MODEL_ID;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500138 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
Stefan Richterb3b29882009-02-15 23:12:34 +0100139 match |= IEEE1394_MATCH_SPECIFIER_ID;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500140 if (key == CSR_VERSION && value == id->version)
Stefan Richterb3b29882009-02-15 23:12:34 +0100141 match |= IEEE1394_MATCH_VERSION;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500142 }
143
Stefan Richtere41f8d72009-02-16 00:22:05 +0100144 return (match & match_flags) == match_flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500145}
146
147static int fw_unit_match(struct device *dev, struct device_driver *drv)
148{
149 struct fw_unit *unit = fw_unit(dev);
Stefan Richtere41f8d72009-02-16 00:22:05 +0100150 struct fw_device *device;
151 const struct ieee1394_device_id *id;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500152
153 /* We only allow binding to fw_units. */
154 if (!is_fw_unit(dev))
155 return 0;
156
Stefan Richtere5110d02009-06-06 18:35:27 +0200157 device = fw_parent_device(unit);
Stefan Richter77c9a5d2009-06-05 16:26:18 +0200158 id = container_of(drv, struct fw_driver, driver)->id_table;
Stefan Richtere41f8d72009-02-16 00:22:05 +0100159
Stefan Richter77c9a5d2009-06-05 16:26:18 +0200160 for (; id->match_flags != 0; id++) {
Stefan Richtere41f8d72009-02-16 00:22:05 +0100161 if (match_unit_directory(unit->directory, id->match_flags, id))
162 return 1;
163
164 /* Also check vendor ID in the root directory. */
165 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
166 match_unit_directory(&device->config_rom[5],
167 IEEE1394_MATCH_VENDOR_ID, id) &&
168 match_unit_directory(unit->directory, id->match_flags
169 & ~IEEE1394_MATCH_VENDOR_ID, id))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500170 return 1;
171 }
172
173 return 0;
174}
175
176static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
177{
Stefan Richtere5110d02009-06-06 18:35:27 +0200178 struct fw_device *device = fw_parent_device(unit);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500179 struct fw_csr_iterator ci;
180
181 int key, value;
182 int vendor = 0;
183 int model = 0;
184 int specifier_id = 0;
185 int version = 0;
186
187 fw_csr_iterator_init(&ci, &device->config_rom[5]);
188 while (fw_csr_iterator_next(&ci, &key, &value)) {
189 switch (key) {
190 case CSR_VENDOR:
191 vendor = value;
192 break;
193 case CSR_MODEL:
194 model = value;
195 break;
196 }
197 }
198
199 fw_csr_iterator_init(&ci, unit->directory);
200 while (fw_csr_iterator_next(&ci, &key, &value)) {
201 switch (key) {
202 case CSR_SPECIFIER_ID:
203 specifier_id = value;
204 break;
205 case CSR_VERSION:
206 version = value;
207 break;
208 }
209 }
210
211 return snprintf(buffer, buffer_size,
212 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
213 vendor, model, specifier_id, version);
214}
215
Stefan Richter53dca512008-12-14 21:47:04 +0100216static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500217{
218 struct fw_unit *unit = fw_unit(dev);
219 char modalias[64];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500220
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400221 get_modalias(unit, modalias, sizeof(modalias));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500222
Kay Sievers7eff2e72007-08-14 15:15:12 +0200223 if (add_uevent_var(env, "MODALIAS=%s", modalias))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500224 return -ENOMEM;
225
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500226 return 0;
227}
228
229struct bus_type fw_bus_type = {
Kristian Høgsberg362c2c82007-02-06 14:49:37 -0500230 .name = "firewire",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500231 .match = fw_unit_match,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500232};
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500233EXPORT_SYMBOL(fw_bus_type);
234
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500235int fw_device_enable_phys_dma(struct fw_device *device)
236{
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100237 int generation = device->generation;
238
239 /* device->node_id, accessed below, must not be older than generation */
240 smp_rmb();
241
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500242 return device->card->driver->enable_phys_dma(device->card,
243 device->node_id,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100244 generation);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500245}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500246EXPORT_SYMBOL(fw_device_enable_phys_dma);
247
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400248struct config_rom_attribute {
249 struct device_attribute attr;
250 u32 key;
251};
252
Stefan Richter53dca512008-12-14 21:47:04 +0100253static ssize_t show_immediate(struct device *dev,
254 struct device_attribute *dattr, char *buf)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400255{
256 struct config_rom_attribute *attr =
257 container_of(dattr, struct config_rom_attribute, attr);
258 struct fw_csr_iterator ci;
259 u32 *dir;
Stefan Richterc9755e12008-03-24 20:54:28 +0100260 int key, value, ret = -ENOENT;
261
262 down_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400263
264 if (is_fw_unit(dev))
265 dir = fw_unit(dev)->directory;
266 else
267 dir = fw_device(dev)->config_rom + 5;
268
269 fw_csr_iterator_init(&ci, dir);
270 while (fw_csr_iterator_next(&ci, &key, &value))
Stefan Richterc9755e12008-03-24 20:54:28 +0100271 if (attr->key == key) {
272 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
273 "0x%06x\n", value);
274 break;
275 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400276
Stefan Richterc9755e12008-03-24 20:54:28 +0100277 up_read(&fw_device_rwsem);
278
279 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400280}
281
282#define IMMEDIATE_ATTR(name, key) \
283 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
284
Stefan Richter53dca512008-12-14 21:47:04 +0100285static ssize_t show_text_leaf(struct device *dev,
286 struct device_attribute *dattr, char *buf)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400287{
288 struct config_rom_attribute *attr =
289 container_of(dattr, struct config_rom_attribute, attr);
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100290 u32 *dir;
291 size_t bufsize;
292 char dummy_buf[2];
293 int ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400294
Stefan Richterc9755e12008-03-24 20:54:28 +0100295 down_read(&fw_device_rwsem);
296
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400297 if (is_fw_unit(dev))
298 dir = fw_unit(dev)->directory;
299 else
300 dir = fw_device(dev)->config_rom + 5;
301
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100302 if (buf) {
303 bufsize = PAGE_SIZE - 1;
304 } else {
305 buf = dummy_buf;
306 bufsize = 1;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400307 }
308
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100309 ret = fw_csr_string(dir, attr->key, buf, bufsize);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400310
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100311 if (ret >= 0) {
312 /* Strip trailing whitespace and add newline. */
313 while (ret > 0 && isspace(buf[ret - 1]))
314 ret--;
315 strcpy(buf + ret, "\n");
316 ret++;
Stefan Richterc9755e12008-03-24 20:54:28 +0100317 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400318
Stefan Richterc9755e12008-03-24 20:54:28 +0100319 up_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400320
Stefan Richterc9755e12008-03-24 20:54:28 +0100321 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400322}
323
324#define TEXT_LEAF_ATTR(name, key) \
325 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
326
327static struct config_rom_attribute config_rom_attributes[] = {
328 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
329 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
330 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
331 IMMEDIATE_ATTR(version, CSR_VERSION),
332 IMMEDIATE_ATTR(model, CSR_MODEL),
333 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
334 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
335 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
336};
337
Stefan Richter53dca512008-12-14 21:47:04 +0100338static void init_fw_attribute_group(struct device *dev,
339 struct device_attribute *attrs,
340 struct fw_attribute_group *group)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400341{
342 struct device_attribute *attr;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400343 int i, j;
344
345 for (j = 0; attrs[j].attr.name != NULL; j++)
346 group->attrs[j] = &attrs[j].attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400347
348 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
349 attr = &config_rom_attributes[i].attr;
350 if (attr->show(dev, attr, NULL) < 0)
351 continue;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400352 group->attrs[j++] = &attr->attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400353 }
354
Stefan Richtere5333db2009-05-22 23:16:27 +0200355 group->attrs[j] = NULL;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400356 group->groups[0] = &group->group;
357 group->groups[1] = NULL;
358 group->group.attrs = group->attrs;
David Brownella4dbd672009-06-24 10:06:31 -0700359 dev->groups = (const struct attribute_group **) group->groups;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400360}
361
Stefan Richter53dca512008-12-14 21:47:04 +0100362static ssize_t modalias_show(struct device *dev,
363 struct device_attribute *attr, char *buf)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500364{
365 struct fw_unit *unit = fw_unit(dev);
366 int length;
367
368 length = get_modalias(unit, buf, PAGE_SIZE);
369 strcpy(buf + length, "\n");
370
371 return length + 1;
372}
373
Stefan Richter53dca512008-12-14 21:47:04 +0100374static ssize_t rom_index_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500376{
377 struct fw_device *device = fw_device(dev->parent);
378 struct fw_unit *unit = fw_unit(dev);
379
380 return snprintf(buf, PAGE_SIZE, "%d\n",
Stefan Richterd84702a2007-03-20 19:42:15 +0100381 (int)(unit->directory - device->config_rom));
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500382}
383
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400384static struct device_attribute fw_unit_attributes[] = {
385 __ATTR_RO(modalias),
386 __ATTR_RO(rom_index),
387 __ATTR_NULL,
388};
389
Stefan Richter53dca512008-12-14 21:47:04 +0100390static ssize_t config_rom_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400392{
393 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100394 size_t length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400395
Stefan Richterc9755e12008-03-24 20:54:28 +0100396 down_read(&fw_device_rwsem);
397 length = device->config_rom_length * 4;
398 memcpy(buf, device->config_rom, length);
399 up_read(&fw_device_rwsem);
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400400
Stefan Richterc9755e12008-03-24 20:54:28 +0100401 return length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400402}
403
Stefan Richter53dca512008-12-14 21:47:04 +0100404static ssize_t guid_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400406{
407 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100408 int ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400409
Stefan Richterc9755e12008-03-24 20:54:28 +0100410 down_read(&fw_device_rwsem);
411 ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
412 device->config_rom[3], device->config_rom[4]);
413 up_read(&fw_device_rwsem);
414
415 return ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400416}
417
Stefan Richter0210b662009-05-23 00:03:29 +0200418static int units_sprintf(char *buf, u32 *directory)
419{
420 struct fw_csr_iterator ci;
421 int key, value;
422 int specifier_id = 0;
423 int version = 0;
424
425 fw_csr_iterator_init(&ci, directory);
426 while (fw_csr_iterator_next(&ci, &key, &value)) {
427 switch (key) {
428 case CSR_SPECIFIER_ID:
429 specifier_id = value;
430 break;
431 case CSR_VERSION:
432 version = value;
433 break;
434 }
435 }
436
437 return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
438}
439
440static ssize_t units_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 struct fw_device *device = fw_device(dev);
444 struct fw_csr_iterator ci;
445 int key, value, i = 0;
446
447 down_read(&fw_device_rwsem);
448 fw_csr_iterator_init(&ci, &device->config_rom[5]);
449 while (fw_csr_iterator_next(&ci, &key, &value)) {
450 if (key != (CSR_UNIT | CSR_DIRECTORY))
451 continue;
452 i += units_sprintf(&buf[i], ci.p + value - 1);
453 if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
454 break;
455 }
456 up_read(&fw_device_rwsem);
457
458 if (i)
459 buf[i - 1] = '\n';
460
461 return i;
462}
463
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400464static struct device_attribute fw_device_attributes[] = {
465 __ATTR_RO(config_rom),
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400466 __ATTR_RO(guid),
Stefan Richter0210b662009-05-23 00:03:29 +0200467 __ATTR_RO(units),
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400468 __ATTR_NULL,
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500469};
470
Stefan Richter53dca512008-12-14 21:47:04 +0100471static int read_rom(struct fw_device *device,
472 int generation, int index, u32 *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500473{
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200474 int rcode;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100475
476 /* device->node_id, accessed below, must not be older than generation */
477 smp_rmb();
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500478
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200479 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100480 device->node_id, generation, device->max_speed,
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200481 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
482 data, 4);
483 be32_to_cpus(data);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500484
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200485 return rcode;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500486}
487
Stefan Richter1dadff72008-03-02 19:35:42 +0100488#define READ_BIB_ROM_SIZE 256
489#define READ_BIB_STACK_SIZE 16
490
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100491/*
492 * Read the bus info block, perform a speed probe, and read all of the rest of
493 * the config ROM. We do all this with a cached bus generation. If the bus
494 * generation changes under us, read_bus_info_block will fail and get retried.
495 * It's better to start all over in this case because the node from which we
496 * are reading the ROM may have changed the ROM during the reset.
497 */
498static int read_bus_info_block(struct fw_device *device, int generation)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500499{
Stefan Richterc9755e12008-03-24 20:54:28 +0100500 u32 *rom, *stack, *old_rom, *new_rom;
Stefan Richter1dadff72008-03-02 19:35:42 +0100501 u32 sp, key;
502 int i, end, length, ret = -1;
503
504 rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
505 sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
506 if (rom == NULL)
507 return -ENOMEM;
508
509 stack = &rom[READ_BIB_ROM_SIZE];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500510
Stefan Richterf1397492007-06-10 21:31:36 +0200511 device->max_speed = SCODE_100;
512
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500513 /* First read the bus info block. */
514 for (i = 0; i < 5; i++) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100515 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100516 goto out;
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400517 /*
518 * As per IEEE1212 7.2, during power-up, devices can
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500519 * reply with a 0 for the first quadlet of the config
520 * rom to indicate that they are booting (for example,
521 * if the firmware is on the disk of a external
522 * harddisk). In that case we just fail, and the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400523 * retry mechanism will try again later.
524 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500525 if (i == 0 && rom[i] == 0)
Stefan Richter1dadff72008-03-02 19:35:42 +0100526 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500527 }
528
Stefan Richterf1397492007-06-10 21:31:36 +0200529 device->max_speed = device->node->max_speed;
530
531 /*
532 * Determine the speed of
533 * - devices with link speed less than PHY speed,
534 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
535 * - all devices if there are 1394b repeaters.
536 * Note, we cannot use the bus info block's link_spd as starting point
537 * because some buggy firmwares set it lower than necessary and because
538 * 1394-1995 nodes do not have the field.
539 */
540 if ((rom[2] & 0x7) < device->max_speed ||
541 device->max_speed == SCODE_BETA ||
542 device->card->beta_repeaters_present) {
543 u32 dummy;
544
545 /* for S1600 and S3200 */
546 if (device->max_speed == SCODE_BETA)
547 device->max_speed = device->card->link_speed;
548
549 while (device->max_speed > SCODE_100) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100550 if (read_rom(device, generation, 0, &dummy) ==
551 RCODE_COMPLETE)
Stefan Richterf1397492007-06-10 21:31:36 +0200552 break;
553 device->max_speed--;
554 }
555 }
556
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400557 /*
558 * Now parse the config rom. The config rom is a recursive
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500559 * directory structure so we parse it using a stack of
560 * references to the blocks that make up the structure. We
561 * push a reference to the root directory on the stack to
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400562 * start things off.
563 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564 length = i;
565 sp = 0;
566 stack[sp++] = 0xc0000005;
567 while (sp > 0) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400568 /*
569 * Pop the next block reference of the stack. The
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500570 * lower 24 bits is the offset into the config rom,
571 * the upper 8 bits are the type of the reference the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400572 * block.
573 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500574 key = stack[--sp];
575 i = key & 0xffffff;
Stefan Richter1dadff72008-03-02 19:35:42 +0100576 if (i >= READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400577 /*
578 * The reference points outside the standard
579 * config rom area, something's fishy.
580 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100581 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500582
583 /* Read header quadlet for the block to get the length. */
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100584 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100585 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500586 end = i + (rom[i] >> 16) + 1;
587 i++;
Stefan Richter1dadff72008-03-02 19:35:42 +0100588 if (end > READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400589 /*
590 * This block extends outside standard config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500591 * area (and the array we're reading it
592 * into). That's broken, so ignore this
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400593 * device.
594 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100595 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500596
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400597 /*
598 * Now read in the block. If this is a directory
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500599 * block, check the entries as we read them to see if
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400600 * it references another block, and push it in that case.
601 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500602 while (i < end) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100603 if (read_rom(device, generation, i, &rom[i]) !=
604 RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100605 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500606 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
Stefan Richter1dadff72008-03-02 19:35:42 +0100607 sp < READ_BIB_STACK_SIZE)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500608 stack[sp++] = i + rom[i];
609 i++;
610 }
611 if (length < i)
612 length = i;
613 }
614
Stefan Richterc9755e12008-03-24 20:54:28 +0100615 old_rom = device->config_rom;
616 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
617 if (new_rom == NULL)
Stefan Richter1dadff72008-03-02 19:35:42 +0100618 goto out;
Stefan Richterc9755e12008-03-24 20:54:28 +0100619
620 down_write(&fw_device_rwsem);
621 device->config_rom = new_rom;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500622 device->config_rom_length = length;
Stefan Richterc9755e12008-03-24 20:54:28 +0100623 up_write(&fw_device_rwsem);
624
625 kfree(old_rom);
Stefan Richter1dadff72008-03-02 19:35:42 +0100626 ret = 0;
Stefan Richter837ec782009-06-09 23:56:55 +0200627 device->max_rec = rom[2] >> 12 & 0xf;
628 device->cmc = rom[2] >> 30 & 1;
629 device->irmc = rom[2] >> 31 & 1;
Stefan Richter1dadff72008-03-02 19:35:42 +0100630 out:
631 kfree(rom);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632
Stefan Richter1dadff72008-03-02 19:35:42 +0100633 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500634}
635
636static void fw_unit_release(struct device *dev)
637{
638 struct fw_unit *unit = fw_unit(dev);
639
640 kfree(unit);
641}
642
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400643static struct device_type fw_unit_type = {
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400644 .uevent = fw_unit_uevent,
645 .release = fw_unit_release,
646};
647
Stefan Richter099d5412009-06-06 18:37:25 +0200648static bool is_fw_unit(struct device *dev)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500649{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400650 return dev->type == &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500651}
652
653static void create_units(struct fw_device *device)
654{
655 struct fw_csr_iterator ci;
656 struct fw_unit *unit;
657 int key, value, i;
658
659 i = 0;
660 fw_csr_iterator_init(&ci, &device->config_rom[5]);
661 while (fw_csr_iterator_next(&ci, &key, &value)) {
662 if (key != (CSR_UNIT | CSR_DIRECTORY))
663 continue;
664
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400665 /*
666 * Get the address of the unit directory and try to
667 * match the drivers id_tables against it.
668 */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400669 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500670 if (unit == NULL) {
671 fw_error("failed to allocate memory for unit\n");
672 continue;
673 }
674
675 unit->directory = ci.p + value - 1;
676 unit->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400677 unit->device.type = &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500678 unit->device.parent = &device->device;
Kay Sieversa1f64812008-10-30 01:41:56 +0100679 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500680
Stefan Richtere5333db2009-05-22 23:16:27 +0200681 BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
682 ARRAY_SIZE(fw_unit_attributes) +
683 ARRAY_SIZE(config_rom_attributes));
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400684 init_fw_attribute_group(&unit->device,
685 fw_unit_attributes,
686 &unit->attribute_group);
Stefan Richtere5333db2009-05-22 23:16:27 +0200687
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400688 if (device_register(&unit->device) < 0)
689 goto skip_unit;
690
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400691 continue;
692
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400693 skip_unit:
694 kfree(unit);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500695 }
696}
697
698static int shutdown_unit(struct device *device, void *data)
699{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400700 device_unregister(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500701
702 return 0;
703}
704
Stefan Richterc9755e12008-03-24 20:54:28 +0100705/*
706 * fw_device_rwsem acts as dual purpose mutex:
707 * - serializes accesses to fw_device_idr,
708 * - serializes accesses to fw_device.config_rom/.config_rom_length and
709 * fw_unit.directory, unless those accesses happen at safe occasions
710 */
711DECLARE_RWSEM(fw_device_rwsem);
712
Stefan Richterd6053e02008-11-24 20:40:00 +0100713DEFINE_IDR(fw_device_idr);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500714int fw_cdev_major;
715
Stefan Richter96b19062008-02-02 15:01:09 +0100716struct fw_device *fw_device_get_by_devt(dev_t devt)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500717{
718 struct fw_device *device;
719
Stefan Richterc9755e12008-03-24 20:54:28 +0100720 down_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500721 device = idr_find(&fw_device_idr, MINOR(devt));
Stefan Richter96b19062008-02-02 15:01:09 +0100722 if (device)
723 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100724 up_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500725
726 return device;
727}
728
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400729/*
730 * These defines control the retry behavior for reading the config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731 * rom. It shouldn't be necessary to tweak these; if the device
732 * doesn't respond to a config rom read within 10 seconds, it's not
733 * going to respond at all. As for the initial delay, a lot of
734 * devices will be able to respond within half a second after bus
735 * reset. On the other hand, it's not really worth being more
736 * aggressive than that, since it scales pretty well; if 10 devices
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400737 * are plugged in, they're all getting read within one second.
738 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500739
Kristian Høgsbergc5dfd0a2007-03-27 01:43:43 -0400740#define MAX_RETRIES 10
741#define RETRY_DELAY (3 * HZ)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500742#define INITIAL_DELAY (HZ / 2)
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100743#define SHUTDOWN_DELAY (2 * HZ)
744
745static void fw_device_shutdown(struct work_struct *work)
746{
747 struct fw_device *device =
748 container_of(work, struct fw_device, work.work);
749 int minor = MINOR(device->device.devt);
750
Stefan Richtere747a5c2009-01-24 20:35:38 +0100751 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
752 && !list_empty(&device->card->link)) {
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100753 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
754 return;
755 }
756
757 if (atomic_cmpxchg(&device->state,
758 FW_DEVICE_GONE,
759 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
760 return;
761
762 fw_device_cdev_remove(device);
763 device_for_each_child(&device->device, NULL, shutdown_unit);
764 device_unregister(&device->device);
765
766 down_write(&fw_device_rwsem);
767 idr_remove(&fw_device_idr, minor);
768 up_write(&fw_device_rwsem);
769
770 fw_device_put(device);
771}
772
Stefan Richteraed80892009-01-17 22:45:54 +0100773static void fw_device_release(struct device *dev)
774{
775 struct fw_device *device = fw_device(dev);
776 struct fw_card *card = device->card;
777 unsigned long flags;
778
779 /*
780 * Take the card lock so we don't set this to NULL while a
781 * FW_NODE_UPDATED callback is being handled or while the
782 * bus manager work looks at this node.
783 */
784 spin_lock_irqsave(&card->lock, flags);
785 device->node->data = NULL;
786 spin_unlock_irqrestore(&card->lock, flags);
787
788 fw_node_put(device->node);
789 kfree(device->config_rom);
790 kfree(device);
791 fw_card_put(card);
792}
793
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100794static struct device_type fw_device_type = {
Stefan Richteraed80892009-01-17 22:45:54 +0100795 .release = fw_device_release,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100796};
797
Stefan Richter099d5412009-06-06 18:37:25 +0200798static bool is_fw_device(struct device *dev)
799{
800 return dev->type == &fw_device_type;
801}
802
Stefan Richteraed80892009-01-17 22:45:54 +0100803static int update_unit(struct device *dev, void *data)
804{
805 struct fw_unit *unit = fw_unit(dev);
806 struct fw_driver *driver = (struct fw_driver *)dev->driver;
807
808 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
809 down(&dev->sem);
810 driver->update(unit);
811 up(&dev->sem);
812 }
813
814 return 0;
815}
816
817static void fw_device_update(struct work_struct *work)
818{
819 struct fw_device *device =
820 container_of(work, struct fw_device, work.work);
821
822 fw_device_cdev_update(device);
823 device_for_each_child(&device->device, NULL, update_unit);
824}
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100825
826/*
827 * If a device was pending for deletion because its node went away but its
828 * bus info block and root directory header matches that of a newly discovered
829 * device, revive the existing fw_device.
830 * The newly allocated fw_device becomes obsolete instead.
831 */
832static int lookup_existing_device(struct device *dev, void *data)
833{
834 struct fw_device *old = fw_device(dev);
835 struct fw_device *new = data;
836 struct fw_card *card = new->card;
837 int match = 0;
838
Stefan Richter099d5412009-06-06 18:37:25 +0200839 if (!is_fw_device(dev))
840 return 0;
841
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100842 down_read(&fw_device_rwsem); /* serialize config_rom access */
843 spin_lock_irq(&card->lock); /* serialize node access */
844
845 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
846 atomic_cmpxchg(&old->state,
847 FW_DEVICE_GONE,
848 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
849 struct fw_node *current_node = new->node;
850 struct fw_node *obsolete_node = old->node;
851
852 new->node = obsolete_node;
853 new->node->data = new;
854 old->node = current_node;
855 old->node->data = old;
856
857 old->max_speed = new->max_speed;
858 old->node_id = current_node->node_id;
859 smp_wmb(); /* update node_id before generation */
860 old->generation = card->generation;
861 old->config_rom_retries = 0;
862 fw_notify("rediscovered device %s\n", dev_name(dev));
863
864 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
865 schedule_delayed_work(&old->work, 0);
866
867 if (current_node == card->root_node)
868 fw_schedule_bm_work(card, 0);
869
870 match = 1;
871 }
872
873 spin_unlock_irq(&card->lock);
874 up_read(&fw_device_rwsem);
875
876 return match;
877}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500878
Stefan Richter7889b602009-03-10 21:09:28 +0100879enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
880
Stefan Richter099d5412009-06-06 18:37:25 +0200881static void set_broadcast_channel(struct fw_device *device, int generation)
Stefan Richter7889b602009-03-10 21:09:28 +0100882{
883 struct fw_card *card = device->card;
884 __be32 data;
885 int rcode;
886
887 if (!card->broadcast_channel_allocated)
888 return;
889
Stefan Richter837ec782009-06-09 23:56:55 +0200890 /*
891 * The Broadcast_Channel Valid bit is required by nodes which want to
892 * transmit on this channel. Such transmissions are practically
893 * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
894 * to be IRM capable and have a max_rec of 8 or more. We use this fact
895 * to narrow down to which nodes we send Broadcast_Channel updates.
896 */
897 if (!device->irmc || device->max_rec < 8)
898 return;
899
900 /*
901 * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
902 * Perform a read test first.
903 */
Stefan Richter7889b602009-03-10 21:09:28 +0100904 if (device->bc_implemented == BC_UNKNOWN) {
905 rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
906 device->node_id, generation, device->max_speed,
907 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
908 &data, 4);
909 switch (rcode) {
910 case RCODE_COMPLETE:
911 if (data & cpu_to_be32(1 << 31)) {
912 device->bc_implemented = BC_IMPLEMENTED;
913 break;
914 }
915 /* else fall through to case address error */
916 case RCODE_ADDRESS_ERROR:
917 device->bc_implemented = BC_UNIMPLEMENTED;
918 }
919 }
920
921 if (device->bc_implemented == BC_IMPLEMENTED) {
922 data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
923 BROADCAST_CHANNEL_VALID);
924 fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
925 device->node_id, generation, device->max_speed,
926 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
927 &data, 4);
928 }
929}
930
Stefan Richter099d5412009-06-06 18:37:25 +0200931int fw_device_set_broadcast_channel(struct device *dev, void *gen)
932{
933 if (is_fw_device(dev))
934 set_broadcast_channel(fw_device(dev), (long)gen);
935
936 return 0;
937}
938
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500939static void fw_device_init(struct work_struct *work)
940{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500941 struct fw_device *device =
942 container_of(work, struct fw_device, work.work);
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100943 struct device *revived_dev;
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100944 int minor, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500945
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400946 /*
947 * All failure paths here set node->data to NULL, so that we
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500948 * don't try to do device_for_each_child() on a kfree()'d
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400949 * device.
950 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500951
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100952 if (read_bus_info_block(device, device->generation) < 0) {
Stefan Richter855c6032008-02-27 22:14:27 +0100953 if (device->config_rom_retries < MAX_RETRIES &&
954 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500955 device->config_rom_retries++;
956 schedule_delayed_work(&device->work, RETRY_DELAY);
957 } else {
Stefan Richter907293d2007-01-23 21:11:43 +0100958 fw_notify("giving up on config rom for node id %x\n",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500959 device->node_id);
Kristian Høgsberg931c4832007-01-26 00:38:45 -0500960 if (device->node == device->card->root_node)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +0100961 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500962 fw_device_release(&device->device);
963 }
964 return;
965 }
966
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100967 revived_dev = device_find_child(device->card->device,
968 device, lookup_existing_device);
969 if (revived_dev) {
970 put_device(revived_dev);
971 fw_device_release(&device->device);
972
973 return;
974 }
975
Stefan Richter62305822009-01-09 20:49:37 +0100976 device_initialize(&device->device);
Stefan Richter96b19062008-02-02 15:01:09 +0100977
978 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100979 down_write(&fw_device_rwsem);
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100980 ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
Stefan Richter62305822009-01-09 20:49:37 +0100981 idr_get_new(&fw_device_idr, device, &minor) :
982 -ENOMEM;
Stefan Richterc9755e12008-03-24 20:54:28 +0100983 up_write(&fw_device_rwsem);
Stefan Richter96b19062008-02-02 15:01:09 +0100984
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100985 if (ret < 0)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500986 goto error;
987
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500988 device->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400989 device->device.type = &fw_device_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500990 device->device.parent = device->card->device;
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500991 device->device.devt = MKDEV(fw_cdev_major, minor);
Kay Sieversa1f64812008-10-30 01:41:56 +0100992 dev_set_name(&device->device, "fw%d", minor);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500993
Stefan Richtere5333db2009-05-22 23:16:27 +0200994 BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
995 ARRAY_SIZE(fw_device_attributes) +
996 ARRAY_SIZE(config_rom_attributes));
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400997 init_fw_attribute_group(&device->device,
998 fw_device_attributes,
999 &device->attribute_group);
Stefan Richtere5333db2009-05-22 23:16:27 +02001000
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001001 if (device_add(&device->device)) {
1002 fw_error("Failed to add device.\n");
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -05001003 goto error_with_cdev;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001004 }
1005
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001006 create_units(device);
1007
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001008 /*
1009 * Transition the device to running state. If it got pulled
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001010 * out from under us while we did the intialization work, we
1011 * have to shut down the device again here. Normally, though,
1012 * fw_node_event will be responsible for shutting it down when
1013 * necessary. We have to use the atomic cmpxchg here to avoid
1014 * racing with the FW_NODE_DESTROYED case in
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001015 * fw_node_event().
1016 */
Stefan Richter641f8792007-01-27 10:34:55 +01001017 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001018 FW_DEVICE_INITIALIZING,
1019 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
1020 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1021 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterfa6e6972008-02-03 23:03:00 +01001022 } else {
1023 if (device->config_rom_retries)
1024 fw_notify("created device %s: GUID %08x%08x, S%d00, "
1025 "%d config ROM retries\n",
Kay Sieversa1f64812008-10-30 01:41:56 +01001026 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +01001027 device->config_rom[3], device->config_rom[4],
1028 1 << device->max_speed,
1029 device->config_rom_retries);
1030 else
1031 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
Kay Sieversa1f64812008-10-30 01:41:56 +01001032 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +01001033 device->config_rom[3], device->config_rom[4],
1034 1 << device->max_speed);
Stefan Richterc9755e12008-03-24 20:54:28 +01001035 device->config_rom_retries = 0;
Stefan Richter7889b602009-03-10 21:09:28 +01001036
Stefan Richter099d5412009-06-06 18:37:25 +02001037 set_broadcast_channel(device, device->generation);
Stefan Richterfa6e6972008-02-03 23:03:00 +01001038 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001039
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001040 /*
1041 * Reschedule the IRM work if we just finished reading the
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001042 * root node config rom. If this races with a bus reset we
1043 * just end up running the IRM work a couple of extra times -
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001044 * pretty harmless.
1045 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001046 if (device->node == device->card->root_node)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +01001047 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001048
1049 return;
1050
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -05001051 error_with_cdev:
Stefan Richterc9755e12008-03-24 20:54:28 +01001052 down_write(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -05001053 idr_remove(&fw_device_idr, minor);
Stefan Richterc9755e12008-03-24 20:54:28 +01001054 up_write(&fw_device_rwsem);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001055 error:
Stefan Richter96b19062008-02-02 15:01:09 +01001056 fw_device_put(device); /* fw_device_idr's reference */
1057
1058 put_device(&device->device); /* our reference */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001059}
1060
Stefan Richterc9755e12008-03-24 20:54:28 +01001061enum {
1062 REREAD_BIB_ERROR,
1063 REREAD_BIB_GONE,
1064 REREAD_BIB_UNCHANGED,
1065 REREAD_BIB_CHANGED,
1066};
1067
1068/* Reread and compare bus info block and header of root directory */
1069static int reread_bus_info_block(struct fw_device *device, int generation)
1070{
1071 u32 q;
1072 int i;
1073
1074 for (i = 0; i < 6; i++) {
1075 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
1076 return REREAD_BIB_ERROR;
1077
1078 if (i == 0 && q == 0)
1079 return REREAD_BIB_GONE;
1080
Stefan Richterd01b0172009-01-17 22:45:54 +01001081 if (q != device->config_rom[i])
Stefan Richterc9755e12008-03-24 20:54:28 +01001082 return REREAD_BIB_CHANGED;
1083 }
1084
1085 return REREAD_BIB_UNCHANGED;
1086}
1087
1088static void fw_device_refresh(struct work_struct *work)
1089{
1090 struct fw_device *device =
1091 container_of(work, struct fw_device, work.work);
1092 struct fw_card *card = device->card;
1093 int node_id = device->node_id;
1094
1095 switch (reread_bus_info_block(device, device->generation)) {
1096 case REREAD_BIB_ERROR:
1097 if (device->config_rom_retries < MAX_RETRIES / 2 &&
1098 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1099 device->config_rom_retries++;
1100 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
1101
1102 return;
1103 }
1104 goto give_up;
1105
1106 case REREAD_BIB_GONE:
1107 goto gone;
1108
1109 case REREAD_BIB_UNCHANGED:
1110 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001111 FW_DEVICE_INITIALIZING,
1112 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +01001113 goto gone;
1114
1115 fw_device_update(work);
1116 device->config_rom_retries = 0;
1117 goto out;
1118
1119 case REREAD_BIB_CHANGED:
1120 break;
1121 }
1122
1123 /*
1124 * Something changed. We keep things simple and don't investigate
1125 * further. We just destroy all previous units and create new ones.
1126 */
1127 device_for_each_child(&device->device, NULL, shutdown_unit);
1128
1129 if (read_bus_info_block(device, device->generation) < 0) {
1130 if (device->config_rom_retries < MAX_RETRIES &&
1131 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1132 device->config_rom_retries++;
1133 schedule_delayed_work(&device->work, RETRY_DELAY);
1134
1135 return;
1136 }
1137 goto give_up;
1138 }
1139
1140 create_units(device);
1141
Stefan Richter0210b662009-05-23 00:03:29 +02001142 /* Userspace may want to re-read attributes. */
1143 kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1144
Stefan Richterc9755e12008-03-24 20:54:28 +01001145 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001146 FW_DEVICE_INITIALIZING,
1147 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +01001148 goto gone;
1149
Kay Sieversa1f64812008-10-30 01:41:56 +01001150 fw_notify("refreshed device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +01001151 device->config_rom_retries = 0;
1152 goto out;
1153
1154 give_up:
Kay Sieversa1f64812008-10-30 01:41:56 +01001155 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +01001156 gone:
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001157 atomic_set(&device->state, FW_DEVICE_GONE);
1158 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1159 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +01001160 out:
1161 if (node_id == card->root_node->node_id)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +01001162 fw_schedule_bm_work(card, 0);
Stefan Richterc9755e12008-03-24 20:54:28 +01001163}
1164
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001165void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1166{
1167 struct fw_device *device;
1168
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001169 switch (event) {
1170 case FW_NODE_CREATED:
1171 case FW_NODE_LINK_ON:
1172 if (!node->link_on)
1173 break;
Stefan Richterc9755e12008-03-24 20:54:28 +01001174 create:
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001175 device = kzalloc(sizeof(*device), GFP_ATOMIC);
1176 if (device == NULL)
1177 break;
1178
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001179 /*
1180 * Do minimal intialization of the device here, the
Stefan Richter62305822009-01-09 20:49:37 +01001181 * rest will happen in fw_device_init().
1182 *
1183 * Attention: A lot of things, even fw_device_get(),
1184 * cannot be done before fw_device_init() finished!
1185 * You can basically just check device->state and
1186 * schedule work until then, but only while holding
1187 * card->lock.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001188 */
Stefan Richter641f8792007-01-27 10:34:55 +01001189 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
Stefan Richter459f7922008-05-24 16:50:22 +02001190 device->card = fw_card_get(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001191 device->node = fw_node_get(node);
1192 device->node_id = node->node_id;
1193 device->generation = card->generation;
Stefan Richter92368892009-05-13 21:42:14 +02001194 device->is_local = node == card->local_node;
Stefan Richterd67cfb92008-10-05 10:37:11 +02001195 mutex_init(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -05001196 INIT_LIST_HEAD(&device->client_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001197
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001198 /*
1199 * Set the node data to point back to this device so
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001200 * FW_NODE_UPDATED callbacks can update the node_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001201 * and generation for the device.
1202 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001203 node->data = device;
1204
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001205 /*
1206 * Many devices are slow to respond after bus resets,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001207 * especially if they are bus powered and go through
1208 * power-up after getting plugged in. We schedule the
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001209 * first config rom scan half a second after bus reset.
1210 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001211 INIT_DELAYED_WORK(&device->work, fw_device_init);
1212 schedule_delayed_work(&device->work, INITIAL_DELAY);
1213 break;
1214
Stefan Richterc9755e12008-03-24 20:54:28 +01001215 case FW_NODE_INITIATED_RESET:
1216 device = node->data;
1217 if (device == NULL)
1218 goto create;
1219
1220 device->node_id = node->node_id;
1221 smp_wmb(); /* update node_id before generation */
1222 device->generation = card->generation;
1223 if (atomic_cmpxchg(&device->state,
1224 FW_DEVICE_RUNNING,
1225 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1226 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1227 schedule_delayed_work(&device->work,
Stefan Richter92368892009-05-13 21:42:14 +02001228 device->is_local ? 0 : INITIAL_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +01001229 }
1230 break;
1231
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001232 case FW_NODE_UPDATED:
1233 if (!node->link_on || node->data == NULL)
1234 break;
1235
1236 device = node->data;
1237 device->node_id = node->node_id;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +01001238 smp_wmb(); /* update node_id before generation */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001239 device->generation = card->generation;
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001240 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1241 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1242 schedule_delayed_work(&device->work, 0);
1243 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001244 break;
1245
1246 case FW_NODE_DESTROYED:
1247 case FW_NODE_LINK_OFF:
1248 if (!node->data)
1249 break;
1250
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001251 /*
1252 * Destroy the device associated with the node. There
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001253 * are two cases here: either the device is fully
1254 * initialized (FW_DEVICE_RUNNING) or we're in the
1255 * process of reading its config rom
1256 * (FW_DEVICE_INITIALIZING). If it is fully
1257 * initialized we can reuse device->work to schedule a
1258 * full fw_device_shutdown(). If not, there's work
1259 * scheduled to read it's config rom, and we just put
1260 * the device in shutdown state to have that code fail
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001261 * to create the device.
1262 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001263 device = node->data;
Stefan Richter641f8792007-01-27 10:34:55 +01001264 if (atomic_xchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001265 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001266 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
Stefan Richtere747a5c2009-01-24 20:35:38 +01001267 schedule_delayed_work(&device->work,
1268 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001269 }
1270 break;
1271 }
1272}