SchOS/include/dev/kdev.h
Nexoniarz 8d836c66b2 PCI and USB devices in the /sys/devices tree, with uevents and USB hot-plug
PCI devices live at /sys/devices/pci0000:00/<addr> and USB devices under the
xHCI controller that carries their bus, below that bus's root hub usbN;
/sys/bus/pci/devices and /sys/bus/usb/devices are symlinks into it, and every
device has its subsystem and driver links, which is how udev learns SUBSYSTEM
and DRIVER. Writing to a device's uevent now announces it with MODALIAS,
PCI_ID, PRODUCT and the rest, so udevadm trigger covers them, and the USB core
sends add when a device is attached and remove as it goes. The root hub is
announced when the controller comes up, before anything is plugged in.

Checked under QEMU: lspci and udevadm info through the links, udevadm monitor
showing the kernel and udev add event for device_add usb-kbd/usb-mouse and
the remove event for device_del, and a triggered add for usb1 and 1-5.

Known limits: the bus-to-controller mapping is a convention (bus N is the Nth
xHCI in PCI order); PCI bridges are not parents; USB interfaces and
/dev/bus/usb nodes are absent; busybox's lsusb reads /dev/bus/usb and prints
nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EVRhxi9zpdW2uDUN4FVcao
2026-10-26 15:51:28 +01:00

46 lines
1.8 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
//
// The devices the kernel implements itself, as /sys and udev see them.
//
// /dev holds them already (kernel/fs/devfs.c). This is the other half of what
// udev needs: for each node a place in /sys/devices, the class and dev/ links
// that lead back to it, the facts udev's rules read (MAJOR, MINOR, DEVNAME,
// input capabilities), and a way to announce it on the uevent netlink socket.
#pragma once
#include "core/types.h"
#define KDEV_NAME 24
#define KDEV_DEVNAME 32
#define KDEV_BLK0 256 // kdev indexes from here are block devices
enum { KD_NONE, KD_KEYBOARD, KD_MOUSE };
struct kdev {
char subsys[16]; // class: input, mem, tty, graphics, block
char name[KDEV_NAME]; // directory name under the parent
char devname[KDEV_DEVNAME]; // path below /dev, "" for a device with no node
short major, minor;
int block;
int parent; // kdev index of the device it hangs off, or -1
int cap; // KD_*: what an input device can do
};
int kdev_nsubsys(void);
const char *kdev_subsys(int s);
// Index of the Nth device that exists, 0.. until -1.
int kdev_nth(int n);
int kdev_get(int i, struct kdev *k);
// The numbers stat() shows for its node, which are what /sys must show too.
void kdev_numbers(const struct kdev *k, int *maj, int *min);
// "devices/virtual/input/input0/event0": where it lives below /sys.
int kdev_path(int i, char *buf, int cap);
// Announce a device on the uevent socket: action is "add", "remove", "change".
void kdev_uevent(int i, const char *action);
// The next SEQNUM: events from every source (kdev, PCI, USB) share one count.
uint kdev_seqnum(void);
// What reading a device's `uevent` file says.
int kdev_uevent_text(int i, char *buf, int cap);