Merge branch 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / comedi_pci.c
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  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/pci.h>
24
25 #include "comedidev.h"
26
27 /**
28  * comedi_to_pci_dev() - comedi_device pointer to pci_dev pointer.
29  * @dev: comedi_device struct
30  */
31 struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
32 {
33         return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL;
34 }
35 EXPORT_SYMBOL_GPL(comedi_to_pci_dev);
36
37 /**
38  * comedi_pci_enable() - Enable the PCI device and request the regions.
39  * @pcidev: pci_dev struct
40  * @res_name: name for the requested reqource
41  */
42 int comedi_pci_enable(struct pci_dev *pcidev, const char *res_name)
43 {
44         int rc;
45
46         rc = pci_enable_device(pcidev);
47         if (rc < 0)
48                 return rc;
49
50         rc = pci_request_regions(pcidev, res_name);
51         if (rc < 0)
52                 pci_disable_device(pcidev);
53
54         return rc;
55 }
56 EXPORT_SYMBOL_GPL(comedi_pci_enable);
57
58 /**
59  * comedi_pci_disable() - Release the regions and disable the PCI device.
60  * @pcidev: pci_dev struct
61  *
62  * This must be matched with a previous successful call to comedi_pci_enable().
63  */
64 void comedi_pci_disable(struct pci_dev *pcidev)
65 {
66         pci_release_regions(pcidev);
67         pci_disable_device(pcidev);
68 }
69 EXPORT_SYMBOL_GPL(comedi_pci_disable);
70
71 /**
72  * comedi_pci_auto_config() - Configure/probe a comedi PCI driver.
73  * @pcidev: pci_dev struct
74  * @driver: comedi_driver struct
75  *
76  * Typically called from the pci_driver (*probe) function.
77  */
78 int comedi_pci_auto_config(struct pci_dev *pcidev,
79                            struct comedi_driver *driver)
80 {
81         return comedi_auto_config(&pcidev->dev, driver, 0);
82 }
83 EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
84
85 /**
86  * comedi_pci_auto_unconfig() - Unconfigure/remove a comedi PCI driver.
87  * @pcidev: pci_dev struct
88  *
89  * Typically called from the pci_driver (*remove) function.
90  */
91 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
92 {
93         comedi_auto_unconfig(&pcidev->dev);
94 }
95 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
96
97 /**
98  * comedi_pci_driver_register() - Register a comedi PCI driver.
99  * @comedi_driver: comedi_driver struct
100  * @pci_driver: pci_driver struct
101  *
102  * This function is used for the module_init() of comedi PCI drivers.
103  * Do not call it directly, use the module_comedi_pci_driver() helper
104  * macro instead.
105  */
106 int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
107                                struct pci_driver *pci_driver)
108 {
109         int ret;
110
111         ret = comedi_driver_register(comedi_driver);
112         if (ret < 0)
113                 return ret;
114
115         ret = pci_register_driver(pci_driver);
116         if (ret < 0) {
117                 comedi_driver_unregister(comedi_driver);
118                 return ret;
119         }
120
121         return 0;
122 }
123 EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
124
125 /**
126  * comedi_pci_driver_unregister() - Unregister a comedi PCI driver.
127  * @comedi_driver: comedi_driver struct
128  * @pci_driver: pci_driver struct
129  *
130  * This function is used for the module_exit() of comedi PCI drivers.
131  * Do not call it directly, use the module_comedi_pci_driver() helper
132  * macro instead.
133  */
134 void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
135                                   struct pci_driver *pci_driver)
136 {
137         pci_unregister_driver(pci_driver);
138         comedi_driver_unregister(comedi_driver);
139 }
140 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);