blob: c53aad0fc2ce2a5fe25ee329decb9ec7562aed2c [file] [log] [blame]
Greg Kroah-Hartmane184e2b2017-11-07 14:58:43 +01001// SPDX-License-Identifier: GPL-2.0+
H Hartley Sweeten309231d2013-01-30 15:22:44 -07002/*
3 * comedi_pcmcia.c
4 * Comedi PCMCIA driver specific functions.
5 *
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
H Hartley Sweeten309231d2013-01-30 15:22:44 -07008 */
9
Ian Abbottaae434b2014-10-31 17:47:37 +000010#include <linux/module.h>
H Hartley Sweeten309231d2013-01-30 15:22:44 -070011#include <linux/kernel.h>
Ian Abbottdf0e68c12021-11-17 12:05:59 +000012#include <linux/comedi/comedi_pcmcia.h>
H Hartley Sweeten309231d2013-01-30 15:22:44 -070013
14/**
Ian Abbott127a0cf2015-09-30 17:10:57 +010015 * comedi_to_pcmcia_dev() - Return PCMCIA device attached to COMEDI device
16 * @dev: COMEDI device.
17 *
18 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
19 * a &struct device embedded in a &struct pcmcia_device.
20 *
21 * Return: Attached PCMCIA device if @dev->hw_dev is non-%NULL.
22 * Return %NULL if @dev->hw_dev is %NULL.
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -070023 */
24struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *dev)
25{
26 return dev->hw_dev ? to_pcmcia_dev(dev->hw_dev) : NULL;
27}
28EXPORT_SYMBOL_GPL(comedi_to_pcmcia_dev);
29
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -070030static int comedi_pcmcia_conf_check(struct pcmcia_device *link,
31 void *priv_data)
32{
33 if (link->config_index == 0)
34 return -EINVAL;
35
36 return pcmcia_request_io(link);
37}
38
39/**
Ian Abbott127a0cf2015-09-30 17:10:57 +010040 * comedi_pcmcia_enable() - Request the regions and enable the PCMCIA device
41 * @dev: COMEDI device.
42 * @conf_check: Optional callback to check each configuration option of the
43 * PCMCIA device and request I/O regions.
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -070044 *
Ian Abbott127a0cf2015-09-30 17:10:57 +010045 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a a
46 * &struct device embedded in a &struct pcmcia_device. The comedi PCMCIA
47 * driver needs to set the 'config_flags' member in the &struct pcmcia_device,
48 * as appropriate for that driver, before calling this function in order to
49 * allow pcmcia_loop_config() to do its internal autoconfiguration.
50 *
51 * If @conf_check is %NULL it is set to a default function. If is
52 * passed to pcmcia_loop_config() and should return %0 if the configuration
53 * is valid and I/O regions requested successfully, otherwise it should return
54 * a negative error value. The default function returns -%EINVAL if the
55 * 'config_index' member is %0, otherwise it calls pcmcia_request_io() and
56 * returns the result.
57 *
58 * If the above configuration check passes, pcmcia_enable_device() is called
59 * to set up and activate the PCMCIA device.
60 *
61 * If this function returns an error, comedi_pcmcia_disable() should be called
62 * to release requested resources.
63 *
64 * Return:
65 * 0 on success,
66 * -%ENODEV id @dev->hw_dev is %NULL,
67 * a negative error number from pcmcia_loop_config() if it fails,
68 * or a negative error number from pcmcia_enable_device() if it fails.
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -070069 */
H Hartley Sweetena3ac9512013-02-05 18:16:07 -070070int comedi_pcmcia_enable(struct comedi_device *dev,
Ian Abbott514871e2016-12-15 13:19:49 +000071 int (*conf_check)(struct pcmcia_device *p_dev,
72 void *priv_data))
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -070073{
74 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
75 int ret;
76
77 if (!link)
78 return -ENODEV;
79
H Hartley Sweetena3ac9512013-02-05 18:16:07 -070080 if (!conf_check)
81 conf_check = comedi_pcmcia_conf_check;
82
83 ret = pcmcia_loop_config(link, conf_check, NULL);
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -070084 if (ret)
85 return ret;
86
87 return pcmcia_enable_device(link);
88}
89EXPORT_SYMBOL_GPL(comedi_pcmcia_enable);
90
91/**
Ian Abbott127a0cf2015-09-30 17:10:57 +010092 * comedi_pcmcia_disable() - Disable the PCMCIA device and release the regions
93 * @dev: COMEDI device.
94 *
95 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
96 * a &struct device embedded in a &struct pcmcia_device. Call
97 * pcmcia_disable_device() to disable and clean up the PCMCIA device.
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -070098 */
99void comedi_pcmcia_disable(struct comedi_device *dev)
100{
101 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
102
103 if (link)
104 pcmcia_disable_device(link);
105}
106EXPORT_SYMBOL_GPL(comedi_pcmcia_disable);
107
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700108/**
Ian Abbott127a0cf2015-09-30 17:10:57 +0100109 * comedi_pcmcia_auto_config() - Configure/probe a PCMCIA COMEDI device
110 * @link: PCMCIA device.
111 * @driver: Registered COMEDI driver.
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700112 *
Ian Abbott127a0cf2015-09-30 17:10:57 +0100113 * Typically called from the pcmcia_driver (*probe) function. Auto-configure
114 * a COMEDI device, using a pointer to the &struct device embedded in *@link
115 * as the hardware device. The @driver's "auto_attach" handler may call
116 * comedi_to_pcmcia_dev() on the passed in COMEDI device to recover @link.
117 *
118 * Return: The result of calling comedi_auto_config() (0 on success, or a
119 * negative error number on failure).
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700120 */
121int comedi_pcmcia_auto_config(struct pcmcia_device *link,
122 struct comedi_driver *driver)
123{
124 return comedi_auto_config(&link->dev, driver, 0);
125}
126EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_config);
127
128/**
Ian Abbott127a0cf2015-09-30 17:10:57 +0100129 * comedi_pcmcia_auto_unconfig() - Unconfigure/remove a PCMCIA COMEDI device
130 * @link: PCMCIA device.
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700131 *
132 * Typically called from the pcmcia_driver (*remove) function.
Ian Abbott127a0cf2015-09-30 17:10:57 +0100133 * Auto-unconfigure a COMEDI device attached to this PCMCIA device, using a
134 * pointer to the &struct device embedded in *@link as the hardware device.
135 * The COMEDI driver's "detach" handler will be called during unconfiguration
136 * of the COMEDI device.
137 *
138 * Note that the COMEDI device may have already been unconfigured using the
139 * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
140 * again should be ignored.
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700141 */
142void comedi_pcmcia_auto_unconfig(struct pcmcia_device *link)
143{
144 comedi_auto_unconfig(&link->dev);
145}
146EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_unconfig);
147
148/**
Ian Abbott127a0cf2015-09-30 17:10:57 +0100149 * comedi_pcmcia_driver_register() - Register a PCMCIA COMEDI driver
150 * @comedi_driver: COMEDI driver to be registered.
151 * @pcmcia_driver: PCMCIA driver to be registered.
H Hartley Sweeten309231d2013-01-30 15:22:44 -0700152 *
Ian Abbott127a0cf2015-09-30 17:10:57 +0100153 * This function is used for the module_init() of PCMCIA COMEDI driver modules
154 * to register the COMEDI driver and the PCMCIA driver. Do not call it
155 * directly, use the module_comedi_pcmcia_driver() helper macro instead.
156 *
157 * Return: 0 on success, or a negative error number on failure.
H Hartley Sweeten309231d2013-01-30 15:22:44 -0700158 */
159int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
160 struct pcmcia_driver *pcmcia_driver)
161{
162 int ret;
163
164 ret = comedi_driver_register(comedi_driver);
165 if (ret < 0)
166 return ret;
167
168 ret = pcmcia_register_driver(pcmcia_driver);
169 if (ret < 0) {
170 comedi_driver_unregister(comedi_driver);
171 return ret;
172 }
173
174 return 0;
175}
176EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
177
178/**
Ian Abbott127a0cf2015-09-30 17:10:57 +0100179 * comedi_pcmcia_driver_unregister() - Unregister a PCMCIA COMEDI driver
180 * @comedi_driver: COMEDI driver to be registered.
181 * @pcmcia_driver: PCMCIA driver to be registered.
H Hartley Sweeten309231d2013-01-30 15:22:44 -0700182 *
Ian Abbott127a0cf2015-09-30 17:10:57 +0100183 * This function is called from the module_exit() of PCMCIA COMEDI driver
184 * modules to unregister the PCMCIA driver and the COMEDI driver. Do not call
185 * it directly, use the module_comedi_pcmcia_driver() helper macro instead.
H Hartley Sweeten309231d2013-01-30 15:22:44 -0700186 */
187void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
188 struct pcmcia_driver *pcmcia_driver)
189{
190 pcmcia_unregister_driver(pcmcia_driver);
191 comedi_driver_unregister(comedi_driver);
192}
193EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
Ian Abbottaae434b2014-10-31 17:47:37 +0000194
195static int __init comedi_pcmcia_init(void)
196{
197 return 0;
198}
199module_init(comedi_pcmcia_init);
200
201static void __exit comedi_pcmcia_exit(void)
202{
203}
204module_exit(comedi_pcmcia_exit);
205
Alexander A. Klimov13d8b1f2020-07-23 21:40:53 +0200206MODULE_AUTHOR("https://www.comedi.org");
Ian Abbottaae434b2014-10-31 17:47:37 +0000207MODULE_DESCRIPTION("Comedi PCMCIA interface module");
208MODULE_LICENSE("GPL");