firewire: Add rom_index attribute for unit sysfs directories.
[deliverable/linux.git] / drivers / firewire / fw-device.c
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 <linux/idr.h>
29 #include "fw-transaction.h"
30 #include "fw-topology.h"
31 #include "fw-device.h"
32
33 void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
34 {
35 ci->p = p + 1;
36 ci->end = ci->p + (p[0] >> 16);
37 }
38 EXPORT_SYMBOL(fw_csr_iterator_init);
39
40 int 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 EXPORT_SYMBOL(fw_csr_iterator_next);
48
49 static int is_fw_unit(struct device *dev);
50
51 static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
52 {
53 struct fw_csr_iterator ci;
54 int key, value, match;
55
56 match = 0;
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;
67 }
68
69 return (match & id->match_flags) == id->match_flags;
70 }
71
72 static int fw_unit_match(struct device *dev, struct device_driver *drv)
73 {
74 struct fw_unit *unit = fw_unit(dev);
75 struct fw_driver *driver = fw_driver(drv);
76 int i;
77
78 /* We only allow binding to fw_units. */
79 if (!is_fw_unit(dev))
80 return 0;
81
82 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
83 if (match_unit_directory(unit->directory, &driver->id_table[i]))
84 return 1;
85 }
86
87 return 0;
88 }
89
90 static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
91 {
92 struct fw_device *device = fw_device(unit->device.parent);
93 struct fw_csr_iterator ci;
94
95 int key, value;
96 int vendor = 0;
97 int model = 0;
98 int specifier_id = 0;
99 int version = 0;
100
101 fw_csr_iterator_init(&ci, &device->config_rom[5]);
102 while (fw_csr_iterator_next(&ci, &key, &value)) {
103 switch (key) {
104 case CSR_VENDOR:
105 vendor = value;
106 break;
107 case CSR_MODEL:
108 model = value;
109 break;
110 }
111 }
112
113 fw_csr_iterator_init(&ci, unit->directory);
114 while (fw_csr_iterator_next(&ci, &key, &value)) {
115 switch (key) {
116 case CSR_SPECIFIER_ID:
117 specifier_id = value;
118 break;
119 case CSR_VERSION:
120 version = value;
121 break;
122 }
123 }
124
125 return snprintf(buffer, buffer_size,
126 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
127 vendor, model, specifier_id, version);
128 }
129
130 static int
131 fw_unit_uevent(struct device *dev, char **envp, int num_envp,
132 char *buffer, int buffer_size)
133 {
134 struct fw_unit *unit = fw_unit(dev);
135 char modalias[64];
136 int length = 0;
137 int i = 0;
138
139 if (!is_fw_unit(dev))
140 goto out;
141
142 get_modalias(unit, modalias, sizeof modalias);
143
144 if (add_uevent_var(envp, num_envp, &i,
145 buffer, buffer_size, &length,
146 "MODALIAS=%s", modalias))
147 return -ENOMEM;
148
149 out:
150 envp[i] = NULL;
151
152 return 0;
153 }
154
155 struct bus_type fw_bus_type = {
156 .name = "firewire",
157 .match = fw_unit_match,
158 .uevent = fw_unit_uevent,
159 };
160 EXPORT_SYMBOL(fw_bus_type);
161
162 extern struct fw_device *fw_device_get(struct fw_device *device)
163 {
164 get_device(&device->device);
165
166 return device;
167 }
168
169 extern void fw_device_put(struct fw_device *device)
170 {
171 put_device(&device->device);
172 }
173
174 static void fw_device_release(struct device *dev)
175 {
176 struct fw_device *device = fw_device(dev);
177 unsigned long flags;
178
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);
184
185 fw_node_put(device->node);
186 fw_card_put(device->card);
187 kfree(device->config_rom);
188 kfree(device);
189 }
190
191 int fw_device_enable_phys_dma(struct fw_device *device)
192 {
193 return device->card->driver->enable_phys_dma(device->card,
194 device->node_id,
195 device->generation);
196 }
197 EXPORT_SYMBOL(fw_device_enable_phys_dma);
198
199 static ssize_t
200 show_modalias_attribute(struct device *dev,
201 struct device_attribute *attr, char *buf)
202 {
203 struct fw_unit *unit = fw_unit(dev);
204 int length;
205
206 length = get_modalias(unit, buf, PAGE_SIZE);
207 strcpy(buf + length, "\n");
208
209 return length + 1;
210 }
211
212 static struct device_attribute modalias_attribute = {
213 .attr = { .name = "modalias", .mode = S_IRUGO, },
214 .show = show_modalias_attribute,
215 };
216
217 static ssize_t
218 show_config_rom_attribute(struct device *dev,
219 struct device_attribute *attr, char *buf)
220 {
221 struct fw_device *device = fw_device(dev);
222
223 memcpy(buf, device->config_rom, device->config_rom_length * 4);
224
225 return device->config_rom_length * 4;
226 }
227
228 static struct device_attribute config_rom_attribute = {
229 .attr = {.name = "config_rom", .mode = S_IRUGO,},
230 .show = show_config_rom_attribute,
231 };
232
233 static ssize_t
234 show_rom_index_attribute(struct device *dev,
235 struct device_attribute *attr, char *buf)
236 {
237 struct fw_device *device = fw_device(dev->parent);
238 struct fw_unit *unit = fw_unit(dev);
239
240 return snprintf(buf, PAGE_SIZE, "%d\n",
241 unit->directory - device->config_rom);
242 }
243
244 static struct device_attribute rom_index_attribute = {
245 .attr = { .name = "rom_index", .mode = S_IRUGO, },
246 .show = show_rom_index_attribute,
247 };
248
249 struct read_quadlet_callback_data {
250 struct completion done;
251 int rcode;
252 u32 data;
253 };
254
255 static void
256 complete_transaction(struct fw_card *card, int rcode,
257 void *payload, size_t length, void *data)
258 {
259 struct read_quadlet_callback_data *callback_data = data;
260
261 if (rcode == RCODE_COMPLETE)
262 callback_data->data = be32_to_cpu(*(__be32 *)payload);
263 callback_data->rcode = rcode;
264 complete(&callback_data->done);
265 }
266
267 static int read_rom(struct fw_device *device, int index, u32 * data)
268 {
269 struct read_quadlet_callback_data callback_data;
270 struct fw_transaction t;
271 u64 offset;
272
273 init_completion(&callback_data.done);
274
275 offset = 0xfffff0000400ULL + index * 4;
276 fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
277 device->node_id,
278 device->generation, SCODE_100,
279 offset, NULL, 4, complete_transaction, &callback_data);
280
281 wait_for_completion(&callback_data.done);
282
283 *data = callback_data.data;
284
285 return callback_data.rcode;
286 }
287
288 static int read_bus_info_block(struct fw_device *device)
289 {
290 static u32 rom[256];
291 u32 stack[16], sp, key;
292 int i, end, length;
293
294 /* First read the bus info block. */
295 for (i = 0; i < 5; i++) {
296 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
297 return -1;
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)
305 return -1;
306 }
307
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. */
313 length = i;
314 sp = 0;
315 stack[sp++] = 0xc0000005;
316 while (sp > 0) {
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
320 * block. */
321 key = stack[--sp];
322 i = key & 0xffffff;
323 if (i >= ARRAY_SIZE(rom))
324 /* The reference points outside the standard
325 * config rom area, something's fishy. */
326 return -1;
327
328 /* Read header quadlet for the block to get the length. */
329 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
330 return -1;
331 end = i + (rom[i] >> 16) + 1;
332 i++;
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
337 * device. */
338 return -1;
339
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. */
343 while (i < end) {
344 if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
345 return -1;
346 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
347 sp < ARRAY_SIZE(stack))
348 stack[sp++] = i + rom[i];
349 i++;
350 }
351 if (length < i)
352 length = i;
353 }
354
355 device->config_rom = kmalloc(length * 4, GFP_KERNEL);
356 if (device->config_rom == NULL)
357 return -1;
358 memcpy(device->config_rom, rom, length * 4);
359 device->config_rom_length = length;
360
361 return 0;
362 }
363
364 static void fw_unit_release(struct device *dev)
365 {
366 struct fw_unit *unit = fw_unit(dev);
367
368 kfree(unit);
369 }
370
371 static int is_fw_unit(struct device *dev)
372 {
373 return dev->release == fw_unit_release;
374 }
375
376 static void create_units(struct fw_device *device)
377 {
378 struct fw_csr_iterator ci;
379 struct fw_unit *unit;
380 int key, value, i;
381
382 i = 0;
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))
386 continue;
387
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);
391 if (unit == NULL) {
392 fw_error("failed to allocate memory for unit\n");
393 continue;
394 }
395
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++);
402
403 if (device_register(&unit->device) < 0) {
404 kfree(unit);
405 continue;
406 }
407
408 if (device_create_file(&unit->device, &modalias_attribute) < 0) {
409 device_unregister(&unit->device);
410 kfree(unit);
411 }
412
413 if (device_create_file(&unit->device, &rom_index_attribute) < 0) {
414 device_unregister(&unit->device);
415 kfree(unit);
416 }
417 }
418 }
419
420 static int shutdown_unit(struct device *device, void *data)
421 {
422 struct fw_unit *unit = fw_unit(device);
423
424 if (is_fw_unit(device)) {
425 device_remove_file(&unit->device, &modalias_attribute);
426 device_unregister(&unit->device);
427 }
428
429 return 0;
430 }
431
432 static DEFINE_IDR(fw_device_idr);
433 int fw_cdev_major;
434
435 struct fw_device *fw_device_from_devt(dev_t devt)
436 {
437 struct fw_device *device;
438
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);
442
443 return device;
444 }
445
446 static void fw_device_shutdown(struct work_struct *work)
447 {
448 struct fw_device *device =
449 container_of(work, struct fw_device, work.work);
450 int minor = MINOR(device->device.devt);
451
452 down_write(&fw_bus_type.subsys.rwsem);
453 idr_remove(&fw_device_idr, minor);
454 up_write(&fw_bus_type.subsys.rwsem);
455
456 device_remove_file(&device->device, &config_rom_attribute);
457 device_for_each_child(&device->device, NULL, shutdown_unit);
458 device_unregister(&device->device);
459 }
460
461 /* These defines control the retry behavior for reading the config
462 * rom. It shouldn't be necessary to tweak these; if the device
463 * doesn't respond to a config rom read within 10 seconds, it's not
464 * going to respond at all. As for the initial delay, a lot of
465 * devices will be able to respond within half a second after bus
466 * reset. On the other hand, it's not really worth being more
467 * aggressive than that, since it scales pretty well; if 10 devices
468 * are plugged in, they're all getting read within one second. */
469
470 #define MAX_RETRIES 5
471 #define RETRY_DELAY (2 * HZ)
472 #define INITIAL_DELAY (HZ / 2)
473
474 static void fw_device_init(struct work_struct *work)
475 {
476 struct fw_device *device =
477 container_of(work, struct fw_device, work.work);
478 int minor, err;
479
480 /* All failure paths here set node->data to NULL, so that we
481 * don't try to do device_for_each_child() on a kfree()'d
482 * device. */
483
484 if (read_bus_info_block(device) < 0) {
485 if (device->config_rom_retries < MAX_RETRIES) {
486 device->config_rom_retries++;
487 schedule_delayed_work(&device->work, RETRY_DELAY);
488 } else {
489 fw_notify("giving up on config rom for node id %x\n",
490 device->node_id);
491 if (device->node == device->card->root_node)
492 schedule_delayed_work(&device->card->work, 0);
493 fw_device_release(&device->device);
494 }
495 return;
496 }
497
498 err = -ENOMEM;
499 down_write(&fw_bus_type.subsys.rwsem);
500 if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
501 err = idr_get_new(&fw_device_idr, device, &minor);
502 up_write(&fw_bus_type.subsys.rwsem);
503 if (err < 0)
504 goto error;
505
506 device->device.bus = &fw_bus_type;
507 device->device.release = fw_device_release;
508 device->device.parent = device->card->device;
509 device->device.devt = MKDEV(fw_cdev_major, minor);
510 snprintf(device->device.bus_id, sizeof device->device.bus_id,
511 "fw%d", minor);
512
513 if (device_add(&device->device)) {
514 fw_error("Failed to add device.\n");
515 goto error_with_cdev;
516 }
517
518 if (device_create_file(&device->device, &config_rom_attribute) < 0) {
519 fw_error("Failed to create config rom file.\n");
520 goto error_with_device;
521 }
522
523 create_units(device);
524
525 /* Transition the device to running state. If it got pulled
526 * out from under us while we did the intialization work, we
527 * have to shut down the device again here. Normally, though,
528 * fw_node_event will be responsible for shutting it down when
529 * necessary. We have to use the atomic cmpxchg here to avoid
530 * racing with the FW_NODE_DESTROYED case in
531 * fw_node_event(). */
532 if (atomic_cmpxchg(&device->state,
533 FW_DEVICE_INITIALIZING,
534 FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
535 fw_device_shutdown(&device->work.work);
536 else
537 fw_notify("created new fw device %s (%d config rom retries)\n",
538 device->device.bus_id, device->config_rom_retries);
539
540 /* Reschedule the IRM work if we just finished reading the
541 * root node config rom. If this races with a bus reset we
542 * just end up running the IRM work a couple of extra times -
543 * pretty harmless. */
544 if (device->node == device->card->root_node)
545 schedule_delayed_work(&device->card->work, 0);
546
547 return;
548
549 error_with_device:
550 device_del(&device->device);
551 error_with_cdev:
552 down_write(&fw_bus_type.subsys.rwsem);
553 idr_remove(&fw_device_idr, minor);
554 up_write(&fw_bus_type.subsys.rwsem);
555 error:
556 put_device(&device->device);
557 }
558
559 static int update_unit(struct device *dev, void *data)
560 {
561 struct fw_unit *unit = fw_unit(dev);
562 struct fw_driver *driver = (struct fw_driver *)dev->driver;
563
564 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL)
565 driver->update(unit);
566
567 return 0;
568 }
569
570 static void fw_device_update(struct work_struct *work)
571 {
572 struct fw_device *device =
573 container_of(work, struct fw_device, work.work);
574
575 fw_device_cdev_update(device);
576 device_for_each_child(&device->device, NULL, update_unit);
577 }
578
579 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
580 {
581 struct fw_device *device;
582
583 switch (event) {
584 case FW_NODE_CREATED:
585 case FW_NODE_LINK_ON:
586 if (!node->link_on)
587 break;
588
589 device = kzalloc(sizeof(*device), GFP_ATOMIC);
590 if (device == NULL)
591 break;
592
593 /* Do minimal intialization of the device here, the
594 * rest will happen in fw_device_init(). We need the
595 * card and node so we can read the config rom and we
596 * need to do device_initialize() now so
597 * device_for_each_child() in FW_NODE_UPDATED is
598 * doesn't freak out. */
599 device_initialize(&device->device);
600 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
601 device->card = fw_card_get(card);
602 device->node = fw_node_get(node);
603 device->node_id = node->node_id;
604 device->generation = card->generation;
605 INIT_LIST_HEAD(&device->client_list);
606
607 /* Set the node data to point back to this device so
608 * FW_NODE_UPDATED callbacks can update the node_id
609 * and generation for the device. */
610 node->data = device;
611
612 /* Many devices are slow to respond after bus resets,
613 * especially if they are bus powered and go through
614 * power-up after getting plugged in. We schedule the
615 * first config rom scan half a second after bus reset. */
616 INIT_DELAYED_WORK(&device->work, fw_device_init);
617 schedule_delayed_work(&device->work, INITIAL_DELAY);
618 break;
619
620 case FW_NODE_UPDATED:
621 if (!node->link_on || node->data == NULL)
622 break;
623
624 device = node->data;
625 device->node_id = node->node_id;
626 device->generation = card->generation;
627 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
628 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
629 schedule_delayed_work(&device->work, 0);
630 }
631 break;
632
633 case FW_NODE_DESTROYED:
634 case FW_NODE_LINK_OFF:
635 if (!node->data)
636 break;
637
638 /* Destroy the device associated with the node. There
639 * are two cases here: either the device is fully
640 * initialized (FW_DEVICE_RUNNING) or we're in the
641 * process of reading its config rom
642 * (FW_DEVICE_INITIALIZING). If it is fully
643 * initialized we can reuse device->work to schedule a
644 * full fw_device_shutdown(). If not, there's work
645 * scheduled to read it's config rom, and we just put
646 * the device in shutdown state to have that code fail
647 * to create the device. */
648 device = node->data;
649 if (atomic_xchg(&device->state,
650 FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
651 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
652 schedule_delayed_work(&device->work, 0);
653 }
654 break;
655 }
656 }
This page took 0.057587 seconds and 5 git commands to generate.