1 /* -*- c-basic-offset: 8 -*-
3 * fw-device.c - Device probing and sysfs code.
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/module.h>
23 #include <linux/wait.h>
24 #include <linux/errno.h>
25 #include <linux/kthread.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include "fw-transaction.h"
29 #include "fw-topology.h"
30 #include "fw-device.h"
32 void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
35 ci->end = ci->p + (p[0] >> 16);
37 EXPORT_SYMBOL(fw_csr_iterator_init);
39 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
42 *value = *ci->p & 0xffffff;
44 return ci->p++ < ci->end;
46 EXPORT_SYMBOL(fw_csr_iterator_next);
48 static int is_fw_unit(struct device *dev);
50 static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
52 struct fw_csr_iterator ci;
53 int key, value, match;
56 fw_csr_iterator_init(&ci, directory);
57 while (fw_csr_iterator_next(&ci, &key, &value)) {
58 if (key == CSR_VENDOR && value == id->vendor)
59 match |= FW_MATCH_VENDOR;
60 if (key == CSR_MODEL && value == id->model)
61 match |= FW_MATCH_MODEL;
62 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
63 match |= FW_MATCH_SPECIFIER_ID;
64 if (key == CSR_VERSION && value == id->version)
65 match |= FW_MATCH_VERSION;
68 return (match & id->match_flags) == id->match_flags;
71 static int fw_unit_match(struct device *dev, struct device_driver *drv)
73 struct fw_unit *unit = fw_unit(dev);
74 struct fw_driver *driver = fw_driver(drv);
77 /* We only allow binding to fw_units. */
81 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
82 if (match_unit_directory(unit->directory, &driver->id_table[i]))
89 static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
91 struct fw_device *device = fw_device(unit->device.parent);
92 struct fw_csr_iterator ci;
100 fw_csr_iterator_init(&ci, &device->config_rom[5]);
101 while (fw_csr_iterator_next(&ci, &key, &value)) {
112 fw_csr_iterator_init(&ci, unit->directory);
113 while (fw_csr_iterator_next(&ci, &key, &value)) {
115 case CSR_SPECIFIER_ID:
116 specifier_id = value;
124 return snprintf(buffer, buffer_size,
125 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
126 vendor, model, specifier_id, version);
130 fw_unit_uevent(struct device *dev, char **envp, int num_envp,
131 char *buffer, int buffer_size)
133 struct fw_unit *unit = fw_unit(dev);
138 if (!is_fw_unit(dev))
141 get_modalias(unit, modalias, sizeof modalias);
143 if (add_uevent_var(envp, num_envp, &i,
144 buffer, buffer_size, &length,
145 "MODALIAS=%s", modalias))
154 struct bus_type fw_bus_type = {
156 .match = fw_unit_match,
157 .uevent = fw_unit_uevent,
159 EXPORT_SYMBOL(fw_bus_type);
161 extern struct fw_device *fw_device_get(struct fw_device *device)
163 get_device(&device->device);
168 extern void fw_device_put(struct fw_device *device)
170 put_device(&device->device);
173 static void fw_device_release(struct device *dev)
175 struct fw_device *device = fw_device(dev);
178 /* Take the card lock so we don't set this to NULL while a
179 * FW_NODE_UPDATED callback is being handled. */
180 spin_lock_irqsave(&device->card->lock, flags);
181 device->node->data = NULL;
182 spin_unlock_irqrestore(&device->card->lock, flags);
184 fw_node_put(device->node);
185 fw_card_put(device->card);
186 kfree(device->config_rom);
190 int fw_device_enable_phys_dma(struct fw_device *device)
192 return device->card->driver->enable_phys_dma(device->card,
196 EXPORT_SYMBOL(fw_device_enable_phys_dma);
199 show_modalias_attribute(struct device *dev,
200 struct device_attribute *attr, char *buf)
202 struct fw_unit *unit = fw_unit(dev);
205 length = get_modalias(unit, buf, PAGE_SIZE);
206 strcpy(buf + length, "\n");
211 static struct device_attribute modalias_attribute = {
212 .attr = { .name = "modalias", .mode = S_IRUGO, },
213 .show = show_modalias_attribute,
217 show_config_rom_attribute(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct fw_device *device = fw_device(dev);
222 memcpy(buf, device->config_rom, device->config_rom_length * 4);
224 return device->config_rom_length * 4;
227 static struct device_attribute config_rom_attribute = {
228 .attr = {.name = "config_rom", .mode = S_IRUGO,},
229 .show = show_config_rom_attribute,
232 struct read_quadlet_callback_data {
233 struct completion done;
239 complete_transaction(struct fw_card *card, int rcode,
240 void *payload, size_t length, void *data)
242 struct read_quadlet_callback_data *callback_data = data;
244 if (rcode == RCODE_COMPLETE)
245 callback_data->data = be32_to_cpu(*(__be32 *)payload);
246 callback_data->rcode = rcode;
247 complete(&callback_data->done);
250 static int read_rom(struct fw_device *device, int index, u32 * data)
252 struct read_quadlet_callback_data callback_data;
253 struct fw_transaction t;
256 init_completion(&callback_data.done);
258 offset = 0xfffff0000400ULL + index * 4;
259 fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
261 device->generation, SCODE_100,
262 offset, NULL, 4, complete_transaction, &callback_data);
264 wait_for_completion(&callback_data.done);
266 *data = callback_data.data;
268 return callback_data.rcode;
271 static int read_bus_info_block(struct fw_device *device)
274 u32 stack[16], sp, key;
277 /* First read the bus info block. */
278 for (i = 0; i < 5; i++) {
279 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
281 /* As per IEEE1212 7.2, during power-up, devices can
282 * reply with a 0 for the first quadlet of the config
283 * rom to indicate that they are booting (for example,
284 * if the firmware is on the disk of a external
285 * harddisk). In that case we just fail, and the
286 * retry mechanism will try again later. */
287 if (i == 0 && rom[i] == 0)
291 /* Now parse the config rom. The config rom is a recursive
292 * directory structure so we parse it using a stack of
293 * references to the blocks that make up the structure. We
294 * push a reference to the root directory on the stack to
295 * start things off. */
298 stack[sp++] = 0xc0000005;
300 /* Pop the next block reference of the stack. The
301 * lower 24 bits is the offset into the config rom,
302 * the upper 8 bits are the type of the reference the
306 if (i >= ARRAY_SIZE(rom))
307 /* The reference points outside the standard
308 * config rom area, something's fishy. */
311 /* Read header quadlet for the block to get the length. */
312 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
314 end = i + (rom[i] >> 16) + 1;
316 if (end > ARRAY_SIZE(rom))
317 /* This block extends outside standard config
318 * area (and the array we're reading it
319 * into). That's broken, so ignore this
323 /* Now read in the block. If this is a directory
324 * block, check the entries as we read them to see if
325 * it references another block, and push it in that case. */
327 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
329 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
330 sp < ARRAY_SIZE(stack))
331 stack[sp++] = i + rom[i];
338 device->config_rom = kmalloc(length * 4, GFP_KERNEL);
339 if (device->config_rom == NULL)
341 memcpy(device->config_rom, rom, length * 4);
342 device->config_rom_length = length;
347 static void fw_unit_release(struct device *dev)
349 struct fw_unit *unit = fw_unit(dev);
354 static int is_fw_unit(struct device *dev)
356 return dev->release == fw_unit_release;
359 static void create_units(struct fw_device *device)
361 struct fw_csr_iterator ci;
362 struct fw_unit *unit;
366 fw_csr_iterator_init(&ci, &device->config_rom[5]);
367 while (fw_csr_iterator_next(&ci, &key, &value)) {
368 if (key != (CSR_UNIT | CSR_DIRECTORY))
371 /* Get the address of the unit directory and try to
372 * match the drivers id_tables against it. */
373 unit = kzalloc(sizeof *unit, GFP_KERNEL);
375 fw_error("failed to allocate memory for unit\n");
379 unit->directory = ci.p + value - 1;
380 unit->device.bus = &fw_bus_type;
381 unit->device.release = fw_unit_release;
382 unit->device.parent = &device->device;
383 snprintf(unit->device.bus_id, sizeof unit->device.bus_id,
384 "%s.%d", device->device.bus_id, i++);
386 if (device_register(&unit->device) < 0) {
391 if (device_create_file(&unit->device, &modalias_attribute) < 0) {
392 device_unregister(&unit->device);
398 static int shutdown_unit(struct device *device, void *data)
400 struct fw_unit *unit = fw_unit(device);
402 if (is_fw_unit(device)) {
403 device_remove_file(&unit->device, &modalias_attribute);
404 device_unregister(&unit->device);
410 static void fw_device_shutdown(struct work_struct *work)
412 struct fw_device *device =
413 container_of(work, struct fw_device, work.work);
415 device_remove_file(&device->device, &config_rom_attribute);
416 cdev_del(&device->cdev);
417 unregister_chrdev_region(device->device.devt, 1);
418 device_for_each_child(&device->device, NULL, shutdown_unit);
419 device_unregister(&device->device);
422 /* These defines control the retry behavior for reading the config
423 * rom. It shouldn't be necessary to tweak these; if the device
424 * doesn't respond to a config rom read within 10 seconds, it's not
425 * going to respond at all. As for the initial delay, a lot of
426 * devices will be able to respond within half a second after bus
427 * reset. On the other hand, it's not really worth being more
428 * aggressive than that, since it scales pretty well; if 10 devices
429 * are plugged in, they're all getting read within one second. */
431 #define MAX_RETRIES 5
432 #define RETRY_DELAY (2 * HZ)
433 #define INITIAL_DELAY (HZ / 2)
435 static void fw_device_init(struct work_struct *work)
438 struct fw_device *device =
439 container_of(work, struct fw_device, work.work);
441 /* All failure paths here set node->data to NULL, so that we
442 * don't try to do device_for_each_child() on a kfree()'d
445 if (read_bus_info_block(device) < 0) {
446 if (device->config_rom_retries < MAX_RETRIES) {
447 device->config_rom_retries++;
448 schedule_delayed_work(&device->work, RETRY_DELAY);
450 fw_notify("giving up on config rom for node id %x\n",
452 if (device->node == device->card->root_node)
453 schedule_delayed_work(&device->card->work, 0);
454 fw_device_release(&device->device);
459 device->device.bus = &fw_bus_type;
460 device->device.release = fw_device_release;
461 device->device.parent = device->card->device;
462 snprintf(device->device.bus_id, sizeof device->device.bus_id,
465 if (alloc_chrdev_region(&device->device.devt, 0, 1, "fw")) {
466 fw_error("Failed to register char device region.\n");
470 cdev_init(&device->cdev, &fw_device_ops);
471 device->cdev.owner = THIS_MODULE;
472 kobject_set_name(&device->cdev.kobj, device->device.bus_id);
473 if (cdev_add(&device->cdev, device->device.devt, 1)) {
474 fw_error("Failed to register char device.\n");
478 if (device_add(&device->device)) {
479 fw_error("Failed to add device.\n");
483 if (device_create_file(&device->device, &config_rom_attribute) < 0) {
484 fw_error("Failed to create config rom file.\n");
485 goto error_with_device;
488 create_units(device);
490 /* Transition the device to running state. If it got pulled
491 * out from under us while we did the intialization work, we
492 * have to shut down the device again here. Normally, though,
493 * fw_node_event will be responsible for shutting it down when
494 * necessary. We have to use the atomic cmpxchg here to avoid
495 * racing with the FW_NODE_DESTROYED case in
496 * fw_node_event(). */
497 if (atomic_cmpxchg(&device->state,
498 FW_DEVICE_INITIALIZING,
499 FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
500 fw_device_shutdown(&device->work.work);
502 fw_notify("created new fw device %s (%d config rom retries)\n",
503 device->device.bus_id, device->config_rom_retries);
505 /* Reschedule the IRM work if we just finished reading the
506 * root node config rom. If this races with a bus reset we
507 * just end up running the IRM work a couple of extra times -
508 * pretty harmless. */
509 if (device->node == device->card->root_node)
510 schedule_delayed_work(&device->card->work, 0);
515 device_del(&device->device);
517 cdev_del(&device->cdev);
518 unregister_chrdev_region(device->device.devt, 1);
519 put_device(&device->device);
522 static int update_unit(struct device *dev, void *data)
524 struct fw_unit *unit = fw_unit(dev);
525 struct fw_driver *driver = (struct fw_driver *)dev->driver;
527 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL)
528 driver->update(unit);
533 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
535 struct fw_device *device;
538 case FW_NODE_CREATED:
539 case FW_NODE_LINK_ON:
543 device = kzalloc(sizeof(*device), GFP_ATOMIC);
547 /* Do minimal intialization of the device here, the
548 * rest will happen in fw_device_init(). We need the
549 * card and node so we can read the config rom and we
550 * need to do device_initialize() now so
551 * device_for_each_child() in FW_NODE_UPDATED is
552 * doesn't freak out. */
553 device_initialize(&device->device);
554 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
555 device->card = fw_card_get(card);
556 device->node = fw_node_get(node);
557 device->node_id = node->node_id;
558 device->generation = card->generation;
560 /* Set the node data to point back to this device so
561 * FW_NODE_UPDATED callbacks can update the node_id
562 * and generation for the device. */
565 /* Many devices are slow to respond after bus resets,
566 * especially if they are bus powered and go through
567 * power-up after getting plugged in. We schedule the
568 * first config rom scan half a second after bus reset. */
569 INIT_DELAYED_WORK(&device->work, fw_device_init);
570 schedule_delayed_work(&device->work, INITIAL_DELAY);
573 case FW_NODE_UPDATED:
574 if (!node->link_on || node->data == NULL)
578 device->node_id = node->node_id;
579 device->generation = card->generation;
580 device_for_each_child(&device->device, NULL, update_unit);
583 case FW_NODE_DESTROYED:
584 case FW_NODE_LINK_OFF:
588 /* Destroy the device associated with the node. There
589 * are two cases here: either the device is fully
590 * initialized (FW_DEVICE_RUNNING) or we're in the
591 * process of reading its config rom
592 * (FW_DEVICE_INITIALIZING). If it is fully
593 * initialized we can reuse device->work to schedule a
594 * full fw_device_shutdown(). If not, there's work
595 * scheduled to read it's config rom, and we just put
596 * the device in shutdown state to have that code fail
597 * to create the device. */
599 if (atomic_xchg(&device->state,
600 FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
601 INIT_DELAYED_WORK(&device->work, fw_device_shutdown);
602 schedule_delayed_work(&device->work, 0);