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