#ifndef _KERNEL_DEVICE_DEVICE_H
#define _KERNEL_DEVICE_DEVICE_H

#include <libk/hash.h>
#include <libk/list.h>
#include <libk/std.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <sync/spin_lock.h>
#include <sys/smp.h>

#define DEVICES_MAX 1024

#define device_op1(d, op, proc, rctx, a1, a2, a3, a4, ...)                                         \
  (d)->ops[(op)]((d), (proc), (rctx), (void*)(a1), (void*)(a2), (void*)(a3), (void*)(a4))

#define device_op(d, op, proc, rctx, ...)                                                          \
  device_op1 (d, op, proc, rctx, __VA_ARGS__, NULL, NULL, NULL, NULL)

struct device;

typedef int (*device_op_func_t) (struct device* device, struct proc*, struct reschedule_ctx* rctx,
                                 void* a1, void* a2, void* a3, void* a4);

typedef bool (*device_init_func_t) (struct device* device, void* arg);
typedef void (*device_fini_func_t) (struct device* device);

struct device {
  char key[0x100];
  device_op_func_t ops[32];
  spin_lock_t lock;
  struct hash_node_link device_table_link;
  device_init_func_t init;
  device_fini_func_t fini;
  void* udata;
};

struct device* device_create (const char* key, device_op_func_t* ops, size_t ops_len,
                              device_init_func_t init, device_fini_func_t fini, void* arg);

struct device* device_find (const char* key);

void devices_init (void);

#endif // _KERNEL_DEVICE_DEVICE_H
