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