Merge branch 'devel' into next
[pandora-kernel.git] / drivers / firewire / fw-transaction.c
index 7f92c45..40db807 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <linux/completion.h>
 #include <linux/kernel.h>
+#include <linux/kref.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
@@ -54,6 +55,9 @@
 #define HEADER_GET_DATA_LENGTH(q)      (((q) >> 16) & 0xffff)
 #define HEADER_GET_EXTENDED_TCODE(q)   (((q) >> 0) & 0xffff)
 
+#define HEADER_DESTINATION_IS_BROADCAST(q) \
+       (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
+
 #define PHY_CONFIG_GAP_COUNT(gap_count)        (((gap_count) << 16) | (1 << 22))
 #define PHY_CONFIG_ROOT_ID(node_id)    ((((node_id) & 0x3f) << 24) | (1 << 23))
 #define PHY_IDENTIFIER(id)             ((id) << 30)
@@ -297,37 +301,55 @@ EXPORT_SYMBOL(fw_send_request);
 struct fw_phy_packet {
        struct fw_packet packet;
        struct completion done;
+       struct kref kref;
 };
 
-static void
-transmit_phy_packet_callback(struct fw_packet *packet,
-                            struct fw_card *card, int status)
+static void phy_packet_release(struct kref *kref)
+{
+       struct fw_phy_packet *p =
+                       container_of(kref, struct fw_phy_packet, kref);
+       kfree(p);
+}
+
+static void transmit_phy_packet_callback(struct fw_packet *packet,
+                                        struct fw_card *card, int status)
 {
        struct fw_phy_packet *p =
                        container_of(packet, struct fw_phy_packet, packet);
 
        complete(&p->done);
+       kref_put(&p->kref, phy_packet_release);
 }
 
 void fw_send_phy_config(struct fw_card *card,
                        int node_id, int generation, int gap_count)
 {
-       struct fw_phy_packet p;
+       struct fw_phy_packet *p;
+       long timeout = DIV_ROUND_UP(HZ, 10);
        u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG) |
                   PHY_CONFIG_ROOT_ID(node_id) |
                   PHY_CONFIG_GAP_COUNT(gap_count);
 
-       p.packet.header[0] = data;
-       p.packet.header[1] = ~data;
-       p.packet.header_length = 8;
-       p.packet.payload_length = 0;
-       p.packet.speed = SCODE_100;
-       p.packet.generation = generation;
-       p.packet.callback = transmit_phy_packet_callback;
-       init_completion(&p.done);
-
-       card->driver->send_request(card, &p.packet);
-       wait_for_completion(&p.done);
+       p = kmalloc(sizeof(*p), GFP_KERNEL);
+       if (p == NULL)
+               return;
+
+       p->packet.header[0] = data;
+       p->packet.header[1] = ~data;
+       p->packet.header_length = 8;
+       p->packet.payload_length = 0;
+       p->packet.speed = SCODE_100;
+       p->packet.generation = generation;
+       p->packet.callback = transmit_phy_packet_callback;
+       init_completion(&p->done);
+       kref_set(&p->kref, 2);
+
+       card->driver->send_request(card, &p->packet);
+       timeout = wait_for_completion_timeout(&p->done, timeout);
+       kref_put(&p->kref, phy_packet_release);
+
+       /* will leak p if the callback is never executed */
+       WARN_ON(timeout == 0);
 }
 
 void fw_flush_transactions(struct fw_card *card)
@@ -605,12 +627,9 @@ allocate_request(struct fw_packet *p)
 void
 fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
 {
-       /*
-        * Broadcast packets are reported as ACK_COMPLETE, so this
-        * check is sufficient to ensure we don't send response to
-        * broadcast packets or posted writes.
-        */
-       if (request->ack != ACK_PENDING) {
+       /* unified transaction or broadcast transaction: don't respond */
+       if (request->ack != ACK_PENDING ||
+           HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
                kfree(request);
                return;
        }
@@ -798,12 +817,13 @@ handle_registers(struct fw_card *card, struct fw_request *request,
        int reg = offset & ~CSR_REGISTER_BASE;
        unsigned long long bus_time;
        __be32 *data = payload;
+       int rcode = RCODE_COMPLETE;
 
        switch (reg) {
        case CSR_CYCLE_TIME:
        case CSR_BUS_TIME:
                if (!TCODE_IS_READ_REQUEST(tcode) || length != 4) {
-                       fw_send_response(card, request, RCODE_TYPE_ERROR);
+                       rcode = RCODE_TYPE_ERROR;
                        break;
                }
 
@@ -812,7 +832,17 @@ handle_registers(struct fw_card *card, struct fw_request *request,
                        *data = cpu_to_be32(bus_time);
                else
                        *data = cpu_to_be32(bus_time >> 25);
-               fw_send_response(card, request, RCODE_COMPLETE);
+               break;
+
+       case CSR_BROADCAST_CHANNEL:
+               if (tcode == TCODE_READ_QUADLET_REQUEST)
+                       *data = cpu_to_be32(card->broadcast_channel);
+               else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
+                       card->broadcast_channel =
+                           (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
+                           BROADCAST_CHANNEL_INITIAL;
+               else
+                       rcode = RCODE_TYPE_ERROR;
                break;
 
        case CSR_BUS_MANAGER_ID:
@@ -831,10 +861,13 @@ handle_registers(struct fw_card *card, struct fw_request *request,
 
        case CSR_BUSY_TIMEOUT:
                /* FIXME: Implement this. */
+
        default:
-               fw_send_response(card, request, RCODE_ADDRESS_ERROR);
+               rcode = RCODE_ADDRESS_ERROR;
                break;
        }
+
+       fw_send_response(card, request, rcode);
 }
 
 static struct fw_address_handler registers = {