1 /*
2  * Copyright (c) 2019, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef DEV_H
8 #define DEV_H
9 
10 #include <cdefs.h>
11 #include <lib/debugfs.h>
12 #include <stddef.h>
13 
14 /* FIXME: need configurability */
15 #define NR_CHANS  10
16 #define NR_CONSS  1
17 #define NR_BINDS  4
18 #define NR_FILES  18
19 
20 #define NODEV     255
21 #define CHDIR     (1 << 15)
22 
23 #define SYNCDEV   0
24 #define SYNCALL   1
25 
26 typedef struct dev dev_t;
27 typedef struct chan chan_t;
28 typedef struct dirtab dirtab_t;
29 typedef int devgen_t(chan_t *, const dirtab_t *, int, int, dir_t *);
30 typedef struct attr attr_t;
31 
32 enum {
33 	DEV_ROOT_QROOT,
34 	DEV_ROOT_QDEV,
35 	DEV_ROOT_QFIP,
36 	DEV_ROOT_QBLOBS,
37 	DEV_ROOT_QBLOBCTL,
38 	DEV_ROOT_QPSCI
39 };
40 
41 /*******************************************************************************
42  * This structure contains the necessary information to represent a directory
43  * of the filesystem.
44  ******************************************************************************/
45 struct dirtab {
46 	char		name[NAMELEN];
47 	qid_t		qid;
48 	long		length;
49 	unsigned char	perm;
50 	void		*data;
51 };
52 
53 /*******************************************************************************
54  * This structure defines the interface of device drivers.
55  * Each driver must implement a subset of those functions.
56  * It is possible to redirect to default implementations defined in dev.c.
57  ******************************************************************************/
58 /* FIXME: comments for the callbacks */
59 struct dev {
60 	char id;
61 	int (*stat)(chan_t *c, const char *file, dir_t *dir);
62 	int (*walk)(chan_t *c, const char *name);
63 	int (*read)(chan_t *c, void *buf, int n);
64 	int (*write)(chan_t *c, void *buf, int n);
65 	int (*seek)(chan_t *c, long off, int whence);
66 	chan_t *(*clone)(chan_t *c, chan_t *nc);
67 	chan_t *(*attach)(int id, int dev);
68 	chan_t *(*mount)(chan_t *c, const char *spec);
69 };
70 
71 /*******************************************************************************
72  * This structure defines the channel structure.
73  * A channel is a handle on an element of the filesystem.
74  ******************************************************************************/
75 struct chan {
76 	long		offset;
77 	qid_t		qid;
78 	unsigned char	index;	/* device index in devtab */
79 	unsigned char	dev;
80 	unsigned char	mode;
81 };
82 
83 /*******************************************************************************
84  * This structure defines an abstract argument passed to physical drivers from
85  * the configuration file.
86  ******************************************************************************/
87 struct attr {
88 	char	*key;
89 	char	*value;
90 };
91 
92 chan_t *path_to_channel(const char *path, int mode);
93 chan_t *clone(chan_t *c, chan_t *nc);
94 chan_t *attach(int id, int dev);
95 void channel_close(chan_t *c);
96 int buf_to_channel(chan_t *c, void *dst, void *src, int nbytes, long len);
97 int dirread(chan_t *c, dir_t *dir, const dirtab_t *tab,
98 	    int ntab, devgen_t *gen);
99 void make_dir_entry(chan_t *c, dir_t *dir, const char *name, long length,
100 		    qid_t qid, unsigned int mode);
101 void devlink(void);
102 
103 chan_t *devattach(int id, int dev);
104 int devseek(chan_t *c, long off, int whence);
105 chan_t *devclone(chan_t *c, chan_t *nc);
106 int devgen(chan_t *c, const dirtab_t *tab, int ntab, int n, dir_t *dir);
107 int devwalk(chan_t *c, const char *name, const dirtab_t *tab, int ntab,
108 		   devgen_t *gen);
109 int devstat(chan_t *dirc, const char *file, dir_t *dir,
110 		   const dirtab_t *tab, int ntab, devgen_t *gen);
111 
112 chan_t *deverrmount(chan_t *c, const char *spec);
113 int deverrwrite(chan_t *c, void *buf, int n);
114 int deverrseek(chan_t *c, long off, int whence);
115 
116 extern dev_t *const devtab[];
117 
118 void __dead2 devpanic(const char *cause);
119 
120 #endif /* DEV_H */
121