aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firewire/fw-device.c
diff options
context:
space:
mode:
authorKristian Høgsberg <krh@redhat.com>2006-12-19 19:58:31 -0500
committerStefan Richter <stefanr@s5r6.in-berlin.de>2007-03-09 16:02:33 -0500
commit19a15b937b26638933307bb02f7b1801310d6eb2 (patch)
tree817efaa8c1d2f4633fa811ba27fa1aee7f00c352 /drivers/firewire/fw-device.c
parent3038e353cfaf548eb94f02b172b9dbe412abd24c (diff)
firewire: Add device probing and sysfs integration.
Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
Diffstat (limited to 'drivers/firewire/fw-device.c')
-rw-r--r--drivers/firewire/fw-device.c613
1 files changed, 613 insertions, 0 deletions
diff --git a/drivers/firewire/fw-device.c b/drivers/firewire/fw-device.c
new file mode 100644
index 000000000000..ec1cb7fc8e96
--- /dev/null
+++ b/drivers/firewire/fw-device.c
@@ -0,0 +1,613 @@
1/* -*- c-basic-offset: 8 -*-
2 *
3 * fw-device.c - Device probing and sysfs code.
4 *
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
6 *
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.
11 *
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.
16 *
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.
20 */
21
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"
31
32void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
33{
34 ci->p = p + 1;
35 ci->end = ci->p + (p[0] >> 16);
36}
37
38EXPORT_SYMBOL(fw_csr_iterator_init);
39
40int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
41{
42 *key = *ci->p >> 24;
43 *value = *ci->p & 0xffffff;
44
45 return ci->p++ < ci->end;
46}
47
48EXPORT_SYMBOL(fw_csr_iterator_next);
49
50static int is_fw_unit(struct device *dev);
51
52static int match_unit_directory(u32 * directory, struct fw_device_id *id)
53{
54 struct fw_csr_iterator ci;
55 int key, value, match;
56
57 match = 0;
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;
68 }
69
70 return (match & id->match_flags) == id->match_flags;
71}
72
73static int fw_unit_match(struct device *dev, struct device_driver *drv)
74{
75 struct fw_unit *unit = fw_unit(dev);
76 struct fw_driver *driver = fw_driver(drv);
77 int i;
78
79 /* We only allow binding to fw_units. */
80 if (!is_fw_unit(dev))
81 return 0;
82
83 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
84 if (match_unit_directory(unit->directory, &driver->id_table[i]))
85 return 1;
86 }
87
88 return 0;
89}
90
91static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
92{
93 struct fw_device *device = fw_device(unit->device.parent);
94 struct fw_csr_iterator ci;
95
96 int key, value;
97 int vendor = 0;
98 int model = 0;
99 int specifier_id = 0;
100 int version = 0;
101
102 fw_csr_iterator_init(&ci, &device->config_rom[5]);
103 while (fw_csr_iterator_next(&ci, &key, &value)) {
104 switch (key) {
105 case CSR_VENDOR:
106 vendor = value;
107 break;
108 case CSR_MODEL:
109 model = value;
110 break;
111 }
112 }
113
114 fw_csr_iterator_init(&ci, unit->directory);
115 while (fw_csr_iterator_next(&ci, &key, &value)) {
116 switch (key) {
117 case CSR_SPECIFIER_ID:
118 specifier_id = value;
119 break;
120 case CSR_VERSION:
121 version = value;
122 break;
123 }
124 }
125
126 return snprintf(buffer, buffer_size,
127 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
128 vendor, model, specifier_id, version);
129}
130
131static int
132fw_unit_uevent(struct device *dev, char **envp, int num_envp,
133 char *buffer, int buffer_size)
134{
135 struct fw_unit *unit = fw_unit(dev);
136 char modalias[64];
137 int length = 0;
138 int i = 0;
139
140 if (!is_fw_unit(dev))
141 goto out;
142
143 get_modalias(unit, modalias, sizeof modalias);
144
145 if (add_uevent_var(envp, num_envp, &i,
146 buffer, buffer_size, &length,
147 "MODALIAS=%s", modalias))
148 return -ENOMEM;
149
150 out:
151 envp[i] = NULL;
152
153 return 0;
154}
155
156struct bus_type fw_bus_type = {
157 .name = "fw",
158 .match = fw_unit_match,
159 .uevent = fw_unit_uevent
160};
161
162EXPORT_SYMBOL(fw_bus_type);
163
164extern struct fw_device *fw_device_get(struct fw_device *device)
165{
166 get_device(&device->device);
167
168 return device;
169}
170
171extern void fw_device_put(struct fw_device *device)
172{
173 put_device(&device->device);
174}
175
176static void fw_device_release(struct device *dev)
177{
178 struct fw_device *device = fw_device(dev);
179 unsigned long flags;
180
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);
186
187 fw_node_put(device->node);
188 fw_card_put(device->card);
189 kfree(device->config_rom);
190 kfree(device);
191}
192
193int fw_device_enable_phys_dma(struct fw_device *device)
194{
195 return device->card->driver->enable_phys_dma(device->card,
196 device->node_id,
197 device->generation);
198}
199
200EXPORT_SYMBOL(fw_device_enable_phys_dma);
201
202static ssize_t
203show_modalias_attribute(struct device *dev,
204 struct device_attribute *attr, char *buf)
205{
206 struct fw_unit *unit = fw_unit(dev);
207 int length;
208
209 length = get_modalias(unit, buf, PAGE_SIZE);
210 strcpy(buf + length, "\n");
211
212 return length + 1;
213}
214
215static struct device_attribute modalias_attribute = {
216 .attr = {.name = "modalias",.mode = S_IRUGO},
217 .show = show_modalias_attribute
218};
219
220static ssize_t
221show_config_rom_attribute(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 struct fw_device *device = fw_device(dev);
225
226 memcpy(buf, device->config_rom, device->config_rom_length * 4);
227
228 return device->config_rom_length * 4;
229}
230
231static struct device_attribute config_rom_attribute = {
232 .attr = {.name = "config_rom",.mode = S_IRUGO},
233 .show = show_config_rom_attribute,
234};
235
236struct read_quadlet_callback_data {
237 struct completion done;
238 int rcode;
239 u32 data;
240};
241
242static void
243complete_transaction(struct fw_card *card, int rcode,
244 void *payload, size_t length, void *data)
245{
246 struct read_quadlet_callback_data *callback_data = data;
247
248 if (rcode == RCODE_COMPLETE)
249 callback_data->data = be32_to_cpu(*(__be32 *)payload);
250 callback_data->rcode = rcode;
251 complete(&callback_data->done);
252}
253
254static int read_rom(struct fw_device *device, int index, u32 * data)
255{
256 struct read_quadlet_callback_data callback_data;
257 struct fw_transaction t;
258 u64 offset;
259
260 init_completion(&callback_data.done);
261
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);
267
268 wait_for_completion(&callback_data.done);
269
270 *data = callback_data.data;
271
272 return callback_data.rcode;
273}
274
275static int read_bus_info_block(struct fw_device *device)
276{
277 static u32 rom[256];
278 u32 stack[16], sp, key;
279 int i, end, length;
280
281 /* First read the bus info block. */
282 for (i = 0; i < 5; i++) {
283 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
284 return -1;
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)
292 return -1;
293 }
294
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. */
300 length = i;
301 sp = 0;
302 stack[sp++] = 0xc0000005;
303 while (sp > 0) {
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
307 * block. */
308 key = stack[--sp];
309 i = key & 0xffffff;
310 if (i >= ARRAY_SIZE(rom))
311 /* The reference points outside the standard
312 * config rom area, something's fishy. */
313 return -1;
314
315 /* Read header quadlet for the block to get the length. */
316 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
317 return -1;
318 end = i + (rom[i] >> 16) + 1;
319 i++;
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
324 * device. */
325 return -1;
326
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. */
330 while (i < end) {
331 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
332 return -1;
333 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
334 sp < ARRAY_SIZE(stack))
335 stack[sp++] = i + rom[i];
336 i++;
337 }
338 if (length < i)
339 length = i;
340 }
341
342 device->config_rom = kmalloc(length * 4, GFP_KERNEL);
343 if (device->config_rom == NULL)
344 return -1;
345 memcpy(device->config_rom, rom, length * 4);
346 device->config_rom_length = length;
347
348 return 0;
349}
350
351static void fw_unit_release(struct device *dev)
352{
353 struct fw_unit *unit = fw_unit(dev);
354
355 kfree(unit);
356}
357
358static int is_fw_unit(struct device *dev)
359{
360 return dev->release == fw_unit_release;
361}
362
363static void create_units(struct fw_device *device)
364{
365 struct fw_csr_iterator ci;
366 struct fw_unit *unit;
367 int key, value, i;
368
369 i = 0;
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))
373 continue;
374
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);
378 if (unit == NULL) {
379 fw_error("failed to allocate memory for unit\n");
380 continue;
381 }
382
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++);
389
390 if (device_register(&unit->device) < 0) {
391 kfree(unit);
392 continue;
393 }
394
395 if (device_create_file(&unit->device, &modalias_attribute) < 0) {
396 device_unregister(&unit->device);
397 kfree(unit);
398 }
399 }
400}
401
402static int shutdown_unit(struct device *device, void *data)
403{
404 struct fw_unit *unit = fw_unit(device);
405
406 if (is_fw_unit(device)) {
407 device_remove_file(&unit->device, &modalias_attribute);
408 device_unregister(&unit->device);
409 }
410
411 return 0;
412}
413
414static void fw_device_shutdown(struct work_struct *work)
415{
416 struct fw_device *device =
417 container_of(work, struct fw_device, work.work);
418
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);
424}
425
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. */
434
435#define MAX_RETRIES 5
436#define RETRY_DELAY (2 * HZ)
437#define INITIAL_DELAY (HZ / 2)
438
439static void fw_device_init(struct work_struct *work)
440{
441 static int serial;
442 struct fw_device *device =
443 container_of(work, struct fw_device, work.work);
444
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
447 * device. */
448
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);
453 } else {
454 fw_notify("giving up on config rom for node id %d\n",
455 device->node_id);
456 fw_device_release(&device->device);
457 }
458 return;
459 }
460
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,
465 "fw%d", serial++);
466
467 if (alloc_chrdev_region(&device->device.devt, 0, 1, "fw")) {
468 fw_error("Failed to register char device region.\n");
469 goto error;
470 }
471
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");
477 goto error;
478 }
479
480 if (device_add(&device->device)) {
481 fw_error("Failed to add device.\n");
482 goto error;
483 }
484
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;
488 }
489
490 create_units(device);
491
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);
503 else
504 fw_notify("created new fw device %s (%d config rom retries)\n",
505 device->device.bus_id, device->config_rom_retries);
506
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);
513
514 return;
515
516 error_with_device:
517 device_del(&device->device);
518 error:
519 cdev_del(&device->cdev);
520 unregister_chrdev_region(device->device.devt, 1);
521 put_device(&device->device);
522}
523
524static int update_unit(struct device *dev, void *data)
525{
526 struct fw_unit *unit = fw_unit(dev);
527 struct fw_driver *driver = (struct fw_driver *)dev->driver;
528
529 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL)
530 driver->update(unit);
531
532 return 0;
533}
534
535void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
536{
537 struct fw_device *device;
538
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)
542 return;
543
544 switch (event) {
545 case FW_NODE_CREATED:
546 case FW_NODE_LINK_ON:
547 if (!node->link_on)
548 break;
549
550 device = kzalloc(sizeof(*device), GFP_ATOMIC);
551 if (device == NULL)
552 break;
553
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;
566
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. */
570 node->data = device;
571
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);
578 break;
579
580 case FW_NODE_UPDATED:
581 if (!node->link_on || node->data == NULL)
582 break;
583
584 device = node->data;
585 device->node_id = node->node_id;
586 device->generation = card->generation;
587 device_for_each_child(&device->device, NULL, update_unit);
588 break;
589
590 case FW_NODE_DESTROYED:
591 case FW_NODE_LINK_OFF:
592 if (!node->data)
593 break;
594
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. */
605 device = node->data;
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);
610 }
611 break;
612 }
613}