140787839153453a43b99bd374ab9d934d2be123
[pandora-kernel.git] / drivers / staging / comedi / drivers / comedi_pci.h
1 /*
2     comedi/drivers/comedi_pci.h
3     Various PCI functions for drivers.
4
5     Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
6
7     COMEDI - Linux Control and Measurement Device Interface
8     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
26 #ifndef _COMEDI_PCI_H_
27 #define _COMEDI_PCI_H_
28
29 #include <linux/version.h>
30 #include <linux/pci.h>
31
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
33 #define PCI_ENABLE_IS_REFCOUNTED
34 #endif
35
36 /*
37  * Enables PCI device without requesting regions.  Just a simple wrapper
38  * for pci_enable_device().
39  */
40 static inline int comedi_pci_enable_no_regions(struct pci_dev *pdev)
41 {
42         return pci_enable_device(pdev);
43 }
44
45 /*
46  * Called to disable PCI device if PCI device has been enabled, but
47  * PCI regions have not been reserved.
48  *
49  * It only disables the PCI device if the kernel supports reference
50  * counting of PCI enables, otherwise it might stop the device working
51  * in another driver instance.
52  */
53 static inline void comedi_pci_disable_no_regions(struct pci_dev *pdev)
54 {
55 #ifdef PCI_ENABLE_IS_REFCOUNTED
56         pci_disable_device(pdev);
57 #endif
58 }
59
60 /*
61  * Enable the PCI device and request the regions.
62  */
63 static inline int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
64 {
65         int rc;
66
67         rc = pci_enable_device(pdev);
68         if (rc < 0) {
69                 return rc;
70         }
71         rc = pci_request_regions(pdev, res_name);
72         if (rc < 0) {
73                 comedi_pci_disable_no_regions(pdev);
74         }
75         return rc;
76 }
77
78 /*
79  * Release the regions and disable the PCI device.
80  *
81  * This must be matched with a previous successful call to comedi_pci_enable().
82  */
83 static inline void comedi_pci_disable(struct pci_dev *pdev)
84 {
85         pci_release_regions(pdev);
86         pci_disable_device(pdev);
87 }
88
89 #endif