packets-buffer.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * helpers for managing a buffer for many packets
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  6. */
  7. #include <linux/firewire.h>
  8. #include <linux/export.h>
  9. #include <linux/slab.h>
  10. #include "packets-buffer.h"
  11. /**
  12. * iso_packets_buffer_init - allocates the memory for packets
  13. * @b: the buffer structure to initialize
  14. * @unit: the device at the other end of the stream
  15. * @count: the number of packets
  16. * @packet_size: the (maximum) size of a packet, in bytes
  17. * @direction: %DMA_TO_DEVICE or %DMA_FROM_DEVICE
  18. */
  19. int iso_packets_buffer_init(struct iso_packets_buffer *b, struct fw_unit *unit,
  20. unsigned int count, unsigned int packet_size,
  21. enum dma_data_direction direction)
  22. {
  23. unsigned int packets_per_page, pages;
  24. unsigned int i, page_index, offset_in_page;
  25. void *p;
  26. int err;
  27. b->packets = kmalloc(count * sizeof(*b->packets), GFP_KERNEL);
  28. if (!b->packets) {
  29. err = -ENOMEM;
  30. goto error;
  31. }
  32. packet_size = L1_CACHE_ALIGN(packet_size);
  33. packets_per_page = PAGE_SIZE / packet_size;
  34. if (WARN_ON(!packets_per_page)) {
  35. err = -EINVAL;
  36. goto err_packets;
  37. }
  38. pages = DIV_ROUND_UP(count, packets_per_page);
  39. err = fw_iso_buffer_init(&b->iso_buffer, fw_parent_device(unit)->card,
  40. pages, direction);
  41. if (err < 0)
  42. goto err_packets;
  43. for (i = 0; i < count; ++i) {
  44. page_index = i / packets_per_page;
  45. p = page_address(b->iso_buffer.pages[page_index]);
  46. offset_in_page = (i % packets_per_page) * packet_size;
  47. b->packets[i].buffer = p + offset_in_page;
  48. b->packets[i].offset = page_index * PAGE_SIZE + offset_in_page;
  49. }
  50. return 0;
  51. err_packets:
  52. kfree(b->packets);
  53. error:
  54. return err;
  55. }
  56. EXPORT_SYMBOL(iso_packets_buffer_init);
  57. /**
  58. * iso_packets_buffer_destroy - frees packet buffer resources
  59. * @b: the buffer structure to free
  60. * @unit: the device at the other end of the stream
  61. */
  62. void iso_packets_buffer_destroy(struct iso_packets_buffer *b,
  63. struct fw_unit *unit)
  64. {
  65. fw_iso_buffer_destroy(&b->iso_buffer, fw_parent_device(unit)->card);
  66. kfree(b->packets);
  67. }
  68. EXPORT_SYMBOL(iso_packets_buffer_destroy);