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
19#include <linux/pci.h>
20#include <linux/interrupt.h>
21
22#include "comedidev.h"
23
24/**
25 * comedi_to_pci_dev() - comedi_device pointer to pci_dev pointer.
26 * @dev: comedi_device struct
27 */
28struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
29{
30	return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL;
31}
32EXPORT_SYMBOL_GPL(comedi_to_pci_dev);
33
34/**
35 * comedi_pci_enable() - Enable the PCI device and request the regions.
36 * @dev: comedi_device struct
37 */
38int comedi_pci_enable(struct comedi_device *dev)
39{
40	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
41	int rc;
42
43	if (!pcidev)
44		return -ENODEV;
45
46	rc = pci_enable_device(pcidev);
47	if (rc < 0)
48		return rc;
49
50	rc = pci_request_regions(pcidev, dev->board_name);
51	if (rc < 0)
52		pci_disable_device(pcidev);
53	else
54		dev->ioenabled = true;
55
56	return rc;
57}
58EXPORT_SYMBOL_GPL(comedi_pci_enable);
59
60/**
61 * comedi_pci_disable() - Release the regions and disable the PCI device.
62 * @dev: comedi_device struct
63 */
64void comedi_pci_disable(struct comedi_device *dev)
65{
66	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
67
68	if (pcidev && dev->ioenabled) {
69		pci_release_regions(pcidev);
70		pci_disable_device(pcidev);
71	}
72	dev->ioenabled = false;
73}
74EXPORT_SYMBOL_GPL(comedi_pci_disable);
75
76/**
77 * comedi_pci_detach() - A generic (*detach) function for PCI drivers.
78 * @dev: comedi_device struct
79 */
80void comedi_pci_detach(struct comedi_device *dev)
81{
82	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
83
84	if (!pcidev || !dev->ioenabled)
85		return;
86
87	if (dev->irq) {
88		free_irq(dev->irq, dev);
89		dev->irq = 0;
90	}
91	if (dev->mmio) {
92		iounmap(dev->mmio);
93		dev->mmio = NULL;
94	}
95	comedi_pci_disable(dev);
96}
97EXPORT_SYMBOL_GPL(comedi_pci_detach);
98
99/**
100 * comedi_pci_auto_config() - Configure/probe a comedi PCI driver.
101 * @pcidev: pci_dev struct
102 * @driver: comedi_driver struct
103 * @context: driver specific data, passed to comedi_auto_config()
104 *
105 * Typically called from the pci_driver (*probe) function.
106 */
107int comedi_pci_auto_config(struct pci_dev *pcidev,
108			   struct comedi_driver *driver,
109			   unsigned long context)
110{
111	return comedi_auto_config(&pcidev->dev, driver, context);
112}
113EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
114
115/**
116 * comedi_pci_auto_unconfig() - Unconfigure/remove a comedi PCI driver.
117 * @pcidev: pci_dev struct
118 *
119 * Typically called from the pci_driver (*remove) function.
120 */
121void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
122{
123	comedi_auto_unconfig(&pcidev->dev);
124}
125EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
126
127/**
128 * comedi_pci_driver_register() - Register a comedi PCI driver.
129 * @comedi_driver: comedi_driver struct
130 * @pci_driver: pci_driver struct
131 *
132 * This function is used for the module_init() of comedi PCI drivers.
133 * Do not call it directly, use the module_comedi_pci_driver() helper
134 * macro instead.
135 */
136int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
137			       struct pci_driver *pci_driver)
138{
139	int ret;
140
141	ret = comedi_driver_register(comedi_driver);
142	if (ret < 0)
143		return ret;
144
145	ret = pci_register_driver(pci_driver);
146	if (ret < 0) {
147		comedi_driver_unregister(comedi_driver);
148		return ret;
149	}
150
151	return 0;
152}
153EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
154
155/**
156 * comedi_pci_driver_unregister() - Unregister a comedi PCI driver.
157 * @comedi_driver: comedi_driver struct
158 * @pci_driver: pci_driver struct
159 *
160 * This function is used for the module_exit() of comedi PCI drivers.
161 * Do not call it directly, use the module_comedi_pci_driver() helper
162 * macro instead.
163 */
164void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
165				  struct pci_driver *pci_driver)
166{
167	pci_unregister_driver(pci_driver);
168	comedi_driver_unregister(comedi_driver);
169}
170EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
171