2006-12-05 06:40:22

by Kristian Høgsberg

[permalink] [raw]
Subject: [PATCH 0/2] fw-core resend

Oops, looks like the fw-core patch was to big for the list. I've
split it into two parts: fw-core which is the transaction logic and
bus reset handling and fw-device which is device probing and sysfs
integration.

cheers,
Kristian


2006-12-05 06:40:52

by Kristian Høgsberg

[permalink] [raw]
Subject: [PATCH 2/2] Add device probing, sysfs interface and char device code.

This patch add code to probe devices and integrate with the device model,
adds minimal sysfs structure and implements a char device for user space
control.
---

drivers/fw/Makefile | 3
drivers/fw/fw-device-cdev.c | 580 +++++++++++++++++++++++++++++++++++++++++
drivers/fw/fw-device-cdev.h | 141 ++++++++++
drivers/fw/fw-device.c | 611 +++++++++++++++++++++++++++++++++++++++++++
drivers/fw/fw-device.h | 156 +++++++++++
drivers/fw/fw-topology.c | 4
6 files changed, 1490 insertions(+), 5 deletions(-)

diff --git a/drivers/fw/Makefile b/drivers/fw/Makefile
index 1b2a3ad..fb6003e 100644
--- a/drivers/fw/Makefile
+++ b/drivers/fw/Makefile
@@ -2,6 +2,7 @@ #
# Makefile for the Linux IEEE 1394 implementation
#

-fw-core-objs := fw-card.o fw-topology.o fw-transaction.o fw-iso.o
+fw-core-objs := fw-card.o fw-topology.o fw-transaction.o fw-iso.o \
+ fw-device.o fw-device-cdev.o

