char_device.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * char_device.h
  3. *
  4. * Copyright (C) 2015 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 _CHAR_DEVICE_H_
  20. #define _CHAR_DEVICE_H_
  21. #include <common.h>
  22. #include <list.h>
  23. #include <sync.h>
  24. #define MAX_CHAR_DEV_NAME 32
  25. #define IOCTL_CHAR_DEV_CHECK_INPUT 0xC0000000
  26. #define IOCTL_CHAR_DEV_CHECK_OUTPUT 0xC0000001
  27. typedef struct _char_device_t char_device_t;
  28. typedef dword_t (*char_dev_init_proc_t)(void);
  29. typedef dword_t (*char_dev_cleanup_proc_t)(void);
  30. typedef dword_t (*char_dev_read_proc_t)(char_device_t *device, void *buffer, size_t length, size_t *bytes_read);
  31. typedef dword_t (*char_dev_write_proc_t)(char_device_t *device, const void *buffer, size_t length, size_t *bytes_written);
  32. typedef dword_t (*char_dev_ioctl_proc_t)(
  33. char_device_t *device,
  34. dword_t control_code,
  35. const void *in_buffer,
  36. size_t in_length,
  37. void *out_buffer,
  38. size_t out_length
  39. );
  40. typedef struct
  41. {
  42. list_entry_t list;
  43. char_dev_init_proc_t init_proc;
  44. char_dev_cleanup_proc_t cleanup_proc;
  45. char_dev_read_proc_t read_proc;
  46. char_dev_write_proc_t write_proc;
  47. char_dev_ioctl_proc_t ioctl_proc;
  48. } char_dev_driver_t;
  49. struct _char_device_t
  50. {
  51. list_entry_t list;
  52. char_dev_driver_t *driver;
  53. char name[MAX_CHAR_DEV_NAME];
  54. resource_t resource;
  55. };
  56. dword_t register_char_dev_driver(char_dev_driver_t *driver);
  57. dword_t unregister_char_dev_driver(char_dev_driver_t *driver);
  58. char_device_t *get_char_device(const char *name);
  59. dword_t register_char_device(char_device_t *device);
  60. dword_t unregister_char_device(char_device_t *device);
  61. dword_t enum_char_devices(char *device_names, size_t *size);
  62. dword_t char_device_read(const char *name, void *buffer, size_t length, size_t *bytes_read);
  63. dword_t char_device_write(const char *name, const void *buffer, size_t length, size_t *bytes_written);
  64. dword_t char_device_ioctl(const char *name, dword_t control_code, const void *in_buffer, size_t in_length, void *out_buffer, size_t out_length);
  65. #endif