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 <linux/rwsem.h>
30 #include <asm/semaphore.h>
31 #include "fw-transaction.h"
32 #include "fw-topology.h"
33 #include "fw-device.h"
35 void fw_csr_iterator_init(struct fw_csr_iterator
*ci
, u32
* p
)
38 ci
->end
= ci
->p
+ (p
[0] >> 16);
40 EXPORT_SYMBOL(fw_csr_iterator_init
);
42 int fw_csr_iterator_next(struct fw_csr_iterator
*ci
, int *key
, int *value
)
45 *value
= *ci
->p
& 0xffffff;
47 return ci
->p
++ < ci
->end
;
49 EXPORT_SYMBOL(fw_csr_iterator_next
);
51 static int is_fw_unit(struct device
*dev
);
53 static int match_unit_directory(u32
* directory
, const struct fw_device_id
*id
)
55 struct fw_csr_iterator ci
;
56 int key
, value
, match
;
59 fw_csr_iterator_init(&ci
, directory
);
60 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
61 if (key
== CSR_VENDOR
&& value
== id
->vendor
)
62 match
|= FW_MATCH_VENDOR
;
63 if (key
== CSR_MODEL
&& value
== id
->model
)
64 match
|= FW_MATCH_MODEL
;
65 if (key
== CSR_SPECIFIER_ID
&& value
== id
->specifier_id
)
66 match
|= FW_MATCH_SPECIFIER_ID
;
67 if (key
== CSR_VERSION
&& value
== id
->version
)
68 match
|= FW_MATCH_VERSION
;
71 return (match
& id
->match_flags
) == id
->match_flags
;
74 static int fw_unit_match(struct device
*dev
, struct device_driver
*drv
)
76 struct fw_unit
*unit
= fw_unit(dev
);
77 struct fw_driver
*driver
= fw_driver(drv
);
80 /* We only allow binding to fw_units. */
84 for (i
= 0; driver
->id_table
[i
].match_flags
!= 0; i
++) {
85 if (match_unit_directory(unit
->directory
, &driver
->id_table
[i
]))
92 static int get_modalias(struct fw_unit
*unit
, char *buffer
, size_t buffer_size
)
94 struct fw_device
*device
= fw_device(unit
->device
.parent
);
95 struct fw_csr_iterator ci
;
100 int specifier_id
= 0;
103 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
104 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
115 fw_csr_iterator_init(&ci
, unit
->directory
);
116 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
118 case CSR_SPECIFIER_ID
:
119 specifier_id
= value
;
127 return snprintf(buffer
, buffer_size
,
128 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
129 vendor
, model
, specifier_id
, version
);
133 fw_unit_uevent(struct device
*dev
, char **envp
, int num_envp
,
134 char *buffer
, int buffer_size
)
136 struct fw_unit
*unit
= fw_unit(dev
);
141 if (!is_fw_unit(dev
))
144 get_modalias(unit
, modalias
, sizeof modalias
);
146 if (add_uevent_var(envp
, num_envp
, &i
,
147 buffer
, buffer_size
, &length
,
148 "MODALIAS=%s", modalias
))
157 struct bus_type fw_bus_type
= {
159 .match
= fw_unit_match
,
160 .uevent
= fw_unit_uevent
,
162 EXPORT_SYMBOL(fw_bus_type
);
164 extern struct fw_device
*fw_device_get(struct fw_device
*device
)
166 get_device(&device
->device
);
171 extern void fw_device_put(struct fw_device
*device
)
173 put_device(&device
->device
);
176 static void fw_device_release(struct device
*dev
)
178 struct fw_device
*device
= fw_device(dev
);
181 /* Take the card lock so we don't set this to NULL while a
182 * FW_NODE_UPDATED callback is being handled. */
183 spin_lock_irqsave(&device
->card
->lock
, flags
);
184 device
->node
->data
= NULL
;
185 spin_unlock_irqrestore(&device
->card
->lock
, flags
);
187 fw_node_put(device
->node
);
188 fw_card_put(device
->card
);
189 kfree(device
->config_rom
);
193 int fw_device_enable_phys_dma(struct fw_device
*device
)
195 return device
->card
->driver
->enable_phys_dma(device
->card
,
199 EXPORT_SYMBOL(fw_device_enable_phys_dma
);
202 show_modalias_attribute(struct device
*dev
,
203 struct device_attribute
*attr
, char *buf
)
205 struct fw_unit
*unit
= fw_unit(dev
);
208 length
= get_modalias(unit
, buf
, PAGE_SIZE
);
209 strcpy(buf
+ length
, "\n");
214 static struct device_attribute modalias_attribute
= {
215 .attr
= { .name
= "modalias", .mode
= S_IRUGO
, },
216 .show
= show_modalias_attribute
,
220 show_config_rom_attribute(struct device
*dev
,
221 struct device_attribute
*attr
, char *buf
)
223 struct fw_device
*device
= fw_device(dev
);
225 memcpy(buf
, device
->config_rom
, device
->config_rom_length
* 4);
227 return device
->config_rom_length
* 4;
230 static struct device_attribute config_rom_attribute
= {
231 .attr
= {.name
= "config_rom", .mode
= S_IRUGO
,},
232 .show
= show_config_rom_attribute
,
236 show_rom_index_attribute(struct device
*dev
,
237 struct device_attribute
*attr
, char *buf
)
239 struct fw_device
*device
= fw_device(dev
->parent
);
240 struct fw_unit
*unit
= fw_unit(dev
);
242 return snprintf(buf
, PAGE_SIZE
, "%d\n",
243 unit
->directory
- device
->config_rom
);
246 static struct device_attribute rom_index_attribute
= {
247 .attr
= { .name
= "rom_index", .mode
= S_IRUGO
, },
248 .show
= show_rom_index_attribute
,
251 struct read_quadlet_callback_data
{
252 struct completion done
;
258 complete_transaction(struct fw_card
*card
, int rcode
,
259 void *payload
, size_t length
, void *data
)
261 struct read_quadlet_callback_data
*callback_data
= data
;
263 if (rcode
== RCODE_COMPLETE
)
264 callback_data
->data
= be32_to_cpu(*(__be32
*)payload
);
265 callback_data
->rcode
= rcode
;
266 complete(&callback_data
->done
);
269 static int read_rom(struct fw_device
*device
, int index
, u32
* data
)
271 struct read_quadlet_callback_data callback_data
;
272 struct fw_transaction t
;
275 init_completion(&callback_data
.done
);
277 offset
= 0xfffff0000400ULL
+ index
* 4;
278 fw_send_request(device
->card
, &t
, TCODE_READ_QUADLET_REQUEST
,
280 device
->generation
, SCODE_100
,
281 offset
, NULL
, 4, complete_transaction
, &callback_data
);
283 wait_for_completion(&callback_data
.done
);
285 *data
= callback_data
.data
;
287 return callback_data
.rcode
;
290 static int read_bus_info_block(struct fw_device
*device
)
293 u32 stack
[16], sp
, key
;
296 /* First read the bus info block. */
297 for (i
= 0; i
< 5; i
++) {
298 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
300 /* As per IEEE1212 7.2, during power-up, devices can
301 * reply with a 0 for the first quadlet of the config
302 * rom to indicate that they are booting (for example,
303 * if the firmware is on the disk of a external
304 * harddisk). In that case we just fail, and the
305 * retry mechanism will try again later. */
306 if (i
== 0 && rom
[i
] == 0)
310 /* Now parse the config rom. The config rom is a recursive
311 * directory structure so we parse it using a stack of
312 * references to the blocks that make up the structure. We
313 * push a reference to the root directory on the stack to
314 * start things off. */
317 stack
[sp
++] = 0xc0000005;
319 /* Pop the next block reference of the stack. The
320 * lower 24 bits is the offset into the config rom,
321 * the upper 8 bits are the type of the reference the
325 if (i
>= ARRAY_SIZE(rom
))
326 /* The reference points outside the standard
327 * config rom area, something's fishy. */
330 /* Read header quadlet for the block to get the length. */
331 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
333 end
= i
+ (rom
[i
] >> 16) + 1;
335 if (end
> ARRAY_SIZE(rom
))
336 /* This block extends outside standard config
337 * area (and the array we're reading it
338 * into). That's broken, so ignore this
342 /* Now read in the block. If this is a directory
343 * block, check the entries as we read them to see if
344 * it references another block, and push it in that case. */
346 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
348 if ((key
>> 30) == 3 && (rom
[i
] >> 30) > 1 &&
349 sp
< ARRAY_SIZE(stack
))
350 stack
[sp
++] = i
+ rom
[i
];
357 device
->config_rom
= kmalloc(length
* 4, GFP_KERNEL
);
358 if (device
->config_rom
== NULL
)
360 memcpy(device
->config_rom
, rom
, length
* 4);
361 device
->config_rom_length
= length
;
366 static void fw_unit_release(struct device
*dev
)
368 struct fw_unit
*unit
= fw_unit(dev
);
373 static int is_fw_unit(struct device
*dev
)
375 return dev
->release
== fw_unit_release
;
378 static void create_units(struct fw_device
*device
)
380 struct fw_csr_iterator ci
;
381 struct fw_unit
*unit
;
385 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
386 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
387 if (key
!= (CSR_UNIT
| CSR_DIRECTORY
))
390 /* Get the address of the unit directory and try to
391 * match the drivers id_tables against it. */
392 unit
= kzalloc(sizeof *unit
, GFP_KERNEL
);
394 fw_error("failed to allocate memory for unit\n");
398 unit
->directory
= ci
.p
+ value
- 1;
399 unit
->device
.bus
= &fw_bus_type
;
400 unit
->device
.release
= fw_unit_release
;
401 unit
->device
.parent
= &device
->device
;
402 snprintf(unit
->device
.bus_id
, sizeof unit
->device
.bus_id
,
403 "%s.%d", device
->device
.bus_id
, i
++);
405 if (device_register(&unit
->device
) < 0) {
410 if (device_create_file(&unit
->device
, &modalias_attribute
) < 0) {
411 device_unregister(&unit
->device
);
415 if (device_create_file(&unit
->device
, &rom_index_attribute
) < 0) {
416 device_unregister(&unit
->device
);
422 static int shutdown_unit(struct device
*device
, void *data
)
424 struct fw_unit
*unit
= fw_unit(device
);
426 if (is_fw_unit(device
)) {
427 device_remove_file(&unit
->device
, &modalias_attribute
);
428 device_unregister(&unit
->device
);
434 static DEFINE_IDR(fw_device_idr
);
437 struct fw_device
*fw_device_from_devt(dev_t devt
)
439 struct fw_device
*device
;
441 down_read(&fw_bus_type
.subsys
.rwsem
);
442 device
= idr_find(&fw_device_idr
, MINOR(devt
));
443 up_read(&fw_bus_type
.subsys
.rwsem
);
448 static void fw_device_shutdown(struct work_struct
*work
)
450 struct fw_device
*device
=
451 container_of(work
, struct fw_device
, work
.work
);
452 int minor
= MINOR(device
->device
.devt
);
454 down_write(&fw_bus_type
.subsys
.rwsem
);
455 idr_remove(&fw_device_idr
, minor
);
456 up_write(&fw_bus_type
.subsys
.rwsem
);
458 fw_device_cdev_remove(device
);
459 device_remove_file(&device
->device
, &config_rom_attribute
);
460 device_for_each_child(&device
->device
, NULL
, shutdown_unit
);
461 device_unregister(&device
->device
);
464 /* These defines control the retry behavior for reading the config
465 * rom. It shouldn't be necessary to tweak these; if the device
466 * doesn't respond to a config rom read within 10 seconds, it's not
467 * going to respond at all. As for the initial delay, a lot of
468 * devices will be able to respond within half a second after bus
469 * reset. On the other hand, it's not really worth being more
470 * aggressive than that, since it scales pretty well; if 10 devices
471 * are plugged in, they're all getting read within one second. */
473 #define MAX_RETRIES 5
474 #define RETRY_DELAY (2 * HZ)
475 #define INITIAL_DELAY (HZ / 2)
477 static void fw_device_init(struct work_struct
*work
)
479 struct fw_device
*device
=
480 container_of(work
, struct fw_device
, work
.work
);
483 /* All failure paths here set node->data to NULL, so that we
484 * don't try to do device_for_each_child() on a kfree()'d
487 if (read_bus_info_block(device
) < 0) {
488 if (device
->config_rom_retries
< MAX_RETRIES
) {
489 device
->config_rom_retries
++;
490 schedule_delayed_work(&device
->work
, RETRY_DELAY
);
492 fw_notify("giving up on config rom for node id %x\n",
494 if (device
->node
== device
->card
->root_node
)
495 schedule_delayed_work(&device
->card
->work
, 0);
496 fw_device_release(&device
->device
);
502 down_write(&fw_bus_type
.subsys
.rwsem
);
503 if (idr_pre_get(&fw_device_idr
, GFP_KERNEL
))
504 err
= idr_get_new(&fw_device_idr
, device
, &minor
);
505 up_write(&fw_bus_type
.subsys
.rwsem
);
509 device
->device
.bus
= &fw_bus_type
;
510 device
->device
.release
= fw_device_release
;
511 device
->device
.parent
= device
->card
->device
;
512 device
->device
.devt
= MKDEV(fw_cdev_major
, minor
);
513 snprintf(device
->device
.bus_id
, sizeof device
->device
.bus_id
,
516 if (device_add(&device
->device
)) {
517 fw_error("Failed to add device.\n");
518 goto error_with_cdev
;
521 if (device_create_file(&device
->device
, &config_rom_attribute
) < 0) {
522 fw_error("Failed to create config rom file.\n");
523 goto error_with_device
;
526 create_units(device
);
528 /* Transition the device to running state. If it got pulled
529 * out from under us while we did the intialization work, we
530 * have to shut down the device again here. Normally, though,
531 * fw_node_event will be responsible for shutting it down when
532 * necessary. We have to use the atomic cmpxchg here to avoid
533 * racing with the FW_NODE_DESTROYED case in
534 * fw_node_event(). */
535 if (atomic_cmpxchg(&device
->state
,
536 FW_DEVICE_INITIALIZING
,
537 FW_DEVICE_RUNNING
) == FW_DEVICE_SHUTDOWN
)
538 fw_device_shutdown(&device
->work
.work
);
540 fw_notify("created new fw device %s (%d config rom retries)\n",
541 device
->device
.bus_id
, device
->config_rom_retries
);
543 /* Reschedule the IRM work if we just finished reading the
544 * root node config rom. If this races with a bus reset we
545 * just end up running the IRM work a couple of extra times -
546 * pretty harmless. */
547 if (device
->node
== device
->card
->root_node
)
548 schedule_delayed_work(&device
->card
->work
, 0);
553 device_del(&device
->device
);
555 down_write(&fw_bus_type
.subsys
.rwsem
);
556 idr_remove(&fw_device_idr
, minor
);
557 up_write(&fw_bus_type
.subsys
.rwsem
);
559 put_device(&device
->device
);
562 static int update_unit(struct device
*dev
, void *data
)
564 struct fw_unit
*unit
= fw_unit(dev
);
565 struct fw_driver
*driver
= (struct fw_driver
*)dev
->driver
;
567 if (is_fw_unit(dev
) && driver
!= NULL
&& driver
->update
!= NULL
) {
569 driver
->update(unit
);
576 static void fw_device_update(struct work_struct
*work
)
578 struct fw_device
*device
=
579 container_of(work
, struct fw_device
, work
.work
);
581 fw_device_cdev_update(device
);
582 device_for_each_child(&device
->device
, NULL
, update_unit
);
585 void fw_node_event(struct fw_card
*card
, struct fw_node
*node
, int event
)
587 struct fw_device
*device
;
590 case FW_NODE_CREATED
:
591 case FW_NODE_LINK_ON
:
595 device
= kzalloc(sizeof(*device
), GFP_ATOMIC
);
599 /* Do minimal intialization of the device here, the
600 * rest will happen in fw_device_init(). We need the
601 * card and node so we can read the config rom and we
602 * need to do device_initialize() now so
603 * device_for_each_child() in FW_NODE_UPDATED is
604 * doesn't freak out. */
605 device_initialize(&device
->device
);
606 atomic_set(&device
->state
, FW_DEVICE_INITIALIZING
);
607 device
->card
= fw_card_get(card
);
608 device
->node
= fw_node_get(node
);
609 device
->node_id
= node
->node_id
;
610 device
->generation
= card
->generation
;
611 INIT_LIST_HEAD(&device
->client_list
);
613 /* Set the node data to point back to this device so
614 * FW_NODE_UPDATED callbacks can update the node_id
615 * and generation for the device. */
618 /* Many devices are slow to respond after bus resets,
619 * especially if they are bus powered and go through
620 * power-up after getting plugged in. We schedule the
621 * first config rom scan half a second after bus reset. */
622 INIT_DELAYED_WORK(&device
->work
, fw_device_init
);
623 schedule_delayed_work(&device
->work
, INITIAL_DELAY
);
626 case FW_NODE_UPDATED
:
627 if (!node
->link_on
|| node
->data
== NULL
)
631 device
->node_id
= node
->node_id
;
632 device
->generation
= card
->generation
;
633 if (atomic_read(&device
->state
) == FW_DEVICE_RUNNING
) {
634 PREPARE_DELAYED_WORK(&device
->work
, fw_device_update
);
635 schedule_delayed_work(&device
->work
, 0);
639 case FW_NODE_DESTROYED
:
640 case FW_NODE_LINK_OFF
:
644 /* Destroy the device associated with the node. There
645 * are two cases here: either the device is fully
646 * initialized (FW_DEVICE_RUNNING) or we're in the
647 * process of reading its config rom
648 * (FW_DEVICE_INITIALIZING). If it is fully
649 * initialized we can reuse device->work to schedule a
650 * full fw_device_shutdown(). If not, there's work
651 * scheduled to read it's config rom, and we just put
652 * the device in shutdown state to have that code fail
653 * to create the device. */
655 if (atomic_xchg(&device
->state
,
656 FW_DEVICE_SHUTDOWN
) == FW_DEVICE_RUNNING
) {
657 PREPARE_DELAYED_WORK(&device
->work
, fw_device_shutdown
);
658 schedule_delayed_work(&device
->work
, 0);