Merge branch 'drm-nouveau-fixes-3.10' of git://anongit.freedesktop.org/git/nouveau...
[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  * @dev: comedi_device struct
40  */
41 int comedi_pci_enable(struct comedi_device *dev)
42 {
43         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
44         int rc;
45
46         if (!pcidev)
47                 return -ENODEV;
48
49         rc = pci_enable_device(pcidev);
50         if (rc < 0)
51                 return rc;
52
53         rc = pci_request_regions(pcidev, dev->board_name);
54         if (rc < 0)
55                 pci_disable_device(pcidev);
56         else
57                 dev->ioenabled = true;
58
59         return rc;
60 }
61 EXPORT_SYMBOL_GPL(comedi_pci_enable);
62
63 /**
64  * comedi_pci_disable() - Release the regions and disable the PCI device.
65  * @dev: comedi_device struct
66  */
67 void comedi_pci_disable(struct comedi_device *dev)
68 {
69         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
70
71         if (pcidev && dev->ioenabled) {
72                 pci_release_regions(pcidev);
73                 pci_disable_device(pcidev);
74         }
75         dev->ioenabled = false;
76 }
77 EXPORT_SYMBOL_GPL(comedi_pci_disable);
78
79 /**
80  * comedi_pci_auto_config() - Configure/probe a comedi PCI driver.
81  * @pcidev: pci_dev struct
82  * @driver: comedi_driver struct
83  * @context: driver specific data, passed to comedi_auto_config()
84  *
85  * Typically called from the pci_driver (*probe) function.
86  */
87 int comedi_pci_auto_config(struct pci_dev *pcidev,
88                            struct comedi_driver *driver,
89                            unsigned long context)
90 {
91         return comedi_auto_config(&pcidev->dev, driver, context);
92 }
93 EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
94
95 /**
96  * comedi_pci_auto_unconfig() - Unconfigure/remove a comedi PCI driver.
97  * @pcidev: pci_dev struct
98  *
99  * Typically called from the pci_driver (*remove) function.
100  */
101 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
102 {
103         comedi_auto_unconfig(&pcidev->dev);
104 }
105 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
106
107 /**
108  * comedi_pci_driver_register() - Register a comedi PCI driver.
109  * @comedi_driver: comedi_driver struct
110  * @pci_driver: pci_driver struct
111  *
112  * This function is used for the module_init() of comedi PCI drivers.
113  * Do not call it directly, use the module_comedi_pci_driver() helper
114  * macro instead.
115  */
116 int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
117                                struct pci_driver *pci_driver)
118 {
119         int ret;
120
121         ret = comedi_driver_register(comedi_driver);
122         if (ret < 0)
123                 return ret;
124
125         ret = pci_register_driver(pci_driver);
126         if (ret < 0) {
127                 comedi_driver_unregister(comedi_driver);
128                 return ret;
129         }
130
131         return 0;
132 }
133 EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
134
135 /**
136  * comedi_pci_driver_unregister() - Unregister a comedi PCI driver.
137  * @comedi_driver: comedi_driver struct
138  * @pci_driver: pci_driver struct
139  *
140  * This function is used for the module_exit() of comedi PCI drivers.
141  * Do not call it directly, use the module_comedi_pci_driver() helper
142  * macro instead.
143  */
144 void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
145                                   struct pci_driver *pci_driver)
146 {
147         pci_unregister_driver(pci_driver);
148         comedi_driver_unregister(comedi_driver);
149 }
150 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);