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