xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_datavar.h (revision 23f6875a43f7ce365f2d52cf857da010c47fb03b)
1 /*-
2  * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_
33 #define _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_
34 
35 #include <sys/param.h>
36 #include <sys/linker_set.h>
37 #include <sys/refcount.h>
38 
39 #include "bhnd_nvram_io.h"
40 
41 #include "bhnd_nvram_data.h"
42 
43 /** Registered NVRAM parser class instances. */
44 SET_DECLARE(bhnd_nvram_data_class_set, bhnd_nvram_data_class);
45 
46 void			*bhnd_nvram_data_generic_find(
47 			     struct bhnd_nvram_data *nv, const char *name);
48 int			 bhnd_nvram_data_generic_rp_getvar(
49 			     struct bhnd_nvram_data *nv, void *cookiep,
50 			     void *outp, size_t *olen, bhnd_nvram_type otype);
51 int			 bhnd_nvram_data_generic_rp_copy_val(
52 			     struct bhnd_nvram_data *nv, void *cookiep,
53 			     bhnd_nvram_val **val);
54 
55 /** @see bhnd_nvram_data_probe() */
56 typedef int		 (bhnd_nvram_data_op_probe)(struct bhnd_nvram_io *io);
57 
58 /** @see bhnd_nvram_data_serialize() */
59 typedef int		 (bhnd_nvram_data_op_serialize)(
60 			      bhnd_nvram_data_class *cls,
61 			      bhnd_nvram_plist *props,
62 			      bhnd_nvram_plist *options, void *outp,
63 			      size_t *olen);
64 
65 /** @see bhnd_nvram_data_new() */
66 typedef int		 (bhnd_nvram_data_op_new)(struct bhnd_nvram_data *nv,
67 			      struct bhnd_nvram_io *io);
68 
69 /** Free all resources associated with @p nv. Called by
70  *  bhnd_nvram_data_release() when the reference count reaches zero. */
71 typedef void		 (bhnd_nvram_data_op_free)(struct bhnd_nvram_data *nv);
72 
73 /** @see bhnd_nvram_data_count() */
74 typedef size_t		 (bhnd_nvram_data_op_count)(struct bhnd_nvram_data *nv);
75 
76 /** @see bhnd_nvram_data_options() */
77 typedef bhnd_nvram_plist*(bhnd_nvram_data_op_options)(
78 			      struct bhnd_nvram_data *nv);
79 
80 /** @see bhnd_nvram_data_caps() */
81 typedef uint32_t	 (bhnd_nvram_data_op_caps)(struct bhnd_nvram_data *nv);
82 
83 /** @see bhnd_nvram_data_next() */
84 typedef const char	*(bhnd_nvram_data_op_next)(struct bhnd_nvram_data *nv,
85 			      void **cookiep);
86 
87 /** @see bhnd_nvram_data_find() */
88 typedef void		*(bhnd_nvram_data_op_find)(struct bhnd_nvram_data *nv,
89 			      const char *name);
90 
91 /** @see bhnd_nvram_data_copy_val() */
92 typedef int		 (bhnd_nvram_data_op_copy_val)(
93 			      struct bhnd_nvram_data *nv, void *cookiep,
94 			      bhnd_nvram_val **value);
95 
96 /** @see bhnd_nvram_data_getvar_order() */
97 typedef int		 (bhnd_nvram_data_op_getvar_order)(
98 			      struct bhnd_nvram_data *nv, void *cookiep1,
99 			      void *cookiep2);
100 
101 /** @see bhnd_nvram_data_getvar_name() */
102 typedef const char	*(bhnd_nvram_data_op_getvar_name)(
103 			      struct bhnd_nvram_data *nv,
104 			      void *cookiep);
105 
106 /** @see bhnd_nvram_data_getvar() */
107 typedef int		 (bhnd_nvram_data_op_getvar)(struct bhnd_nvram_data *nv,
108 			      void *cookiep, void *buf, size_t *len,
109 			      bhnd_nvram_type type);
110 
111 /** @see bhnd_nvram_data_getvar_ptr() */
112 typedef const void	*(bhnd_nvram_data_op_getvar_ptr)(
113 			      struct bhnd_nvram_data *nv, void *cookiep,
114 			      size_t *len, bhnd_nvram_type *type);
115 
116 /** @see bhnd_nvram_data_filter_setvar() */
117 typedef int		 (bhnd_nvram_data_op_filter_setvar)(
118 			      struct bhnd_nvram_data *nv, const char *name,
119 			      bhnd_nvram_val *value, bhnd_nvram_val **result);
120 
121 /** @see bhnd_nvram_data_filter_unsetvar() */
122 typedef int		 (bhnd_nvram_data_op_filter_unsetvar)(
123 			      struct bhnd_nvram_data *nv, const char *name);
124 
125 /**
126  * NVRAM data class.
127  */
128 struct bhnd_nvram_data_class {
129 	const char			*desc;		/**< description */
130 	uint32_t			 caps;		/**< capabilities (BHND_NVRAM_DATA_CAP_*) */
131 	size_t				 size;		/**< instance size */
132 
133 	bhnd_nvram_data_op_probe		*op_probe;
134 	bhnd_nvram_data_op_serialize		*op_serialize;
135 	bhnd_nvram_data_op_new			*op_new;
136 	bhnd_nvram_data_op_free			*op_free;
137 	bhnd_nvram_data_op_count		*op_count;
138 	bhnd_nvram_data_op_options		*op_options;
139 	bhnd_nvram_data_op_caps			*op_caps;
140 	bhnd_nvram_data_op_next			*op_next;
141 	bhnd_nvram_data_op_find			*op_find;
142 	bhnd_nvram_data_op_copy_val		*op_copy_val;
143 	bhnd_nvram_data_op_getvar_order		*op_getvar_order;
144 	bhnd_nvram_data_op_getvar		*op_getvar;
145 	bhnd_nvram_data_op_getvar_ptr		*op_getvar_ptr;
146 	bhnd_nvram_data_op_getvar_name		*op_getvar_name;
147 	bhnd_nvram_data_op_filter_setvar	*op_filter_setvar;
148 	bhnd_nvram_data_op_filter_unsetvar	*op_filter_unsetvar;
149 };
150 
151 /**
152  * NVRAM data instance.
153  */
154 struct bhnd_nvram_data {
155 	struct bhnd_nvram_data_class	*cls;
156 	volatile u_int			 refs;
157 };
158 
159 /*
160  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
161  *
162  * Declares a bhnd_nvram_data_class method implementation with class name
163  * _cname and method name _mname
164  */
165 #define	BHND_NVRAM_DATA_CLASS_DECL_METHOD(_cname, _mname)		\
166 	static bhnd_nvram_data_op_ ## _mname				\
167 	    bhnd_nvram_ ## _cname ## _ ## _mname;			\
168 
169 /*
170  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
171  *
172  * Assign a bhnd_nvram_data_class method implementation with class name
173  * @p _cname and method name @p _mname
174  */
175 #define	BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD(_cname, _mname)		\
176 	.op_ ## _mname = bhnd_nvram_ ## _cname ## _ ## _mname,
177 
178 /*
179  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
180  *
181  * Iterate over all bhnd_nvram_data_class method names, calling
182  * _macro with the class name _cname as the first argument, and
183  * a bhnd_nvram_data_class method name as the second.
184  */
185 #define	BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname, _macro)	\
186 	_macro(_cname, probe)					\
187 	_macro(_cname, serialize)				\
188 	_macro(_cname, new)					\
189 	_macro(_cname, free)					\
190 	_macro(_cname, count)					\
191 	_macro(_cname, options)					\
192 	_macro(_cname, caps)					\
193 	_macro(_cname, next)					\
194 	_macro(_cname, find)					\
195 	_macro(_cname, copy_val)				\
196 	_macro(_cname, getvar_order)				\
197 	_macro(_cname, getvar)					\
198 	_macro(_cname, getvar_ptr)				\
199 	_macro(_cname, getvar_name)				\
200 	_macro(_cname, filter_setvar)				\
201 	_macro(_cname, filter_unsetvar)
202 
203 /**
204  * Define a bhnd_nvram_data_class with class name @p _n and description
205  * @p _desc, and register with bhnd_nvram_data_class_set.
206  */
207 #define	BHND_NVRAM_DATA_CLASS_DEFN(_cname, _desc, _caps, _size)		\
208 	BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname,			\
209 	    BHND_NVRAM_DATA_CLASS_DECL_METHOD)				\
210 									\
211 	struct bhnd_nvram_data_class bhnd_nvram_## _cname ## _class = {	\
212 		.desc		= (_desc),				\
213 		.caps		= (_caps),				\
214 		.size		= (_size),				\
215 		BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname,		\
216 		    BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD)		\
217 	};								\
218 									\
219 	DATA_SET(bhnd_nvram_data_class_set,				\
220 	    bhnd_nvram_## _cname ## _class);
221 
222 #endif /* _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_ */
223