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);
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
;
48 EXPORT_SYMBOL(fw_csr_iterator_next
);
50 static int is_fw_unit(struct device
*dev
);
52 static int match_unit_directory(u32
* directory
, const struct fw_device_id
*id
)
54 struct fw_csr_iterator ci
;
55 int key
, value
, match
;
58 fw_csr_iterator_init(&ci
, directory
);
59 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
60 if (key
== CSR_VENDOR
&& value
== id
->vendor
)
61 match
|= FW_MATCH_VENDOR
;
62 if (key
== CSR_MODEL
&& value
== id
->model
)
63 match
|= FW_MATCH_MODEL
;
64 if (key
== CSR_SPECIFIER_ID
&& value
== id
->specifier_id
)
65 match
|= FW_MATCH_SPECIFIER_ID
;
66 if (key
== CSR_VERSION
&& value
== id
->version
)
67 match
|= FW_MATCH_VERSION
;
70 return (match
& id
->match_flags
) == id
->match_flags
;
73 static int fw_unit_match(struct device
*dev
, struct device_driver
*drv
)
75 struct fw_unit
*unit
= fw_unit(dev
);
76 struct fw_driver
*driver
= fw_driver(drv
);
79 /* We only allow binding to fw_units. */
83 for (i
= 0; driver
->id_table
[i
].match_flags
!= 0; i
++) {
84 if (match_unit_directory(unit
->directory
, &driver
->id_table
[i
]))
91 static int get_modalias(struct fw_unit
*unit
, char *buffer
, size_t buffer_size
)
93 struct fw_device
*device
= fw_device(unit
->device
.parent
);
94 struct fw_csr_iterator ci
;
102 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
103 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
114 fw_csr_iterator_init(&ci
, unit
->directory
);
115 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
117 case CSR_SPECIFIER_ID
:
118 specifier_id
= value
;
126 return snprintf(buffer
, buffer_size
,
127 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
128 vendor
, model
, specifier_id
, version
);
132 fw_unit_uevent(struct device
*dev
, char **envp
, int num_envp
,
133 char *buffer
, int buffer_size
)
135 struct fw_unit
*unit
= fw_unit(dev
);
140 if (!is_fw_unit(dev
))
143 get_modalias(unit
, modalias
, sizeof modalias
);
145 if (add_uevent_var(envp
, num_envp
, &i
,
146 buffer
, buffer_size
, &length
,
147 "MODALIAS=%s", modalias
))
156 struct bus_type fw_bus_type
= {
158 .match
= fw_unit_match
,
159 .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
,
200 EXPORT_SYMBOL(fw_device_enable_phys_dma
);
203 show_modalias_attribute(struct device
*dev
,
204 struct device_attribute
*attr
, char *buf
)
206 struct fw_unit
*unit
= fw_unit(dev
);
209 length
= get_modalias(unit
, buf
, PAGE_SIZE
);
210 strcpy(buf
+ length
, "\n");
215 static struct device_attribute modalias_attribute
= {
216 .attr
= {.name
= "modalias",.mode
= S_IRUGO
},
217 .show
= show_modalias_attribute
221 show_config_rom_attribute(struct device
*dev
,
222 struct device_attribute
*attr
, char *buf
)
224 struct fw_device
*device
= fw_device(dev
);
226 memcpy(buf
, device
->config_rom
, device
->config_rom_length
* 4);
228 return device
->config_rom_length
* 4;
231 static struct device_attribute config_rom_attribute
= {
232 .attr
= {.name
= "config_rom",.mode
= S_IRUGO
},
233 .show
= show_config_rom_attribute
,
236 struct read_quadlet_callback_data
{
237 struct completion done
;
243 complete_transaction(struct fw_card
*card
, int rcode
,
244 void *payload
, size_t length
, void *data
)
246 struct read_quadlet_callback_data
*callback_data
= data
;
248 if (rcode
== RCODE_COMPLETE
)
249 callback_data
->data
= be32_to_cpu(*(__be32
*)payload
);
250 callback_data
->rcode
= rcode
;
251 complete(&callback_data
->done
);
254 static int read_rom(struct fw_device
*device
, int index
, u32
* data
)
256 struct read_quadlet_callback_data callback_data
;
257 struct fw_transaction t
;
260 init_completion(&callback_data
.done
);
262 offset
= 0xfffff0000400ULL
+ index
* 4;
263 fw_send_request(device
->card
, &t
, TCODE_READ_QUADLET_REQUEST
,
264 device
->node_id
| LOCAL_BUS
,
265 device
->generation
, SCODE_100
,
266 offset
, NULL
, 4, complete_transaction
, &callback_data
);
268 wait_for_completion(&callback_data
.done
);
270 *data
= callback_data
.data
;
272 return callback_data
.rcode
;
275 static int read_bus_info_block(struct fw_device
*device
)
278 u32 stack
[16], sp
, key
;
281 /* First read the bus info block. */
282 for (i
= 0; i
< 5; i
++) {
283 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
285 /* As per IEEE1212 7.2, during power-up, devices can
286 * reply with a 0 for the first quadlet of the config
287 * rom to indicate that they are booting (for example,
288 * if the firmware is on the disk of a external
289 * harddisk). In that case we just fail, and the
290 * retry mechanism will try again later. */
291 if (i
== 0 && rom
[i
] == 0)
295 /* Now parse the config rom. The config rom is a recursive
296 * directory structure so we parse it using a stack of
297 * references to the blocks that make up the structure. We
298 * push a reference to the root directory on the stack to
299 * start things off. */
302 stack
[sp
++] = 0xc0000005;
304 /* Pop the next block reference of the stack. The
305 * lower 24 bits is the offset into the config rom,
306 * the upper 8 bits are the type of the reference the
310 if (i
>= ARRAY_SIZE(rom
))
311 /* The reference points outside the standard
312 * config rom area, something's fishy. */
315 /* Read header quadlet for the block to get the length. */
316 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
318 end
= i
+ (rom
[i
] >> 16) + 1;
320 if (end
> ARRAY_SIZE(rom
))
321 /* This block extends outside standard config
322 * area (and the array we're reading it
323 * into). That's broken, so ignore this
327 /* Now read in the block. If this is a directory
328 * block, check the entries as we read them to see if
329 * it references another block, and push it in that case. */
331 if (read_rom(device
, i
, &rom
[i
]) != RCODE_COMPLETE
)
333 if ((key
>> 30) == 3 && (rom
[i
] >> 30) > 1 &&
334 sp
< ARRAY_SIZE(stack
))
335 stack
[sp
++] = i
+ rom
[i
];
342 device
->config_rom
= kmalloc(length
* 4, GFP_KERNEL
);
343 if (device
->config_rom
== NULL
)
345 memcpy(device
->config_rom
, rom
, length
* 4);
346 device
->config_rom_length
= length
;
351 static void fw_unit_release(struct device
*dev
)
353 struct fw_unit
*unit
= fw_unit(dev
);
358 static int is_fw_unit(struct device
*dev
)
360 return dev
->release
== fw_unit_release
;
363 static void create_units(struct fw_device
*device
)
365 struct fw_csr_iterator ci
;
366 struct fw_unit
*unit
;
370 fw_csr_iterator_init(&ci
, &device
->config_rom
[5]);
371 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
372 if (key
!= (CSR_UNIT
| CSR_DIRECTORY
))
375 /* Get the address of the unit directory and try to
376 * match the drivers id_tables against it. */
377 unit
= kzalloc(sizeof *unit
, GFP_KERNEL
);
379 fw_error("failed to allocate memory for unit\n");
383 unit
->directory
= ci
.p
+ value
- 1;
384 unit
->device
.bus
= &fw_bus_type
;
385 unit
->device
.release
= fw_unit_release
;
386 unit
->device
.parent
= &device
->device
;
387 snprintf(unit
->device
.bus_id
, sizeof unit
->device
.bus_id
,
388 "%s.%d", device
->device
.bus_id
, i
++);
390 if (device_register(&unit
->device
) < 0) {
395 if (device_create_file(&unit
->device
, &modalias_attribute
) < 0) {
396 device_unregister(&unit
->device
);
402 static int shutdown_unit(struct device
*device
, void *data
)
404 struct fw_unit
*unit
= fw_unit(device
);
406 if (is_fw_unit(device
)) {
407 device_remove_file(&unit
->device
, &modalias_attribute
);
408 device_unregister(&unit
->device
);
414 static void fw_device_shutdown(struct work_struct
*work
)
416 struct fw_device
*device
=
417 container_of(work
, struct fw_device
, work
.work
);
419 device_remove_file(&device
->device
, &config_rom_attribute
);
420 cdev_del(&device
->cdev
);
421 unregister_chrdev_region(device
->device
.devt
, 1);
422 device_for_each_child(&device
->device
, NULL
, shutdown_unit
);
423 device_unregister(&device
->device
);
426 /* These defines control the retry behavior for reading the config
427 * rom. It shouldn't be necessary to tweak these; if the device
428 * doesn't respond to a config rom read within 10 seconds, it's not
429 * going to respond at all. As for the initial delay, a lot of
430 * devices will be able to respond within half a second after bus
431 * reset. On the other hand, it's not really worth being more
432 * aggressive than that, since it scales pretty well; if 10 devices
433 * are plugged in, they're all getting read within one second. */
435 #define MAX_RETRIES 5
436 #define RETRY_DELAY (2 * HZ)
437 #define INITIAL_DELAY (HZ / 2)
439 static void fw_device_init(struct work_struct
*work
)
442 struct fw_device
*device
=
443 container_of(work
, struct fw_device
, work
.work
);
445 /* All failure paths here set node->data to NULL, so that we
446 * don't try to do device_for_each_child() on a kfree()'d
449 if (read_bus_info_block(device
) < 0) {
450 if (device
->config_rom_retries
< MAX_RETRIES
) {
451 device
->config_rom_retries
++;
452 schedule_delayed_work(&device
->work
, RETRY_DELAY
);
454 fw_notify("giving up on config rom for node id %d\n",
456 fw_device_release(&device
->device
);
461 device
->device
.bus
= &fw_bus_type
;
462 device
->device
.release
= fw_device_release
;
463 device
->device
.parent
= device
->card
->device
;
464 snprintf(device
->device
.bus_id
, sizeof device
->device
.bus_id
,
467 if (alloc_chrdev_region(&device
->device
.devt
, 0, 1, "fw")) {
468 fw_error("Failed to register char device region.\n");
472 cdev_init(&device
->cdev
, &fw_device_ops
);
473 device
->cdev
.owner
= THIS_MODULE
;
474 kobject_set_name(&device
->cdev
.kobj
, device
->device
.bus_id
);
475 if (cdev_add(&device
->cdev
, device
->device
.devt
, 1)) {
476 fw_error("Failed to register char device.\n");
480 if (device_add(&device
->device
)) {
481 fw_error("Failed to add device.\n");
485 if (device_create_file(&device
->device
, &config_rom_attribute
) < 0) {
486 fw_error("Failed to create config rom file.\n");
487 goto error_with_device
;
490 create_units(device
);
492 /* Transition the device to running state. If it got pulled
493 * out from under us while we did the intialization work, we
494 * have to shut down the device again here. Normally, though,
495 * fw_node_event will be responsible for shutting it down when
496 * necessary. We have to use the atomic cmpxchg here to avoid
497 * racing with the FW_NODE_DESTROYED case in
498 * fw_node_event(). */
499 if (cmpxchg(&device
->state
,
500 FW_DEVICE_INITIALIZING
,
501 FW_DEVICE_RUNNING
) == FW_DEVICE_SHUTDOWN
)
502 fw_device_shutdown(&device
->work
.work
);
504 fw_notify("created new fw device %s (%d config rom retries)\n",
505 device
->device
.bus_id
, device
->config_rom_retries
);
507 /* Reschedule the IRM work if we just finished reading the
508 * root node config rom. If this races with a bus reset we
509 * just end up running the IRM work a couple of extra times -
510 * pretty harmless. */
511 if (device
->node
== device
->card
->root_node
)
512 schedule_delayed_work(&device
->card
->work
, 0);
517 device_del(&device
->device
);
519 cdev_del(&device
->cdev
);
520 unregister_chrdev_region(device
->device
.devt
, 1);
521 put_device(&device
->device
);
524 static int update_unit(struct device
*dev
, void *data
)
526 struct fw_unit
*unit
= fw_unit(dev
);
527 struct fw_driver
*driver
= (struct fw_driver
*)dev
->driver
;
529 if (is_fw_unit(dev
) && driver
!= NULL
&& driver
->update
!= NULL
)
530 driver
->update(unit
);
535 void fw_node_event(struct fw_card
*card
, struct fw_node
*node
, int event
)
537 struct fw_device
*device
;
539 /* Ignore events for the local node (i.e. the node that
540 * corresponds to the ieee1394 controller in this linux box). */
541 if (node
== card
->local_node
)
545 case FW_NODE_CREATED
:
546 case FW_NODE_LINK_ON
:
550 device
= kzalloc(sizeof(*device
), GFP_ATOMIC
);
554 /* Do minimal intialization of the device here, the
555 * rest will happen in fw_device_init(). We need the
556 * card and node so we can read the config rom and we
557 * need to do device_initialize() now so
558 * device_for_each_child() in FW_NODE_UPDATED is
559 * doesn't freak out. */
560 device_initialize(&device
->device
);
561 device
->state
= FW_DEVICE_INITIALIZING
;
562 device
->card
= fw_card_get(card
);
563 device
->node
= fw_node_get(node
);
564 device
->node_id
= node
->node_id
;
565 device
->generation
= card
->generation
;
567 /* Set the node data to point back to this device so
568 * FW_NODE_UPDATED callbacks can update the node_id
569 * and generation for the device. */
572 /* Many devices are slow to respond after bus resets,
573 * especially if they are bus powered and go through
574 * power-up after getting plugged in. We schedule the
575 * first config rom scan half a second after bus reset. */
576 INIT_DELAYED_WORK(&device
->work
, fw_device_init
);
577 schedule_delayed_work(&device
->work
, INITIAL_DELAY
);
580 case FW_NODE_UPDATED
:
581 if (!node
->link_on
|| node
->data
== NULL
)
585 device
->node_id
= node
->node_id
;
586 device
->generation
= card
->generation
;
587 device_for_each_child(&device
->device
, NULL
, update_unit
);
590 case FW_NODE_DESTROYED
:
591 case FW_NODE_LINK_OFF
:
595 /* Destroy the device associated with the node. There
596 * are two cases here: either the device is fully
597 * initialized (FW_DEVICE_RUNNING) or we're in the
598 * process of reading its config rom
599 * (FW_DEVICE_INITIALIZING). If it is fully
600 * initialized we can reuse device->work to schedule a
601 * full fw_device_shutdown(). If not, there's work
602 * scheduled to read it's config rom, and we just put
603 * the device in shutdown state to have that code fail
604 * to create the device. */
606 if (xchg(&device
->state
,
607 FW_DEVICE_SHUTDOWN
) == FW_DEVICE_RUNNING
) {
608 INIT_DELAYED_WORK(&device
->work
, fw_device_shutdown
);
609 schedule_delayed_work(&device
->work
, 0);