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
,
233 struct read_quadlet_callback_data
{
234 struct completion done
;
240 complete_transaction(struct fw_card
*card
, int rcode
,
241 void *payload
, size_t length
, void *data
)
243 struct read_quadlet_callback_data
*callback_data
= data
;
245 if (rcode
== RCODE_COMPLETE
)
246 callback_data
->data
= be32_to_cpu(*(__be32
*)payload
);
247 callback_data
->rcode
= rcode
;
248 complete(&callback_data
->done
);
251 static int read_rom(struct fw_device
*device
, int index
, u32
* data
)
253 struct read_quadlet_callback_data callback_data
;
254 struct fw_transaction t
;
257 init_completion(&callback_data
.done
);
259 offset
= 0xfffff0000400ULL
+ index
* 4;
260 fw_send_request(device
->card
, &t
, TCODE_READ_QUADLET_REQUEST
,
262 device
->generation
, SCODE_100
,
263 offset
, NULL
, 4, complete_transaction
, &callback_data
);
265 wait_for_completion(&callback_data
.done
);
267 *data
= callback_data
.data
;
269 return callback_data
.rcode
;
272 static int read_bus_info_block(struct fw_device
*device
)
275 u32 stack
[16], sp
, key
;
278 /* First read the bus info block. */
279 for (i
= 0; i
< 5; i
++) {
280 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
282 /* As per IEEE1212 7.2, during power-up, devices can
283 * reply with a 0 for the first quadlet of the config
284 * rom to indicate that they are booting (for example,
285 * if the firmware is on the disk of a external
286 * harddisk). In that case we just fail, and the
287 * retry mechanism will try again later. */
288 if (i
== 0 && rom
[i
] == 0)
292 /* Now parse the config rom. The config rom is a recursive
293 * directory structure so we parse it using a stack of
294 * references to the blocks that make up the structure. We
295 * push a reference to the root directory on the stack to
296 * start things off. */
299 stack
[sp
++] = 0xc0000005;
301 /* Pop the next block reference of the stack. The
302 * lower 24 bits is the offset into the config rom,
303 * the upper 8 bits are the type of the reference the
307 if (i
>= ARRAY_SIZE(rom
))
308 /* The reference points outside the standard
309 * config rom area, something's fishy. */
312 /* Read header quadlet for the block to get the length. */
313 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
315 end
= i
+ (rom
[i
] >> 16) + 1;
317 if (end
> ARRAY_SIZE(rom
))
318 /* This block extends outside standard config
319 * area (and the array we're reading it
320 * into). That's broken, so ignore this
324 /* Now read in the block. If this is a directory
325 * block, check the entries as we read them to see if
326 * it references another block, and push it in that case. */
328 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
330 if ((key
>> 30) == 3 && (rom
[i
] >> 30) > 1 &&
331 sp
< ARRAY_SIZE(stack
))
332 stack
[sp
++] = i
+ rom
[i
];
339 device
->config_rom
= kmalloc(length
* 4, GFP_KERNEL
);
340 if (device
->config_rom
== NULL
)
342 memcpy(device
->config_rom
, rom
, length
* 4);
343 device
->config_rom_length
= length
;
348 static void fw_unit_release(struct device
*dev
)
350 struct fw_unit
*unit
= fw_unit(dev
);
355 static int is_fw_unit(struct device
*dev
)
357 return dev
->release
== fw_unit_release
;
360 static void create_units(struct fw_device
*device
)
362 struct fw_csr_iterator ci
;
363 struct fw_unit
*unit
;
367 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
368 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
369 if (key
!= (CSR_UNIT
| CSR_DIRECTORY
))
372 /* Get the address of the unit directory and try to
373 * match the drivers id_tables against it. */
374 unit
= kzalloc(sizeof *unit
, GFP_KERNEL
);
376 fw_error("failed to allocate memory for unit\n");
380 unit
->directory
= ci
.p
+ value
- 1;
381 unit
->device
.bus
= &fw_bus_type
;
382 unit
->device
.release
= fw_unit_release
;
383 unit
->device
.parent
= &device
->device
;
384 snprintf(unit
->device
.bus_id
, sizeof unit
->device
.bus_id
,
385 "%s.%d", device
->device
.bus_id
, i
++);
387 if (device_register(&unit
->device
) < 0) {
392 if (device_create_file(&unit
->device
, &modalias_attribute
) < 0) {
393 device_unregister(&unit
->device
);
399 static int shutdown_unit(struct device
*device
, void *data
)
401 struct fw_unit
*unit
= fw_unit(device
);
403 if (is_fw_unit(device
)) {
404 device_remove_file(&unit
->device
, &modalias_attribute
);
405 device_unregister(&unit
->device
);
411 static DEFINE_IDR(fw_device_idr
);
414 struct fw_device
*fw_device_from_devt(dev_t devt
)
416 struct fw_device
*device
;
418 down_read(&fw_bus_type
.subsys
.rwsem
);
419 device
= idr_find(&fw_device_idr
, MINOR(devt
));
420 up_read(&fw_bus_type
.subsys
.rwsem
);
425 static void fw_device_shutdown(struct work_struct
*work
)
427 struct fw_device
*device
=
428 container_of(work
, struct fw_device
, work
.work
);
429 int minor
= MINOR(device
->device
.devt
);
431 down_write(&fw_bus_type
.subsys
.rwsem
);
432 idr_remove(&fw_device_idr
, minor
);
433 up_write(&fw_bus_type
.subsys
.rwsem
);
435 device_remove_file(&device
->device
, &config_rom_attribute
);
436 device_for_each_child(&device
->device
, NULL
, shutdown_unit
);
437 device_unregister(&device
->device
);
440 /* These defines control the retry behavior for reading the config
441 * rom. It shouldn't be necessary to tweak these; if the device
442 * doesn't respond to a config rom read within 10 seconds, it's not
443 * going to respond at all. As for the initial delay, a lot of
444 * devices will be able to respond within half a second after bus
445 * reset. On the other hand, it's not really worth being more
446 * aggressive than that, since it scales pretty well; if 10 devices
447 * are plugged in, they're all getting read within one second. */
449 #define MAX_RETRIES 5
450 #define RETRY_DELAY (2 * HZ)
451 #define INITIAL_DELAY (HZ / 2)
453 static void fw_device_init(struct work_struct
*work
)
455 struct fw_device
*device
=
456 container_of(work
, struct fw_device
, work
.work
);
459 /* All failure paths here set node->data to NULL, so that we
460 * don't try to do device_for_each_child() on a kfree()'d
463 if (read_bus_info_block(device
) < 0) {
464 if (device
->config_rom_retries
< MAX_RETRIES
) {
465 device
->config_rom_retries
++;
466 schedule_delayed_work(&device
->work
, RETRY_DELAY
);
468 fw_notify("giving up on config rom for node id %x\n",
470 if (device
->node
== device
->card
->root_node
)
471 schedule_delayed_work(&device
->card
->work
, 0);
472 fw_device_release(&device
->device
);
478 down_write(&fw_bus_type
.subsys
.rwsem
);
479 if (idr_pre_get(&fw_device_idr
, GFP_KERNEL
))
480 err
= idr_get_new(&fw_device_idr
, device
, &minor
);
481 up_write(&fw_bus_type
.subsys
.rwsem
);
485 device
->device
.bus
= &fw_bus_type
;
486 device
->device
.release
= fw_device_release
;
487 device
->device
.parent
= device
->card
->device
;
488 device
->device
.devt
= MKDEV(fw_cdev_major
, minor
);
489 snprintf(device
->device
.bus_id
, sizeof device
->device
.bus_id
,
492 if (device_add(&device
->device
)) {
493 fw_error("Failed to add device.\n");
494 goto error_with_cdev
;
497 if (device_create_file(&device
->device
, &config_rom_attribute
) < 0) {
498 fw_error("Failed to create config rom file.\n");
499 goto error_with_device
;
502 create_units(device
);
504 /* Transition the device to running state. If it got pulled
505 * out from under us while we did the intialization work, we
506 * have to shut down the device again here. Normally, though,
507 * fw_node_event will be responsible for shutting it down when
508 * necessary. We have to use the atomic cmpxchg here to avoid
509 * racing with the FW_NODE_DESTROYED case in
510 * fw_node_event(). */
511 if (atomic_cmpxchg(&device
->state
,
512 FW_DEVICE_INITIALIZING
,
513 FW_DEVICE_RUNNING
) == FW_DEVICE_SHUTDOWN
)
514 fw_device_shutdown(&device
->work
.work
);
516 fw_notify("created new fw device %s (%d config rom retries)\n",
517 device
->device
.bus_id
, device
->config_rom_retries
);
519 /* Reschedule the IRM work if we just finished reading the
520 * root node config rom. If this races with a bus reset we
521 * just end up running the IRM work a couple of extra times -
522 * pretty harmless. */
523 if (device
->node
== device
->card
->root_node
)
524 schedule_delayed_work(&device
->card
->work
, 0);
529 device_del(&device
->device
);
531 down_write(&fw_bus_type
.subsys
.rwsem
);
532 idr_remove(&fw_device_idr
, minor
);
533 up_write(&fw_bus_type
.subsys
.rwsem
);
535 put_device(&device
->device
);
538 static int update_unit(struct device
*dev
, void *data
)
540 struct fw_unit
*unit
= fw_unit(dev
);
541 struct fw_driver
*driver
= (struct fw_driver
*)dev
->driver
;
543 if (is_fw_unit(dev
) && driver
!= NULL
&& driver
->update
!= NULL
)
544 driver
->update(unit
);
549 static void fw_device_update(struct work_struct
*work
)
551 struct fw_device
*device
=
552 container_of(work
, struct fw_device
, work
.work
);
554 fw_device_cdev_update(device
);
555 device_for_each_child(&device
->device
, NULL
, update_unit
);
558 void fw_node_event(struct fw_card
*card
, struct fw_node
*node
, int event
)
560 struct fw_device
*device
;
563 case FW_NODE_CREATED
:
564 case FW_NODE_LINK_ON
:
568 device
= kzalloc(sizeof(*device
), GFP_ATOMIC
);
572 /* Do minimal intialization of the device here, the
573 * rest will happen in fw_device_init(). We need the
574 * card and node so we can read the config rom and we
575 * need to do device_initialize() now so
576 * device_for_each_child() in FW_NODE_UPDATED is
577 * doesn't freak out. */
578 device_initialize(&device
->device
);
579 atomic_set(&device
->state
, FW_DEVICE_INITIALIZING
);
580 device
->card
= fw_card_get(card
);
581 device
->node
= fw_node_get(node
);
582 device
->node_id
= node
->node_id
;
583 device
->generation
= card
->generation
;
584 INIT_LIST_HEAD(&device
->client_list
);
586 /* Set the node data to point back to this device so
587 * FW_NODE_UPDATED callbacks can update the node_id
588 * and generation for the device. */
591 /* Many devices are slow to respond after bus resets,
592 * especially if they are bus powered and go through
593 * power-up after getting plugged in. We schedule the
594 * first config rom scan half a second after bus reset. */
595 INIT_DELAYED_WORK(&device
->work
, fw_device_init
);
596 schedule_delayed_work(&device
->work
, INITIAL_DELAY
);
599 case FW_NODE_UPDATED
:
600 if (!node
->link_on
|| node
->data
== NULL
)
604 device
->node_id
= node
->node_id
;
605 device
->generation
= card
->generation
;
606 if (atomic_read(&device
->state
) == FW_DEVICE_RUNNING
) {
607 PREPARE_DELAYED_WORK(&device
->work
, fw_device_update
);
608 schedule_delayed_work(&device
->work
, 0);
612 case FW_NODE_DESTROYED
:
613 case FW_NODE_LINK_OFF
:
617 /* Destroy the device associated with the node. There
618 * are two cases here: either the device is fully
619 * initialized (FW_DEVICE_RUNNING) or we're in the
620 * process of reading its config rom
621 * (FW_DEVICE_INITIALIZING). If it is fully
622 * initialized we can reuse device->work to schedule a
623 * full fw_device_shutdown(). If not, there's work
624 * scheduled to read it's config rom, and we just put
625 * the device in shutdown state to have that code fail
626 * to create the device. */
628 if (atomic_xchg(&device
->state
,
629 FW_DEVICE_SHUTDOWN
) == FW_DEVICE_RUNNING
) {
630 PREPARE_DELAYED_WORK(&device
->work
, fw_device_shutdown
);
631 schedule_delayed_work(&device
->work
, 0);