obj-$(CONFIG_FW_CORE) += fw-core.o
diff --git a/drivers/fw/fw-device-cdev.c b/drivers/fw/fw-device-cdev.c
new file mode 100644
index 0000000..d3f14bd
--- /dev/null
+++ b/drivers/fw/fw-device-cdev.c
@@ -0,0 +1,580 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-device-cdev.c - Char device for device raw access
+ *
+ * Copyright © 2005 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/wait.h>
+#include <linux/errno.h>
+#include <linux/device.h>
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <linux/poll.h>
+#include <linux/delay.h>
+#include <asm/uaccess.h>
+#include "fw-transaction.h"
+#include "fw-topology.h"
+#include "fw-device.h"
+#include "fw-device-cdev.h"
+
+/*
+ * todo
+ *
+ * - bus resets sends a new packet with new generation and node id
+ *
+ */
+
+/* This struct is embedded just before the actual data, so event.data
+ * becomes the data to copy to user space. Also, since
+ * dequeue_event() just kfree()'s the event, the event has to be the
+ * first field in the struct. */
+
+struct event {
+ size_t immediate_size;
+ size_t indirect_size;
+ struct list_head link;
+ void *indirect;
+ u32 immediate[0];
+};
+
+struct response {
+ struct fw_transaction transaction;
+ struct client *client;
+ struct event event;
+ struct fw_cdev_event_response response;
+};
+
+struct iso_interrupt {
+ struct event event;
+ struct fw_cdev_event_iso_interrupt interrupt;
+};
+
+struct client {
+ struct fw_device *device;
+ spinlock_t lock;
+ struct list_head handler_list;
+ struct list_head request_list;
+ u32 request_serial;
+ struct list_head event_list;
+ struct semaphore event_list_sem;
+ wait_queue_head_t wait;
+ unsigned long vm_start;
+ struct fw_iso_context *iso_context;
+};
+
+static int fw_device_op_open(struct inode *inode, struct file *file)
+{
+ struct fw_device *device;
+ struct client *client;
+
+ device = container_of(inode->i_cdev, struct fw_device, cdev);
+
+ client = kzalloc(sizeof *client, GFP_KERNEL);
+ if (client == NULL)
+ return -ENOMEM;
+
+ client->device = fw_device_get(device);
+ INIT_LIST_HEAD(&client->event_list);
+ sema_init(&client->event_list_sem, 0);
+ INIT_LIST_HEAD(&client->handler_list);
+ INIT_LIST_HEAD(&client->request_list);
+ spin_lock_init(&client->lock);
+ init_waitqueue_head(&client->wait);
+
+ file->private_data = client;
+
+ return 0;
+}
+
+static void queue_event(struct client *client, struct event *event)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&client->lock, flags);
+
+ list_add_tail(&event->link, &client->event_list);
+
+ up(&client->event_list_sem);
+ wake_up_interruptible(&client->wait);
+
+ spin_unlock_irqrestore(&client->lock, flags);
+}
+
+static int dequeue_event(struct client *client, char *buffer, size_t count)
+{
+ unsigned long flags;
+ struct event *event;
+ int retval;
+
+ if (down_interruptible(&client->event_list_sem) < 0)
+ return -EINTR;
+
+ spin_lock_irqsave(&client->lock, flags);
+
+ event = container_of(client->event_list.next, struct event, link);
+ list_del(&event->link);
+
+ spin_unlock_irqrestore(&client->lock, flags);
+
+ retval = min(event->immediate_size, count);
+ if (buffer && copy_to_user(buffer, event->immediate, retval))
+ retval = -EFAULT;
+
+ retval = min(event->indirect_size + event->immediate_size, count);
+ if (buffer && copy_to_user(buffer + event->immediate_size,
+ event->indirect,
+ retval - event->immediate_size))
+ retval = -EFAULT;
+
+ kfree(event);
+
+ return retval;
+}
+
+static ssize_t
+fw_device_op_read(struct file *file,
+ char *buffer, size_t count, loff_t *offset)
+{
+ struct client *client = file->private_data;
+
+ return dequeue_event(client, buffer, count);
+}
+
+static int ioctl_config_rom(struct client *client, unsigned long arg)
+{
+ struct fw_cdev_get_config_rom rom;
+
+ rom.length = client->device->config_rom_length;
+ memcpy(rom.data, client->device->config_rom, rom.length * 4);
+ if (copy_to_user((void *)arg, &rom,
+ (char *)&rom.data[rom.length] - (char *)&rom))
+ return -EFAULT;
+
+ return 0;
+}
+
+static void
+complete_transaction(struct fw_card *card, int rcode,
+ u32 *payload, size_t length, void *data)
+{
+ struct response *response = data;
+ struct client *client = response->client;
+
+ if (length < response->response.length)
+ response->response.length = length;
+ if (rcode == RCODE_COMPLETE)
+ memcpy(response->response.data, payload,
+ response->response.length);
+
+ response->response.type = FW_CDEV_EVENT_RESPONSE;
+ response->response.rcode = rcode;
+ response->event.immediate_size =
+ sizeof response->response + response->response.length;
+ response->event.indirect_size = 0;
+ queue_event(client, &response->event);
+}
+
+static ssize_t ioctl_send_request(struct client *client, unsigned long arg)
+{
+ struct fw_device *device = client->device;
+ struct fw_cdev_send_request request;
+ struct response *response;
+
+ if (copy_from_user(&request, (void *)arg, sizeof request))
+ return -EFAULT;
+
+ /* What is the biggest size we'll accept, really? */
+ if (request.length > 4096)
+ return -EINVAL;
+
+ response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
+ if (response == NULL)
+ return -ENOMEM;
+
+ response->client = client;
+ response->response.length = request.length;
+ response->response.closure = request.closure;
+
+ if (request.data &&
+ copy_from_user(response->response.data,
+ request.data, request.length)) {
+ kfree(response);
+ return -EINVAL;
+ }
+
+ fw_send_request(device->card, &response->transaction,
+ request.tcode,
+ device->node->node_id | LOCAL_BUS,
+ device->card->generation,
+ device->node->max_speed,
+ request.offset,
+ response->response.data, request.length,
+ complete_transaction, response);
+
+ if (request.data)
+ return sizeof request + request.length;
+ else
+ return sizeof request;
+}
+
+struct address_handler {
+ struct fw_address_handler handler;
+ void *closure;
+ struct client *client;
+ struct list_head link;
+};
+
+struct request {
+ struct fw_request *request;
+ void *data;
+ size_t length;
+ u32 serial;
+ struct list_head link;
+};
+
+struct request_event {
+ struct event event;
+ struct fw_cdev_event_request request;
+};
+
+static void
+handle_request(struct fw_card *card, struct fw_request *r,
+ int tcode, int destination, int source,
+ int generation, int speed,
+ unsigned long long offset,
+ u32 *payload, size_t length, void *callback_data)
+{
+ struct address_handler *handler = callback_data;
+ struct request *request;
+ struct request_event *e;
+ unsigned long flags;
+ struct client *client = handler->client;
+
+ request = kmalloc(sizeof *request, GFP_ATOMIC);
+ e = kmalloc(sizeof *e, GFP_ATOMIC);
+ if (request == NULL || e == NULL) {
+ kfree(request);
+ kfree(e);
+ fw_send_response(card, r, RCODE_CONFLICT_ERROR);
+ return;
+ }
+
+ request->request = r;
+ request->data = payload;
+ request->length = length;
+
+ spin_lock_irqsave(&client->lock, flags);
+ request->serial = client->request_serial++;
+ list_add_tail(&request->link, &client->request_list);
+ spin_unlock_irqrestore(&client->lock, flags);
+
+ e->request.type = FW_CDEV_EVENT_REQUEST;
+ e->request.tcode = tcode;
+ e->request.offset = offset;
+ e->request.length = length;
+ e->request.serial = request->serial;
+ e->request.closure = handler->closure;
+
+ e->event.immediate_size = sizeof e->request;
+ e->event.indirect = payload;
+ e->event.indirect_size = length;
+ queue_event(client, &e->event);
+}
+
+static int ioctl_allocate(struct client *client, unsigned long arg)
+{
+ struct fw_cdev_allocate request;
+ struct address_handler *handler;
+ unsigned long flags;
+ struct fw_address_region region;
+
+ if (copy_from_user(&request, (void *)arg, sizeof request))
+ return -EFAULT;
+
+ handler = kmalloc(sizeof *handler, GFP_KERNEL);
+ if (handler == NULL)
+ return -ENOMEM;
+
+ region.start = request.offset;
+ region.end = request.offset + request.length;
+ handler->handler.length = request.length;
+ handler->handler.address_callback = handle_request;
+ handler->handler.callback_data = handler;
+ handler->closure = request.closure;
+ handler->client = client;
+
+ if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
+ kfree(handler);
+ return -EBUSY;
+ }
+
+ spin_lock_irqsave(&client->lock, flags);
+ list_add_tail(&handler->link, &client->handler_list);
+ spin_unlock_irqrestore(&client->lock, flags);
+
+ return 0;
+}
+
+static int ioctl_send_response(struct client *client, unsigned long arg)
+{
+ struct fw_cdev_send_response request;
+ struct request *r;
+ unsigned long flags;
+
+ if (copy_from_user(&request, (void *)arg, sizeof request))
+ return -EFAULT;
+
+ spin_lock_irqsave(&client->lock, flags);
+ list_for_each_entry(r, &client->request_list, link) {
+ if (r->serial == request.serial) {
+ list_del(&r->link);
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&client->lock, flags);
+
+ if (&r->link == &client->request_list)
+ return -EINVAL;
+
+ if (request.length < r->length)
+ r->length = request.length;
+ if (copy_from_user(r->data, request.data, r->length))
+ return -EFAULT;
+
+ fw_send_response(client->device->card, r->request, request.rcode);
+
+ kfree(r);
+
+ return 0;
+}
+
+static void
+iso_callback(struct fw_iso_context *context, int status, u32 cycle, void *data)
+{
+ struct client *client = data;
+ struct iso_interrupt *interrupt;
+
+ interrupt = kzalloc(sizeof *interrupt, GFP_ATOMIC);
+ if (interrupt == NULL)
+ return;
+
+ interrupt->event.immediate_size = sizeof interrupt->interrupt;
+ interrupt->event.indirect_size = 0;
+ interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
+ interrupt->interrupt.closure = NULL;
+ interrupt->interrupt.cycle = cycle;
+ queue_event(client, &interrupt->event);
+}
+
+static int ioctl_create_iso_context(struct client *client, unsigned long arg)
+{
+ struct fw_cdev_create_iso_context *request =
+ (struct fw_cdev_create_iso_context *)arg;
+ u32 buffer_size;
+
+ if (get_user(buffer_size, &request->buffer_size))
+ return -EFAULT;
+
+ client->iso_context = fw_iso_context_create(client->device->card,
+ FW_ISO_CONTEXT_TRANSMIT,
+ buffer_size,
+ iso_callback, client);
+ if (IS_ERR(client->iso_context))
+ return PTR_ERR(client->iso_context);
+
+ return 0;
+}
+
+static int ioctl_queue_iso(struct client *client, unsigned long arg)
+{
+ struct fw_cdev_queue_iso request;
+ struct fw_cdev_iso_packet *p, *end, *next;
+ void *payload, *payload_end;
+ unsigned long index;
+ int count;
+ struct {
+ struct fw_iso_packet packet;
+ u8 header[256];
+ } u;
+
+ if (client->iso_context == NULL)
+ return -EINVAL;
+ if (copy_from_user(&request, (void *)arg, sizeof request))
+ return -EFAULT;
+
+ /* If the user passes a non-NULL data pointer, has mmap()'ed
+ * the iso buffer, and the pointer points inside the buffer,
+ * we setup the payload pointers accordingly. Otherwise we
+ * set them both to NULL, which will still let packets with
+ * payload_length == 0 through. In other words, if no packets
+ * use the indirect payload, the iso buffer need not be mapped
+ * and the request.data pointer is ignored.*/
+
+ index = (unsigned long)request.data - client->vm_start;
+ if (request.data != NULL && client->vm_start != 0 &&
+ index <= client->iso_context->buffer_size) {
+ payload = client->iso_context->buffer + index;
+ payload_end = client->iso_context->buffer +
+ client->iso_context->buffer_size;
+ } else {
+ payload = NULL;
+ payload_end = NULL;
+ }
+
+ if (!access_ok(VERIFY_READ, request.packets, request.size))
+ return -EFAULT;
+
+ p = request.packets;
+ end = (void *)p + request.size;
+ count = 0;
+ while (p < end) {
+ if (__copy_from_user(&u.packet, p, sizeof *p))
+ return -EFAULT;
+ next = (struct fw_cdev_iso_packet *)
+ &p->header[u.packet.header_length / 4];
+ if (next > end)
+ return -EINVAL;
+ if (__copy_from_user
+ (u.packet.header, p->header, u.packet.header_length))
+ return -EFAULT;
+ if (u.packet.skip &&
+ u.packet.header_length + u.packet.payload_length > 0)
+ return -EINVAL;
+ if (payload + u.packet.payload_length > payload_end)
+ return -EINVAL;
+
+ if (fw_iso_context_queue(client->iso_context,
+ &u.packet, payload))
+ break;
+
+ p = next;
+ payload += u.packet.payload_length;
+ count++;
+ }
+
+ request.size -= (void *)p - (void *)request.packets;
+ request.packets = p;
+ request.data = (void *)client->vm_start +
+ (payload - client->iso_context->buffer);
+
+ if (copy_to_user((void *)arg, &request, sizeof request))
+ return -EFAULT;
+
+ return count;
+}
+
+static int ioctl_send_iso(struct client *client, unsigned long arg)
+{
+ struct fw_cdev_send_iso *request = (struct fw_cdev_send_iso *)arg;
+
+ return fw_iso_context_send(client->iso_context, request->channel,
+ request->speed, request->cycle);
+}
+
+static int
+fw_device_op_ioctl(struct inode *inode, struct file *file,
+ unsigned int cmd, unsigned long arg)
+{
+ struct client *client = file->private_data;
+
+ switch (cmd) {
+ case FW_CDEV_IOC_GET_CONFIG_ROM:
+ return ioctl_config_rom(client, arg);
+ case FW_CDEV_IOC_SEND_REQUEST:
+ return ioctl_send_request(client, arg);
+ case FW_CDEV_IOC_ALLOCATE:
+ return ioctl_allocate(client, arg);
+ case FW_CDEV_IOC_SEND_RESPONSE:
+ return ioctl_send_response(client, arg);
+ case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
+ return ioctl_create_iso_context(client, arg);
+ case FW_CDEV_IOC_QUEUE_ISO:
+ return ioctl_queue_iso(client, arg);
+ case FW_CDEV_IOC_SEND_ISO:
+ return ioctl_send_iso(client, arg);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int fw_card_op_mmap(struct file *file, struct vm_area_struct *vma)
+{
+ struct client *client = file->private_data;
+
+ if (client->iso_context->buffer == NULL)
+ return -EINVAL;
+
+ client->vm_start = vma->vm_start;
+
+ return remap_vmalloc_range(vma, client->iso_context->buffer, 0);
+}
+
+static int fw_device_op_release(struct inode *inode, struct file *file)
+{
+ struct client *client = file->private_data;
+ struct address_handler *h, *next;
+ struct request *r, *next_r;
+
+ if (client->iso_context)
+ fw_iso_context_destroy(client->iso_context);
+
+ list_for_each_entry_safe(h, next, &client->handler_list, link) {
+ fw_core_remove_address_handler(&h->handler);
+ kfree(h);
+ }
+
+ list_for_each_entry_safe(r, next_r, &client->request_list, link) {
+ fw_send_response(client->device->card, r->request,
+ RCODE_CONFLICT_ERROR);
+ kfree(r);
+ }
+
+ /* TODO: wait for all transactions to finish so
+ * complete_transaction doesn't try to queue up responses
+ * after we free client. */
+ while (!list_empty(&client->event_list))
+ dequeue_event(client, NULL, 0);
+
+ fw_device_put(client->device);
+ kfree(client);
+
+ return 0;
+}
+
+static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
+{
+ struct client *client = file->private_data;
+
+ poll_wait(file, &client->wait, pt);
+
+ if (!list_empty(&client->event_list))
+ return POLLIN | POLLRDNORM;
+ else
+ return 0;
+}
+
+struct file_operations fw_device_ops = {
+ .owner = THIS_MODULE,
+ .open = fw_device_op_open,
+ .read = fw_device_op_read,
+ .ioctl = fw_device_op_ioctl,
+ .poll = fw_device_op_poll,
+ .release = fw_device_op_release,
+ .mmap = fw_card_op_mmap,
+};
diff --git a/drivers/fw/fw-device-cdev.h b/drivers/fw/fw-device-cdev.h
new file mode 100644
index 0000000..af6d376
--- /dev/null
+++ b/drivers/fw/fw-device-cdev.h
@@ -0,0 +1,141 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-device-cdev.h -- Char device interface.
+ *
+ * Copyright © 2005 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __fw_cdev_h
+#define __fw_cdev_h
+
+#include <asm/ioctl.h>
+#include <asm/types.h>
+
+#define TCODE_WRITE_QUADLET_REQUEST 0
+#define TCODE_WRITE_BLOCK_REQUEST 1
+#define TCODE_WRITE_RESPONSE 2
+#define TCODE_READ_QUADLET_REQUEST 4
+#define TCODE_READ_BLOCK_REQUEST 5
+#define TCODE_READ_QUADLET_RESPONSE 6
+#define TCODE_READ_BLOCK_RESPONSE 7
+#define TCODE_CYCLE_START 8
+#define TCODE_LOCK_REQUEST 9
+#define TCODE_STREAM_DATA 10
+#define TCODE_LOCK_RESPONSE 11
+
+#define RCODE_COMPLETE 0x0
+#define RCODE_CONFLICT_ERROR 0x4
+#define RCODE_DATA_ERROR 0x5
+#define RCODE_TYPE_ERROR 0x6
+#define RCODE_ADDRESS_ERROR 0x7
+
+#define SCODE_100 0x0
+#define SCODE_200 0x1
+#define SCODE_400 0x2
+#define SCODE_800 0x3
+#define SCODE_1600 0x4
+#define SCODE_3200 0x5
+
+#define FW_CDEV_EVENT_RESPONSE 0x00
+#define FW_CDEV_EVENT_REQUEST 0x01
+#define FW_CDEV_EVENT_ISO_INTERRUPT 0x02
+
+struct fw_cdev_event_response {
+ __u32 type;
+ __u32 length;
+ __u32 rcode;
+ void *closure;
+ __u32 data[0];
+};
+
+struct fw_cdev_event_request {
+ __u32 type;
+ __u32 tcode;
+ __u64 offset;
+ __u32 length;
+ __u32 serial;
+ void *closure;
+ __u32 data[0];
+};
+
+struct fw_cdev_event_iso_interrupt {
+ __u32 type;
+ __u32 cycle;
+ void *closure;
+};
+
+#define FW_CDEV_IOC_GET_CONFIG_ROM _IOR('#', 0x00, struct fw_cdev_get_config_rom)
+#define FW_CDEV_IOC_SEND_REQUEST _IO('#', 0x01)
+#define FW_CDEV_IOC_ALLOCATE _IO('#', 0x02)
+#define FW_CDEV_IOC_SEND_RESPONSE _IO('#', 0x03)
+#define FW_CDEV_IOC_CREATE_ISO_CONTEXT _IO('#', 0x04)
+#define FW_CDEV_IOC_QUEUE_ISO _IO('#', 0x05)
+#define FW_CDEV_IOC_SEND_ISO _IO('#', 0x06)
+
+struct fw_cdev_get_config_rom {
+ __u32 length;
+ __u32 data[256];
+};
+
+struct fw_cdev_send_request {
+ __u32 tcode;
+ __u64 offset;
+ __u32 length;
+ void *closure;
+ void *data;
+};
+
+struct fw_cdev_send_response {
+ __u32 length;
+ __u32 rcode;
+ __u32 serial;
+ void *data;
+};
+
+struct fw_cdev_allocate {
+ __u64 offset;
+ __u32 length;
+ void *closure;
+};
+
+struct fw_cdev_create_iso_context {
+ __u32 buffer_size;
+};
+
+struct fw_cdev_iso_packet {
+ __u16 payload_length; /* Length of indirect payload. */
+ __u32 interrupt : 1; /* Generate interrupt on this packet */
+ __u32 skip : 1; /* Set to not send packet at all. */
+ __u32 tag : 2;
+ __u32 sy : 4;
+ __u32 header_length : 8; /* Length of immediate header. */
+ __u32 header[0];
+};
+
+struct fw_cdev_queue_iso {
+ struct fw_cdev_iso_packet *packets;
+ __u32 size;
+ void *data;
+};
+
+struct fw_cdev_send_iso {
+ __u32 channel;
+ __u32 speed;
+ __s32 cycle;
+};
+
+#endif
diff --git a/drivers/fw/fw-device.c b/drivers/fw/fw-device.c
new file mode 100644
index 0000000..b464bc7
--- /dev/null
+++ b/drivers/fw/fw-device.c
@@ -0,0 +1,611 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-device.c - Device probing and sysfs code.
+ *
+ * Copyright © 2005 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/wait.h>
+#include <linux/errno.h>
+#include <linux/kthread.h>
+#include <linux/device.h>
+#include <linux/delay.h>
+#include "fw-transaction.h"
+#include "fw-topology.h"
+#include "fw-device.h"
+
+void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
+{
+ ci->p = p + 1;
+ ci->end = ci->p + (p[0] >> 16);
+}
+
+EXPORT_SYMBOL(fw_csr_iterator_init);
+
+int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
+{
+ *key = *ci->p >> 24;
+ *value = *ci->p & 0xffffff;
+
+ return ci->p++ < ci->end;
+}
+
+EXPORT_SYMBOL(fw_csr_iterator_next);
+
+static int is_fw_unit(struct device *dev);
+
+static int match_unit_directory(u32 * directory, struct fw_device_id *id)
+{
+ struct fw_csr_iterator ci;
+ int key, value, match;
+
+ match = 0;
+ fw_csr_iterator_init(&ci, directory);
+ while (fw_csr_iterator_next(&ci, &key, &value)) {
+ if (key == CSR_VENDOR && value == id->vendor)
+ match |= FW_MATCH_VENDOR;
+ if (key == CSR_MODEL && value == id->model)
+ match |= FW_MATCH_MODEL;
+ if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
+ match |= FW_MATCH_SPECIFIER_ID;
+ if (key == CSR_VERSION && value == id->version)
+ match |= FW_MATCH_VERSION;
+ }
+
+ return (match & id->match_flags) == id->match_flags;
+}
+
+static int fw_unit_match(struct device *dev, struct device_driver *drv)
+{
+ struct fw_unit *unit = fw_unit(dev);
+ struct fw_driver *driver = fw_driver(drv);
+ int i;
+
+ /* We only allow binding to fw_units. */
+ if (!is_fw_unit(dev))
+ return 0;
+
+ for (i = 0; driver->id_table[i].match_flags != 0; i++) {
+ if (match_unit_directory(unit->directory, &driver->id_table[i]))
+ return 1;
+ }
+
+ return 0;
+}
+
+static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
+{
+ struct fw_device *device = fw_device(unit->device.parent);
+ struct fw_csr_iterator ci;
+
+ int key, value;
+ int vendor = 0;
+ int model = 0;
+ int specifier_id = 0;
+ int version = 0;
+
+ fw_csr_iterator_init(&ci, device->config_rom->data);
+ while (fw_csr_iterator_next(&ci, &key, &value)) {
+ switch (key) {
+ case CSR_VENDOR:
+ vendor = value;
+ break;
+ case CSR_MODEL:
+ model = value;
+ break;
+ }
+ }
+
+ fw_csr_iterator_init(&ci, unit->directory);
+ while (fw_csr_iterator_next(&ci, &key, &value)) {
+ switch (key) {
+ case CSR_SPECIFIER_ID:
+ specifier_id = value;
+ break;
+ case CSR_VERSION:
+ version = value;
+ break;
+ }
+ }
+
+ return snprintf(buffer, buffer_size,
+ "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
+ vendor, model, specifier_id, version);
+}
+
+static int
+fw_unit_uevent(struct device *dev, char **envp, int num_envp,
+ char *buffer, int buffer_size)
+{
+ struct fw_unit *unit = fw_unit(dev);
+ char modalias[64];
+ int length = 0;
+ int i = 0;
+
+ if (!is_fw_unit(dev))
+ goto out;
+
+ get_modalias(unit, modalias, sizeof modalias);
+
+ if (add_uevent_var(envp, num_envp, &i,
+ buffer, buffer_size, &length,
+ "MODALIAS=%s", modalias))
+ return -ENOMEM;
+
+ out:
+ envp[i] = NULL;
+
+ return 0;
+}
+
+struct bus_type fw_bus_type = {
+ .name = "fw",
+ .match = fw_unit_match,
+ .uevent = fw_unit_uevent
+};
+
+EXPORT_SYMBOL(fw_bus_type);
+
+extern struct fw_device *fw_device_get(struct fw_device *device)
+{
+ get_device(&device->device);
+
+ return device;
+}
+
+extern void fw_device_put(struct fw_device *device)
+{
+ put_device(&device->device);
+}
+
+static void fw_device_release(struct device *dev)
+{
+ struct fw_device *device = fw_device(dev);
+ unsigned long flags;
+
+ /* Take the card lock so we don't set this to NULL while a
+ * FW_NODE_UPDATED callback is being handled. */
+ spin_lock_irqsave(&device->card->lock, flags);
+ device->node->data = NULL;
+ spin_unlock_irqrestore(&device->card->lock, flags);
+
+ fw_node_put(device->node);
+ fw_card_put(device->card);
+ kfree(device->config_rom);
+ kfree(device);
+}
+
+int fw_device_enable_phys_dma(struct fw_device *device)
+{
+ return device->card->driver->enable_phys_dma(device->card,
+ device->node_id,
+ device->generation);
+}
+
+EXPORT_SYMBOL(fw_device_enable_phys_dma);
+
+static ssize_t
+show_modalias_attribute(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fw_unit *unit = fw_unit(dev);
+ int length;
+
+ length = get_modalias(unit, buf, PAGE_SIZE);
+ strcpy(buf + length, "\n");
+
+ return length + 1;
+}
+
+static struct device_attribute modalias_attribute = {
+ .attr = {.name = "modalias",.mode = S_IRUGO},
+ .show = show_modalias_attribute
+};
+
+static ssize_t
+show_config_rom_attribute(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fw_device *device = fw_device(dev);
+
+ memcpy(buf, device->config_rom, device->config_rom_length * 4);
+
+ return device->config_rom_length * 4;
+}
+
+static struct device_attribute config_rom_attribute = {
+ .attr = {.name = "config_rom",.mode = S_IRUGO},
+ .show = show_config_rom_attribute,
+};
+
+struct read_quadlet_callback_data {
+ struct completion done;
+ int rcode;
+ u32 data;
+};
+
+static void
+complete_transaction(struct fw_card *card, int rcode,
+ u32 * payload, size_t length, void *data)
+{
+ struct read_quadlet_callback_data *callback_data = data;
+
+ if (rcode == RCODE_COMPLETE)
+ callback_data->data = payload[0];
+ callback_data->rcode = rcode;
+ complete(&callback_data->done);
+}
+
+static int read_rom(struct fw_device *device, int index, u32 * data)
+{
+ struct read_quadlet_callback_data callback_data;
+ struct fw_transaction t;
+ u64 offset;
+
+ init_completion(&callback_data.done);
+
+ offset = 0xfffff0000400ULL + index * 4;
+ fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
+ device->node_id | LOCAL_BUS,
+ device->generation, SCODE_100,
+ offset, NULL, 4, complete_transaction, &callback_data);
+
+ wait_for_completion(&callback_data.done);
+
+ *data = be32_to_cpu(callback_data.data);
+
+ return callback_data.rcode;
+}
+
+static int read_bus_info_block(struct fw_device *device)
+{
+ static u32 rom[256];
+ u32 stack[16], sp, key;
+ int i, end, length;
+
+ /* First read the bus info block. */
+ for (i = 0; i < 5; i++) {
+ if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
+ return -1;
+ /* As per IEEE1212 7.2, during power-up, devices can
+ * reply with a 0 for the first quadlet of the config
+ * rom to indicate that they are booting (for example,
+ * if the firmware is on the disk of a external
+ * harddisk). In that case we just fail, and the
+ * retry mechanism will try again later. */
+ if (i == 0 && rom[i] == 0)
+ return -1;
+ }
+
+ /* Now parse the config rom. The config rom is a recursive
+ * directory structure so we parse it using a stack of
+ * references to the blocks that make up the structure. We
+ * push a reference to the root directory on the stack to
+ * start things off. */
+ length = i;
+ sp = 0;
+ stack[sp++] = 0xc0000005;
+ while (sp > 0) {
+ /* Pop the next block reference of the stack. The
+ * lower 24 bits is the offset into the config rom,
+ * the upper 8 bits are the type of the reference the
+ * block. */
+ key = stack[--sp];
+ i = key & 0xffffff;
+ if (i >= ARRAY_SIZE(rom))
+ /* The reference points outside the standard
+ * config rom area, something's fishy. */
+ return -1;
+
+ /* Read header quadlet for the block to get the length. */
+ if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
+ return -1;
+ end = i + (rom[i] >> 16) + 1;
+ i++;
+ if (end > ARRAY_SIZE(rom))
+ /* This block extends outside standard config
+ * area (and the array we're reading it
+ * into). That's broken, so ignore this
+ * device. */
+ return -1;
+
+ /* Now read in the block. If this is a directory
+ * block, check the entries as we read them to see if
+ * it references another block, and push it in that case. */
+ while (i < end) {
+ if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
+ return -1;
+ if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
+ sp < ARRAY_SIZE(stack))
+ stack[sp++] = i + rom[i];
+ i++;
+ }
+ if (length < i)
+ length = i;
+ }
+
+ device->config_rom = kmalloc(length * 4, GFP_KERNEL);
+ if (device->config_rom == NULL)
+ return -1;
+ memcpy(device->config_rom, rom, length * 4);
+ device->config_rom_length = length;
+
+ return 0;
+}
+
+static void fw_unit_release(struct device *dev)
+{
+ struct fw_unit *unit = fw_unit(dev);
+
+ kfree(unit);
+}
+
+static int is_fw_unit(struct device *dev)
+{
+ return dev->release == fw_unit_release;
+}
+
+static void create_units(struct fw_device *device)
+{
+ struct fw_csr_iterator ci;
+ struct fw_unit *unit;
+ int key, value, i;
+
+ i = 0;
+ fw_csr_iterator_init(&ci, device->config_rom->data);
+ while (fw_csr_iterator_next(&ci, &key, &value)) {
+ if (key != (CSR_UNIT | CSR_DIRECTORY))
+ continue;
+
+ /* Get the address of the unit directory and try to
+ * match the drivers id_tables against it. */
+ unit = kzalloc(sizeof *unit, GFP_KERNEL);
+ if (unit == NULL) {
+ fw_error("failed to allocate memory for unit\n");
+ continue;
+ }
+
+ unit->directory = ci.p + value - 1;
+ unit->device.bus = &fw_bus_type;
+ unit->device.release = fw_unit_release;
+ unit->device.parent = &device->device;
+ snprintf(unit->device.bus_id, sizeof unit->device.bus_id,
+ "%s.%d", device->device.bus_id, i++);
+
+ if (device_register(&unit->device) < 0) {
+ kfree(unit);
+ continue;
+ }
+
+ if (device_create_file(&unit->device, &modalias_attribute) < 0) {
+ device_unregister(&unit->device);
+ kfree(unit);
+ }
+ }
+}
+
+static int shutdown_unit(struct device *device, void *data)
+{
+ struct fw_unit *unit = fw_unit(device);
+
+ if (is_fw_unit(device)) {
+ device_remove_file(&unit->device, &modalias_attribute);
+ device_unregister(&unit->device);
+ }
+
+ return 0;
+}
+
+static void fw_device_shutdown(void *data)
+{
+ struct fw_device *device = data;
+
+ device_remove_file(&device->device, &config_rom_attribute);
+ cdev_del(&device->cdev);
+ unregister_chrdev_region(device->device.devt, 1);
+ device_for_each_child(&device->device, NULL, shutdown_unit);
+ device_unregister(&device->device);
+}
+
+/* These defines control the retry behavior for reading the config
+ * rom. It shouldn't be necessary to tweak these; if the device
+ * doesn't respond to a config rom read within 10 seconds, it's not
+ * going to respond at all. As for the initial delay, a lot of
+ * devices will be able to respond within half a second after bus
+ * reset. On the other hand, it's not really worth being more
+ * aggressive than that, since it scales pretty well; if 10 devices
+ * are plugged in, they're all getting read within one second. */
+
+#define MAX_RETRIES 5
+#define RETRY_DELAY (2 * HZ)
+#define INITIAL_DELAY (HZ / 2)
+
+static void fw_device_init(void *data)
+{
+ static int serial;
+ struct fw_device *device = data;
+
+ /* All failure paths here set node->data to NULL, so that we
+ * don't try to do device_for_each_child() on a kfree()'d
+ * device. */
+
+ if (read_bus_info_block(device) < 0) {
+ if (device->config_rom_retries < MAX_RETRIES) {
+ device->config_rom_retries++;
+ schedule_delayed_work(&device->work, RETRY_DELAY);
+ } else {
+ fw_notify("giving up on config rom for node id %d\n",
+ device->node_id);
+ fw_device_release(&device->device);
+ }
+ return;
+ }
+
+ device->device.bus = &fw_bus_type;
+ device->device.release = fw_device_release;
+ device->device.parent = device->card->device;
+ snprintf(device->device.bus_id, sizeof device->device.bus_id,
+ "fw%d", serial++);
+
+ if (alloc_chrdev_region(&device->device.devt, 0, 1, "fw")) {
+ fw_error("Failed to register char device region.\n");
+ goto error;
+ }
+
+ cdev_init(&device->cdev, &fw_device_ops);
+ device->cdev.owner = THIS_MODULE;
+ kobject_set_name(&device->cdev.kobj, device->device.bus_id);
+ if (cdev_add(&device->cdev, device->device.devt, 1)) {
+ fw_error("Failed to register char device.\n");
+ goto error;
+ }
+
+ if (device_add(&device->device)) {
+ fw_error("Failed to add device.\n");
+ goto error;
+ }
+
+ if (device_create_file(&device->device, &config_rom_attribute) < 0) {
+ fw_error("Failed to create config rom file.\n");
+ goto error_with_device;
+ }
+
+ create_units(device);
+
+ /* Transition the device to running state. If it got pulled
+ * out from under us while we did the intialization work, we
+ * have to shut down the device again here. Normally, though,
+ * fw_node_event will be responsible for shutting it down when
+ * necessary. We have to use the atomic cmpxchg here to avoid
+ * racing with the FW_NODE_DESTROYED case in
+ * fw_node_event(). */
+ if (cmpxchg(&device->state,
+ FW_DEVICE_INITIALIZING,
+ FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
+ fw_device_shutdown(device);
+ else
+ fw_notify("created new fw device %s (%d config rom retries)\n",
+ device->device.bus_id, device->config_rom_retries);
+
+ /* Reschedule the IRM work if we just finished reading the
+ * root node config rom. If this races with a bus reset we
+ * just end up running the IRM work a couple of extra times -
+ * pretty harmless. */
+ if (device->node == device->card->root_node)
+ schedule_work(&device->card->work);
+
+ return;
+
+ error_with_device:
+ device_del(&device->device);
+ error:
+ cdev_del(&device->cdev);
+ unregister_chrdev_region(device->device.devt, 1);
+ put_device(&device->device);
+}
+
+static int update_unit(struct device *dev, void *data)
+{
+ struct fw_unit *unit = fw_unit(dev);
+ struct fw_driver *driver = (struct fw_driver *)dev->driver;
+
+ if (is_fw_unit(dev) && driver != NULL && driver->update != NULL)
+ driver->update(unit);
+
+ return 0;
+}
+
+void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
+{
+ struct fw_device *device;
+
+ /* Ignore events for the local node (i.e. the node that
+ * corresponds to the ieee1394 controller in this linux box). */
+ if (node == card->local_node)
+ return;
+
+ switch (event) {
+ case FW_NODE_CREATED:
+ case FW_NODE_LINK_ON:
+ if (!node->link_on)
+ break;
+
+ device = kzalloc(sizeof(*device), GFP_ATOMIC);
+ if (device == NULL)
+ break;
+
+ /* Do minimal intialization of the device here, the
+ * rest will happen in fw_device_init(). We need the
+ * card and node so we can read the config rom and we
+ * need to do device_initialize() now so
+ * device_for_each_child() in FW_NODE_UPDATED is
+ * doesn't freak out. */
+ device_initialize(&device->device);
+ device->state = FW_DEVICE_INITIALIZING;
+ device->card = fw_card_get(card);
+ device->node = fw_node_get(node);
+ device->node_id = node->node_id;
+ device->generation = card->generation;
+
+ /* Set the node data to point back to this device so
+ * FW_NODE_UPDATED callbacks can update the node_id
+ * and generation for the device. */
+ node->data = device;
+
+ /* Many devices are slow to respond after bus resets,
+ * especially if they are bus powered and go through
+ * power-up after getting plugged in. We schedule the
+ * first config rom scan half a second after bus reset. */
+ INIT_WORK(&device->work, fw_device_init, device);
+ schedule_delayed_work(&device->work, INITIAL_DELAY);
+ break;
+
+ case FW_NODE_UPDATED:
+ if (!node->link_on || node->data == NULL)
+ break;
+
+ device = node->data;
+ device->node_id = node->node_id;
+ device->generation = card->generation;
+ device_for_each_child(&device->device, NULL, update_unit);
+ break;
+
+ case FW_NODE_DESTROYED:
+ case FW_NODE_LINK_OFF:
+ if (!node->data)
+ break;
+
+ /* Destroy the device associated with the node. There
+ * are two cases here: either the device is fully
+ * initialized (FW_DEVICE_RUNNING) or we're in the
+ * process of reading its config rom
+ * (FW_DEVICE_INITIALIZING). If it is fully
+ * initialized we can reuse device->work to schedule a
+ * full fw_device_shutdown(). If not, there's work
+ * scheduled to read it's config rom, and we just put
+ * the device in shutdown state to have that code fail
+ * to create the device. */
+ device = node->data;
+ if (xchg(&device->state,
+ FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
+ INIT_WORK(&device->work, fw_device_shutdown, device);
+ schedule_work(&device->work);
+ }
+ break;
+ }
+}
diff --git a/drivers/fw/fw-device.h b/drivers/fw/fw-device.h
new file mode 100644
index 0000000..fe0c6e5
--- /dev/null
+++ b/drivers/fw/fw-device.h
@@ -0,0 +1,156 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-device.h - Device probing and sysfs code.
+ *
+ * Copyright © 2005 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __fw_device_h
+#define __fw_device_h
+
+#include <linux/fs.h>
+#include <linux/cdev.h>
+
+enum fw_device_state {
+ FW_DEVICE_INITIALIZING,
+ FW_DEVICE_RUNNING,
+ FW_DEVICE_SHUTDOWN
+};
+
+struct fw_config_rom {
+ u32 crc : 16;
+ u32 crc_length : 8;
+ u32 info_length : 8;
+ u32 magic;
+
+
+ u32 link_speed : 3;
+ u32 reserved0 : 1;
+ u32 generation : 4;
+
+ u32 max_rom : 2;
+ u32 reserved1 : 2;
+ u32 max_receive : 4;
+
+ u32 cyc_clk_acc : 8;
+
+ u32 reserved : 3;
+ u32 pmc : 1;
+ u32 bmc : 1;
+ u32 isc : 1;
+ u32 cmc : 1;
+ u32 imc : 1;
+
+ u32 guid_high;
+ u32 guid_low;
+ u32 data[0];
+};
+
+struct fw_device {
+ int state;
+ struct fw_node *node;
+ int node_id;
+ int generation;
+ struct fw_card *card;
+ struct device device;
+ struct cdev cdev;
+ struct fw_config_rom *config_rom;
+ size_t config_rom_length;
+ int config_rom_retries;
+ struct work_struct work;
+};
+
+static inline struct fw_device *
+fw_device(struct device *dev)
+{
+ return container_of(dev, struct fw_device, device);
+}
+
+struct fw_device *fw_device_get(struct fw_device *device);
+void fw_device_put(struct fw_device *device);
+int fw_device_enable_phys_dma(struct fw_device *device);
+
+struct fw_unit {
+ struct device device;
+ u32 *directory;
+};
+
+static inline struct fw_unit *
+fw_unit(struct device *dev)
+{
+ return container_of(dev, struct fw_unit, device);
+}
+
+#define CSR_OFFSET 0x40
+#define CSR_LEAF 0x80
+#define CSR_DIRECTORY 0xc0
+
+#define CSR_DESCRIPTOR 0x01
+#define CSR_VENDOR 0x03
+#define CSR_HARDWARE_VERSION 0x04
+#define CSR_NODE_CAPABILITIES 0x0c
+#define CSR_UNIT 0x11
+#define CSR_SPECIFIER_ID 0x12
+#define CSR_VERSION 0x13
+#define CSR_DEPENDENT_INFO 0x14
+#define CSR_MODEL 0x17
+#define CSR_INSTANCE 0x18
+
+#define SBP2_COMMAND_SET_SPECIFIER 0x38
+#define SBP2_COMMAND_SET 0x39
+#define SBP2_COMMAND_SET_REVISION 0x3b
+#define SBP2_FIRMWARE_REVISION 0x3c
+
+struct fw_csr_iterator {
+ u32 *p;
+ u32 *end;
+};
+
+void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 *p);
+int fw_csr_iterator_next(struct fw_csr_iterator *ci,
+ int *key, int *value);
+
+#define FW_MATCH_VENDOR 0x0001
+#define FW_MATCH_MODEL 0x0002
+#define FW_MATCH_SPECIFIER_ID 0x0004
+#define FW_MATCH_VERSION 0x0008
+
+struct fw_device_id {
+ u32 match_flags;
+ u32 vendor;
+ u32 model;
+ u32 specifier_id;
+ u32 version;
+ void *driver_data;
+};
+
+struct fw_driver {
+ struct device_driver driver;
+ /* Called when the parent device sits through a bus reset. */
+ void (*update) (struct fw_unit *unit);
+ struct fw_device_id *id_table;
+};
+
+static inline struct fw_driver *
+fw_driver(struct device_driver *drv)
+{
+ return container_of(drv, struct fw_driver, driver);
+}
+
+extern struct file_operations fw_device_ops;
+
+#endif
diff --git a/drivers/fw/fw-topology.c b/drivers/fw/fw-topology.c
index 9fe1ab8..d691115 100644
--- a/drivers/fw/fw-topology.c
+++ b/drivers/fw/fw-topology.c
@@ -438,7 +438,3 @@ fw_core_handle_bus_reset(struct fw_card
}

EXPORT_SYMBOL(fw_core_handle_bus_reset);
-
-void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
-{
-}

2006-12-05 06:40:44

by Kristian Høgsberg

[permalink] [raw]
Subject: [PATCH 1/2] Import fw-core driver.

Pull in the core stack and build system changes.

Signed-off-by: Kristian Høgsberg <[email protected]>
---

drivers/Kconfig | 2
drivers/Makefile | 1
drivers/fw/Kconfig | 23 +
drivers/fw/Makefile | 7
drivers/fw/fw-card.c | 410 +++++++++++++++++++++
drivers/fw/fw-iso.c | 136 +++++++
drivers/fw/fw-topology.c | 444 ++++++++++++++++++++++
drivers/fw/fw-topology.h | 131 +++++++
drivers/fw/fw-transaction.c | 856 +++++++++++++++++++++++++++++++++++++++++++
drivers/fw/fw-transaction.h | 415 +++++++++++++++++++++
10 files changed, 2425 insertions(+), 0 deletions(-)

diff --git a/drivers/Kconfig b/drivers/Kconfig
index f394634..cf39e45 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -30,6 +30,8 @@ source "drivers/md/Kconfig"

source "drivers/message/fusion/Kconfig"

+source "drivers/fw/Kconfig"
+
source "drivers/ieee1394/Kconfig"

source "drivers/message/i2o/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 4ac14da..43335ef 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -36,6 +36,7 @@ obj-$(CONFIG_FC4) += fc4/
obj-$(CONFIG_SCSI) += scsi/
obj-$(CONFIG_ATA) += ata/
obj-$(CONFIG_FUSION) += message/
+obj-$(CONFIG_FW_CORE) += fw/
obj-$(CONFIG_IEEE1394) += ieee1394/
obj-y += cdrom/
obj-$(CONFIG_MTD) += mtd/
diff --git a/drivers/fw/Kconfig b/drivers/fw/Kconfig
new file mode 100644
index 0000000..f09ed59
--- /dev/null
+++ b/drivers/fw/Kconfig
@@ -0,0 +1,23 @@
+# -*- shell-script -*-
+
+menu "IEEE 1394 (FireWire) support (JUJU alternative stack)"
+
+config FW_CORE
+ tristate "IEEE 1394 (FireWire) support (JUJU alternative stack)"
+ help
+ IEEE 1394 describes a high performance serial bus, which is also
+ known as FireWire(tm) or i.Link(tm) and is used for connecting all
+ sorts of devices (most notably digital video cameras) to your
+ computer.
+
+ If you have FireWire hardware and want to use it, say Y here. This
+ is the core support only, you will also need to select a driver for
+ your IEEE 1394 adapter.
+
+ This is the "JUJU" firewire stack, an alternative
+ implementation designed for roboustness and simplicity.
+
+ To compile this driver as a module, say M here: the
+ module will be called fw-core.
+
+endmenu
diff --git a/drivers/fw/Makefile b/drivers/fw/Makefile
new file mode 100644
index 0000000..1b2a3ad
--- /dev/null
+++ b/drivers/fw/Makefile
@@ -0,0 +1,7 @@
+#
+# Makefile for the Linux IEEE 1394 implementation
+#
+
+fw-core-objs := fw-card.o fw-topology.o fw-transaction.o fw-iso.o
+
+obj-$(CONFIG_FW_CORE) += fw-core.o
diff --git a/drivers/fw/fw-card.c b/drivers/fw/fw-card.c
new file mode 100644
index 0000000..6840441
--- /dev/null
+++ b/drivers/fw/fw-card.c
@@ -0,0 +1,410 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-card.c - card level functions
+ *
+ * Copyright © 2005 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/device.h>
+#include "fw-transaction.h"
+#include "fw-topology.h"
+#include "fw-device.h"
+
+/* The lib/crc16.c implementation uses the standard (0x8005)
+ * polynomial, but we need the ITU-T (or CCITT) polynomial (0x1021).
+ * The implementation below works on an array of host-endian u32
+ * words, assuming they'll be transmited msb first. */
+static u16
+crc16_itu_t(const u32 *buffer, size_t length)
+{
+ int shift, i;
+ u32 data;
+ u16 sum, crc = 0;
+
+ for (i = 0; i < length; i++) {
+ data = *buffer++;
+ for (shift = 28; shift >= 0; shift -= 4 ) {
+ sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
+ crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
+ }
+ crc &= 0xffff;
+ }
+
+ return crc;
+}
+
+static LIST_HEAD(card_list);
+
+static LIST_HEAD(descriptor_list);
+static int descriptor_count;
+
+static u32 *
+generate_config_rom (struct fw_card *card, size_t *config_rom_length)
+{
+ struct fw_descriptor *desc;
+ struct fw_config_rom *crh;
+ static u32 config_rom[256];
+ int i, j, length;
+
+ /* Initialize contents of config rom buffer. On the OHCI
+ * controller, block reads to the config rom accesses the host
+ * memory, but quadlet read access the hardware bus info block
+ * registers. That's just crack, but it means we should make
+ * sure the contents of bus info block in host memory mathces
+ * the version stored in the OHCI registers. */
+
+ memset(config_rom, 0, sizeof config_rom);
+ crh = (struct fw_config_rom *) config_rom;
+ crh->crc_length = 4;
+ crh->info_length = 4;
+ crh->crc = 0;
+ crh->magic = 0x31333934;
+
+ crh->link_speed = card->link_speed;
+ /* 1394a 10.25 specifies that the config rom generation field
+ * must be updated every time a node updates its config rom
+ * and that it should be a number between 0x2 and 0xf; thus the
+ * generation++ % 14 + 2. */
+ crh->generation = card->config_rom_generation++ % 14 + 2;
+ crh->max_rom = 2;
+ crh->max_receive = card->max_receive;
+ crh->isc = 1;
+ crh->cmc = 1;
+ crh->imc = 1;
+ crh->guid_high = card->guid >> 32;
+ crh->guid_low = card->guid;
+
+ /* Generate root directory. */
+ i = 0;
+ crh->data[i++] = 0;
+ crh->data[i++] = 0x0c0083c0; /* node capabilities */
+ crh->data[i++] = 0x03d00d1e; /* vendor id */
+ j = i + descriptor_count;
+
+ /* Generate root directory entries for descriptors. */
+ list_for_each_entry (desc, &descriptor_list, link) {
+ crh->data[i] = desc->key | (j - i);
+ i++;
+ j += desc->length;
+ }
+
+ /* Update root directory length. */
+ crh->data[0] = (i - 1) << 16;
+
+ /* End of root directory, now copy in descriptors. */
+ list_for_each_entry (desc, &descriptor_list, link) {
+ memcpy(&crh->data[i], desc->data, desc->length * 4);
+ i += desc->length;
+ }
+
+ /* Byteswap and calculate CRCs for all blocks in the config
+ * rom. This assumes that CRC length and info length are
+ * identical for the bus info block, which is always the case
+ * for this implementation. */
+ for (i = 0; i < 5 + j; i += length + 1) {
+ length = (config_rom[i] >> 16) & 0xff;
+ config_rom[i] |= crc16_itu_t(&config_rom[i + 1], length);
+ }
+
+ *config_rom_length = 5 + j;
+
+ return config_rom;
+}
+
+static void
+update_config_roms (void)
+{
+ struct fw_card *card;
+ u32 *config_rom;
+ int length;
+
+ list_for_each_entry (card, &card_list, link) {
+ config_rom = generate_config_rom(card, &length);
+ card->driver->set_config_rom(card, config_rom, length);
+ }
+}
+
+int
+fw_core_add_descriptor (struct fw_descriptor *desc)
+{
+ size_t i;
+
+ /* Check descriptor is valid; the length of all blocks in the
+ * descriptor has to add up to exactly the length of the
+ * block. */
+ i = 0;
+ while (i < desc->length)
+ i += (desc->data[i] >> 16) + 1;
+
+ if (i != desc->length)
+ return -1;
+
+ down_write(&fw_bus_type.subsys.rwsem);
+
+ list_add_tail (&desc->link, &descriptor_list);
+ descriptor_count++;
+ update_config_roms();
+
+ up_write(&fw_bus_type.subsys.rwsem);
+
+ return 0;
+}
+EXPORT_SYMBOL(fw_core_add_descriptor);
+
+void
+fw_core_remove_descriptor (struct fw_descriptor *desc)
+{
+ down_write(&fw_bus_type.subsys.rwsem);
+
+ list_del(&desc->link);
+ descriptor_count--;
+ update_config_roms();
+
+ up_write(&fw_bus_type.subsys.rwsem);
+}
+EXPORT_SYMBOL(fw_core_remove_descriptor);
+
+static void
+fw_card_irm_work(void *data)
+{
+ struct fw_card *card = data;
+ struct fw_device *root;
+ unsigned long flags;
+
+ /* FIXME: This simple bus management unconditionally picks a
+ * cycle master if the current root can't do it. We need to
+ * not do this if there is a bus manager already. Also, some
+ * hubs set the contender bit, which is bogus, so we should
+ * probably do a little sanity check on the IRM (like, read
+ * the bandwidth register) if it's not us. */
+
+ spin_lock_irqsave(&card->lock, flags);
+
+ root = card->root_node->data;
+
+ /* Either link_on is false, or we failed to read the config
+ * rom. In either case, pick another root. */
+ if (root == NULL)
+ goto reset;
+
+ /* If we haven't probed this device yet, bail out now and
+ * let's try again once that's done. */
+ if (root->state != FW_DEVICE_RUNNING)
+ goto out;
+
+ if (root->config_rom->cmc)
+ /* FIXME: I suppose we should set the cmstr bit in the
+ * STATE_CLEAR register of this node, as described in
+ * 1394-1995, 8.4.2.6. Also, send out a force root
+ * packet for this node. */
+ goto out;
+
+ reset:
+ if (card->irm_retries++ > 5)
+ goto out;
+
+ fw_notify("Trying to become root (card %p)\n", card);
+ fw_send_force_root(card, card->local_node->node_id,
+ card->generation);
+ fw_core_initiate_bus_reset(card, 1);
+
+ out:
+ spin_unlock_irqrestore(&card->lock, flags);
+}
+
+static void
+release_card(struct device *device)
+{
+ struct fw_card *card =
+ container_of(device, struct fw_card, card_device);
+
+ kfree(card);
+}
+
+int
+fw_core_add_card (struct fw_card *card, struct fw_card_driver *driver,
+ struct device *device,
+ u32 max_receive, u32 link_speed, u64 guid)
+{
+ static int index;
+ u32 *config_rom;
+ int length, retval;
+
+ card->index = index++;
+ card->driver = driver;
+ card->device = device;
+ card->max_receive = max_receive;
+ card->link_speed = link_speed;
+ card->guid = guid;
+ card->current_tlabel = 0;
+ card->tlabel_mask = 0;
+ card->color = 0;
+
+ INIT_LIST_HEAD(&card->transaction_list);
+ spin_lock_init(&card->lock);
+
+ card->local_node = NULL;
+
+ /* FIXME: add #define's for phy registers. */
+ /* Activate link_on bit and contender bit in our self ID packets.*/
+ if (driver->update_phy_reg(card, 4, 0, 0x80 | 0x40) < 0)
+ return -EIO;
+
+ down_write(&fw_bus_type.subsys.rwsem);
+
+ config_rom = generate_config_rom (card, &length);
+
+ list_add_tail(&card->link, &card_list);
+
+ up_write(&fw_bus_type.subsys.rwsem);
+
+ INIT_WORK(&card->work, fw_card_irm_work, card);
+
+ card->card_device.bus = &fw_bus_type;
+ card->card_device.release = release_card;
+ card->card_device.parent = card->device;
+ snprintf(card->card_device.bus_id, sizeof card->card_device.bus_id,
+ "fwcard%d", card->index);
+
+ retval = device_register(&card->card_device);
+ if (retval < 0) {
+ fw_error("Failed to register card device.");
+ return retval;
+ }
+
+ /* The subsystem grabs a reference when the card is added and
+ * drops it when the driver calls fw_core_remove_card. */
+ fw_card_get(card);
+
+ return driver->enable(card, config_rom, length);
+}
+EXPORT_SYMBOL(fw_core_add_card);
+
+/* The next few functions implements a dummy driver that use once a
+ * card driver shuts down an fw_card. This allows the driver to
+ * cleanly unload, as all IO to the card will be handled by the dummy
+ * driver instead of calling into the (possibly) unloaded module. The
+ * dummy driver just fails all IO. */
+
+static int
+dummy_enable(struct fw_card *card, u32 *config_rom, size_t length)
+{
+ BUG();
+ return -1;
+}
+
+static int
+dummy_update_phy_reg(struct fw_card *card, int address,
+ int clear_bits, int set_bits)
+{
+ return -ENODEV;
+}
+
+static int
+dummy_set_config_rom(struct fw_card *card,
+ u32 *config_rom, size_t length)
+{
+ /* We take the card out of card_list before setting the dummy
+ * driver, so this should never get called. */
+ BUG();
+ return -1;
+}
+
+static void
+dummy_send_request(struct fw_card *card, struct fw_packet *packet)
+{
+ packet->callback(packet, card, -ENODEV);
+}
+
+static void
+dummy_send_response(struct fw_card *card, struct fw_packet *packet)
+{
+ packet->callback(packet, card, -ENODEV);
+}
+
+static int
+dummy_enable_phys_dma(struct fw_card *card,
+ int node_id, int generation)
+{
+ return -ENODEV;
+}
+
+static struct fw_card_driver dummy_driver = {
+ .name = "dummy",
+ .enable = dummy_enable,
+ .update_phy_reg = dummy_update_phy_reg,
+ .set_config_rom = dummy_set_config_rom,
+ .send_request = dummy_send_request,
+ .send_response = dummy_send_response,
+ .enable_phys_dma = dummy_enable_phys_dma
+};
+
+void flush_transactions(struct fw_card *card, u32 timestamp);
+
+void
+fw_core_remove_card(struct fw_card *card)
+{
+ down_write(&fw_bus_type.subsys.rwsem);
+ list_del(&card->link);
+ up_write(&fw_bus_type.subsys.rwsem);
+
+ /* Set up the dummy driver. */
+ card->driver = &dummy_driver;
+
+ flush_transactions(card, 0x20000);
+
+ fw_destroy_nodes(card);
+
+ /* This also drops the subsystem reference. */
+ device_unregister(&card->card_device);
+}
+EXPORT_SYMBOL(fw_core_remove_card);
+
+struct fw_card *
+fw_card_get(struct fw_card *card)
+{
+ get_device(&card->card_device);
+
+ return card;
+}
+EXPORT_SYMBOL(fw_card_get);
+
+/* An assumption for fw_card_put() is that the card driver allocates
+ * the fw_card struct with kalloc and that it has been shut down
+ * before the last ref is dropped. */
+void
+fw_card_put(struct fw_card *card)
+{
+ put_device(&card->card_device);
+}
+EXPORT_SYMBOL(fw_card_put);
+
+int
+fw_core_initiate_bus_reset(struct fw_card *card, int short_reset)
+{
+ u32 address;
+
+ if (short_reset)
+ address = 5;
+ else
+ address = 1;
+
+ return card->driver->update_phy_reg(card, address, 0, 0x40);
+}
+EXPORT_SYMBOL(fw_core_initiate_bus_reset);
diff --git a/drivers/fw/fw-iso.c b/drivers/fw/fw-iso.c
new file mode 100644
index 0000000..8cfaa5a
--- /dev/null
+++ b/drivers/fw/fw-iso.c
@@ -0,0 +1,136 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-iso.c - Isochronous IO
+ * Copyright (C) 2006 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/dma-mapping.h>
+#include <linux/vmalloc.h>
+
+#include "fw-transaction.h"
+#include "fw-topology.h"
+#include "fw-device.h"
+
+static int
+setup_iso_buffer(struct fw_iso_context *ctx, size_t size,
+ enum dma_data_direction direction)
+{
+ struct page *page;
+ int i;
+ void *p;
+
+ ctx->buffer_size = PAGE_ALIGN(size);
+ if (size == 0)
+ return 0;
+
+ ctx->buffer = vmalloc_32_user(ctx->buffer_size);
+ if (ctx->buffer == NULL)
+ return -ENOMEM;
+
+ ctx->page_count = ctx->buffer_size >> PAGE_SHIFT;
+ ctx->pages =
+ kzalloc(ctx->page_count * sizeof(ctx->pages[0]), GFP_KERNEL);
+ if (ctx->pages == NULL) {
+ vfree(ctx->buffer);
+ return -ENOMEM;
+ }
+
+ p = ctx->buffer;
+ for (i = 0; i < ctx->page_count; i++, p += PAGE_SIZE) {
+ page = vmalloc_to_page(p);
+ ctx->pages[i] = dma_map_page(ctx->card->device,
+ page, 0, PAGE_SIZE, direction);
+ }
+
+ return 0;
+}
+
+static void destroy_iso_buffer(struct fw_iso_context *ctx)
+{
+ int i;
+
+ for (i = 0; i < ctx->page_count; i++)
+ dma_unmap_page(ctx->card->device, ctx->pages[i],
+ PAGE_SIZE, DMA_TO_DEVICE);
+
+ kfree(ctx->pages);
+ vfree(ctx->buffer);
+}
+
+struct fw_iso_context *fw_iso_context_create(struct fw_card *card, int type,
+ size_t buffer_size,
+ fw_iso_callback_t callback,
+ void *callback_data)
+{
+ struct fw_iso_context *ctx;
+ int retval;
+
+ ctx = card->driver->allocate_iso_context(card, type);
+ if (IS_ERR(ctx))
+ return ctx;
+
+ ctx->card = card;
+ ctx->type = type;
+ ctx->callback = callback;
+ ctx->callback_data = callback_data;
+
+ retval = setup_iso_buffer(ctx, buffer_size, DMA_TO_DEVICE);
+ if (retval < 0) {
+ card->driver->free_iso_context(ctx);
+ return ERR_PTR(retval);
+ }
+
+ return ctx;
+}
+
+EXPORT_SYMBOL(fw_iso_context_create);
+
+void fw_iso_context_destroy(struct fw_iso_context *ctx)
+{
+ struct fw_card *card = ctx->card;
+
+ destroy_iso_buffer(ctx);
+
+ card->driver->free_iso_context(ctx);
+}
+
+EXPORT_SYMBOL(fw_iso_context_destroy);
+
+int
+fw_iso_context_send(struct fw_iso_context *ctx,
+ int channel, int speed, int cycle)
+{
+ ctx->channel = channel;
+ ctx->speed = speed;
+
+ return ctx->card->driver->send_iso(ctx, cycle);
+}
+
+EXPORT_SYMBOL(fw_iso_context_send);
+
+int
+fw_iso_context_queue(struct fw_iso_context *ctx,
+ struct fw_iso_packet *packet, void *payload)
+{
+ struct fw_card *card = ctx->card;
+
+ return card->driver->queue_iso(ctx, packet, payload);
+}
+
+EXPORT_SYMBOL(fw_iso_context_queue);
diff --git a/drivers/fw/fw-topology.c b/drivers/fw/fw-topology.c
new file mode 100644
index 0000000..9fe1ab8
--- /dev/null
+++ b/drivers/fw/fw-topology.c
@@ -0,0 +1,444 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-topology.c - Incremental bus scan, based on bus topology
+ *
+ * Copyright (C) 2004 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/wait.h>
+#include <linux/errno.h>
+#include "fw-transaction.h"
+#include "fw-topology.h"
+
+static struct fw_phy_packet *count_ports(struct fw_phy_packet *sid,
+ int *total_port_count,
+ int *child_port_count)
+{
+ struct fw_phy_packet q;
+ int port_type, shift, seq;
+
+ *total_port_count = 0;
+ *child_port_count = 0;
+
+ shift = 6;
+ q = *sid;
+ seq = 0;
+
+ while (1) {
+ port_type = (q.quadlet >> shift) & 0x03;
+ switch (port_type) {
+ case SELFID_PORT_CHILD:
+ (*child_port_count)++;
+ case SELFID_PORT_PARENT:
+ case SELFID_PORT_NCONN:
+ (*total_port_count)++;
+ case SELFID_PORT_NONE:
+ break;
+ }
+
+ shift -= 2;
+ if (shift == 0) {
+ if (!q.self_id.more_packets)
+ return sid + 1;
+
+ shift = 16;
+ sid++;
+ q = *sid;
+
+ /* Check that the extra packets actually are
+ * extended self ID packets and that the
+ * sequence numbers in the extended self ID
+ * packets increase as expected. */
+
+ if (!q.extended_self_id.extended ||
+ seq != q.extended_self_id.sequence)
+ return NULL;
+
+ seq++;
+ }
+ }
+}
+
+static int get_port_type(struct fw_phy_packet *sid, int port_index)
+{
+ int index, shift;
+
+ index = (port_index + 5) / 8;
+ shift = 16 - ((port_index + 5) & 7) * 2;
+ return (sid[index].quadlet >> shift) & 0x03;
+}
+
+/* Nodes are created with a ref count of one, belonging to the tree
+ * they are part of. */
+struct fw_node *fw_node_create(struct fw_phy_packet sid, int port_count,
+ int color)
+{
+ struct fw_node *node;
+
+ node = kzalloc(sizeof *node + port_count * sizeof node->ports[0],
+ GFP_ATOMIC);
+ if (node == NULL)
+ return NULL;
+
+ node->color = color;
+ node->node_id = sid.self_id.phy_id;
+ node->link_on = sid.self_id.link_on;
+ node->phy_speed = sid.self_id.phy_speed;
+ node->port_count = port_count;
+
+ atomic_set(&node->ref_count, 1);
+ INIT_LIST_HEAD(&node->link);
+
+ return node;
+}
+
+/**
+ * build_tree - Build the tree representation of the topology
+ * @self_ids: array of self IDs to create the tree from
+ * @self_id_count: the length of the self_ids array
+ * @local_id: the node ID of the local node
+ *
+ * This function builds the tree representation of the topology given
+ * by the self IDs from the latest bus reset. During the construction
+ * of the tree, the function checks that the self IDs are valid and
+ * internally consistent. On succcess this funtions returns the
+ * fw_node corresponding to the local card otherwise NULL.
+ */
+static struct fw_node *build_tree(struct fw_card *card)
+{
+ struct fw_node *node, *child, *local_node;
+ struct list_head stack, *h;
+ struct fw_phy_packet *sid, *next_sid, *end, q;
+ int i, port_count, child_port_count, phy_id, parent_count, stack_depth;
+
+ local_node = NULL;
+ node = NULL;
+ INIT_LIST_HEAD(&stack);
+ stack_depth = 0;
+ sid = (struct fw_phy_packet *)card->self_ids;
+ end = sid + card->self_id_count;
+ phy_id = 0;
+ card->irm_node = NULL;
+
+ while (sid < end) {
+ next_sid = count_ports(sid, &port_count, &child_port_count);
+
+ if (next_sid == NULL) {
+ fw_error("Inconsistent extended self IDs.\n");
+ return NULL;
+ }
+
+ q = *sid;
+ if (phy_id != q.self_id.phy_id) {
+ fw_error("PHY ID mismatch in self ID: %d != %d.\n",
+ phy_id, q.self_id.phy_id);
+ return NULL;
+ }
+
+ if (child_port_count > stack_depth) {
+ fw_error("Topology stack underflow\n");
+ return NULL;
+ }
+
+ /* Seek back from the top of our stack to find the
+ * start of the child nodes for this node. */
+ for (i = 0, h = &stack; i < child_port_count; i++)
+ h = h->prev;
+ child = fw_node(h);
+
+ node = fw_node_create(q, port_count, card->color);
+ if (node == NULL) {
+ fw_error("Out of memory while building topology.");
+ return NULL;
+ }
+
+ if (phy_id == (card->node_id & 0x3f))
+ local_node = node;
+
+ if (q.self_id.contender)
+ card->irm_node = node;
+
+ parent_count = 0;
+
+ for (i = 0; i < port_count; i++) {
+ switch (get_port_type(sid, i)) {
+ case SELFID_PORT_PARENT:
+ /* Who's your daddy? We dont know the
+ * parent node at this time, so we
+ * temporarily abuse node->color for
+ * remembering the entry in the
+ * node->ports array where the parent
+ * node should be. Later, when we
+ * handle the parent node, we fix up
+ * the reference.
+ */
+ parent_count++;
+ node->color = i;
+ break;
+
+ case SELFID_PORT_CHILD:
+ node->ports[i].node = child;
+ /* Fix up parent reference for this
+ * child node. */
+ child->ports[child->color].node = node;
+ child->color = card->color;
+ child = fw_node(child->link.next);
+ break;
+ }
+ }
+
+ /* Check that the node reports exactly one parent
+ * port, except for the root, which of course should
+ * have no parents. */
+ if ((next_sid == end && parent_count != 0) ||
+ (next_sid < end && parent_count != 1)) {
+ fw_error("Parent port inconsistency for node %d: "
+ "parent_count=%d\n", phy_id, parent_count);
+ return NULL;
+ }
+
+ /* Pop the child nodes off the stack and push the new node. */
+ __list_del(h->prev, &stack);
+ list_add_tail(&node->link, &stack);
+ stack_depth += 1 - child_port_count;
+
+ sid = next_sid;
+ phy_id++;
+ }
+
+ card->root_node = node;
+
+ return local_node;
+}
+
+typedef void (*fw_node_callback_t) (struct fw_card * card,
+ struct fw_node * node,
+ struct fw_node * parent);
+
+static void
+for_each_fw_node(struct fw_card *card, struct fw_node *root,
+ fw_node_callback_t callback)
+{
+ struct list_head list;
+ struct fw_node *node, *child, *parent;
+ int i;
+
+ INIT_LIST_HEAD(&list);
+
+ list_add_tail(&root->link, &list);
+ parent = NULL;
+ while (!list_empty(&list)) {
+ node = fw_node(list.next);
+ list_del(&node->link);
+ node->color = card->color;
+
+ for (i = 0; i < node->port_count; i++) {
+ child = node->ports[i].node;
+ if (!child)
+ continue;
+ if (child->color == card->color)
+ parent = child;
+ else
+ list_add_tail(&child->link, &list);
+ }
+
+ callback(card, node, parent);
+ }
+}
+
+static void
+report_lost_node(struct fw_card *card,
+ struct fw_node *node, struct fw_node *parent)
+{
+ fw_node_event(card, node, FW_NODE_DESTROYED);
+ fw_node_put(node);
+}
+
+static void
+report_found_node(struct fw_card *card,
+ struct fw_node *node, struct fw_node *parent)
+{
+ int b_path = (node->phy_speed == SCODE_BETA);
+
+ if (parent != NULL) {
+ node->max_speed = min(parent->max_speed, node->phy_speed);
+ node->b_path = parent->b_path && b_path;
+ } else {
+ node->max_speed = node->phy_speed;
+ node->b_path = b_path;
+ }
+
+ fw_node_event(card, node, FW_NODE_CREATED);
+}
+
+void fw_destroy_nodes(struct fw_card *card)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&card->lock, flags);
+ card->color++;
+ if (card->local_node != NULL)
+ for_each_fw_node(card, card->local_node, report_lost_node);
+ spin_unlock_irqrestore(&card->lock, flags);
+}
+
+static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)
+{
+ struct fw_node *tree;
+ int i;
+
+ tree = node1->ports[port].node;
+ node0->ports[port].node = tree;
+ for (i = 0; i < tree->port_count; i++) {
+ if (tree->ports[i].node == node1) {
+ tree->ports[i].node = node0;
+ break;
+ }
+ }
+}
+
+/**
+ * update_tree - compare the old topology tree for card with the new
+ * one specified by root. Queue the nodes and mark them as either
+ * found, lost or updated. Update the nodes in the card topology tree
+ * as we go.
+ */
+static void
+update_tree(struct fw_card *card, struct fw_node *root, int *changed)
+{
+ struct list_head list0, list1;
+ struct fw_node *node0, *node1;
+ int i, event;
+
+ INIT_LIST_HEAD(&list0);
+ list_add_tail(&card->local_node->link, &list0);
+ INIT_LIST_HEAD(&list1);
+ list_add_tail(&root->link, &list1);
+
+ node0 = fw_node(list0.next);
+ node1 = fw_node(list1.next);
+ *changed = 0;
+
+ while (&node0->link != &list0) {
+
+ /* assert(node0->port_count == node1->port_count); */
+ if (node0->link_on && !node1->link_on)
+ event = FW_NODE_LINK_OFF;
+ else if (!node0->link_on && node1->link_on)
+ event = FW_NODE_LINK_ON;
+ else
+ event = FW_NODE_UPDATED;
+
+ node0->node_id = node1->node_id;
+ node0->color = card->color;
+ node0->link_on = node1->link_on;
+ node0->initiated_reset = node1->initiated_reset;
+ node1->color = card->color;
+ fw_node_event(card, node0, event);
+
+ if (card->root_node == node1)
+ card->root_node = node0;
+ if (card->irm_node == node1)
+ card->irm_node = node0;
+
+ for (i = 0; i < node0->port_count; i++) {
+ if (node0->ports[i].node && node1->ports[i].node) {
+ /* This port didn't change, queue the
+ * connected node for further
+ * investigation. */
+ if (node0->ports[i].node->color == card->color)
+ continue;
+ list_add_tail(&node0->ports[i].node->link,
+ &list0);
+ list_add_tail(&node1->ports[i].node->link,
+ &list1);
+ } else if (node0->ports[i].node) {
+ /* The nodes connected here were
+ * unplugged; unref the lost nodes and
+ * queue FW_NODE_LOST callbacks for
+ * them. */
+
+ for_each_fw_node(card, node0->ports[i].node,
+ report_lost_node);
+ node0->ports[i].node = NULL;
+ *changed = 1;
+ } else if (node1->ports[i].node) {
+ /* One or more node were connected to
+ * this port. Move the new nodes into
+ * the tree and queue FW_NODE_CREATED
+ * callbacks for them. */
+ move_tree(node0, node1, i);
+ for_each_fw_node(card, node0->ports[i].node,
+ report_found_node);
+ *changed = 1;
+ }
+ }
+
+ node0 = fw_node(node0->link.next);
+ node1 = fw_node(node1->link.next);
+ }
+}
+
+void flush_transactions(struct fw_card *card, u32 timestamp);
+
+void
+fw_core_handle_bus_reset(struct fw_card *card,
+ int node_id, int generation,
+ int self_id_count, u32 * self_ids)
+{
+ struct fw_node *local_node;
+ unsigned long flags;
+ int changed;
+
+ flush_transactions(card, 0x20000);
+
+ spin_lock_irqsave(&card->lock, flags);
+
+ card->node_id = node_id;
+ card->self_id_count = self_id_count;
+ card->generation = generation;
+ memcpy(card->self_ids, self_ids, self_id_count * 4);
+
+ local_node = build_tree(card);
+
+ card->color++;
+
+ if (local_node == NULL) {
+ fw_error("topology build failed\n");
+ /* FIXME: We need to issue a bus reset in this case. */
+ } else if (card->local_node == NULL) {
+ card->local_node = local_node;
+ for_each_fw_node(card, local_node, report_found_node);
+ } else {
+ update_tree(card, local_node, &changed);
+ if (changed)
+ card->irm_retries = 0;
+ }
+
+ /* If we're not the root node, we may have to do some IRM work. */
+ if (card->local_node != card->root_node)
+ schedule_work(&card->work);
+
+ spin_unlock_irqrestore(&card->lock, flags);
+}
+
+EXPORT_SYMBOL(fw_core_handle_bus_reset);
+
+void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
+{
+}
diff --git a/drivers/fw/fw-topology.h b/drivers/fw/fw-topology.h
new file mode 100644
index 0000000..219f50e
--- /dev/null
+++ b/drivers/fw/fw-topology.h
@@ -0,0 +1,131 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-topology.h -- Incremental bus scan, based on bus topology
+ *
+ * Copyright (C) 2003 Kristian Hoegsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __fw_topology_h
+#define __fw_topology_h
+
+struct fw_phy_packet {
+ union {
+ struct {
+ u32 reserved : 24;
+ u32 phy_id : 6;
+ u32 identifier : 2;
+ } link_on;
+
+ struct {
+ u32 reserved : 16;
+ u32 gap_count : 6;
+ u32 t : 1;
+ u32 r : 1;
+ u32 root_id : 6;
+ u32 identifier : 2;
+ } phy_config;
+
+ struct {
+ u32 more_packets : 1;
+ u32 initiated_reset : 1;
+ u32 ports : 6;
+ u32 power : 3;
+ u32 contender : 1;
+ u32 delay : 2;
+ u32 phy_speed : 2;
+ u32 gap_count : 6;
+ u32 link_on : 1;
+ u32 extended : 1;
+ u32 phy_id : 6;
+ u32 packet_type : 2;
+ } self_id;
+
+ struct {
+ u32 more_packets : 1;
+ u32 reserved0 : 1;
+ u32 ports : 16;
+ u32 reserved1 : 2;
+ u32 sequence : 3;
+ u32 extended : 1;
+ u32 phy_id : 6;
+ u32 packet_type : 2;
+ } extended_self_id;
+
+ u32 quadlet;
+ };
+};
+
+enum {
+ FW_NODE_CREATED = 0x00,
+ FW_NODE_UPDATED = 0x01,
+ FW_NODE_DESTROYED = 0x02,
+ FW_NODE_LINK_ON = 0x03,
+ FW_NODE_LINK_OFF = 0x04
+};
+
+struct fw_port {
+ struct fw_node *node;
+ unsigned speed : 3; /* S100, S200, ... S3200 */
+};
+
+struct fw_node {
+ u16 node_id;
+ u8 color;
+ u8 port_count;
+ unsigned link_on : 1;
+ unsigned initiated_reset : 1;
+ unsigned b_path : 1;
+ u8 phy_speed; /* As in the self ID packet. */
+ u8 max_speed; /* Minimum of all phy-speeds and port speeds on
+ * the path from the local node to this node. */
+
+ atomic_t ref_count;
+
+ /* For serializing node topology into a list. */
+ struct list_head link;
+
+ /* Upper layer specific data. */
+ void *data;
+
+ struct fw_port ports[0];
+};
+
+extern inline struct fw_node *
+fw_node(struct list_head *l)
+{
+ return list_entry (l, struct fw_node, link);
+}
+
+extern inline struct fw_node *
+fw_node_get(struct fw_node *node)
+{
+ atomic_inc(&node->ref_count);
+
+ return node;
+}
+
+extern inline void
+fw_node_put(struct fw_node *node)
+{
+ if (atomic_dec_and_test(&node->ref_count))
+ kfree(node);
+}
+
+void
+fw_destroy_nodes(struct fw_card *card);
+
+#endif
diff --git a/drivers/fw/fw-transaction.c b/drivers/fw/fw-transaction.c
new file mode 100644
index 0000000..a42e985
--- /dev/null
+++ b/drivers/fw/fw-transaction.c
@@ -0,0 +1,856 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-transaction.c - core IEEE1394 transaction logic
+ *
+ * Copyright (C) 2004 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/pci.h>
+#include <linux/delay.h>
+#include <linux/poll.h>
+#include <linux/list.h>
+#include <linux/kthread.h>
+#include <asm/uaccess.h>
+#include <asm/semaphore.h>
+
+#include "fw-transaction.h"
+#include "fw-topology.h"
+#include "fw-device.h"
+
+struct fw_packet_header {
+ union {
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int offset_high:16;
+ unsigned int source:16;
+ unsigned long offset_low;
+ } request;
+
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int reserved0:12;
+ unsigned int rcode:4;
+ unsigned int source:16;
+ unsigned long reserved1;
+ } response;
+
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int offset_high:16;
+ unsigned int source:16;
+ unsigned long offset_low;
+ } read_quadlet_request;
+
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int reserved0:12;
+ unsigned int rcode:4;
+ unsigned int source:16;
+ unsigned long reserved1;
+ unsigned long data;
+ } read_quadlet_response;
+
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int offset_high:16;
+ unsigned int source:16;
+ unsigned long offset_low;
+ unsigned int extended_tcode:16;
+ unsigned int data_length:16;
+ } read_block_request;
+
+ /* This response packet format is the same for block
+ * read responses and lock responses. Furthermore, we
+ * define a field called block_response, for the
+ * situations where we want to treat responses with
+ * block payload generically. */
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int reserved0:12;
+ unsigned int rcode:4;
+ unsigned int source:16;
+ unsigned long reserved1;
+ unsigned int extended_tcode:16;
+ unsigned int data_length:16;
+ u32 data[0];
+ } read_block_response, lock_response, block_response;
+
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int offset_high:16;
+ unsigned int source:16;
+ u32 offset_low;
+ u32 data;
+ } write_quadlet_request;
+
+ /* This response packet format is the same for block
+ * write requests and lock requests. Furthermore, we
+ * define a field called block_request, for the
+ * situations where we want to treat requests with
+ * block payload generically. */
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int offset_high:16;
+ unsigned int source:16;
+ unsigned int offset_low:32;
+ unsigned int extended_tcode:16;
+ unsigned int data_length:16;
+ u32 data[0];
+ } write_block_request, lock_request, block_request;
+
+ struct {
+ unsigned int priority:4;
+ unsigned int tcode:4;
+ unsigned int rt:2;
+ unsigned int tlabel:6;
+ unsigned int destination:16;
+ unsigned int reserved0:12;
+ unsigned int rcode:4;
+ unsigned int source:16;
+ unsigned long reserved1;
+ } write_response;
+ };
+};
+
+static void
+close_transaction(struct fw_transaction *t, struct fw_card *card, int rcode,
+ u32 * payload, size_t length)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&card->lock, flags);
+ card->tlabel_mask &= ~(1 << t->tlabel);
+ list_del(&t->link);
+ spin_unlock_irqrestore(&card->lock, flags);
+
+ t->callback(card, rcode, payload, length, t->callback_data);
+}
+
+static struct fw_transaction *lookup_transaction(struct fw_card *card,
+ int source, int tlabel)
+{
+ struct fw_transaction *t, *result;
+ unsigned long flags;
+
+ result = NULL;
+ spin_lock_irqsave(&card->lock, flags);
+ list_for_each_entry(t, &card->transaction_list, link) {
+ if (t->node_id == source && t->tlabel == tlabel) {
+ result = t;
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&card->lock, flags);
+
+ return result;
+}
+
+static void
+transmit_complete_callback(struct fw_packet *packet,
+ struct fw_card *card, int status)
+{
+ struct fw_transaction *t =
+ container_of(packet, struct fw_transaction, packet);
+
+ switch (status) {
+ case ACK_COMPLETE:
+ close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
+ break;
+ case ACK_PENDING:
+ t->timestamp = packet->timestamp;
+ break;
+ case ACK_BUSY_X:
+ case ACK_BUSY_A:
+ case ACK_BUSY_B:
+ close_transaction(t, card, RCODE_BUSY, NULL, 0);
+ break;
+ case ACK_DATA_ERROR:
+ case ACK_TYPE_ERROR:
+ close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
+ break;
+ default:
+ /* FIXME: In this case, status is a negative errno,
+ * corresponding to an OHCI specific transmit error
+ * code. We should map that to an RCODE instead of
+ * just the generic RCODE_SEND_ERROR. */
+ close_transaction(t, card, RCODE_SEND_ERROR, NULL, 0);
+ break;
+ }
+}
+
+void
+fw_fill_packet(struct fw_packet *packet, int tcode, int tlabel,
+ int node_id, int generation, int speed,
+ unsigned long long offset, u32 * payload, size_t length)
+{
+ int ext_tcode;
+ struct fw_packet_header *header;
+
+ if (tcode > 0x10) {
+ ext_tcode = tcode - 0x10;
+ tcode = TCODE_LOCK_REQUEST;
+ } else
+ ext_tcode = 0;
+
+ header = (struct fw_packet_header *)packet->header;
+ header->request.priority = 0;
+ header->request.rt = RETRY_X;
+ header->request.tlabel = tlabel;
+ header->request.tcode = tcode;
+ header->request.destination = node_id | LOCAL_BUS;
+ header->request.offset_high = offset >> 32;
+ header->request.source = 0; /* Inserted by OHCI controller */
+ header->request.offset_low = offset;
+
+ switch (tcode) {
+ case TCODE_WRITE_QUADLET_REQUEST:
+ header->write_quadlet_request.data = payload[0];
+ packet->header_length = sizeof header->write_quadlet_request;
+ packet->payload_length = 0;
+ break;
+
+ case TCODE_LOCK_REQUEST:
+ case TCODE_WRITE_BLOCK_REQUEST:
+ header->block_request.data_length = length;
+ header->block_request.extended_tcode = ext_tcode;
+ packet->header_length = sizeof header->block_request;
+ packet->payload = payload;
+ packet->payload_length = length;
+ break;
+
+ case TCODE_READ_QUADLET_REQUEST:
+ packet->header_length = sizeof header->read_quadlet_request;
+ packet->payload_length = 0;
+ break;
+
+ case TCODE_READ_BLOCK_REQUEST:
+ header->read_block_request.data_length = length;
+ header->read_block_request.extended_tcode = 0;
+ packet->header_length = sizeof header->read_block_request;
+ packet->payload_length = 0;
+ break;
+ }
+
+ packet->speed = speed;
+ packet->generation = generation;
+}
+
+/**
+ * This function provides low-level access to the IEEE1394 transaction
+ * logic. Most C programs would use either fw_read(), fw_write() or
+ * fw_lock() instead - those function are convenience wrappers for
+ * this function. The fw_send_request() function is primarily
+ * provided as a flexible, one-stop entry point for languages bindings
+ * and protocol bindings.
+ *
+ * FIXME: Document this function further, in particular the possible
+ * values for rcode in the callback. In short, we map ACK_COMPLETE to
+ * RCODE_COMPLETE, internal errors set errno and set rcode to
+ * RCODE_SEND_ERROR (which is out of range for standard ieee1394
+ * rcodes). All other rcodes are forwarded unchanged. For all
+ * errors, payload is NULL, length is 0.
+ *
+ * Can not expect the callback to be called before the function
+ * returns, though this does happen in some cases (ACK_COMPLETE and
+ * errors).
+ *
+ * The payload is only used for write requests and must not be freed
+ * until the callback has been called.
+ *
+ * @param card the card from which to send the request
+ * @param tcode the tcode for this transaction. Do not use
+ * TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP
+ * etc. to specify tcode and ext_tcode.
+ * @param node_id the node_id of the destination node
+ * @param generation the generation for which node_id is valid
+ * @param speed the speed to use for sending the request
+ * @param offset the 48 bit offset on the destination node
+ * @param payload the data payload for the request subaction
+ * @param length the length in bytes of the data to read
+ * @param callback function to be called when the transaction is completed
+ * @param callback_data pointer to arbitrary data, which will be
+ * passed to the callback
+ */
+void
+fw_send_request(struct fw_card *card, struct fw_transaction *t,
+ int tcode, int node_id, int generation, int speed,
+ unsigned long long offset,
+ u32 * payload, size_t length,
+ fw_transaction_callback_t callback, void *callback_data)
+{
+ unsigned long flags;
+ int tlabel;
+
+ /* Allocate tlabel from the bitmap and put the transaction on
+ * the list while holding the card spinlock. */
+
+ spin_lock_irqsave(&card->lock, flags);
+
+ tlabel = card->current_tlabel;
+ if (card->tlabel_mask & (1 << tlabel)) {
+ spin_unlock_irqrestore(&card->lock, flags);
+ callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
+ return;
+ }
+
+ card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
+ card->tlabel_mask |= (1 << tlabel);
+
+ list_add_tail(&t->link, &card->transaction_list);
+
+ spin_unlock_irqrestore(&card->lock, flags);
+
+ /* Initialize rest of transaction, fill out packet and send it. */
+ t->node_id = node_id;
+ t->tlabel = tlabel;
+ t->callback = callback;
+ t->callback_data = callback_data;
+
+ fw_fill_packet(&t->packet, tcode, t->tlabel,
+ node_id, generation, speed, offset, payload, length);
+ t->packet.callback = transmit_complete_callback;
+
+ card->driver->send_request(card, &t->packet);
+}
+
+EXPORT_SYMBOL(fw_send_request);
+
+static void
+transmit_phy_packet_callback(struct fw_packet *packet,
+ struct fw_card *card, int status)
+{
+ kfree(packet);
+}
+
+static void send_phy_packet(struct fw_card *card, u32 data, int generation)
+{
+ struct fw_packet *packet;
+
+ packet = kzalloc(sizeof *packet, GFP_ATOMIC);
+ if (packet == NULL)
+ return;
+
+ packet->header[0] = data;
+ packet->header[1] = ~data;
+ packet->header_length = 8;
+ packet->payload_length = 0;
+ packet->speed = SCODE_100;
+ packet->generation = generation;
+ packet->callback = transmit_phy_packet_callback;
+
+ card->driver->send_request(card, packet);
+}
+
+void fw_send_force_root(struct fw_card *card, int node_id, int generation)
+{
+ struct fw_phy_packet p;
+
+ p.quadlet = 0;
+ p.phy_config.identifier = PHY_PACKET_CONFIG;
+ p.phy_config.root_id = node_id;
+ p.phy_config.r = 1;
+ send_phy_packet(card, p.quadlet, generation);
+}
+
+void flush_transactions(struct fw_card *card, u32 timestamp)
+{
+ struct fw_transaction *t, *next;
+ LIST_HEAD(list);
+ unsigned long flags;
+
+ /* To avoid serving the transaction complete callback under
+ * the card lock, we first grab the card lock and filter out
+ * expired transactions into a local list. Then we just
+ * iterate through the local list and close those
+ * transactions. */
+ spin_lock_irqsave(&card->lock, flags);
+ list_for_each_entry_safe(t, next, &card->transaction_list, link) {
+ if (t->timestamp + 800 < timestamp ||
+ (timestamp < t->timestamp &&
+ t->timestamp + 800 < timestamp + 0x10000)) {
+ list_move_tail(&t->link, &list);
+ }
+ }
+ spin_unlock_irqrestore(&card->lock, flags);
+
+ list_for_each_entry_safe(t, next, &list, link)
+ close_transaction(t, card, RCODE_CANCELLED, NULL, 0);
+}
+
+static struct fw_address_handler *lookup_overlapping_address_handler(struct
+ list_head
+ *list,
+ unsigned
+ long long
+ offset,
+ size_t
+ length)
+{
+ struct fw_address_handler *handler;
+
+ list_for_each_entry(handler, list, link) {
+ if (handler->offset < offset + length &&
+ offset < handler->offset + handler->length)
+ return handler;
+ }
+
+ return NULL;
+}
+
+static struct fw_address_handler *lookup_enclosing_address_handler(struct
+ list_head
+ *list,
+ unsigned long
+ long offset,
+ size_t
+ length)
+{
+ struct fw_address_handler *handler;
+
+ list_for_each_entry(handler, list, link) {
+ if (handler->offset <= offset &&
+ offset + length <= handler->offset + handler->length)
+ return handler;
+ }
+
+ return NULL;
+}
+
+static DEFINE_SPINLOCK(address_handler_lock);
+static LIST_HEAD(address_handler_list);
+
+struct fw_address_region fw_low_memory_region =
+ { 0x000000000000ull, 0x000100000000ull };
+struct fw_address_region fw_high_memory_region =
+ { 0x000100000000ull, 0xffffe0000000ull };
+struct fw_address_region fw_private_region =
+ { 0xffffe0000000ull, 0xfffff0000000ull };
+struct fw_address_region fw_csr_region =
+ { 0xfffff0000000ULL, 0xfffff0000800ull };
+struct fw_address_region fw_unit_space_region =
+ { 0xfffff0000900ull, 0x1000000000000ull };
+
+EXPORT_SYMBOL(fw_low_memory_region);
+EXPORT_SYMBOL(fw_high_memory_region);
+EXPORT_SYMBOL(fw_private_region);
+EXPORT_SYMBOL(fw_csr_region);
+EXPORT_SYMBOL(fw_unit_space_region);
+
+/**
+ * Allocate a range of addresses in the node space of the OHCI
+ * controller. When a request is received that falls within the
+ * specified address range, the specified callback is invoked. The
+ * parameters passed to the callback give the details of the
+ * particular request
+ */
+
+int
+fw_core_add_address_handler(struct fw_address_handler *handler,
+ struct fw_address_region *region)
+{
+ struct fw_address_handler *other;
+ unsigned long flags;
+ int ret = -EBUSY;
+
+ spin_lock_irqsave(&address_handler_lock, flags);
+
+ handler->offset = region->start;
+ while (handler->offset + handler->length <= region->end) {
+ other =
+ lookup_overlapping_address_handler(&address_handler_list,
+ handler->offset,
+ handler->length);
+ if (other != NULL) {
+ handler->offset += other->length;
+ } else {
+ list_add_tail(&handler->link, &address_handler_list);
+ ret = 0;
+ break;
+ }
+ }
+
+ spin_unlock_irqrestore(&address_handler_lock, flags);
+
+ return ret;
+}
+
+EXPORT_SYMBOL(fw_core_add_address_handler);
+
+/**
+ * Deallocate a range of addresses allocated with fw_allocate. This
+ * will call the associated callback one last time with a the special
+ * tcode TCODE_DEALLOCATE, to let the client destroy the registered
+ * callback data. For convenience, the callback parameters offset and
+ * length are set to the start and the length respectively for the
+ * deallocated region, payload is set to NULL.
+ */
+
+void fw_core_remove_address_handler(struct fw_address_handler *handler)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&address_handler_lock, flags);
+ list_del(&handler->link);
+ spin_unlock_irqrestore(&address_handler_lock, flags);
+}
+
+EXPORT_SYMBOL(fw_core_remove_address_handler);
+
+struct fw_request {
+ struct fw_packet response;
+ int ack;
+ u32 length;
+ u32 data[0];
+};
+
+static void
+free_response_callback(struct fw_packet *packet,
+ struct fw_card *card, int status)
+{
+ struct fw_request *request;
+
+ request = container_of(packet, struct fw_request, response);
+ kfree(request);
+}
+
+static void
+fw_fill_response(struct fw_packet *response,
+ struct fw_packet_header *request, u32 * data, size_t length)
+{
+ struct fw_packet_header *header;
+
+ header = (struct fw_packet_header *)response->header;
+ header->response.priority = 0;
+ header->response.rt = RETRY_1;
+ header->response.tlabel = request->request.tlabel;
+ header->response.destination = request->request.source;
+ header->response.source = request->request.destination;
+ header->response.reserved0 = 0;
+ header->response.reserved1 = 0;
+
+ switch (request->request.tcode) {
+ case TCODE_WRITE_QUADLET_REQUEST:
+ case TCODE_WRITE_BLOCK_REQUEST:
+ header->write_response.tcode = TCODE_WRITE_RESPONSE;
+ response->header_length = sizeof header->write_response;
+ response->payload_length = 0;
+ break;
+
+ case TCODE_READ_QUADLET_REQUEST:
+ header->read_quadlet_response.tcode =
+ TCODE_READ_QUADLET_RESPONSE;
+ header->read_quadlet_response.data = 0;
+ response->header_length = sizeof header->read_quadlet_response;
+ response->payload_length = 0;
+ break;
+
+ case TCODE_READ_BLOCK_REQUEST:
+ case TCODE_LOCK_REQUEST:
+ header->block_response.tcode = request->request.tcode + 2;
+ header->block_response.data_length = length;
+ header->block_response.extended_tcode =
+ request->block_request.extended_tcode;
+ response->header_length = sizeof header->block_response;
+ response->payload = data;
+ response->payload_length = length;
+ break;
+
+ default:
+ BUG();
+ return;
+ }
+}
+
+static struct fw_request *allocate_request(struct fw_packet_header *header,
+ int ack, int speed, int timestamp,
+ int generation)
+{
+ struct fw_request *request;
+ u32 *data, length;
+
+ switch (header->request.tcode) {
+ case TCODE_WRITE_QUADLET_REQUEST:
+ data = &header->write_quadlet_request.data;
+ length = 4;
+ break;
+
+ case TCODE_WRITE_BLOCK_REQUEST:
+ data = header->write_block_request.data;
+ length = header->write_block_request.data_length;
+ break;
+
+ case TCODE_READ_QUADLET_REQUEST:
+ data = NULL;
+ length = 4;
+ break;
+
+ case TCODE_READ_BLOCK_REQUEST:
+ data = NULL;
+ length = header->read_block_request.data_length;
+ break;
+
+ case TCODE_LOCK_REQUEST:
+ data = header->lock_response.data;
+ length = header->lock_request.data_length;
+ break;
+
+ default:
+ BUG();
+ return NULL;
+ }
+
+ request = kmalloc(sizeof *request + length, GFP_ATOMIC);
+ if (request == NULL)
+ return NULL;
+
+ request->response.speed = speed;
+ request->response.timestamp = timestamp;
+ request->response.generation = generation;
+ request->response.callback = free_response_callback;
+ request->ack = ack;
+ request->length = length;
+ if (data)
+ memcpy(request->data, data, length);
+
+ fw_fill_response(&request->response, header, request->data, length);
+
+ return request;
+}
+
+void
+fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
+{
+ struct fw_packet_header *header;
+
+ /* 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)
+ return;
+
+ header = (struct fw_packet_header *)request->response.header;
+
+ header->response.rcode = rcode;
+ if (rcode != RCODE_COMPLETE)
+ request->response.payload_length = 0;
+ else if (header->response.tcode == TCODE_READ_QUADLET_RESPONSE)
+ header->read_quadlet_response.data = request->data[0];
+
+ card->driver->send_response(card, &request->response);
+}
+
+EXPORT_SYMBOL(fw_send_response);
+
+void
+fw_core_handle_request(struct fw_card *card,
+ int speed, int ack, int timestamp,
+ int generation, u32 length, u32 * payload)
+{
+ struct fw_address_handler *handler;
+ struct fw_packet_header *header;
+ struct fw_request *request;
+ unsigned long long offset;
+ unsigned long flags;
+ int t;
+
+ if (length > 2048) {
+ /* FIXME: send error response. */
+ return;
+ }
+
+ header = (struct fw_packet_header *)payload;
+
+ if (ack != ACK_PENDING && ack != ACK_COMPLETE)
+ return;
+
+ t = (timestamp & 0x1fff) + 4000;
+ if (t >= 8000)
+ t = (timestamp & ~0x1fff) + 0x2000 + t - 8000;
+ else
+ t = (timestamp & ~0x1fff) + t;
+
+ request = allocate_request(header, ack, speed, t, generation);
+ if (request == NULL) {
+ /* FIXME: send statically allocated busy packet. */
+ return;
+ }
+
+ offset = ((unsigned long long)header->request.offset_high << 32) |
+ header->request.offset_low;
+
+ spin_lock_irqsave(&address_handler_lock, flags);
+ handler = lookup_enclosing_address_handler(&address_handler_list,
+ offset, request->length);
+ spin_unlock_irqrestore(&address_handler_lock, flags);
+
+ /* FIXME: lookup the fw_node corresponding to the sender of
+ * this request and pass that to the address handler instead
+ * of the node ID. We may also want to move the address
+ * allocations to fw_node so we only do this callback if the
+ * upper layers registered it for this node. */
+
+ if (handler == NULL)
+ fw_send_response(card, request, RCODE_ADDRESS_ERROR);
+ else
+ handler->address_callback(card, request,
+ header->request.tcode,
+ header->request.destination,
+ header->request.source,
+ generation, speed, offset,
+ request->data, request->length,
+ handler->callback_data);
+}
+
+EXPORT_SYMBOL(fw_core_handle_request);
+
+void
+fw_core_handle_response(struct fw_card *card,
+ int speed, int ack, int timestamp,
+ u32 length, u32 * response)
+{
+ struct fw_transaction *t;
+ struct fw_packet_header *packet;
+ u32 *data;
+ size_t data_length;
+
+ packet = (struct fw_packet_header *)response;
+ t = lookup_transaction(card,
+ packet->response.source,
+ packet->response.tlabel);
+ if (t == NULL) {
+ fw_notify("Unsolicited response\n");
+ return;
+ }
+
+ /* FIXME: sanity check packet, is length correct, does tcodes
+ * and addresses match. */
+
+ switch (packet->response.tcode) {
+ case TCODE_READ_QUADLET_RESPONSE:
+ data = (u32 *) & packet->read_quadlet_response.data;
+ data_length = 4;
+ break;
+
+ case TCODE_WRITE_RESPONSE:
+ data = NULL;
+ data_length = 0;
+ break;
+
+ case TCODE_READ_BLOCK_RESPONSE:
+ case TCODE_LOCK_RESPONSE:
+ data = packet->block_response.data;
+ data_length = packet->block_response.data_length;
+ break;
+
+ default:
+ /* Should never happen, this is just to shut up gcc. */
+ data = NULL;
+ data_length = 0;
+ break;
+ }
+
+ close_transaction(t, card, packet->response.rcode, data, data_length);
+}
+
+EXPORT_SYMBOL(fw_core_handle_response);
+
+MODULE_AUTHOR("Kristian Høgsberg <[email protected]>");
+MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
+MODULE_LICENSE("GPL");
+
+static u32 vendor_textual_descriptor_data[] = {
+ /* textual descriptor leaf () */
+ 0x00080000,
+ 0x00000000,
+ 0x00000000,
+ 0x4c696e75, /* L i n u */
+ 0x78204669, /* x F i */
+ 0x72657769, /* r e w i */
+ 0x72652028, /* r e ( */
+ 0x4a554a55, /* J U J U */
+ 0x29000000, /* ) */
+};
+
+static struct fw_descriptor vendor_textual_descriptor = {
+ .length = ARRAY_SIZE(vendor_textual_descriptor_data),
+ .key = 0x81000000,
+ .data = vendor_textual_descriptor_data
+};
+
+static int __init fw_core_init(void)
+{
+ int retval;
+
+ retval = bus_register(&fw_bus_type);
+ if (retval < 0) {
+ fw_error("failed to register fw bus");
+ return retval;
+ }
+
+ /* Add the vendor textual descriptor. */
+ fw_core_add_descriptor(&vendor_textual_descriptor);
+
+ fw_notify("Loaded fw-core driver.\n");
+
+ return 0;
+}
+
+static void __exit fw_core_cleanup(void)
+{
+ bus_unregister(&fw_bus_type);
+
+ fw_notify("Unloaded fw-core driver.\n");
+}
+
+module_init(fw_core_init);
+module_exit(fw_core_cleanup);
diff --git a/drivers/fw/fw-transaction.h b/drivers/fw/fw-transaction.h
new file mode 100644
index 0000000..17404e7
--- /dev/null
+++ b/drivers/fw/fw-transaction.h
@@ -0,0 +1,415 @@
+/* -*- c-basic-offset: 8 -*-
+ *
+ * fw-transaction.h - Header for IEEE1394 transaction logic
+ *
+ * Copyright (C) 2003 Kristian Høgsberg <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __fw_core_h
+#define __fw_core_h
+
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/list.h>
+#include <linux/fs.h>
+
+#define TCODE_WRITE_QUADLET_REQUEST 0
+#define TCODE_WRITE_BLOCK_REQUEST 1
+#define TCODE_WRITE_RESPONSE 2
+#define TCODE_READ_QUADLET_REQUEST 4
+#define TCODE_READ_BLOCK_REQUEST 5
+#define TCODE_READ_QUADLET_RESPONSE 6
+#define TCODE_READ_BLOCK_RESPONSE 7
+#define TCODE_CYCLE_START 8
+#define TCODE_LOCK_REQUEST 9
+#define TCODE_STREAM_DATA 10
+#define TCODE_LOCK_RESPONSE 11
+
+/* Juju specific tcodes */
+#define TCODE_DEALLOCATE 0x10
+#define TCODE_LOCK_MASK_SWAP 0x11
+#define TCODE_LOCK_COMPARE_SWAP 0x12
+#define TCODE_LOCK_FETCH_ADD 0x13
+#define TCODE_LOCK_LITTLE_ADD 0x14
+#define TCODE_LOCK_BOUNDED_ADD 0x15
+#define TCODE_LOCK_WRAP_ADD 0x16
+#define TCODE_LOCK_VENDOR_SPECIFIC 0x17
+
+#define TCODE_IS_WRITE_REQUEST(tcode) ( ((tcode) & ~1) == 0 )
+#define TCODE_IS_READ_REQUEST(tcode) ( ((tcode) & ~1) == 4 )
+
+#define SCODE_100 0x0
+#define SCODE_200 0x1
+#define SCODE_400 0x2
+#define SCODE_BETA 0x3
+
+#define EXTCODE_MASK_SWAP 0x1
+#define EXTCODE_COMPARE_SWAP 0x2
+#define EXTCODE_FETCH_ADD 0x3
+#define EXTCODE_LITTLE_ADD 0x4
+#define EXTCODE_BOUNDED_ADD 0x5
+#define EXTCODE_WRAP_ADD 0x6
+
+#define ACK_COMPLETE 0x1
+#define ACK_PENDING 0x2
+#define ACK_BUSY_X 0x4
+#define ACK_BUSY_A 0x5
+#define ACK_BUSY_B 0x6
+#define ACK_DATA_ERROR 0xd
+#define ACK_TYPE_ERROR 0xe
+
+#define RCODE_COMPLETE 0x0
+#define RCODE_CONFLICT_ERROR 0x4
+#define RCODE_DATA_ERROR 0x5
+#define RCODE_TYPE_ERROR 0x6
+#define RCODE_ADDRESS_ERROR 0x7
+
+/* Juju specific rcodes */
+#define RCODE_SEND_ERROR 0x10
+#define RCODE_CANCELLED 0x11
+#define RCODE_BUSY 0x12
+
+#define RETRY_1 0x00
+#define RETRY_X 0x01
+#define RETRY_A 0x02
+#define RETRY_B 0x03
+
+#define LOCAL_BUS 0xffc0
+
+#define SELFID_PORT_CHILD 0x3
+#define SELFID_PORT_PARENT 0x2
+#define SELFID_PORT_NCONN 0x1
+#define SELFID_PORT_NONE 0x0
+
+#define PHY_PACKET_CONFIG 0x0
+#define PHY_PACKET_LINK_ON 0x1
+#define PHY_PACKET_SELF_ID 0x2
+
+#define fw_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, ## args)
+#define fw_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
+#define fw_debug(s, args...) printk(KERN_DEBUG KBUILD_MODNAME ": " s, ## args)
+
+static inline void
+fw_memcpy_from_be32(void *_dst, void *_src, size_t size)
+{
+ u32 *dst = _dst;
+ u32 *src = _src;
+ int i;
+
+ for (i = 0; i < size / 4; i++)
+ dst[i] = cpu_to_be32(src[i]);
+}
+
+static inline void
+fw_memcpy_to_be32(void *_dst, void *_src, size_t size)
+{
+ fw_memcpy_from_be32(_dst, _src, size);
+}
+
+struct fw_card;
+struct fw_packet;
+struct fw_node;
+struct fw_request;
+
+struct fw_descriptor {
+ struct list_head link;
+ size_t length;
+ u32 key;
+ u32 *data;
+};
+
+int fw_core_add_descriptor (struct fw_descriptor *desc);
+void fw_core_remove_descriptor (struct fw_descriptor *desc);
+
+typedef void (*fw_packet_callback_t) (struct fw_packet *packet,
+ struct fw_card *card, int status);
+
+typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
+ u32 *payload,
+ size_t length,
+ void *callback_data);
+
+typedef void (*fw_address_callback_t)(struct fw_card *card,
+ struct fw_request *request,
+ int tcode, int destination, int source,
+ int generation, int speed,
+ unsigned long long offset,
+ u32 *payload, size_t length,
+ void *callback_data);
+
+typedef void (*fw_bus_reset_callback_t)(struct fw_card *handle,
+ int node_id, int generation,
+ u32 *self_ids,
+ int self_id_count,
+ void *callback_data);
+
+struct fw_packet {
+ int speed;
+ int generation;
+ u32 header[4];
+ size_t header_length;
+ u32 *payload;
+ size_t payload_length;
+ u32 timestamp;
+
+ dma_addr_t payload_bus;
+
+ /* This callback is called when the packet transmission has
+ * completed; for successful transmission, the status code is
+ * the ack received from the destination, otherwise it's a
+ * negative errno: ENOMEM, ESTALE, ETIMEDOUT, ENODEV, EIO.
+ * The callback can be called from tasklet context and thus
+ * must never block.
+ */
+ fw_packet_callback_t callback;
+ struct list_head link;
+};
+
+struct fw_transaction {
+ int node_id; /* The generation is implied; it is always the current. */
+ int tlabel;
+ int timestamp;
+ struct list_head link;
+
+ struct fw_packet packet;
+
+ /* The data passed to the callback is valid only during the
+ * callback. */
+ fw_transaction_callback_t callback;
+ void *callback_data;
+};
+
+extern inline struct fw_packet *
+fw_packet(struct list_head *l)
+{
+ return list_entry (l, struct fw_packet, link);
+}
+
+struct fw_address_handler {
+ u64 offset;
+ size_t length;
+ fw_address_callback_t address_callback;
+ void *callback_data;
+ struct list_head link;
+};
+
+
+struct fw_address_region {
+ u64 start;
+ u64 end;
+};
+
+extern struct fw_address_region fw_low_memory_region;
+extern struct fw_address_region fw_high_memory_region;
+extern struct fw_address_region fw_private_region;
+extern struct fw_address_region fw_csr_region;
+extern struct fw_address_region fw_unit_space_region;
+
+int fw_core_add_address_handler(struct fw_address_handler *handler,
+ struct fw_address_region *region);
+void fw_core_remove_address_handler(struct fw_address_handler *handler);
+void fw_send_response(struct fw_card *card,
+ struct fw_request *request, int rcode);
+
+extern struct bus_type fw_bus_type;
+
+struct fw_card {
+ struct fw_card_driver *driver;
+ struct device *device;
+
+ int node_id;
+ int generation;
+ /* This is the generation used for timestamping incoming requests. */
+ int request_generation;
+ int current_tlabel, tlabel_mask;
+ struct list_head transaction_list;
+ unsigned long long guid;
+ int max_receive;
+ int link_speed;
+ int config_rom_generation;
+
+ /* We need to store up to 4 self ID for a maximum of 63 devices. */
+ int self_id_count;
+ u32 self_ids[252];
+
+ spinlock_t lock; /* Take this lock when handling the lists in
+ * this struct. */
+ struct fw_node *local_node;
+ struct fw_node *root_node;
+ struct fw_node *irm_node;
+ int color;
+
+ int index;
+
+ struct device card_device;
+
+ struct list_head link;
+
+ /* Work struct for IRM duties. */
+ struct work_struct work;
+ int irm_retries;
+};
+
+struct fw_card *fw_card_get(struct fw_card *card);
+void fw_card_put(struct fw_card *card);
+
+/* The iso packet format allows for an immediate header/payload part
+ * stored in 'header' immediately after the packet info plus an
+ * indirect payload part that is pointer to by the 'payload' field.
+ * Applications can use one or the other or both to implement simple
+ * low-bandwidth streaming (e.g. audio) or more advanced
+ * scatter-gather streaming (e.g. assembling video frame automatically). */
+
+struct fw_iso_packet {
+ u16 payload_length; /* Length of indirect payload. */
+ u32 interrupt : 1; /* Generate interrupt on this packet */
+ u32 skip : 1; /* Set to not send packet at all. */
+ u32 tag : 2;
+ u32 sy : 4;
+ u32 header_length : 8; /* Length of immediate header. */
+ u32 header[0];
+};
+
+#define FW_ISO_CONTEXT_TRANSMIT 0
+#define FW_ISO_CONTEXT_RECEIVE 1
+
+struct fw_iso_context;
+
+typedef void (*fw_iso_callback_t) (struct fw_iso_context *context,
+ int status, u32 cycle, void *data);
+
+struct fw_iso_context {
+ struct fw_card *card;
+ int type;
+ int channel;
+ int speed;
+ fw_iso_callback_t callback;
+ void *callback_data;
+
+ void *buffer;
+ size_t buffer_size;
+ dma_addr_t *pages;
+ int page_count;
+};
+
+struct fw_iso_context *
+fw_iso_context_create(struct fw_card *card, int type,
+ size_t buffer_size,
+ fw_iso_callback_t callback,
+ void *callback_data);
+
+void
+fw_iso_context_destroy(struct fw_iso_context *ctx);
+
+void
+fw_iso_context_start(struct fw_iso_context *ctx,
+ int channel, int speed, int cycle);
+
+int
+fw_iso_context_queue(struct fw_iso_context *ctx,
+ struct fw_iso_packet *packet, void *payload);
+
+int
+fw_iso_context_send(struct fw_iso_context *ctx,
+ int channel, int speed, int cycle);
+
+struct fw_card_driver {
+ const char *name;
+
+ /* Enable the given card with the given initial config rom.
+ * This function is expected to activate the card, and either
+ * enable the PHY or set the link_on bit and initiate a bus
+ * reset. */
+ int (*enable) (struct fw_card *card, u32 *config_rom, size_t length);
+
+ int (*update_phy_reg) (struct fw_card *card, int address,
+ int clear_bits, int set_bits);
+
+ /* Update the config rom for an enabled card. This function
+ * should change the config rom that is presented on the bus
+ * an initiate a bus reset. */
+ int (*set_config_rom) (struct fw_card *card,
+ u32 *config_rom, size_t length);
+
+ void (*send_request) (struct fw_card *card, struct fw_packet *packet);
+ void (*send_response) (struct fw_card *card, struct fw_packet *packet);
+
+ /* Allow the specified node ID to do direct DMA out and in of
+ * host memory. The card will disable this for all node when
+ * a bus reset happens, so driver need to reenable this after
+ * bus reset. Returns 0 on success, -ENODEV if the card
+ * doesn't support this, -ESTALE if the generation doesn't
+ * match. */
+ int (*enable_phys_dma) (struct fw_card *card,
+ int node_id, int generation);
+
+ struct fw_iso_context *
+ (*allocate_iso_context)(struct fw_card *card, int type);
+ void (*free_iso_context)(struct fw_iso_context *ctx);
+
+ int (*send_iso)(struct fw_iso_context *ctx, s32 cycle);
+
+ int (*queue_iso)(struct fw_iso_context *ctx,
+ struct fw_iso_packet *packet, void *payload);
+};
+
+int
+fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
+
+void
+fw_send_request(struct fw_card *card, struct fw_transaction *t,
+ int tcode, int node_id, int generation, int speed,
+ unsigned long long offset,
+ u32 *payload, size_t length,
+ fw_transaction_callback_t callback, void *callback_data);
+
+void
+fw_send_force_root(struct fw_card *card, int node_id, int generation);
+
+/* Called by the topology code to inform the device code of node
+ * activity; found, lost, or updated nodes */
+void
+fw_node_event(struct fw_card *card, struct fw_node *node, int event);
+
+/* API used by card level drivers */
+
+/* Do we need phy speed here also? If we add more args, maybe we
+ should go back to struct fw_card_info. */
+int
+fw_core_add_card (struct fw_card *card, struct fw_card_driver *driver,
+ struct device *device,
+ u32 max_receive, u32 link_speed, u64 guid);
+
+void
+fw_core_remove_card(struct fw_card *card);
+
+void
+fw_core_handle_bus_reset(struct fw_card *card,
+ int node_id, int generation,
+ int self_id_count, u32 *self_ids);
+void
+fw_core_handle_request(struct fw_card *card,
+ int speed, int ack, int timestamp,
+ int generation,
+ u32 length, u32 *payload);
+void
+fw_core_handle_response(struct fw_card *card,
+ int speed, int ack, int timestamp,
+ u32 length, u32 *payload);
+
+
+#endif /* __fw_core_h */