Merge remote-tracking branches 'asoc/fix/fsl-ssi' and 'asoc/fix/pxa' into asoc-linus
[cascardo/linux.git] / drivers / staging / comedi / drivers / comedi_fc.c
1 /*
2  * comedi_fc.c
3  * This is a place for code driver writers wish to share between
4  * two or more drivers.  fc is short for frank-common.
5  *
6  * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
7  * Copyright (C) 2002 Frank Mori Hess
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #include <linux/module.h>
21 #include "../comedidev.h"
22
23 #include "comedi_fc.h"
24
25 unsigned int cfc_bytes_per_scan(struct comedi_subdevice *s)
26 {
27         struct comedi_cmd *cmd = &s->async->cmd;
28         unsigned int num_samples;
29         unsigned int bits_per_sample;
30
31         switch (s->type) {
32         case COMEDI_SUBD_DI:
33         case COMEDI_SUBD_DO:
34         case COMEDI_SUBD_DIO:
35                 bits_per_sample = 8 * bytes_per_sample(s);
36                 num_samples = (cmd->chanlist_len + bits_per_sample - 1) /
37                                 bits_per_sample;
38                 break;
39         default:
40                 num_samples = cmd->chanlist_len;
41                 break;
42         }
43         return num_samples * bytes_per_sample(s);
44 }
45 EXPORT_SYMBOL_GPL(cfc_bytes_per_scan);
46
47 void cfc_inc_scan_progress(struct comedi_subdevice *s, unsigned int num_bytes)
48 {
49         struct comedi_async *async = s->async;
50         unsigned int scan_length = cfc_bytes_per_scan(s);
51
52         async->scan_progress += num_bytes;
53         if (async->scan_progress >= scan_length) {
54                 async->scan_progress %= scan_length;
55                 async->events |= COMEDI_CB_EOS;
56         }
57 }
58 EXPORT_SYMBOL_GPL(cfc_inc_scan_progress);
59
60 /* Writes an array of data points to comedi's buffer */
61 unsigned int cfc_write_array_to_buffer(struct comedi_subdevice *s,
62                                        void *data, unsigned int num_bytes)
63 {
64         struct comedi_async *async = s->async;
65         unsigned int retval;
66
67         if (num_bytes == 0)
68                 return 0;
69
70         retval = comedi_buf_write_alloc(s, num_bytes);
71         if (retval != num_bytes) {
72                 dev_warn(s->device->class_dev, "buffer overrun\n");
73                 async->events |= COMEDI_CB_OVERFLOW;
74                 return 0;
75         }
76
77         comedi_buf_memcpy_to(s, 0, data, num_bytes);
78         comedi_buf_write_free(s, num_bytes);
79         cfc_inc_scan_progress(s, num_bytes);
80         async->events |= COMEDI_CB_BLOCK;
81
82         return num_bytes;
83 }
84 EXPORT_SYMBOL_GPL(cfc_write_array_to_buffer);
85
86 unsigned int cfc_read_array_from_buffer(struct comedi_subdevice *s,
87                                         void *data, unsigned int num_bytes)
88 {
89         if (num_bytes == 0)
90                 return 0;
91
92         num_bytes = comedi_buf_read_alloc(s, num_bytes);
93         comedi_buf_memcpy_from(s, 0, data, num_bytes);
94         comedi_buf_read_free(s, num_bytes);
95         cfc_inc_scan_progress(s, num_bytes);
96         s->async->events |= COMEDI_CB_BLOCK;
97
98         return num_bytes;
99 }
100 EXPORT_SYMBOL_GPL(cfc_read_array_from_buffer);
101
102 unsigned int cfc_handle_events(struct comedi_device *dev,
103                                struct comedi_subdevice *s)
104 {
105         unsigned int events = s->async->events;
106
107         if (events == 0)
108                 return events;
109
110         if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
111                 s->cancel(dev, s);
112
113         comedi_event(dev, s);
114
115         return events;
116 }
117 EXPORT_SYMBOL_GPL(cfc_handle_events);
118
119 static int __init comedi_fc_init_module(void)
120 {
121         return 0;
122 }
123 module_init(comedi_fc_init_module);
124
125 static void __exit comedi_fc_cleanup_module(void)
126 {
127 }
128 module_exit(comedi_fc_cleanup_module);
129
130 MODULE_AUTHOR("Frank Mori Hess <fmhess@users.sourceforge.net>");
131 MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
132 MODULE_LICENSE("GPL");