rt_dev_bus.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-10-13 flybreak the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #define DBG_TAG "dev_bus"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #if defined(RT_USING_POSIX_DEVIO)
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <poll.h>
  21. #include <sys/ioctl.h>
  22. #include <dfs_file.h>
  23. static int bus_fops_open(struct dfs_fd *fd)
  24. {
  25. LOG_D("bus fops open");
  26. return 0;
  27. }
  28. static int bus_fops_close(struct dfs_fd *fd)
  29. {
  30. LOG_D("bus fops close");
  31. return 0;
  32. }
  33. static const struct dfs_file_ops bus_fops =
  34. {
  35. bus_fops_open,
  36. bus_fops_close,
  37. RT_NULL,
  38. RT_NULL,
  39. RT_NULL,
  40. RT_NULL,
  41. RT_NULL,
  42. RT_NULL,
  43. RT_NULL,
  44. };
  45. #endif
  46. rt_device_t rt_device_bus_create(char *name, int attach_size)
  47. {
  48. rt_err_t result = RT_EOK;
  49. rt_device_t dev = rt_device_create(RT_Device_Class_Bus, 0);
  50. result = rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
  51. if (result < 0)
  52. {
  53. rt_kprintf("dev bus [%s] register failed!, ret=%d\n", name, result);
  54. return RT_NULL;
  55. }
  56. #if defined(RT_USING_POSIX_DEVIO)
  57. dev->fops = &bus_fops;
  58. #endif
  59. LOG_D("bus create");
  60. return dev;
  61. }
  62. rt_err_t rt_device_bus_destroy(rt_device_t dev)
  63. {
  64. rt_device_unregister(dev);
  65. dev->parent.type = RT_Object_Class_Device;
  66. rt_device_destroy(dev);
  67. LOG_D("bus destroy");
  68. return RT_EOK;
  69. }