blob: bcbe794a3ea5ca3198df3c1593ef696edf0f716c [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Isochronous IO functionality
Kristian Høgsberg3038e352006-12-19 19:58:27 -05003 *
Kristian Høgsberg3038e352006-12-19 19:58:27 -05004 * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/dma-mapping.h>
24#include <linux/vmalloc.h>
25#include <linux/mm.h>
26
27#include "fw-transaction.h"
28#include "fw-topology.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050029#include "fw-device.h"
Kristian Høgsberg3038e352006-12-19 19:58:27 -050030
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050031int
32fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
33 int page_count, enum dma_data_direction direction)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050034{
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050035 int i, j, retval = -ENOMEM;
36 dma_addr_t address;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050037
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050038 buffer->page_count = page_count;
39 buffer->direction = direction;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050040
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050041 buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
42 GFP_KERNEL);
43 if (buffer->pages == NULL)
44 goto out;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050045
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050046 for (i = 0; i < buffer->page_count; i++) {
Kristian Høgsberg68be3fa2007-02-16 17:34:48 -050047 buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050048 if (buffer->pages[i] == NULL)
49 goto out_pages;
Stefan Richter373b2ed2007-03-04 14:45:18 +010050
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050051 address = dma_map_page(card->device, buffer->pages[i],
52 0, PAGE_SIZE, direction);
53 if (dma_mapping_error(address)) {
54 __free_page(buffer->pages[i]);
55 goto out_pages;
56 }
57 set_page_private(buffer->pages[i], address);
Kristian Høgsberg3038e352006-12-19 19:58:27 -050058 }
59
60 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050061
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050062 out_pages:
63 for (j = 0; j < i; j++) {
64 address = page_private(buffer->pages[j]);
65 dma_unmap_page(card->device, address,
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050066 PAGE_SIZE, DMA_TO_DEVICE);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050067 __free_page(buffer->pages[j]);
68 }
69 kfree(buffer->pages);
70 out:
71 buffer->pages = NULL;
72 return retval;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050073}
74
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050075int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
76{
77 unsigned long uaddr;
78 int i, retval;
79
80 uaddr = vma->vm_start;
81 for (i = 0; i < buffer->page_count; i++) {
82 retval = vm_insert_page(vma, uaddr, buffer->pages[i]);
83 if (retval)
84 return retval;
85 uaddr += PAGE_SIZE;
86 }
87
88 return 0;
89}
90
91void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
92 struct fw_card *card)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050093{
94 int i;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050095 dma_addr_t address;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050096
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050097 for (i = 0; i < buffer->page_count; i++) {
98 address = page_private(buffer->pages[i]);
99 dma_unmap_page(card->device, address,
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500100 PAGE_SIZE, DMA_TO_DEVICE);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500101 __free_page(buffer->pages[i]);
102 }
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500103
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500104 kfree(buffer->pages);
105 buffer->pages = NULL;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500106}
107
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500108struct fw_iso_context *
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500109fw_iso_context_create(struct fw_card *card, int type,
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400110 int channel, int speed, size_t header_size,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500111 fw_iso_callback_t callback, void *callback_data)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500112{
113 struct fw_iso_context *ctx;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500114
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400115 ctx = card->driver->allocate_iso_context(card, type, header_size);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500116 if (IS_ERR(ctx))
117 return ctx;
118
119 ctx->card = card;
120 ctx->type = type;
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500121 ctx->channel = channel;
122 ctx->speed = speed;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500123 ctx->header_size = header_size;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500124 ctx->callback = callback;
125 ctx->callback_data = callback_data;
126
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500127 return ctx;
128}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500129
130void fw_iso_context_destroy(struct fw_iso_context *ctx)
131{
132 struct fw_card *card = ctx->card;
133
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500134 card->driver->free_iso_context(ctx);
135}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500136
137int
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400138fw_iso_context_start(struct fw_iso_context *ctx, int cycle, int sync, int tags)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500139{
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400140 return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500141}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500142
143int
144fw_iso_context_queue(struct fw_iso_context *ctx,
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500145 struct fw_iso_packet *packet,
146 struct fw_iso_buffer *buffer,
147 unsigned long payload)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500148{
149 struct fw_card *card = ctx->card;
150
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500151 return card->driver->queue_iso(ctx, packet, buffer, payload);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500152}
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500153
154int
155fw_iso_context_stop(struct fw_iso_context *ctx)
156{
157 return ctx->card->driver->stop_iso(ctx);
158}