Linux-libre 3.14.42-gnu
[librecmc/linux-libre.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
22 #ifndef _COMEDI_FC_H
23 #define _COMEDI_FC_H
24
25 #include "../comedidev.h"
26
27 /* Writes an array of data points to comedi's buffer */
28 extern unsigned int cfc_write_array_to_buffer(struct comedi_subdevice *subd,
29                                               void *data,
30                                               unsigned int num_bytes);
31
32 static inline unsigned int cfc_write_to_buffer(struct comedi_subdevice *subd,
33                                                unsigned short data)
34 {
35         return cfc_write_array_to_buffer(subd, &data, sizeof(data));
36 };
37
38 static inline unsigned int cfc_write_long_to_buffer(struct comedi_subdevice
39                                                     *subd, unsigned int data)
40 {
41         return cfc_write_array_to_buffer(subd, &data, sizeof(data));
42 };
43
44 extern unsigned int cfc_read_array_from_buffer(struct comedi_subdevice *subd,
45                                                void *data,
46                                                unsigned int num_bytes);
47
48 extern unsigned int cfc_handle_events(struct comedi_device *dev,
49                                       struct comedi_subdevice *subd);
50
51 static inline unsigned int cfc_bytes_per_scan(struct comedi_subdevice *subd)
52 {
53         int num_samples;
54         int bits_per_sample;
55
56         switch (subd->type) {
57         case COMEDI_SUBD_DI:
58         case COMEDI_SUBD_DO:
59         case COMEDI_SUBD_DIO:
60                 bits_per_sample = 8 * bytes_per_sample(subd);
61                 num_samples = (subd->async->cmd.chanlist_len +
62                                bits_per_sample - 1) / bits_per_sample;
63                 break;
64         default:
65                 num_samples = subd->async->cmd.chanlist_len;
66                 break;
67         }
68         return num_samples * bytes_per_sample(subd);
69 }
70
71 /**
72  * cfc_check_trigger_src() - trivially validate a comedi_cmd trigger source
73  * @src: pointer to the trigger source to validate
74  * @flags: bitmask of valid TRIG_* for the trigger
75  *
76  * This is used in "step 1" of the do_cmdtest functions of comedi drivers
77  * to vaildate the comedi_cmd triggers. The mask of the @src against the
78  * @flags allows the userspace comedilib to pass all the comedi_cmd
79  * triggers as TRIG_ANY and get back a bitmask of the valid trigger sources.
80  */
81 static inline int cfc_check_trigger_src(unsigned int *src, unsigned int flags)
82 {
83         unsigned int orig_src = *src;
84
85         *src = orig_src & flags;
86         if (*src == TRIG_INVALID || *src != orig_src)
87                 return -EINVAL;
88         return 0;
89 }
90
91 /**
92  * cfc_check_trigger_is_unique() - make sure a trigger source is unique
93  * @src: the trigger source to check
94  */
95 static inline int cfc_check_trigger_is_unique(unsigned int src)
96 {
97         /* this test is true if more than one _src bit is set */
98         if ((src & (src - 1)) != 0)
99                 return -EINVAL;
100         return 0;
101 }
102
103 /**
104  * cfc_check_trigger_arg_is() - trivially validate a trigger argument
105  * @arg: pointer to the trigger arg to validate
106  * @val: the value the argument should be
107  */
108 static inline int cfc_check_trigger_arg_is(unsigned int *arg, unsigned int val)
109 {
110         if (*arg != val) {
111                 *arg = val;
112                 return -EINVAL;
113         }
114         return 0;
115 }
116
117 /**
118  * cfc_check_trigger_arg_min() - trivially validate a trigger argument
119  * @arg: pointer to the trigger arg to validate
120  * @val: the minimum value the argument should be
121  */
122 static inline int cfc_check_trigger_arg_min(unsigned int *arg,
123                                             unsigned int val)
124 {
125         if (*arg < val) {
126                 *arg = val;
127                 return -EINVAL;
128         }
129         return 0;
130 }
131
132 /**
133  * cfc_check_trigger_arg_max() - trivially validate a trigger argument
134  * @arg: pointer to the trigger arg to validate
135  * @val: the maximum value the argument should be
136  */
137 static inline int cfc_check_trigger_arg_max(unsigned int *arg,
138                                             unsigned int val)
139 {
140         if (*arg > val) {
141                 *arg = val;
142                 return -EINVAL;
143         }
144         return 0;
145 }
146
147 #endif /* _COMEDI_FC_H */