drvier.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <rtthread.h>
  7. #include <fdt.h>
  8. #include <rt_dtb_node.h>
  9. #include <rt_device_node.h>
  10. #if defined(RT_USING_POSIX)
  11. #include <rtdevice.h> /* for wqueue_init */
  12. #endif
  13. #define MAX_COMPATIBLE_NUM 10
  14. /**
  15. * This function bind drvier and device
  16. *
  17. * @param driver the pointer of driver structure
  18. * @param device the pointer of device structure
  19. * @param node the pointer of fdt node structure
  20. *
  21. * @return the error code, RT_EOK on successfully.
  22. */
  23. rt_err_t rt_device_bind(rt_driver_t driver, rt_device_t device, void *node)
  24. {
  25. if((!driver) || (!device))
  26. {
  27. return -RT_ERROR;
  28. }
  29. device->drv = driver;
  30. device->ops = driver->dev_ops;
  31. device->fdt_node = node;
  32. return RT_EOK;
  33. }
  34. /**
  35. * This function create rt_device and init the device
  36. *
  37. * @param driver the pointer of driver structure
  38. * @param device_num how many device should be create
  39. * @param ftd_node_list ftd node list
  40. *
  41. * @return the error code, RT_EOK on successfully.
  42. */
  43. rt_err_t rt_device_create_and_init(rt_driver_t drv,int device_num,struct dtb_node **ftd_node_list)
  44. {
  45. int i = 0;
  46. int ret = -1;
  47. rt_device_t device;
  48. for(i = 0; i < device_num; i ++)
  49. {
  50. device = (rt_device_t)rt_malloc(drv->device_size);
  51. if(device == RT_NULL)
  52. {
  53. return -RT_ERROR;
  54. }
  55. rt_memset(device,0,drv->device_size);
  56. if(drv->device_priv_data_size != 0)
  57. {
  58. device->user_data = (void *)(rt_malloc(drv->device_priv_data_size));
  59. if(device->user_data == RT_NULL)
  60. {
  61. rt_free(device);
  62. return -RT_ERROR;
  63. }
  64. rt_memset(device->user_data,0,drv->device_priv_data_size);
  65. }
  66. device->device_id = i;
  67. rt_strncpy(device->parent.name,drv->name,rt_strlen(drv->name));
  68. rt_sprintf(device->parent.name + rt_strlen(drv->name),"%d",i);
  69. rt_device_bind(drv,device,ftd_node_list[i]);
  70. if(device->drv->probe)
  71. {
  72. ret = device->drv->probe((rt_device_t)device);
  73. }
  74. if(device->drv->init)
  75. {
  76. ret = device->drv->init((rt_device_t)device);
  77. }
  78. }
  79. return ret;
  80. }
  81. /**
  82. * This function registers a device driver with specified name.
  83. *
  84. * @param dev the pointer of driver structure
  85. * @param flags the capabilities flag of device
  86. *
  87. * @return the error code, RT_EOK on successfully.
  88. */
  89. rt_err_t rt_driver_register(rt_driver_t drv)
  90. {
  91. struct dtb_node *root_node = RT_NULL;
  92. struct dtb_node* node_list[MAX_COMPATIBLE_NUM];
  93. int ret,i,device_num;
  94. if (drv == RT_NULL)
  95. {
  96. return -RT_ERROR;
  97. }
  98. root_node = get_dtb_node_head();
  99. if(drv->dev_match->compatible != RT_NULL)
  100. {
  101. ret = fdt_find_all_active_compatible_node(root_node,drv->dev_match->compatible,node_list,MAX_COMPATIBLE_NUM,&device_num);
  102. if(!ret)
  103. {
  104. }
  105. }
  106. else
  107. {
  108. device_num = drv->total_device_num;
  109. for(i = 0; i < device_num; i++)
  110. {
  111. node_list[i] = RT_NULL;
  112. }
  113. }
  114. if(!device_num)
  115. {
  116. rt_kprintf("can not match compatible device\n");
  117. }
  118. ret = rt_device_create_and_init(drv,device_num,node_list);
  119. return ret;
  120. }
  121. RTM_EXPORT(rt_driver_register);
  122. /**
  123. * This function removes a previously registered device driver
  124. *
  125. * @param dev the pointer of device driver structure
  126. *
  127. * @return the error code, RT_EOK on successfully.
  128. */
  129. rt_err_t rt_driver_unregister(rt_driver_t drv)
  130. {
  131. /*todo*/
  132. return RT_EOK;
  133. }
  134. RTM_EXPORT(rt_driver_unregister);