aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorDavid Schleef <ds@schleef.org>2008-11-14 17:42:01 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2009-01-06 16:52:18 -0500
commita9f23e00c17567cc4b7ce50cd07226f7bfb70da6 (patch)
tree9452b66f626b2b4d601b9d393a525ceed5cd24ec /drivers
parent94ab11c22727acb4cefad6f7d0953e23eda06dad (diff)
Staging: comedi: comedi driver common function module
This module is shared by many comedi drivers. From: David Schleef <ds@schleef.org> Cc: Frank Mori Hess <fmhess@users.sourceforge.net> Cc: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/staging/comedi/drivers/Makefile3
-rw-r--r--drivers/staging/comedi/drivers/comedi_fc.c118
-rw-r--r--drivers/staging/comedi/drivers/comedi_fc.h75
-rw-r--r--drivers/staging/comedi/drivers/comedi_pci.h89
4 files changed, 285 insertions, 0 deletions
diff --git a/drivers/staging/comedi/drivers/Makefile b/drivers/staging/comedi/drivers/Makefile
index 3c668a7f4194..c202ca3ecc39 100644
--- a/drivers/staging/comedi/drivers/Makefile
+++ b/drivers/staging/comedi/drivers/Makefile
@@ -1,2 +1,5 @@
1# Makefile for individual comedi drivers 1# Makefile for individual comedi drivers
2# 2#
3
4# Comedi "helper" modules
5obj-$(CONFIG_COMEDI) += comedi_fc.o
diff --git a/drivers/staging/comedi/drivers/comedi_fc.c b/drivers/staging/comedi/drivers/comedi_fc.c
new file mode 100644
index 000000000000..da8e9d2844f0
--- /dev/null
+++ b/drivers/staging/comedi/drivers/comedi_fc.c
@@ -0,0 +1,118 @@
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 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#include "../comedidev.h"
28
29#include "comedi_fc.h"
30
31static void increment_scan_progress(comedi_subdevice * subd,
32 unsigned int num_bytes)
33{
34 comedi_async *async = subd->async;
35 unsigned int scan_length = cfc_bytes_per_scan(subd);
36
37 async->scan_progress += num_bytes;
38 if (async->scan_progress >= scan_length) {
39 async->scan_progress %= scan_length;
40 async->events |= COMEDI_CB_EOS;
41 }
42}
43
44/* Writes an array of data points to comedi's buffer */
45unsigned int cfc_write_array_to_buffer(comedi_subdevice * subd, void *data,
46 unsigned int num_bytes)
47{
48 comedi_async *async = subd->async;
49 unsigned int retval;
50
51 if (num_bytes == 0)
52 return 0;
53
54 retval = comedi_buf_write_alloc(async, num_bytes);
55 if (retval != num_bytes) {
56 rt_printk("comedi: buffer overrun\n");
57 async->events |= COMEDI_CB_OVERFLOW;
58 return 0;
59 }
60
61 comedi_buf_memcpy_to(async, 0, data, num_bytes);
62 comedi_buf_write_free(async, num_bytes);
63 increment_scan_progress(subd, num_bytes);
64 async->events |= COMEDI_CB_BLOCK;
65
66 return num_bytes;
67}
68
69unsigned int cfc_read_array_from_buffer(comedi_subdevice * subd, void *data,
70 unsigned int num_bytes)
71{
72 comedi_async *async = subd->async;
73
74 if (num_bytes == 0)
75 return 0;
76
77 num_bytes = comedi_buf_read_alloc(async, num_bytes);
78 comedi_buf_memcpy_from(async, 0, data, num_bytes);
79 comedi_buf_read_free(async, num_bytes);
80 increment_scan_progress(subd, num_bytes);
81 async->events |= COMEDI_CB_BLOCK;
82
83 return num_bytes;
84}
85
86unsigned int cfc_handle_events(comedi_device * dev, comedi_subdevice * subd)
87{
88 unsigned int events = subd->async->events;
89
90 if (events == 0)
91 return events;
92
93 if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
94 subd->cancel(dev, subd);
95
96 comedi_event(dev, subd);
97
98 return events;
99}
100
101MODULE_AUTHOR("Frank Mori Hess <fmhess@users.sourceforge.net>");
102MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
103MODULE_LICENSE("GPL");
104
105static int __init comedi_fc_init_module(void)
106{
107 return 0;
108}
109static void __exit comedi_fc_cleanup_module(void)
110{
111}
112
113module_init(comedi_fc_init_module);
114module_exit(comedi_fc_cleanup_module);
115
116EXPORT_SYMBOL(cfc_write_array_to_buffer);
117EXPORT_SYMBOL(cfc_read_array_from_buffer);
118EXPORT_SYMBOL(cfc_handle_events);
diff --git a/drivers/staging/comedi/drivers/comedi_fc.h b/drivers/staging/comedi/drivers/comedi_fc.h
new file mode 100644
index 000000000000..b8edd673edfd
--- /dev/null
+++ b/drivers/staging/comedi/drivers/comedi_fc.h
@@ -0,0 +1,75 @@
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 */
33extern unsigned int cfc_write_array_to_buffer(comedi_subdevice * subd,
34 void *data, unsigned int num_bytes);
35
36static inline unsigned int cfc_write_to_buffer(comedi_subdevice * subd,
37 sampl_t data)
38{
39 return cfc_write_array_to_buffer(subd, &data, sizeof(data));
40};
41
42static inline unsigned int cfc_write_long_to_buffer(comedi_subdevice * subd,
43 lsampl_t data)
44{
45 return cfc_write_array_to_buffer(subd, &data, sizeof(data));
46};
47
48extern unsigned int cfc_read_array_from_buffer(comedi_subdevice * subd,
49 void *data, unsigned int num_bytes);
50
51extern unsigned int cfc_handle_events(comedi_device * dev,
52 comedi_subdevice * subd);
53
54static inline unsigned int cfc_bytes_per_scan(comedi_subdevice * subd)
55{
56 int num_samples;
57 int bits_per_sample;
58
59 switch (subd->type) {
60 case COMEDI_SUBD_DI:
61 case COMEDI_SUBD_DO:
62 case COMEDI_SUBD_DIO:
63 bits_per_sample = 8 * bytes_per_sample(subd);
64 num_samples =
65 (subd->async->cmd.chanlist_len + bits_per_sample -
66 1) / bits_per_sample;
67 break;
68 default:
69 num_samples = subd->async->cmd.chanlist_len;
70 break;
71 }
72 return num_samples * bytes_per_sample(subd);
73}
74
75#endif /* _COMEDI_FC_H */
diff --git a/drivers/staging/comedi/drivers/comedi_pci.h b/drivers/staging/comedi/drivers/comedi_pci.h
new file mode 100644
index 000000000000..140787839153
--- /dev/null
+++ b/drivers/staging/comedi/drivers/comedi_pci.h
@@ -0,0 +1,89 @@
1/*
2 comedi/drivers/comedi_pci.h
3 Various PCI functions for drivers.
4
5 Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
6
7 COMEDI - Linux Control and Measurement Device Interface
8 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
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 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24*/
25
26#ifndef _COMEDI_PCI_H_
27#define _COMEDI_PCI_H_
28
29#include <linux/version.h>
30#include <linux/pci.h>
31
32#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
33#define PCI_ENABLE_IS_REFCOUNTED
34#endif
35
36/*
37 * Enables PCI device without requesting regions. Just a simple wrapper
38 * for pci_enable_device().
39 */
40static inline int comedi_pci_enable_no_regions(struct pci_dev *pdev)
41{
42 return pci_enable_device(pdev);
43}
44
45/*
46 * Called to disable PCI device if PCI device has been enabled, but
47 * PCI regions have not been reserved.
48 *
49 * It only disables the PCI device if the kernel supports reference
50 * counting of PCI enables, otherwise it might stop the device working
51 * in another driver instance.
52 */
53static inline void comedi_pci_disable_no_regions(struct pci_dev *pdev)
54{
55#ifdef PCI_ENABLE_IS_REFCOUNTED
56 pci_disable_device(pdev);
57#endif
58}
59
60/*
61 * Enable the PCI device and request the regions.
62 */
63static inline int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
64{
65 int rc;
66
67 rc = pci_enable_device(pdev);
68 if (rc < 0) {
69 return rc;
70 }
71 rc = pci_request_regions(pdev, res_name);
72 if (rc < 0) {
73 comedi_pci_disable_no_regions(pdev);
74 }
75 return rc;
76}
77
78/*
79 * Release the regions and disable the PCI device.
80 *
81 * This must be matched with a previous successful call to comedi_pci_enable().
82 */
83static inline void comedi_pci_disable(struct pci_dev *pdev)
84{
85 pci_release_regions(pdev);
86 pci_disable_device(pdev);
87}
88
89#endif