Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 1 | /* -*- c-basic-offset: 8 -*- |
| 2 | * |
| 3 | * fw-transaction.c - core IEEE1394 transaction logic |
| 4 | * |
| 5 | * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software Foundation, |
| 19 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/pci.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/poll.h> |
| 29 | #include <linux/list.h> |
| 30 | #include <linux/kthread.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/semaphore.h> |
| 33 | |
| 34 | #include "fw-transaction.h" |
| 35 | #include "fw-topology.h" |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 36 | #include "fw-device.h" |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 37 | |
| 38 | #define header_pri(pri) ((pri) << 0) |
| 39 | #define header_tcode(tcode) ((tcode) << 4) |
| 40 | #define header_retry(retry) ((retry) << 8) |
| 41 | #define header_tlabel(tlabel) ((tlabel) << 10) |
| 42 | #define header_destination(destination) ((destination) << 16) |
| 43 | #define header_source(source) ((source) << 16) |
| 44 | #define header_rcode(rcode) ((rcode) << 12) |
| 45 | #define header_offset_high(offset_high) ((offset_high) << 0) |
| 46 | #define header_data_length(length) ((length) << 16) |
| 47 | #define header_extended_tcode(tcode) ((tcode) << 0) |
| 48 | |
| 49 | #define header_get_tcode(q) (((q) >> 4) & 0x0f) |
| 50 | #define header_get_tlabel(q) (((q) >> 10) & 0x3f) |
| 51 | #define header_get_rcode(q) (((q) >> 4) & 0x0f) |
| 52 | #define header_get_destination(q) (((q) >> 16) & 0xffff) |
| 53 | #define header_get_source(q) (((q) >> 16) & 0xffff) |
| 54 | #define header_get_offset_high(q) (((q) >> 0) & 0xffff) |
| 55 | #define header_get_data_length(q) (((q) >> 16) & 0xffff) |
| 56 | #define header_get_extended_tcode(q) (((q) >> 0) & 0xffff) |
| 57 | |
| 58 | #define phy_config_gap_count(gap_count) (((gap_count) << 16) | (1 << 22)) |
| 59 | #define phy_config_root_id(node_id) (((node_id) << 24) | (1 << 23)) |
| 60 | #define phy_identifier(id) ((id) << 30) |
| 61 | |
| 62 | static void |
| 63 | close_transaction(struct fw_transaction *t, struct fw_card *card, int rcode, |
| 64 | u32 * payload, size_t length) |
| 65 | { |
| 66 | unsigned long flags; |
| 67 | |
| 68 | spin_lock_irqsave(&card->lock, flags); |
| 69 | card->tlabel_mask &= ~(1 << t->tlabel); |
| 70 | list_del(&t->link); |
| 71 | spin_unlock_irqrestore(&card->lock, flags); |
| 72 | |
| 73 | t->callback(card, rcode, payload, length, t->callback_data); |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | transmit_complete_callback(struct fw_packet *packet, |
| 78 | struct fw_card *card, int status) |
| 79 | { |
| 80 | struct fw_transaction *t = |
| 81 | container_of(packet, struct fw_transaction, packet); |
| 82 | |
| 83 | switch (status) { |
| 84 | case ACK_COMPLETE: |
| 85 | close_transaction(t, card, RCODE_COMPLETE, NULL, 0); |
| 86 | break; |
| 87 | case ACK_PENDING: |
| 88 | t->timestamp = packet->timestamp; |
| 89 | break; |
| 90 | case ACK_BUSY_X: |
| 91 | case ACK_BUSY_A: |
| 92 | case ACK_BUSY_B: |
| 93 | close_transaction(t, card, RCODE_BUSY, NULL, 0); |
| 94 | break; |
| 95 | case ACK_DATA_ERROR: |
| 96 | case ACK_TYPE_ERROR: |
| 97 | close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0); |
| 98 | break; |
| 99 | default: |
| 100 | /* FIXME: In this case, status is a negative errno, |
| 101 | * corresponding to an OHCI specific transmit error |
| 102 | * code. We should map that to an RCODE instead of |
| 103 | * just the generic RCODE_SEND_ERROR. */ |
| 104 | close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Adrian Bunk | 95688e9 | 2007-01-22 19:17:37 +0100 | [diff] [blame^] | 109 | static void |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 110 | fw_fill_packet(struct fw_packet *packet, int tcode, int tlabel, |
| 111 | int node_id, int generation, int speed, |
| 112 | unsigned long long offset, void *payload, size_t length) |
| 113 | { |
| 114 | int ext_tcode; |
| 115 | |
| 116 | if (tcode > 0x10) { |
| 117 | ext_tcode = tcode - 0x10; |
| 118 | tcode = TCODE_LOCK_REQUEST; |
| 119 | } else |
| 120 | ext_tcode = 0; |
| 121 | |
| 122 | packet->header[0] = |
| 123 | header_retry(RETRY_X) | |
| 124 | header_tlabel(tlabel) | |
| 125 | header_tcode(tcode) | |
| 126 | header_destination(node_id | LOCAL_BUS); |
| 127 | packet->header[1] = |
| 128 | header_offset_high(offset >> 32) | header_source(0); |
| 129 | packet->header[2] = |
| 130 | offset; |
| 131 | |
| 132 | switch (tcode) { |
| 133 | case TCODE_WRITE_QUADLET_REQUEST: |
| 134 | packet->header[3] = *(u32 *)payload; |
| 135 | packet->header_length = 16; |
| 136 | packet->payload_length = 0; |
| 137 | break; |
| 138 | |
| 139 | case TCODE_LOCK_REQUEST: |
| 140 | case TCODE_WRITE_BLOCK_REQUEST: |
| 141 | packet->header[3] = |
| 142 | header_data_length(length) | |
| 143 | header_extended_tcode(ext_tcode); |
| 144 | packet->header_length = 16; |
| 145 | packet->payload = payload; |
| 146 | packet->payload_length = length; |
| 147 | break; |
| 148 | |
| 149 | case TCODE_READ_QUADLET_REQUEST: |
| 150 | packet->header_length = 12; |
| 151 | packet->payload_length = 0; |
| 152 | break; |
| 153 | |
| 154 | case TCODE_READ_BLOCK_REQUEST: |
| 155 | packet->header[3] = |
| 156 | header_data_length(length) | |
| 157 | header_extended_tcode(ext_tcode); |
| 158 | packet->header_length = 16; |
| 159 | packet->payload_length = 0; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | packet->speed = speed; |
| 164 | packet->generation = generation; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * This function provides low-level access to the IEEE1394 transaction |
| 169 | * logic. Most C programs would use either fw_read(), fw_write() or |
| 170 | * fw_lock() instead - those function are convenience wrappers for |
| 171 | * this function. The fw_send_request() function is primarily |
| 172 | * provided as a flexible, one-stop entry point for languages bindings |
| 173 | * and protocol bindings. |
| 174 | * |
| 175 | * FIXME: Document this function further, in particular the possible |
| 176 | * values for rcode in the callback. In short, we map ACK_COMPLETE to |
| 177 | * RCODE_COMPLETE, internal errors set errno and set rcode to |
| 178 | * RCODE_SEND_ERROR (which is out of range for standard ieee1394 |
| 179 | * rcodes). All other rcodes are forwarded unchanged. For all |
| 180 | * errors, payload is NULL, length is 0. |
| 181 | * |
| 182 | * Can not expect the callback to be called before the function |
| 183 | * returns, though this does happen in some cases (ACK_COMPLETE and |
| 184 | * errors). |
| 185 | * |
| 186 | * The payload is only used for write requests and must not be freed |
| 187 | * until the callback has been called. |
| 188 | * |
| 189 | * @param card the card from which to send the request |
| 190 | * @param tcode the tcode for this transaction. Do not use |
| 191 | * TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP |
| 192 | * etc. to specify tcode and ext_tcode. |
| 193 | * @param node_id the node_id of the destination node |
| 194 | * @param generation the generation for which node_id is valid |
| 195 | * @param speed the speed to use for sending the request |
| 196 | * @param offset the 48 bit offset on the destination node |
| 197 | * @param payload the data payload for the request subaction |
| 198 | * @param length the length in bytes of the data to read |
| 199 | * @param callback function to be called when the transaction is completed |
| 200 | * @param callback_data pointer to arbitrary data, which will be |
| 201 | * passed to the callback |
| 202 | */ |
| 203 | void |
| 204 | fw_send_request(struct fw_card *card, struct fw_transaction *t, |
| 205 | int tcode, int node_id, int generation, int speed, |
| 206 | unsigned long long offset, |
| 207 | void *payload, size_t length, |
| 208 | fw_transaction_callback_t callback, void *callback_data) |
| 209 | { |
| 210 | unsigned long flags; |
| 211 | int tlabel; |
| 212 | |
| 213 | /* Bump the flush timer up 100ms first of all so we |
| 214 | * don't race with a flush timer callback. */ |
| 215 | |
| 216 | mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10)); |
| 217 | |
| 218 | /* Allocate tlabel from the bitmap and put the transaction on |
| 219 | * the list while holding the card spinlock. */ |
| 220 | |
| 221 | spin_lock_irqsave(&card->lock, flags); |
| 222 | |
| 223 | tlabel = card->current_tlabel; |
| 224 | if (card->tlabel_mask & (1 << tlabel)) { |
| 225 | spin_unlock_irqrestore(&card->lock, flags); |
| 226 | callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | card->current_tlabel = (card->current_tlabel + 1) & 0x1f; |
| 231 | card->tlabel_mask |= (1 << tlabel); |
| 232 | |
| 233 | list_add_tail(&t->link, &card->transaction_list); |
| 234 | |
| 235 | spin_unlock_irqrestore(&card->lock, flags); |
| 236 | |
| 237 | /* Initialize rest of transaction, fill out packet and send it. */ |
| 238 | t->node_id = node_id; |
| 239 | t->tlabel = tlabel; |
| 240 | t->callback = callback; |
| 241 | t->callback_data = callback_data; |
| 242 | |
| 243 | fw_fill_packet(&t->packet, tcode, t->tlabel, |
| 244 | node_id, generation, speed, offset, payload, length); |
| 245 | t->packet.callback = transmit_complete_callback; |
| 246 | |
| 247 | card->driver->send_request(card, &t->packet); |
| 248 | } |
| 249 | EXPORT_SYMBOL(fw_send_request); |
| 250 | |
| 251 | static void |
| 252 | transmit_phy_packet_callback(struct fw_packet *packet, |
| 253 | struct fw_card *card, int status) |
| 254 | { |
| 255 | kfree(packet); |
| 256 | } |
| 257 | |
| 258 | static void send_phy_packet(struct fw_card *card, u32 data, int generation) |
| 259 | { |
| 260 | struct fw_packet *packet; |
| 261 | |
| 262 | packet = kzalloc(sizeof *packet, GFP_ATOMIC); |
| 263 | if (packet == NULL) |
| 264 | return; |
| 265 | |
| 266 | packet->header[0] = data; |
| 267 | packet->header[1] = ~data; |
| 268 | packet->header_length = 8; |
| 269 | packet->payload_length = 0; |
| 270 | packet->speed = SCODE_100; |
| 271 | packet->generation = generation; |
| 272 | packet->callback = transmit_phy_packet_callback; |
| 273 | |
| 274 | card->driver->send_request(card, packet); |
| 275 | } |
| 276 | |
| 277 | void fw_send_force_root(struct fw_card *card, int node_id, int generation) |
| 278 | { |
| 279 | u32 q; |
| 280 | |
| 281 | q = phy_identifier(PHY_PACKET_CONFIG) | phy_config_root_id(node_id); |
| 282 | send_phy_packet(card, q, generation); |
| 283 | } |
| 284 | |
| 285 | void fw_flush_transactions(struct fw_card *card) |
| 286 | { |
| 287 | struct fw_transaction *t, *next; |
| 288 | struct list_head list; |
| 289 | unsigned long flags; |
| 290 | |
| 291 | INIT_LIST_HEAD(&list); |
| 292 | spin_lock_irqsave(&card->lock, flags); |
| 293 | list_splice_init(&card->transaction_list, &list); |
| 294 | card->tlabel_mask = 0; |
| 295 | spin_unlock_irqrestore(&card->lock, flags); |
| 296 | |
| 297 | list_for_each_entry_safe(t, next, &list, link) |
| 298 | t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data); |
| 299 | } |
| 300 | |
| 301 | static struct fw_address_handler * |
| 302 | lookup_overlapping_address_handler(struct list_head *list, |
| 303 | unsigned long long offset, size_t length) |
| 304 | { |
| 305 | struct fw_address_handler *handler; |
| 306 | |
| 307 | list_for_each_entry(handler, list, link) { |
| 308 | if (handler->offset < offset + length && |
| 309 | offset < handler->offset + handler->length) |
| 310 | return handler; |
| 311 | } |
| 312 | |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | static struct fw_address_handler * |
| 317 | lookup_enclosing_address_handler(struct list_head *list, |
| 318 | unsigned long long offset, size_t length) |
| 319 | { |
| 320 | struct fw_address_handler *handler; |
| 321 | |
| 322 | list_for_each_entry(handler, list, link) { |
| 323 | if (handler->offset <= offset && |
| 324 | offset + length <= handler->offset + handler->length) |
| 325 | return handler; |
| 326 | } |
| 327 | |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | static DEFINE_SPINLOCK(address_handler_lock); |
| 332 | static LIST_HEAD(address_handler_list); |
| 333 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 334 | const struct fw_address_region fw_low_memory_region = |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 335 | { 0x000000000000ull, 0x000100000000ull }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 336 | const struct fw_address_region fw_high_memory_region = |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 337 | { 0x000100000000ull, 0xffffe0000000ull }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 338 | const struct fw_address_region fw_private_region = |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 339 | { 0xffffe0000000ull, 0xfffff0000000ull }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 340 | const struct fw_address_region fw_csr_region = |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 341 | { 0xfffff0000000ULL, 0xfffff0000800ull }; |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 342 | const struct fw_address_region fw_unit_space_region = |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 343 | { 0xfffff0000900ull, 0x1000000000000ull }; |
| 344 | |
| 345 | EXPORT_SYMBOL(fw_low_memory_region); |
| 346 | EXPORT_SYMBOL(fw_high_memory_region); |
| 347 | EXPORT_SYMBOL(fw_private_region); |
| 348 | EXPORT_SYMBOL(fw_csr_region); |
| 349 | EXPORT_SYMBOL(fw_unit_space_region); |
| 350 | |
| 351 | /** |
| 352 | * Allocate a range of addresses in the node space of the OHCI |
| 353 | * controller. When a request is received that falls within the |
| 354 | * specified address range, the specified callback is invoked. The |
| 355 | * parameters passed to the callback give the details of the |
| 356 | * particular request |
| 357 | */ |
| 358 | |
| 359 | int |
| 360 | fw_core_add_address_handler(struct fw_address_handler *handler, |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 361 | const struct fw_address_region *region) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 362 | { |
| 363 | struct fw_address_handler *other; |
| 364 | unsigned long flags; |
| 365 | int ret = -EBUSY; |
| 366 | |
| 367 | spin_lock_irqsave(&address_handler_lock, flags); |
| 368 | |
| 369 | handler->offset = region->start; |
| 370 | while (handler->offset + handler->length <= region->end) { |
| 371 | other = |
| 372 | lookup_overlapping_address_handler(&address_handler_list, |
| 373 | handler->offset, |
| 374 | handler->length); |
| 375 | if (other != NULL) { |
| 376 | handler->offset += other->length; |
| 377 | } else { |
| 378 | list_add_tail(&handler->link, &address_handler_list); |
| 379 | ret = 0; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | spin_unlock_irqrestore(&address_handler_lock, flags); |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | EXPORT_SYMBOL(fw_core_add_address_handler); |
| 390 | |
| 391 | /** |
| 392 | * Deallocate a range of addresses allocated with fw_allocate. This |
| 393 | * will call the associated callback one last time with a the special |
| 394 | * tcode TCODE_DEALLOCATE, to let the client destroy the registered |
| 395 | * callback data. For convenience, the callback parameters offset and |
| 396 | * length are set to the start and the length respectively for the |
| 397 | * deallocated region, payload is set to NULL. |
| 398 | */ |
| 399 | |
| 400 | void fw_core_remove_address_handler(struct fw_address_handler *handler) |
| 401 | { |
| 402 | unsigned long flags; |
| 403 | |
| 404 | spin_lock_irqsave(&address_handler_lock, flags); |
| 405 | list_del(&handler->link); |
| 406 | spin_unlock_irqrestore(&address_handler_lock, flags); |
| 407 | } |
| 408 | |
| 409 | EXPORT_SYMBOL(fw_core_remove_address_handler); |
| 410 | |
| 411 | struct fw_request { |
| 412 | struct fw_packet response; |
| 413 | int ack; |
| 414 | u32 length; |
| 415 | u32 data[0]; |
| 416 | }; |
| 417 | |
| 418 | static void |
| 419 | free_response_callback(struct fw_packet *packet, |
| 420 | struct fw_card *card, int status) |
| 421 | { |
| 422 | struct fw_request *request; |
| 423 | |
| 424 | request = container_of(packet, struct fw_request, response); |
| 425 | kfree(request); |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | fw_fill_response(struct fw_packet *response, |
| 430 | u32 *request, u32 *data, size_t length) |
| 431 | { |
| 432 | int tcode, tlabel, extended_tcode, source, destination; |
| 433 | |
| 434 | tcode = header_get_tcode(request[0]); |
| 435 | tlabel = header_get_tlabel(request[0]); |
| 436 | source = header_get_destination(request[0]); |
| 437 | destination = header_get_source(request[1]); |
| 438 | extended_tcode = header_get_extended_tcode(request[3]); |
| 439 | |
| 440 | response->header[0] = |
| 441 | header_retry(RETRY_1) | |
| 442 | header_tlabel(tlabel) | |
| 443 | header_destination(destination); |
| 444 | response->header[1] = header_source(source); |
| 445 | response->header[2] = 0; |
| 446 | |
| 447 | switch (tcode) { |
| 448 | case TCODE_WRITE_QUADLET_REQUEST: |
| 449 | case TCODE_WRITE_BLOCK_REQUEST: |
| 450 | response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE); |
| 451 | response->header_length = 12; |
| 452 | response->payload_length = 0; |
| 453 | break; |
| 454 | |
| 455 | case TCODE_READ_QUADLET_REQUEST: |
| 456 | response->header[0] |= |
| 457 | header_tcode(TCODE_READ_QUADLET_RESPONSE); |
| 458 | response->header[3] = 0; |
| 459 | response->header_length = 16; |
| 460 | response->payload_length = 0; |
| 461 | break; |
| 462 | |
| 463 | case TCODE_READ_BLOCK_REQUEST: |
| 464 | case TCODE_LOCK_REQUEST: |
| 465 | response->header[0] |= header_tcode(tcode + 2); |
| 466 | response->header[3] = |
| 467 | header_data_length(length) | |
| 468 | header_extended_tcode(extended_tcode); |
| 469 | response->header_length = 16; |
| 470 | response->payload = data; |
| 471 | response->payload_length = length; |
| 472 | break; |
| 473 | |
| 474 | default: |
| 475 | BUG(); |
| 476 | return; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static struct fw_request * |
| 481 | allocate_request(u32 *header, int ack, |
| 482 | int speed, int timestamp, int generation) |
| 483 | { |
| 484 | struct fw_request *request; |
| 485 | u32 *data, length; |
| 486 | int request_tcode; |
| 487 | |
| 488 | request_tcode = header_get_tcode(header[0]); |
| 489 | switch (request_tcode) { |
| 490 | case TCODE_WRITE_QUADLET_REQUEST: |
| 491 | data = &header[3]; |
| 492 | length = 4; |
| 493 | break; |
| 494 | |
| 495 | case TCODE_WRITE_BLOCK_REQUEST: |
| 496 | case TCODE_LOCK_REQUEST: |
| 497 | data = &header[4]; |
| 498 | length = header_get_data_length(header[3]); |
| 499 | break; |
| 500 | |
| 501 | case TCODE_READ_QUADLET_REQUEST: |
| 502 | data = NULL; |
| 503 | length = 4; |
| 504 | break; |
| 505 | |
| 506 | case TCODE_READ_BLOCK_REQUEST: |
| 507 | data = NULL; |
| 508 | length = header_get_data_length(header[3]); |
| 509 | break; |
| 510 | |
| 511 | default: |
| 512 | BUG(); |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | request = kmalloc(sizeof *request + length, GFP_ATOMIC); |
| 517 | if (request == NULL) |
| 518 | return NULL; |
| 519 | |
| 520 | request->response.speed = speed; |
| 521 | request->response.timestamp = timestamp; |
| 522 | request->response.generation = generation; |
| 523 | request->response.callback = free_response_callback; |
| 524 | request->ack = ack; |
| 525 | request->length = length; |
| 526 | if (data) |
| 527 | memcpy(request->data, data, length); |
| 528 | |
| 529 | fw_fill_response(&request->response, header, request->data, length); |
| 530 | |
| 531 | return request; |
| 532 | } |
| 533 | |
| 534 | void |
| 535 | fw_send_response(struct fw_card *card, struct fw_request *request, int rcode) |
| 536 | { |
| 537 | int response_tcode; |
| 538 | |
| 539 | /* Broadcast packets are reported as ACK_COMPLETE, so this |
| 540 | * check is sufficient to ensure we don't send response to |
| 541 | * broadcast packets or posted writes. */ |
| 542 | if (request->ack != ACK_PENDING) |
| 543 | return; |
| 544 | |
| 545 | request->response.header[1] |= header_rcode(rcode); |
| 546 | response_tcode = header_get_tcode(request->response.header[0]); |
| 547 | if (rcode != RCODE_COMPLETE) |
| 548 | /* Clear the data_length field. */ |
| 549 | request->response.header[3] &= 0xffff; |
| 550 | else if (response_tcode == TCODE_READ_QUADLET_RESPONSE) |
| 551 | request->response.header[3] = request->data[0]; |
| 552 | |
| 553 | card->driver->send_response(card, &request->response); |
| 554 | } |
| 555 | |
| 556 | EXPORT_SYMBOL(fw_send_response); |
| 557 | |
| 558 | void |
| 559 | fw_core_handle_request(struct fw_card *card, |
| 560 | int speed, int ack, int timestamp, |
| 561 | int generation, u32 length, u32 *header) |
| 562 | { |
| 563 | struct fw_address_handler *handler; |
| 564 | struct fw_request *request; |
| 565 | unsigned long long offset; |
| 566 | unsigned long flags; |
| 567 | int tcode, destination, source, t; |
| 568 | |
| 569 | if (length > 2048) { |
| 570 | /* FIXME: send error response. */ |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | if (ack != ACK_PENDING && ack != ACK_COMPLETE) |
| 575 | return; |
| 576 | |
| 577 | t = (timestamp & 0x1fff) + 4000; |
| 578 | if (t >= 8000) |
| 579 | t = (timestamp & ~0x1fff) + 0x2000 + t - 8000; |
| 580 | else |
| 581 | t = (timestamp & ~0x1fff) + t; |
| 582 | |
| 583 | request = allocate_request(header, ack, speed, t, generation); |
| 584 | if (request == NULL) { |
| 585 | /* FIXME: send statically allocated busy packet. */ |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | offset = |
| 590 | ((unsigned long long) |
| 591 | header_get_offset_high(header[1]) << 32) | header[2]; |
| 592 | tcode = header_get_tcode(header[0]); |
| 593 | destination = header_get_destination(header[0]); |
| 594 | source = header_get_source(header[0]); |
| 595 | |
| 596 | spin_lock_irqsave(&address_handler_lock, flags); |
| 597 | handler = lookup_enclosing_address_handler(&address_handler_list, |
| 598 | offset, request->length); |
| 599 | spin_unlock_irqrestore(&address_handler_lock, flags); |
| 600 | |
| 601 | /* FIXME: lookup the fw_node corresponding to the sender of |
| 602 | * this request and pass that to the address handler instead |
| 603 | * of the node ID. We may also want to move the address |
| 604 | * allocations to fw_node so we only do this callback if the |
| 605 | * upper layers registered it for this node. */ |
| 606 | |
| 607 | if (handler == NULL) |
| 608 | fw_send_response(card, request, RCODE_ADDRESS_ERROR); |
| 609 | else |
| 610 | handler->address_callback(card, request, |
| 611 | tcode, destination, source, |
| 612 | generation, speed, offset, |
| 613 | request->data, request->length, |
| 614 | handler->callback_data); |
| 615 | } |
| 616 | |
| 617 | EXPORT_SYMBOL(fw_core_handle_request); |
| 618 | |
| 619 | void |
| 620 | fw_core_handle_response(struct fw_card *card, |
| 621 | int speed, int ack, int timestamp, |
| 622 | u32 length, u32 *header) |
| 623 | { |
| 624 | struct fw_transaction *t; |
| 625 | unsigned long flags; |
| 626 | u32 *data; |
| 627 | size_t data_length; |
| 628 | int tcode, tlabel, destination, source, rcode; |
| 629 | |
| 630 | tcode = header_get_tcode(header[0]); |
| 631 | tlabel = header_get_tlabel(header[0]); |
| 632 | destination = header_get_destination(header[0]); |
| 633 | source = header_get_source(header[1]); |
| 634 | rcode = header_get_rcode(header[1]); |
| 635 | |
| 636 | spin_lock_irqsave(&card->lock, flags); |
| 637 | list_for_each_entry(t, &card->transaction_list, link) { |
| 638 | if (t->node_id == source && t->tlabel == tlabel) { |
| 639 | list_del(&t->link); |
| 640 | card->tlabel_mask &= ~(1 << t->tlabel); |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | spin_unlock_irqrestore(&card->lock, flags); |
| 645 | |
| 646 | if (&t->link == &card->transaction_list) { |
| 647 | fw_notify("Unsolicited response\n"); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | /* FIXME: sanity check packet, is length correct, does tcodes |
| 652 | * and addresses match. */ |
| 653 | |
| 654 | switch (tcode) { |
| 655 | case TCODE_READ_QUADLET_RESPONSE: |
| 656 | data = (u32 *) &header[3]; |
| 657 | data_length = 4; |
| 658 | break; |
| 659 | |
| 660 | case TCODE_WRITE_RESPONSE: |
| 661 | data = NULL; |
| 662 | data_length = 0; |
| 663 | break; |
| 664 | |
| 665 | case TCODE_READ_BLOCK_RESPONSE: |
| 666 | case TCODE_LOCK_RESPONSE: |
| 667 | data = &header[4]; |
| 668 | data_length = header_get_data_length(header[3]); |
| 669 | break; |
| 670 | |
| 671 | default: |
| 672 | /* Should never happen, this is just to shut up gcc. */ |
| 673 | data = NULL; |
| 674 | data_length = 0; |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | t->callback(card, rcode, data, data_length, t->callback_data); |
| 679 | } |
| 680 | |
| 681 | EXPORT_SYMBOL(fw_core_handle_response); |
| 682 | |
| 683 | MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>"); |
| 684 | MODULE_DESCRIPTION("Core IEEE1394 transaction logic"); |
| 685 | MODULE_LICENSE("GPL"); |
| 686 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 687 | static const u32 vendor_textual_descriptor_data[] = { |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 688 | /* textual descriptor leaf () */ |
| 689 | 0x00080000, |
| 690 | 0x00000000, |
| 691 | 0x00000000, |
| 692 | 0x4c696e75, /* L i n u */ |
| 693 | 0x78204669, /* x F i */ |
| 694 | 0x72657769, /* r e w i */ |
| 695 | 0x72652028, /* r e ( */ |
| 696 | 0x4a554a55, /* J U J U */ |
| 697 | 0x29000000, /* ) */ |
| 698 | }; |
| 699 | |
| 700 | static struct fw_descriptor vendor_textual_descriptor = { |
| 701 | .length = ARRAY_SIZE(vendor_textual_descriptor_data), |
| 702 | .key = 0x81000000, |
| 703 | .data = vendor_textual_descriptor_data |
| 704 | }; |
| 705 | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 706 | static int __init fw_core_init(void) |
| 707 | { |
| 708 | int retval; |
| 709 | |
| 710 | retval = bus_register(&fw_bus_type); |
| 711 | if (retval < 0) |
| 712 | return retval; |
| 713 | |
| 714 | /* Add the vendor textual descriptor. */ |
| 715 | retval = fw_core_add_descriptor(&vendor_textual_descriptor); |
| 716 | BUG_ON(retval < 0); |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static void __exit fw_core_cleanup(void) |
| 722 | { |
| 723 | bus_unregister(&fw_bus_type); |
| 724 | } |
| 725 | |
| 726 | module_init(fw_core_init); |
| 727 | module_exit(fw_core_cleanup); |