1/* 2 * comedi_pci.c 3 * Comedi PCI 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/interrupt.h> 21 22#include "comedi_pci.h" 23 24/** 25 * comedi_to_pci_dev() - Return PCI 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 pci_dev. 30 * 31 * Return: Attached PCI device if @dev->hw_dev is non-%NULL. 32 * Return %NULL if @dev->hw_dev is %NULL. 33 */ 34struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev) 35{ 36 return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL; 37} 38EXPORT_SYMBOL_GPL(comedi_to_pci_dev); 39 40/** 41 * comedi_pci_enable() - Enable the PCI device and request the regions 42 * @dev: COMEDI device. 43 * 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. 48 * 49 * Calls to comedi_pci_enable() and comedi_pci_disable() cannot be nested. 50 * 51 * Return: 52 * 0 on success, 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. 56 * 57 */ 58int comedi_pci_enable(struct comedi_device *dev) 59{ 60 struct pci_dev *pcidev = comedi_to_pci_dev(dev); 61 int rc; 62 63 if (!pcidev) 64 return -ENODEV; 65 66 rc = pci_enable_device(pcidev); 67 if (rc < 0) 68 return rc; 69 70 rc = pci_request_regions(pcidev, dev->board_name); 71 if (rc < 0) 72 pci_disable_device(pcidev); 73 else 74 dev->ioenabled = true; 75 76 return rc; 77} 78EXPORT_SYMBOL_GPL(comedi_pci_enable); 79 80/** 81 * comedi_pci_disable() - Release the regions and disable the PCI device 82 * @dev: COMEDI device. 83 * 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. 88 */ 89void comedi_pci_disable(struct comedi_device *dev) 90{ 91 struct pci_dev *pcidev = comedi_to_pci_dev(dev); 92 93 if (pcidev && dev->ioenabled) { 94 pci_release_regions(pcidev); 95 pci_disable_device(pcidev); 96 } 97 dev->ioenabled = false; 98} 99EXPORT_SYMBOL_GPL(comedi_pci_disable); 100 101/** 102 * comedi_pci_detach() - A generic "detach" handler for PCI COMEDI drivers 103 * @dev: COMEDI device. 104 * 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. 110 * 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 113 * and disable it. 114 */ 115void comedi_pci_detach(struct comedi_device *dev) 116{ 117 struct pci_dev *pcidev = comedi_to_pci_dev(dev); 118 119 if (!pcidev || !dev->ioenabled) 120 return; 121 122 if (dev->irq) { 123 free_irq(dev->irq, dev); 124 dev->irq = 0; 125 } 126 if (dev->mmio) { 127 iounmap(dev->mmio); 128 dev->mmio = NULL; 129 } 130 comedi_pci_disable(dev); 131} 132EXPORT_SYMBOL_GPL(comedi_pci_detach); 133 134/** 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(). 139 * 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. 145 * 146 * Return: The result of calling comedi_auto_config() (0 on success, or 147 * a negative error number on failure). 148 */ 149int comedi_pci_auto_config(struct pci_dev *pcidev, 150 struct comedi_driver *driver, 151 unsigned long context) 152{ 153 return comedi_auto_config(&pcidev->dev, driver, context); 154} 155EXPORT_SYMBOL_GPL(comedi_pci_auto_config); 156 157/** 158 * comedi_pci_auto_unconfig() - Unconfigure/remove a PCI COMEDI device 159 * @pcidev: PCI device. 160 * 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 165 * COMEDI device. 166 * 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. 170 */ 171void comedi_pci_auto_unconfig(struct pci_dev *pcidev) 172{ 173 comedi_auto_unconfig(&pcidev->dev); 174} 175EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig); 176 177/** 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. 181 * 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. 185 * 186 * Return: 0 on success, or a negative error number on failure. 187 */ 188int comedi_pci_driver_register(struct comedi_driver *comedi_driver, 189 struct pci_driver *pci_driver) 190{ 191 int ret; 192 193 ret = comedi_driver_register(comedi_driver); 194 if (ret < 0) 195 return ret; 196 197 ret = pci_register_driver(pci_driver); 198 if (ret < 0) { 199 comedi_driver_unregister(comedi_driver); 200 return ret; 201 } 202 203 return 0; 204} 205EXPORT_SYMBOL_GPL(comedi_pci_driver_register); 206 207/** 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. 211 * 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. 215 */ 216void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver, 217 struct pci_driver *pci_driver) 218{ 219 pci_unregister_driver(pci_driver); 220 comedi_driver_unregister(comedi_driver); 221} 222EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister); 223 224static int __init comedi_pci_init(void) 225{ 226 return 0; 227} 228module_init(comedi_pci_init); 229 230static void __exit comedi_pci_exit(void) 231{ 232} 233module_exit(comedi_pci_exit); 234 235MODULE_AUTHOR("http://www.comedi.org"); 236MODULE_DESCRIPTION("Comedi PCI interface module"); 237MODULE_LICENSE("GPL"); 238