GNU Linux-libre 4.14.303-gnu1
[releases.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 "comedi_pcmcia.h"
23
24 /**
25  * comedi_to_pcmcia_dev() - Return PCMCIA device attached to COMEDI device
26  * @dev: COMEDI device.
27  *
28  * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
29  * a &struct device embedded in a &struct pcmcia_device.
30  *
31  * Return: Attached PCMCIA device if @dev->hw_dev is non-%NULL.
32  * Return %NULL if @dev->hw_dev is %NULL.
33  */
34 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *dev)
35 {
36         return dev->hw_dev ? to_pcmcia_dev(dev->hw_dev) : NULL;
37 }
38 EXPORT_SYMBOL_GPL(comedi_to_pcmcia_dev);
39
40 static int comedi_pcmcia_conf_check(struct pcmcia_device *link,
41                                     void *priv_data)
42 {
43         if (link->config_index == 0)
44                 return -EINVAL;
45
46         return pcmcia_request_io(link);
47 }
48
49 /**
50  * comedi_pcmcia_enable() - Request the regions and enable the PCMCIA device
51  * @dev: COMEDI device.
52  * @conf_check: Optional callback to check each configuration option of the
53  *      PCMCIA device and request I/O regions.
54  *
55  * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a a
56  * &struct device embedded in a &struct pcmcia_device.  The comedi PCMCIA
57  * driver needs to set the 'config_flags' member in the &struct pcmcia_device,
58  * as appropriate for that driver, before calling this function in order to
59  * allow pcmcia_loop_config() to do its internal autoconfiguration.
60  *
61  * If @conf_check is %NULL it is set to a default function.  If is
62  * passed to pcmcia_loop_config() and should return %0 if the configuration
63  * is valid and I/O regions requested successfully, otherwise it should return
64  * a negative error value.  The default function returns -%EINVAL if the
65  * 'config_index' member is %0, otherwise it calls pcmcia_request_io() and
66  * returns the result.
67  *
68  * If the above configuration check passes, pcmcia_enable_device() is called
69  * to set up and activate the PCMCIA device.
70  *
71  * If this function returns an error, comedi_pcmcia_disable() should be called
72  * to release requested resources.
73  *
74  * Return:
75  *      0 on success,
76  *      -%ENODEV id @dev->hw_dev is %NULL,
77  *      a negative error number from pcmcia_loop_config() if it fails,
78  *      or a negative error number from pcmcia_enable_device() if it fails.
79  */
80 int comedi_pcmcia_enable(struct comedi_device *dev,
81                          int (*conf_check)(struct pcmcia_device *p_dev,
82                                            void *priv_data))
83 {
84         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
85         int ret;
86
87         if (!link)
88                 return -ENODEV;
89
90         if (!conf_check)
91                 conf_check = comedi_pcmcia_conf_check;
92
93         ret = pcmcia_loop_config(link, conf_check, NULL);
94         if (ret)
95                 return ret;
96
97         return pcmcia_enable_device(link);
98 }
99 EXPORT_SYMBOL_GPL(comedi_pcmcia_enable);
100
101 /**
102  * comedi_pcmcia_disable() - Disable the PCMCIA device and release the regions
103  * @dev: COMEDI device.
104  *
105  * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
106  * a &struct device embedded in a &struct pcmcia_device.  Call
107  * pcmcia_disable_device() to disable and clean up the PCMCIA device.
108  */
109 void comedi_pcmcia_disable(struct comedi_device *dev)
110 {
111         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
112
113         if (link)
114                 pcmcia_disable_device(link);
115 }
116 EXPORT_SYMBOL_GPL(comedi_pcmcia_disable);
117
118 /**
119  * comedi_pcmcia_auto_config() - Configure/probe a PCMCIA COMEDI device
120  * @link: PCMCIA device.
121  * @driver: Registered COMEDI driver.
122  *
123  * Typically called from the pcmcia_driver (*probe) function.  Auto-configure
124  * a COMEDI device, using a pointer to the &struct device embedded in *@link
125  * as the hardware device.  The @driver's "auto_attach" handler may call
126  * comedi_to_pcmcia_dev() on the passed in COMEDI device to recover @link.
127  *
128  * Return: The result of calling comedi_auto_config() (0 on success, or a
129  * negative error number on failure).
130  */
131 int comedi_pcmcia_auto_config(struct pcmcia_device *link,
132                               struct comedi_driver *driver)
133 {
134         return comedi_auto_config(&link->dev, driver, 0);
135 }
136 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_config);
137
138 /**
139  * comedi_pcmcia_auto_unconfig() - Unconfigure/remove a PCMCIA COMEDI device
140  * @link: PCMCIA device.
141  *
142  * Typically called from the pcmcia_driver (*remove) function.
143  * Auto-unconfigure a COMEDI device attached to this PCMCIA device, using a
144  * pointer to the &struct device embedded in *@link as the hardware device.
145  * The COMEDI driver's "detach" handler will be called during unconfiguration
146  * of the COMEDI device.
147  *
148  * Note that the COMEDI device may have already been unconfigured using the
149  * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
150  * again should be ignored.
151  */
152 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *link)
153 {
154         comedi_auto_unconfig(&link->dev);
155 }
156 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_unconfig);
157
158 /**
159  * comedi_pcmcia_driver_register() - Register a PCMCIA COMEDI driver
160  * @comedi_driver: COMEDI driver to be registered.
161  * @pcmcia_driver: PCMCIA driver to be registered.
162  *
163  * This function is used for the module_init() of PCMCIA COMEDI driver modules
164  * to register the COMEDI driver and the PCMCIA driver.  Do not call it
165  * directly, use the module_comedi_pcmcia_driver() helper macro instead.
166  *
167  * Return: 0 on success, or a negative error number on failure.
168  */
169 int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
170                                   struct pcmcia_driver *pcmcia_driver)
171 {
172         int ret;
173
174         ret = comedi_driver_register(comedi_driver);
175         if (ret < 0)
176                 return ret;
177
178         ret = pcmcia_register_driver(pcmcia_driver);
179         if (ret < 0) {
180                 comedi_driver_unregister(comedi_driver);
181                 return ret;
182         }
183
184         return 0;
185 }
186 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
187
188 /**
189  * comedi_pcmcia_driver_unregister() - Unregister a PCMCIA COMEDI driver
190  * @comedi_driver: COMEDI driver to be registered.
191  * @pcmcia_driver: PCMCIA driver to be registered.
192  *
193  * This function is called from the module_exit() of PCMCIA COMEDI driver
194  * modules to unregister the PCMCIA driver and the COMEDI driver.  Do not call
195  * it directly, use the module_comedi_pcmcia_driver() helper macro instead.
196  */
197 void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
198                                      struct pcmcia_driver *pcmcia_driver)
199 {
200         pcmcia_unregister_driver(pcmcia_driver);
201         comedi_driver_unregister(comedi_driver);
202 }
203 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
204
205 static int __init comedi_pcmcia_init(void)
206 {
207         return 0;
208 }
209 module_init(comedi_pcmcia_init);
210
211 static void __exit comedi_pcmcia_exit(void)
212 {
213 }
214 module_exit(comedi_pcmcia_exit);
215
216 MODULE_AUTHOR("http://www.comedi.org");
217 MODULE_DESCRIPTION("Comedi PCMCIA interface module");
218 MODULE_LICENSE("GPL");