OSDN Git Service

Merge tag 'rdma-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / staging / comedi / drivers / comedi_fc.c
1 /*
2     comedi/drivers/comedi_fc.c
3
4     This is a place for code driver writers wish to share between
5     two or more drivers.  fc is short
6     for frank-common.
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 #include <linux/module.h>
23 #include "../comedidev.h"
24
25 #include "comedi_fc.h"
26
27 static void increment_scan_progress(struct comedi_subdevice *subd,
28                                     unsigned int num_bytes)
29 {
30         struct comedi_async *async = subd->async;
31         unsigned int scan_length = cfc_bytes_per_scan(subd);
32
33         async->scan_progress += num_bytes;
34         if (async->scan_progress >= scan_length) {
35                 async->scan_progress %= scan_length;
36                 async->events |= COMEDI_CB_EOS;
37         }
38 }
39
40 /* Writes an array of data points to comedi's buffer */
41 unsigned int cfc_write_array_to_buffer(struct comedi_subdevice *subd,
42                                        void *data, unsigned int num_bytes)
43 {
44         struct comedi_async *async = subd->async;
45         unsigned int retval;
46
47         if (num_bytes == 0)
48                 return 0;
49
50         retval = comedi_buf_write_alloc(async, num_bytes);
51         if (retval != num_bytes) {
52                 dev_warn(subd->device->class_dev, "comedi: buffer overrun\n");
53                 async->events |= COMEDI_CB_OVERFLOW;
54                 return 0;
55         }
56
57         comedi_buf_memcpy_to(async, 0, data, num_bytes);
58         comedi_buf_write_free(async, num_bytes);
59         increment_scan_progress(subd, num_bytes);
60         async->events |= COMEDI_CB_BLOCK;
61
62         return num_bytes;
63 }
64 EXPORT_SYMBOL_GPL(cfc_write_array_to_buffer);
65
66 unsigned int cfc_read_array_from_buffer(struct comedi_subdevice *subd,
67                                         void *data, unsigned int num_bytes)
68 {
69         struct comedi_async *async = subd->async;
70
71         if (num_bytes == 0)
72                 return 0;
73
74         num_bytes = comedi_buf_read_alloc(async, num_bytes);
75         comedi_buf_memcpy_from(async, 0, data, num_bytes);
76         comedi_buf_read_free(async, num_bytes);
77         increment_scan_progress(subd, num_bytes);
78         async->events |= COMEDI_CB_BLOCK;
79
80         return num_bytes;
81 }
82 EXPORT_SYMBOL_GPL(cfc_read_array_from_buffer);
83
84 unsigned int cfc_handle_events(struct comedi_device *dev,
85                                struct comedi_subdevice *subd)
86 {
87         unsigned int events = subd->async->events;
88
89         if (events == 0)
90                 return events;
91
92         if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
93                 subd->cancel(dev, subd);
94
95         comedi_event(dev, subd);
96
97         return events;
98 }
99 EXPORT_SYMBOL_GPL(cfc_handle_events);
100
101 MODULE_AUTHOR("Frank Mori Hess <fmhess@users.sourceforge.net>");
102 MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
103 MODULE_LICENSE("GPL");
104
105 static int __init comedi_fc_init_module(void)
106 {
107         return 0;
108 }
109
110 static void __exit comedi_fc_cleanup_module(void)
111 {
112 }
113
114 module_init(comedi_fc_init_module);
115 module_exit(comedi_fc_cleanup_module);