2021-02-28 02:18:39 +01:00
|
|
|
/* See the end of this file for copyright, license, and warranty information. */
|
2021-02-03 04:01:27 +01:00
|
|
|
|
|
|
|
#include <ardix/device.h>
|
|
|
|
#include <ardix/kent.h>
|
|
|
|
#include <ardix/list.h>
|
|
|
|
#include <ardix/malloc.h>
|
|
|
|
#include <ardix/types.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
struct kent *devices_kent = NULL;
|
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
static void devices_destroy(struct kent *kent)
|
2021-02-17 15:01:15 +01:00
|
|
|
{
|
2021-02-28 01:28:07 +01:00
|
|
|
/* should never be executed because the root devices kent is immortal */
|
|
|
|
free(kent);
|
2021-02-17 15:01:15 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 04:01:27 +01:00
|
|
|
/** Initialize the devices subsystem. */
|
|
|
|
int devices_init(void)
|
|
|
|
{
|
|
|
|
if (devices_kent != NULL)
|
|
|
|
return -EEXIST;
|
|
|
|
|
|
|
|
devices_kent = malloc(sizeof(*devices_kent));
|
|
|
|
if (devices_kent == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
devices_kent->parent = kent_root;
|
2021-07-31 15:58:29 +02:00
|
|
|
devices_kent->destroy = devices_destroy;
|
2021-02-17 15:01:15 +01:00
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
return kent_init(devices_kent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_destroy(struct kent *kent)
|
|
|
|
{
|
|
|
|
struct device *dev = kent_to_device(kent);
|
|
|
|
free(dev);
|
2021-02-03 04:01:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
int device_init(struct device *dev)
|
2021-02-03 04:01:27 +01:00
|
|
|
{
|
2021-07-31 15:58:29 +02:00
|
|
|
if (dev->kent.destroy == NULL)
|
|
|
|
dev->kent.destroy = device_destroy;
|
2021-02-28 01:28:07 +01:00
|
|
|
if (dev->kent.parent == NULL)
|
|
|
|
dev->kent.parent = devices_kent;
|
|
|
|
|
2021-08-01 04:22:09 +02:00
|
|
|
mutex_init(&dev->lock);
|
2021-02-28 01:28:07 +01:00
|
|
|
return kent_init(&dev->kent);
|
2021-02-03 04:01:27 +01:00
|
|
|
}
|
|
|
|
|
2021-08-04 03:29:52 +02:00
|
|
|
static void device_kevent_destroy(struct kent *kent)
|
|
|
|
{
|
|
|
|
struct kevent *event = container_of(kent, struct kevent, kent);
|
|
|
|
struct device_kevent *device_kevent = container_of(event, struct device_kevent, event);
|
|
|
|
free(device_kevent);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct device_kevent *device_kevent_create(struct device *device, enum device_channel channel)
|
|
|
|
{
|
|
|
|
struct device_kevent *event = malloc(sizeof(*event));
|
|
|
|
if (event == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
event->channel = channel;
|
|
|
|
event->event.kind = KEVENT_DEVICE;
|
|
|
|
|
|
|
|
event->event.kent.parent = &device->kent;
|
|
|
|
event->event.kent.destroy = device_kevent_destroy;
|
|
|
|
int err = kent_init(&event->event.kent);
|
|
|
|
if (err) {
|
|
|
|
free(event);
|
|
|
|
event = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:46:51 +02:00
|
|
|
void device_kevent_create_and_dispatch(struct device *device, enum device_channel channel)
|
|
|
|
{
|
|
|
|
struct device_kevent *event = device_kevent_create(device, channel);
|
|
|
|
if (event != NULL)
|
|
|
|
kevent_dispatch(&event->event);
|
|
|
|
}
|
|
|
|
|
2021-02-03 04:01:27 +01:00
|
|
|
/*
|
2021-02-28 02:18:39 +01:00
|
|
|
* This file is part of Ardix.
|
|
|
|
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
2021-02-03 04:01:27 +01:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix is non-violent software: you may only use, redistribute,
|
|
|
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
|
|
|
* the LICENSE file in the source code root directory or at
|
|
|
|
* <https://git.pixie.town/thufie/CNPL>.
|
2021-02-03 04:01:27 +01:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
|
|
* permitted by applicable law. See the CNPLv6+ for details.
|
2021-02-03 04:01:27 +01:00
|
|
|
*/
|