Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / firewire / fw-transaction.c
blob022ac4fabb6740d3fec872142e9d65d1bc001a96
1 /*
2 * Core IEEE1394 transaction logic
4 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
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.
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.
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.
21 #include <linux/completion.h>
22 #include <linux/kernel.h>
23 #include <linux/kref.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/poll.h>
31 #include <linux/list.h>
32 #include <linux/kthread.h>
33 #include <asm/uaccess.h>
35 #include "fw-transaction.h"
36 #include "fw-topology.h"
37 #include "fw-device.h"
39 #define HEADER_PRI(pri) ((pri) << 0)
40 #define HEADER_TCODE(tcode) ((tcode) << 4)
41 #define HEADER_RETRY(retry) ((retry) << 8)
42 #define HEADER_TLABEL(tlabel) ((tlabel) << 10)
43 #define HEADER_DESTINATION(destination) ((destination) << 16)
44 #define HEADER_SOURCE(source) ((source) << 16)
45 #define HEADER_RCODE(rcode) ((rcode) << 12)
46 #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
47 #define HEADER_DATA_LENGTH(length) ((length) << 16)
48 #define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
50 #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
51 #define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
52 #define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
53 #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
54 #define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
55 #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
56 #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
57 #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
59 #define HEADER_DESTINATION_IS_BROADCAST(q) \
60 (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
62 #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
63 #define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
64 #define PHY_IDENTIFIER(id) ((id) << 30)
66 static int
67 close_transaction(struct fw_transaction *transaction,
68 struct fw_card *card, int rcode,
69 u32 *payload, size_t length)
71 struct fw_transaction *t;
72 unsigned long flags;
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;
82 spin_unlock_irqrestore(&card->lock, flags);
84 if (&t->link != &card->transaction_list) {
85 t->callback(card, rcode, payload, length, t->callback_data);
86 return 0;
89 return -ENOENT;
93 * Only valid for transactions that are potentially pending (ie have
94 * been sent).
96 int
97 fw_cancel_transaction(struct fw_card *card,
98 struct fw_transaction *transaction)
101 * Cancel the packet transmission if it's still queued. That
102 * will call the packet transmission callback which cancels
103 * the transaction.
106 if (card->driver->cancel_packet(card, &transaction->packet) == 0)
107 return 0;
110 * If the request packet has already been sent, we need to see
111 * if the transaction is still pending and remove it in that case.
114 return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);
116 EXPORT_SYMBOL(fw_cancel_transaction);
118 static void
119 transmit_complete_callback(struct fw_packet *packet,
120 struct fw_card *card, int status)
122 struct fw_transaction *t =
123 container_of(packet, struct fw_transaction, packet);
125 switch (status) {
126 case ACK_COMPLETE:
127 close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
128 break;
129 case ACK_PENDING:
130 t->timestamp = packet->timestamp;
131 break;
132 case ACK_BUSY_X:
133 case ACK_BUSY_A:
134 case ACK_BUSY_B:
135 close_transaction(t, card, RCODE_BUSY, NULL, 0);
136 break;
137 case ACK_DATA_ERROR:
138 close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0);
139 break;
140 case ACK_TYPE_ERROR:
141 close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0);
142 break;
143 default:
145 * In this case the ack is really a juju specific
146 * rcode, so just forward that to the callback.
148 close_transaction(t, card, status, NULL, 0);
149 break;
153 static void
154 fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
155 int destination_id, int source_id, int generation, int speed,
156 unsigned long long offset, void *payload, size_t length)
158 int ext_tcode;
160 if (tcode > 0x10) {
161 ext_tcode = tcode & ~0x10;
162 tcode = TCODE_LOCK_REQUEST;
163 } else
164 ext_tcode = 0;
166 packet->header[0] =
167 HEADER_RETRY(RETRY_X) |
168 HEADER_TLABEL(tlabel) |
169 HEADER_TCODE(tcode) |
170 HEADER_DESTINATION(destination_id);
171 packet->header[1] =
172 HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
173 packet->header[2] =
174 offset;
176 switch (tcode) {
177 case TCODE_WRITE_QUADLET_REQUEST:
178 packet->header[3] = *(u32 *)payload;
179 packet->header_length = 16;
180 packet->payload_length = 0;
181 break;
183 case TCODE_LOCK_REQUEST:
184 case TCODE_WRITE_BLOCK_REQUEST:
185 packet->header[3] =
186 HEADER_DATA_LENGTH(length) |
187 HEADER_EXTENDED_TCODE(ext_tcode);
188 packet->header_length = 16;
189 packet->payload = payload;
190 packet->payload_length = length;
191 break;
193 case TCODE_READ_QUADLET_REQUEST:
194 packet->header_length = 12;
195 packet->payload_length = 0;
196 break;
198 case TCODE_READ_BLOCK_REQUEST:
199 packet->header[3] =
200 HEADER_DATA_LENGTH(length) |
201 HEADER_EXTENDED_TCODE(ext_tcode);
202 packet->header_length = 16;
203 packet->payload_length = 0;
204 break;
207 packet->speed = speed;
208 packet->generation = generation;
209 packet->ack = 0;
213 * This function provides low-level access to the IEEE1394 transaction
214 * logic. Most C programs would use either fw_read(), fw_write() or
215 * fw_lock() instead - those function are convenience wrappers for
216 * this function. The fw_send_request() function is primarily
217 * provided as a flexible, one-stop entry point for languages bindings
218 * and protocol bindings.
220 * FIXME: Document this function further, in particular the possible
221 * values for rcode in the callback. In short, we map ACK_COMPLETE to
222 * RCODE_COMPLETE, internal errors set errno and set rcode to
223 * RCODE_SEND_ERROR (which is out of range for standard ieee1394
224 * rcodes). All other rcodes are forwarded unchanged. For all
225 * errors, payload is NULL, length is 0.
227 * Can not expect the callback to be called before the function
228 * returns, though this does happen in some cases (ACK_COMPLETE and
229 * errors).
231 * The payload is only used for write requests and must not be freed
232 * until the callback has been called.
234 * @param card the card from which to send the request
235 * @param tcode the tcode for this transaction. Do not use
236 * TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP
237 * etc. to specify tcode and ext_tcode.
238 * @param node_id the destination node ID (bus ID and PHY ID concatenated)
239 * @param generation the generation for which node_id is valid
240 * @param speed the speed to use for sending the request
241 * @param offset the 48 bit offset on the destination node
242 * @param payload the data payload for the request subaction
243 * @param length the length in bytes of the data to read
244 * @param callback function to be called when the transaction is completed
245 * @param callback_data pointer to arbitrary data, which will be
246 * passed to the callback
248 void
249 fw_send_request(struct fw_card *card, struct fw_transaction *t,
250 int tcode, int destination_id, int generation, int speed,
251 unsigned long long offset,
252 void *payload, size_t length,
253 fw_transaction_callback_t callback, void *callback_data)
255 unsigned long flags;
256 int tlabel;
259 * Bump the flush timer up 100ms first of all so we
260 * don't race with a flush timer callback.
263 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
266 * Allocate tlabel from the bitmap and put the transaction on
267 * the list while holding the card spinlock.
270 spin_lock_irqsave(&card->lock, flags);
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;
279 card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
280 card->tlabel_mask |= (1 << tlabel);
282 t->node_id = destination_id;
283 t->tlabel = tlabel;
284 t->callback = callback;
285 t->callback_data = callback_data;
287 fw_fill_request(&t->packet, tcode, t->tlabel,
288 destination_id, card->node_id, generation,
289 speed, offset, payload, length);
290 t->packet.callback = transmit_complete_callback;
292 list_add_tail(&t->link, &card->transaction_list);
294 spin_unlock_irqrestore(&card->lock, flags);
296 card->driver->send_request(card, &t->packet);
298 EXPORT_SYMBOL(fw_send_request);
300 struct transaction_callback_data {
301 struct completion done;
302 void *payload;
303 int rcode;
306 static void transaction_callback(struct fw_card *card, int rcode,
307 void *payload, size_t length, void *data)
309 struct transaction_callback_data *d = data;
311 if (rcode == RCODE_COMPLETE)
312 memcpy(d->payload, payload, length);
313 d->rcode = rcode;
314 complete(&d->done);
318 * fw_run_transaction - send request and sleep until transaction is completed
320 * Returns the RCODE.
322 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
323 int generation, int speed, unsigned long long offset,
324 void *data, size_t length)
326 struct transaction_callback_data d;
327 struct fw_transaction t;
329 init_completion(&d.done);
330 d.payload = data;
331 fw_send_request(card, &t, tcode, destination_id, generation, speed,
332 offset, data, length, transaction_callback, &d);
333 wait_for_completion(&d.done);
335 return d.rcode;
337 EXPORT_SYMBOL(fw_run_transaction);
339 static DEFINE_MUTEX(phy_config_mutex);
340 static DECLARE_COMPLETION(phy_config_done);
342 static void transmit_phy_packet_callback(struct fw_packet *packet,
343 struct fw_card *card, int status)
345 complete(&phy_config_done);
348 static struct fw_packet phy_config_packet = {
349 .header_length = 8,
350 .payload_length = 0,
351 .speed = SCODE_100,
352 .callback = transmit_phy_packet_callback,
355 void fw_send_phy_config(struct fw_card *card,
356 int node_id, int generation, int gap_count)
358 long timeout = DIV_ROUND_UP(HZ, 10);
359 u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
360 PHY_CONFIG_ROOT_ID(node_id) |
361 PHY_CONFIG_GAP_COUNT(gap_count);
363 mutex_lock(&phy_config_mutex);
365 phy_config_packet.header[0] = data;
366 phy_config_packet.header[1] = ~data;
367 phy_config_packet.generation = generation;
368 INIT_COMPLETION(phy_config_done);
370 card->driver->send_request(card, &phy_config_packet);
371 wait_for_completion_timeout(&phy_config_done, timeout);
373 mutex_unlock(&phy_config_mutex);
376 void fw_flush_transactions(struct fw_card *card)
378 struct fw_transaction *t, *next;
379 struct list_head list;
380 unsigned long flags;
382 INIT_LIST_HEAD(&list);
383 spin_lock_irqsave(&card->lock, flags);
384 list_splice_init(&card->transaction_list, &list);
385 card->tlabel_mask = 0;
386 spin_unlock_irqrestore(&card->lock, flags);
388 list_for_each_entry_safe(t, next, &list, link) {
389 card->driver->cancel_packet(card, &t->packet);
392 * At this point cancel_packet will never call the
393 * transaction callback, since we just took all the
394 * transactions out of the list. So do it here.
396 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
400 static struct fw_address_handler *
401 lookup_overlapping_address_handler(struct list_head *list,
402 unsigned long long offset, size_t length)
404 struct fw_address_handler *handler;
406 list_for_each_entry(handler, list, link) {
407 if (handler->offset < offset + length &&
408 offset < handler->offset + handler->length)
409 return handler;
412 return NULL;
415 static struct fw_address_handler *
416 lookup_enclosing_address_handler(struct list_head *list,
417 unsigned long long offset, size_t length)
419 struct fw_address_handler *handler;
421 list_for_each_entry(handler, list, link) {
422 if (handler->offset <= offset &&
423 offset + length <= handler->offset + handler->length)
424 return handler;
427 return NULL;
430 static DEFINE_SPINLOCK(address_handler_lock);
431 static LIST_HEAD(address_handler_list);
433 const struct fw_address_region fw_high_memory_region =
434 { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
435 EXPORT_SYMBOL(fw_high_memory_region);
437 #if 0
438 const struct fw_address_region fw_low_memory_region =
439 { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
440 const struct fw_address_region fw_private_region =
441 { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
442 const struct fw_address_region fw_csr_region =
443 { .start = CSR_REGISTER_BASE,
444 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
445 const struct fw_address_region fw_unit_space_region =
446 { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
447 #endif /* 0 */
450 * Allocate a range of addresses in the node space of the OHCI
451 * controller. When a request is received that falls within the
452 * specified address range, the specified callback is invoked. The
453 * parameters passed to the callback give the details of the
454 * particular request.
456 * Return value: 0 on success, non-zero otherwise.
457 * The start offset of the handler's address region is determined by
458 * fw_core_add_address_handler() and is returned in handler->offset.
459 * The offset is quadlet-aligned.
462 fw_core_add_address_handler(struct fw_address_handler *handler,
463 const struct fw_address_region *region)
465 struct fw_address_handler *other;
466 unsigned long flags;
467 int ret = -EBUSY;
469 spin_lock_irqsave(&address_handler_lock, flags);
471 handler->offset = roundup(region->start, 4);
472 while (handler->offset + handler->length <= region->end) {
473 other =
474 lookup_overlapping_address_handler(&address_handler_list,
475 handler->offset,
476 handler->length);
477 if (other != NULL) {
478 handler->offset =
479 roundup(other->offset + other->length, 4);
480 } else {
481 list_add_tail(&handler->link, &address_handler_list);
482 ret = 0;
483 break;
487 spin_unlock_irqrestore(&address_handler_lock, flags);
489 return ret;
491 EXPORT_SYMBOL(fw_core_add_address_handler);
494 * Deallocate a range of addresses allocated with fw_allocate. This
495 * will call the associated callback one last time with a the special
496 * tcode TCODE_DEALLOCATE, to let the client destroy the registered
497 * callback data. For convenience, the callback parameters offset and
498 * length are set to the start and the length respectively for the
499 * deallocated region, payload is set to NULL.
501 void fw_core_remove_address_handler(struct fw_address_handler *handler)
503 unsigned long flags;
505 spin_lock_irqsave(&address_handler_lock, flags);
506 list_del(&handler->link);
507 spin_unlock_irqrestore(&address_handler_lock, flags);
509 EXPORT_SYMBOL(fw_core_remove_address_handler);
511 struct fw_request {
512 struct fw_packet response;
513 u32 request_header[4];
514 int ack;
515 u32 length;
516 u32 data[0];
519 static void
520 free_response_callback(struct fw_packet *packet,
521 struct fw_card *card, int status)
523 struct fw_request *request;
525 request = container_of(packet, struct fw_request, response);
526 kfree(request);
529 void
530 fw_fill_response(struct fw_packet *response, u32 *request_header,
531 int rcode, void *payload, size_t length)
533 int tcode, tlabel, extended_tcode, source, destination;
535 tcode = HEADER_GET_TCODE(request_header[0]);
536 tlabel = HEADER_GET_TLABEL(request_header[0]);
537 source = HEADER_GET_DESTINATION(request_header[0]);
538 destination = HEADER_GET_SOURCE(request_header[1]);
539 extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
541 response->header[0] =
542 HEADER_RETRY(RETRY_1) |
543 HEADER_TLABEL(tlabel) |
544 HEADER_DESTINATION(destination);
545 response->header[1] =
546 HEADER_SOURCE(source) |
547 HEADER_RCODE(rcode);
548 response->header[2] = 0;
550 switch (tcode) {
551 case TCODE_WRITE_QUADLET_REQUEST:
552 case TCODE_WRITE_BLOCK_REQUEST:
553 response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
554 response->header_length = 12;
555 response->payload_length = 0;
556 break;
558 case TCODE_READ_QUADLET_REQUEST:
559 response->header[0] |=
560 HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
561 if (payload != NULL)
562 response->header[3] = *(u32 *)payload;
563 else
564 response->header[3] = 0;
565 response->header_length = 16;
566 response->payload_length = 0;
567 break;
569 case TCODE_READ_BLOCK_REQUEST:
570 case TCODE_LOCK_REQUEST:
571 response->header[0] |= HEADER_TCODE(tcode + 2);
572 response->header[3] =
573 HEADER_DATA_LENGTH(length) |
574 HEADER_EXTENDED_TCODE(extended_tcode);
575 response->header_length = 16;
576 response->payload = payload;
577 response->payload_length = length;
578 break;
580 default:
581 BUG();
582 return;
585 EXPORT_SYMBOL(fw_fill_response);
587 static struct fw_request *
588 allocate_request(struct fw_packet *p)
590 struct fw_request *request;
591 u32 *data, length;
592 int request_tcode, t;
594 request_tcode = HEADER_GET_TCODE(p->header[0]);
595 switch (request_tcode) {
596 case TCODE_WRITE_QUADLET_REQUEST:
597 data = &p->header[3];
598 length = 4;
599 break;
601 case TCODE_WRITE_BLOCK_REQUEST:
602 case TCODE_LOCK_REQUEST:
603 data = p->payload;
604 length = HEADER_GET_DATA_LENGTH(p->header[3]);
605 break;
607 case TCODE_READ_QUADLET_REQUEST:
608 data = NULL;
609 length = 4;
610 break;
612 case TCODE_READ_BLOCK_REQUEST:
613 data = NULL;
614 length = HEADER_GET_DATA_LENGTH(p->header[3]);
615 break;
617 default:
618 fw_error("ERROR - corrupt request received - %08x %08x %08x\n",
619 p->header[0], p->header[1], p->header[2]);
620 return NULL;
623 request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
624 if (request == NULL)
625 return NULL;
627 t = (p->timestamp & 0x1fff) + 4000;
628 if (t >= 8000)
629 t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000;
630 else
631 t = (p->timestamp & ~0x1fff) + t;
633 request->response.speed = p->speed;
634 request->response.timestamp = t;
635 request->response.generation = p->generation;
636 request->response.ack = 0;
637 request->response.callback = free_response_callback;
638 request->ack = p->ack;
639 request->length = length;
640 if (data)
641 memcpy(request->data, data, length);
643 memcpy(request->request_header, p->header, sizeof(p->header));
645 return request;
648 void
649 fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
651 /* unified transaction or broadcast transaction: don't respond */
652 if (request->ack != ACK_PENDING ||
653 HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
654 kfree(request);
655 return;
658 if (rcode == RCODE_COMPLETE)
659 fw_fill_response(&request->response, request->request_header,
660 rcode, request->data, request->length);
661 else
662 fw_fill_response(&request->response, request->request_header,
663 rcode, NULL, 0);
665 card->driver->send_response(card, &request->response);
667 EXPORT_SYMBOL(fw_send_response);
669 void
670 fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
672 struct fw_address_handler *handler;
673 struct fw_request *request;
674 unsigned long long offset;
675 unsigned long flags;
676 int tcode, destination, source;
678 if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
679 return;
681 request = allocate_request(p);
682 if (request == NULL) {
683 /* FIXME: send statically allocated busy packet. */
684 return;
687 offset =
688 ((unsigned long long)
689 HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) | p->header[2];
690 tcode = HEADER_GET_TCODE(p->header[0]);
691 destination = HEADER_GET_DESTINATION(p->header[0]);
692 source = HEADER_GET_SOURCE(p->header[1]);
694 spin_lock_irqsave(&address_handler_lock, flags);
695 handler = lookup_enclosing_address_handler(&address_handler_list,
696 offset, request->length);
697 spin_unlock_irqrestore(&address_handler_lock, flags);
700 * FIXME: lookup the fw_node corresponding to the sender of
701 * this request and pass that to the address handler instead
702 * of the node ID. We may also want to move the address
703 * allocations to fw_node so we only do this callback if the
704 * upper layers registered it for this node.
707 if (handler == NULL)
708 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
709 else
710 handler->address_callback(card, request,
711 tcode, destination, source,
712 p->generation, p->speed, offset,
713 request->data, request->length,
714 handler->callback_data);
716 EXPORT_SYMBOL(fw_core_handle_request);
718 void
719 fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
721 struct fw_transaction *t;
722 unsigned long flags;
723 u32 *data;
724 size_t data_length;
725 int tcode, tlabel, destination, source, rcode;
727 tcode = HEADER_GET_TCODE(p->header[0]);
728 tlabel = HEADER_GET_TLABEL(p->header[0]);
729 destination = HEADER_GET_DESTINATION(p->header[0]);
730 source = HEADER_GET_SOURCE(p->header[1]);
731 rcode = HEADER_GET_RCODE(p->header[1]);
733 spin_lock_irqsave(&card->lock, flags);
734 list_for_each_entry(t, &card->transaction_list, link) {
735 if (t->node_id == source && t->tlabel == tlabel) {
736 list_del(&t->link);
737 card->tlabel_mask &= ~(1 << t->tlabel);
738 break;
741 spin_unlock_irqrestore(&card->lock, flags);
743 if (&t->link == &card->transaction_list) {
744 fw_notify("Unsolicited response (source %x, tlabel %x)\n",
745 source, tlabel);
746 return;
750 * FIXME: sanity check packet, is length correct, does tcodes
751 * and addresses match.
754 switch (tcode) {
755 case TCODE_READ_QUADLET_RESPONSE:
756 data = (u32 *) &p->header[3];
757 data_length = 4;
758 break;
760 case TCODE_WRITE_RESPONSE:
761 data = NULL;
762 data_length = 0;
763 break;
765 case TCODE_READ_BLOCK_RESPONSE:
766 case TCODE_LOCK_RESPONSE:
767 data = p->payload;
768 data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
769 break;
771 default:
772 /* Should never happen, this is just to shut up gcc. */
773 data = NULL;
774 data_length = 0;
775 break;
779 * The response handler may be executed while the request handler
780 * is still pending. Cancel the request handler.
782 card->driver->cancel_packet(card, &t->packet);
784 t->callback(card, rcode, data, data_length, t->callback_data);
786 EXPORT_SYMBOL(fw_core_handle_response);
788 static const struct fw_address_region topology_map_region =
789 { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
790 .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
792 static void
793 handle_topology_map(struct fw_card *card, struct fw_request *request,
794 int tcode, int destination, int source,
795 int generation, int speed,
796 unsigned long long offset,
797 void *payload, size_t length, void *callback_data)
799 int i, start, end;
800 __be32 *map;
802 if (!TCODE_IS_READ_REQUEST(tcode)) {
803 fw_send_response(card, request, RCODE_TYPE_ERROR);
804 return;
807 if ((offset & 3) > 0 || (length & 3) > 0) {
808 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
809 return;
812 start = (offset - topology_map_region.start) / 4;
813 end = start + length / 4;
814 map = payload;
816 for (i = 0; i < length / 4; i++)
817 map[i] = cpu_to_be32(card->topology_map[start + i]);
819 fw_send_response(card, request, RCODE_COMPLETE);
822 static struct fw_address_handler topology_map = {
823 .length = 0x200,
824 .address_callback = handle_topology_map,
827 static const struct fw_address_region registers_region =
828 { .start = CSR_REGISTER_BASE,
829 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
831 static void
832 handle_registers(struct fw_card *card, struct fw_request *request,
833 int tcode, int destination, int source,
834 int generation, int speed,
835 unsigned long long offset,
836 void *payload, size_t length, void *callback_data)
838 int reg = offset & ~CSR_REGISTER_BASE;
839 unsigned long long bus_time;
840 __be32 *data = payload;
841 int rcode = RCODE_COMPLETE;
843 switch (reg) {
844 case CSR_CYCLE_TIME:
845 case CSR_BUS_TIME:
846 if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
847 rcode = RCODE_TYPE_ERROR;
848 break;
851 bus_time = card->driver->get_bus_time(card);
852 if (reg == CSR_CYCLE_TIME)
853 *data = cpu_to_be32(bus_time);
854 else
855 *data = cpu_to_be32(bus_time >> 25);
856 break;
858 case CSR_BROADCAST_CHANNEL:
859 if (tcode == TCODE_READ_QUADLET_REQUEST)
860 *data = cpu_to_be32(card->broadcast_channel);
861 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
862 card->broadcast_channel =
863 (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
864 BROADCAST_CHANNEL_INITIAL;
865 else
866 rcode = RCODE_TYPE_ERROR;
867 break;
869 case CSR_BUS_MANAGER_ID:
870 case CSR_BANDWIDTH_AVAILABLE:
871 case CSR_CHANNELS_AVAILABLE_HI:
872 case CSR_CHANNELS_AVAILABLE_LO:
874 * FIXME: these are handled by the OHCI hardware and
875 * the stack never sees these request. If we add
876 * support for a new type of controller that doesn't
877 * handle this in hardware we need to deal with these
878 * transactions.
880 BUG();
881 break;
883 case CSR_BUSY_TIMEOUT:
884 /* FIXME: Implement this. */
886 default:
887 rcode = RCODE_ADDRESS_ERROR;
888 break;
891 fw_send_response(card, request, rcode);
894 static struct fw_address_handler registers = {
895 .length = 0x400,
896 .address_callback = handle_registers,
899 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
900 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
901 MODULE_LICENSE("GPL");
903 static const u32 vendor_textual_descriptor[] = {
904 /* textual descriptor leaf () */
905 0x00060000,
906 0x00000000,
907 0x00000000,
908 0x4c696e75, /* L i n u */
909 0x78204669, /* x F i */
910 0x72657769, /* r e w i */
911 0x72650000, /* r e */
914 static const u32 model_textual_descriptor[] = {
915 /* model descriptor leaf () */
916 0x00030000,
917 0x00000000,
918 0x00000000,
919 0x4a756a75, /* J u j u */
922 static struct fw_descriptor vendor_id_descriptor = {
923 .length = ARRAY_SIZE(vendor_textual_descriptor),
924 .immediate = 0x03d00d1e,
925 .key = 0x81000000,
926 .data = vendor_textual_descriptor,
929 static struct fw_descriptor model_id_descriptor = {
930 .length = ARRAY_SIZE(model_textual_descriptor),
931 .immediate = 0x17000001,
932 .key = 0x81000000,
933 .data = model_textual_descriptor,
936 static int __init fw_core_init(void)
938 int retval;
940 retval = bus_register(&fw_bus_type);
941 if (retval < 0)
942 return retval;
944 fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
945 if (fw_cdev_major < 0) {
946 bus_unregister(&fw_bus_type);
947 return fw_cdev_major;
950 retval = fw_core_add_address_handler(&topology_map,
951 &topology_map_region);
952 BUG_ON(retval < 0);
954 retval = fw_core_add_address_handler(&registers,
955 &registers_region);
956 BUG_ON(retval < 0);
958 /* Add the vendor textual descriptor. */
959 retval = fw_core_add_descriptor(&vendor_id_descriptor);
960 BUG_ON(retval < 0);
961 retval = fw_core_add_descriptor(&model_id_descriptor);
962 BUG_ON(retval < 0);
964 return 0;
967 static void __exit fw_core_cleanup(void)
969 unregister_chrdev(fw_cdev_major, "firewire");
970 bus_unregister(&fw_bus_type);
973 module_init(fw_core_init);
974 module_exit(fw_core_cleanup);