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