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