Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 1 | /* -*- 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øgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame^] | 29 | #include "fw-device.h" |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 30 | |
| 31 | static int |
| 32 | setup_iso_buffer(struct fw_iso_context *ctx, size_t size, |
| 33 | enum dma_data_direction direction) |
| 34 | { |
| 35 | struct page *page; |
| 36 | int i; |
| 37 | 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) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | ctx->page_count = ctx->buffer_size >> PAGE_SHIFT; |
| 48 | ctx->pages = |
| 49 | kzalloc(ctx->page_count * sizeof(ctx->pages[0]), GFP_KERNEL); |
| 50 | if (ctx->pages == NULL) { |
| 51 | vfree(ctx->buffer); |
| 52 | return -ENOMEM; |
| 53 | } |
| 54 | |
| 55 | p = ctx->buffer; |
| 56 | for (i = 0; i < ctx->page_count; i++, p += PAGE_SIZE) { |
| 57 | page = vmalloc_to_page(p); |
| 58 | ctx->pages[i] = dma_map_page(ctx->card->device, |
| 59 | page, 0, PAGE_SIZE, direction); |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static void destroy_iso_buffer(struct fw_iso_context *ctx) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < ctx->page_count; i++) |
| 70 | dma_unmap_page(ctx->card->device, ctx->pages[i], |
| 71 | PAGE_SIZE, DMA_TO_DEVICE); |
| 72 | |
| 73 | kfree(ctx->pages); |
| 74 | vfree(ctx->buffer); |
| 75 | } |
| 76 | |
| 77 | struct fw_iso_context *fw_iso_context_create(struct fw_card *card, int type, |
| 78 | size_t buffer_size, |
| 79 | fw_iso_callback_t callback, |
| 80 | void *callback_data) |
| 81 | { |
| 82 | struct fw_iso_context *ctx; |
| 83 | int retval; |
| 84 | |
| 85 | ctx = card->driver->allocate_iso_context(card, type); |
| 86 | if (IS_ERR(ctx)) |
| 87 | return ctx; |
| 88 | |
| 89 | ctx->card = card; |
| 90 | ctx->type = type; |
| 91 | ctx->callback = callback; |
| 92 | ctx->callback_data = callback_data; |
| 93 | |
| 94 | retval = setup_iso_buffer(ctx, buffer_size, DMA_TO_DEVICE); |
| 95 | if (retval < 0) { |
| 96 | card->driver->free_iso_context(ctx); |
| 97 | return ERR_PTR(retval); |
| 98 | } |
| 99 | |
| 100 | return ctx; |
| 101 | } |
| 102 | |
| 103 | EXPORT_SYMBOL(fw_iso_context_create); |
| 104 | |
| 105 | void fw_iso_context_destroy(struct fw_iso_context *ctx) |
| 106 | { |
| 107 | struct fw_card *card = ctx->card; |
| 108 | |
| 109 | destroy_iso_buffer(ctx); |
| 110 | |
| 111 | card->driver->free_iso_context(ctx); |
| 112 | } |
| 113 | |
| 114 | EXPORT_SYMBOL(fw_iso_context_destroy); |
| 115 | |
| 116 | int |
| 117 | fw_iso_context_send(struct fw_iso_context *ctx, |
| 118 | int channel, int speed, int cycle) |
| 119 | { |
| 120 | ctx->channel = channel; |
| 121 | ctx->speed = speed; |
| 122 | |
| 123 | return ctx->card->driver->send_iso(ctx, cycle); |
| 124 | } |
| 125 | |
| 126 | EXPORT_SYMBOL(fw_iso_context_send); |
| 127 | |
| 128 | int |
| 129 | fw_iso_context_queue(struct fw_iso_context *ctx, |
| 130 | struct fw_iso_packet *packet, void *payload) |
| 131 | { |
| 132 | struct fw_card *card = ctx->card; |
| 133 | |
| 134 | return card->driver->queue_iso(ctx, packet, payload); |
| 135 | } |
| 136 | |
| 137 | EXPORT_SYMBOL(fw_iso_context_queue); |