1/*
2 * Copyright (c) 2013, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#ifndef DRIVERKIT_H
11#define DRIVERKIT_H
12
13#include <barrelfish/types.h>
14#include <errors/errno.h>
15#include <collections/list.h>
16
17struct bfdriver;
18
19// Generic struc to track all driver instance state.
20struct bfdriver_instance {
21    char* name; //< This is owned by driverkit 'modules.c'.
22    struct bfdriver* driver; //< This is owned by driverkit 'modules.c'.
23    iref_t control; //< This is initialized by driverkit 'dcontrol_service.c'.
24
25    iref_t device; //< Driver state. This is owned by the driver implementation.
26    void* dstate; //< Driver state. This is owned by the driver implementation.
27};
28
29typedef errval_t(*driver_init_fn)(struct bfdriver_instance*, const char*, uint64_t flags, struct capref*, size_t, char**, size_t, iref_t*);
30typedef errval_t(*driver_attach_fn)(struct bfdriver_instance*);
31typedef errval_t(*driver_detach_fn)(struct bfdriver_instance*);
32typedef errval_t(*driver_set_sleep_level_fn)(struct bfdriver_instance*, uint32_t level);
33typedef errval_t(*driver_destroy_fn)(struct bfdriver_instance*);
34
35struct bfdriver {
36    char name[256];
37    driver_init_fn init;
38    driver_attach_fn attach;
39    driver_detach_fn detach;
40    driver_set_sleep_level_fn set_sleep_level;
41    driver_destroy_fn destroy;
42};
43
44errval_t driverkit_create_driver(const char* cls, const char* name, struct capref* caps, size_t caps_len, char** args, size_t args_len, uint64_t flags, iref_t* dev, iref_t* ctrl);
45errval_t driverkit_destroy(const char* name);
46void driverkit_list(struct bfdriver**, size_t*);
47struct bfdriver* driverkit_lookup_cls(const char*);
48
49/** driver domain flounder interface */
50struct domain_instance {
51    uint64_t service_id;
52    collections_listnode* to_spawn;
53    collections_listnode* spawned;
54    struct ddomain_binding *b;
55};
56
57struct driver_instance {
58    char* driver_name;
59    char* inst_name;
60    char** args;
61    size_t cap_idx;
62    struct capref* caps;
63    uint64_t flags;
64    iref_t dev;
65    iref_t control;
66};
67errval_t ddomain_communication_init(iref_t kaluga_iref, uint64_t id);
68errval_t ddomain_controller_init(void);
69struct domain_instance* ddomain_create_domain_instance(uint64_t id);
70struct driver_instance* ddomain_create_driver_instance(char* driver_name, char* inst_name);
71void ddomain_instantiate_driver(struct domain_instance* di, struct driver_instance* drv);
72void ddomain_free_driver_inst(void* arg);
73void ddomain_free_domain_inst(void* arg);
74errval_t ddomain_driver_add_cap(struct driver_instance* drv, struct capref cap);
75void ddomain_wait_for_id(void);
76
77/** driver control flounder interface */
78errval_t dcontrol_service_init(struct bfdriver_instance* bfi, struct waitset* ws);
79
80errval_t map_device_register(lpaddr_t, size_t, lvaddr_t*);
81errval_t map_device_cap(struct capref, lvaddr_t *);
82
83errval_t driverkit_local_service_register(char* name, void* tbl);
84void* driverkit_local_service_lookup(char* name);
85
86#define __bfdrivers		__attribute__((__section__(".bfdrivers")))
87#define __visible       __attribute__((__externally_visible__))
88#define __waligned       __attribute__((__aligned__(sizeof(size_t))))
89#define __used          __attribute__((__used__))
90#define ___PASTE(a,b) a##b
91#define __PASTE(a,b) ___PASTE(a,b)
92#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
93
94#define DEFINE_MODULE(name, init_fn, attach_fn, detach_fn, sleep_fn, destroy_fn) \
95    struct bfdriver __UNIQUE_ID(name)               \
96        __used                                      \
97        __visible                                   \
98        __bfdrivers                                 \
99        __waligned =  {                             \
100        #name,                                      \
101        init_fn,                                    \
102        attach_fn,                                  \
103        detach_fn,                                  \
104        sleep_fn,                                   \
105        destroy_fn                                  \
106    };
107
108
109#endif // DRIVERKIT_H
110