Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / comedi_pcmcia.c
1 /*
2  * comedi_pcmcia.c
3  * Comedi PCMCIA driver specific functions.
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21
22 #include <pcmcia/cistpl.h>
23 #include <pcmcia/ds.h>
24
25 #include "comedidev.h"
26
27 /**
28  * comedi_to_pcmcia_dev() - comedi_device pointer to pcmcia_device pointer.
29  * @dev: comedi_device struct
30  */
31 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *dev)
32 {
33         return dev->hw_dev ? to_pcmcia_dev(dev->hw_dev) : NULL;
34 }
35 EXPORT_SYMBOL_GPL(comedi_to_pcmcia_dev);
36
37 static int comedi_pcmcia_conf_check(struct pcmcia_device *link,
38                                     void *priv_data)
39 {
40         if (link->config_index == 0)
41                 return -EINVAL;
42
43         return pcmcia_request_io(link);
44 }
45
46 /**
47  * comedi_pcmcia_enable() - Request the regions and enable the PCMCIA device.
48  * @dev: comedi_device struct
49  * @conf_check: optional callback to check the pcmcia_device configuration
50  *
51  * The comedi PCMCIA driver needs to set the link->config_flags, as
52  * appropriate for that driver, before calling this function in order
53  * to allow pcmcia_loop_config() to do its internal autoconfiguration.
54  */
55 int comedi_pcmcia_enable(struct comedi_device *dev,
56                          int (*conf_check)(struct pcmcia_device *, void *))
57 {
58         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
59         int ret;
60
61         if (!link)
62                 return -ENODEV;
63
64         if (!conf_check)
65                 conf_check = comedi_pcmcia_conf_check;
66
67         ret = pcmcia_loop_config(link, conf_check, NULL);
68         if (ret)
69                 return ret;
70
71         return pcmcia_enable_device(link);
72 }
73 EXPORT_SYMBOL_GPL(comedi_pcmcia_enable);
74
75 /**
76  * comedi_pcmcia_disable() - Disable the PCMCIA device and release the regions.
77  * @dev: comedi_device struct
78  */
79 void comedi_pcmcia_disable(struct comedi_device *dev)
80 {
81         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
82
83         if (link)
84                 pcmcia_disable_device(link);
85 }
86 EXPORT_SYMBOL_GPL(comedi_pcmcia_disable);
87
88 /**
89  * comedi_pcmcia_auto_config() - Configure/probe a comedi PCMCIA driver.
90  * @link: pcmcia_device struct
91  * @driver: comedi_driver struct
92  *
93  * Typically called from the pcmcia_driver (*probe) function.
94  */
95 int comedi_pcmcia_auto_config(struct pcmcia_device *link,
96                               struct comedi_driver *driver)
97 {
98         return comedi_auto_config(&link->dev, driver, 0);
99 }
100 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_config);
101
102 /**
103  * comedi_pcmcia_auto_unconfig() - Unconfigure/remove a comedi PCMCIA driver.
104  * @link: pcmcia_device struct
105  *
106  * Typically called from the pcmcia_driver (*remove) function.
107  */
108 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *link)
109 {
110         comedi_auto_unconfig(&link->dev);
111 }
112 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_unconfig);
113
114 /**
115  * comedi_pcmcia_driver_register() - Register a comedi PCMCIA driver.
116  * @comedi_driver: comedi_driver struct
117  * @pcmcia_driver: pcmcia_driver struct
118  *
119  * This function is used for the module_init() of comedi USB drivers.
120  * Do not call it directly, use the module_comedi_pcmcia_driver() helper
121  * macro instead.
122  */
123 int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
124                                   struct pcmcia_driver *pcmcia_driver)
125 {
126         int ret;
127
128         ret = comedi_driver_register(comedi_driver);
129         if (ret < 0)
130                 return ret;
131
132         ret = pcmcia_register_driver(pcmcia_driver);
133         if (ret < 0) {
134                 comedi_driver_unregister(comedi_driver);
135                 return ret;
136         }
137
138         return 0;
139 }
140 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
141
142 /**
143  * comedi_pcmcia_driver_unregister() - Unregister a comedi PCMCIA driver.
144  * @comedi_driver: comedi_driver struct
145  * @pcmcia_driver: pcmcia_driver struct
146  *
147  * This function is used for the module_exit() of comedi PCMCIA drivers.
148  * Do not call it directly, use the module_comedi_pcmcia_driver() helper
149  * macro instead.
150  */
151 void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
152                                      struct pcmcia_driver *pcmcia_driver)
153 {
154         pcmcia_unregister_driver(pcmcia_driver);
155         comedi_driver_unregister(comedi_driver);
156 }
157 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
158
159 static int __init comedi_pcmcia_init(void)
160 {
161         return 0;
162 }
163 module_init(comedi_pcmcia_init);
164
165 static void __exit comedi_pcmcia_exit(void)
166 {
167 }
168 module_exit(comedi_pcmcia_exit);
169
170 MODULE_AUTHOR("http://www.comedi.org");
171 MODULE_DESCRIPTION("Comedi PCMCIA interface module");
172 MODULE_LICENSE("GPL");