2 comedi/drivers/icp_multi.h
6 Author: Anne Smorthit <anne.smorthit@sfwte.ch>
13 #include "../comedidev.h"
14 #include "comedi_pci.h"
16 /****************************************************************************/
18 struct pcilst_struct {
19 struct pcilst_struct *next;
21 struct pci_dev *pcidev;
22 unsigned short vendor;
23 unsigned short device;
24 unsigned char pci_bus;
25 unsigned char pci_slot;
26 unsigned char pci_func;
27 resource_size_t io_addr[5];
31 struct pcilst_struct *inova_devices;
32 /* ptr to root list of all Inova devices */
34 /****************************************************************************/
36 static void pci_card_list_init(unsigned short pci_vendor, char display);
37 static void pci_card_list_cleanup(unsigned short pci_vendor);
38 static struct pcilst_struct *find_free_pci_card_by_device(unsigned short
42 static int find_free_pci_card_by_position(unsigned short vendor_id,
43 unsigned short device_id,
44 unsigned short pci_bus,
45 unsigned short pci_slot,
46 struct pcilst_struct **card);
47 static struct pcilst_struct *select_and_alloc_pci_card(unsigned short vendor_id,
48 unsigned short device_id,
49 unsigned short pci_bus,
50 unsigned short pci_slot);
52 static int pci_card_alloc(struct pcilst_struct *amcc);
53 static int pci_card_free(struct pcilst_struct *amcc);
54 static void pci_card_list_display(void);
55 static int pci_card_data(struct pcilst_struct *amcc,
56 unsigned char *pci_bus, unsigned char *pci_slot,
57 unsigned char *pci_func, resource_size_t * io_addr,
60 /****************************************************************************/
62 /* build list of Inova cards in this system */
63 static void pci_card_list_init(unsigned short pci_vendor, char display)
65 struct pci_dev *pcidev = NULL;
66 struct pcilst_struct *inova, *last;
72 for_each_pci_dev(pcidev) {
73 if (pcidev->vendor == pci_vendor) {
74 inova = kzalloc(sizeof(*inova), GFP_KERNEL);
77 ("icp_multi: pci_card_list_init: allocation failed\n");
82 inova->pcidev = pci_dev_get(pcidev);
86 inova_devices = inova;
90 inova->vendor = pcidev->vendor;
91 inova->device = pcidev->device;
92 inova->pci_bus = pcidev->bus->number;
93 inova->pci_slot = PCI_SLOT(pcidev->devfn);
94 inova->pci_func = PCI_FUNC(pcidev->devfn);
95 /* Note: resources may be invalid if PCI device
96 * not enabled, but they are corrected in
98 for (i = 0; i < 5; i++)
100 pci_resource_start(pcidev, i);
101 inova->irq = pcidev->irq;
106 pci_card_list_display();
109 /****************************************************************************/
110 /* free up list of amcc cards in this system */
111 static void pci_card_list_cleanup(unsigned short pci_vendor)
113 struct pcilst_struct *inova, *next;
115 for (inova = inova_devices; inova; inova = next) {
117 pci_dev_put(inova->pcidev);
121 inova_devices = NULL;
124 /****************************************************************************/
125 /* find first unused card with this device_id */
126 static struct pcilst_struct *find_free_pci_card_by_device(unsigned short
131 struct pcilst_struct *inova, *next;
133 for (inova = inova_devices; inova; inova = next) {
135 if ((!inova->used) && (inova->device == device_id)
136 && (inova->vendor == vendor_id))
144 /****************************************************************************/
145 /* find card on requested position */
146 static int find_free_pci_card_by_position(unsigned short vendor_id,
147 unsigned short device_id,
148 unsigned short pci_bus,
149 unsigned short pci_slot,
150 struct pcilst_struct **card)
152 struct pcilst_struct *inova, *next;
155 for (inova = inova_devices; inova; inova = next) {
157 if ((inova->vendor == vendor_id) && (inova->device == device_id)
158 && (inova->pci_bus == pci_bus)
159 && (inova->pci_slot == pci_slot)) {
160 if (!(inova->used)) {
162 return 0; /* ok, card is found */
164 return 2; /* card exist but is used */
169 return 1; /* no card found */
172 /****************************************************************************/
173 /* mark card as used */
174 static int pci_card_alloc(struct pcilst_struct *inova)
179 printk(" - BUG!! inova is NULL!\n");
185 if (comedi_pci_enable(inova->pcidev, "icp_multi")) {
186 printk(" - Can't enable PCI device and request regions!\n");
189 /* Resources will be accurate now. */
190 for (i = 0; i < 5; i++)
191 inova->io_addr[i] = pci_resource_start(inova->pcidev, i);
192 inova->irq = inova->pcidev->irq;
197 /****************************************************************************/
198 /* mark card as free */
199 static int pci_card_free(struct pcilst_struct *inova)
207 comedi_pci_disable(inova->pcidev);
211 /****************************************************************************/
212 /* display list of found cards */
213 static void pci_card_list_display(void)
215 struct pcilst_struct *inova, *next;
217 printk("Anne's List of pci cards\n");
218 printk("bus:slot:func vendor device io_inova io_daq irq used\n");
220 for (inova = inova_devices; inova; inova = next) {
223 ("%2d %2d %2d 0x%4x 0x%4x 0x%8llx 0x%8llx %2u %2d\n",
224 inova->pci_bus, inova->pci_slot, inova->pci_func,
225 inova->vendor, inova->device,
226 (unsigned long long)inova->io_addr[0],
227 (unsigned long long)inova->io_addr[2], inova->irq,
233 /****************************************************************************/
234 /* return all card information for driver */
235 static int pci_card_data(struct pcilst_struct *inova,
236 unsigned char *pci_bus, unsigned char *pci_slot,
237 unsigned char *pci_func, resource_size_t * io_addr,
244 *pci_bus = inova->pci_bus;
245 *pci_slot = inova->pci_slot;
246 *pci_func = inova->pci_func;
247 for (i = 0; i < 5; i++)
248 io_addr[i] = inova->io_addr[i];
253 /****************************************************************************/
254 /* select and alloc card */
255 static struct pcilst_struct *select_and_alloc_pci_card(unsigned short vendor_id,
256 unsigned short device_id,
257 unsigned short pci_bus,
258 unsigned short pci_slot)
260 struct pcilst_struct *card;
263 if ((pci_bus < 1) & (pci_slot < 1)) { /* use autodetection */
265 card = find_free_pci_card_by_device(vendor_id, device_id);
267 printk(" - Unused card not found in system!\n");
271 switch (find_free_pci_card_by_position(vendor_id, device_id,
276 (" - Card not found on requested position b:s %d:%d!\n",
281 (" - Card on requested position is used b:s %d:%d!\n",
287 err = pci_card_alloc(card);
290 printk(" - Can't allocate card!\n");
291 /* else: error already printed. */