Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * comedi_pcmcia.c
0004  * Comedi PCMCIA driver specific functions.
0005  *
0006  * COMEDI - Linux Control and Measurement Device Interface
0007  * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/comedi/comedi_pcmcia.h>
0013 
0014 /**
0015  * comedi_to_pcmcia_dev() - Return PCMCIA device attached to COMEDI device
0016  * @dev: COMEDI device.
0017  *
0018  * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
0019  * a &struct device embedded in a &struct pcmcia_device.
0020  *
0021  * Return: Attached PCMCIA device if @dev->hw_dev is non-%NULL.
0022  * Return %NULL if @dev->hw_dev is %NULL.
0023  */
0024 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *dev)
0025 {
0026     return dev->hw_dev ? to_pcmcia_dev(dev->hw_dev) : NULL;
0027 }
0028 EXPORT_SYMBOL_GPL(comedi_to_pcmcia_dev);
0029 
0030 static int comedi_pcmcia_conf_check(struct pcmcia_device *link,
0031                     void *priv_data)
0032 {
0033     if (link->config_index == 0)
0034         return -EINVAL;
0035 
0036     return pcmcia_request_io(link);
0037 }
0038 
0039 /**
0040  * comedi_pcmcia_enable() - Request the regions and enable the PCMCIA device
0041  * @dev: COMEDI device.
0042  * @conf_check: Optional callback to check each configuration option of the
0043  *  PCMCIA device and request I/O regions.
0044  *
0045  * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a a
0046  * &struct device embedded in a &struct pcmcia_device.  The comedi PCMCIA
0047  * driver needs to set the 'config_flags' member in the &struct pcmcia_device,
0048  * as appropriate for that driver, before calling this function in order to
0049  * allow pcmcia_loop_config() to do its internal autoconfiguration.
0050  *
0051  * If @conf_check is %NULL it is set to a default function.  If is
0052  * passed to pcmcia_loop_config() and should return %0 if the configuration
0053  * is valid and I/O regions requested successfully, otherwise it should return
0054  * a negative error value.  The default function returns -%EINVAL if the
0055  * 'config_index' member is %0, otherwise it calls pcmcia_request_io() and
0056  * returns the result.
0057  *
0058  * If the above configuration check passes, pcmcia_enable_device() is called
0059  * to set up and activate the PCMCIA device.
0060  *
0061  * If this function returns an error, comedi_pcmcia_disable() should be called
0062  * to release requested resources.
0063  *
0064  * Return:
0065  *  0 on success,
0066  *  -%ENODEV id @dev->hw_dev is %NULL,
0067  *  a negative error number from pcmcia_loop_config() if it fails,
0068  *  or a negative error number from pcmcia_enable_device() if it fails.
0069  */
0070 int comedi_pcmcia_enable(struct comedi_device *dev,
0071              int (*conf_check)(struct pcmcia_device *p_dev,
0072                        void *priv_data))
0073 {
0074     struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
0075     int ret;
0076 
0077     if (!link)
0078         return -ENODEV;
0079 
0080     if (!conf_check)
0081         conf_check = comedi_pcmcia_conf_check;
0082 
0083     ret = pcmcia_loop_config(link, conf_check, NULL);
0084     if (ret)
0085         return ret;
0086 
0087     return pcmcia_enable_device(link);
0088 }
0089 EXPORT_SYMBOL_GPL(comedi_pcmcia_enable);
0090 
0091 /**
0092  * comedi_pcmcia_disable() - Disable the PCMCIA device and release the regions
0093  * @dev: COMEDI device.
0094  *
0095  * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
0096  * a &struct device embedded in a &struct pcmcia_device.  Call
0097  * pcmcia_disable_device() to disable and clean up the PCMCIA device.
0098  */
0099 void comedi_pcmcia_disable(struct comedi_device *dev)
0100 {
0101     struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
0102 
0103     if (link)
0104         pcmcia_disable_device(link);
0105 }
0106 EXPORT_SYMBOL_GPL(comedi_pcmcia_disable);
0107 
0108 /**
0109  * comedi_pcmcia_auto_config() - Configure/probe a PCMCIA COMEDI device
0110  * @link: PCMCIA device.
0111  * @driver: Registered COMEDI driver.
0112  *
0113  * Typically called from the pcmcia_driver (*probe) function.  Auto-configure
0114  * a COMEDI device, using a pointer to the &struct device embedded in *@link
0115  * as the hardware device.  The @driver's "auto_attach" handler may call
0116  * comedi_to_pcmcia_dev() on the passed in COMEDI device to recover @link.
0117  *
0118  * Return: The result of calling comedi_auto_config() (0 on success, or a
0119  * negative error number on failure).
0120  */
0121 int comedi_pcmcia_auto_config(struct pcmcia_device *link,
0122                   struct comedi_driver *driver)
0123 {
0124     return comedi_auto_config(&link->dev, driver, 0);
0125 }
0126 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_config);
0127 
0128 /**
0129  * comedi_pcmcia_auto_unconfig() - Unconfigure/remove a PCMCIA COMEDI device
0130  * @link: PCMCIA device.
0131  *
0132  * Typically called from the pcmcia_driver (*remove) function.
0133  * Auto-unconfigure a COMEDI device attached to this PCMCIA device, using a
0134  * pointer to the &struct device embedded in *@link as the hardware device.
0135  * The COMEDI driver's "detach" handler will be called during unconfiguration
0136  * of the COMEDI device.
0137  *
0138  * Note that the COMEDI device may have already been unconfigured using the
0139  * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
0140  * again should be ignored.
0141  */
0142 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *link)
0143 {
0144     comedi_auto_unconfig(&link->dev);
0145 }
0146 EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_unconfig);
0147 
0148 /**
0149  * comedi_pcmcia_driver_register() - Register a PCMCIA COMEDI driver
0150  * @comedi_driver: COMEDI driver to be registered.
0151  * @pcmcia_driver: PCMCIA driver to be registered.
0152  *
0153  * This function is used for the module_init() of PCMCIA COMEDI driver modules
0154  * to register the COMEDI driver and the PCMCIA driver.  Do not call it
0155  * directly, use the module_comedi_pcmcia_driver() helper macro instead.
0156  *
0157  * Return: 0 on success, or a negative error number on failure.
0158  */
0159 int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
0160                   struct pcmcia_driver *pcmcia_driver)
0161 {
0162     int ret;
0163 
0164     ret = comedi_driver_register(comedi_driver);
0165     if (ret < 0)
0166         return ret;
0167 
0168     ret = pcmcia_register_driver(pcmcia_driver);
0169     if (ret < 0) {
0170         comedi_driver_unregister(comedi_driver);
0171         return ret;
0172     }
0173 
0174     return 0;
0175 }
0176 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
0177 
0178 /**
0179  * comedi_pcmcia_driver_unregister() - Unregister a PCMCIA COMEDI driver
0180  * @comedi_driver: COMEDI driver to be registered.
0181  * @pcmcia_driver: PCMCIA driver to be registered.
0182  *
0183  * This function is called from the module_exit() of PCMCIA COMEDI driver
0184  * modules to unregister the PCMCIA driver and the COMEDI driver.  Do not call
0185  * it directly, use the module_comedi_pcmcia_driver() helper macro instead.
0186  */
0187 void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
0188                      struct pcmcia_driver *pcmcia_driver)
0189 {
0190     pcmcia_unregister_driver(pcmcia_driver);
0191     comedi_driver_unregister(comedi_driver);
0192 }
0193 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
0194 
0195 static int __init comedi_pcmcia_init(void)
0196 {
0197     return 0;
0198 }
0199 module_init(comedi_pcmcia_init);
0200 
0201 static void __exit comedi_pcmcia_exit(void)
0202 {
0203 }
0204 module_exit(comedi_pcmcia_exit);
0205 
0206 MODULE_AUTHOR("https://www.comedi.org");
0207 MODULE_DESCRIPTION("Comedi PCMCIA interface module");
0208 MODULE_LICENSE("GPL");