]> err.no Git - linux-2.6/blob - drivers/firewire/fw-transaction.c
firewire: Implement CSR cycle time and bus time registers.
[linux-2.6] / drivers / firewire / fw-transaction.c
1 /*                                              -*- c-basic-offset: 8 -*-
2  *
3  * fw-transaction.c - core IEEE1394 transaction logic
4  *
5  * Copyright (C) 2004-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/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/delay.h>
28 #include <linux/poll.h>
29 #include <linux/list.h>
30 #include <linux/kthread.h>
31 #include <asm/uaccess.h>
32 #include <asm/semaphore.h>
33
34 #include "fw-transaction.h"
35 #include "fw-topology.h"
36 #include "fw-device.h"
37
38 #define header_pri(pri)                 ((pri) << 0)
39 #define header_tcode(tcode)             ((tcode) << 4)
40 #define header_retry(retry)             ((retry) << 8)
41 #define header_tlabel(tlabel)           ((tlabel) << 10)
42 #define header_destination(destination) ((destination) << 16)
43 #define header_source(source)           ((source) << 16)
44 #define header_rcode(rcode)             ((rcode) << 12)
45 #define header_offset_high(offset_high) ((offset_high) << 0)
46 #define header_data_length(length)      ((length) << 16)
47 #define header_extended_tcode(tcode)    ((tcode) << 0)
48
49 #define header_get_tcode(q)             (((q) >> 4) & 0x0f)
50 #define header_get_tlabel(q)            (((q) >> 10) & 0x3f)
51 #define header_get_rcode(q)             (((q) >> 12) & 0x0f)
52 #define header_get_destination(q)       (((q) >> 16) & 0xffff)
53 #define header_get_source(q)            (((q) >> 16) & 0xffff)
54 #define header_get_offset_high(q)       (((q) >> 0) & 0xffff)
55 #define header_get_data_length(q)       (((q) >> 16) & 0xffff)
56 #define header_get_extended_tcode(q)    (((q) >> 0) & 0xffff)
57
58 #define phy_config_gap_count(gap_count) (((gap_count) << 16) | (1 << 22))
59 #define phy_config_root_id(node_id)     ((((node_id) & 0x3f) << 24) | (1 << 23))
60 #define phy_identifier(id)              ((id) << 30)
61
62 static int
63 close_transaction(struct fw_transaction *transaction,
64                   struct fw_card *card, int rcode,
65                   u32 *payload, size_t length)
66 {
67         struct fw_transaction *t;
68         unsigned long flags;
69
70         spin_lock_irqsave(&card->lock, flags);
71         list_for_each_entry(t, &card->transaction_list, link) {
72                 if (t == transaction) {
73                         list_del(&t->link);
74                         card->tlabel_mask &= ~(1 << t->tlabel);
75                         break;
76                 }
77         }
78         spin_unlock_irqrestore(&card->lock, flags);
79
80         if (&t->link != &card->transaction_list) {
81                 t->callback(card, rcode, payload, length, t->callback_data);
82                 return 0;
83         }
84
85         return -ENOENT;
86 }
87
88 /* Only valid for transactions that are potentially pending (ie have
89  * been sent). */
90 int
91 fw_cancel_transaction(struct fw_card *card,
92                       struct fw_transaction *transaction)
93 {
94         /* Cancel the packet transmission if it's still queued.  That
95          * will call the packet transmission callback which cancels
96          * the transaction. */
97
98         if (card->driver->cancel_packet(card, &transaction->packet) == 0)
99                 return 0;
100
101         /* If the request packet has already been sent, we need to see
102          * if the transaction is still pending and remove it in that case. */
103
104         return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);
105 }
106 EXPORT_SYMBOL(fw_cancel_transaction);
107
108 static void
109 transmit_complete_callback(struct fw_packet *packet,
110                            struct fw_card *card, int status)
111 {
112         struct fw_transaction *t =
113             container_of(packet, struct fw_transaction, packet);
114
115         switch (status) {
116         case ACK_COMPLETE:
117                 close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
118                 break;
119         case ACK_PENDING:
120                 t->timestamp = packet->timestamp;
121                 break;
122         case ACK_BUSY_X:
123         case ACK_BUSY_A:
124         case ACK_BUSY_B:
125                 close_transaction(t, card, RCODE_BUSY, NULL, 0);
126                 break;
127         case ACK_DATA_ERROR:
128                 close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0);
129                 break;
130         case ACK_TYPE_ERROR:
131                 close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0);
132                 break;
133         default:
134                 /* In this case the ack is really a juju specific
135                  * rcode, so just forward that to the callback. */
136                 close_transaction(t, card, status, NULL, 0);
137                 break;
138         }
139 }
140
141 static void
142 fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
143                 int node_id, int source_id, int generation, int speed,
144                 unsigned long long offset, void *payload, size_t length)
145 {
146         int ext_tcode;
147
148         if (tcode > 0x10) {
149                 ext_tcode = tcode - 0x10;
150                 tcode = TCODE_LOCK_REQUEST;
151         } else
152                 ext_tcode = 0;
153
154         packet->header[0] =
155                 header_retry(RETRY_X) |
156                 header_tlabel(tlabel) |
157                 header_tcode(tcode) |
158                 header_destination(node_id);
159         packet->header[1] =
160                 header_offset_high(offset >> 32) | header_source(source_id);
161         packet->header[2] =
162                 offset;
163
164         switch (tcode) {
165         case TCODE_WRITE_QUADLET_REQUEST:
166                 packet->header[3] = *(u32 *)payload;
167                 packet->header_length = 16;
168                 packet->payload_length = 0;
169                 break;
170
171         case TCODE_LOCK_REQUEST:
172         case TCODE_WRITE_BLOCK_REQUEST:
173                 packet->header[3] =
174                         header_data_length(length) |
175                         header_extended_tcode(ext_tcode);
176                 packet->header_length = 16;
177                 packet->payload = payload;
178                 packet->payload_length = length;
179                 break;
180
181         case TCODE_READ_QUADLET_REQUEST:
182                 packet->header_length = 12;
183                 packet->payload_length = 0;
184                 break;
185
186         case TCODE_READ_BLOCK_REQUEST:
187                 packet->header[3] =
188                         header_data_length(length) |
189                         header_extended_tcode(ext_tcode);
190                 packet->header_length = 16;
191                 packet->payload_length = 0;
192                 break;
193         }
194
195         packet->speed = speed;
196         packet->generation = generation;
197         packet->ack = 0;
198 }
199
200 /**
201  * This function provides low-level access to the IEEE1394 transaction
202  * logic.  Most C programs would use either fw_read(), fw_write() or
203  * fw_lock() instead - those function are convenience wrappers for
204  * this function.  The fw_send_request() function is primarily
205  * provided as a flexible, one-stop entry point for languages bindings
206  * and protocol bindings.
207  *
208  * FIXME: Document this function further, in particular the possible
209  * values for rcode in the callback.  In short, we map ACK_COMPLETE to
210  * RCODE_COMPLETE, internal errors set errno and set rcode to
211  * RCODE_SEND_ERROR (which is out of range for standard ieee1394
212  * rcodes).  All other rcodes are forwarded unchanged.  For all
213  * errors, payload is NULL, length is 0.
214  *
215  * Can not expect the callback to be called before the function
216  * returns, though this does happen in some cases (ACK_COMPLETE and
217  * errors).
218  *
219  * The payload is only used for write requests and must not be freed
220  * until the callback has been called.
221  *
222  * @param card the card from which to send the request
223  * @param tcode the tcode for this transaction.  Do not use
224  *   TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP
225  *   etc. to specify tcode and ext_tcode.
226  * @param node_id the destination node ID (bus ID and PHY ID concatenated)
227  * @param generation the generation for which node_id is valid
228  * @param speed the speed to use for sending the request
229  * @param offset the 48 bit offset on the destination node
230  * @param payload the data payload for the request subaction
231  * @param length the length in bytes of the data to read
232  * @param callback function to be called when the transaction is completed
233  * @param callback_data pointer to arbitrary data, which will be
234  *   passed to the callback
235  */
236 void
237 fw_send_request(struct fw_card *card, struct fw_transaction *t,
238                 int tcode, int node_id, int generation, int speed,
239                 unsigned long long offset,
240                 void *payload, size_t length,
241                 fw_transaction_callback_t callback, void *callback_data)
242 {
243         unsigned long flags;
244         int tlabel, source;
245
246         /* Bump the flush timer up 100ms first of all so we
247          * don't race with a flush timer callback. */
248
249         mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
250
251         /* Allocate tlabel from the bitmap and put the transaction on
252          * the list while holding the card spinlock. */
253
254         spin_lock_irqsave(&card->lock, flags);
255
256         source = card->node_id;
257         tlabel = card->current_tlabel;
258         if (card->tlabel_mask & (1 << tlabel)) {
259                 spin_unlock_irqrestore(&card->lock, flags);
260                 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
261                 return;
262         }
263
264         card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
265         card->tlabel_mask |= (1 << tlabel);
266
267         list_add_tail(&t->link, &card->transaction_list);
268
269         spin_unlock_irqrestore(&card->lock, flags);
270
271         /* Initialize rest of transaction, fill out packet and send it. */
272         t->node_id = node_id;
273         t->tlabel = tlabel;
274         t->callback = callback;
275         t->callback_data = callback_data;
276
277         fw_fill_request(&t->packet, tcode, t->tlabel,
278                         node_id, source, generation,
279                         speed, offset, payload, length);
280         t->packet.callback = transmit_complete_callback;
281
282         card->driver->send_request(card, &t->packet);
283 }
284 EXPORT_SYMBOL(fw_send_request);
285
286 static void
287 transmit_phy_packet_callback(struct fw_packet *packet,
288                              struct fw_card *card, int status)
289 {
290         kfree(packet);
291 }
292
293 static void send_phy_packet(struct fw_card *card, u32 data, int generation)
294 {
295         struct fw_packet *packet;
296
297         packet = kzalloc(sizeof *packet, GFP_ATOMIC);
298         if (packet == NULL)
299                 return;
300
301         packet->header[0] = data;
302         packet->header[1] = ~data;
303         packet->header_length = 8;
304         packet->payload_length = 0;
305         packet->speed = SCODE_100;
306         packet->generation = generation;
307         packet->callback = transmit_phy_packet_callback;
308
309         card->driver->send_request(card, packet);
310 }
311
312 void fw_send_phy_config(struct fw_card *card,
313                         int node_id, int generation, int gap_count)
314 {
315         u32 q;
316
317         q = phy_identifier(PHY_PACKET_CONFIG) |
318                 phy_config_root_id(node_id) |
319                 phy_config_gap_count(gap_count);
320
321         send_phy_packet(card, q, generation);
322 }
323
324 void fw_flush_transactions(struct fw_card *card)
325 {
326         struct fw_transaction *t, *next;
327         struct list_head list;
328         unsigned long flags;
329
330         INIT_LIST_HEAD(&list);
331         spin_lock_irqsave(&card->lock, flags);
332         list_splice_init(&card->transaction_list, &list);
333         card->tlabel_mask = 0;
334         spin_unlock_irqrestore(&card->lock, flags);
335
336         list_for_each_entry_safe(t, next, &list, link) {
337                 card->driver->cancel_packet(card, &t->packet);
338
339                 /* At this point cancel_packet will never call the
340                  * transaction callback, since we just took all the
341                  * transactions out of the list.  So do it here.*/
342                 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
343         }
344 }
345
346 static struct fw_address_handler *
347 lookup_overlapping_address_handler(struct list_head *list,
348                                    unsigned long long offset, size_t length)
349 {
350         struct fw_address_handler *handler;
351
352         list_for_each_entry(handler, list, link) {
353                 if (handler->offset < offset + length &&
354                     offset < handler->offset + handler->length)
355                         return handler;
356         }
357
358         return NULL;
359 }
360
361 static struct fw_address_handler *
362 lookup_enclosing_address_handler(struct list_head *list,
363                                  unsigned long long offset, size_t length)
364 {
365         struct fw_address_handler *handler;
366
367         list_for_each_entry(handler, list, link) {
368                 if (handler->offset <= offset &&
369                     offset + length <= handler->offset + handler->length)
370                         return handler;
371         }
372
373         return NULL;
374 }
375
376 static DEFINE_SPINLOCK(address_handler_lock);
377 static LIST_HEAD(address_handler_list);
378
379 const struct fw_address_region fw_low_memory_region =
380         { .start = 0x000000000000ULL, .end = 0x000100000000ULL,  };
381 const struct fw_address_region fw_high_memory_region =
382         { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL,  };
383 const struct fw_address_region fw_private_region =
384         { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL,  };
385 const struct fw_address_region fw_csr_region =
386         { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL,  };
387 const struct fw_address_region fw_unit_space_region =
388         { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
389 EXPORT_SYMBOL(fw_low_memory_region);
390 EXPORT_SYMBOL(fw_high_memory_region);
391 EXPORT_SYMBOL(fw_private_region);
392 EXPORT_SYMBOL(fw_csr_region);
393 EXPORT_SYMBOL(fw_unit_space_region);
394
395 /**
396  * Allocate a range of addresses in the node space of the OHCI
397  * controller.  When a request is received that falls within the
398  * specified address range, the specified callback is invoked.  The
399  * parameters passed to the callback give the details of the
400  * particular request
401  */
402 int
403 fw_core_add_address_handler(struct fw_address_handler *handler,
404                             const struct fw_address_region *region)
405 {
406         struct fw_address_handler *other;
407         unsigned long flags;
408         int ret = -EBUSY;
409
410         spin_lock_irqsave(&address_handler_lock, flags);
411
412         handler->offset = region->start;
413         while (handler->offset + handler->length <= region->end) {
414                 other =
415                     lookup_overlapping_address_handler(&address_handler_list,
416                                                        handler->offset,
417                                                        handler->length);
418                 if (other != NULL) {
419                         handler->offset += other->length;
420                 } else {
421                         list_add_tail(&handler->link, &address_handler_list);
422                         ret = 0;
423                         break;
424                 }
425         }
426
427         spin_unlock_irqrestore(&address_handler_lock, flags);
428
429         return ret;
430 }
431 EXPORT_SYMBOL(fw_core_add_address_handler);
432
433 /**
434  * Deallocate a range of addresses allocated with fw_allocate.  This
435  * will call the associated callback one last time with a the special
436  * tcode TCODE_DEALLOCATE, to let the client destroy the registered
437  * callback data.  For convenience, the callback parameters offset and
438  * length are set to the start and the length respectively for the
439  * deallocated region, payload is set to NULL.
440  */
441 void fw_core_remove_address_handler(struct fw_address_handler *handler)
442 {
443         unsigned long flags;
444
445         spin_lock_irqsave(&address_handler_lock, flags);
446         list_del(&handler->link);
447         spin_unlock_irqrestore(&address_handler_lock, flags);
448 }
449 EXPORT_SYMBOL(fw_core_remove_address_handler);
450
451 struct fw_request {
452         struct fw_packet response;
453         u32 request_header[4];
454         int ack;
455         u32 length;
456         u32 data[0];
457 };
458
459 static void
460 free_response_callback(struct fw_packet *packet,
461                        struct fw_card *card, int status)
462 {
463         struct fw_request *request;
464
465         request = container_of(packet, struct fw_request, response);
466         kfree(request);
467 }
468
469 void
470 fw_fill_response(struct fw_packet *response, u32 *request_header,
471                  int rcode, void *payload, size_t length)
472 {
473         int tcode, tlabel, extended_tcode, source, destination;
474
475         tcode          = header_get_tcode(request_header[0]);
476         tlabel         = header_get_tlabel(request_header[0]);
477         source         = header_get_destination(request_header[0]);
478         destination    = header_get_source(request_header[1]);
479         extended_tcode = header_get_extended_tcode(request_header[3]);
480
481         response->header[0] =
482                 header_retry(RETRY_1) |
483                 header_tlabel(tlabel) |
484                 header_destination(destination);
485         response->header[1] =
486                 header_source(source) |
487                 header_rcode(rcode);
488         response->header[2] = 0;
489
490         switch (tcode) {
491         case TCODE_WRITE_QUADLET_REQUEST:
492         case TCODE_WRITE_BLOCK_REQUEST:
493                 response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE);
494                 response->header_length = 12;
495                 response->payload_length = 0;
496                 break;
497
498         case TCODE_READ_QUADLET_REQUEST:
499                 response->header[0] |=
500                         header_tcode(TCODE_READ_QUADLET_RESPONSE);
501                 if (payload != NULL)
502                         response->header[3] = *(u32 *)payload;
503                 else
504                         response->header[3] = 0;
505                 response->header_length = 16;
506                 response->payload_length = 0;
507                 break;
508
509         case TCODE_READ_BLOCK_REQUEST:
510         case TCODE_LOCK_REQUEST:
511                 response->header[0] |= header_tcode(tcode + 2);
512                 response->header[3] =
513                         header_data_length(length) |
514                         header_extended_tcode(extended_tcode);
515                 response->header_length = 16;
516                 response->payload = payload;
517                 response->payload_length = length;
518                 break;
519
520         default:
521                 BUG();
522                 return;
523         }
524 }
525 EXPORT_SYMBOL(fw_fill_response);
526
527 static struct fw_request *
528 allocate_request(struct fw_packet *p)
529 {
530         struct fw_request *request;
531         u32 *data, length;
532         int request_tcode, t;
533
534         request_tcode = header_get_tcode(p->header[0]);
535         switch (request_tcode) {
536         case TCODE_WRITE_QUADLET_REQUEST:
537                 data = &p->header[3];
538                 length = 4;
539                 break;
540
541         case TCODE_WRITE_BLOCK_REQUEST:
542         case TCODE_LOCK_REQUEST:
543                 data = p->payload;
544                 length = header_get_data_length(p->header[3]);
545                 break;
546
547         case TCODE_READ_QUADLET_REQUEST:
548                 data = NULL;
549                 length = 4;
550                 break;
551
552         case TCODE_READ_BLOCK_REQUEST:
553                 data = NULL;
554                 length = header_get_data_length(p->header[3]);
555                 break;
556
557         default:
558                 BUG();
559                 return NULL;
560         }
561
562         request = kmalloc(sizeof *request + length, GFP_ATOMIC);
563         if (request == NULL)
564                 return NULL;
565
566         t = (p->timestamp & 0x1fff) + 4000;
567         if (t >= 8000)
568                 t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000;
569         else
570                 t = (p->timestamp & ~0x1fff) + t;
571
572         request->response.speed = p->speed;
573         request->response.timestamp = t;
574         request->response.generation = p->generation;
575         request->response.ack = 0;
576         request->response.callback = free_response_callback;
577         request->ack = p->ack;
578         request->length = length;
579         if (data)
580                 memcpy(request->data, data, length);
581
582         memcpy(request->request_header, p->header, sizeof p->header);
583
584         return request;
585 }
586
587 void
588 fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
589 {
590         /* Broadcast packets are reported as ACK_COMPLETE, so this
591          * check is sufficient to ensure we don't send response to
592          * broadcast packets or posted writes. */
593         if (request->ack != ACK_PENDING)
594                 return;
595
596         if (rcode == RCODE_COMPLETE)
597                 fw_fill_response(&request->response, request->request_header,
598                                  rcode, request->data, request->length);
599         else
600                 fw_fill_response(&request->response, request->request_header,
601                                  rcode, NULL, 0);
602
603         card->driver->send_response(card, &request->response);
604 }
605 EXPORT_SYMBOL(fw_send_response);
606
607 void
608 fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
609 {
610         struct fw_address_handler *handler;
611         struct fw_request *request;
612         unsigned long long offset;
613         unsigned long flags;
614         int tcode, destination, source;
615
616         if (p->payload_length > 2048) {
617                 /* FIXME: send error response. */
618                 return;
619         }
620
621         if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
622                 return;
623
624         request = allocate_request(p);
625         if (request == NULL) {
626                 /* FIXME: send statically allocated busy packet. */
627                 return;
628         }
629
630         offset      =
631                 ((unsigned long long)
632                  header_get_offset_high(p->header[1]) << 32) | p->header[2];
633         tcode       = header_get_tcode(p->header[0]);
634         destination = header_get_destination(p->header[0]);
635         source      = header_get_source(p->header[0]);
636
637         spin_lock_irqsave(&address_handler_lock, flags);
638         handler = lookup_enclosing_address_handler(&address_handler_list,
639                                                    offset, request->length);
640         spin_unlock_irqrestore(&address_handler_lock, flags);
641
642         /* FIXME: lookup the fw_node corresponding to the sender of
643          * this request and pass that to the address handler instead
644          * of the node ID.  We may also want to move the address
645          * allocations to fw_node so we only do this callback if the
646          * upper layers registered it for this node. */
647
648         if (handler == NULL)
649                 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
650         else
651                 handler->address_callback(card, request,
652                                           tcode, destination, source,
653                                           p->generation, p->speed, offset,
654                                           request->data, request->length,
655                                           handler->callback_data);
656 }
657 EXPORT_SYMBOL(fw_core_handle_request);
658
659 void
660 fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
661 {
662         struct fw_transaction *t;
663         unsigned long flags;
664         u32 *data;
665         size_t data_length;
666         int tcode, tlabel, destination, source, rcode;
667
668         tcode       = header_get_tcode(p->header[0]);
669         tlabel      = header_get_tlabel(p->header[0]);
670         destination = header_get_destination(p->header[0]);
671         source      = header_get_source(p->header[1]);
672         rcode       = header_get_rcode(p->header[1]);
673
674         spin_lock_irqsave(&card->lock, flags);
675         list_for_each_entry(t, &card->transaction_list, link) {
676                 if (t->node_id == source && t->tlabel == tlabel) {
677                         list_del(&t->link);
678                         card->tlabel_mask &= ~(1 << t->tlabel);
679                         break;
680                 }
681         }
682         spin_unlock_irqrestore(&card->lock, flags);
683
684         if (&t->link == &card->transaction_list) {
685                 fw_notify("Unsolicited response (source %x, tlabel %x)\n",
686                           source, tlabel);
687                 return;
688         }
689
690         /* FIXME: sanity check packet, is length correct, does tcodes
691          * and addresses match. */
692
693         switch (tcode) {
694         case TCODE_READ_QUADLET_RESPONSE:
695                 data = (u32 *) &p->header[3];
696                 data_length = 4;
697                 break;
698
699         case TCODE_WRITE_RESPONSE:
700                 data = NULL;
701                 data_length = 0;
702                 break;
703
704         case TCODE_READ_BLOCK_RESPONSE:
705         case TCODE_LOCK_RESPONSE:
706                 data = p->payload;
707                 data_length = header_get_data_length(p->header[3]);
708                 break;
709
710         default:
711                 /* Should never happen, this is just to shut up gcc. */
712                 data = NULL;
713                 data_length = 0;
714                 break;
715         }
716
717         t->callback(card, rcode, data, data_length, t->callback_data);
718 }
719 EXPORT_SYMBOL(fw_core_handle_response);
720
721 const struct fw_address_region topology_map_region =
722         { .start = 0xfffff0001000ull, .end = 0xfffff0001400ull, };
723
724 static void
725 handle_topology_map(struct fw_card *card, struct fw_request *request,
726                     int tcode, int destination, int source,
727                     int generation, int speed,
728                     unsigned long long offset,
729                     void *payload, size_t length, void *callback_data)
730 {
731         int i, start, end;
732         u32 *map;
733
734         if (!TCODE_IS_READ_REQUEST(tcode)) {
735                 fw_send_response(card, request, RCODE_TYPE_ERROR);
736                 return;
737         }
738
739         if ((offset & 3) > 0 || (length & 3) > 0) {
740                 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
741                 return;
742         }
743
744         start = (offset - topology_map_region.start) / 4;
745         end = start + length / 4;
746         map = payload;
747
748         for (i = 0; i < length / 4; i++)
749                 map[i] = cpu_to_be32(card->topology_map[start + i]);
750
751         fw_send_response(card, request, RCODE_COMPLETE);
752 }
753
754 static struct fw_address_handler topology_map = {
755         .length                 = 0x200,
756         .address_callback       = handle_topology_map,
757 };
758
759 const struct fw_address_region registers_region =
760         { .start = 0xfffff0000000ull, .end = 0xfffff0000400ull, };
761
762 static void
763 handle_registers(struct fw_card *card, struct fw_request *request,
764                  int tcode, int destination, int source,
765                  int generation, int speed,
766                  unsigned long long offset,
767                  void *payload, size_t length, void *callback_data)
768 {
769         int reg = offset - CSR_REGISTER_BASE;
770         unsigned long long bus_time;
771         __be32 *data = payload;
772
773         switch (reg) {
774         case CSR_CYCLE_TIME:
775         case CSR_BUS_TIME:
776                 if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
777                         fw_send_response(card, request, RCODE_TYPE_ERROR);
778                         break;
779                 }
780
781                 bus_time = card->driver->get_bus_time(card);
782                 if (reg == CSR_CYCLE_TIME)
783                         *data = cpu_to_be32(bus_time);
784                 else
785                         *data = cpu_to_be32(bus_time >> 25);
786                 fw_send_response(card, request, RCODE_COMPLETE);
787                 break;
788
789         case CSR_BUS_MANAGER_ID:
790         case CSR_BANDWIDTH_AVAILABLE:
791         case CSR_CHANNELS_AVAILABLE_HI:
792         case CSR_CHANNELS_AVAILABLE_LO:
793                 /* FIXME: these are handled by the OHCI hardware and
794                  * the stack never sees these request. If we add
795                  * support for a new type of controller that doesn't
796                  * handle this in hardware we need to deal with these
797                  * transactions. */
798                 BUG();
799                 break;
800
801         case CSR_BUSY_TIMEOUT:
802                 /* FIXME: Implement this. */
803         default:
804                 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
805                 break;
806         }
807 }
808
809 static struct fw_address_handler registers = {
810         .length                 = 0x400,
811         .address_callback       = handle_registers,
812 };
813
814 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
815 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
816 MODULE_LICENSE("GPL");
817
818 static const u32 vendor_textual_descriptor[] = {
819         /* textual descriptor leaf () */
820         0x00060000,
821         0x00000000,
822         0x00000000,
823         0x4c696e75,             /* L i n u */
824         0x78204669,             /* x   F i */
825         0x72657769,             /* r e w i */
826         0x72650000,             /* r e     */
827 };
828
829 static const u32 model_textual_descriptor[] = {
830         /* model descriptor leaf () */
831         0x00030000,
832         0x00000000,
833         0x00000000,
834         0x4a756a75,             /* J u j u */
835 };
836
837 static struct fw_descriptor vendor_id_descriptor = {
838         .length = ARRAY_SIZE(vendor_textual_descriptor),
839         .immediate = 0x03d00d1e,
840         .key = 0x81000000,
841         .data = vendor_textual_descriptor,
842 };
843
844 static struct fw_descriptor model_id_descriptor = {
845         .length = ARRAY_SIZE(model_textual_descriptor),
846         .immediate = 0x17000001,
847         .key = 0x81000000,
848         .data = model_textual_descriptor,
849 };
850
851 static int __init fw_core_init(void)
852 {
853         int retval;
854
855         retval = bus_register(&fw_bus_type);
856         if (retval < 0)
857                 return retval;
858
859         fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
860         if (fw_cdev_major < 0) {
861                 bus_unregister(&fw_bus_type);
862                 return fw_cdev_major;
863         }
864
865         retval = fw_core_add_address_handler(&topology_map,
866                                              &topology_map_region);
867         BUG_ON(retval < 0);
868
869         retval = fw_core_add_address_handler(&registers,
870                                              &registers_region);
871         BUG_ON(retval < 0);
872
873         /* Add the vendor textual descriptor. */
874         retval = fw_core_add_descriptor(&vendor_id_descriptor);
875         BUG_ON(retval < 0);
876         retval = fw_core_add_descriptor(&model_id_descriptor);
877         BUG_ON(retval < 0);
878
879         return 0;
880 }
881
882 static void __exit fw_core_cleanup(void)
883 {
884         unregister_chrdev(fw_cdev_major, "firewire");
885         bus_unregister(&fw_bus_type);
886 }
887
888 module_init(fw_core_init);
889 module_exit(fw_core_cleanup);