fw-iso.c 3.8 KB
Newer Older
1 2
/*
 * Isochronous IO functionality
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
 *
 * 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 <linux/mm.h>

#include "fw-transaction.h"
#include "fw-topology.h"
29
#include "fw-device.h"
30

31 32 33
int
fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
		   int page_count, enum dma_data_direction direction)
34
{
35
	int i, j;
36 37 38 39 40 41 42 43 44 45 46
	dma_addr_t address;

	buffer->page_count = page_count;
	buffer->direction = direction;

	buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
				GFP_KERNEL);
	if (buffer->pages == NULL)
		goto out;

	for (i = 0; i < buffer->page_count; i++) {
47
		buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
48 49
		if (buffer->pages[i] == NULL)
			goto out_pages;
S
Stefan Richter 已提交
50

51 52
		address = dma_map_page(card->device, buffer->pages[i],
				       0, PAGE_SIZE, direction);
53
		if (dma_mapping_error(card->device, address)) {
54 55 56 57
			__free_page(buffer->pages[i]);
			goto out_pages;
		}
		set_page_private(buffer->pages[i], address);
58 59 60
	}

	return 0;
61

62 63 64 65
 out_pages:
	for (j = 0; j < i; j++) {
		address = page_private(buffer->pages[j]);
		dma_unmap_page(card->device, address,
66
			       PAGE_SIZE, DMA_TO_DEVICE);
67 68 69 70 71
		__free_page(buffer->pages[j]);
	}
	kfree(buffer->pages);
 out:
	buffer->pages = NULL;
72
	return -ENOMEM;
73 74 75 76 77
}

int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
{
	unsigned long uaddr;
78
	int i, ret;
79 80 81

	uaddr = vma->vm_start;
	for (i = 0; i < buffer->page_count; i++) {
82 83 84
		ret = vm_insert_page(vma, uaddr, buffer->pages[i]);
		if (ret)
			return ret;
85 86 87 88
		uaddr += PAGE_SIZE;
	}

	return 0;
89 90
}

91 92
void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
			   struct fw_card *card)
93 94
{
	int i;
95
	dma_addr_t address;
96

97 98 99
	for (i = 0; i < buffer->page_count; i++) {
		address = page_private(buffer->pages[i]);
		dma_unmap_page(card->device, address,
100
			       PAGE_SIZE, DMA_TO_DEVICE);
101 102
		__free_page(buffer->pages[i]);
	}
103

104 105
	kfree(buffer->pages);
	buffer->pages = NULL;
106 107
}

108
struct fw_iso_context *
109
fw_iso_context_create(struct fw_card *card, int type,
110
		      int channel, int speed, size_t header_size,
111
		      fw_iso_callback_t callback, void *callback_data)
112 113 114
{
	struct fw_iso_context *ctx;

115
	ctx = card->driver->allocate_iso_context(card, type, header_size);
116 117 118 119 120
	if (IS_ERR(ctx))
		return ctx;

	ctx->card = card;
	ctx->type = type;
121 122
	ctx->channel = channel;
	ctx->speed = speed;
123
	ctx->header_size = header_size;
124 125 126 127 128 129 130 131 132 133 134 135 136 137
	ctx->callback = callback;
	ctx->callback_data = callback_data;

	return ctx;
}

void fw_iso_context_destroy(struct fw_iso_context *ctx)
{
	struct fw_card *card = ctx->card;

	card->driver->free_iso_context(ctx);
}

int
138
fw_iso_context_start(struct fw_iso_context *ctx, int cycle, int sync, int tags)
139
{
140
	return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
141 142 143 144
}

int
fw_iso_context_queue(struct fw_iso_context *ctx,
145 146 147
		     struct fw_iso_packet *packet,
		     struct fw_iso_buffer *buffer,
		     unsigned long payload)
148 149 150
{
	struct fw_card *card = ctx->card;

151
	return card->driver->queue_iso(ctx, packet, buffer, payload);
152
}
153 154 155 156 157 158

int
fw_iso_context_stop(struct fw_iso_context *ctx)
{
	return ctx->card->driver->stop_iso(ctx);
}