dev_zero.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /****************************************************************************
  2. * drivers/dev_zero.c
  3. *
  4. * Copyright (C) 2008-2009, 2012-2013, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdbool.h>
  41. #include <string.h>
  42. #include <poll.h>
  43. #include <errno.h>
  44. #include <nuttx/fs/fs.h>
  45. #include <nuttx/drivers/drivers.h>
  46. /****************************************************************************
  47. * Private Function Prototypes
  48. ****************************************************************************/
  49. static ssize_t devzero_read(FAR struct file *filep, FAR char *buffer,
  50. size_t buflen);
  51. static ssize_t devzero_write(FAR struct file *filep, FAR const char *buffer,
  52. size_t buflen);
  53. #ifndef CONFIG_DISABLE_POLL
  54. static int devzero_poll(FAR struct file *filep, FAR struct pollfd *fds,
  55. bool setup);
  56. #endif
  57. /****************************************************************************
  58. * Private Data
  59. ****************************************************************************/
  60. static const struct file_operations devzero_fops =
  61. {
  62. NULL, /* open */
  63. NULL, /* close */
  64. devzero_read, /* read */
  65. devzero_write, /* write */
  66. NULL, /* seek */
  67. NULL /* ioctl */
  68. #ifndef CONFIG_DISABLE_POLL
  69. , devzero_poll /* poll */
  70. #endif
  71. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  72. , NULL /* unlink */
  73. #endif
  74. };
  75. /****************************************************************************
  76. * Private Functions
  77. ****************************************************************************/
  78. /****************************************************************************
  79. * Name: devzero_read
  80. ****************************************************************************/
  81. static ssize_t devzero_read(FAR struct file *filep, FAR char *buffer,
  82. size_t len)
  83. {
  84. memset(buffer, 0, len);
  85. return len;
  86. }
  87. /****************************************************************************
  88. * Name: devzero_write
  89. ****************************************************************************/
  90. static ssize_t devzero_write(FAR struct file *filep, FAR const char *buffer,
  91. size_t len)
  92. {
  93. return len;
  94. }
  95. /****************************************************************************
  96. * Name: devzero_poll
  97. ****************************************************************************/
  98. #ifndef CONFIG_DISABLE_POLL
  99. static int devzero_poll(FAR struct file *filep, FAR struct pollfd *fds,
  100. bool setup)
  101. {
  102. if (setup)
  103. {
  104. fds->revents |= (fds->events & (POLLIN | POLLOUT));
  105. if (fds->revents != 0)
  106. {
  107. sem_post(fds->sem);
  108. }
  109. }
  110. return OK;
  111. }
  112. #endif
  113. /****************************************************************************
  114. * Public Functions
  115. ****************************************************************************/
  116. /****************************************************************************
  117. * Name: devzero_register
  118. *
  119. * Description:
  120. * Register /dev/zero
  121. *
  122. ****************************************************************************/
  123. void devzero_register(void)
  124. {
  125. (void)register_driver("/dev/zero", &devzero_fops, 0666, NULL);
  126. }