Greg Kroah-Hartman | e184e2b | 2017-11-07 14:58:43 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * comedi_pci.c |
| 4 | * Comedi PCI 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 Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Ian Abbott | bc3fe15 | 2014-10-31 17:47:39 +0000 | [diff] [blame] | 10 | #include <linux/module.h> |
H Hartley Sweeten | aac307f | 2014-08-25 17:55:48 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 12 | |
Ian Abbott | 7b39675 | 2015-03-10 16:10:21 +0000 | [diff] [blame] | 13 | #include "comedi_pci.h" |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 16 | * comedi_to_pci_dev() - Return PCI device attached to COMEDI device |
| 17 | * @dev: COMEDI device. |
| 18 | * |
| 19 | * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a |
| 20 | * a &struct device embedded in a &struct pci_dev. |
| 21 | * |
Ian Abbott | ea1ea69 | 2015-09-30 16:43:55 +0100 | [diff] [blame] | 22 | * Return: Attached PCI device if @dev->hw_dev is non-%NULL. |
| 23 | * Return %NULL if @dev->hw_dev is %NULL. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 24 | */ |
| 25 | struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev) |
| 26 | { |
| 27 | return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL; |
| 28 | } |
| 29 | EXPORT_SYMBOL_GPL(comedi_to_pci_dev); |
| 30 | |
| 31 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 32 | * comedi_pci_enable() - Enable the PCI device and request the regions |
| 33 | * @dev: COMEDI device. |
| 34 | * |
| 35 | * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a |
| 36 | * a &struct device embedded in a &struct pci_dev. Enable the PCI device |
| 37 | * and request its regions. Set @dev->ioenabled to %true if successful, |
| 38 | * otherwise undo what was done. |
| 39 | * |
| 40 | * Calls to comedi_pci_enable() and comedi_pci_disable() cannot be nested. |
| 41 | * |
Ian Abbott | ea1ea69 | 2015-09-30 16:43:55 +0100 | [diff] [blame] | 42 | * Return: |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 43 | * 0 on success, |
| 44 | * -%ENODEV if @dev->hw_dev is %NULL, |
| 45 | * -%EBUSY if regions busy, |
| 46 | * or some negative error number if failed to enable PCI device. |
| 47 | * |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 48 | */ |
H Hartley Sweeten | 818f569 | 2013-03-13 10:36:31 -0700 | [diff] [blame] | 49 | int comedi_pci_enable(struct comedi_device *dev) |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 50 | { |
H Hartley Sweeten | 818f569 | 2013-03-13 10:36:31 -0700 | [diff] [blame] | 51 | struct pci_dev *pcidev = comedi_to_pci_dev(dev); |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 52 | int rc; |
| 53 | |
H Hartley Sweeten | 818f569 | 2013-03-13 10:36:31 -0700 | [diff] [blame] | 54 | if (!pcidev) |
| 55 | return -ENODEV; |
| 56 | |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 57 | rc = pci_enable_device(pcidev); |
| 58 | if (rc < 0) |
| 59 | return rc; |
| 60 | |
H Hartley Sweeten | 46c5812 | 2013-04-08 10:56:20 -0700 | [diff] [blame] | 61 | rc = pci_request_regions(pcidev, dev->board_name); |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 62 | if (rc < 0) |
| 63 | pci_disable_device(pcidev); |
Ian Abbott | 00ca6884 | 2013-03-15 13:15:35 +0000 | [diff] [blame] | 64 | else |
| 65 | dev->ioenabled = true; |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(comedi_pci_enable); |
| 70 | |
| 71 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 72 | * comedi_pci_disable() - Release the regions and disable the PCI device |
| 73 | * @dev: COMEDI device. |
| 74 | * |
| 75 | * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a |
| 76 | * a &struct device embedded in a &struct pci_dev. If the earlier call |
| 77 | * to comedi_pci_enable() was successful, release the PCI device's regions |
| 78 | * and disable it. Reset @dev->ioenabled back to %false. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 79 | */ |
H Hartley Sweeten | 7f072f5 | 2013-03-13 10:35:51 -0700 | [diff] [blame] | 80 | void comedi_pci_disable(struct comedi_device *dev) |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 81 | { |
H Hartley Sweeten | 7f072f5 | 2013-03-13 10:35:51 -0700 | [diff] [blame] | 82 | struct pci_dev *pcidev = comedi_to_pci_dev(dev); |
| 83 | |
Ian Abbott | 00ca6884 | 2013-03-15 13:15:35 +0000 | [diff] [blame] | 84 | if (pcidev && dev->ioenabled) { |
H Hartley Sweeten | 7f072f5 | 2013-03-13 10:35:51 -0700 | [diff] [blame] | 85 | pci_release_regions(pcidev); |
| 86 | pci_disable_device(pcidev); |
| 87 | } |
Ian Abbott | 00ca6884 | 2013-03-15 13:15:35 +0000 | [diff] [blame] | 88 | dev->ioenabled = false; |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 89 | } |
| 90 | EXPORT_SYMBOL_GPL(comedi_pci_disable); |
| 91 | |
| 92 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 93 | * comedi_pci_detach() - A generic "detach" handler for PCI COMEDI drivers |
| 94 | * @dev: COMEDI device. |
| 95 | * |
| 96 | * COMEDI drivers for PCI devices that need no special clean-up of private data |
| 97 | * and have no ioremapped regions other than that pointed to by @dev->mmio may |
| 98 | * use this function as its "detach" handler called by the COMEDI core when a |
| 99 | * COMEDI device is being detached from the low-level driver. It may be also |
| 100 | * called from a more specific "detach" handler that does additional clean-up. |
| 101 | * |
| 102 | * Free the IRQ if @dev->irq is non-zero, iounmap @dev->mmio if it is |
| 103 | * non-%NULL, and call comedi_pci_disable() to release the PCI device's regions |
| 104 | * and disable it. |
H Hartley Sweeten | aac307f | 2014-08-25 17:55:48 -0700 | [diff] [blame] | 105 | */ |
| 106 | void comedi_pci_detach(struct comedi_device *dev) |
| 107 | { |
| 108 | struct pci_dev *pcidev = comedi_to_pci_dev(dev); |
| 109 | |
| 110 | if (!pcidev || !dev->ioenabled) |
| 111 | return; |
| 112 | |
| 113 | if (dev->irq) { |
| 114 | free_irq(dev->irq, dev); |
| 115 | dev->irq = 0; |
| 116 | } |
| 117 | if (dev->mmio) { |
| 118 | iounmap(dev->mmio); |
| 119 | dev->mmio = NULL; |
| 120 | } |
| 121 | comedi_pci_disable(dev); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(comedi_pci_detach); |
| 124 | |
| 125 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 126 | * comedi_pci_auto_config() - Configure/probe a PCI COMEDI device |
| 127 | * @pcidev: PCI device. |
| 128 | * @driver: Registered COMEDI driver. |
| 129 | * @context: Driver specific data, passed to comedi_auto_config(). |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 130 | * |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 131 | * Typically called from the pci_driver (*probe) function. Auto-configure |
| 132 | * a COMEDI device, using the &struct device embedded in *@pcidev as the |
| 133 | * hardware device. The @context value gets passed through to @driver's |
| 134 | * "auto_attach" handler. The "auto_attach" handler may call |
| 135 | * comedi_to_pci_dev() on the passed in COMEDI device to recover @pcidev. |
| 136 | * |
Ian Abbott | ea1ea69 | 2015-09-30 16:43:55 +0100 | [diff] [blame] | 137 | * Return: The result of calling comedi_auto_config() (0 on success, or |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 138 | * a negative error number on failure). |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 139 | */ |
| 140 | int comedi_pci_auto_config(struct pci_dev *pcidev, |
H Hartley Sweeten | b8f4ac2 | 2013-03-05 09:53:41 -0700 | [diff] [blame] | 141 | struct comedi_driver *driver, |
| 142 | unsigned long context) |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 143 | { |
H Hartley Sweeten | b8f4ac2 | 2013-03-05 09:53:41 -0700 | [diff] [blame] | 144 | return comedi_auto_config(&pcidev->dev, driver, context); |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 145 | } |
| 146 | EXPORT_SYMBOL_GPL(comedi_pci_auto_config); |
| 147 | |
| 148 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 149 | * comedi_pci_auto_unconfig() - Unconfigure/remove a PCI COMEDI device |
| 150 | * @pcidev: PCI device. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 151 | * |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 152 | * Typically called from the pci_driver (*remove) function. Auto-unconfigure |
| 153 | * a COMEDI device attached to this PCI device, using a pointer to the |
| 154 | * &struct device embedded in *@pcidev as the hardware device. The COMEDI |
| 155 | * driver's "detach" handler will be called during unconfiguration of the |
| 156 | * COMEDI device. |
| 157 | * |
| 158 | * Note that the COMEDI device may have already been unconfigured using the |
| 159 | * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it |
| 160 | * again should be ignored. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 161 | */ |
| 162 | void comedi_pci_auto_unconfig(struct pci_dev *pcidev) |
| 163 | { |
| 164 | comedi_auto_unconfig(&pcidev->dev); |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig); |
| 167 | |
| 168 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 169 | * comedi_pci_driver_register() - Register a PCI COMEDI driver |
| 170 | * @comedi_driver: COMEDI driver to be registered. |
| 171 | * @pci_driver: PCI driver to be registered. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 172 | * |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 173 | * This function is called from the module_init() of PCI COMEDI driver modules |
| 174 | * to register the COMEDI driver and the PCI driver. Do not call it directly, |
| 175 | * use the module_comedi_pci_driver() helper macro instead. |
| 176 | * |
Ian Abbott | ea1ea69 | 2015-09-30 16:43:55 +0100 | [diff] [blame] | 177 | * Return: 0 on success, or a negative error number on failure. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 178 | */ |
| 179 | int comedi_pci_driver_register(struct comedi_driver *comedi_driver, |
| 180 | struct pci_driver *pci_driver) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | ret = comedi_driver_register(comedi_driver); |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | |
| 188 | ret = pci_register_driver(pci_driver); |
| 189 | if (ret < 0) { |
| 190 | comedi_driver_unregister(comedi_driver); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(comedi_pci_driver_register); |
| 197 | |
| 198 | /** |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 199 | * comedi_pci_driver_unregister() - Unregister a PCI COMEDI driver |
| 200 | * @comedi_driver: COMEDI driver to be unregistered. |
| 201 | * @pci_driver: PCI driver to be unregistered. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 202 | * |
Ian Abbott | 7a064fd | 2015-09-23 19:35:56 +0100 | [diff] [blame] | 203 | * This function is called from the module_exit() of PCI COMEDI driver modules |
| 204 | * to unregister the PCI driver and the COMEDI driver. Do not call it |
| 205 | * directly, use the module_comedi_pci_driver() helper macro instead. |
H Hartley Sweeten | 33782dd | 2013-01-30 15:22:21 -0700 | [diff] [blame] | 206 | */ |
| 207 | void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver, |
| 208 | struct pci_driver *pci_driver) |
| 209 | { |
| 210 | pci_unregister_driver(pci_driver); |
| 211 | comedi_driver_unregister(comedi_driver); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister); |
Ian Abbott | bc3fe15 | 2014-10-31 17:47:39 +0000 | [diff] [blame] | 214 | |
| 215 | static int __init comedi_pci_init(void) |
| 216 | { |
| 217 | return 0; |
| 218 | } |
| 219 | module_init(comedi_pci_init); |
| 220 | |
| 221 | static void __exit comedi_pci_exit(void) |
| 222 | { |
| 223 | } |
| 224 | module_exit(comedi_pci_exit); |
| 225 | |
Alexander A. Klimov | 13d8b1f | 2020-07-23 21:40:53 +0200 | [diff] [blame] | 226 | MODULE_AUTHOR("https://www.comedi.org"); |
Ian Abbott | bc3fe15 | 2014-10-31 17:47:39 +0000 | [diff] [blame] | 227 | MODULE_DESCRIPTION("Comedi PCI interface module"); |
| 228 | MODULE_LICENSE("GPL"); |