blob: 283dac6d327db100853c1768be00f37dbc346b10 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Core IEEE1394 transaction logic
Kristian Høgsberg3038e352006-12-19 19:58:27 -05003 *
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
Stefan Richter2a0a2592008-03-20 23:48:23 +010021#include <linux/completion.h>
Stefan Richterd6053e02008-11-24 20:40:00 +010022#include <linux/idr.h>
Kristian Høgsberg3038e352006-12-19 19:58:27 -050023#include <linux/kernel.h>
Stefan Richterae1e5352008-06-18 18:20:45 +020024#include <linux/kref.h>
Kristian Høgsberg3038e352006-12-19 19:58:27 -050025#include <linux/module.h>
Stefan Richterc0220d62008-07-22 21:35:47 +020026#include <linux/mutex.h>
Kristian Høgsberg3038e352006-12-19 19:58:27 -050027#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>
Kristian Høgsberg3038e352006-12-19 19:58:27 -050035
36#include "fw-transaction.h"
37#include "fw-topology.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050038#include "fw-device.h"
Kristian Høgsberg3038e352006-12-19 19:58:27 -050039
Kristian Høgsberga77754a2007-05-07 20:33:35 -040040#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)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050050
Kristian Høgsberga77754a2007-05-07 20:33:35 -040051#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)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050059
Stefan Richtera7ea6782008-05-25 11:06:55 +020060#define HEADER_DESTINATION_IS_BROADCAST(q) \
61 (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
62
Kristian Høgsberga77754a2007-05-07 20:33:35 -040063#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)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050066
Stefan Richter53dca512008-12-14 21:47:04 +010067static int close_transaction(struct fw_transaction *transaction,
Stefan Richtera38a00f2009-03-10 21:07:06 +010068 struct fw_card *card, int rcode)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050069{
Kristian Høgsberg730c32f2007-02-06 14:49:32 -050070 struct fw_transaction *t;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050071 unsigned long flags;
72
73 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -050074 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 }
Kristian Høgsberg3038e352006-12-19 19:58:27 -050081 spin_unlock_irqrestore(&card->lock, flags);
82
Kristian Høgsberg730c32f2007-02-06 14:49:32 -050083 if (&t->link != &card->transaction_list) {
Stefan Richtera38a00f2009-03-10 21:07:06 +010084 t->callback(card, rcode, NULL, 0, t->callback_data);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -050085 return 0;
86 }
87
88 return -ENOENT;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050089}
90
Kristian Høgsbergc781c062007-05-07 20:33:32 -040091/*
92 * Only valid for transactions that are potentially pending (ie have
93 * been sent).
94 */
Stefan Richter53dca512008-12-14 21:47:04 +010095int fw_cancel_transaction(struct fw_card *card,
96 struct fw_transaction *transaction)
Kristian Høgsberg730c32f2007-02-06 14:49:32 -050097{
Kristian Høgsbergc781c062007-05-07 20:33:32 -040098 /*
99 * Cancel the packet transmission if it's still queued. That
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500100 * will call the packet transmission callback which cancels
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400101 * the transaction.
102 */
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500103
104 if (card->driver->cancel_packet(card, &transaction->packet) == 0)
105 return 0;
106
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400107 /*
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 */
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500111
Stefan Richtera38a00f2009-03-10 21:07:06 +0100112 return close_transaction(transaction, card, RCODE_CANCELLED);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500113}
114EXPORT_SYMBOL(fw_cancel_transaction);
115
Stefan Richter53dca512008-12-14 21:47:04 +0100116static void transmit_complete_callback(struct fw_packet *packet,
117 struct fw_card *card, int status)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500118{
119 struct fw_transaction *t =
120 container_of(packet, struct fw_transaction, packet);
121
122 switch (status) {
123 case ACK_COMPLETE:
Stefan Richtera38a00f2009-03-10 21:07:06 +0100124 close_transaction(t, card, RCODE_COMPLETE);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500125 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:
Stefan Richtera38a00f2009-03-10 21:07:06 +0100132 close_transaction(t, card, RCODE_BUSY);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500133 break;
134 case ACK_DATA_ERROR:
Stefan Richtera38a00f2009-03-10 21:07:06 +0100135 close_transaction(t, card, RCODE_DATA_ERROR);
Kristian Høgsberge5f49c32007-01-26 00:38:34 -0500136 break;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500137 case ACK_TYPE_ERROR:
Stefan Richtera38a00f2009-03-10 21:07:06 +0100138 close_transaction(t, card, RCODE_TYPE_ERROR);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500139 break;
140 default:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400141 /*
142 * In this case the ack is really a juju specific
143 * rcode, so just forward that to the callback.
144 */
Stefan Richtera38a00f2009-03-10 21:07:06 +0100145 close_transaction(t, card, status);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500146 break;
147 }
148}
149
Stefan Richter53dca512008-12-14 21:47:04 +0100150static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
Stefan Richterb9549bc2008-07-12 14:50:42 +0200151 int destination_id, int source_id, int generation, int speed,
Kristian Høgsberg36bfe492007-01-26 00:38:13 -0500152 unsigned long long offset, void *payload, size_t length)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500153{
154 int ext_tcode;
155
Stefan Richter18e9b102009-03-10 21:02:21 +0100156 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
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500168 if (tcode > 0x10) {
Jarod Wilson8f9f9632008-01-23 16:05:45 -0500169 ext_tcode = tcode & ~0x10;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500170 tcode = TCODE_LOCK_REQUEST;
171 } else
172 ext_tcode = 0;
173
174 packet->header[0] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400175 HEADER_RETRY(RETRY_X) |
176 HEADER_TLABEL(tlabel) |
177 HEADER_TCODE(tcode) |
Stefan Richterb9549bc2008-07-12 14:50:42 +0200178 HEADER_DESTINATION(destination_id);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500179 packet->header[1] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400180 HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500181 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] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400194 HEADER_DATA_LENGTH(length) |
195 HEADER_EXTENDED_TCODE(ext_tcode);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500196 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] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400208 HEADER_DATA_LENGTH(length) |
209 HEADER_EXTENDED_TCODE(ext_tcode);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500210 packet->header_length = 16;
211 packet->payload_length = 0;
212 break;
213 }
Stefan Richter18e9b102009-03-10 21:02:21 +0100214 common:
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500215 packet->speed = speed;
216 packet->generation = generation;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500217 packet->ack = 0;
Stefan Richter1d1dc5e2008-12-10 00:20:38 +0100218 packet->payload_bus = 0;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500219}
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
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200245 * TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500246 * etc. to specify tcode and ext_tcode.
Stefan Richter907293d2007-01-23 21:11:43 +0100247 * @param node_id the destination node ID (bus ID and PHY ID concatenated)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500248 * @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
Stefan Richter18e9b102009-03-10 21:02:21 +0100256 *
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().
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500259 */
Stefan Richter53dca512008-12-14 21:47:04 +0100260void 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)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500264{
265 unsigned long flags;
Stefan Richterb9549bc2008-07-12 14:50:42 +0200266 int tlabel;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500267
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400268 /*
269 * Bump the flush timer up 100ms first of all so we
270 * don't race with a flush timer callback.
271 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500272
273 mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
274
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400275 /*
276 * Allocate tlabel from the bitmap and put the transaction on
277 * the list while holding the card spinlock.
278 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500279
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
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200292 t->node_id = destination_id;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500293 t->tlabel = tlabel;
294 t->callback = callback;
295 t->callback_data = callback_data;
296
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200297 fw_fill_request(&t->packet, tcode, t->tlabel,
298 destination_id, card->node_id, generation,
299 speed, offset, payload, length);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500300 t->packet.callback = transmit_complete_callback;
301
Stefan Richtere9aeb462008-07-12 14:50:06 +0200302 list_add_tail(&t->link, &card->transaction_list);
303
304 spin_unlock_irqrestore(&card->lock, flags);
305
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500306 card->driver->send_request(card, &t->packet);
307}
308EXPORT_SYMBOL(fw_send_request);
309
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200310struct transaction_callback_data {
311 struct completion done;
312 void *payload;
313 int rcode;
314};
315
316static 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 */
332int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
Stefan Richter53dca512008-12-14 21:47:04 +0100333 int generation, int speed, unsigned long long offset,
Stefan Richterba27e1f2009-03-05 19:07:00 +0100334 void *payload, size_t length)
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200335{
336 struct transaction_callback_data d;
337 struct fw_transaction t;
338
339 init_completion(&d.done);
Stefan Richterba27e1f2009-03-05 19:07:00 +0100340 d.payload = payload;
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200341 fw_send_request(card, &t, tcode, destination_id, generation, speed,
Stefan Richterba27e1f2009-03-05 19:07:00 +0100342 offset, payload, length, transaction_callback, &d);
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200343 wait_for_completion(&d.done);
344
345 return d.rcode;
346}
347EXPORT_SYMBOL(fw_run_transaction);
348
Stefan Richterc0220d62008-07-22 21:35:47 +0200349static DEFINE_MUTEX(phy_config_mutex);
350static DECLARE_COMPLETION(phy_config_done);
Stefan Richterae1e5352008-06-18 18:20:45 +0200351
352static void transmit_phy_packet_callback(struct fw_packet *packet,
353 struct fw_card *card, int status)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500354{
Stefan Richterc0220d62008-07-22 21:35:47 +0200355 complete(&phy_config_done);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500356}
357
Stefan Richterc0220d62008-07-22 21:35:47 +0200358static 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
Kristian Høgsberg83db8012007-01-26 00:37:50 -0500365void fw_send_phy_config(struct fw_card *card,
366 int node_id, int generation, int gap_count)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500367{
Stefan Richterae1e5352008-06-18 18:20:45 +0200368 long timeout = DIV_ROUND_UP(HZ, 10);
Stefan Richter2a0a2592008-03-20 23:48:23 +0100369 u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
370 PHY_CONFIG_ROOT_ID(node_id) |
371 PHY_CONFIG_GAP_COUNT(gap_count);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500372
Stefan Richterc0220d62008-07-22 21:35:47 +0200373 mutex_lock(&phy_config_mutex);
Kristian Høgsberg83db8012007-01-26 00:37:50 -0500374
Stefan Richterc0220d62008-07-22 21:35:47 +0200375 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);
Stefan Richterae1e5352008-06-18 18:20:45 +0200379
Stefan Richterc0220d62008-07-22 21:35:47 +0200380 card->driver->send_request(card, &phy_config_packet);
381 wait_for_completion_timeout(&phy_config_done, timeout);
Stefan Richterae1e5352008-06-18 18:20:45 +0200382
Stefan Richterc0220d62008-07-22 21:35:47 +0200383 mutex_unlock(&phy_config_mutex);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500384}
385
386void 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
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500398 list_for_each_entry_safe(t, next, &list, link) {
399 card->driver->cancel_packet(card, &t->packet);
400
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400401 /*
402 * At this point cancel_packet will never call the
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500403 * transaction callback, since we just took all the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400404 * transactions out of the list. So do it here.
405 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500406 t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500407 }
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500408}
409
Stefan Richter53dca512008-12-14 21:47:04 +0100410static struct fw_address_handler *lookup_overlapping_address_handler(
411 struct list_head *list, unsigned long long offset, size_t length)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500412{
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
Stefan Richter53dca512008-12-14 21:47:04 +0100424static struct fw_address_handler *lookup_enclosing_address_handler(
425 struct list_head *list, unsigned long long offset, size_t length)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500426{
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
438static DEFINE_SPINLOCK(address_handler_lock);
439static LIST_HEAD(address_handler_list);
440
Stefan Richter21ebcd12007-01-14 15:29:07 +0100441const struct fw_address_region fw_high_memory_region =
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100442 { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
Adrian Bunkdb8be072008-03-31 02:22:11 +0300443EXPORT_SYMBOL(fw_high_memory_region);
444
445#if 0
446const struct fw_address_region fw_low_memory_region =
447 { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
Stefan Richter21ebcd12007-01-14 15:29:07 +0100448const struct fw_address_region fw_private_region =
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100449 { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
Stefan Richter21ebcd12007-01-14 15:29:07 +0100450const struct fw_address_region fw_csr_region =
Jarod Wilsoncca60972008-03-08 12:52:03 -0500451 { .start = CSR_REGISTER_BASE,
452 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
Stefan Richter21ebcd12007-01-14 15:29:07 +0100453const struct fw_address_region fw_unit_space_region =
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100454 { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
Adrian Bunkdb8be072008-03-31 02:22:11 +0300455#endif /* 0 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500456
457/**
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100458 * 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.
Stefan Richter1415d912007-07-17 02:10:16 +0200467 *
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.
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500471 */
Stefan Richter53dca512008-12-14 21:47:04 +0100472int fw_core_add_address_handler(struct fw_address_handler *handler,
473 const struct fw_address_region *region)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500474{
475 struct fw_address_handler *other;
476 unsigned long flags;
477 int ret = -EBUSY;
478
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100479 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
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500486 spin_lock_irqsave(&address_handler_lock, flags);
487
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100488 handler->offset = region->start;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500489 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) {
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100495 handler->offset += other->length;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500496 } 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}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500507EXPORT_SYMBOL(fw_core_add_address_handler);
508
509/**
Stefan Richter44be21b2008-12-14 19:21:31 +0100510 * fw_core_remove_address_handler - unregister an address handler
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500511 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500512void 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}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500520EXPORT_SYMBOL(fw_core_remove_address_handler);
521
522struct fw_request {
523 struct fw_packet response;
Kristian Høgsberg36bfe492007-01-26 00:38:13 -0500524 u32 request_header[4];
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500525 int ack;
526 u32 length;
527 u32 data[0];
528};
529
Stefan Richter53dca512008-12-14 21:47:04 +0100530static void free_response_callback(struct fw_packet *packet,
531 struct fw_card *card, int status)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500532{
533 struct fw_request *request;
534
535 request = container_of(packet, struct fw_request, response);
536 kfree(request);
537}
538
Stefan Richter53dca512008-12-14 21:47:04 +0100539void fw_fill_response(struct fw_packet *response, u32 *request_header,
540 int rcode, void *payload, size_t length)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500541{
542 int tcode, tlabel, extended_tcode, source, destination;
543
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400544 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]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500549
550 response->header[0] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400551 HEADER_RETRY(RETRY_1) |
552 HEADER_TLABEL(tlabel) |
553 HEADER_DESTINATION(destination);
Kristian Høgsberg36bfe492007-01-26 00:38:13 -0500554 response->header[1] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400555 HEADER_SOURCE(source) |
556 HEADER_RCODE(rcode);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500557 response->header[2] = 0;
558
559 switch (tcode) {
560 case TCODE_WRITE_QUADLET_REQUEST:
561 case TCODE_WRITE_BLOCK_REQUEST:
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400562 response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500563 response->header_length = 12;
564 response->payload_length = 0;
565 break;
566
567 case TCODE_READ_QUADLET_REQUEST:
568 response->header[0] |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400569 HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500570 if (payload != NULL)
571 response->header[3] = *(u32 *)payload;
572 else
573 response->header[3] = 0;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500574 response->header_length = 16;
575 response->payload_length = 0;
576 break;
577
578 case TCODE_READ_BLOCK_REQUEST:
579 case TCODE_LOCK_REQUEST:
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400580 response->header[0] |= HEADER_TCODE(tcode + 2);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500581 response->header[3] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400582 HEADER_DATA_LENGTH(length) |
583 HEADER_EXTENDED_TCODE(extended_tcode);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500584 response->header_length = 16;
Kristian Høgsberg36bfe492007-01-26 00:38:13 -0500585 response->payload = payload;
586 response->payload_length = length;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500587 break;
588
589 default:
590 BUG();
591 return;
592 }
Stefan Richter1d1dc5e2008-12-10 00:20:38 +0100593
594 response->payload_bus = 0;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500595}
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500596EXPORT_SYMBOL(fw_fill_response);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500597
Stefan Richter53dca512008-12-14 21:47:04 +0100598static struct fw_request *allocate_request(struct fw_packet *p)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500599{
600 struct fw_request *request;
601 u32 *data, length;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500602 int request_tcode, t;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500603
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400604 request_tcode = HEADER_GET_TCODE(p->header[0]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500605 switch (request_tcode) {
606 case TCODE_WRITE_QUADLET_REQUEST:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500607 data = &p->header[3];
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500608 length = 4;
609 break;
610
611 case TCODE_WRITE_BLOCK_REQUEST:
612 case TCODE_LOCK_REQUEST:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500613 data = p->payload;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400614 length = HEADER_GET_DATA_LENGTH(p->header[3]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500615 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;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400624 length = HEADER_GET_DATA_LENGTH(p->header[3]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500625 break;
626
627 default:
Stefan Richter0bf607c2008-05-31 19:01:26 +0200628 fw_error("ERROR - corrupt request received - %08x %08x %08x\n",
629 p->header[0], p->header[1], p->header[2]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500630 return NULL;
631 }
632
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400633 request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500634 if (request == NULL)
635 return NULL;
636
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500637 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;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500642
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500643 request->response.speed = p->speed;
644 request->response.timestamp = t;
645 request->response.generation = p->generation;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500646 request->response.ack = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500647 request->response.callback = free_response_callback;
648 request->ack = p->ack;
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500649 request->length = length;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500650 if (data)
Kristian Høgsberg6e2e8422007-02-16 17:34:37 -0500651 memcpy(request->data, data, length);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500652
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400653 memcpy(request->request_header, p->header, sizeof(p->header));
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500654
655 return request;
656}
657
Stefan Richter53dca512008-12-14 21:47:04 +0100658void fw_send_response(struct fw_card *card,
659 struct fw_request *request, int rcode)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500660{
Stefan Richtera7ea6782008-05-25 11:06:55 +0200661 /* unified transaction or broadcast transaction: don't respond */
662 if (request->ack != ACK_PENDING ||
663 HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
Stefan Richter9c9bdf42007-07-17 02:15:36 +0200664 kfree(request);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500665 return;
Stefan Richter9c9bdf42007-07-17 02:15:36 +0200666 }
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500667
Kristian Høgsberg36bfe492007-01-26 00:38:13 -0500668 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);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500674
675 card->driver->send_response(card, &request->response);
676}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500677EXPORT_SYMBOL(fw_send_response);
678
Stefan Richter53dca512008-12-14 21:47:04 +0100679void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500680{
681 struct fw_address_handler *handler;
682 struct fw_request *request;
683 unsigned long long offset;
684 unsigned long flags;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500685 int tcode, destination, source;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500686
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500687 if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500688 return;
689
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500690 request = allocate_request(p);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500691 if (request == NULL) {
692 /* FIXME: send statically allocated busy packet. */
693 return;
694 }
695
696 offset =
697 ((unsigned long long)
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400698 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]);
Rabin Vincent478b2332007-12-21 23:02:15 +0530701 source = HEADER_GET_SOURCE(p->header[1]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500702
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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400708 /*
709 * FIXME: lookup the fw_node corresponding to the sender of
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500710 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400713 * upper layers registered it for this node.
714 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500715
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,
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500721 p->generation, p->speed, offset,
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500722 request->data, request->length,
723 handler->callback_data);
724}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500725EXPORT_SYMBOL(fw_core_handle_request);
726
Stefan Richter53dca512008-12-14 21:47:04 +0100727void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500728{
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
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400735 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]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500740
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) {
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500752 fw_notify("Unsolicited response (source %x, tlabel %x)\n",
753 source, tlabel);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500754 return;
755 }
756
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400757 /*
758 * FIXME: sanity check packet, is length correct, does tcodes
759 * and addresses match.
760 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500761
762 switch (tcode) {
763 case TCODE_READ_QUADLET_RESPONSE:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500764 data = (u32 *) &p->header[3];
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500765 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:
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500775 data = p->payload;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400776 data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500777 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
Stefan Richter10a4c732008-03-16 00:56:41 +0100786 /*
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
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500792 t->callback(card, rcode, data, data_length, t->callback_data);
793}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500794EXPORT_SYMBOL(fw_core_handle_response);
795
Stefan Richterae579882007-08-02 20:34:17 +0200796static const struct fw_address_region topology_map_region =
Jarod Wilsoncca60972008-03-08 12:52:03 -0500797 { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
798 .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500799
Stefan Richter53dca512008-12-14 21:47:04 +0100800static 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)
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500804{
805 int i, start, end;
Stefan Richterefbf3902008-02-23 12:24:57 +0100806 __be32 *map;
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500807
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
828static struct fw_address_handler topology_map = {
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500829 .length = 0x200,
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500830 .address_callback = handle_topology_map,
831};
832
Stefan Richterae579882007-08-02 20:34:17 +0200833static const struct fw_address_region registers_region =
Jarod Wilsoncca60972008-03-08 12:52:03 -0500834 { .start = CSR_REGISTER_BASE,
835 .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500836
Stefan Richter53dca512008-12-14 21:47:04 +0100837static 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)
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500841{
Jarod Wilson15f0d832008-03-08 13:18:58 -0500842 int reg = offset & ~CSR_REGISTER_BASE;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500843 unsigned long long bus_time;
844 __be32 *data = payload;
Stefan Richtere534fe12008-05-24 16:41:09 +0200845 int rcode = RCODE_COMPLETE;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500846
847 switch (reg) {
848 case CSR_CYCLE_TIME:
849 case CSR_BUS_TIME:
850 if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
Stefan Richtere534fe12008-05-24 16:41:09 +0200851 rcode = RCODE_TYPE_ERROR;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500852 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);
Stefan Richtere534fe12008-05-24 16:41:09 +0200860 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;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500871 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:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400877 /*
878 * FIXME: these are handled by the OHCI hardware and
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500879 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400882 * transactions.
883 */
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500884 BUG();
885 break;
886
887 case CSR_BUSY_TIMEOUT:
888 /* FIXME: Implement this. */
Stefan Richtere534fe12008-05-24 16:41:09 +0200889
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500890 default:
Stefan Richtere534fe12008-05-24 16:41:09 +0200891 rcode = RCODE_ADDRESS_ERROR;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500892 break;
893 }
Stefan Richtere534fe12008-05-24 16:41:09 +0200894
895 fw_send_response(card, request, rcode);
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500896}
897
898static struct fw_address_handler registers = {
899 .length = 0x400,
900 .address_callback = handle_registers,
901};
902
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500903MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
904MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
905MODULE_LICENSE("GPL");
906
Kristian Høgsberg937f6872007-03-07 12:12:36 -0500907static const u32 vendor_textual_descriptor[] = {
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500908 /* textual descriptor leaf () */
Kristian Høgsberg937f6872007-03-07 12:12:36 -0500909 0x00060000,
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500910 0x00000000,
911 0x00000000,
912 0x4c696e75, /* L i n u */
913 0x78204669, /* x F i */
914 0x72657769, /* r e w i */
Kristian Høgsberg937f6872007-03-07 12:12:36 -0500915 0x72650000, /* r e */
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500916};
917
Kristian Høgsberg937f6872007-03-07 12:12:36 -0500918static const u32 model_textual_descriptor[] = {
919 /* model descriptor leaf () */
920 0x00030000,
921 0x00000000,
922 0x00000000,
923 0x4a756a75, /* J u j u */
924};
925
926static struct fw_descriptor vendor_id_descriptor = {
927 .length = ARRAY_SIZE(vendor_textual_descriptor),
928 .immediate = 0x03d00d1e,
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500929 .key = 0x81000000,
Kristian Høgsberg937f6872007-03-07 12:12:36 -0500930 .data = vendor_textual_descriptor,
931};
932
933static struct fw_descriptor model_id_descriptor = {
934 .length = ARRAY_SIZE(model_textual_descriptor),
935 .immediate = 0x17000001,
936 .key = 0x81000000,
937 .data = model_textual_descriptor,
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500938};
939
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500940static int __init fw_core_init(void)
941{
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100942 int ret;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500943
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100944 ret = bus_register(&fw_bus_type);
945 if (ret < 0)
946 return ret;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500947
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500948 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
Stefan Richterc490a6d2008-12-14 21:45:14 +0100954 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);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500958
959 return 0;
960}
961
962static void __exit fw_core_cleanup(void)
963{
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500964 unregister_chrdev(fw_cdev_major, "firewire");
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500965 bus_unregister(&fw_bus_type);
Stefan Richterd6053e02008-11-24 20:40:00 +0100966 idr_destroy(&fw_device_idr);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500967}
968
969module_init(fw_core_init);
970module_exit(fw_core_cleanup);