1/*
2 * comedi_fc.h
3 * This is a place for code driver writers wish to share between
4 * two or more drivers. These functions are meant to be used only
5 * by drivers, they are NOT part of the kcomedilib API!
6 *
7 * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
8 * Copyright (C) 2002 Frank Mori Hess
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 */
20
21#ifndef _COMEDI_FC_H
22#define _COMEDI_FC_H
23
24#include "../comedidev.h"
25
26static inline unsigned int cfc_bytes_per_scan(struct comedi_subdevice *s)
27{
28	return comedi_bytes_per_scan(s);
29}
30
31static inline void cfc_inc_scan_progress(struct comedi_subdevice *s,
32					 unsigned int num_bytes)
33{
34	comedi_inc_scan_progress(s, num_bytes);
35}
36
37static inline unsigned int cfc_write_array_to_buffer(struct comedi_subdevice *s,
38						     const void *data,
39						     unsigned int num_bytes)
40{
41	return comedi_write_array_to_buffer(s, data, num_bytes);
42}
43
44static inline unsigned int cfc_write_to_buffer(struct comedi_subdevice *s,
45					       unsigned short data)
46{
47	return comedi_write_array_to_buffer(s, &data, sizeof(data));
48};
49
50static inline unsigned int cfc_write_long_to_buffer(struct comedi_subdevice *s,
51						    unsigned int data)
52{
53	return comedi_write_array_to_buffer(s, &data, sizeof(data));
54};
55
56static inline unsigned int
57cfc_read_array_from_buffer(struct comedi_subdevice *s, void *data,
58			   unsigned int num_bytes)
59{
60	return comedi_read_array_from_buffer(s, data, num_bytes);
61}
62
63static inline unsigned int cfc_handle_events(struct comedi_device *dev,
64					     struct comedi_subdevice *s)
65{
66	return comedi_handle_events(dev, s);
67}
68
69/**
70 * cfc_check_trigger_src() - trivially validate a comedi_cmd trigger source
71 * @src: pointer to the trigger source to validate
72 * @flags: bitmask of valid TRIG_* for the trigger
73 *
74 * This is used in "step 1" of the do_cmdtest functions of comedi drivers
75 * to vaildate the comedi_cmd triggers. The mask of the @src against the
76 * @flags allows the userspace comedilib to pass all the comedi_cmd
77 * triggers as TRIG_ANY and get back a bitmask of the valid trigger sources.
78 */
79static inline int cfc_check_trigger_src(unsigned int *src, unsigned int flags)
80{
81	unsigned int orig_src = *src;
82
83	*src = orig_src & flags;
84	if (*src == TRIG_INVALID || *src != orig_src)
85		return -EINVAL;
86	return 0;
87}
88
89/**
90 * cfc_check_trigger_is_unique() - make sure a trigger source is unique
91 * @src: the trigger source to check
92 */
93static inline int cfc_check_trigger_is_unique(unsigned int src)
94{
95	/* this test is true if more than one _src bit is set */
96	if ((src & (src - 1)) != 0)
97		return -EINVAL;
98	return 0;
99}
100
101/**
102 * cfc_check_trigger_arg_is() - trivially validate a trigger argument
103 * @arg: pointer to the trigger arg to validate
104 * @val: the value the argument should be
105 */
106static inline int cfc_check_trigger_arg_is(unsigned int *arg, unsigned int val)
107{
108	if (*arg != val) {
109		*arg = val;
110		return -EINVAL;
111	}
112	return 0;
113}
114
115/**
116 * cfc_check_trigger_arg_min() - trivially validate a trigger argument
117 * @arg: pointer to the trigger arg to validate
118 * @val: the minimum value the argument should be
119 */
120static inline int cfc_check_trigger_arg_min(unsigned int *arg,
121					    unsigned int val)
122{
123	if (*arg < val) {
124		*arg = val;
125		return -EINVAL;
126	}
127	return 0;
128}
129
130/**
131 * cfc_check_trigger_arg_max() - trivially validate a trigger argument
132 * @arg: pointer to the trigger arg to validate
133 * @val: the maximum value the argument should be
134 */
135static inline int cfc_check_trigger_arg_max(unsigned int *arg,
136					    unsigned int val)
137{
138	if (*arg > val) {
139		*arg = val;
140		return -EINVAL;
141	}
142	return 0;
143}
144
145#endif /* _COMEDI_FC_H */
146