Blame view
drivers/firewire/core-iso.c
9.61 KB
c781c06d1 firewire: Clean u... |
1 |
/* |
b1bda4cdc firewire: cdev: a... |
2 3 4 |
* Isochronous I/O functionality: * - Isochronous DMA context management * - Isochronous bus resource management (channels, bandwidth), client side |
3038e353c firewire: Add cor... |
5 |
* |
3038e353c firewire: Add cor... |
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
* 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. */ |
3038e353c firewire: Add cor... |
22 |
#include <linux/dma-mapping.h> |
b1bda4cdc firewire: cdev: a... |
23 |
#include <linux/errno.h> |
77c9a5daa firewire: reorgan... |
24 |
#include <linux/firewire.h> |
b1bda4cdc firewire: cdev: a... |
25 26 |
#include <linux/firewire-constants.h> #include <linux/kernel.h> |
3038e353c firewire: Add cor... |
27 |
#include <linux/mm.h> |
5a0e3ad6a include cleanup: ... |
28 |
#include <linux/slab.h> |
b1bda4cdc firewire: cdev: a... |
29 30 |
#include <linux/spinlock.h> #include <linux/vmalloc.h> |
3038e353c firewire: Add cor... |
31 |
|
e8ca97021 firewire: clean u... |
32 |
#include <asm/byteorder.h> |
77c9a5daa firewire: reorgan... |
33 |
#include "core.h" |
b1bda4cdc firewire: cdev: a... |
34 35 36 37 |
/* * Isochronous DMA context management */ |
3038e353c firewire: Add cor... |
38 |
|
53dca5117 firewire: remove ... |
39 40 |
int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card, int page_count, enum dma_data_direction direction) |
3038e353c firewire: Add cor... |
41 |
{ |
2dbd7d7e2 firewire: standar... |
42 |
int i, j; |
9aad81253 firewire: Split t... |
43 44 45 46 47 48 49 50 51 52 53 |
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++) { |
68be3fa15 firewire: Get zer... |
54 |
buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
9aad81253 firewire: Split t... |
55 56 |
if (buffer->pages[i] == NULL) goto out_pages; |
373b2edd8 firewire: adjust ... |
57 |
|
9aad81253 firewire: Split t... |
58 59 |
address = dma_map_page(card->device, buffer->pages[i], 0, PAGE_SIZE, direction); |
8d8bb39b9 dma-mapping: add ... |
60 |
if (dma_mapping_error(card->device, address)) { |
9aad81253 firewire: Split t... |
61 62 63 64 |
__free_page(buffer->pages[i]); goto out_pages; } set_page_private(buffer->pages[i], address); |
3038e353c firewire: Add cor... |
65 66 67 |
} return 0; |
82eff9db7 firewire: Use dma... |
68 |
|
9aad81253 firewire: Split t... |
69 70 71 72 |
out_pages: for (j = 0; j < i; j++) { address = page_private(buffer->pages[j]); dma_unmap_page(card->device, address, |
29ad14cdd firewire: core: f... |
73 |
PAGE_SIZE, direction); |
9aad81253 firewire: Split t... |
74 75 76 77 78 |
__free_page(buffer->pages[j]); } kfree(buffer->pages); out: buffer->pages = NULL; |
e1eff7a39 firewire: normali... |
79 |
|
2dbd7d7e2 firewire: standar... |
80 |
return -ENOMEM; |
9aad81253 firewire: Split t... |
81 |
} |
c76acec6d firewire: add IPv... |
82 |
EXPORT_SYMBOL(fw_iso_buffer_init); |
9aad81253 firewire: Split t... |
83 84 85 86 |
int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma) { unsigned long uaddr; |
e1eff7a39 firewire: normali... |
87 |
int i, err; |
9aad81253 firewire: Split t... |
88 89 90 |
uaddr = vma->vm_start; for (i = 0; i < buffer->page_count; i++) { |
e1eff7a39 firewire: normali... |
91 92 93 |
err = vm_insert_page(vma, uaddr, buffer->pages[i]); if (err) return err; |
9aad81253 firewire: Split t... |
94 95 96 97 |
uaddr += PAGE_SIZE; } return 0; |
3038e353c firewire: Add cor... |
98 |
} |
9aad81253 firewire: Split t... |
99 100 |
void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card) |
3038e353c firewire: Add cor... |
101 102 |
{ int i; |
9aad81253 firewire: Split t... |
103 |
dma_addr_t address; |
3038e353c firewire: Add cor... |
104 |
|
9aad81253 firewire: Split t... |
105 106 107 |
for (i = 0; i < buffer->page_count; i++) { address = page_private(buffer->pages[i]); dma_unmap_page(card->device, address, |
29ad14cdd firewire: core: f... |
108 |
PAGE_SIZE, buffer->direction); |
9aad81253 firewire: Split t... |
109 110 |
__free_page(buffer->pages[i]); } |
3038e353c firewire: Add cor... |
111 |
|
9aad81253 firewire: Split t... |
112 113 |
kfree(buffer->pages); buffer->pages = NULL; |
3038e353c firewire: Add cor... |
114 |
} |
c76acec6d firewire: add IPv... |
115 |
EXPORT_SYMBOL(fw_iso_buffer_destroy); |
3038e353c firewire: Add cor... |
116 |
|
872e330e3 firewire: add iso... |
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
/* Convert DMA address to offset into virtually contiguous buffer. */ size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed) { int i; dma_addr_t address; ssize_t offset; for (i = 0; i < buffer->page_count; i++) { address = page_private(buffer->pages[i]); offset = (ssize_t)completed - (ssize_t)address; if (offset > 0 && offset <= PAGE_SIZE) return (i << PAGE_SHIFT) + offset; } return 0; } |
53dca5117 firewire: remove ... |
133 134 135 |
struct fw_iso_context *fw_iso_context_create(struct fw_card *card, int type, int channel, int speed, size_t header_size, fw_iso_callback_t callback, void *callback_data) |
3038e353c firewire: Add cor... |
136 137 |
{ struct fw_iso_context *ctx; |
3038e353c firewire: Add cor... |
138 |
|
4817ed240 firewire: prevent... |
139 140 |
ctx = card->driver->allocate_iso_context(card, type, channel, header_size); |
3038e353c firewire: Add cor... |
141 142 143 144 145 |
if (IS_ERR(ctx)) return ctx; ctx->card = card; ctx->type = type; |
21efb3cfc firewire: Configu... |
146 147 |
ctx->channel = channel; ctx->speed = speed; |
295e3feb9 firewire: Impleme... |
148 |
ctx->header_size = header_size; |
872e330e3 firewire: add iso... |
149 |
ctx->callback.sc = callback; |
3038e353c firewire: Add cor... |
150 |
ctx->callback_data = callback_data; |
3038e353c firewire: Add cor... |
151 152 |
return ctx; } |
c76acec6d firewire: add IPv... |
153 |
EXPORT_SYMBOL(fw_iso_context_create); |
3038e353c firewire: Add cor... |
154 155 156 |
void fw_iso_context_destroy(struct fw_iso_context *ctx) { |
872e330e3 firewire: add iso... |
157 |
ctx->card->driver->free_iso_context(ctx); |
3038e353c firewire: Add cor... |
158 |
} |
c76acec6d firewire: add IPv... |
159 |
EXPORT_SYMBOL(fw_iso_context_destroy); |
3038e353c firewire: Add cor... |
160 |
|
53dca5117 firewire: remove ... |
161 162 |
int fw_iso_context_start(struct fw_iso_context *ctx, int cycle, int sync, int tags) |
3038e353c firewire: Add cor... |
163 |
{ |
eb0306eac firewire: Move sy... |
164 |
return ctx->card->driver->start_iso(ctx, cycle, sync, tags); |
3038e353c firewire: Add cor... |
165 |
} |
c76acec6d firewire: add IPv... |
166 |
EXPORT_SYMBOL(fw_iso_context_start); |
3038e353c firewire: Add cor... |
167 |
|
872e330e3 firewire: add iso... |
168 169 170 171 |
int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels) { return ctx->card->driver->set_iso_channels(ctx, channels); } |
53dca5117 firewire: remove ... |
172 173 174 175 |
int fw_iso_context_queue(struct fw_iso_context *ctx, struct fw_iso_packet *packet, struct fw_iso_buffer *buffer, unsigned long payload) |
3038e353c firewire: Add cor... |
176 |
{ |
872e330e3 firewire: add iso... |
177 |
return ctx->card->driver->queue_iso(ctx, packet, buffer, payload); |
3038e353c firewire: Add cor... |
178 |
} |
c76acec6d firewire: add IPv... |
179 |
EXPORT_SYMBOL(fw_iso_context_queue); |
b82956685 firewire: Impleme... |
180 |
|
13882a82e firewire: optimiz... |
181 182 183 184 185 |
void fw_iso_context_queue_flush(struct fw_iso_context *ctx) { ctx->card->driver->flush_queue_iso(ctx); } EXPORT_SYMBOL(fw_iso_context_queue_flush); |
53dca5117 firewire: remove ... |
186 |
int fw_iso_context_stop(struct fw_iso_context *ctx) |
b82956685 firewire: Impleme... |
187 188 189 |
{ return ctx->card->driver->stop_iso(ctx); } |
c76acec6d firewire: add IPv... |
190 |
EXPORT_SYMBOL(fw_iso_context_stop); |
b1bda4cdc firewire: cdev: a... |
191 192 193 194 195 196 |
/* * Isochronous bus resource management (channels, bandwidth), client side */ static int manage_bandwidth(struct fw_card *card, int irm_id, int generation, |
f30e6d3e4 firewire: octlet ... |
197 |
int bandwidth, bool allocate) |
b1bda4cdc firewire: cdev: a... |
198 |
{ |
b1bda4cdc firewire: cdev: a... |
199 |
int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0; |
f30e6d3e4 firewire: octlet ... |
200 |
__be32 data[2]; |
b1bda4cdc firewire: cdev: a... |
201 202 203 204 205 206 207 208 209 |
/* * On a 1394a IRM with low contention, try < 1 is enough. * On a 1394-1995 IRM, we need at least try < 2. * Let's just do try < 5. */ for (try = 0; try < 5; try++) { new = allocate ? old - bandwidth : old + bandwidth; if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL) |
d6372b6e7 firewire: core: f... |
210 |
return -EBUSY; |
b1bda4cdc firewire: cdev: a... |
211 212 213 214 215 216 |
data[0] = cpu_to_be32(old); data[1] = cpu_to_be32(new); switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP, irm_id, generation, SCODE_100, CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE, |
1821bc19d firewire: core: f... |
217 |
data, 8)) { |
b1bda4cdc firewire: cdev: a... |
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
case RCODE_GENERATION: /* A generation change frees all bandwidth. */ return allocate ? -EAGAIN : bandwidth; case RCODE_COMPLETE: if (be32_to_cpup(data) == old) return bandwidth; old = be32_to_cpup(data); /* Fall through. */ } } return -EIO; } static int manage_channel(struct fw_card *card, int irm_id, int generation, |
f30e6d3e4 firewire: octlet ... |
235 |
u32 channels_mask, u64 offset, bool allocate) |
b1bda4cdc firewire: cdev: a... |
236 |
{ |
5aaffc65a firewire: core: r... |
237 |
__be32 bit, all, old; |
f30e6d3e4 firewire: octlet ... |
238 |
__be32 data[2]; |
5aaffc65a firewire: core: r... |
239 |
int channel, ret = -EIO, retry = 5; |
b1bda4cdc firewire: cdev: a... |
240 |
|
5d9cb7d27 firewire: cdev: a... |
241 |
old = all = allocate ? cpu_to_be32(~0) : 0; |
5aaffc65a firewire: core: r... |
242 243 |
for (channel = 0; channel < 32; channel++) { if (!(channels_mask & 1 << channel)) |
b1bda4cdc firewire: cdev: a... |
244 |
continue; |
d6372b6e7 firewire: core: f... |
245 |
ret = -EBUSY; |
5aaffc65a firewire: core: r... |
246 247 |
bit = cpu_to_be32(1 << (31 - channel)); if ((old & bit) != (all & bit)) |
b1bda4cdc firewire: cdev: a... |
248 249 250 |
continue; data[0] = old; |
5aaffc65a firewire: core: r... |
251 |
data[1] = old ^ bit; |
b1bda4cdc firewire: cdev: a... |
252 253 |
switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP, irm_id, generation, SCODE_100, |
1821bc19d firewire: core: f... |
254 |
offset, data, 8)) { |
b1bda4cdc firewire: cdev: a... |
255 256 |
case RCODE_GENERATION: /* A generation change frees all channels. */ |
5aaffc65a firewire: core: r... |
257 |
return allocate ? -EAGAIN : channel; |
b1bda4cdc firewire: cdev: a... |
258 259 260 |
case RCODE_COMPLETE: if (data[0] == old) |
5aaffc65a firewire: core: r... |
261 |
return channel; |
b1bda4cdc firewire: cdev: a... |
262 263 264 265 |
old = data[0]; /* Is the IRM 1394a-2000 compliant? */ |
5aaffc65a firewire: core: r... |
266 |
if ((data[0] & bit) == (data[1] & bit)) |
b1bda4cdc firewire: cdev: a... |
267 268 269 270 |
continue; /* 1394-1995 IRM, fall through to retry. */ default: |
3a1f0a0e3 firewire: core: f... |
271 272 |
if (retry) { retry--; |
5aaffc65a firewire: core: r... |
273 |
channel--; |
d6372b6e7 firewire: core: f... |
274 275 |
} else { ret = -EIO; |
3a1f0a0e3 firewire: core: f... |
276 |
} |
b1bda4cdc firewire: cdev: a... |
277 278 |
} } |
d6372b6e7 firewire: core: f... |
279 |
return ret; |
b1bda4cdc firewire: cdev: a... |
280 281 282 |
} static void deallocate_channel(struct fw_card *card, int irm_id, |
f30e6d3e4 firewire: octlet ... |
283 |
int generation, int channel) |
b1bda4cdc firewire: cdev: a... |
284 |
{ |
5d9cb7d27 firewire: cdev: a... |
285 |
u32 mask; |
b1bda4cdc firewire: cdev: a... |
286 |
u64 offset; |
5d9cb7d27 firewire: cdev: a... |
287 |
mask = channel < 32 ? 1 << channel : 1 << (channel - 32); |
b1bda4cdc firewire: cdev: a... |
288 289 |
offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI : CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO; |
f30e6d3e4 firewire: octlet ... |
290 |
manage_channel(card, irm_id, generation, mask, offset, false); |
b1bda4cdc firewire: cdev: a... |
291 292 293 |
} /** |
656b7afd4 firewire: core: f... |
294 |
* fw_iso_resource_manage() - Allocate or deallocate a channel and/or bandwidth |
b1bda4cdc firewire: cdev: a... |
295 296 297 298 |
* * In parameters: card, generation, channels_mask, bandwidth, allocate * Out parameters: channel, bandwidth * This function blocks (sleeps) during communication with the IRM. |
5d9cb7d27 firewire: cdev: a... |
299 |
* |
b1bda4cdc firewire: cdev: a... |
300 |
* Allocates or deallocates at most one channel out of channels_mask. |
5d9cb7d27 firewire: cdev: a... |
301 302 303 304 |
* channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0. * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for * channel 0 and LSB for channel 63.) * Allocates or deallocates as many bandwidth allocation units as specified. |
b1bda4cdc firewire: cdev: a... |
305 306 307 308 309 310 311 |
* * Returns channel < 0 if no channel was allocated or deallocated. * Returns bandwidth = 0 if no bandwidth was allocated or deallocated. * * If generation is stale, deallocations succeed but allocations fail with * channel = -EAGAIN. * |
5d9cb7d27 firewire: cdev: a... |
312 |
* If channel allocation fails, no bandwidth will be allocated either. |
b1bda4cdc firewire: cdev: a... |
313 |
* If bandwidth allocation fails, no channel will be allocated either. |
5d9cb7d27 firewire: cdev: a... |
314 315 |
* But deallocations of channel and bandwidth are tried independently * of each other's success. |
b1bda4cdc firewire: cdev: a... |
316 317 318 |
*/ void fw_iso_resource_manage(struct fw_card *card, int generation, u64 channels_mask, int *channel, int *bandwidth, |
f30e6d3e4 firewire: octlet ... |
319 |
bool allocate) |
b1bda4cdc firewire: cdev: a... |
320 |
{ |
5d9cb7d27 firewire: cdev: a... |
321 322 |
u32 channels_hi = channels_mask; /* channels 31...0 */ u32 channels_lo = channels_mask >> 32; /* channels 63...32 */ |
b1bda4cdc firewire: cdev: a... |
323 324 325 326 327 328 329 330 |
int irm_id, ret, c = -EINVAL; spin_lock_irq(&card->lock); irm_id = card->irm_node->node_id; spin_unlock_irq(&card->lock); if (channels_hi) c = manage_channel(card, irm_id, generation, channels_hi, |
6fdc03709 firewire: core: d... |
331 |
CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI, |
f30e6d3e4 firewire: octlet ... |
332 |
allocate); |
b1bda4cdc firewire: cdev: a... |
333 334 |
if (channels_lo && c < 0) { c = manage_channel(card, irm_id, generation, channels_lo, |
6fdc03709 firewire: core: d... |
335 |
CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO, |
f30e6d3e4 firewire: octlet ... |
336 |
allocate); |
b1bda4cdc firewire: cdev: a... |
337 338 339 340 |
if (c >= 0) c += 32; } *channel = c; |
5d9cb7d27 firewire: cdev: a... |
341 |
if (allocate && channels_mask != 0 && c < 0) |
b1bda4cdc firewire: cdev: a... |
342 343 344 345 |
*bandwidth = 0; if (*bandwidth == 0) return; |
f30e6d3e4 firewire: octlet ... |
346 |
ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate); |
b1bda4cdc firewire: cdev: a... |
347 348 |
if (ret < 0) *bandwidth = 0; |
cf36df6bf firewire: core: f... |
349 350 |
if (allocate && ret < 0) { if (c >= 0) |
f30e6d3e4 firewire: octlet ... |
351 |
deallocate_channel(card, irm_id, generation, c); |
b1bda4cdc firewire: cdev: a... |
352 353 354 |
*channel = ret; } } |
31ef9134e ALSA: add LaCie F... |
355 |
EXPORT_SYMBOL(fw_iso_resource_manage); |