2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-02-16 00:47:20 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-04-24 02:37:57 +02:00
|
|
|
// Device is the base class of everything that lives in the /dev directory.
|
|
|
|
|
//
|
|
|
|
|
// To expose a Device to the filesystem, simply pass two unique numbers to the constructor,
|
|
|
|
|
// and then mknod a file in /dev with those numbers.
|
|
|
|
|
//
|
|
|
|
|
// There are two main subclasses:
|
|
|
|
|
// - BlockDevice (random access)
|
|
|
|
|
// - CharacterDevice (sequential)
|
2020-11-02 11:16:01 -07:00
|
|
|
#include <AK/DoublyLinkedList.h>
|
2019-08-18 14:48:15 +03:00
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2021-08-14 11:40:37 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2020-11-02 11:16:01 -07:00
|
|
|
#include <Kernel/Devices/AsyncDeviceRequest.h>
|
2019-07-09 14:46:23 +02:00
|
|
|
#include <Kernel/FileSystem/File.h>
|
2021-08-14 11:40:37 +03:00
|
|
|
#include <Kernel/FileSystem/SysFS.h>
|
2021-07-18 09:10:27 +02:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2019-04-29 04:55:54 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-16 00:47:20 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
class Device : public File {
|
2021-08-14 11:40:37 +03:00
|
|
|
protected:
|
|
|
|
|
enum class State {
|
|
|
|
|
Normal,
|
|
|
|
|
BeingRemoved,
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-16 00:47:20 +01:00
|
|
|
public:
|
2019-04-28 15:02:55 +02:00
|
|
|
virtual ~Device() override;
|
2019-02-16 00:47:20 +01:00
|
|
|
|
|
|
|
|
unsigned major() const { return m_major; }
|
|
|
|
|
unsigned minor() const { return m_minor; }
|
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
virtual String absolute_path(const OpenFileDescription&) const override;
|
2019-08-17 12:17:36 +03:00
|
|
|
virtual String absolute_path() const;
|
2019-02-16 00:47:20 +01:00
|
|
|
|
2021-08-28 22:11:16 +02:00
|
|
|
UserID uid() const { return m_uid; }
|
|
|
|
|
GroupID gid() const { return m_gid; }
|
2019-02-16 00:47:20 +01:00
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
virtual bool is_device() const override { return true; }
|
2021-08-14 11:40:37 +03:00
|
|
|
virtual void before_removing();
|
2019-02-16 09:57:42 +01:00
|
|
|
|
2019-08-18 14:48:15 +03:00
|
|
|
static void for_each(Function<void(Device&)>);
|
|
|
|
|
static Device* get_device(unsigned major, unsigned minor);
|
|
|
|
|
|
2020-11-02 11:16:01 -07:00
|
|
|
void process_next_queued_request(Badge<AsyncDeviceRequest>, const AsyncDeviceRequest&);
|
|
|
|
|
|
|
|
|
|
template<typename AsyncRequestType, typename... Args>
|
2021-09-07 16:40:54 +02:00
|
|
|
KResultOr<NonnullRefPtr<AsyncRequestType>> try_make_request(Args&&... args)
|
2020-11-02 11:16:01 -07:00
|
|
|
{
|
2021-09-07 16:40:54 +02:00
|
|
|
auto request = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AsyncRequestType(*this, forward<Args>(args)...)));
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_requests_lock);
|
2021-02-06 16:59:32 -07:00
|
|
|
bool was_empty = m_requests.is_empty();
|
|
|
|
|
m_requests.append(request);
|
2020-11-02 11:16:01 -07:00
|
|
|
if (was_empty)
|
2021-02-06 16:59:32 -07:00
|
|
|
request->do_start(move(lock));
|
2020-11-02 11:16:01 -07:00
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-16 00:47:20 +01:00
|
|
|
protected:
|
2020-01-22 22:23:50 +01:00
|
|
|
Device(unsigned major, unsigned minor);
|
2021-08-28 22:11:16 +02:00
|
|
|
void set_uid(UserID uid) { m_uid = uid; }
|
|
|
|
|
void set_gid(GroupID gid) { m_gid = gid; }
|
2019-02-16 00:47:20 +01:00
|
|
|
|
2021-08-14 10:13:27 +03:00
|
|
|
static MutexProtected<HashMap<u32, Device*>>& all_devices();
|
2020-01-22 22:23:50 +01:00
|
|
|
|
2019-02-16 00:47:20 +01:00
|
|
|
private:
|
|
|
|
|
unsigned m_major { 0 };
|
|
|
|
|
unsigned m_minor { 0 };
|
2021-08-28 22:11:16 +02:00
|
|
|
UserID m_uid { 0 };
|
|
|
|
|
GroupID m_gid { 0 };
|
2020-11-02 11:16:01 -07:00
|
|
|
|
2021-08-14 11:40:37 +03:00
|
|
|
State m_state { State::Normal };
|
|
|
|
|
|
2021-09-05 10:02:03 -07:00
|
|
|
Spinlock m_requests_lock;
|
2020-11-02 11:16:01 -07:00
|
|
|
DoublyLinkedList<RefPtr<AsyncDeviceRequest>> m_requests;
|
2021-08-14 11:40:37 +03:00
|
|
|
WeakPtr<SysFSDeviceComponent> m_sysfs_component;
|
2019-02-16 00:47:20 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|