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 */
30
31#ifndef _BHND_NVRAM_BHND_NVRAM_IOVAR_H_
32#define _BHND_NVRAM_BHND_NVRAM_IOVAR_H_
33
34#include <sys/param.h>
35
36#include "bhnd_nvram_io.h"
37
38/** @see bhnd_nvram_io_read() */
39typedef int (bhnd_nvram_iop_read)(struct bhnd_nvram_io *io, size_t offset,
40    void *buffer, size_t nbytes);
41
42/** @see bhnd_nvram_io_read_ptr() */
43typedef int (bhnd_nvram_iop_read_ptr)(struct bhnd_nvram_io *io, size_t offset,
44    const void **ptr, size_t nbytes, size_t *navail);
45
46/** @see bhnd_nvram_io_write() */
47typedef int (bhnd_nvram_iop_write)(struct bhnd_nvram_io *io, size_t offset,
48    void *buffer, size_t nbytes);
49
50/** @see bhnd_nvram_io_write_ptr() */
51typedef int (bhnd_nvram_iop_write_ptr)(struct bhnd_nvram_io *io, size_t offset,
52    void **ptr, size_t nbytes, size_t *navail);
53
54/** @see bhnd_nvram_io_getsize() */
55typedef size_t (bhnd_nvram_iop_getsize)(struct bhnd_nvram_io *io);
56
57/** @see bhnd_nvram_io_setsize() */
58typedef int (bhnd_nvram_iop_setsize)(struct bhnd_nvram_io *io, size_t size);
59
60/** @see bhnd_nvram_io_free() */
61typedef void (bhnd_nvram_iop_free)(struct bhnd_nvram_io *io);
62
63/**
64 * NVRAM abstract I/O operations.
65 */
66struct bhnd_nvram_iops {
67	bhnd_nvram_iop_read		*read;		/**< read() implementation */
68	bhnd_nvram_iop_read_ptr		*read_ptr;	/**< read_ptr() implementation */
69	bhnd_nvram_iop_getsize		*getsize;	/**< getsize() implementation */
70	bhnd_nvram_iop_setsize		*setsize;	/**< setsize() implementation */
71	bhnd_nvram_iop_write		*write;		/**< write() implementation */
72	bhnd_nvram_iop_write_ptr	*write_ptr;	/**< write_ptr() implementation */
73	bhnd_nvram_iop_free		*free;		/**< free() implementation */
74};
75
76/**
77 * NVRAM abstract I/O context.
78 */
79struct bhnd_nvram_io {
80	const struct bhnd_nvram_iops	*iops;
81};
82
83/**
84 * Declare a bhnd_nvram_iops class with name @p _n.
85 */
86#define	BHND_NVRAM_IOPS_DEFN(_n)					\
87	static bhnd_nvram_iop_read	bhnd_nvram_ ## _n ## _read;	\
88	static bhnd_nvram_iop_read_ptr	bhnd_nvram_ ## _n ## _read_ptr;	\
89	static bhnd_nvram_iop_write	bhnd_nvram_ ## _n ## _write;	\
90	static bhnd_nvram_iop_write_ptr	bhnd_nvram_ ## _n ## _write_ptr;\
91	static bhnd_nvram_iop_getsize	bhnd_nvram_ ## _n ## _getsize;	\
92	static bhnd_nvram_iop_setsize	bhnd_nvram_ ## _n ## _setsize;	\
93	static bhnd_nvram_iop_free	bhnd_nvram_ ## _n ## _free;	\
94									\
95	static struct bhnd_nvram_iops	bhnd_nvram_ ## _n ## _ops = {	\
96		.read		= bhnd_nvram_ ## _n ## _read,		\
97		.read_ptr	= bhnd_nvram_ ## _n ## _read_ptr,	\
98		.write		= bhnd_nvram_ ## _n ## _write,		\
99		.write_ptr	= bhnd_nvram_ ## _n ## _write_ptr,	\
100		.getsize	= bhnd_nvram_ ## _n ## _getsize,	\
101		.setsize	= bhnd_nvram_ ## _n ## _setsize,	\
102		.free		= bhnd_nvram_ ## _n ## _free		\
103	};
104
105#endif /* _BHND_NVRAM_BHND_NVRAM_IOVAR_H_ */
106