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