]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/firewire/fw-device.c
firewire: Use only a wait queue and terminate poll and read on device removal.
[linux-2.6-omap-h63xx.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         fw_device_cdev_remove(device);
457         device_remove_file(&device->device, &config_rom_attribute);
458         device_for_each_child(&device->device, NULL, shutdown_unit);
459         device_unregister(&device->device);
460 }
461
462 /* These defines control the retry behavior for reading the config
463  * rom.  It shouldn't be necessary to tweak these; if the device
464  * doesn't respond to a config rom read within 10 seconds, it's not
465  * going to respond at all.  As for the initial delay, a lot of
466  * devices will be able to respond within half a second after bus
467  * reset.  On the other hand, it's not really worth being more
468  * aggressive than that, since it scales pretty well; if 10 devices
469  * are plugged in, they're all getting read within one second. */
470
471 #define MAX_RETRIES     5
472 #define RETRY_DELAY     (2 * HZ)
473 #define INITIAL_DELAY   (HZ / 2)
474
475 static void fw_device_init(struct work_struct *work)
476 {
477         struct fw_device *device =
478                 container_of(work, struct fw_device, work.work);
479         int minor, err;
480
481         /* All failure paths here set node->data to NULL, so that we
482          * don't try to do device_for_each_child() on a kfree()'d
483          * device. */
484
485         if (read_bus_info_block(device) < 0) {
486                 if (device->config_rom_retries < MAX_RETRIES) {
487                         device->config_rom_retries++;
488                         schedule_delayed_work(&device->work, RETRY_DELAY);
489                 } else {
490                         fw_notify("giving up on config rom for node id %x\n",
491                                   device->node_id);
492                         if (device->node == device->card->root_node)
493                                 schedule_delayed_work(&device->card->work, 0);
494                         fw_device_release(&device->device);
495                 }
496                 return;
497         }
498
499         err = -ENOMEM;
500         down_write(&fw_bus_type.subsys.rwsem);
501         if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
502                 err = idr_get_new(&fw_device_idr, device, &minor);
503         up_write(&fw_bus_type.subsys.rwsem);
504         if (err < 0)
505                 goto error;
506
507         device->device.bus = &fw_bus_type;
508         device->device.release = fw_device_release;
509         device->device.parent = device->card->device;
510         device->device.devt = MKDEV(fw_cdev_major, minor);
511         snprintf(device->device.bus_id, sizeof device->device.bus_id,
512                  "fw%d", minor);
513
514         if (device_add(&device->device)) {
515                 fw_error("Failed to add device.\n");
516                 goto error_with_cdev;
517         }
518
519         if (device_create_file(&device->device, &config_rom_attribute) < 0) {
520                 fw_error("Failed to create config rom file.\n");
521                 goto error_with_device;
522         }
523
524         create_units(device);
525
526         /* Transition the device to running state.  If it got pulled
527          * out from under us while we did the intialization work, we
528          * have to shut down the device again here.  Normally, though,
529          * fw_node_event will be responsible for shutting it down when
530          * necessary.  We have to use the atomic cmpxchg here to avoid
531          * racing with the FW_NODE_DESTROYED case in
532          * fw_node_event(). */
533         if (atomic_cmpxchg(&device->state,
534                     FW_DEVICE_INITIALIZING,
535                     FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
536                 fw_device_shutdown(&device->work.work);
537         else
538                 fw_notify("created new fw device %s (%d config rom retries)\n",
539                           device->device.bus_id, device->config_rom_retries);
540
541         /* Reschedule the IRM work if we just finished reading the
542          * root node config rom.  If this races with a bus reset we
543          * just end up running the IRM work a couple of extra times -
544          * pretty harmless. */
545         if (device->node == device->card->root_node)
546                 schedule_delayed_work(&device->card->work, 0);
547
548         return;
549
550  error_with_device:
551         device_del(&device->device);
552  error_with_cdev:
553         down_write(&fw_bus_type.subsys.rwsem);
554         idr_remove(&fw_device_idr, minor);
555         up_write(&fw_bus_type.subsys.rwsem);
556  error:
557         put_device(&device->device);
558 }
559
560 static int update_unit(struct device *dev, void *data)
561 {
562         struct fw_unit *unit = fw_unit(dev);
563         struct fw_driver *driver = (struct fw_driver *)dev->driver;
564
565         if (is_fw_unit(dev) && driver != NULL && driver->update != NULL)
566                 driver->update(unit);
567
568         return 0;
569 }
570
571 static void fw_device_update(struct work_struct *work)
572 {
573         struct fw_device *device =
574                 container_of(work, struct fw_device, work.work);
575
576         fw_device_cdev_update(device);
577         device_for_each_child(&device->device, NULL, update_unit);
578 }
579
580 void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
581 {
582         struct fw_device *device;
583
584         switch (event) {
585         case FW_NODE_CREATED:
586         case FW_NODE_LINK_ON:
587                 if (!node->link_on)
588                         break;
589
590                 device = kzalloc(sizeof(*device), GFP_ATOMIC);
591                 if (device == NULL)
592                         break;
593
594                 /* Do minimal intialization of the device here, the
595                  * rest will happen in fw_device_init().  We need the
596                  * card and node so we can read the config rom and we
597                  * need to do device_initialize() now so
598                  * device_for_each_child() in FW_NODE_UPDATED is
599                  * doesn't freak out. */
600                 device_initialize(&device->device);
601                 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
602                 device->card = fw_card_get(card);
603                 device->node = fw_node_get(node);
604                 device->node_id = node->node_id;
605                 device->generation = card->generation;
606                 INIT_LIST_HEAD(&device->client_list);
607
608                 /* Set the node data to point back to this device so
609                  * FW_NODE_UPDATED callbacks can update the node_id
610                  * and generation for the device. */
611                 node->data = device;
612
613                 /* Many devices are slow to respond after bus resets,
614                  * especially if they are bus powered and go through
615                  * power-up after getting plugged in.  We schedule the
616                  * first config rom scan half a second after bus reset. */
617                 INIT_DELAYED_WORK(&device->work, fw_device_init);
618                 schedule_delayed_work(&device->work, INITIAL_DELAY);
619                 break;
620
621         case FW_NODE_UPDATED:
622                 if (!node->link_on || node->data == NULL)
623                         break;
624
625                 device = node->data;
626                 device->node_id = node->node_id;
627                 device->generation = card->generation;
628                 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
629                         PREPARE_DELAYED_WORK(&device->work, fw_device_update);
630                         schedule_delayed_work(&device->work, 0);
631                 }
632                 break;
633
634         case FW_NODE_DESTROYED:
635         case FW_NODE_LINK_OFF:
636                 if (!node->data)
637                         break;
638
639                 /* Destroy the device associated with the node.  There
640                  * are two cases here: either the device is fully
641                  * initialized (FW_DEVICE_RUNNING) or we're in the
642                  * process of reading its config rom
643                  * (FW_DEVICE_INITIALIZING).  If it is fully
644                  * initialized we can reuse device->work to schedule a
645                  * full fw_device_shutdown().  If not, there's work
646                  * scheduled to read it's config rom, and we just put
647                  * the device in shutdown state to have that code fail
648                  * to create the device. */
649                 device = node->data;
650                 if (atomic_xchg(&device->state,
651                                 FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
652                         PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
653                         schedule_delayed_work(&device->work, 0);
654                 }
655                 break;
656         }
657 }