ionutils.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. //#include <stdint.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/mman.h>
  9. #include "ionutils.h"
  10. #include "ipcsocket.h"
  11. void write_buffer(void *buffer, unsigned long len)
  12. {
  13. int i;
  14. unsigned char *ptr = (unsigned char *)buffer;
  15. if (!ptr) {
  16. fprintf(stderr, "<%s>: Invalid buffer...\n", __func__);
  17. return;
  18. }
  19. printf("Fill buffer content:\n");
  20. memset(ptr, 0xfd, len);
  21. for (i = 0; i < len; i++)
  22. printf("0x%x ", ptr[i]);
  23. printf("\n");
  24. }
  25. void read_buffer(void *buffer, unsigned long len)
  26. {
  27. int i;
  28. unsigned char *ptr = (unsigned char *)buffer;
  29. if (!ptr) {
  30. fprintf(stderr, "<%s>: Invalid buffer...\n", __func__);
  31. return;
  32. }
  33. printf("Read buffer content:\n");
  34. for (i = 0; i < len; i++)
  35. printf("0x%x ", ptr[i]);
  36. printf("\n");
  37. }
  38. int ion_export_buffer_fd(struct ion_buffer_info *ion_info)
  39. {
  40. int i, ret, ionfd, buffer_fd;
  41. unsigned int heap_id;
  42. unsigned long maplen;
  43. unsigned char *map_buffer;
  44. struct ion_allocation_data alloc_data;
  45. struct ion_heap_query query;
  46. struct ion_heap_data heap_data[MAX_HEAP_COUNT];
  47. if (!ion_info) {
  48. fprintf(stderr, "<%s>: Invalid ion info\n", __func__);
  49. return -1;
  50. }
  51. /* Create an ION client */
  52. ionfd = open(ION_DEVICE, O_RDWR);
  53. if (ionfd < 0) {
  54. fprintf(stderr, "<%s>: Failed to open ion client: %s\n",
  55. __func__, strerror(errno));
  56. return -1;
  57. }
  58. memset(&query, 0, sizeof(query));
  59. query.cnt = MAX_HEAP_COUNT;
  60. query.heaps = (unsigned long int)&heap_data[0];
  61. /* Query ION heap_id_mask from ION heap */
  62. ret = ioctl(ionfd, ION_IOC_HEAP_QUERY, &query);
  63. if (ret < 0) {
  64. fprintf(stderr, "<%s>: Failed: ION_IOC_HEAP_QUERY: %s\n",
  65. __func__, strerror(errno));
  66. goto err_query;
  67. }
  68. heap_id = MAX_HEAP_COUNT + 1;
  69. for (i = 0; i < query.cnt; i++) {
  70. if (heap_data[i].type == ion_info->heap_type) {
  71. heap_id = heap_data[i].heap_id;
  72. break;
  73. }
  74. }
  75. if (heap_id > MAX_HEAP_COUNT) {
  76. fprintf(stderr, "<%s>: ERROR: heap type does not exists\n",
  77. __func__);
  78. goto err_heap;
  79. }
  80. alloc_data.len = ion_info->heap_size;
  81. alloc_data.heap_id_mask = 1 << heap_id;
  82. alloc_data.flags = ion_info->flag_type;
  83. /* Allocate memory for this ION client as per heap_type */
  84. ret = ioctl(ionfd, ION_IOC_ALLOC, &alloc_data);
  85. if (ret < 0) {
  86. fprintf(stderr, "<%s>: Failed: ION_IOC_ALLOC: %s\n",
  87. __func__, strerror(errno));
  88. goto err_alloc;
  89. }
  90. /* This will return a valid buffer fd */
  91. buffer_fd = alloc_data.fd;
  92. maplen = alloc_data.len;
  93. if (buffer_fd < 0 || maplen <= 0) {
  94. fprintf(stderr, "<%s>: Invalid map data, fd: %d, len: %ld\n",
  95. __func__, buffer_fd, maplen);
  96. goto err_fd_data;
  97. }
  98. /* Create memory mapped buffer for the buffer fd */
  99. map_buffer = (unsigned char *)mmap(NULL, maplen, PROT_READ|PROT_WRITE,
  100. MAP_SHARED, buffer_fd, 0);
  101. if (map_buffer == MAP_FAILED) {
  102. fprintf(stderr, "<%s>: Failed: mmap: %s\n",
  103. __func__, strerror(errno));
  104. goto err_mmap;
  105. }
  106. ion_info->ionfd = ionfd;
  107. ion_info->buffd = buffer_fd;
  108. ion_info->buffer = map_buffer;
  109. ion_info->buflen = maplen;
  110. return 0;
  111. munmap(map_buffer, maplen);
  112. err_fd_data:
  113. err_mmap:
  114. /* in case of error: close the buffer fd */
  115. if (buffer_fd)
  116. close(buffer_fd);
  117. err_query:
  118. err_heap:
  119. err_alloc:
  120. /* In case of error: close the ion client fd */
  121. if (ionfd)
  122. close(ionfd);
  123. return -1;
  124. }
  125. int ion_import_buffer_fd(struct ion_buffer_info *ion_info)
  126. {
  127. int buffd;
  128. unsigned char *map_buf;
  129. unsigned long map_len;
  130. if (!ion_info) {
  131. fprintf(stderr, "<%s>: Invalid ion info\n", __func__);
  132. return -1;
  133. }
  134. map_len = ion_info->buflen;
  135. buffd = ion_info->buffd;
  136. if (buffd < 0 || map_len <= 0) {
  137. fprintf(stderr, "<%s>: Invalid map data, fd: %d, len: %ld\n",
  138. __func__, buffd, map_len);
  139. goto err_buffd;
  140. }
  141. map_buf = (unsigned char *)mmap(NULL, map_len, PROT_READ|PROT_WRITE,
  142. MAP_SHARED, buffd, 0);
  143. if (map_buf == MAP_FAILED) {
  144. printf("<%s>: Failed - mmap: %s\n",
  145. __func__, strerror(errno));
  146. goto err_mmap;
  147. }
  148. ion_info->buffer = map_buf;
  149. ion_info->buflen = map_len;
  150. return 0;
  151. err_mmap:
  152. if (buffd)
  153. close(buffd);
  154. err_buffd:
  155. return -1;
  156. }
  157. void ion_close_buffer_fd(struct ion_buffer_info *ion_info)
  158. {
  159. if (ion_info) {
  160. /* unmap the buffer properly in the end */
  161. munmap(ion_info->buffer, ion_info->buflen);
  162. /* close the buffer fd */
  163. if (ion_info->buffd > 0)
  164. close(ion_info->buffd);
  165. /* Finally, close the client fd */
  166. if (ion_info->ionfd > 0)
  167. close(ion_info->ionfd);
  168. }
  169. }
  170. int socket_send_fd(struct socket_info *info)
  171. {
  172. int status;
  173. int fd, sockfd;
  174. struct socketdata skdata;
  175. if (!info) {
  176. fprintf(stderr, "<%s>: Invalid socket info\n", __func__);
  177. return -1;
  178. }
  179. sockfd = info->sockfd;
  180. fd = info->datafd;
  181. memset(&skdata, 0, sizeof(skdata));
  182. skdata.data = fd;
  183. skdata.len = sizeof(skdata.data);
  184. status = sendtosocket(sockfd, &skdata);
  185. if (status < 0) {
  186. fprintf(stderr, "<%s>: Failed: sendtosocket\n", __func__);
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. int socket_receive_fd(struct socket_info *info)
  192. {
  193. int status;
  194. int fd, sockfd;
  195. struct socketdata skdata;
  196. if (!info) {
  197. fprintf(stderr, "<%s>: Invalid socket info\n", __func__);
  198. return -1;
  199. }
  200. sockfd = info->sockfd;
  201. memset(&skdata, 0, sizeof(skdata));
  202. status = receivefromsocket(sockfd, &skdata);
  203. if (status < 0) {
  204. fprintf(stderr, "<%s>: Failed: receivefromsocket\n", __func__);
  205. return -1;
  206. }
  207. fd = (int)skdata.data;
  208. info->datafd = fd;
  209. return status;
  210. }