blob: d6e54a5173fc5874653ddd6667e30d8621e197d5 [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
Stefan Richter099d5412009-06-06 18:37:25 +020062static bool is_fw_unit(struct device *dev);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050063
Stefan Richtere41f8d72009-02-16 00:22:05 +010064static int match_unit_directory(u32 *directory, u32 match_flags,
Stefan Richterb3b29882009-02-15 23:12:34 +010065 const struct ieee1394_device_id *id)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050066{
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 Richterb3b29882009-02-15 23:12:34 +010073 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øgsberg19a15b92006-12-19 19:58:31 -050077 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
Stefan Richterb3b29882009-02-15 23:12:34 +010078 match |= IEEE1394_MATCH_SPECIFIER_ID;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079 if (key == CSR_VERSION && value == id->version)
Stefan Richterb3b29882009-02-15 23:12:34 +010080 match |= IEEE1394_MATCH_VERSION;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050081 }
82
Stefan Richtere41f8d72009-02-16 00:22:05 +010083 return (match & match_flags) == match_flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050084}
85
86static int fw_unit_match(struct device *dev, struct device_driver *drv)
87{
88 struct fw_unit *unit = fw_unit(dev);
Stefan Richtere41f8d72009-02-16 00:22:05 +010089 struct fw_device *device;
90 const struct ieee1394_device_id *id;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050091
92 /* We only allow binding to fw_units. */
93 if (!is_fw_unit(dev))
94 return 0;
95
Stefan Richtere5110d02009-06-06 18:35:27 +020096 device = fw_parent_device(unit);
Stefan Richter77c9a5d2009-06-05 16:26:18 +020097 id = container_of(drv, struct fw_driver, driver)->id_table;
Stefan Richtere41f8d72009-02-16 00:22:05 +010098
Stefan Richter77c9a5d2009-06-05 16:26:18 +020099 for (; id->match_flags != 0; id++) {
Stefan Richtere41f8d72009-02-16 00:22:05 +0100100 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øgsberg19a15b92006-12-19 19:58:31 -0500109 return 1;
110 }
111
112 return 0;
113}
114
115static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
116{
Stefan Richtere5110d02009-06-06 18:35:27 +0200117 struct fw_device *device = fw_parent_device(unit);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500118 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 Richter53dca512008-12-14 21:47:04 +0100155static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500156{
157 struct fw_unit *unit = fw_unit(dev);
158 char modalias[64];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500159
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400160 get_modalias(unit, modalias, sizeof(modalias));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500161
Kay Sievers7eff2e72007-08-14 15:15:12 +0200162 if (add_uevent_var(env, "MODALIAS=%s", modalias))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500163 return -ENOMEM;
164
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500165 return 0;
166}
167
168struct bus_type fw_bus_type = {
Kristian Høgsberg362c2c82007-02-06 14:49:37 -0500169 .name = "firewire",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500170 .match = fw_unit_match,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171};
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500172EXPORT_SYMBOL(fw_bus_type);
173
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500174int fw_device_enable_phys_dma(struct fw_device *device)
175{
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100176 int generation = device->generation;
177
178 /* device->node_id, accessed below, must not be older than generation */
179 smp_rmb();
180
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500181 return device->card->driver->enable_phys_dma(device->card,
182 device->node_id,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100183 generation);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500184}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500185EXPORT_SYMBOL(fw_device_enable_phys_dma);
186
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400187struct config_rom_attribute {
188 struct device_attribute attr;
189 u32 key;
190};
191
Stefan Richter53dca512008-12-14 21:47:04 +0100192static ssize_t show_immediate(struct device *dev,
193 struct device_attribute *dattr, char *buf)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400194{
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 Richterc9755e12008-03-24 20:54:28 +0100199 int key, value, ret = -ENOENT;
200
201 down_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400202
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 Richterc9755e12008-03-24 20:54:28 +0100210 if (attr->key == key) {
211 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
212 "0x%06x\n", value);
213 break;
214 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400215
Stefan Richterc9755e12008-03-24 20:54:28 +0100216 up_read(&fw_device_rwsem);
217
218 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400219}
220
221#define IMMEDIATE_ATTR(name, key) \
222 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
223
Stefan Richter53dca512008-12-14 21:47:04 +0100224static ssize_t show_text_leaf(struct device *dev,
225 struct device_attribute *dattr, char *buf)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400226{
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 Richterc9755e12008-03-24 20:54:28 +0100231 int length, key, value, last_key = 0, ret = -ENOENT;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400232 char *b;
233
Stefan Richterc9755e12008-03-24 20:54:28 +0100234 down_read(&fw_device_rwsem);
235
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400236 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 Richterc9755e12008-03-24 20:54:28 +0100250 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400251
252 length = min(block[0] >> 16, 256U);
253 if (length < 3)
Stefan Richterc9755e12008-03-24 20:54:28 +0100254 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400255
256 if (block[1] != 0 || block[2] != 0)
257 /* Unknown encoding. */
Stefan Richterc9755e12008-03-24 20:54:28 +0100258 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400259
Stefan Richterc9755e12008-03-24 20:54:28 +0100260 if (buf == NULL) {
261 ret = length * 4;
262 goto out;
263 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400264
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 Richterc9755e12008-03-24 20:54:28 +0100273 ret = b + 2 - buf;
274 out:
275 up_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400276
Stefan Richterc9755e12008-03-24 20:54:28 +0100277 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400278}
279
280#define TEXT_LEAF_ATTR(name, key) \
281 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
282
283static 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 Richter53dca512008-12-14 21:47:04 +0100294static void init_fw_attribute_group(struct device *dev,
295 struct device_attribute *attrs,
296 struct fw_attribute_group *group)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400297{
298 struct device_attribute *attr;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400299 int i, j;
300
301 for (j = 0; attrs[j].attr.name != NULL; j++)
302 group->attrs[j] = &attrs[j].attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400303
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øgsberg6f2e53d2007-03-27 19:35:13 -0400308 group->attrs[j++] = &attr->attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400309 }
310
Stefan Richtere5333db2009-05-22 23:16:27 +0200311 group->attrs[j] = NULL;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400312 group->groups[0] = &group->group;
313 group->groups[1] = NULL;
314 group->group.attrs = group->attrs;
315 dev->groups = group->groups;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400316}
317
Stefan Richter53dca512008-12-14 21:47:04 +0100318static ssize_t modalias_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500320{
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 Richter53dca512008-12-14 21:47:04 +0100330static ssize_t rom_index_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500332{
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 Richterd84702a2007-03-20 19:42:15 +0100337 (int)(unit->directory - device->config_rom));
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500338}
339
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400340static struct device_attribute fw_unit_attributes[] = {
341 __ATTR_RO(modalias),
342 __ATTR_RO(rom_index),
343 __ATTR_NULL,
344};
345
Stefan Richter53dca512008-12-14 21:47:04 +0100346static ssize_t config_rom_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400348{
349 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100350 size_t length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400351
Stefan Richterc9755e12008-03-24 20:54:28 +0100352 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øgsberg21351db2007-03-20 20:58:33 -0400356
Stefan Richterc9755e12008-03-24 20:54:28 +0100357 return length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400358}
359
Stefan Richter53dca512008-12-14 21:47:04 +0100360static ssize_t guid_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400362{
363 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100364 int ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400365
Stefan Richterc9755e12008-03-24 20:54:28 +0100366 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øgsbergbbd14942007-03-20 20:58:35 -0400372}
373
Stefan Richter0210b662009-05-23 00:03:29 +0200374static 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
396static 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øgsberg21351db2007-03-20 20:58:33 -0400420static struct device_attribute fw_device_attributes[] = {
421 __ATTR_RO(config_rom),
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400422 __ATTR_RO(guid),
Stefan Richter0210b662009-05-23 00:03:29 +0200423 __ATTR_RO(units),
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400424 __ATTR_NULL,
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500425};
426
Stefan Richter53dca512008-12-14 21:47:04 +0100427static int read_rom(struct fw_device *device,
428 int generation, int index, u32 *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500429{
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200430 int rcode;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100431
432 /* device->node_id, accessed below, must not be older than generation */
433 smp_rmb();
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500434
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200435 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100436 device->node_id, generation, device->max_speed,
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200437 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
438 data, 4);
439 be32_to_cpus(data);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500440
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200441 return rcode;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500442}
443
Stefan Richter1dadff72008-03-02 19:35:42 +0100444#define READ_BIB_ROM_SIZE 256
445#define READ_BIB_STACK_SIZE 16
446
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100447/*
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 */
454static int read_bus_info_block(struct fw_device *device, int generation)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500455{
Stefan Richterc9755e12008-03-24 20:54:28 +0100456 u32 *rom, *stack, *old_rom, *new_rom;
Stefan Richter1dadff72008-03-02 19:35:42 +0100457 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øgsberg19a15b92006-12-19 19:58:31 -0500466
Stefan Richterf1397492007-06-10 21:31:36 +0200467 device->max_speed = SCODE_100;
468
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500469 /* First read the bus info block. */
470 for (i = 0; i < 5; i++) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100471 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100472 goto out;
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400473 /*
474 * As per IEEE1212 7.2, during power-up, devices can
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500475 * 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øgsbergc781c062007-05-07 20:33:32 -0400479 * retry mechanism will try again later.
480 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500481 if (i == 0 && rom[i] == 0)
Stefan Richter1dadff72008-03-02 19:35:42 +0100482 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500483 }
484
Stefan Richterf1397492007-06-10 21:31:36 +0200485 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 Richterf8d2dc32008-01-25 17:53:49 +0100506 if (read_rom(device, generation, 0, &dummy) ==
507 RCODE_COMPLETE)
Stefan Richterf1397492007-06-10 21:31:36 +0200508 break;
509 device->max_speed--;
510 }
511 }
512
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400513 /*
514 * Now parse the config rom. The config rom is a recursive
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500515 * 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øgsbergc781c062007-05-07 20:33:32 -0400518 * start things off.
519 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500520 length = i;
521 sp = 0;
522 stack[sp++] = 0xc0000005;
523 while (sp > 0) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400524 /*
525 * Pop the next block reference of the stack. The
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500526 * lower 24 bits is the offset into the config rom,
527 * the upper 8 bits are the type of the reference the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400528 * block.
529 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500530 key = stack[--sp];
531 i = key & 0xffffff;
Stefan Richter1dadff72008-03-02 19:35:42 +0100532 if (i >= READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400533 /*
534 * The reference points outside the standard
535 * config rom area, something's fishy.
536 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100537 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538
539 /* Read header quadlet for the block to get the length. */
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100540 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100541 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500542 end = i + (rom[i] >> 16) + 1;
543 i++;
Stefan Richter1dadff72008-03-02 19:35:42 +0100544 if (end > READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400545 /*
546 * This block extends outside standard config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500547 * area (and the array we're reading it
548 * into). That's broken, so ignore this
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400549 * device.
550 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100551 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500552
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400553 /*
554 * Now read in the block. If this is a directory
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500555 * block, check the entries as we read them to see if
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400556 * it references another block, and push it in that case.
557 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500558 while (i < end) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100559 if (read_rom(device, generation, i, &rom[i]) !=
560 RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100561 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500562 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
Stefan Richter1dadff72008-03-02 19:35:42 +0100563 sp < READ_BIB_STACK_SIZE)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564 stack[sp++] = i + rom[i];
565 i++;
566 }
567 if (length < i)
568 length = i;
569 }
570
Stefan Richterc9755e12008-03-24 20:54:28 +0100571 old_rom = device->config_rom;
572 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
573 if (new_rom == NULL)
Stefan Richter1dadff72008-03-02 19:35:42 +0100574 goto out;
Stefan Richterc9755e12008-03-24 20:54:28 +0100575
576 down_write(&fw_device_rwsem);
577 device->config_rom = new_rom;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500578 device->config_rom_length = length;
Stefan Richterc9755e12008-03-24 20:54:28 +0100579 up_write(&fw_device_rwsem);
580
581 kfree(old_rom);
Stefan Richter1dadff72008-03-02 19:35:42 +0100582 ret = 0;
Stefan Richter7889b602009-03-10 21:09:28 +0100583 device->cmc = rom[2] >> 30 & 1;
Stefan Richter1dadff72008-03-02 19:35:42 +0100584 out:
585 kfree(rom);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500586
Stefan Richter1dadff72008-03-02 19:35:42 +0100587 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500588}
589
590static void fw_unit_release(struct device *dev)
591{
592 struct fw_unit *unit = fw_unit(dev);
593
594 kfree(unit);
595}
596
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400597static struct device_type fw_unit_type = {
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400598 .uevent = fw_unit_uevent,
599 .release = fw_unit_release,
600};
601
Stefan Richter099d5412009-06-06 18:37:25 +0200602static bool is_fw_unit(struct device *dev)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500603{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400604 return dev->type == &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500605}
606
607static void create_units(struct fw_device *device)
608{
609 struct fw_csr_iterator ci;
610 struct fw_unit *unit;
611 int key, value, i;
612
613 i = 0;
614 fw_csr_iterator_init(&ci, &device->config_rom[5]);
615 while (fw_csr_iterator_next(&ci, &key, &value)) {
616 if (key != (CSR_UNIT | CSR_DIRECTORY))
617 continue;
618
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400619 /*
620 * Get the address of the unit directory and try to
621 * match the drivers id_tables against it.
622 */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400623 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500624 if (unit == NULL) {
625 fw_error("failed to allocate memory for unit\n");
626 continue;
627 }
628
629 unit->directory = ci.p + value - 1;
630 unit->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400631 unit->device.type = &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632 unit->device.parent = &device->device;
Kay Sieversa1f64812008-10-30 01:41:56 +0100633 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500634
Stefan Richtere5333db2009-05-22 23:16:27 +0200635 BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
636 ARRAY_SIZE(fw_unit_attributes) +
637 ARRAY_SIZE(config_rom_attributes));
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400638 init_fw_attribute_group(&unit->device,
639 fw_unit_attributes,
640 &unit->attribute_group);
Stefan Richtere5333db2009-05-22 23:16:27 +0200641
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400642 if (device_register(&unit->device) < 0)
643 goto skip_unit;
644
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400645 continue;
646
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400647 skip_unit:
648 kfree(unit);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500649 }
650}
651
652static int shutdown_unit(struct device *device, void *data)
653{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400654 device_unregister(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655
656 return 0;
657}
658
Stefan Richterc9755e12008-03-24 20:54:28 +0100659/*
660 * fw_device_rwsem acts as dual purpose mutex:
661 * - serializes accesses to fw_device_idr,
662 * - serializes accesses to fw_device.config_rom/.config_rom_length and
663 * fw_unit.directory, unless those accesses happen at safe occasions
664 */
665DECLARE_RWSEM(fw_device_rwsem);
666
Stefan Richterd6053e02008-11-24 20:40:00 +0100667DEFINE_IDR(fw_device_idr);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500668int fw_cdev_major;
669
Stefan Richter96b19062008-02-02 15:01:09 +0100670struct fw_device *fw_device_get_by_devt(dev_t devt)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500671{
672 struct fw_device *device;
673
Stefan Richterc9755e12008-03-24 20:54:28 +0100674 down_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500675 device = idr_find(&fw_device_idr, MINOR(devt));
Stefan Richter96b19062008-02-02 15:01:09 +0100676 if (device)
677 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100678 up_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500679
680 return device;
681}
682
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400683/*
684 * These defines control the retry behavior for reading the config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500685 * rom. It shouldn't be necessary to tweak these; if the device
686 * doesn't respond to a config rom read within 10 seconds, it's not
687 * going to respond at all. As for the initial delay, a lot of
688 * devices will be able to respond within half a second after bus
689 * reset. On the other hand, it's not really worth being more
690 * aggressive than that, since it scales pretty well; if 10 devices
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400691 * are plugged in, they're all getting read within one second.
692 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500693
Kristian Høgsbergc5dfd0a2007-03-27 01:43:43 -0400694#define MAX_RETRIES 10
695#define RETRY_DELAY (3 * HZ)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500696#define INITIAL_DELAY (HZ / 2)
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100697#define SHUTDOWN_DELAY (2 * HZ)
698
699static void fw_device_shutdown(struct work_struct *work)
700{
701 struct fw_device *device =
702 container_of(work, struct fw_device, work.work);
703 int minor = MINOR(device->device.devt);
704
Stefan Richtere747a5c2009-01-24 20:35:38 +0100705 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
706 && !list_empty(&device->card->link)) {
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100707 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
708 return;
709 }
710
711 if (atomic_cmpxchg(&device->state,
712 FW_DEVICE_GONE,
713 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
714 return;
715
716 fw_device_cdev_remove(device);
717 device_for_each_child(&device->device, NULL, shutdown_unit);
718 device_unregister(&device->device);
719
720 down_write(&fw_device_rwsem);
721 idr_remove(&fw_device_idr, minor);
722 up_write(&fw_device_rwsem);
723
724 fw_device_put(device);
725}
726
Stefan Richteraed80892009-01-17 22:45:54 +0100727static void fw_device_release(struct device *dev)
728{
729 struct fw_device *device = fw_device(dev);
730 struct fw_card *card = device->card;
731 unsigned long flags;
732
733 /*
734 * Take the card lock so we don't set this to NULL while a
735 * FW_NODE_UPDATED callback is being handled or while the
736 * bus manager work looks at this node.
737 */
738 spin_lock_irqsave(&card->lock, flags);
739 device->node->data = NULL;
740 spin_unlock_irqrestore(&card->lock, flags);
741
742 fw_node_put(device->node);
743 kfree(device->config_rom);
744 kfree(device);
745 fw_card_put(card);
746}
747
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100748static struct device_type fw_device_type = {
Stefan Richteraed80892009-01-17 22:45:54 +0100749 .release = fw_device_release,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100750};
751
Stefan Richter099d5412009-06-06 18:37:25 +0200752static bool is_fw_device(struct device *dev)
753{
754 return dev->type == &fw_device_type;
755}
756
Stefan Richteraed80892009-01-17 22:45:54 +0100757static int update_unit(struct device *dev, void *data)
758{
759 struct fw_unit *unit = fw_unit(dev);
760 struct fw_driver *driver = (struct fw_driver *)dev->driver;
761
762 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
763 down(&dev->sem);
764 driver->update(unit);
765 up(&dev->sem);
766 }
767
768 return 0;
769}
770
771static void fw_device_update(struct work_struct *work)
772{
773 struct fw_device *device =
774 container_of(work, struct fw_device, work.work);
775
776 fw_device_cdev_update(device);
777 device_for_each_child(&device->device, NULL, update_unit);
778}
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100779
780/*
781 * If a device was pending for deletion because its node went away but its
782 * bus info block and root directory header matches that of a newly discovered
783 * device, revive the existing fw_device.
784 * The newly allocated fw_device becomes obsolete instead.
785 */
786static int lookup_existing_device(struct device *dev, void *data)
787{
788 struct fw_device *old = fw_device(dev);
789 struct fw_device *new = data;
790 struct fw_card *card = new->card;
791 int match = 0;
792
Stefan Richter099d5412009-06-06 18:37:25 +0200793 if (!is_fw_device(dev))
794 return 0;
795
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100796 down_read(&fw_device_rwsem); /* serialize config_rom access */
797 spin_lock_irq(&card->lock); /* serialize node access */
798
799 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
800 atomic_cmpxchg(&old->state,
801 FW_DEVICE_GONE,
802 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
803 struct fw_node *current_node = new->node;
804 struct fw_node *obsolete_node = old->node;
805
806 new->node = obsolete_node;
807 new->node->data = new;
808 old->node = current_node;
809 old->node->data = old;
810
811 old->max_speed = new->max_speed;
812 old->node_id = current_node->node_id;
813 smp_wmb(); /* update node_id before generation */
814 old->generation = card->generation;
815 old->config_rom_retries = 0;
816 fw_notify("rediscovered device %s\n", dev_name(dev));
817
818 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
819 schedule_delayed_work(&old->work, 0);
820
821 if (current_node == card->root_node)
822 fw_schedule_bm_work(card, 0);
823
824 match = 1;
825 }
826
827 spin_unlock_irq(&card->lock);
828 up_read(&fw_device_rwsem);
829
830 return match;
831}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500832
Stefan Richter7889b602009-03-10 21:09:28 +0100833enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
834
Stefan Richter099d5412009-06-06 18:37:25 +0200835static void set_broadcast_channel(struct fw_device *device, int generation)
Stefan Richter7889b602009-03-10 21:09:28 +0100836{
837 struct fw_card *card = device->card;
838 __be32 data;
839 int rcode;
840
841 if (!card->broadcast_channel_allocated)
842 return;
843
844 if (device->bc_implemented == BC_UNKNOWN) {
845 rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
846 device->node_id, generation, device->max_speed,
847 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
848 &data, 4);
849 switch (rcode) {
850 case RCODE_COMPLETE:
851 if (data & cpu_to_be32(1 << 31)) {
852 device->bc_implemented = BC_IMPLEMENTED;
853 break;
854 }
855 /* else fall through to case address error */
856 case RCODE_ADDRESS_ERROR:
857 device->bc_implemented = BC_UNIMPLEMENTED;
858 }
859 }
860
861 if (device->bc_implemented == BC_IMPLEMENTED) {
862 data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
863 BROADCAST_CHANNEL_VALID);
864 fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
865 device->node_id, generation, device->max_speed,
866 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
867 &data, 4);
868 }
869}
870
Stefan Richter099d5412009-06-06 18:37:25 +0200871int fw_device_set_broadcast_channel(struct device *dev, void *gen)
872{
873 if (is_fw_device(dev))
874 set_broadcast_channel(fw_device(dev), (long)gen);
875
876 return 0;
877}
878
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500879static void fw_device_init(struct work_struct *work)
880{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500881 struct fw_device *device =
882 container_of(work, struct fw_device, work.work);
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100883 struct device *revived_dev;
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100884 int minor, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500885
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400886 /*
887 * All failure paths here set node->data to NULL, so that we
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500888 * don't try to do device_for_each_child() on a kfree()'d
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400889 * device.
890 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500891
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100892 if (read_bus_info_block(device, device->generation) < 0) {
Stefan Richter855c6032008-02-27 22:14:27 +0100893 if (device->config_rom_retries < MAX_RETRIES &&
894 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500895 device->config_rom_retries++;
896 schedule_delayed_work(&device->work, RETRY_DELAY);
897 } else {
Stefan Richter907293d2007-01-23 21:11:43 +0100898 fw_notify("giving up on config rom for node id %x\n",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500899 device->node_id);
Kristian Høgsberg931c4832007-01-26 00:38:45 -0500900 if (device->node == device->card->root_node)
Jay Fenlason0fa19862008-11-29 17:44:57 +0100901 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500902 fw_device_release(&device->device);
903 }
904 return;
905 }
906
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100907 revived_dev = device_find_child(device->card->device,
908 device, lookup_existing_device);
909 if (revived_dev) {
910 put_device(revived_dev);
911 fw_device_release(&device->device);
912
913 return;
914 }
915
Stefan Richter62305822009-01-09 20:49:37 +0100916 device_initialize(&device->device);
Stefan Richter96b19062008-02-02 15:01:09 +0100917
918 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100919 down_write(&fw_device_rwsem);
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100920 ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
Stefan Richter62305822009-01-09 20:49:37 +0100921 idr_get_new(&fw_device_idr, device, &minor) :
922 -ENOMEM;
Stefan Richterc9755e12008-03-24 20:54:28 +0100923 up_write(&fw_device_rwsem);
Stefan Richter96b19062008-02-02 15:01:09 +0100924
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100925 if (ret < 0)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500926 goto error;
927
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500928 device->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400929 device->device.type = &fw_device_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500930 device->device.parent = device->card->device;
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500931 device->device.devt = MKDEV(fw_cdev_major, minor);
Kay Sieversa1f64812008-10-30 01:41:56 +0100932 dev_set_name(&device->device, "fw%d", minor);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500933
Stefan Richtere5333db2009-05-22 23:16:27 +0200934 BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
935 ARRAY_SIZE(fw_device_attributes) +
936 ARRAY_SIZE(config_rom_attributes));
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400937 init_fw_attribute_group(&device->device,
938 fw_device_attributes,
939 &device->attribute_group);
Stefan Richtere5333db2009-05-22 23:16:27 +0200940
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500941 if (device_add(&device->device)) {
942 fw_error("Failed to add device.\n");
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500943 goto error_with_cdev;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500944 }
945
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500946 create_units(device);
947
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400948 /*
949 * Transition the device to running state. If it got pulled
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500950 * out from under us while we did the intialization work, we
951 * have to shut down the device again here. Normally, though,
952 * fw_node_event will be responsible for shutting it down when
953 * necessary. We have to use the atomic cmpxchg here to avoid
954 * racing with the FW_NODE_DESTROYED case in
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400955 * fw_node_event().
956 */
Stefan Richter641f8792007-01-27 10:34:55 +0100957 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100958 FW_DEVICE_INITIALIZING,
959 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
960 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
961 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterfa6e6972008-02-03 23:03:00 +0100962 } else {
963 if (device->config_rom_retries)
964 fw_notify("created device %s: GUID %08x%08x, S%d00, "
965 "%d config ROM retries\n",
Kay Sieversa1f64812008-10-30 01:41:56 +0100966 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +0100967 device->config_rom[3], device->config_rom[4],
968 1 << device->max_speed,
969 device->config_rom_retries);
970 else
971 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
Kay Sieversa1f64812008-10-30 01:41:56 +0100972 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +0100973 device->config_rom[3], device->config_rom[4],
974 1 << device->max_speed);
Stefan Richterc9755e12008-03-24 20:54:28 +0100975 device->config_rom_retries = 0;
Stefan Richter7889b602009-03-10 21:09:28 +0100976
Stefan Richter099d5412009-06-06 18:37:25 +0200977 set_broadcast_channel(device, device->generation);
Stefan Richterfa6e6972008-02-03 23:03:00 +0100978 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500979
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400980 /*
981 * Reschedule the IRM work if we just finished reading the
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500982 * root node config rom. If this races with a bus reset we
983 * just end up running the IRM work a couple of extra times -
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400984 * pretty harmless.
985 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500986 if (device->node == device->card->root_node)
Jay Fenlason0fa19862008-11-29 17:44:57 +0100987 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500988
989 return;
990
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500991 error_with_cdev:
Stefan Richterc9755e12008-03-24 20:54:28 +0100992 down_write(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500993 idr_remove(&fw_device_idr, minor);
Stefan Richterc9755e12008-03-24 20:54:28 +0100994 up_write(&fw_device_rwsem);
Stefan Richter373b2ed2007-03-04 14:45:18 +0100995 error:
Stefan Richter96b19062008-02-02 15:01:09 +0100996 fw_device_put(device); /* fw_device_idr's reference */
997
998 put_device(&device->device); /* our reference */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500999}
1000
Stefan Richterc9755e12008-03-24 20:54:28 +01001001enum {
1002 REREAD_BIB_ERROR,
1003 REREAD_BIB_GONE,
1004 REREAD_BIB_UNCHANGED,
1005 REREAD_BIB_CHANGED,
1006};
1007
1008/* Reread and compare bus info block and header of root directory */
1009static int reread_bus_info_block(struct fw_device *device, int generation)
1010{
1011 u32 q;
1012 int i;
1013
1014 for (i = 0; i < 6; i++) {
1015 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
1016 return REREAD_BIB_ERROR;
1017
1018 if (i == 0 && q == 0)
1019 return REREAD_BIB_GONE;
1020
Stefan Richterd01b0172009-01-17 22:45:54 +01001021 if (q != device->config_rom[i])
Stefan Richterc9755e12008-03-24 20:54:28 +01001022 return REREAD_BIB_CHANGED;
1023 }
1024
1025 return REREAD_BIB_UNCHANGED;
1026}
1027
1028static void fw_device_refresh(struct work_struct *work)
1029{
1030 struct fw_device *device =
1031 container_of(work, struct fw_device, work.work);
1032 struct fw_card *card = device->card;
1033 int node_id = device->node_id;
1034
1035 switch (reread_bus_info_block(device, device->generation)) {
1036 case REREAD_BIB_ERROR:
1037 if (device->config_rom_retries < MAX_RETRIES / 2 &&
1038 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1039 device->config_rom_retries++;
1040 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
1041
1042 return;
1043 }
1044 goto give_up;
1045
1046 case REREAD_BIB_GONE:
1047 goto gone;
1048
1049 case REREAD_BIB_UNCHANGED:
1050 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001051 FW_DEVICE_INITIALIZING,
1052 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +01001053 goto gone;
1054
1055 fw_device_update(work);
1056 device->config_rom_retries = 0;
1057 goto out;
1058
1059 case REREAD_BIB_CHANGED:
1060 break;
1061 }
1062
1063 /*
1064 * Something changed. We keep things simple and don't investigate
1065 * further. We just destroy all previous units and create new ones.
1066 */
1067 device_for_each_child(&device->device, NULL, shutdown_unit);
1068
1069 if (read_bus_info_block(device, device->generation) < 0) {
1070 if (device->config_rom_retries < MAX_RETRIES &&
1071 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1072 device->config_rom_retries++;
1073 schedule_delayed_work(&device->work, RETRY_DELAY);
1074
1075 return;
1076 }
1077 goto give_up;
1078 }
1079
1080 create_units(device);
1081
Stefan Richter0210b662009-05-23 00:03:29 +02001082 /* Userspace may want to re-read attributes. */
1083 kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1084
Stefan Richterc9755e12008-03-24 20:54:28 +01001085 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001086 FW_DEVICE_INITIALIZING,
1087 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +01001088 goto gone;
1089
Kay Sieversa1f64812008-10-30 01:41:56 +01001090 fw_notify("refreshed device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +01001091 device->config_rom_retries = 0;
1092 goto out;
1093
1094 give_up:
Kay Sieversa1f64812008-10-30 01:41:56 +01001095 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +01001096 gone:
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001097 atomic_set(&device->state, FW_DEVICE_GONE);
1098 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1099 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +01001100 out:
1101 if (node_id == card->root_node->node_id)
Jay Fenlason0fa19862008-11-29 17:44:57 +01001102 fw_schedule_bm_work(card, 0);
Stefan Richterc9755e12008-03-24 20:54:28 +01001103}
1104
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001105void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1106{
1107 struct fw_device *device;
1108
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001109 switch (event) {
1110 case FW_NODE_CREATED:
1111 case FW_NODE_LINK_ON:
1112 if (!node->link_on)
1113 break;
Stefan Richterc9755e12008-03-24 20:54:28 +01001114 create:
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001115 device = kzalloc(sizeof(*device), GFP_ATOMIC);
1116 if (device == NULL)
1117 break;
1118
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001119 /*
1120 * Do minimal intialization of the device here, the
Stefan Richter62305822009-01-09 20:49:37 +01001121 * rest will happen in fw_device_init().
1122 *
1123 * Attention: A lot of things, even fw_device_get(),
1124 * cannot be done before fw_device_init() finished!
1125 * You can basically just check device->state and
1126 * schedule work until then, but only while holding
1127 * card->lock.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001128 */
Stefan Richter641f8792007-01-27 10:34:55 +01001129 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
Stefan Richter459f7922008-05-24 16:50:22 +02001130 device->card = fw_card_get(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001131 device->node = fw_node_get(node);
1132 device->node_id = node->node_id;
1133 device->generation = card->generation;
Stefan Richter92368892009-05-13 21:42:14 +02001134 device->is_local = node == card->local_node;
Stefan Richterd67cfb92008-10-05 10:37:11 +02001135 mutex_init(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -05001136 INIT_LIST_HEAD(&device->client_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001137
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001138 /*
1139 * Set the node data to point back to this device so
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001140 * FW_NODE_UPDATED callbacks can update the node_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001141 * and generation for the device.
1142 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001143 node->data = device;
1144
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001145 /*
1146 * Many devices are slow to respond after bus resets,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001147 * especially if they are bus powered and go through
1148 * power-up after getting plugged in. We schedule the
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001149 * first config rom scan half a second after bus reset.
1150 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001151 INIT_DELAYED_WORK(&device->work, fw_device_init);
1152 schedule_delayed_work(&device->work, INITIAL_DELAY);
1153 break;
1154
Stefan Richterc9755e12008-03-24 20:54:28 +01001155 case FW_NODE_INITIATED_RESET:
1156 device = node->data;
1157 if (device == NULL)
1158 goto create;
1159
1160 device->node_id = node->node_id;
1161 smp_wmb(); /* update node_id before generation */
1162 device->generation = card->generation;
1163 if (atomic_cmpxchg(&device->state,
1164 FW_DEVICE_RUNNING,
1165 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1166 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1167 schedule_delayed_work(&device->work,
Stefan Richter92368892009-05-13 21:42:14 +02001168 device->is_local ? 0 : INITIAL_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +01001169 }
1170 break;
1171
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001172 case FW_NODE_UPDATED:
1173 if (!node->link_on || node->data == NULL)
1174 break;
1175
1176 device = node->data;
1177 device->node_id = node->node_id;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +01001178 smp_wmb(); /* update node_id before generation */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001179 device->generation = card->generation;
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001180 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1181 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1182 schedule_delayed_work(&device->work, 0);
1183 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001184 break;
1185
1186 case FW_NODE_DESTROYED:
1187 case FW_NODE_LINK_OFF:
1188 if (!node->data)
1189 break;
1190
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001191 /*
1192 * Destroy the device associated with the node. There
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001193 * are two cases here: either the device is fully
1194 * initialized (FW_DEVICE_RUNNING) or we're in the
1195 * process of reading its config rom
1196 * (FW_DEVICE_INITIALIZING). If it is fully
1197 * initialized we can reuse device->work to schedule a
1198 * full fw_device_shutdown(). If not, there's work
1199 * scheduled to read it's config rom, and we just put
1200 * the device in shutdown state to have that code fail
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001201 * to create the device.
1202 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001203 device = node->data;
Stefan Richter641f8792007-01-27 10:34:55 +01001204 if (atomic_xchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001205 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001206 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
Stefan Richtere747a5c2009-01-24 20:35:38 +01001207 schedule_delayed_work(&device->work,
1208 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001209 }
1210 break;
1211 }
1212}