Kernel: Make device generate their own names

Besides removing the monolithic DevFSDeviceInode::determine_name()
method, being able to determine a device's name inside the /dev
hierarchy outside of DevFS has its uses.
This commit is contained in:
Jean-Baptiste Boric 2021-01-21 18:49:56 +01:00 committed by Andreas Kling
parent a2601e1308
commit f64e287b82
Notes: sideshowbarker 2024-07-18 22:57:36 +09:00
29 changed files with 76 additions and 69 deletions

View file

@ -53,6 +53,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0666; }
virtual String device_name() const override { return "console"; }
private:
CircularQueue<char, 16384> m_logbuffer;

View file

@ -192,6 +192,11 @@ KResultOr<Region*> BXVGADevice::mmap(Process& process, FileDescription&, Virtual
shared);
}
String BXVGADevice::device_name() const
{
return String::formatted("fb{}", minor());
}
int BXVGADevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
{
REQUIRE_PROMISE(video);

View file

@ -46,6 +46,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0660; }
virtual String device_name() const override;
private:
virtual const char* class_name() const override { return "BXVGA"; }

View file

@ -58,6 +58,7 @@ public:
uid_t gid() const { return m_gid; }
virtual mode_t required_mode() const = 0;
virtual String device_name() const = 0;
virtual bool is_device() const override { return true; }
virtual bool is_disk_device() const { return false; }

View file

@ -38,6 +38,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0600; }
virtual String device_name() const override { return "full"; }
private:
// ^CharacterDevice

View file

@ -76,6 +76,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0440; }
virtual String device_name() const override { return "keyboard"; }
private:
// ^IRQHandler

View file

@ -114,4 +114,9 @@ int MBVGADevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
};
}
String MBVGADevice::device_name() const
{
return String::formatted("fb{}", minor());
}
}

View file

@ -45,6 +45,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0660; }
virtual String device_name() const override;
private:
virtual const char* class_name() const override { return "MBVGA"; }

View file

@ -41,6 +41,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0666; }
virtual String device_name() const override { return "null"; }
private:
// ^CharacterDevice

View file

@ -63,6 +63,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0440; }
virtual String device_name() const override { return "mouse"; }
private:
// ^IRQHandler

View file

@ -38,6 +38,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0666; }
virtual String device_name() const override { return "random"; }
private:
// ^CharacterDevice

View file

@ -55,6 +55,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0220; }
virtual String device_name() const override { return "audio"; }
private:
// ^IRQHandler

View file

@ -87,6 +87,11 @@ KResultOr<size_t> SerialDevice::write(FileDescription&, size_t, const UserOrKern
return (size_t)nread;
}
String SerialDevice::device_name() const
{
return String::formatted("ttyS{}", minor() - 64);
}
void SerialDevice::initialize()
{
set_interrupts(0);

View file

@ -125,6 +125,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0620; }
virtual String device_name() const override;
private:
// ^CharacterDevice

View file

@ -38,6 +38,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0666; }
virtual String device_name() const override { return "zero"; }
private:
// ^CharacterDevice

View file

