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