fw-iso.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Isochronous IO functionality
  3. *
  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. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/mm.h>
  25. #include "fw-transaction.h"
  26. #include "fw-topology.h"
  27. #include "fw-device.h"
  28. int
  29. fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  30. int page_count, enum dma_data_direction direction)
  31. {
  32. int i, j, retval = -ENOMEM;
  33. dma_addr_t address;
  34. buffer->page_count = page_count;
  35. buffer->direction = direction;
  36. buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
  37. GFP_KERNEL);
  38. if (buffer->pages == NULL)
  39. goto out;
  40. for (i = 0; i < buffer->page_count; i++) {
  41. buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  42. if (buffer->pages[i] == NULL)
  43. goto out_pages;
  44. address = dma_map_page(card->device, buffer->pages[i],
  45. 0, PAGE_SIZE, direction);
  46. if (dma_mapping_error(card->device, address)) {
  47. __free_page(buffer->pages[i]);
  48. goto out_pages;
  49. }
  50. set_page_private(buffer->pages[i], address);
  51. }
  52. return 0;
  53. out_pages:
  54. for (j = 0; j < i; j++) {
  55. address = page_private(buffer->pages[j]);
  56. dma_unmap_page(card->device, address,
  57. PAGE_SIZE, DMA_TO_DEVICE);
  58. __free_page(buffer->pages[j]);
  59. }
  60. kfree(buffer->pages);
  61. out:
  62. buffer->pages = NULL;
  63. return retval;
  64. }
  65. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
  66. {
  67. unsigned long uaddr;
  68. int i, retval;
  69. uaddr = vma->vm_start;
  70. for (i = 0; i < buffer->page_count; i++) {
  71. retval = vm_insert_page(vma, uaddr, buffer->pages[i]);
  72. if (retval)
  73. return retval;
  74. uaddr += PAGE_SIZE;
  75. }
  76. return 0;
  77. }
  78. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
  79. struct fw_card *card)
  80. {
  81. int i;
  82. dma_addr_t address;
  83. for (i = 0; i < buffer->page_count; i++) {
  84. address = page_private(buffer->pages[i]);
  85. dma_unmap_page(card->device, address,
  86. PAGE_SIZE, DMA_TO_DEVICE);
  87. __free_page(buffer->pages[i]);
  88. }
  89. kfree(buffer->pages);
  90. buffer->pages = NULL;
  91. }
  92. struct fw_iso_context *
  93. fw_iso_context_create(struct fw_card *card, int type,
  94. int channel, int speed, size_t header_size,
  95. fw_iso_callback_t callback, void *callback_data)
  96. {
  97. struct fw_iso_context *ctx;
  98. ctx = card->driver->allocate_iso_context(card, type, header_size);
  99. if (IS_ERR(ctx))
  100. return ctx;
  101. ctx->card = card;
  102. ctx->type = type;
  103. ctx->channel = channel;
  104. ctx->speed = speed;
  105. ctx->header_size = header_size;
  106. ctx->callback = callback;
  107. ctx->callback_data = callback_data;
  108. return ctx;
  109. }
  110. void fw_iso_context_destroy(struct fw_iso_context *ctx)
  111. {
  112. struct fw_card *card = ctx->card;
  113. card->driver->free_iso_context(ctx);
  114. }
  115. int
  116. fw_iso_context_start(struct fw_iso_context *ctx, int cycle, int sync, int tags)
  117. {
  118. return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
  119. }
  120. int
  121. fw_iso_context_queue(struct fw_iso_context *ctx,
  122. struct fw_iso_packet *packet,
  123. struct fw_iso_buffer *buffer,
  124. unsigned long payload)
  125. {
  126. struct fw_card *card = ctx->card;
  127. return card->driver->queue_iso(ctx, packet, buffer, payload);
  128. }
  129. int
  130. fw_iso_context_stop(struct fw_iso_context *ctx)
  131. {
  132. return ctx->card->driver->stop_iso(ctx);
  133. }