@ -350,79 +350,12 @@ KResult DevFSDeviceInode::chown(uid_t uid, gid_t gid)
String DevFSDeviceInode::name() const
{
LOCKER(m_lock);
if (m_cached_name.is_null() || m_cached_name.is_empty())
const_cast<DevFSDeviceInode&>(*this).m_cached_name = determine_name();
const_cast<DevFSDeviceInode&>(*this).m_cached_name = m_attached_device->device_name();
return m_cached_name;
}
String DevFSDeviceInode::determine_name() const
{
LOCKER(m_lock);
if (m_attached_device->is_character_device()) {
switch (m_attached_device->major()) {
case 85:
if (m_attached_device->minor() == 1)
return "keyboard";
ASSERT_NOT_REACHED();
case 10:
if (m_attached_device->minor() == 1)
return "mouse";
ASSERT_NOT_REACHED();
case 42:
if (m_attached_device->minor() == 42)
return "audio";
ASSERT_NOT_REACHED();
case 1:
switch (m_attached_device->minor()) {
case 8:
return "random";
case 3:
return "null";
case 5:
return "zero";
case 7:
return "full";
default:
ASSERT_NOT_REACHED();
}
case 5:
if (m_attached_device->minor() == 1)
return "console";
if (m_attached_device->minor() == 2)
return "ptmx";
ASSERT_NOT_REACHED();
case 4:
if (m_attached_device->minor() >= 64)
return String::formatted("ttyS{}", m_attached_device->minor() - 64);
return String::formatted("tty{}", m_attached_device->minor());
default:
ASSERT_NOT_REACHED();
}
} else {
switch (m_attached_device->major()) {
case 29:
return String::formatted("fb{}", m_attached_device->minor());
case 3: {
size_t drive_index = (u8)'a' + m_attached_device->minor();
char drive_letter = (u8)drive_index;
return String::format("hd%c", drive_letter);
}
case 6: {
return String::formatted("ramdisk{}", m_attached_device->minor());
}
case 100:
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
size_t drive_index = (u8)'a' + (m_attached_device->minor() / 16);
char drive_letter = (u8)drive_index;
return String::formatted("hd{:c}{}", drive_letter, m_attached_device->minor() + 1);
}
}
ASSERT_NOT_REACHED();
}
ssize_t DevFSDeviceInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
{
LOCKER(m_lock);

View file

@ -65,6 +65,13 @@ void PATADiskDevice::start_request(AsyncBlockDeviceRequest& request)
m_channel.start_request(request, use_dma, is_slave());
}
String PATADiskDevice::device_name() const
{
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
size_t drive_index = minor() / 16;
return String::formatted("hd{:c}{}", 'a' + drive_index, minor() + 1);
}
size_t PATADiskDevice::max_addressable_block() const
{
return m_cylinders * m_heads * m_sectors_per_track;

View file

@ -61,6 +61,7 @@ public:
// ^BlockDevice
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String device_name() const override;
private:
PATADiskDevice(const IDEController&, IDEChannel&, DriveType, u8, u8, u8, int major, int minor);

View file

@ -102,6 +102,13 @@ bool DiskPartition::can_write(const FileDescription& fd, size_t offset) const
return m_device->can_write(fd, offset + adjust);
}
String DiskPartition::device_name() const
{
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
size_t partition_index = minor() % 16;
return String::formatted("{}{}", m_device->device_name(), partition_index + 1);
}
const char* DiskPartition::class_name() const
{
return "DiskPartition";

View file

@ -47,6 +47,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0600; }
virtual String device_name() const override;
const DiskPartitionMetadata& metadata() const;

View file

@ -82,4 +82,11 @@ void RamdiskDevice::start_request(AsyncBlockDeviceRequest& request)
}
}
String RamdiskDevice::device_name() const
{
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
size_t drive_index = minor() / 16;
return String::formatted("ramdisk{}", drive_index);
}
}

View file

@ -50,6 +50,7 @@ public:
// ^DiskDevice
virtual const char* class_name() const override;
virtual String device_name() const override;
bool is_slave() const;

View file

@ -141,4 +141,9 @@ String MasterPTY::absolute_path(const FileDescription&) const
return String::formatted("ptm:{}", m_pts_name);
}
String MasterPTY::device_name() const
{
return String::formatted("{}", minor());
}
}

View file

@ -50,6 +50,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0640; }
virtual String device_name() const override;
private:
// ^CharacterDevice

View file

@ -57,6 +57,7 @@ public:
// ^Device
virtual mode_t required_mode() const override { return 0666; }
virtual String device_name() const override { return "ptmx"; }
private:
// ^CharacterDevice

View file

@ -106,6 +106,11 @@ KResult SlavePTY::close()
return KSuccess;
}
String SlavePTY::device_name() const
{
return String::formatted("{}", minor());
}
FileBlockCondition& SlavePTY::block_condition()
{
return m_master->block_condition();

View file

@ -57,6 +57,9 @@ private:
virtual const char* class_name() const override { return "SlavePTY"; }
virtual KResult close() override;
// ^Device
virtual String device_name() const override;
friend class MasterPTY;
SlavePTY(MasterPTY&, unsigned index);

View file

@ -338,6 +338,11 @@ void VirtualConsole::emit(const u8* data, size_t size)
TTY::emit(data[i]);
}
String VirtualConsole::device_name() const
{
return String::formatted("tty{}", minor());
}
void VirtualConsole::echo(u8 ch)
{
if (should_echo_input()) {

View file

@ -69,6 +69,9 @@ private:
// ^CharacterDevice
virtual const char* class_name() const override { return "VirtualConsole"; }
// ^Device
virtual String device_name() const override;
void set_active(bool);
void flush_vga_cursor();