1 // SPDX-License-Identifier: GPL-2.0+
4 * Comedi PCMCIA driver specific functions.
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
13 #include "comedi_pcmcia.h"
16 * comedi_to_pcmcia_dev() - Return PCMCIA device attached to COMEDI device
17 * @dev: COMEDI device.
19 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
20 * a &struct device embedded in a &struct pcmcia_device.
22 * Return: Attached PCMCIA device if @dev->hw_dev is non-%NULL.
23 * Return %NULL if @dev->hw_dev is %NULL.
25 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *dev)
27 return dev->hw_dev ? to_pcmcia_dev(dev->hw_dev) : NULL;
29 EXPORT_SYMBOL_GPL(comedi_to_pcmcia_dev);
31 static int comedi_pcmcia_conf_check(struct pcmcia_device *link,
34 if (link->config_index == 0)
37 return pcmcia_request_io(link);
41 * comedi_pcmcia_enable() - Request the regions and enable the PCMCIA device
42 * @dev: COMEDI device.
43 * @conf_check: Optional callback to check each configuration option of the
44 * PCMCIA device and request I/O regions.
46 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a a
47 * &struct device embedded in a &struct pcmcia_device. The comedi PCMCIA
48 * driver needs to set the 'config_flags' member in the &struct pcmcia_device,
49 * as appropriate for that driver, before calling this function in order to
50 * allow pcmcia_loop_config() to do its internal autoconfiguration.
52 * If @conf_check is %NULL it is set to a default function. If is
53 * passed to pcmcia_loop_config() and should return %0 if the configuration
54 * is valid and I/O regions requested successfully, otherwise it should return
55 * a negative error value. The default function returns -%EINVAL if the
56 * 'config_index' member is %0, otherwise it calls pcmcia_request_io() and
59 * If the above configuration check passes, pcmcia_enable_device() is called
60 * to set up and activate the PCMCIA device.
62 * If this function returns an error, comedi_pcmcia_disable() should be called
63 * to release requested resources.
67 * -%ENODEV id @dev->hw_dev is %NULL,
68 * a negative error number from pcmcia_loop_config() if it fails,
69 * or a negative error number from pcmcia_enable_device() if it fails.
71 int comedi_pcmcia_enable(struct comedi_device *dev,
72 int (*conf_check)(struct pcmcia_device *p_dev,
75 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
82 conf_check = comedi_pcmcia_conf_check;
84 ret = pcmcia_loop_config(link, conf_check, NULL);
88 return pcmcia_enable_device(link);
90 EXPORT_SYMBOL_GPL(comedi_pcmcia_enable);
93 * comedi_pcmcia_disable() - Disable the PCMCIA device and release the regions
94 * @dev: COMEDI device.
96 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
97 * a &struct device embedded in a &struct pcmcia_device. Call
98 * pcmcia_disable_device() to disable and clean up the PCMCIA device.
100 void comedi_pcmcia_disable(struct comedi_device *dev)
102 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
105 pcmcia_disable_device(link);
107 EXPORT_SYMBOL_GPL(comedi_pcmcia_disable);
110 * comedi_pcmcia_auto_config() - Configure/probe a PCMCIA COMEDI device
111 * @link: PCMCIA device.
112 * @driver: Registered COMEDI driver.
114 * Typically called from the pcmcia_driver (*probe) function. Auto-configure
115 * a COMEDI device, using a pointer to the &struct device embedded in *@link
116 * as the hardware device. The @driver's "auto_attach" handler may call
117 * comedi_to_pcmcia_dev() on the passed in COMEDI device to recover @link.
119 * Return: The result of calling comedi_auto_config() (0 on success, or a
120 * negative error number on failure).
122 int comedi_pcmcia_auto_config(struct pcmcia_device *link,
123 struct comedi_driver *driver)
125 return comedi_auto_config(&link->dev, driver, 0);
127 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_config);
130 * comedi_pcmcia_auto_unconfig() - Unconfigure/remove a PCMCIA COMEDI device
131 * @link: PCMCIA device.
133 * Typically called from the pcmcia_driver (*remove) function.
134 * Auto-unconfigure a COMEDI device attached to this PCMCIA device, using a
135 * pointer to the &struct device embedded in *@link as the hardware device.
136 * The COMEDI driver's "detach" handler will be called during unconfiguration
137 * of the COMEDI device.
139 * Note that the COMEDI device may have already been unconfigured using the
140 * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
141 * again should be ignored.
143 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *link)
145 comedi_auto_unconfig(&link->dev);
147 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_unconfig);
150 * comedi_pcmcia_driver_register() - Register a PCMCIA COMEDI driver
151 * @comedi_driver: COMEDI driver to be registered.
152 * @pcmcia_driver: PCMCIA driver to be registered.
154 * This function is used for the module_init() of PCMCIA COMEDI driver modules
155 * to register the COMEDI driver and the PCMCIA driver. Do not call it
156 * directly, use the module_comedi_pcmcia_driver() helper macro instead.
158 * Return: 0 on success, or a negative error number on failure.
160 int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
161 struct pcmcia_driver *pcmcia_driver)
165 ret = comedi_driver_register(comedi_driver);
169 ret = pcmcia_register_driver(pcmcia_driver);
171 comedi_driver_unregister(comedi_driver);
177 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
180 * comedi_pcmcia_driver_unregister() - Unregister a PCMCIA COMEDI driver
181 * @comedi_driver: COMEDI driver to be registered.
182 * @pcmcia_driver: PCMCIA driver to be registered.
184 * This function is called from the module_exit() of PCMCIA COMEDI driver
185 * modules to unregister the PCMCIA driver and the COMEDI driver. Do not call
186 * it directly, use the module_comedi_pcmcia_driver() helper macro instead.
188 void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
189 struct pcmcia_driver *pcmcia_driver)
191 pcmcia_unregister_driver(pcmcia_driver);
192 comedi_driver_unregister(comedi_driver);
194 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
196 static int __init comedi_pcmcia_init(void)
200 module_init(comedi_pcmcia_init);
202 static void __exit comedi_pcmcia_exit(void)
205 module_exit(comedi_pcmcia_exit);
207 MODULE_AUTHOR("https://www.comedi.org");
208 MODULE_DESCRIPTION("Comedi PCMCIA interface module");
209 MODULE_LICENSE("GPL");