fs_registerdriver.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /****************************************************************************
  2. * fs/driver/fs_registerdriver.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <errno.h>
  26. #include <nuttx/fs/fs.h>
  27. #include "inode/inode.h"
  28. /****************************************************************************
  29. * Public Functions
  30. ****************************************************************************/
  31. /****************************************************************************
  32. * Name: register_driver
  33. *
  34. * Description:
  35. * Register a character driver inode the pseudo file system.
  36. *
  37. * Input Parameters:
  38. * path - The path to the inode to create
  39. * fops - The file operations structure
  40. * mode - inmode privileges (not used)
  41. * priv - Private, user data that will be associated with the inode.
  42. *
  43. * Returned Value:
  44. * Zero on success (with the inode point in 'inode'); A negated errno
  45. * value is returned on a failure (all error values returned by
  46. * inode_reserve):
  47. *
  48. * EINVAL - 'path' is invalid for this operation
  49. * EEXIST - An inode already exists at 'path'
  50. * ENOMEM - Failed to allocate in-memory resources for the operation
  51. *
  52. ****************************************************************************/
  53. int register_driver(FAR const char *path,
  54. FAR const struct file_operations *fops,
  55. mode_t mode, FAR void *priv)
  56. {
  57. FAR struct inode *node;
  58. int ret;
  59. /* Insert a dummy node -- we need to hold the inode semaphore because we
  60. * will have a momentarily bad structure.
  61. */
  62. ret = inode_semtake();
  63. if (ret < 0)
  64. {
  65. return ret;
  66. }
  67. ret = inode_reserve(path, &node);
  68. if (ret >= 0)
  69. {
  70. /* We have it, now populate it with driver specific information.
  71. * NOTE that the initial reference count on the new inode is zero.
  72. */
  73. INODE_SET_DRIVER(node);
  74. node->u.i_ops = fops;
  75. #ifdef CONFIG_FILE_MODE
  76. node->i_mode = mode;
  77. #endif
  78. node->i_private = priv;
  79. ret = OK;
  80. }
  81. inode_semgive();
  82. return ret;
  83. }