Commit | Line | Data |
---|---|---|
33782dd5 HS |
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. | |
33782dd5 HS |
17 | */ |
18 | ||
bc3fe156 | 19 | #include <linux/module.h> |
aac307f9 | 20 | #include <linux/interrupt.h> |
33782dd5 | 21 | |
7b39675a | 22 | #include "comedi_pci.h" |
33782dd5 HS |
23 | |
24 | /** | |
7a064fd1 IA |
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 | * | |
ea1ea695 IA |
31 | * Return: Attached PCI device if @dev->hw_dev is non-%NULL. |
32 | * Return %NULL if @dev->hw_dev is %NULL. | |
33782dd5 HS |
33 | */ |
34 | struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev) | |
35 | { | |
36 | return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL; | |
37 | } | |
38 | EXPORT_SYMBOL_GPL(comedi_to_pci_dev); | |
39 | ||
40 | /** | |
7a064fd1 IA |
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 | * | |
ea1ea695 | 51 | * Return: |
7a064fd1 IA |
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 | * | |
33782dd5 | 57 | */ |
818f569f | 58 | int comedi_pci_enable(struct comedi_device *dev) |
33782dd5 | 59 | { |
818f569f | 60 | struct pci_dev *pcidev = comedi_to_pci_dev(dev); |
33782dd5 HS |
61 | int rc; |
62 | ||
818f569f HS |
63 | if (!pcidev) |
64 | return -ENODEV; | |
65 | ||
33782dd5 HS |
66 | rc = pci_enable_device(pcidev); |
67 | if (rc < 0) | |
68 | return rc; | |
69 | ||
46c58127 | 70 | rc = pci_request_regions(pcidev, dev->board_name); |
33782dd5 HS |
71 | if (rc < 0) |
72 | pci_disable_device(pcidev); | |
00ca6884 IA |
73 | else |
74 | dev->ioenabled = true; | |
33782dd5 HS |
75 | |
76 | return rc; | |
77 | } | |
78 | EXPORT_SYMBOL_GPL(comedi_pci_enable); | |
79 | ||
80 | /** | |
7a064fd1 IA |
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. | |
33782dd5 | 88 | */ |
7f072f54 | 89 | void comedi_pci_disable(struct comedi_device *dev) |
33782dd5 | 90 | { |
7f072f54 HS |
91 | struct pci_dev *pcidev = comedi_to_pci_dev(dev); |
92 | ||
00ca6884 | 93 | if (pcidev && dev->ioenabled) { |
7f072f54 HS |
94 | pci_release_regions(pcidev); |
95 | pci_disable_device(pcidev); | |
96 | } | |
00ca6884 | 97 | dev->ioenabled = false; |
33782dd5 HS |
98 | } |
99 | EXPORT_SYMBOL_GPL(comedi_pci_disable); | |
100 | ||
aac307f9 | 101 | /** |
7a064fd1 IA |
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. | |
aac307f9 HS |
114 | */ |
115 | void 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 | } | |
132 | EXPORT_SYMBOL_GPL(comedi_pci_detach); | |
133 | ||
33782dd5 | 134 | /** |
7a064fd1 IA |
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(). | |
33782dd5 | 139 | * |
7a064fd1 IA |
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 | * | |
ea1ea695 | 146 | * Return: The result of calling comedi_auto_config() (0 on success, or |
7a064fd1 | 147 | * a negative error number on failure). |
33782dd5 HS |
148 | */ |
149 | int comedi_pci_auto_config(struct pci_dev *pcidev, | |
b8f4ac23 HS |
150 | struct comedi_driver *driver, |
151 | unsigned long context) | |
33782dd5 | 152 | { |
b8f4ac23 | 153 | return comedi_auto_config(&pcidev->dev, driver, context); |
33782dd5 HS |
154 | } |
155 | EXPORT_SYMBOL_GPL(comedi_pci_auto_config); | |
156 | ||
157 | /** | |
7a064fd1 IA |
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. | |
33782dd5 | 166 | * |
7a064fd1 IA |
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. | |
33782dd5 HS |
170 | */ |
171 | void comedi_pci_auto_unconfig(struct pci_dev *pcidev) | |
172 | { | |
173 | comedi_auto_unconfig(&pcidev->dev); | |
174 | } | |
175 | EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig); | |
176 | ||
177 | /** | |
7a064fd1 IA |
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. | |
33782dd5 | 185 | * |
ea1ea695 | 186 | * Return: 0 on success, or a negative error number on failure. |
33782dd5 HS |
187 | */ |
188 | int 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 | } | |
205 | EXPORT_SYMBOL_GPL(comedi_pci_driver_register); | |
206 | ||
207 | /** | |
7a064fd1 IA |
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. | |
33782dd5 | 211 | * |
7a064fd1 IA |
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. | |
33782dd5 HS |
215 | */ |
216 | void 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 | } | |
222 | EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister); | |
bc3fe156 IA |
223 | |
224 | static int __init comedi_pci_init(void) | |
225 | { | |
226 | return 0; | |
227 | } | |
228 | module_init(comedi_pci_init); | |
229 | ||
230 | static void __exit comedi_pci_exit(void) | |
231 | { | |
232 | } | |
233 | module_exit(comedi_pci_exit); | |
234 | ||
235 | MODULE_AUTHOR("http://www.comedi.org"); | |
236 | MODULE_DESCRIPTION("Comedi PCI interface module"); | |
237 | MODULE_LICENSE("GPL"); |