usb: plug and play -- hot-plug, unplug, every interface, real mice

Devices can now arrive and leave after boot. A "usbhotplug" kernel thread
handles root-port changes, which it is told about by Port Status Change
events and also checks for every quarter second. It also handles changes
behind hubs, which each hub reports on its status-change endpoint. An
unplugged device has its endpoints closed and any transfer in flight
failed. Then its drivers' disconnect hooks run, the slot is disabled and
its memory is freed. Everything behind a hub that goes goes with it. This
needed the interrupt handler to file command and endpoint-0 completions
for their waiters, so that enumeration can run while keyboards interrupt.

On real hardware: the controller is taken from the firmware through USB
Legacy Support before reset, with SMIs off. On Intel 7/8/9 series parts
the ports are switched over from EHCI, which is where firmware leaves
them.

Every interface of a device is offered to the class drivers, not just
the first, so a receiver's mouse on interface 1 gets a driver. Configure
Endpoint now copies the slot context from the controller. Before, it
rebuilt it and lost the route string and TT fields of devices behind
hubs.

HID: pointers are read in report protocol from their report descriptor.
That covers buttons, X/Y and wheel, relative or absolute, and report IDs.
Boot protocol is kept as the fallback for mice and for keyboards.
Non-pointer HID interfaces are declined rather than treated as tablets.
USB keyboards get software key repeat. Mass storage handles up to four
sticks, each unregistered from the block layer when it is pulled out,
along with its cached blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HH7xukt4aaiGj1nQEAoQf1
This commit is contained in:
Nexoniarz 2026-10-22 11:38:13 +02:00
commit fa7669254e
13 changed files with 1367 additions and 240 deletions

View file

@ -114,6 +114,21 @@ bdevflush(uint dev)
// Look through buffer cache for block on device dev.
// If not found, allocate a buffer.
// In either case, return locked buffer.
// Forget every cached block of a device that is no longer there: clean and
// invalid, so it is neither served to a reader nor written back on eviction.
// A buffer somebody is holding is left to them -- they are about to find out.
void
binval(uint dev)
{
struct buf *b;
acquire(&bcache.lock);
for(b = bcache.head.next; b != &bcache.head; b = b->next)
if(b->dev == dev && b->refcnt == 0)
b->flags = 0;
release(&bcache.lock);
}
static struct buf*
bget(uint dev, uint64 blockno)
{

View file

@ -222,6 +222,27 @@ blkdev_register(const char *name, const struct blkdev_ops *ops, void *priv,
return dev;
}
void
blkdev_unregister(int dev)
{
struct blkdev *d = lookup(dev);
if (!d)
return;
int disk = dev - BLKDEV_FIRST;
acquire(&blklock);
for (int i = 0; i < NBLKDEV; i++) {
if (devs[i].used && (i == disk || devs[i].parent == disk)) {
devs[i].used = 0;
kinfo("blkdev: %s removed\n", devs[i].name);
release(&blklock);
binval(BLKDEV_FIRST + i);
acquire(&blklock);
}
}
release(&blklock);
}
// --- I/O ---------------------------------------------------------------------
int