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 <linux/idr.h>
29 #include "fw-transaction.h"
30 #include "fw-topology.h"
31 #include "fw-device.h"
33 void fw_csr_iterator_init(struct fw_csr_iterator
*ci
, u32
* p
)
36 ci
->end
= ci
->p
+ (p
[0] >> 16);
38 EXPORT_SYMBOL(fw_csr_iterator_init
);
40 int fw_csr_iterator_next(struct fw_csr_iterator
*ci
, int *key
, int *value
)
43 *value
= *ci
->p
& 0xffffff;
45 return ci
->p
++ < ci
->end
;
47 EXPORT_SYMBOL(fw_csr_iterator_next
);
49 static int is_fw_unit(struct device
*dev
);
51 static int match_unit_directory(u32
* directory
, const struct fw_device_id
*id
)
53 struct fw_csr_iterator ci
;
54 int key
, value
, match
;
57 fw_csr_iterator_init(&ci
, directory
);
58 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
59 if (key
== CSR_VENDOR
&& value
== id
->vendor
)
60 match
|= FW_MATCH_VENDOR
;
61 if (key
== CSR_MODEL
&& value
== id
->model
)
62 match
|= FW_MATCH_MODEL
;
63 if (key
== CSR_SPECIFIER_ID
&& value
== id
->specifier_id
)
64 match
|= FW_MATCH_SPECIFIER_ID
;
65 if (key
== CSR_VERSION
&& value
== id
->version
)
66 match
|= FW_MATCH_VERSION
;
69 return (match
& id
->match_flags
) == id
->match_flags
;
72 static int fw_unit_match(struct device
*dev
, struct device_driver
*drv
)
74 struct fw_unit
*unit
= fw_unit(dev
);
75 struct fw_driver
*driver
= fw_driver(drv
);
78 /* We only allow binding to fw_units. */
82 for (i
= 0; driver
->id_table
[i
].match_flags
!= 0; i
++) {
83 if (match_unit_directory(unit
->directory
, &driver
->id_table
[i
]))
90 static int get_modalias(struct fw_unit
*unit
, char *buffer
, size_t buffer_size
)
92 struct fw_device
*device
= fw_device(unit
->device
.parent
);
93 struct fw_csr_iterator ci
;
101 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
102 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
113 fw_csr_iterator_init(&ci
, unit
->directory
);
114 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
116 case CSR_SPECIFIER_ID
:
117 specifier_id
= value
;
125 return snprintf(buffer
, buffer_size
,
126 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
127 vendor
, model
, specifier_id
, version
);
131 fw_unit_uevent(struct device
*dev
, char **envp
, int num_envp
,
132 char *buffer
, int buffer_size
)
134 struct fw_unit
*unit
= fw_unit(dev
);
139 if (!is_fw_unit(dev
))
142 get_modalias(unit
, modalias
, sizeof modalias
);
144 if (add_uevent_var(envp
, num_envp
, &i
,
145 buffer
, buffer_size
, &length
,
146 "MODALIAS=%s", modalias
))
155 struct bus_type fw_bus_type
= {
157 .match
= fw_unit_match
,
158 .uevent
= fw_unit_uevent
,
160 EXPORT_SYMBOL(fw_bus_type
);
162 extern struct fw_device
*fw_device_get(struct fw_device
*device
)
164 get_device(&device
->device
);
169 extern void fw_device_put(struct fw_device
*device
)
171 put_device(&device
->device
);
174 static void fw_device_release(struct device
*dev
)
176 struct fw_device
*device
= fw_device(dev
);
179 /* Take the card lock so we don't set this to NULL while a
180 * FW_NODE_UPDATED callback is being handled. */
181 spin_lock_irqsave(&device
->card
->lock
, flags
);
182 device
->node
->data
= NULL
;
183 spin_unlock_irqrestore(&device
->card
->lock
, flags
);
185 fw_node_put(device
->node
);
186 fw_card_put(device
->card
);
187 kfree(device
->config_rom
);
191 int fw_device_enable_phys_dma(struct fw_device
*device
)
193 return device
->card
->driver
->enable_phys_dma(device
->card
,
197 EXPORT_SYMBOL(fw_device_enable_phys_dma
);
200 show_modalias_attribute(struct device
*dev
,
201 struct device_attribute
*attr
, char *buf
)
203 struct fw_unit
*unit
= fw_unit(dev
);
206 length
= get_modalias(unit
, buf
, PAGE_SIZE
);
207 strcpy(buf
+ length
, "\n");
212 static struct device_attribute modalias_attribute
= {
213 .attr
= { .name
= "modalias", .mode
= S_IRUGO
, },
214 .show
= show_modalias_attribute
,
218 show_config_rom_attribute(struct device
*dev
,
219 struct device_attribute
*attr
, char *buf
)
221 struct fw_device
*device
= fw_device(dev
);
223 memcpy(buf
, device
->config_rom
, device
->config_rom_length
* 4);
225 return device
->config_rom_length
* 4;
228 static struct device_attribute config_rom_attribute
= {
229 .attr
= {.name
= "config_rom", .mode
= S_IRUGO
,},
230 .show
= show_config_rom_attribute
,
234 show_rom_index_attribute(struct device
*dev
,
235 struct device_attribute
*attr
, char *buf
)
237 struct fw_device
*device
= fw_device(dev
->parent
);
238 struct fw_unit
*unit
= fw_unit(dev
);
240 return snprintf(buf
, PAGE_SIZE
, "%d\n",
241 unit
->directory
- device
->config_rom
);
244 static struct device_attribute rom_index_attribute
= {
245 .attr
= { .name
= "rom_index", .mode
= S_IRUGO
, },
246 .show
= show_rom_index_attribute
,
249 struct read_quadlet_callback_data
{
250 struct completion done
;
256 complete_transaction(struct fw_card
*card
, int rcode
,
257 void *payload
, size_t length
, void *data
)
259 struct read_quadlet_callback_data
*callback_data
= data
;
261 if (rcode
== RCODE_COMPLETE
)
262 callback_data
->data
= be32_to_cpu(*(__be32
*)payload
);
263 callback_data
->rcode
= rcode
;
264 complete(&callback_data
->done
);
267 static int read_rom(struct fw_device
*device
, int index
, u32
* data
)
269 struct read_quadlet_callback_data callback_data
;
270 struct fw_transaction t
;
273 init_completion(&callback_data
.done
);
275 offset
= 0xfffff0000400ULL
+ index
* 4;
276 fw_send_request(device
->card
, &t
, TCODE_READ_QUADLET_REQUEST
,
278 device
->generation
, SCODE_100
,
279 offset
, NULL
, 4, complete_transaction
, &callback_data
);
281 wait_for_completion(&callback_data
.done
);
283 *data
= callback_data
.data
;
285 return callback_data
.rcode
;
288 static int read_bus_info_block(struct fw_device
*device
)
291 u32 stack
[16], sp
, key
;
294 /* First read the bus info block. */
295 for (i
= 0; i
< 5; i
++) {
296 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
298 /* As per IEEE1212 7.2, during power-up, devices can
299 * reply with a 0 for the first quadlet of the config
300 * rom to indicate that they are booting (for example,
301 * if the firmware is on the disk of a external
302 * harddisk). In that case we just fail, and the
303 * retry mechanism will try again later. */
304 if (i
== 0 && rom
[i
] == 0)
308 /* Now parse the config rom. The config rom is a recursive
309 * directory structure so we parse it using a stack of
310 * references to the blocks that make up the structure. We
311 * push a reference to the root directory on the stack to
312 * start things off. */
315 stack
[sp
++] = 0xc0000005;
317 /* Pop the next block reference of the stack. The
318 * lower 24 bits is the offset into the config rom,
319 * the upper 8 bits are the type of the reference the
323 if (i
>= ARRAY_SIZE(rom
))
324 /* The reference points outside the standard
325 * config rom area, something's fishy. */
328 /* Read header quadlet for the block to get the length. */
329 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
331 end
= i
+ (rom
[i
] >> 16) + 1;
333 if (end
> ARRAY_SIZE(rom
))
334 /* This block extends outside standard config
335 * area (and the array we're reading it
336 * into). That's broken, so ignore this
340 /* Now read in the block. If this is a directory
341 * block, check the entries as we read them to see if
342 * it references another block, and push it in that case. */
344 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
346 if ((key
>> 30) == 3 && (rom
[i
] >> 30) > 1 &&
347 sp
< ARRAY_SIZE(stack
))
348 stack
[sp
++] = i
+ rom
[i
];
355 device
->config_rom
= kmalloc(length
* 4, GFP_KERNEL
);
356 if (device
->config_rom
== NULL
)
358 memcpy(device
->config_rom
, rom
, length
* 4);
359 device
->config_rom_length
= length
;
364 static void fw_unit_release(struct device
*dev
)
366 struct fw_unit
*unit
= fw_unit(dev
);
371 static int is_fw_unit(struct device
*dev
)
373 return dev
->release
== fw_unit_release
;
376 static void create_units(struct fw_device
*device
)
378 struct fw_csr_iterator ci
;
379 struct fw_unit
*unit
;
383 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
384 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
385 if (key
!= (CSR_UNIT
| CSR_DIRECTORY
))
388 /* Get the address of the unit directory and try to
389 * match the drivers id_tables against it. */
390 unit
= kzalloc(sizeof *unit
, GFP_KERNEL
);
392 fw_error("failed to allocate memory for unit\n");
396 unit
->directory
= ci
.p
+ value
- 1;
397 unit
->device
.bus
= &fw_bus_type
;
398 unit
->device
.release
= fw_unit_release
;
399 unit
->device
.parent
= &device
->device
;
400 snprintf(unit
->device
.bus_id
, sizeof unit
->device
.bus_id
,
401 "%s.%d", device
->device
.bus_id
, i
++);
403 if (device_register(&unit
->device
) < 0) {
408 if (device_create_file(&unit
->device
, &modalias_attribute
) < 0) {
409 device_unregister(&unit
->device
);
413 if (device_create_file(&unit
->device
, &rom_index_attribute
) < 0) {
414 device_unregister(&unit
->device
);
420 static int shutdown_unit(struct device
*device
, void *data
)
422 struct fw_unit
*unit
= fw_unit(device
);
424 if (is_fw_unit(device
)) {
425 device_remove_file(&unit
->device
, &modalias_attribute
);
426 device_unregister(&unit
->device
);
432 static DEFINE_IDR(fw_device_idr
);
435 struct fw_device
*fw_device_from_devt(dev_t devt
)
437 struct fw_device
*device
;
439 down_read(&fw_bus_type
.subsys
.rwsem
);
440 device
= idr_find(&fw_device_idr
, MINOR(devt
));
441 up_read(&fw_bus_type
.subsys
.rwsem
);
446 static void fw_device_shutdown(struct work_struct
*work
)
448 struct fw_device
*device
=
449 container_of(work
, struct fw_device
, work
.work
);
450 int minor
= MINOR(device
->device
.devt
);
452 down_write(&fw_bus_type
.subsys
.rwsem
);
453 idr_remove(&fw_device_idr
, minor
);
454 up_write(&fw_bus_type
.subsys
.rwsem
);
456 fw_device_cdev_remove(device
);
457 device_remove_file(&device
->device
, &config_rom_attribute
);
458 device_for_each_child(&device
->device
, NULL
, shutdown_unit
);
459 device_unregister(&device
->device
);
462 /* These defines control the retry behavior for reading the config
463 * rom. It shouldn't be necessary to tweak these; if the device
464 * doesn't respond to a config rom read within 10 seconds, it's not
465 * going to respond at all. As for the initial delay, a lot of
466 * devices will be able to respond within half a second after bus
467 * reset. On the other hand, it's not really worth being more
468 * aggressive than that, since it scales pretty well; if 10 devices
469 * are plugged in, they're all getting read within one second. */
471 #define MAX_RETRIES 5
472 #define RETRY_DELAY (2 * HZ)
473 #define INITIAL_DELAY (HZ / 2)
475 static void fw_device_init(struct work_struct
*work
)
477 struct fw_device
*device
=
478 container_of(work
, struct fw_device
, work
.work
);
481 /* All failure paths here set node->data to NULL, so that we
482 * don't try to do device_for_each_child() on a kfree()'d
485 if (read_bus_info_block(device
) < 0) {
486 if (device
->config_rom_retries
< MAX_RETRIES
) {
487 device
->config_rom_retries
++;
488 schedule_delayed_work(&device
->work
, RETRY_DELAY
);
490 fw_notify("giving up on config rom for node id %x\n",
492 if (device
->node
== device
->card
->root_node
)
493 schedule_delayed_work(&device
->card
->work
, 0);
494 fw_device_release(&device
->device
);
500 down_write(&fw_bus_type
.subsys
.rwsem
);
501 if (idr_pre_get(&fw_device_idr
, GFP_KERNEL
))
502 err
= idr_get_new(&fw_device_idr
, device
, &minor
);
503 up_write(&fw_bus_type
.subsys
.rwsem
);
507 device
->device
.bus
= &fw_bus_type
;
508 device
->device
.release
= fw_device_release
;
509 device
->device
.parent
= device
->card
->device
;
510 device
->device
.devt
= MKDEV(fw_cdev_major
, minor
);
511 snprintf(device
->device
.bus_id
, sizeof device
->device
.bus_id
,
514 if (device_add(&device
->device
)) {
515 fw_error("Failed to add device.\n");
516 goto error_with_cdev
;
519 if (device_create_file(&device
->device
, &config_rom_attribute
) < 0) {
520 fw_error("Failed to create config rom file.\n");
521 goto error_with_device
;
524 create_units(device
);
526 /* Transition the device to running state. If it got pulled
527 * out from under us while we did the intialization work, we
528 * have to shut down the device again here. Normally, though,
529 * fw_node_event will be responsible for shutting it down when
530 * necessary. We have to use the atomic cmpxchg here to avoid
531 * racing with the FW_NODE_DESTROYED case in
532 * fw_node_event(). */
533 if (atomic_cmpxchg(&device
->state
,
534 FW_DEVICE_INITIALIZING
,
535 FW_DEVICE_RUNNING
) == FW_DEVICE_SHUTDOWN
)
536 fw_device_shutdown(&device
->work
.work
);
538 fw_notify("created new fw device %s (%d config rom retries)\n",
539 device
->device
.bus_id
, device
->config_rom_retries
);
541 /* Reschedule the IRM work if we just finished reading the
542 * root node config rom. If this races with a bus reset we
543 * just end up running the IRM work a couple of extra times -
544 * pretty harmless. */
545 if (device
->node
== device
->card
->root_node
)
546 schedule_delayed_work(&device
->card
->work
, 0);
551 device_del(&device
->device
);
553 down_write(&fw_bus_type
.subsys
.rwsem
);
554 idr_remove(&fw_device_idr
, minor
);
555 up_write(&fw_bus_type
.subsys
.rwsem
);
557 put_device(&device
->device
);
560 static int update_unit(struct device
*dev
, void *data
)
562 struct fw_unit
*unit
= fw_unit(dev
);
563 struct fw_driver
*driver
= (struct fw_driver
*)dev
->driver
;
565 if (is_fw_unit(dev
) && driver
!= NULL
&& driver
->update
!= NULL
)
566 driver
->update(unit
);
571 static void fw_device_update(struct work_struct
*work
)
573 struct fw_device
*device
=
574 container_of(work
, struct fw_device
, work
.work
);
576 fw_device_cdev_update(device
);
577 device_for_each_child(&device
->device
, NULL
, update_unit
);
580 void fw_node_event(struct fw_card
*card
, struct fw_node
*node
, int event
)
582 struct fw_device
*device
;
585 case FW_NODE_CREATED
:
586 case FW_NODE_LINK_ON
:
590 device
= kzalloc(sizeof(*device
), GFP_ATOMIC
);
594 /* Do minimal intialization of the device here, the
595 * rest will happen in fw_device_init(). We need the
596 * card and node so we can read the config rom and we
597 * need to do device_initialize() now so
598 * device_for_each_child() in FW_NODE_UPDATED is
599 * doesn't freak out. */
600 device_initialize(&device
->device
);
601 atomic_set(&device
->state
, FW_DEVICE_INITIALIZING
);
602 device
->card
= fw_card_get(card
);
603 device
->node
= fw_node_get(node
);
604 device
->node_id
= node
->node_id
;
605 device
->generation
= card
->generation
;
606 INIT_LIST_HEAD(&device
->client_list
);
608 /* Set the node data to point back to this device so
609 * FW_NODE_UPDATED callbacks can update the node_id
610 * and generation for the device. */
613 /* Many devices are slow to respond after bus resets,
614 * especially if they are bus powered and go through
615 * power-up after getting plugged in. We schedule the
616 * first config rom scan half a second after bus reset. */
617 INIT_DELAYED_WORK(&device
->work
, fw_device_init
);
618 schedule_delayed_work(&device
->work
, INITIAL_DELAY
);
621 case FW_NODE_UPDATED
:
622 if (!node
->link_on
|| node
->data
== NULL
)
626 device
->node_id
= node
->node_id
;
627 device
->generation
= card
->generation
;
628 if (atomic_read(&device
->state
) == FW_DEVICE_RUNNING
) {
629 PREPARE_DELAYED_WORK(&device
->work
, fw_device_update
);
630 schedule_delayed_work(&device
->work
, 0);
634 case FW_NODE_DESTROYED
:
635 case FW_NODE_LINK_OFF
:
639 /* Destroy the device associated with the node. There
640 * are two cases here: either the device is fully
641 * initialized (FW_DEVICE_RUNNING) or we're in the
642 * process of reading its config rom
643 * (FW_DEVICE_INITIALIZING). If it is fully
644 * initialized we can reuse device->work to schedule a
645 * full fw_device_shutdown(). If not, there's work
646 * scheduled to read it's config rom, and we just put
647 * the device in shutdown state to have that code fail
648 * to create the device. */
650 if (atomic_xchg(&device
->state
,
651 FW_DEVICE_SHUTDOWN
) == FW_DEVICE_RUNNING
) {
652 PREPARE_DELAYED_WORK(&device
->work
, fw_device_shutdown
);
653 schedule_delayed_work(&device
->work
, 0);