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