user_freertos.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2021 VERISILICON
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2021 VERISILICON
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #include "osal.h"
  55. #include "user_freertos.h"
  56. #include "io_tools.h"
  57. #include "memalloc_freertos.h"
  58. static pthread_mutex_t vcmd_dev_open_count_mutex = PTHREAD_MUTEX_INITIALIZER;
  59. static u32 vcmd_dev_open_count = 0;
  60. extern int vcmd; //it is defined in hantro_dec_freertos.c
  61. /**
  62. * Unify driver for vcmd, the related vcmd interface besides init/open/release/
  63. * ioctl will be called in the hantrdec_init/open/release/ioctl when vcmd is used.
  64. * All drivers's probe should be added and be called by main() explicitly
  65. */
  66. int Platform_init() {
  67. //Firstly, initialize the mem for IO device
  68. if (memalloc_init() != 0) {
  69. PDEBUG("memalloc_init error\n");
  70. assert(0);
  71. }
  72. //vcmd will modify if exist vcmd device in hantrodec_init
  73. if (hantrodec_init() != 0) {
  74. PDEBUG("hantroenc_init error\n");
  75. assert(0);
  76. }
  77. return 0;
  78. }
  79. int freertos_open(const char* dev_name, int flag) {
  80. //do a hash operator to gain the only dev fd
  81. //int fd = hash(name);
  82. int fd = 0; //the default is error id
  83. if(!strcmp(dev_name, DEC_MODULE_PATH)) {
  84. fd = DEC_FD << 28;
  85. if(vcmd) {
  86. //u32, MSB 4bit for device type, LSB 28bit for reference count
  87. pthread_mutex_lock(&vcmd_dev_open_count_mutex);
  88. vcmd_dev_open_count++;
  89. fd += vcmd_dev_open_count;
  90. pthread_mutex_unlock(&vcmd_dev_open_count_mutex);
  91. }
  92. hantrodec_open(NULL, fd);
  93. }
  94. else if(!strcmp(dev_name, MEMALLOC_MODULE_PATH)) {
  95. fd = MEM_FD << 28;
  96. memalloc_open(NULL, fd);
  97. }
  98. return fd;
  99. }
  100. void freertos_close(int fd) {
  101. //u32, MSB 4bit for device type
  102. u32 fd_tmp = fd;
  103. fd_tmp = fd_tmp >> 28;
  104. switch(fd_tmp) {
  105. case DEC_FD: {
  106. hantrodec_release(NULL, fd);
  107. break;
  108. }
  109. case MEM_FD: {
  110. //so if low in special paltform, could comment it when test, but in normal, it will be opened
  111. //memalloc_release(NULL, fd);
  112. break;
  113. }
  114. case 0: //for /dev/mem, it it not used, so shouldn't go to default
  115. break;
  116. default: {
  117. PDEBUG("freertos_release fd 0x%x error\n", fd);
  118. break;
  119. }
  120. }
  121. }
  122. long freertos_ioctl(int fd, unsigned int cmd, void *arg) {
  123. long ret = -1;
  124. //u32, MSB 4bit for device type
  125. u32 fd_tmp = fd;
  126. fd_tmp = fd_tmp >> 28;
  127. switch(fd_tmp) {
  128. case DEC_FD: {
  129. ret = hantrodec_ioctl(fd, cmd, arg);
  130. break;
  131. }
  132. case MEM_FD: {
  133. ret = memalloc_ioctl(fd, cmd, arg);
  134. break;
  135. }
  136. default: {
  137. PDEBUG("freertos_ioctl fd 0x%x error\n", fd);
  138. break;
  139. }
  140. }
  141. return ret;
  142. }