Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Isochronous IO functionality |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 3 | * |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 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 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 31 | int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card, |
| 32 | int page_count, enum dma_data_direction direction) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 33 | { |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 34 | int i, j; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 35 | dma_addr_t address; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 36 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 37 | buffer->page_count = page_count; |
| 38 | buffer->direction = direction; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 39 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 40 | buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]), |
| 41 | GFP_KERNEL); |
| 42 | if (buffer->pages == NULL) |
| 43 | goto out; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 44 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 45 | for (i = 0; i < buffer->page_count; i++) { |
Kristian Høgsberg | 68be3fa | 2007-02-16 17:34:48 -0500 | [diff] [blame] | 46 | buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 47 | if (buffer->pages[i] == NULL) |
| 48 | goto out_pages; |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 49 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 50 | address = dma_map_page(card->device, buffer->pages[i], |
| 51 | 0, PAGE_SIZE, direction); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 52 | if (dma_mapping_error(card->device, address)) { |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 53 | __free_page(buffer->pages[i]); |
| 54 | goto out_pages; |
| 55 | } |
| 56 | set_page_private(buffer->pages[i], address); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return 0; |
Kristian Høgsberg | 82eff9d | 2007-02-06 14:49:40 -0500 | [diff] [blame] | 60 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 61 | out_pages: |
| 62 | for (j = 0; j < i; j++) { |
| 63 | address = page_private(buffer->pages[j]); |
| 64 | dma_unmap_page(card->device, address, |
Kristian Høgsberg | 82eff9d | 2007-02-06 14:49:40 -0500 | [diff] [blame] | 65 | PAGE_SIZE, DMA_TO_DEVICE); |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 66 | __free_page(buffer->pages[j]); |
| 67 | } |
| 68 | kfree(buffer->pages); |
| 69 | out: |
| 70 | buffer->pages = NULL; |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 71 | return -ENOMEM; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 74 | int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma) |
| 75 | { |
| 76 | unsigned long uaddr; |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 77 | int i, ret; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 78 | |
| 79 | uaddr = vma->vm_start; |
| 80 | for (i = 0; i < buffer->page_count; i++) { |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 81 | ret = vm_insert_page(vma, uaddr, buffer->pages[i]); |
| 82 | if (ret) |
| 83 | return ret; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 84 | uaddr += PAGE_SIZE; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, |
| 91 | struct fw_card *card) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 92 | { |
| 93 | int i; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 94 | dma_addr_t address; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 95 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 96 | for (i = 0; i < buffer->page_count; i++) { |
| 97 | address = page_private(buffer->pages[i]); |
| 98 | dma_unmap_page(card->device, address, |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 99 | PAGE_SIZE, DMA_TO_DEVICE); |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 100 | __free_page(buffer->pages[i]); |
| 101 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 102 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 103 | kfree(buffer->pages); |
| 104 | buffer->pages = NULL; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 107 | struct fw_iso_context *fw_iso_context_create(struct fw_card *card, |
| 108 | int type, int channel, int speed, size_t header_size, |
| 109 | fw_iso_callback_t callback, void *callback_data) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 110 | { |
| 111 | struct fw_iso_context *ctx; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 112 | |
Stefan Richter | 4817ed2 | 2008-12-21 16:39:46 +0100 | [diff] [blame^] | 113 | ctx = card->driver->allocate_iso_context(card, |
| 114 | type, channel, header_size); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 115 | if (IS_ERR(ctx)) |
| 116 | return ctx; |
| 117 | |
| 118 | ctx->card = card; |
| 119 | ctx->type = type; |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 120 | ctx->channel = channel; |
| 121 | ctx->speed = speed; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 122 | ctx->header_size = header_size; |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 123 | ctx->callback = callback; |
| 124 | ctx->callback_data = callback_data; |
| 125 | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 126 | return ctx; |
| 127 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 128 | |
| 129 | void fw_iso_context_destroy(struct fw_iso_context *ctx) |
| 130 | { |
| 131 | struct fw_card *card = ctx->card; |
| 132 | |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 133 | card->driver->free_iso_context(ctx); |
| 134 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 135 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 136 | int fw_iso_context_start(struct fw_iso_context *ctx, |
| 137 | int cycle, int sync, int tags) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 138 | { |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 139 | return ctx->card->driver->start_iso(ctx, cycle, sync, tags); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 140 | } |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 141 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 142 | int fw_iso_context_queue(struct fw_iso_context *ctx, |
| 143 | struct fw_iso_packet *packet, |
| 144 | struct fw_iso_buffer *buffer, |
| 145 | unsigned long payload) |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 146 | { |
| 147 | struct fw_card *card = ctx->card; |
| 148 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 149 | return card->driver->queue_iso(ctx, packet, buffer, payload); |
Kristian Høgsberg | 3038e35 | 2006-12-19 19:58:27 -0500 | [diff] [blame] | 150 | } |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 151 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 152 | int fw_iso_context_stop(struct fw_iso_context *ctx) |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 153 | { |
| 154 | return ctx->card->driver->stop_iso(ctx); |
| 155 | } |