driver.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <rtthread.h>
  7. #ifdef RT_USING_FDT
  8. #include <dtb_node.h>
  9. #endif
  10. #if defined(RT_USING_POSIX_DEVIO)
  11. #include <rtdevice.h> /* for wqueue_init */
  12. #endif
  13. /**
  14. * This function driver device match with id
  15. *
  16. * @param drv the pointer of driver structure
  17. * @param device_id the id of the device
  18. *
  19. * @return the error code, RT_EOK on successfully.
  20. */
  21. rt_err_t rt_driver_match_with_id(const rt_driver_t drv,int device_id)
  22. {
  23. rt_device_t device;
  24. int ret;
  25. if (!drv)
  26. {
  27. return -RT_EINVAL;
  28. }
  29. device = rt_device_create_since_driver(drv,device_id);
  30. if(!device)
  31. {
  32. return -RT_ERROR;
  33. }
  34. ret = rt_device_bind_driver(device,drv,RT_NULL);
  35. if(ret != 0)
  36. {
  37. return -RT_ERROR;
  38. }
  39. ret = rt_device_probe_and_init(device);
  40. if(ret != 0)
  41. {
  42. return -RT_ERROR;
  43. }
  44. return ret;
  45. }
  46. RTM_EXPORT(rt_driver_match_with_id);
  47. #ifdef RT_USING_FDT
  48. /**
  49. * This function driver device match with dtb_node
  50. *
  51. * @param drv the pointer of driver structure
  52. * @param from_node dtb node entry
  53. * @param max_dev_num the max device support
  54. *
  55. * @return the error code, RT_EOK on successfully.
  56. */
  57. rt_err_t rt_driver_match_with_dtb(const rt_driver_t drv,void *from_node,int max_dev_num)
  58. {
  59. struct dtb_node** node_list;
  60. rt_device_t device;
  61. int ret,i;
  62. int total_dev_num = 0;
  63. if ((!drv)||(!drv->dev_match)||(!drv->dev_match->compatible)||(!from_node)||(!drv->device_size))
  64. {
  65. return -RT_EINVAL;
  66. }
  67. node_list = rt_calloc(max_dev_num,sizeof(void *));
  68. if(!node_list)
  69. {
  70. return -RT_ERROR;
  71. }
  72. ret = dtb_node_find_all_compatible_node(from_node,drv->dev_match->compatible,node_list,max_dev_num,&total_dev_num);
  73. if((ret != 0) || (!total_dev_num))
  74. {
  75. return -RT_ERROR;
  76. }
  77. for(i = 0; i < total_dev_num; i ++)
  78. {
  79. if (!dtb_node_device_is_available(node_list[i]))
  80. {
  81. continue;
  82. }
  83. device = rt_device_create_since_driver(drv,i);
  84. if(!device)
  85. {
  86. continue;
  87. }
  88. ret = rt_device_bind_driver(device,drv,node_list[i]);
  89. if(ret != 0)
  90. {
  91. continue;
  92. }
  93. ret = rt_device_probe_and_init(device);
  94. if(ret != 0)
  95. {
  96. continue;
  97. }
  98. }
  99. rt_free(node_list);
  100. return ret;
  101. }
  102. RTM_EXPORT(rt_driver_match_with_dtb);
  103. #endif