2023-04-02 21:03:23 +01:00
|
|
|
#ifndef SOCKS_DEVICE_H_
|
|
|
|
|
#define SOCKS_DEVICE_H_
|
|
|
|
|
|
|
|
|
|
#include <socks/queue.h>
|
|
|
|
|
#include <socks/status.h>
|
2023-05-06 19:48:14 +01:00
|
|
|
#include <socks/object.h>
|
2023-05-10 20:33:40 +01:00
|
|
|
#include <socks/ringbuffer.h>
|
2023-04-02 21:03:23 +01:00
|
|
|
|
|
|
|
|
struct device;
|
2023-05-10 20:33:40 +01:00
|
|
|
struct input_event;
|
2023-04-02 21:03:23 +01:00
|
|
|
|
2023-05-08 18:18:34 +01:00
|
|
|
#define DEV_NAME_MAX OBJECT_NAME_MAX
|
|
|
|
|
|
2023-05-10 20:33:40 +01:00
|
|
|
#define INPUT_DEVICE_EVENT_QUEUE_SIZE 128
|
2023-05-11 21:19:00 +01:00
|
|
|
#define INPUT_DEVICE_MAX 4096
|
2023-05-10 20:33:40 +01:00
|
|
|
|
2023-04-02 21:03:23 +01:00
|
|
|
#define BLOCK_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_BLOCK ? &(dev)->blk : NULL);
|
|
|
|
|
#define CHAR_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_CHAR ? &(dev)->chr : NULL);
|
|
|
|
|
#define NET_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_NET ? &(dev)->net : NULL);
|
|
|
|
|
#define INPUT_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_INPUT ? &(dev)->input : NULL);
|
2023-04-03 16:59:14 +01:00
|
|
|
#define BUS_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_BUS ? &(dev)->bus : NULL);
|
2023-04-02 21:03:23 +01:00
|
|
|
|
|
|
|
|
enum device_type {
|
|
|
|
|
DEV_TYPE_BLOCK,
|
|
|
|
|
DEV_TYPE_CHAR,
|
|
|
|
|
DEV_TYPE_NET,
|
|
|
|
|
DEV_TYPE_INPUT,
|
2023-04-03 16:59:14 +01:00
|
|
|
DEV_TYPE_BUS,
|
2023-04-02 21:03:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct block_device_ops {
|
2023-05-14 21:11:32 +01:00
|
|
|
kern_status_t(*read_blocks)(struct device *, size_t, size_t *, void *, socks_flags_t);
|
|
|
|
|
kern_status_t(*write_blocks)(struct device *, size_t, size_t *, const void *, socks_flags_t);
|
2023-04-02 21:03:23 +01:00
|
|
|
kern_status_t(*ioctl)(struct device *, unsigned int, void *);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct net_device_ops {
|
|
|
|
|
kern_status_t(*online)(struct device *);
|
|
|
|
|
kern_status_t(*offline)(struct device *);
|
|
|
|
|
kern_status_t(*transmit)(struct device *, const void *, size_t);
|
|
|
|
|
kern_status_t(*ioctl)(struct device *, unsigned int, void *);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct char_device_ops {
|
2023-05-14 21:11:32 +01:00
|
|
|
kern_status_t(*read)(struct device *, size_t, size_t *, void *, socks_flags_t);
|
|
|
|
|
kern_status_t(*write)(struct device *, size_t, size_t *, const void *, socks_flags_t);
|
2023-04-02 21:03:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct input_device_ops {
|
|
|
|
|
kern_status_t(*ioctl)(struct device *, unsigned int, void *);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct bus_device_ops {
|
|
|
|
|
kern_status_t(*scan)(struct device *);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct block_device {
|
|
|
|
|
struct block_device_ops *b_ops;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct char_device {
|
|
|
|
|
struct char_device_ops *c_ops;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct net_device {
|
|
|
|
|
struct net_device_ops *n_ops;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct input_device {
|
|
|
|
|
struct input_device_ops *i_ops;
|
2023-05-11 21:19:00 +01:00
|
|
|
unsigned int i_id;
|
2023-05-10 20:33:40 +01:00
|
|
|
struct ringbuffer i_events;
|
2023-04-02 21:03:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct bus_device {
|
|
|
|
|
struct bus_device_ops *b_ops;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct device {
|
2023-05-06 19:48:14 +01:00
|
|
|
struct object dev_base;
|
2023-04-02 21:03:23 +01:00
|
|
|
enum device_type dev_type;
|
|
|
|
|
struct device *dev_parent;
|
2023-04-12 20:17:11 +01:00
|
|
|
struct queue dev_children;
|
|
|
|
|
struct queue_entry dev_childent;
|
2023-05-08 18:18:34 +01:00
|
|
|
char dev_name[DEV_NAME_MAX];
|
2023-04-02 21:03:23 +01:00
|
|
|
|
|
|
|
|
void *dev_priv;
|
|
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
struct block_device blk;
|
|
|
|
|
struct char_device chr;
|
|
|
|
|
struct net_device net;
|
|
|
|
|
struct input_device input;
|
|
|
|
|
struct bus_device bus;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
extern kern_status_t device_init(void);
|
2023-05-08 18:18:34 +01:00
|
|
|
extern struct device *root_device(void);
|
|
|
|
|
extern struct device *misc_device(void);
|
2023-04-02 21:03:23 +01:00
|
|
|
|
2023-04-03 16:59:14 +01:00
|
|
|
extern struct device *device_alloc(void);
|
2023-05-08 18:18:34 +01:00
|
|
|
static inline void device_lock_irqsave(struct device *dev, unsigned long *flags)
|
|
|
|
|
{
|
|
|
|
|
object_lock(&dev->dev_base, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void device_unlock_irqrestore(struct device *dev, unsigned long flags)
|
|
|
|
|
{
|
|
|
|
|
object_unlock(&dev->dev_base, flags);
|
|
|
|
|
}
|
2023-04-03 16:59:14 +01:00
|
|
|
|
2023-05-14 21:11:32 +01:00
|
|
|
extern kern_status_t device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags);
|
|
|
|
|
extern kern_status_t device_write(struct device *dev, const void *buf, size_t size, size_t *bytes_written, socks_flags_t flags);
|
2023-05-10 20:33:40 +01:00
|
|
|
|
|
|
|
|
extern struct device *cast_to_device(struct object *obj);
|
|
|
|
|
|
2023-04-02 21:03:23 +01:00
|
|
|
extern struct char_device *char_device_create(void);
|
|
|
|
|
extern struct block_device *block_device_create(void);
|
|
|
|
|
extern struct net_device *net_device_create(void);
|
|
|
|
|
extern struct input_device *input_device_create(void);
|
|
|
|
|
extern struct bus_device *bus_device_create(void);
|
|
|
|
|
|
|
|
|
|
static inline struct device *char_device_base(struct char_device *dev)
|
|
|
|
|
{
|
|
|
|
|
return (struct device *)((char *)dev - offsetof(struct device, chr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct device *block_device_base(struct block_device *dev)
|
|
|
|
|
{
|
|
|
|
|
return (struct device *)((char *)dev - offsetof(struct device, blk));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct device *net_device_base(struct net_device *dev)
|
|
|
|
|
{
|
|
|
|
|
return (struct device *)((char *)dev - offsetof(struct device, net));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct device *input_device_base(struct input_device *dev)
|
|
|
|
|
{
|
|
|
|
|
return (struct device *)((char *)dev - offsetof(struct device, input));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct device *bus_device_base(struct bus_device *dev)
|
|
|
|
|
{
|
|
|
|
|
return (struct device *)((char *)dev - offsetof(struct device, bus));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern kern_status_t device_register(struct device *dev, struct device *parent);
|
|
|
|
|
|
2023-05-10 20:33:40 +01:00
|
|
|
extern kern_status_t input_device_report_event(struct input_device *dev, const struct input_event *ev, bool noblock);
|
2023-05-14 21:11:32 +01:00
|
|
|
extern kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags);
|
2023-05-11 21:19:00 +01:00
|
|
|
extern kern_status_t input_device_generate_name(struct input_device *dev);
|
2023-05-10 20:33:40 +01:00
|
|
|
|
2023-04-02 21:03:23 +01:00
|
|
|
#endif
|