device.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * device.h
  3. *
  4. * Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _DEVICE_H_
  20. #define _DEVICE_H_
  21. #include <common.h>
  22. #include <lock.h>
  23. #include <cache.h>
  24. #include <filesystem.h>
  25. #include <sdk/list.h>
  26. #include <sdk/device.h>
  27. #ifndef DEVICE_TYPEDEF
  28. #define DEVICE_TYPEDEF
  29. typedef struct device device_t;
  30. #endif
  31. typedef dword_t (*device_init_proc_t)(void);
  32. typedef dword_t (*device_cleanup_proc_t)(void);
  33. typedef dword_t (*device_ioctl_proc_t)(
  34. device_t *device,
  35. dword_t control_code,
  36. const void *in_buffer,
  37. size_t in_length,
  38. void *out_buffer,
  39. size_t out_length
  40. );
  41. typedef dword_t (*block_dev_read_proc_t)(device_t *device, void *buffer, qword_t offset, size_t length, size_t *bytes_read);
  42. typedef dword_t (*block_dev_write_proc_t)(device_t *device, const void *buffer, qword_t offset, size_t length, size_t *bytes_written);
  43. typedef dword_t (*char_dev_read_proc_t)(device_t *device, void *buffer, size_t length, size_t *bytes_read);
  44. typedef dword_t (*char_dev_write_proc_t)(device_t *device, const void *buffer, size_t length, size_t *bytes_written);
  45. typedef struct
  46. {
  47. list_entry_t list;
  48. dword_t mounted_devices;
  49. device_init_proc_t init_proc;
  50. device_cleanup_proc_t cleanup_proc;
  51. block_dev_read_proc_t read_proc;
  52. block_dev_write_proc_t write_proc;
  53. device_ioctl_proc_t ioctl_proc;
  54. } block_dev_driver_t;
  55. typedef struct
  56. {
  57. list_entry_t list;
  58. device_init_proc_t init_proc;
  59. device_cleanup_proc_t cleanup_proc;
  60. char_dev_read_proc_t read_proc;
  61. char_dev_write_proc_t write_proc;
  62. device_ioctl_proc_t ioctl_proc;
  63. } char_dev_driver_t;
  64. typedef struct device
  65. {
  66. list_entry_t list;
  67. device_type_t type;
  68. char name[MAX_DEVICE_NAME];
  69. lock_t lock;
  70. dword_t flags;
  71. qword_t capacity;
  72. union
  73. {
  74. void *driver;
  75. block_dev_driver_t *block_driver;
  76. char_dev_driver_t *char_driver;
  77. };
  78. } device_t;
  79. typedef struct
  80. {
  81. dword_t heads;
  82. dword_t tracks;
  83. dword_t sectors_per_track;
  84. dword_t bytes_per_sector;
  85. } block_dev_media_info_t;
  86. typedef struct
  87. {
  88. file_t header;
  89. device_t *device;
  90. } device_file_t;
  91. typedef struct
  92. {
  93. file_instance_t header;
  94. device_t *next_device;
  95. } device_dir_inst_t;
  96. device_t *get_block_device(const char *name);
  97. dword_t register_block_dev_driver(block_dev_driver_t *driver);
  98. dword_t unregister_block_dev_driver(block_dev_driver_t *driver);
  99. dword_t register_block_device(device_t *device);
  100. dword_t unregister_block_device(device_t *device);
  101. device_t *get_char_device(const char *name);
  102. dword_t register_char_dev_driver(char_dev_driver_t *driver);
  103. dword_t unregister_char_dev_driver(char_dev_driver_t *driver);
  104. dword_t register_char_device(device_t *device);
  105. dword_t unregister_char_device(device_t *device);
  106. dword_t device_read(device_t *device, void *buffer, qword_t offset, size_t length, size_t *bytes_read);
  107. dword_t device_write(device_t *device, const void *buffer, qword_t offset, size_t length, size_t *bytes_written);
  108. dword_t device_ioctl_internal(device_t *device, dword_t control_code, const void *in_buffer, size_t in_length, void *out_buffer, size_t out_length);
  109. void device_init(void);
  110. #endif