blob: 6481e3df2c93197c362660c1e812bab028477345 [file] [log] [blame]
Kristian Høgsberg3038e352006-12-19 19:58:27 -05001/* -*- c-basic-offset: 8 -*-
2 *
3 * fw-iso.c - Isochronous IO
4 * 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
31static int
32setup_iso_buffer(struct fw_iso_context *ctx, size_t size,
33 enum dma_data_direction direction)
34{
35 struct page *page;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050036 int i, j;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050037 void *p;
38
39 ctx->buffer_size = PAGE_ALIGN(size);
40 if (size == 0)
41 return 0;
42
43 ctx->buffer = vmalloc_32_user(ctx->buffer_size);
44 if (ctx->buffer == NULL)
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050045 goto fail_buffer_alloc;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050046
47 ctx->page_count = ctx->buffer_size >> PAGE_SHIFT;
48 ctx->pages =
49 kzalloc(ctx->page_count * sizeof(ctx->pages[0]), GFP_KERNEL);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050050 if (ctx->pages == NULL)
51 goto fail_pages_alloc;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050052
53 p = ctx->buffer;
54 for (i = 0; i < ctx->page_count; i++, p += PAGE_SIZE) {
55 page = vmalloc_to_page(p);
56 ctx->pages[i] = dma_map_page(ctx->card->device,
57 page, 0, PAGE_SIZE, direction);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050058 if (dma_mapping_error(ctx->pages[i]))
59 goto fail_mapping;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050060 }
61
62 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050063
64 fail_mapping:
65 for (j = 0; j < i; j++)
66 dma_unmap_page(ctx->card->device, ctx->pages[j],
67 PAGE_SIZE, DMA_TO_DEVICE);
68 fail_pages_alloc:
69 vfree(ctx->buffer);
70 fail_buffer_alloc:
71 return -ENOMEM;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050072}
73
74static void destroy_iso_buffer(struct fw_iso_context *ctx)
75{
76 int i;
77
78 for (i = 0; i < ctx->page_count; i++)
79 dma_unmap_page(ctx->card->device, ctx->pages[i],
80 PAGE_SIZE, DMA_TO_DEVICE);
81
82 kfree(ctx->pages);
83 vfree(ctx->buffer);
84}
85
86struct fw_iso_context *fw_iso_context_create(struct fw_card *card, int type,
87 size_t buffer_size,
88 fw_iso_callback_t callback,
89 void *callback_data)
90{
91 struct fw_iso_context *ctx;
92 int retval;
93
94 ctx = card->driver->allocate_iso_context(card, type);
95 if (IS_ERR(ctx))
96 return ctx;
97
98 ctx->card = card;
99 ctx->type = type;
100 ctx->callback = callback;
101 ctx->callback_data = callback_data;
102
103 retval = setup_iso_buffer(ctx, buffer_size, DMA_TO_DEVICE);
104 if (retval < 0) {
105 card->driver->free_iso_context(ctx);
106 return ERR_PTR(retval);
107 }
108
109 return ctx;
110}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500111EXPORT_SYMBOL(fw_iso_context_create);
112
113void fw_iso_context_destroy(struct fw_iso_context *ctx)
114{
115 struct fw_card *card = ctx->card;
116
117 destroy_iso_buffer(ctx);
118
119 card->driver->free_iso_context(ctx);
120}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500121EXPORT_SYMBOL(fw_iso_context_destroy);
122
123int
124fw_iso_context_send(struct fw_iso_context *ctx,
125 int channel, int speed, int cycle)
126{
127 ctx->channel = channel;
128 ctx->speed = speed;
129
130 return ctx->card->driver->send_iso(ctx, cycle);
131}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500132EXPORT_SYMBOL(fw_iso_context_send);
133
134int
135fw_iso_context_queue(struct fw_iso_context *ctx,
136 struct fw_iso_packet *packet, void *payload)
137{
138 struct fw_card *card = ctx->card;
139
140 return card->driver->queue_iso(ctx, packet, payload);
141}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500142EXPORT_SYMBOL(fw_iso_context_queue);