AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
dev.h
Go to the documentation of this file.
1#ifndef _DEVICE_H
2#define _DEVICE_H
3
4#include <core/system.h>
5
6struct devid;
7struct dev;
8
9#include <mm/vm.h>
10#include <fs/vfs.h>
11#include <sys/proc.h>
12#include <fs/iobuf.h>
13
14/*
15 * The device layer (dev/kdev.c): devices, by their major number, block or
16 * character, and the operations the rest of the kernel calls on them. See
17 * docs/kernel/devices.md.
18 */
19
26
28struct dev {
29 char *name;
30
31 int (*probe)(void);
32
33 /* a character device's: `io->resid` bytes at `offset`, through the iobuf */
34 ssize_t (*read) (struct devid *dev, off_t offset, struct iobuf *io);
35 ssize_t (*write)(struct devid *dev, off_t offset, struct iobuf *io);
36
37 /* a block device's: `count` blocks from block `lba`, in kernel memory
38 * (kdev_bread() and kdev_bwrite() turn them into bytes) */
39 ssize_t (*bread) (struct devid *dev, off_t lba, size_t count, void *buf);
40 ssize_t (*bwrite)(struct devid *dev, off_t lba, size_t count, void *buf);
41
42 int (*ioctl)(struct devid *dev, int request, void *argp);
43 int (*map) (struct devid *dev, struct vm_space *vm_space, struct vm_entry *vm_entry);
44
45 struct fops fops;
46
47 struct dev *(*mux)(struct devid *dev); /* the driver for a minor number */
48 size_t (*getbs)(struct devid *dev); /* a block device's block size */
49 int (*sync)(void); /* writes a block device's cached writes */
50};
51
52/* Registering devices, and the operations on them (dev/kdev.c). */
53void kdev_init(void);
56int kdev_sync(void);
57
58ssize_t kdev_read(struct devid *dd, off_t offset, struct iobuf *io);
59ssize_t kdev_write(struct devid *dd, off_t offset, struct iobuf *io);
60int kdev_ioctl(struct devid *dd, int request, void *argp);
61int kdev_map(struct devid *dd, struct vm_space *vm_space, struct vm_entry *vm_entry);
62
63struct fops *kdev_fops(struct devid *dd);
64int kdev_file_open(struct devid *dd, struct file *file);
65ssize_t kdev_file_read(struct devid *dd, struct file *file, struct iobuf *io);
66ssize_t kdev_file_write(struct devid *dd, struct file *file, struct iobuf *io);
67off_t kdev_file_lseek(struct devid *dd, struct file *file, off_t offset, int whence);
68ssize_t kdev_file_close(struct devid *dd, struct file *file);
69int kdev_file_ioctl(struct devid *dd, struct file *file, int request, void *argp);
70int kdev_file_can_read(struct devid *dd, struct file * file, size_t size);
71int kdev_file_can_write(struct devid *dd, struct file * file, size_t size);
72int kdev_file_eof(struct devid *dd, struct file *file);
73
74/* A device number from its major and minor numbers, and back; a vnode's
75 * device as a devid. */
76#define DEV(major, minor) ((dev_t)(((major) & 0xFF) << 8) | ((minor) & 0xFF))
77#define DEV_MAJOR(dev) ((devid_t)(((dev) >> 8) & 0xFF))
78#define DEV_MINOR(dev) ((devid_t)(((dev) >> 0) & 0xFF))
79#define VNODE_DEV(vnode) ((struct devid){.type = (vnode)->mode & S_IFMT, .major = DEV_MAJOR((vnode)->rdev), .minor = DEV_MINOR((vnode)->rdev)})
80
81/* Some drivers that are named elsewhere. */
82extern struct dev ttydev;
83extern struct dev pcidev;
84extern struct dev atadev;
85extern struct dev fbdev;
86extern struct dev rddev;
87
88#endif
struct dev atadev
Definition ata.c:592
int kdev_map(struct devid *dd, struct vm_space *vm_space, struct vm_entry *vm_entry)
Maps a device into an address space (mmap() of a framebuffer).
Definition kdev.c:361
ssize_t kdev_file_close(struct devid *dd, struct file *file)
Closes an open device file, through its driver's file operations.
Definition kdev.c:468
off_t kdev_file_lseek(struct devid *dd, struct file *file, off_t offset, int whence)
Moves an open device file's offset, through its driver's file operations.
Definition kdev.c:447
void kdev_blkdev_register(devid_t major, struct dev *dev)
Registers a driver as the block device of a major number.
Definition kdev.c:605
struct fops * kdev_fops(struct devid *dd)
Returns the file operations of a device.
Definition kdev.c:52
int kdev_file_can_write(struct devid *dd, struct file *file, size_t size)
Asks a device's driver whether a write wouldn't have to wait.
Definition kdev.c:534
int kdev_file_eof(struct devid *dd, struct file *file)
Asks a device's driver whether a read that found no data is at the end of the file.
Definition kdev.c:555
ssize_t kdev_file_write(struct devid *dd, struct file *file, struct iobuf *io)
Writes to an open device file, through its driver's file operations.
Definition kdev.c:423
ssize_t kdev_file_read(struct devid *dd, struct file *file, struct iobuf *io)
Reads an open device file, through its driver's file operations.
Definition kdev.c:402
int kdev_file_ioctl(struct devid *dd, struct file *file, int request, void *argp)
Passes an ioctl() on an open device file to its driver's file operations.
Definition kdev.c:492
ssize_t kdev_read(struct devid *dd, off_t offset, struct iobuf *io)
Reads from a device: a character device through its driver, a block device in blocks.
Definition kdev.c:297
void kdev_chrdev_register(devid_t major, struct dev *dev)
Registers a driver as the character device of a major number.
Definition kdev.c:575
int kdev_file_open(struct devid *dd, struct file *file)
Opens a device, through its driver's file operations.
Definition kdev.c:381
int kdev_sync(void)
Writes what the block cache holds that was written, and what the block devices hold in their write ca...
Definition kdev.c:587
int kdev_ioctl(struct devid *dd, int request, void *argp)
Passes an ioctl() to a device's driver.
Definition kdev.c:340
int kdev_file_can_read(struct devid *dd, struct file *file, size_t size)
Asks a device's driver whether a read wouldn't have to wait.
Definition kdev.c:513
ssize_t kdev_write(struct devid *dd, off_t offset, struct iobuf *io)
Writes to a device: a character device through its driver, a block device in blocks.
Definition kdev.c:319
void kdev_init()
Starts the device layer, at boot.
Definition kdev.c:615
struct dev pcidev
Definition pci.c:225
struct dev rddev
Definition ramdisk.c:93
__SIZE_TYPE__ size_t
Definition stddef.h:7
A device driver: its operations, any of which may be NULL.
Definition dev.h:28
ssize_t(*) read(struct devid *dev, off_t offset, struct iobuf *io)
Definition dev.h:34
int(*) map(struct devid *dev, struct vm_space *vm_space, struct vm_entry *vm_entry)
Definition dev.h:43
ssize_t(*) write(struct devid *dev, off_t offset, struct iobuf *io)
Definition dev.h:35
char * name
Definition dev.h:29
struct fops fops
Definition dev.h:45
size_t(*) getbs(struct devid *dev)
Definition dev.h:48
int(*) sync(void)
Definition dev.h:49
int(*) ioctl(struct devid *dev, int request, void *argp)
Definition dev.h:42
ssize_t(*) bread(struct devid *dev, off_t lba, size_t count, void *buf)
Definition dev.h:39
int(*) probe(void)
Definition dev.h:31
ssize_t(*) bwrite(struct devid *dev, off_t lba, size_t count, void *buf)
Definition dev.h:40
Which device: its kind (S_IFCHR or S_IFBLK) and numbers.
Definition dev.h:21
mode_t type
Definition dev.h:22
devid_t minor
Definition dev.h:24
devid_t major
Definition dev.h:23
A framebuffer: its driver, its screen's information, its operations.
Definition fbdev.h:23
An open file (POSIX's "open file description", fs/file.c).
Definition vfs.h:157
An open file's operations.
Definition vfs.h:28
An iobuf.
Definition iobuf.h:35
A mapping (map entry): a range of an address space, its permissions, and where its pages come from.
Definition vm.h:66
An address space: its mappings, and its page tables.
Definition vm.h:25
struct dev ttydev
Definition tty.c:121
uint8_t devid_t
Definition types.h:17
long int ssize_t
Definition types.h:12
long int off_t
Definition types.h:10
uint32_t mode_t
Definition types.h:15