3 * Comedi PCI driver specific functions.
5 * COMEDI - Linux Control and Measurement Device Interface
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.
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.
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
22 #include "comedi_pci.h"
25 * comedi_to_pci_dev() - Return PCI device attached to COMEDI device
26 * @dev: COMEDI device.
28 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
29 * a &struct device embedded in a &struct pci_dev.
31 * Return: Attached PCI device if @dev->hw_dev is non-%NULL.
32 * Return %NULL if @dev->hw_dev is %NULL.
34 struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
36 return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL;
38 EXPORT_SYMBOL_GPL(comedi_to_pci_dev);
41 * comedi_pci_enable() - Enable the PCI device and request the regions
42 * @dev: COMEDI device.
44 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
45 * a &struct device embedded in a &struct pci_dev. Enable the PCI device
46 * and request its regions. Set @dev->ioenabled to %true if successful,
47 * otherwise undo what was done.
49 * Calls to comedi_pci_enable() and comedi_pci_disable() cannot be nested.
53 * -%ENODEV if @dev->hw_dev is %NULL,
54 * -%EBUSY if regions busy,
55 * or some negative error number if failed to enable PCI device.
58 int comedi_pci_enable(struct comedi_device *dev)
60 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
66 rc = pci_enable_device(pcidev);
70 rc = pci_request_regions(pcidev, dev->board_name);
72 pci_disable_device(pcidev);
74 dev->ioenabled = true;
78 EXPORT_SYMBOL_GPL(comedi_pci_enable);
81 * comedi_pci_disable() - Release the regions and disable the PCI device
82 * @dev: COMEDI device.
84 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
85 * a &struct device embedded in a &struct pci_dev. If the earlier call
86 * to comedi_pci_enable() was successful, release the PCI device's regions
87 * and disable it. Reset @dev->ioenabled back to %false.
89 void comedi_pci_disable(struct comedi_device *dev)
91 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
93 if (pcidev && dev->ioenabled) {
94 pci_release_regions(pcidev);
95 pci_disable_device(pcidev);
97 dev->ioenabled = false;
99 EXPORT_SYMBOL_GPL(comedi_pci_disable);
102 * comedi_pci_detach() - A generic "detach" handler for PCI COMEDI drivers
103 * @dev: COMEDI device.
105 * COMEDI drivers for PCI devices that need no special clean-up of private data
106 * and have no ioremapped regions other than that pointed to by @dev->mmio may
107 * use this function as its "detach" handler called by the COMEDI core when a
108 * COMEDI device is being detached from the low-level driver. It may be also
109 * called from a more specific "detach" handler that does additional clean-up.
111 * Free the IRQ if @dev->irq is non-zero, iounmap @dev->mmio if it is
112 * non-%NULL, and call comedi_pci_disable() to release the PCI device's regions
115 void comedi_pci_detach(struct comedi_device *dev)
117 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
119 if (!pcidev || !dev->ioenabled)
123 free_irq(dev->irq, dev);
130 comedi_pci_disable(dev);
132 EXPORT_SYMBOL_GPL(comedi_pci_detach);
135 * comedi_pci_auto_config() - Configure/probe a PCI COMEDI device
136 * @pcidev: PCI device.
137 * @driver: Registered COMEDI driver.
138 * @context: Driver specific data, passed to comedi_auto_config().
140 * Typically called from the pci_driver (*probe) function. Auto-configure
141 * a COMEDI device, using the &struct device embedded in *@pcidev as the
142 * hardware device. The @context value gets passed through to @driver's
143 * "auto_attach" handler. The "auto_attach" handler may call
144 * comedi_to_pci_dev() on the passed in COMEDI device to recover @pcidev.
146 * Return: The result of calling comedi_auto_config() (0 on success, or
147 * a negative error number on failure).
149 int comedi_pci_auto_config(struct pci_dev *pcidev,
150 struct comedi_driver *driver,
151 unsigned long context)
153 return comedi_auto_config(&pcidev->dev, driver, context);
155 EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
158 * comedi_pci_auto_unconfig() - Unconfigure/remove a PCI COMEDI device
159 * @pcidev: PCI device.
161 * Typically called from the pci_driver (*remove) function. Auto-unconfigure
162 * a COMEDI device attached to this PCI device, using a pointer to the
163 * &struct device embedded in *@pcidev as the hardware device. The COMEDI
164 * driver's "detach" handler will be called during unconfiguration of the
167 * Note that the COMEDI device may have already been unconfigured using the
168 * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
169 * again should be ignored.
171 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
173 comedi_auto_unconfig(&pcidev->dev);
175 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
178 * comedi_pci_driver_register() - Register a PCI COMEDI driver
179 * @comedi_driver: COMEDI driver to be registered.
180 * @pci_driver: PCI driver to be registered.
182 * This function is called from the module_init() of PCI COMEDI driver modules
183 * to register the COMEDI driver and the PCI driver. Do not call it directly,
184 * use the module_comedi_pci_driver() helper macro instead.
186 * Return: 0 on success, or a negative error number on failure.
188 int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
189 struct pci_driver *pci_driver)
193 ret = comedi_driver_register(comedi_driver);
197 ret = pci_register_driver(pci_driver);
199 comedi_driver_unregister(comedi_driver);
205 EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
208 * comedi_pci_driver_unregister() - Unregister a PCI COMEDI driver
209 * @comedi_driver: COMEDI driver to be unregistered.
210 * @pci_driver: PCI driver to be unregistered.
212 * This function is called from the module_exit() of PCI COMEDI driver modules
213 * to unregister the PCI driver and the COMEDI driver. Do not call it
214 * directly, use the module_comedi_pci_driver() helper macro instead.
216 void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
217 struct pci_driver *pci_driver)
219 pci_unregister_driver(pci_driver);
220 comedi_driver_unregister(comedi_driver);
222 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
224 static int __init comedi_pci_init(void)
228 module_init(comedi_pci_init);
230 static void __exit comedi_pci_exit(void)
233 module_exit(comedi_pci_exit);
235 MODULE_AUTHOR("http://www.comedi.org");
236 MODULE_DESCRIPTION("Comedi PCI interface module");
237 MODULE_LICENSE("GPL");