]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/firewire/fw-transaction.c
Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-zesty-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/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>
34
35 #include "fw-transaction.h"
36 #include "fw-topology.h"
37 #include "fw-device.h"
38
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)
49
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)
58
59 #define HEADER_DESTINATION_IS_BROADCAST(q) \
60 (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
61
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)
65
66 static int
67 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
97 fw_cancel_transaction(struct fw_card *card,
98 struct fw_transaction *transaction)
99 {
100 /*
101 * Cancel the packet transmission if it's still queued. That
102 * will call the packet transmission callback which cancels
103 * the transaction.
104 */
105
106 if (card->driver->cancel_packet(card, &transaction->packet) == 0)
107 return 0;
108
109 /*
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.
112 */
113
114 return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);
115 }
116 EXPORT_SYMBOL(fw_cancel_transaction);
117
118 static void
119 transmit_complete_callback(struct fw_packet *packet,
120 struct fw_card *card, int status)
121 {
122 struct fw_transaction *t =
123 container_of(packet, struct fw_transaction, packet);
124
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:
144 /*
145 * In this case the ack is really a juju specific
146 * rcode, so just forward that to the callback.
147 */
148 close_transaction(t, card, status, NULL, 0);
149 break;
150 }
151 }
152
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)
157 {
158 int ext_tcode;
159
160 if (tcode > 0x10) {
161 ext_tcode = tcode & ~0x10;
162 tcode = TCODE_LOCK_REQUEST;
163 } else
164 ext_tcode = 0;
165
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;
175
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;
182
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;
192
193 case TCODE_READ_QUADLET_REQUEST:
194 packet->header_length = 12;
195 packet->payload_length = 0;
196 break;
197
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;
205 }
206
207 packet->speed = speed;
208 packet->generation = generation;
209 packet->ack = 0;
210 }
211
212 /**
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.
219 *
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.
226 *
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).
230 *
231 * The payload is only used for write requests and must not be freed
232 * until the callback has been called.
233 *
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
247 */
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)
254 {
255 unsigned long flags;
256 int tlabel;
257
258 /*
259 * Bump the flush timer up 100ms first of all so we
260 * don't race with a flush timer callback.
261 */
262
263 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
264
265 /*
266 * Allocate tlabel from the bitmap and put the transaction on
267 * the list while holding the card spinlock.
268 */
269
270 spin_lock_irqsave(&card->lock, flags);
271
272 tlabel = card->current_tlabel;
273 if (card->tlabel_mask & (1 << tlabel)) {
274 spin_unlock_irqrestore(&card->lock, flags);
275 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
276 return;
277 }
278
279 card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
280 card->tlabel_mask |= (1 << tlabel);
281
282 t->node_id = destination_id;
283 t->tlabel = tlabel;
284 t->callback = callback;
285 t->callback_data = callback_data;
286
287 fw_fill_request(&t->packet, tcode, t->tlabel,
288 destination_id, card->node_id, generation,
289 speed, offset, payload, length);
290 t->packet.callback = transmit_complete_callback;
291
292 list_add_tail(&t->link, &card->transaction_list);
293
294 spin_unlock_irqrestore(&card->lock, flags);
295
296 card->driver->send_request(card, &t->packet);
297 }
298 EXPORT_SYMBOL(fw_send_request);
299
300 struct transaction_callback_data {
301 struct completion done;
302 void *payload;
303 int rcode;
304 };
305
306 static void transaction_callback(struct fw_card *card, int rcode,
307 void *payload, size_t length, void *data)
308 {
309 struct transaction_callback_data *d = data;
310
311 if (rcode == RCODE_COMPLETE)
312 memcpy(d->payload, payload, length);
313 d->rcode = rcode;
314 complete(&d->done);
315 }
316
317 /**
318 * fw_run_transaction - send request and sleep until transaction is completed
319 *
320 * Returns the RCODE.
321 */
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)
325 {
326 struct transaction_callback_data d;
327 struct fw_transaction t;
328
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);
334
335 return d.rcode;
336 }
337 EXPORT_SYMBOL(fw_run_transaction);
338
339 static DEFINE_MUTEX(phy_config_mutex);
340 static DECLARE_COMPLETION(phy_config_done);
341
342 static void transmit_phy_packet_callback(struct fw_packet *packet,
343 struct fw_card *card, int status)
344 {
345 complete(&phy_config_done);
346 }
347
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,
353 };
354
355 void fw_send_phy_config(struct fw_card *card,
356 int node_id, int generation, int gap_count)
357 {
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);
362
363 mutex_lock(&phy_config_mutex);
364
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);
369
370 card->driver->send_request(card, &phy_config_packet);
371 wait_for_completion_timeout(&phy_config_done, timeout);
372
373 mutex_unlock(&phy_config_mutex);
374 }
375
376 void fw_flush_transactions(struct fw_card *card)
377 {
378 struct fw_transaction *t, *next;
379 struct list_head list;
380 unsigned long flags;
381
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);
387
388 list_for_each_entry_safe(t, next, &list, link) {
389 card->driver->cancel_packet(card, &t->packet);
390
391 /*
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.
395 */
396 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
397 }
398 }
399
400 static struct fw_address_handler *
401 lookup_overlapping_address_handler(struct list_head *list,
402 unsigned long long offset, size_t length)
403 {
404 struct fw_address_handler *handler;
405
406 list_for_each_entry(handler, list, link) {
407 if (handler->offset < offset + length &&
408 offset < handler->offset + handler->length)
409 return handler;
410 }
411
412 return NULL;
413 }
414
415 static struct fw_address_handler *
416 lookup_enclosing_address_handler(struct list_head *list,
417 unsigned long long offset, size_t length)
418 {
419 struct fw_address_handler *handler;
420
421 list_for_each_entry(handler, list, link) {
422 if (handler->offset <= offset &&
423 offset + length <= handler->offset + handler->length)
424 return handler;
425 }
426
427 return NULL;
428 }
429
430 static DEFINE_SPINLOCK(address_handler_lock);
431 static LIST_HEAD(address_handler_list);
432
433 const struct fw_address_region fw_high_memory_region =
434 { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
435 EXPORT_SYMBOL(fw_high_memory_region);
436
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 */
448
449 /**
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.
455 *
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.
460 */
461 int
462 fw_core_add_address_handler(struct fw_address_handler *handler,
463 const struct fw_address_region *region)
464 {
465 struct fw_address_handler *other;
466 unsigned long flags;
467 int ret = -EBUSY;
468
469 spin_lock_irqsave(&address_handler_lock, flags);
470
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;
484 }
485 }
486
487 spin_unlock_irqrestore(&address_handler_lock, flags);
488
489 return ret;
490 }
491 EXPORT_SYMBOL(fw_core_add_address_handler);
492
493 /**
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.
500 */
501 void fw_core_remove_address_handler(struct fw_address_handler *handler)
502 {
503 unsigned long flags;
504
505 spin_lock_irqsave(&address_handler_lock, flags);
506 list_del(&handler->link);
507 spin_unlock_irqrestore(&address_handler_lock, flags);
508 }
509 EXPORT_SYMBOL(fw_core_remove_address_handler);
510
511 struct fw_request {
512 struct fw_packet response;
513 u32 request_header[4];
514 int ack;
515 u32 length;
516 u32 data[0];
517 };
518
519 static void
520 free_response_callback(struct fw_packet *packet,
521 struct fw_card *card, int status)
522 {
523 struct fw_request *request;
524
525 request = container_of(packet, struct fw_request, response);
526 kfree(request);
527 }
528
529 void
530 fw_fill_response(struct fw_packet *response, u32 *request_header,
531 int rcode, void *payload, size_t length)
532 {
533 int tcode, tlabel, extended_tcode, source, destination;
534
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]);
540
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;
549
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;
557
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;
568
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;
579
580 default:
581 BUG();
582 return;
583 }
584 }
585 EXPORT_SYMBOL(fw_fill_response);
586
587 static struct fw_request *
588 allocate_request(struct fw_packet *p)
589 {
590 struct fw_request *request;
591 u32 *data, length;
592 int request_tcode, t;
593
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;
600
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;
606
607 case TCODE_READ_QUADLET_REQUEST:
608 data = NULL;
609 length = 4;
610 break;
611
612 case TCODE_READ_BLOCK_REQUEST:
613 data = NULL;
614 length = HEADER_GET_DATA_LENGTH(p->header[3]);
615 break;
616
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;
621 }
622
623 request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
624 if (request == NULL)
625 return NULL;
626
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;
632
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);
642
643 memcpy(request->request_header, p->header, sizeof(p->header));
644
645 return request;
646 }
647
648 void
649 fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
650 {
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;
656 }
657
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);
664
665 card->driver->send_response(card, &request->response);
666 }
667 EXPORT_SYMBOL(fw_send_response);
668
669 void
670 fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
671 {
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;
677
678 if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
679 return;
680
681 request = allocate_request(p);
682 if (request == NULL) {
683 /* FIXME: send statically allocated busy packet. */
684 return;
685 }
686
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]);
693
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);
698
699 /*
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.
705 */
706
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);
715 }
716 EXPORT_SYMBOL(fw_core_handle_request);
717
718 void
719 fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
720 {
721 struct fw_transaction *t;
722 unsigned long flags;
723 u32 *data;
724 size_t data_length;
725 int tcode, tlabel, destination, source, rcode;
726
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]);
732
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;
739 }
740 }
741 spin_unlock_irqrestore(&card->lock, flags);
742
743 if (&t->link == &card->transaction_list) {
744 fw_notify("Unsolicited response (source %x, tlabel %x)\n",
745 source, tlabel);
746 return;
747 }
748
749 /*
750 * FIXME: sanity check packet, is length correct, does tcodes
751 * and addresses match.
752 */
753
754 switch (tcode) {
755 case TCODE_READ_QUADLET_RESPONSE:
756 data = (u32 *) &p->header[3];
757 data_length = 4;
758 break;
759
760 case TCODE_WRITE_RESPONSE:
761 data = NULL;
762 data_length = 0;
763 break;
764
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;
770
771 default:
772 /* Should never happen, this is just to shut up gcc. */
773 data = NULL;
774 data_length = 0;
775 break;
776 }
777
778 /*
779 * The response handler may be executed while the request handler
780 * is still pending. Cancel the request handler.
781 */
782 card->driver->cancel_packet(card, &t->packet);
783
784 t->callback(card, rcode, data, data_length, t->callback_data);
785 }
786 EXPORT_SYMBOL(fw_core_handle_response);
787
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, };
791
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)
798 {
799 int i, start, end;
800 __be32 *map;
801
802 if (!TCODE_IS_READ_REQUEST(tcode)) {
803 fw_send_response(card, request, RCODE_TYPE_ERROR);
804 return;
805 }
806
807 if ((offset & 3) > 0 || (length & 3) > 0) {
808 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
809 return;
810 }
811
812 start = (offset - topology_map_region.start) / 4;
813 end = start + length / 4;
814 map = payload;
815
816 for (i = 0; i < length / 4; i++)
817 map[i] = cpu_to_be32(card->topology_map[start + i]);
818
819 fw_send_response(card, request, RCODE_COMPLETE);
820 }
821
822 static struct fw_address_handler topology_map = {
823 .length = 0x200,
824 .address_callback = handle_topology_map,
825 };
826
827 static const struct fw_address_region registers_region =
828 { .start = CSR_REGISTER_BASE,
829 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
830
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)
837 {
838 int reg = offset & ~CSR_REGISTER_BASE;
839 unsigned long long bus_time;
840 __be32 *data = payload;
841 int rcode = RCODE_COMPLETE;
842
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;
849 }
850
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;
857
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;
868
869 case CSR_BUS_MANAGER_ID:
870 case CSR_BANDWIDTH_AVAILABLE:
871 case CSR_CHANNELS_AVAILABLE_HI:
872 case CSR_CHANNELS_AVAILABLE_LO:
873 /*
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.
879 */
880 BUG();
881 break;
882
883 case CSR_BUSY_TIMEOUT:
884 /* FIXME: Implement this. */
885
886 default:
887 rcode = RCODE_ADDRESS_ERROR;
888 break;
889 }
890
891 fw_send_response(card, request, rcode);
892 }
893
894 static struct fw_address_handler registers = {
895 .length = 0x400,
896 .address_callback = handle_registers,
897 };
898
899 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
900 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
901 MODULE_LICENSE("GPL");
902
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 */
912 };
913
914 static const u32 model_textual_descriptor[] = {
915 /* model descriptor leaf () */
916 0x00030000,
917 0x00000000,
918 0x00000000,
919 0x4a756a75, /* J u j u */
920 };
921
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,
927 };
928
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,
934 };
935
936 static int __init fw_core_init(void)
937 {
938 int retval;
939
940 retval = bus_register(&fw_bus_type);
941 if (retval < 0)
942 return retval;
943
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;
948 }
949
950 retval = fw_core_add_address_handler(&topology_map,
951 &topology_map_region);
952 BUG_ON(retval < 0);
953
954 retval = fw_core_add_address_handler(&registers,
955 &registers_region);
956 BUG_ON(retval < 0);
957
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);
963
964 return 0;
965 }
966
967 static void __exit fw_core_cleanup(void)
968 {
969 unregister_chrdev(fw_cdev_major, "firewire");
970 bus_unregister(&fw_bus_type);
971 }
972
973 module_init(fw_core_init);
974 module_exit(fw_core_cleanup);