| 1 | /* $Id: pci.c,v 1.1.1.1 2003/07/02 13:57:02 eleph Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * OS/2 implementation of Linux PCI functions (using direct port I/O)
|
|---|
| 4 | *
|
|---|
| 5 | * (C) 2000-2002 InnoTek Systemberatung GmbH
|
|---|
| 6 | * (C) 2000-2001 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright (c) 2016-2017 David Azarewicz <david@88watts.net>
|
|---|
| 8 | *
|
|---|
| 9 | * Parts based on Linux kernel sources
|
|---|
| 10 | *
|
|---|
| 11 | * This program is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU General Public License as
|
|---|
| 13 | * published by the Free Software Foundation; either version 2 of
|
|---|
| 14 | * the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * This program is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public
|
|---|
| 22 | * License along with this program; if not, write to the Free
|
|---|
| 23 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
|
|---|
| 24 | * USA.
|
|---|
| 25 | *
|
|---|
| 26 | */
|
|---|
| 27 | #define CONFIG_PM
|
|---|
| 28 | #include "linux.h"
|
|---|
| 29 | #include <linux/init.h>
|
|---|
| 30 | #include <linux/poll.h>
|
|---|
| 31 | #include <linux/dma-mapping.h>
|
|---|
| 32 | #include <asm/uaccess.h>
|
|---|
| 33 | #include <asm/hardirq.h>
|
|---|
| 34 | #include <asm/io.h>
|
|---|
| 35 | #include <sound/config.h>
|
|---|
| 36 | #include <sound/core.h>
|
|---|
| 37 | #include <sound/asound.h>
|
|---|
| 38 |
|
|---|
| 39 | #define LINUX
|
|---|
| 40 | #include <ossidc.h>
|
|---|
| 41 | #include <stacktoflat.h>
|
|---|
| 42 | #include <dbgos2.h>
|
|---|
| 43 | #include <osspci.h>
|
|---|
| 44 |
|
|---|
| 45 | #define MAX_PCI_BUSSES 256
|
|---|
| 46 | #define MAX_PCI_DEVICES 16
|
|---|
| 47 |
|
|---|
| 48 | struct pci_dev pci_devices[MAX_PCI_DEVICES] = {0};
|
|---|
| 49 | //struct pci_bus pci_busses[MAX_PCI_BUSSES] = {0};
|
|---|
| 50 |
|
|---|
| 51 | extern int nrCardsDetected;
|
|---|
| 52 | extern int iAdapterNumber;
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | #define PCI_CONFIG_ENABLE 0x80000000
|
|---|
| 56 | #define PCI_CONFIG_ADDRESS 0xCF8
|
|---|
| 57 | #define PCI_CONFIG_DATA 0xCFC
|
|---|
| 58 |
|
|---|
| 59 | //******************************************************************************
|
|---|
| 60 | #define CONFIG_CMD(dev, where) \
|
|---|
| 61 | (PCI_CONFIG_ENABLE | (dev->bus->number<<16) | (dev->devfn<<8) | (where & ~3))
|
|---|
| 62 | //******************************************************************************
|
|---|
| 63 | int pci_read_config_byte(struct pci_dev *dev, int where, u8 *value)
|
|---|
| 64 | {
|
|---|
| 65 | outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
|
|---|
| 66 | *value = inb(PCI_CONFIG_DATA + (where&3));
|
|---|
| 67 | return PCIBIOS_SUCCESSFUL;
|
|---|
| 68 | }
|
|---|
| 69 | //******************************************************************************
|
|---|
| 70 | //******************************************************************************
|
|---|
| 71 | int pci_read_config_word(struct pci_dev *dev, int where, u16 *value)
|
|---|
| 72 | {
|
|---|
| 73 | outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
|
|---|
| 74 | *value = inw(PCI_CONFIG_DATA + (where&2));
|
|---|
| 75 | return PCIBIOS_SUCCESSFUL;
|
|---|
| 76 | }
|
|---|
| 77 | //******************************************************************************
|
|---|
| 78 | //******************************************************************************
|
|---|
| 79 | int pci_read_config_dword(struct pci_dev *dev, int where, u32 *value)
|
|---|
| 80 | {
|
|---|
| 81 | outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
|
|---|
| 82 | *value = inl(PCI_CONFIG_DATA);
|
|---|
| 83 | return PCIBIOS_SUCCESSFUL;
|
|---|
| 84 | }
|
|---|
| 85 | //******************************************************************************
|
|---|
| 86 | //******************************************************************************
|
|---|
| 87 | int pci_write_config_byte(struct pci_dev *dev, int where, u8 value)
|
|---|
| 88 | {
|
|---|
| 89 | outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
|
|---|
| 90 | outb(value, PCI_CONFIG_DATA + (where&3));
|
|---|
| 91 | return PCIBIOS_SUCCESSFUL;
|
|---|
| 92 | }
|
|---|
| 93 | //******************************************************************************
|
|---|
| 94 | //******************************************************************************
|
|---|
| 95 | int pci_write_config_word(struct pci_dev *dev, int where, u16 value)
|
|---|
| 96 | {
|
|---|
| 97 | outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
|
|---|
| 98 | outw(value, PCI_CONFIG_DATA + (where&2));
|
|---|
| 99 | return PCIBIOS_SUCCESSFUL;
|
|---|
| 100 | }
|
|---|
| 101 | //******************************************************************************
|
|---|
| 102 | //******************************************************************************
|
|---|
| 103 | int pci_write_config_dword(struct pci_dev *dev, int where, u32 value)
|
|---|
| 104 | {
|
|---|
| 105 | outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
|
|---|
| 106 | outl(value, PCI_CONFIG_DATA);
|
|---|
| 107 | return PCIBIOS_SUCCESSFUL;
|
|---|
| 108 | }
|
|---|
| 109 | //******************************************************************************
|
|---|
| 110 | //******************************************************************************
|
|---|
| 111 | int pcidev_prepare(struct pci_dev *dev)
|
|---|
| 112 | {
|
|---|
| 113 | dprintf(("pcidev_prepare %x not implemented", dev));
|
|---|
| 114 | return 1; //todo: correct return value??
|
|---|
| 115 | }
|
|---|
| 116 | //******************************************************************************
|
|---|
| 117 | //******************************************************************************
|
|---|
| 118 | int pcidev_activate(struct pci_dev *dev)
|
|---|
| 119 | {
|
|---|
| 120 | dprintf(("pcidev_activate %x not implemented", dev));
|
|---|
| 121 | return 1; //todo: correct return value??
|
|---|
| 122 | }
|
|---|
| 123 | //******************************************************************************
|
|---|
| 124 | //******************************************************************************
|
|---|
| 125 | int pcidev_deactivate(struct pci_dev *dev)
|
|---|
| 126 | {
|
|---|
| 127 | dprintf(("pcidev_deactivate %x not implemented", dev));
|
|---|
| 128 | return 1; //todo: correct return value??
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Called by: pci_find_device, register_driver
|
|---|
| 133 | *
|
|---|
| 134 | * Find the next matching PCI device starting with the device specified by ulLast
|
|---|
| 135 | * Returns: the found device, pcidev filled in. Returns zero if no device found.
|
|---|
| 136 | */
|
|---|
| 137 | static ULONG pci_query_device(const struct pci_device_id *pIdTable, struct pci_dev near *pcidev, ULONG ulLast)
|
|---|
| 138 | {
|
|---|
| 139 | int resNo, addr;
|
|---|
| 140 | u32 devNr, busNr, funcNr, detectedId, cfgaddrreg, ulPciAdr, ulTmp1, ulTmp2;
|
|---|
| 141 | u8 headerType;
|
|---|
| 142 |
|
|---|
| 143 | busNr = (ulLast >> 8) & 0xff;
|
|---|
| 144 | devNr = PCI_SLOT(ulLast);
|
|---|
| 145 | funcNr = PCI_FUNC(ulLast);
|
|---|
| 146 | if (ulLast) funcNr++;
|
|---|
| 147 |
|
|---|
| 148 | cfgaddrreg = inl(PCI_CONFIG_ADDRESS);
|
|---|
| 149 | for ( ; busNr<MAX_PCI_BUSSES; busNr++) //BusNumber<255
|
|---|
| 150 | {
|
|---|
| 151 | for( ; devNr<32; devNr++)
|
|---|
| 152 | {
|
|---|
| 153 | for( ; funcNr<8; funcNr++)
|
|---|
| 154 | {
|
|---|
| 155 | headerType = 0;
|
|---|
| 156 | ulPciAdr = PCI_CONFIG_ENABLE | (busNr<<16) | (devNr<<11) | (funcNr<<8);
|
|---|
| 157 | outl(ulPciAdr, PCI_CONFIG_ADDRESS);
|
|---|
| 158 | detectedId = inl(PCI_CONFIG_DATA);
|
|---|
| 159 |
|
|---|
| 160 | if ( detectedId == 0xffffffff )
|
|---|
| 161 | {
|
|---|
| 162 | if ( funcNr == 0 ) break; /* if func 0 isn't there, the others aren't either */
|
|---|
| 163 | continue;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | outl(ulPciAdr + PCI_CLASS_REVISION, PCI_CONFIG_ADDRESS);
|
|---|
| 167 | ulTmp2 = inl(PCI_CONFIG_DATA) >> 8; /* get class */
|
|---|
| 168 |
|
|---|
| 169 | //dprintf(("Found: %x Class=%x need: %x%x class=%x", detectedId, ulTmp2, pIdTable->device&0xffff, pIdTable->vendor, pIdTable->class));
|
|---|
| 170 |
|
|---|
| 171 | if ( pIdTable->class )
|
|---|
| 172 | {
|
|---|
| 173 | if ( (ulTmp2 & pIdTable->class_mask) != pIdTable->class ) continue;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if ( pIdTable->vendor && (pIdTable->vendor != (detectedId & 0xffff)) ) continue;
|
|---|
| 177 | if ( pIdTable->device && (pIdTable->device != PCI_ANY_ID) && (pIdTable->device != (detectedId >> 16)) ) continue;
|
|---|
| 178 |
|
|---|
| 179 | outl(ulPciAdr | (PCI_HEADER_TYPE & ~3), PCI_CONFIG_ADDRESS);
|
|---|
| 180 | headerType = inb(PCI_CONFIG_DATA + (PCI_HEADER_TYPE & 3));
|
|---|
| 181 |
|
|---|
| 182 | if ( (headerType & 0x7f) != PCI_HEADER_TYPE_NORMAL ) continue;
|
|---|
| 183 |
|
|---|
| 184 | memset((void near *)pcidev, 0, sizeof(struct pci_dev));
|
|---|
| 185 |
|
|---|
| 186 | pcidev->class = ulTmp2;
|
|---|
| 187 | pcidev->vendor = detectedId & 0xffff;
|
|---|
| 188 | pcidev->device = detectedId >> 16;
|
|---|
| 189 | //pcidev->bus = &pci_busses[busNr];
|
|---|
| 190 | pcidev->bus = kmalloc(sizeof(struct pci_bus), GFP_KERNEL);
|
|---|
| 191 | if (pcidev->bus == NULL) return 0;
|
|---|
| 192 | memset (pcidev->bus, 0, sizeof(struct pci_bus));
|
|---|
| 193 | pcidev->bus->number = busNr;
|
|---|
| 194 | pcidev->devfn = PCI_DEVFN(devNr, funcNr);
|
|---|
| 195 | pcidev->hdr_type = headerType & 0x7f;
|
|---|
| 196 |
|
|---|
| 197 | pcidev->prepare = pcidev_prepare;
|
|---|
| 198 | pcidev->activate = pcidev_activate;
|
|---|
| 199 | pcidev->deactivate = pcidev_deactivate;
|
|---|
| 200 | pcidev->active = 1;
|
|---|
| 201 | pcidev->ro = 0;
|
|---|
| 202 | pcidev->sibling = NULL;
|
|---|
| 203 | pcidev->next = NULL;
|
|---|
| 204 | pcidev->dma_mask = 0xffffffff;
|
|---|
| 205 | pcidev->dev.dma_mask = &pcidev->dma_mask;
|
|---|
| 206 | pcidev->dev.coherent_dma_mask = 0xffffffffull;
|
|---|
| 207 |
|
|---|
| 208 | // Subsystem ID
|
|---|
| 209 | pci_read_config_word(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &pcidev->subsystem_vendor);
|
|---|
| 210 | pci_read_config_word(pcidev, PCI_SUBSYSTEM_ID, &pcidev->subsystem_device);
|
|---|
| 211 |
|
|---|
| 212 | // revision
|
|---|
| 213 | pci_read_config_byte(pcidev, PCI_REVISION_ID, &pcidev->revision);
|
|---|
| 214 |
|
|---|
| 215 | // I/O and MEM
|
|---|
| 216 | resNo = 0;
|
|---|
| 217 | for( addr = PCI_BASE_ADDRESS_0; addr <= PCI_BASE_ADDRESS_5; addr += 4 )
|
|---|
| 218 | {
|
|---|
| 219 | pci_read_config_dword(pcidev, addr, &ulTmp1);
|
|---|
| 220 | if( ulTmp1 != 0 && ulTmp1 != 0xffffffff )
|
|---|
| 221 | {
|
|---|
| 222 | pci_write_config_dword(pcidev, addr, 0xffffffff);
|
|---|
| 223 | pci_read_config_dword(pcidev, addr, &ulTmp2);
|
|---|
| 224 | pci_write_config_dword(pcidev, addr, ulTmp1);
|
|---|
| 225 |
|
|---|
| 226 | if( ulTmp1 & PCI_BASE_ADDRESS_SPACE_IO )
|
|---|
| 227 | {
|
|---|
| 228 | pcidev->resource[resNo].flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
|
|---|
| 229 | pcidev->resource[resNo].start = ulTmp1 & PCI_BASE_ADDRESS_IO_MASK;
|
|---|
| 230 | pcidev->resource[resNo].end = pcidev->resource[resNo].start +
|
|---|
| 231 | ~(ulTmp2 & PCI_BASE_ADDRESS_IO_MASK) + 1;
|
|---|
| 232 | }
|
|---|
| 233 | else
|
|---|
| 234 | {
|
|---|
| 235 | pcidev->resource[resNo].flags = IORESOURCE_MEM | IORESOURCE_MEM_WRITEABLE;
|
|---|
| 236 | pcidev->resource[resNo].start = ulTmp1 & PCI_BASE_ADDRESS_MEM_MASK;
|
|---|
| 237 | pcidev->resource[resNo].end = pcidev->resource[resNo].start +
|
|---|
| 238 | ~(ulTmp2 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | resNo++;
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | // IRQ and PIN
|
|---|
| 246 | pci_read_config_dword(pcidev, PCI_INTERRUPT_LINE, &ulTmp1);
|
|---|
| 247 | //rprintf(("pci_query_device: PCI config IRQ=%d", ulTmp1&0xff));
|
|---|
| 248 | if( (u8)ulTmp1 && (u8)ulTmp1 != 0xff )
|
|---|
| 249 | {
|
|---|
| 250 | pcidev->irq_resource[0].flags = IORESOURCE_IRQ;
|
|---|
| 251 | pcidev->irq_resource[0].start = pcidev->irq_resource[0].end = ulTmp1 & 0xffff;
|
|---|
| 252 | pcidev->irq = (u8)ulTmp1; // This is the interrupt used for init time processing
|
|---|
| 253 | pcidev->irq_pin = ulTmp1>>8;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | return ((busNr << 8) | PCI_DEVFN(devNr, funcNr));
|
|---|
| 257 | } /* for funcNr */
|
|---|
| 258 | funcNr = 0;
|
|---|
| 259 | } /* for devNr */
|
|---|
| 260 | devNr = 0;
|
|---|
| 261 | }
|
|---|
| 262 | outl(cfgaddrreg, PCI_CONFIG_ADDRESS);
|
|---|
| 263 | return 0;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * Called by: snd_pci_dev_present, various sound drivers
|
|---|
| 268 | *
|
|---|
| 269 | * Find the requested device
|
|---|
| 270 | */
|
|---|
| 271 | struct pci_dev *pci_find_device (unsigned int vendor, unsigned int device, struct pci_dev *from)
|
|---|
| 272 | {
|
|---|
| 273 | int i;
|
|---|
| 274 | struct pci_device_id id_table;
|
|---|
| 275 |
|
|---|
| 276 | for(i=0; i<MAX_PCI_DEVICES; i++)
|
|---|
| 277 | {
|
|---|
| 278 | if ( pci_devices[i].devfn && (pci_devices[i].vendor == vendor) && (pci_devices[i].device == device) ) return &pci_devices[i];
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | for(i=0; i<MAX_PCI_DEVICES; i++)
|
|---|
| 282 | {
|
|---|
| 283 | if(pci_devices[i].devfn == 0)
|
|---|
| 284 | {
|
|---|
| 285 | memset(&id_table, 0, sizeof(id_table));
|
|---|
| 286 | id_table.vendor = vendor;
|
|---|
| 287 | id_table.device = device;
|
|---|
| 288 | if( pci_query_device(&id_table, (struct pci_dev near *)&pci_devices[i], 0) ) return &pci_devices[i];
|
|---|
| 289 | else break;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | return NULL;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /** __request_region
|
|---|
| 297 | */
|
|---|
| 298 | struct resource * __request_region(struct resource *a, unsigned long start, unsigned long n, const char *name)
|
|---|
| 299 | {
|
|---|
| 300 | struct resource *resource;
|
|---|
| 301 |
|
|---|
| 302 | if(a->flags & IORESOURCE_MEM) {
|
|---|
| 303 | if(RMRequestMem(/*hResMgr,*/ start, n) == FALSE) {
|
|---|
| 304 | printk("RMRequestIO failed for mem %x length %x\n", start, n);
|
|---|
| 305 | return NULL;
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 | else if(a->flags & IORESOURCE_IO) {
|
|---|
| 309 | if(RMRequestIO(/*hResMgr,*/ start, n) == FALSE) {
|
|---|
| 310 | printk("RMRequestIO failed for io %x length %x\n", start, n);
|
|---|
| 311 | return NULL;
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
|
|---|
| 316 | if (resource == NULL)
|
|---|
| 317 | return NULL;
|
|---|
| 318 | resource->name = name;
|
|---|
| 319 | resource->start = start;
|
|---|
| 320 | resource->end = start + n; // - 1;
|
|---|
| 321 | resource->flags = a->flags;
|
|---|
| 322 | resource->parent =
|
|---|
| 323 | resource->child = NULL;
|
|---|
| 324 |
|
|---|
| 325 | // insert in list
|
|---|
| 326 | resource->sibling = a->sibling;
|
|---|
| 327 | a->sibling = resource;
|
|---|
| 328 |
|
|---|
| 329 | return resource;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | /**
|
|---|
| 333 | */
|
|---|
| 334 | void __release_region(struct resource *a, unsigned long start, unsigned long n)
|
|---|
| 335 | {
|
|---|
| 336 | struct resource *resource;
|
|---|
| 337 | struct resource **ppres = &a->sibling;
|
|---|
| 338 | unsigned long end = start + n; // - 1;
|
|---|
| 339 |
|
|---|
| 340 | while( *ppres )
|
|---|
| 341 | {
|
|---|
| 342 | resource = *ppres;
|
|---|
| 343 |
|
|---|
| 344 | if( resource->start == start && resource->end == end )
|
|---|
| 345 | {
|
|---|
| 346 | // remove from list
|
|---|
| 347 | *ppres = resource->sibling;
|
|---|
| 348 | kfree(resource);
|
|---|
| 349 | return;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | ppres = &resource->sibling;
|
|---|
| 353 | }
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | /**
|
|---|
| 357 | */
|
|---|
| 358 | int pci_get_flags (struct pci_dev *dev, int n_base)
|
|---|
| 359 | {
|
|---|
| 360 | if(n_base >= DEVICE_COUNT_RESOURCE || !dev->resource[n_base].flags) {
|
|---|
| 361 | DebugInt3();
|
|---|
| 362 | return 0;
|
|---|
| 363 | }
|
|---|
| 364 | return dev->resource[n_base].flags;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /**
|
|---|
| 368 | */
|
|---|
| 369 | int pcibios_present(void)
|
|---|
| 370 | {
|
|---|
| 371 | printk("pcibios_present -> pretend BIOS present\n");
|
|---|
| 372 | return 1;
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /**
|
|---|
| 376 | */
|
|---|
| 377 | struct pci_dev *pci_find_slot (unsigned int bus, unsigned int devfn)
|
|---|
| 378 | {
|
|---|
| 379 | printk("pci_find_slot %d %x not implemented!!\n", bus, devfn);
|
|---|
| 380 | DebugInt3();
|
|---|
| 381 | return NULL;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /**
|
|---|
| 385 | */
|
|---|
| 386 | int pci_dma_supported(struct pci_dev *dev, unsigned long mask)
|
|---|
| 387 | {
|
|---|
| 388 | printk("pci_dma_supported: return TRUE\n");
|
|---|
| 389 | return 1;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | /**
|
|---|
| 393 | */
|
|---|
| 394 | int pci_find_capability(struct pci_dev *dev, int cap)
|
|---|
| 395 | {
|
|---|
| 396 | u16 status;
|
|---|
| 397 | u8 pos, id;
|
|---|
| 398 | int ttl = 48;
|
|---|
| 399 |
|
|---|
| 400 | pci_read_config_word(dev, PCI_STATUS, &status);
|
|---|
| 401 | if (!(status & PCI_STATUS_CAP_LIST))
|
|---|
| 402 | return 0;
|
|---|
| 403 | pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &pos);
|
|---|
| 404 | while (ttl-- && pos >= 0x40) {
|
|---|
| 405 | pos &= ~3;
|
|---|
| 406 | pci_read_config_byte(dev, pos + PCI_CAP_LIST_ID, &id);
|
|---|
| 407 | if (id == 0xff)
|
|---|
| 408 | break;
|
|---|
| 409 | if (id == cap)
|
|---|
| 410 | return pos;
|
|---|
| 411 | pci_read_config_byte(dev, pos + PCI_CAP_LIST_NEXT, &pos);
|
|---|
| 412 | }
|
|---|
| 413 | return 0;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /**
|
|---|
| 417 | * Set power management state of a device. For transitions from state D3
|
|---|
| 418 | * it isn't as straightforward as one could assume since many devices forget
|
|---|
| 419 | * their configuration space during wakeup. Returns old power state.
|
|---|
| 420 | */
|
|---|
| 421 | int pci_set_power_state(struct pci_dev *dev, int new_state)
|
|---|
| 422 | {
|
|---|
| 423 | u32 base[5], romaddr;
|
|---|
| 424 | u16 pci_command, pwr_command;
|
|---|
| 425 | u8 pci_latency, pci_cacheline;
|
|---|
| 426 | int i, old_state;
|
|---|
| 427 | int pm = pci_find_capability(dev, PCI_CAP_ID_PM);
|
|---|
| 428 |
|
|---|
| 429 | if (!pm)
|
|---|
| 430 | return 0;
|
|---|
| 431 | pci_read_config_word(dev, pm + PCI_PM_CTRL, &pwr_command);
|
|---|
| 432 | old_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
|
|---|
| 433 | if (old_state == new_state)
|
|---|
| 434 | return old_state;
|
|---|
| 435 | if (old_state == 3) {
|
|---|
| 436 | pci_read_config_word(dev, PCI_COMMAND, &pci_command);
|
|---|
| 437 | pci_write_config_word(dev, PCI_COMMAND, pci_command & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
|
|---|
| 438 | for (i = 0; i < 5; i++)
|
|---|
| 439 | pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + i*4, &base[i]);
|
|---|
| 440 | pci_read_config_dword(dev, PCI_ROM_ADDRESS, &romaddr);
|
|---|
| 441 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
|
|---|
| 442 | pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_cacheline);
|
|---|
| 443 | pci_write_config_word(dev, pm + PCI_PM_CTRL, new_state);
|
|---|
| 444 | for (i = 0; i < 5; i++)
|
|---|
| 445 | pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + i*4, base[i]);
|
|---|
| 446 | pci_write_config_dword(dev, PCI_ROM_ADDRESS, romaddr);
|
|---|
| 447 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
|
|---|
| 448 | pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cacheline);
|
|---|
| 449 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, pci_latency);
|
|---|
| 450 | pci_write_config_word(dev, PCI_COMMAND, pci_command);
|
|---|
| 451 | } else
|
|---|
| 452 | pci_write_config_word(dev, pm + PCI_PM_CTRL, (pwr_command & ~PCI_PM_CTRL_STATE_MASK) | new_state);
|
|---|
| 453 | return old_state;
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | /**
|
|---|
| 457 | * Initialize device before it's used by a driver. Ask low-level code
|
|---|
| 458 | * to enable I/O and memory. Wake up the device if it was suspended.
|
|---|
| 459 | * Beware, this function can fail.
|
|---|
| 460 | */
|
|---|
| 461 | int pci_enable_device(struct pci_dev *dev)
|
|---|
| 462 | {
|
|---|
| 463 | u16 pci_command;
|
|---|
| 464 |
|
|---|
| 465 | dprintf(("pci_enable_device %x\n", dev));
|
|---|
| 466 |
|
|---|
| 467 | pci_read_config_word(dev, PCI_COMMAND, &pci_command);
|
|---|
| 468 | pci_write_config_word(dev, PCI_COMMAND, pci_command | (PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
|
|---|
| 469 | pci_set_power_state(dev, 0);
|
|---|
| 470 | return 0;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /** pci_register_driver
|
|---|
| 474 | *
|
|---|
| 475 | * probes and registers a sound driver with RM.
|
|---|
| 476 | *
|
|---|
| 477 | * Returns: number of cards found.
|
|---|
| 478 | */
|
|---|
| 479 | int pci_register_driver(struct pci_driver *driver)
|
|---|
| 480 | {
|
|---|
| 481 | int iNumCards, iTmp;
|
|---|
| 482 | ULONG ulLast;
|
|---|
| 483 | struct pci_dev *pcidev;
|
|---|
| 484 | struct pci_device_id IdTable;
|
|---|
| 485 | USHORT usVendor, usDevice;
|
|---|
| 486 | int iAdapter = 0;
|
|---|
| 487 |
|
|---|
| 488 | if (!driver->probe) return 0;
|
|---|
| 489 |
|
|---|
| 490 | iNumCards = 0;
|
|---|
| 491 |
|
|---|
| 492 | /* find an empty slot */
|
|---|
| 493 | for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
|
|---|
| 494 | {
|
|---|
| 495 | if (pci_devices[iTmp].devfn == 0) break;
|
|---|
| 496 | }
|
|---|
| 497 | if (iTmp >= MAX_PCI_DEVICES) return 0;
|
|---|
| 498 | pcidev = &pci_devices[iTmp];
|
|---|
| 499 |
|
|---|
| 500 | memset(&IdTable, 0, sizeof(IdTable));
|
|---|
| 501 | IdTable.class = 0x000400 << 8; /* Any multimedia device */
|
|---|
| 502 | IdTable.class_mask = 0xffff00 << 8;
|
|---|
| 503 | ulLast = 0;
|
|---|
| 504 | while( (ulLast = pci_query_device(&IdTable, pcidev, ulLast)) != 0 )
|
|---|
| 505 | {
|
|---|
| 506 | int iTableIx;
|
|---|
| 507 |
|
|---|
| 508 | rprintf((__func__": query_device found %x %04x:%04x class=%x checking %s",
|
|---|
| 509 | ulLast, pcidev->vendor, pcidev->device, pcidev->class, driver->name));
|
|---|
| 510 |
|
|---|
| 511 | usVendor = 0;
|
|---|
| 512 | usDevice = 0;
|
|---|
| 513 |
|
|---|
| 514 | for( iTableIx = 0; driver->id_table[iTableIx].vendor; iTableIx++)
|
|---|
| 515 | {
|
|---|
| 516 | struct pci_device_id const *pDriverId = &driver->id_table[iTableIx];
|
|---|
| 517 |
|
|---|
| 518 | if ( (pDriverId->class) && ((pcidev->class & pDriverId->class_mask) != pDriverId->class) ) continue;
|
|---|
| 519 | if (pDriverId->vendor != pcidev->vendor) continue;
|
|---|
| 520 | if ( (pDriverId->device != PCI_ANY_ID) && (pDriverId->device != pcidev->device) ) continue;
|
|---|
| 521 |
|
|---|
| 522 | /* skip a duplicate device that could be matched by both and exact match and a class match */
|
|---|
| 523 | if (usVendor == pcidev->vendor && usDevice == pcidev->device) continue;
|
|---|
| 524 | usVendor = pcidev->vendor;
|
|---|
| 525 | usDevice = pcidev->device;
|
|---|
| 526 |
|
|---|
| 527 | rprintf((__func__": matched %d %x:%x/%x with %x:%x/%x %x (%s)", iTableIx,
|
|---|
| 528 | pcidev->vendor, pcidev->device, pcidev->class,
|
|---|
| 529 | pDriverId->vendor, pDriverId->device, pDriverId->class, pDriverId->class_mask, driver->name));
|
|---|
| 530 |
|
|---|
| 531 | if ((iAdapterNumber >= 0) && (iAdapter < iAdapterNumber))
|
|---|
| 532 | {
|
|---|
| 533 | rprintf((__func__": AdapterNumber=%x skipping Adapter=%x", iAdapterNumber, iAdapter));
|
|---|
| 534 | iAdapter++;
|
|---|
| 535 | continue;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | if (driver->probe(pcidev, pDriverId) == 0)
|
|---|
| 539 | {
|
|---|
| 540 | pcidev->pcidriver = (void *)driver;
|
|---|
| 541 | pcidev->current_state = 4;
|
|---|
| 542 |
|
|---|
| 543 | // create adapter
|
|---|
| 544 | RMCreateAdapterU32((pcidev->device << 16) | pcidev->vendor, &pcidev->hAdapter, ulLast, iNumCards);
|
|---|
| 545 |
|
|---|
| 546 | iNumCards++;
|
|---|
| 547 | pcidev = NULL; /* we need a new slot */
|
|---|
| 548 | break;
|
|---|
| 549 | }
|
|---|
| 550 | // release resources which were possibly allocated during probe()
|
|---|
| 551 | RMDeallocRes();
|
|---|
| 552 | } /* for id_table loop */
|
|---|
| 553 |
|
|---|
| 554 | if (pcidev)
|
|---|
| 555 | {
|
|---|
| 556 | kfree(pcidev->bus);
|
|---|
| 557 | pcidev->devfn = 0;
|
|---|
| 558 | }
|
|---|
| 559 | else
|
|---|
| 560 | {
|
|---|
| 561 | if (iAdapterNumber >= 0) break;
|
|---|
| 562 | /* find another empty slot */
|
|---|
| 563 | for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
|
|---|
| 564 | {
|
|---|
| 565 | if (pci_devices[iTmp].devfn == 0) break;
|
|---|
| 566 | }
|
|---|
| 567 | if (iTmp >= MAX_PCI_DEVICES) break;
|
|---|
| 568 | pcidev = &pci_devices[iTmp];
|
|---|
| 569 | }
|
|---|
| 570 | } /* pci_query_device loop */
|
|---|
| 571 |
|
|---|
| 572 | return iNumCards;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | /**
|
|---|
| 576 | */
|
|---|
| 577 | int pci_module_init(struct pci_driver *drv)
|
|---|
| 578 | {
|
|---|
| 579 | int res = pci_register_driver(drv);
|
|---|
| 580 | if (res == 0) return -ENODEV;
|
|---|
| 581 | return res;
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | /**
|
|---|
| 585 | */
|
|---|
| 586 | int pci_unregister_driver(struct pci_driver *driver)
|
|---|
| 587 | {
|
|---|
| 588 | struct pci_dev *pcidev;
|
|---|
| 589 | int i, j;
|
|---|
| 590 |
|
|---|
| 591 | for (i=0; driver->id_table[i].vendor; i++) {
|
|---|
| 592 | for(j=0; j<MAX_PCI_DEVICES; j++) {
|
|---|
| 593 | pcidev = &pci_devices[j];
|
|---|
| 594 | if (pcidev->devfn == 0) continue;
|
|---|
| 595 | if (pcidev->vendor != driver->id_table[i].vendor) continue;
|
|---|
| 596 | if ( (driver->id_table[i].device != PCI_ANY_ID) && (pcidev->device != driver->id_table[i].device) ) continue;
|
|---|
| 597 | dprintf(("pci unreg match: %x:%x %x:%x", pci_devices[j].vendor, pci_devices[j].device, driver->id_table[i].vendor, driver->id_table[i].device));
|
|---|
| 598 | if (driver->remove) driver->remove(pcidev);
|
|---|
| 599 | kfree(pcidev->bus);
|
|---|
| 600 | pcidev->devfn = 0;
|
|---|
| 601 | }
|
|---|
| 602 | }
|
|---|
| 603 | return 0;
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | /**
|
|---|
| 607 | */
|
|---|
| 608 | void pci_set_master(struct pci_dev *dev)
|
|---|
| 609 | {
|
|---|
| 610 | u16 cmd;
|
|---|
| 611 |
|
|---|
| 612 | pci_read_config_word(dev, PCI_COMMAND, &cmd);
|
|---|
| 613 | if (! (cmd & PCI_COMMAND_MASTER)) {
|
|---|
| 614 | dprintf(("pci_set_master %x", dev));
|
|---|
| 615 | cmd |= PCI_COMMAND_MASTER;
|
|---|
| 616 | pci_write_config_word(dev, PCI_COMMAND, cmd);
|
|---|
| 617 | }
|
|---|
| 618 | return;
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | /**
|
|---|
| 622 | * Register a device with power management
|
|---|
| 623 | */
|
|---|
| 624 | struct pm_dev *pm_register(pm_dev_t type, unsigned long id, pm_callback callback)
|
|---|
| 625 | {
|
|---|
| 626 | dprintf(("pm_register STUB"));
|
|---|
| 627 | DebugInt3();
|
|---|
| 628 | return NULL;
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | /**
|
|---|
| 632 | * Unregister a device with power management
|
|---|
| 633 | */
|
|---|
| 634 | void pm_unregister(struct pm_dev *dev)
|
|---|
| 635 | {
|
|---|
| 636 | dprintf(("pm_unregister STUB"));
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | /**
|
|---|
| 640 | */
|
|---|
| 641 | int __compat_get_order(unsigned long size)
|
|---|
| 642 | {
|
|---|
| 643 | int order;
|
|---|
| 644 |
|
|---|
| 645 | size = (size-1) >> (PAGE_SHIFT-1);
|
|---|
| 646 | order = -1;
|
|---|
| 647 | do {
|
|---|
| 648 | size >>= 1;
|
|---|
| 649 | order++;
|
|---|
| 650 | } while (size);
|
|---|
| 651 | return order;
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | /**
|
|---|
| 655 | */
|
|---|
| 656 | void *pci_alloc_consistent(struct pci_dev *hwdev,
|
|---|
| 657 | long size, dma_addr_t *dma_handle)
|
|---|
| 658 | {
|
|---|
| 659 | void *ret = NULL;
|
|---|
| 660 | int gfp = GFP_ATOMIC;
|
|---|
| 661 | int order;
|
|---|
| 662 | dprintf(("pci_alloc_consistent %d mask %x", size, (hwdev) ? hwdev->dma_mask : 0));
|
|---|
| 663 | if (hwdev == NULL || hwdev->dma_mask != 0xffffffff) {
|
|---|
| 664 | //try not to exhaust low memory (< 16mb) so allocate from the high region first
|
|---|
| 665 | //if that doesn't satisfy the dma mask requirement, then get it from the low
|
|---|
| 666 | //region anyway
|
|---|
| 667 | if(hwdev->dma_mask > 0x00ffffff) {
|
|---|
| 668 | order = __compat_get_order(size);
|
|---|
| 669 | ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
|
|---|
| 670 | *dma_handle = virt_to_bus(ret);
|
|---|
| 671 | if(*dma_handle > hwdev->dma_mask) {
|
|---|
| 672 | free_pages((unsigned long)ret, __compat_get_order(size));
|
|---|
| 673 | //be sure and allocate below 16 mb
|
|---|
| 674 | gfp |= GFP_DMA;
|
|---|
| 675 | ret = NULL;
|
|---|
| 676 | }
|
|---|
| 677 | }
|
|---|
| 678 | else { //must always allocate below 16 mb
|
|---|
| 679 | gfp |= GFP_DMA;
|
|---|
| 680 | }
|
|---|
| 681 | }
|
|---|
| 682 | if(ret == NULL) {
|
|---|
| 683 | ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
|
|---|
| 684 | }
|
|---|
| 685 | if (ret != NULL) {
|
|---|
| 686 | memset(ret, 0, size);
|
|---|
| 687 | *dma_handle = virt_to_bus(ret);
|
|---|
| 688 | }
|
|---|
| 689 | return ret;
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | #if 0
|
|---|
| 693 | void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
|
|---|
| 694 | dma_addr_t *dma_handle)
|
|---|
| 695 | {
|
|---|
| 696 | return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC);
|
|---|
| 697 | }
|
|---|
| 698 | #endif
|
|---|
| 699 | #if 0
|
|---|
| 700 | void *dma_alloc_coherent(struct device *dev, size_t size,
|
|---|
| 701 | dma_addr_t *dma_handle, gfp_t gfp)
|
|---|
| 702 | {
|
|---|
| 703 | void *ret = NULL;
|
|---|
| 704 | int order;
|
|---|
| 705 |
|
|---|
| 706 | dprintf(("dma_alloc_coherent %d mask %x", size, (dev) ? dev->dma_mask : 0));
|
|---|
| 707 | if (dev == NULL || *dev->dma_mask != 0xffffffff) {
|
|---|
| 708 | dprintf(("dma_alloc_coherent"));
|
|---|
| 709 | //try not to exhaust low memory (< 16mb) so allocate from the high region first
|
|---|
| 710 | //if that doesn't satisfy the dma mask requirement, then get it from the low
|
|---|
| 711 | //region anyway
|
|---|
| 712 | if(*dev->dma_mask > 0x00ffffff) {
|
|---|
| 713 | dprintf(("dma_alloc_coherent2"));
|
|---|
| 714 | order = __compat_get_order(size);
|
|---|
| 715 | ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
|
|---|
| 716 | *dma_handle = virt_to_bus(ret);
|
|---|
| 717 | if(*dma_handle > *dev->dma_mask) {
|
|---|
| 718 | dprintf(("dma_alloc_coherent3"));
|
|---|
| 719 | free_pages((unsigned long)ret, __compat_get_order(size));
|
|---|
| 720 | //be sure and allocate below 16 mb
|
|---|
| 721 | gfp |= GFP_DMA;
|
|---|
| 722 | ret = NULL;
|
|---|
| 723 | }
|
|---|
| 724 | dprintf(("dma_alloc_coherent3a"));
|
|---|
| 725 | }
|
|---|
| 726 | else { //must always allocate below 16 mb
|
|---|
| 727 | dprintf(("dma_alloc_coherent4"));
|
|---|
| 728 | gfp |= GFP_DMA;
|
|---|
| 729 | }
|
|---|
| 730 | }
|
|---|
| 731 | if(ret == NULL) {
|
|---|
| 732 | dprintf(("dma_alloc_coherent5"));
|
|---|
| 733 | ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | if (ret != NULL) {
|
|---|
| 737 | memset(ret, 0, size);
|
|---|
| 738 | *dma_handle = virt_to_bus(ret);
|
|---|
| 739 | }
|
|---|
| 740 | return ret;
|
|---|
| 741 |
|
|---|
| 742 | }
|
|---|
| 743 | #endif
|
|---|
| 744 |
|
|---|
| 745 | int dma_supported(struct device *dev, u64 mask)
|
|---|
| 746 | {
|
|---|
| 747 | return 1;
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | int dma_set_coherent_mask(struct device *dev, u64 mask)
|
|---|
| 751 | {
|
|---|
| 752 | /*
|
|---|
| 753 | * Truncate the mask to the actually supported dma_addr_t width to
|
|---|
| 754 | * avoid generating unsupportable addresses.
|
|---|
| 755 | */
|
|---|
| 756 | mask = (dma_addr_t)mask;
|
|---|
| 757 |
|
|---|
| 758 | if (!dma_supported(dev, mask))
|
|---|
| 759 | return -EIO;
|
|---|
| 760 |
|
|---|
| 761 | dev->coherent_dma_mask = mask;
|
|---|
| 762 | return 0;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | int dma_set_mask(struct device *dev, u64 mask)
|
|---|
| 766 | {
|
|---|
| 767 | /*
|
|---|
| 768 | * Truncate the mask to the actually supported dma_addr_t width to
|
|---|
| 769 | * avoid generating unsupportable addresses.
|
|---|
| 770 | */
|
|---|
| 771 | mask = (dma_addr_t)mask;
|
|---|
| 772 |
|
|---|
| 773 | if (!dev->dma_mask || !dma_supported(dev, mask))
|
|---|
| 774 | return -EIO;
|
|---|
| 775 |
|
|---|
| 776 | *dev->dma_mask = mask;
|
|---|
| 777 | return 0;
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | /**
|
|---|
| 781 | */
|
|---|
| 782 | void pci_free_consistent(struct pci_dev *hwdev, long size,
|
|---|
| 783 | void *vaddr, dma_addr_t dma_handle)
|
|---|
| 784 | {
|
|---|
| 785 | free_pages((unsigned long)vaddr, __compat_get_order(size));
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | /**
|
|---|
| 789 | */
|
|---|
| 790 | void pci_set_driver_data (struct pci_dev *dev, void *driver_data)
|
|---|
| 791 | {
|
|---|
| 792 | if (dev)
|
|---|
| 793 | dev->driver_data = driver_data;
|
|---|
| 794 | }
|
|---|
| 795 |
|
|---|
| 796 | /**
|
|---|
| 797 | */
|
|---|
| 798 | void *pci_get_driver_data (struct pci_dev *dev)
|
|---|
| 799 | {
|
|---|
| 800 | if (dev)
|
|---|
| 801 | return dev->driver_data;
|
|---|
| 802 | return 0;
|
|---|
| 803 | }
|
|---|
| 804 |
|
|---|
| 805 | /**
|
|---|
| 806 | */
|
|---|
| 807 | unsigned long pci_get_dma_mask (struct pci_dev *dev)
|
|---|
| 808 | {
|
|---|
| 809 | if (dev)
|
|---|
| 810 | return dev->dma_mask;
|
|---|
| 811 | return 0;
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | /**
|
|---|
| 815 | */
|
|---|
| 816 | int release_resource(struct resource *newres)
|
|---|
| 817 | {
|
|---|
| 818 | return 0;
|
|---|
| 819 | }
|
|---|
| 820 |
|
|---|
| 821 | /**
|
|---|
| 822 | */
|
|---|
| 823 | int pci_set_latency_time(struct pci_dev *dev, int latency)
|
|---|
| 824 | {
|
|---|
| 825 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
|
|---|
| 826 | return 0;
|
|---|
| 827 | }
|
|---|
| 828 |
|
|---|
| 829 | /**
|
|---|
| 830 | * pci_save_state - save the PCI configuration space of a device before suspending
|
|---|
| 831 | * @dev: - PCI device that we're dealing with
|
|---|
| 832 | * @buffer: - buffer to hold config space context
|
|---|
| 833 | *
|
|---|
| 834 | * @buffer must be large enough to hold the entire PCI 2.2 config space
|
|---|
| 835 | * (>= 64 bytes).
|
|---|
| 836 | */
|
|---|
| 837 | int pci_orig_save_state(struct pci_dev *dev, u32 *buffer)
|
|---|
| 838 | {
|
|---|
| 839 | int i;
|
|---|
| 840 | if (buffer) {
|
|---|
| 841 | /* XXX: 100% dword access ok here? */
|
|---|
| 842 | for (i = 0; i < 16; i++)
|
|---|
| 843 | pci_read_config_dword(dev, i * 4,&buffer[i]);
|
|---|
| 844 | }
|
|---|
| 845 | return 0;
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | /**
|
|---|
| 849 | * pci_restore_state - Restore the saved state of a PCI device
|
|---|
| 850 | * @dev: - PCI device that we're dealing with
|
|---|
| 851 | * @buffer: - saved PCI config space
|
|---|
| 852 | *
|
|---|
| 853 | */
|
|---|
| 854 | int pci_orig_restore_state(struct pci_dev *dev, u32 *buffer)
|
|---|
| 855 | {
|
|---|
| 856 | int i;
|
|---|
| 857 |
|
|---|
| 858 | if (buffer) {
|
|---|
| 859 | for (i = 0; i < 16; i++)
|
|---|
| 860 | pci_write_config_dword(dev,i * 4, buffer[i]);
|
|---|
| 861 | }
|
|---|
| 862 | /*
|
|---|
| 863 | * otherwise, write the context information we know from bootup.
|
|---|
| 864 | * This works around a problem where warm-booting from Windows
|
|---|
| 865 | * combined with a D3(hot)->D0 transition causes PCI config
|
|---|
| 866 | * header data to be forgotten.
|
|---|
| 867 | */
|
|---|
| 868 | else {
|
|---|
| 869 | for (i = 0; i < 6; i ++)
|
|---|
| 870 | pci_write_config_dword(dev,
|
|---|
| 871 | PCI_BASE_ADDRESS_0 + (i * 4),
|
|---|
| 872 | dev->resource[i].start);
|
|---|
| 873 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
|
|---|
| 874 | }
|
|---|
| 875 | return 0;
|
|---|
| 876 | }
|
|---|
| 877 |
|
|---|
| 878 | struct saved_config_tbl {
|
|---|
| 879 | struct pci_dev *pci;
|
|---|
| 880 | u32 config[16];
|
|---|
| 881 | };
|
|---|
| 882 | static struct saved_config_tbl saved_tbl[16];
|
|---|
| 883 |
|
|---|
| 884 | int pci_save_state(struct pci_dev *pci)
|
|---|
| 885 | {
|
|---|
| 886 | int i;
|
|---|
| 887 | /* FIXME: mutex needed for race? */
|
|---|
| 888 | for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
|
|---|
| 889 | if (! saved_tbl[i].pci) {
|
|---|
| 890 | saved_tbl[i].pci = pci;
|
|---|
| 891 | pci_orig_save_state(pci, saved_tbl[i].config);
|
|---|
| 892 | return 1;
|
|---|
| 893 | }
|
|---|
| 894 | }
|
|---|
| 895 | printk(KERN_DEBUG "snd: no pci config space found!\n");
|
|---|
| 896 | return 0;
|
|---|
| 897 | }
|
|---|
| 898 |
|
|---|
| 899 | int pci_restore_state(struct pci_dev *pci)
|
|---|
| 900 | {
|
|---|
| 901 | int i;
|
|---|
| 902 | /* FIXME: mutex needed for race? */
|
|---|
| 903 | for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
|
|---|
| 904 | if (saved_tbl[i].pci == pci) {
|
|---|
| 905 | saved_tbl[i].pci = NULL;
|
|---|
| 906 | pci_orig_restore_state(pci, saved_tbl[i].config);
|
|---|
| 907 | return 0;
|
|---|
| 908 | }
|
|---|
| 909 | }
|
|---|
| 910 | printk(KERN_DEBUG "snd: no saved pci config!\n");
|
|---|
| 911 | return 1;
|
|---|
| 912 | }
|
|---|
| 913 |
|
|---|
| 914 | void pci_disable_device(struct pci_dev *dev)
|
|---|
| 915 | {
|
|---|
| 916 | u16 pci_command;
|
|---|
| 917 |
|
|---|
| 918 | pci_read_config_word(dev, PCI_COMMAND, &pci_command);
|
|---|
| 919 | if (pci_command & PCI_COMMAND_MASTER) {
|
|---|
| 920 | pci_command &= ~PCI_COMMAND_MASTER;
|
|---|
| 921 | pci_write_config_word(dev, PCI_COMMAND, pci_command);
|
|---|
| 922 | }
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | int pci_request_region(struct pci_dev *pdev, int bar, char *res_name)
|
|---|
| 926 | {
|
|---|
| 927 | int flags;
|
|---|
| 928 |
|
|---|
| 929 | if (pci_resource_len(pdev, bar) == 0)
|
|---|
| 930 | return 0;
|
|---|
| 931 | flags = pci_get_flags(pdev, bar);
|
|---|
| 932 | if (flags & IORESOURCE_IO) {
|
|---|
| 933 | if (check_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
|
|---|
| 934 | goto err_out;
|
|---|
| 935 | request_region(pci_resource_start(pdev, bar),
|
|---|
| 936 | pci_resource_len(pdev, bar), res_name);
|
|---|
| 937 | }
|
|---|
| 938 | else if (flags & IORESOURCE_MEM) {
|
|---|
| 939 | if (check_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
|
|---|
| 940 | goto err_out;
|
|---|
| 941 | request_mem_region(pci_resource_start(pdev, bar),
|
|---|
| 942 | pci_resource_len(pdev, bar), res_name);
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | return 0;
|
|---|
| 946 |
|
|---|
| 947 | err_out:
|
|---|
| 948 | printk(KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
|
|---|
| 949 | flags & IORESOURCE_IO ? "I/O" : "mem",
|
|---|
| 950 | bar + 1, /* PCI BAR # */
|
|---|
| 951 | pci_resource_len(pdev, bar), pci_resource_start(pdev, bar),
|
|---|
| 952 | res_name);
|
|---|
| 953 | return -EBUSY;
|
|---|
| 954 | }
|
|---|
| 955 |
|
|---|
| 956 | void pci_release_region(struct pci_dev *pdev, int bar)
|
|---|
| 957 | {
|
|---|
| 958 | int flags;
|
|---|
| 959 |
|
|---|
| 960 | if (pci_resource_len(pdev, bar) == 0)
|
|---|
| 961 | return;
|
|---|
| 962 | flags = pci_get_flags(pdev, bar);
|
|---|
| 963 | if (flags & IORESOURCE_IO) {
|
|---|
| 964 | release_region(pci_resource_start(pdev, bar),
|
|---|
| 965 | pci_resource_len(pdev, bar));
|
|---|
| 966 | }
|
|---|
| 967 | else if (flags & IORESOURCE_MEM) {
|
|---|
| 968 | release_mem_region(pci_resource_start(pdev, bar),
|
|---|
| 969 | pci_resource_len(pdev, bar));
|
|---|
| 970 | }
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | int pci_request_regions(struct pci_dev *pdev, char *res_name)
|
|---|
| 974 | {
|
|---|
| 975 | int i;
|
|---|
| 976 |
|
|---|
| 977 | for (i = 0; i < 6; i++)
|
|---|
| 978 | if (pci_request_region(pdev, i, res_name))
|
|---|
| 979 | goto err;
|
|---|
| 980 | return 0;
|
|---|
| 981 | err:
|
|---|
| 982 | while (--i >= 0)
|
|---|
| 983 | pci_release_region(pdev, i);
|
|---|
| 984 | return -EBUSY;
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | void pci_release_regions(struct pci_dev *pdev)
|
|---|
| 988 | {
|
|---|
| 989 | int i;
|
|---|
| 990 | for (i = 0; i < 6; i++)
|
|---|
| 991 | pci_release_region(pdev, i);
|
|---|
| 992 | }
|
|---|
| 993 |
|
|---|
| 994 | const struct pci_device_id * pci_match_id(const struct pci_device_id *ids, struct pci_dev *dev)
|
|---|
| 995 | {
|
|---|
| 996 | u16 subsystem_vendor, subsystem_device;
|
|---|
| 997 |
|
|---|
| 998 | pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
|
|---|
| 999 | pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subsystem_device);
|
|---|
| 1000 |
|
|---|
| 1001 | while (ids->vendor || ids->subvendor || ids->class_mask) {
|
|---|
| 1002 | if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
|
|---|
| 1003 | (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
|
|---|
| 1004 | (ids->subvendor == PCI_ANY_ID || ids->subvendor == subsystem_vendor) &&
|
|---|
| 1005 | (ids->subdevice == PCI_ANY_ID || ids->subdevice == subsystem_device) &&
|
|---|
| 1006 | !((ids->class ^ dev->class) & ids->class_mask))
|
|---|
| 1007 | return ids;
|
|---|
| 1008 | ids++;
|
|---|
| 1009 | }
|
|---|
| 1010 | return NULL;
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | /** snd_pci_dev_present
|
|---|
| 1014 | * Called by: various sound drivers
|
|---|
| 1015 | */
|
|---|
| 1016 | int snd_pci_dev_present(const struct pci_device_id *ids)
|
|---|
| 1017 | {
|
|---|
| 1018 | while (ids->vendor || ids->subvendor)
|
|---|
| 1019 | {
|
|---|
| 1020 | if (pci_find_device(ids->vendor, ids->subvendor, NULL)) return 1;
|
|---|
| 1021 | ids++;
|
|---|
| 1022 | }
|
|---|
| 1023 | return 0;
|
|---|
| 1024 | }
|
|---|
| 1025 |
|
|---|
| 1026 | struct pci_driver_mapping {
|
|---|
| 1027 | struct pci_dev *dev;
|
|---|
| 1028 | struct pci_driver *drv;
|
|---|
| 1029 | unsigned long dma_mask;
|
|---|
| 1030 | void *driver_data;
|
|---|
| 1031 | u32 saved_config[16];
|
|---|
| 1032 | };
|
|---|
| 1033 |
|
|---|
| 1034 | #define PCI_MAX_MAPPINGS 64
|
|---|
| 1035 | static struct pci_driver_mapping drvmap [PCI_MAX_MAPPINGS] = { { NULL, } , };
|
|---|
| 1036 |
|
|---|
| 1037 |
|
|---|
| 1038 | static struct pci_driver_mapping *get_pci_driver_mapping(struct pci_dev *dev)
|
|---|
| 1039 | {
|
|---|
| 1040 | int i;
|
|---|
| 1041 |
|
|---|
| 1042 | for (i = 0; i < PCI_MAX_MAPPINGS; i++)
|
|---|
| 1043 | if (drvmap[i].dev == dev)
|
|---|
| 1044 | return &drvmap[i];
|
|---|
| 1045 | return NULL;
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | struct pci_driver *snd_pci_compat_get_pci_driver(struct pci_dev *dev)
|
|---|
| 1049 | {
|
|---|
| 1050 | struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
|
|---|
| 1051 | if (map)
|
|---|
| 1052 | return map->drv;
|
|---|
| 1053 | return NULL;
|
|---|
| 1054 | }
|
|---|
| 1055 | #if 0
|
|---|
| 1056 | void * pci_get_drvdata (struct pci_dev *dev)
|
|---|
| 1057 | {
|
|---|
| 1058 | struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
|
|---|
| 1059 | if (map)
|
|---|
| 1060 | return map->driver_data;
|
|---|
| 1061 | return NULL;
|
|---|
| 1062 | }
|
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 | void pci_set_drvdata (struct pci_dev *dev, void *driver_data)
|
|---|
| 1066 | {
|
|---|
| 1067 | struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
|
|---|
| 1068 | if (map)
|
|---|
| 1069 | map->driver_data = driver_data;
|
|---|
| 1070 | }
|
|---|
| 1071 | #endif
|
|---|
| 1072 |
|
|---|
| 1073 |
|
|---|
| 1074 | //******************************************************************************
|
|---|
| 1075 | //******************************************************************************
|
|---|
| 1076 | OSSRET OSS32_APMResume()
|
|---|
| 1077 | {
|
|---|
| 1078 | int i;
|
|---|
| 1079 | struct pci_driver *driver;
|
|---|
| 1080 |
|
|---|
| 1081 | dprintf(("OSS32_APMResume"));
|
|---|
| 1082 |
|
|---|
| 1083 | for(i=0;i<MAX_PCI_DEVICES;i++)
|
|---|
| 1084 | {
|
|---|
| 1085 | if(pci_devices[i].devfn)
|
|---|
| 1086 | {
|
|---|
| 1087 | RMSetHandles(pci_devices[i].hAdapter); /* DAZ - dirty hack */
|
|---|
| 1088 | driver = pci_devices[i].pcidriver;
|
|---|
| 1089 | if(driver && driver->resume) {
|
|---|
| 1090 | driver->resume(&pci_devices[i]);
|
|---|
| 1091 | }
|
|---|
| 1092 | }
|
|---|
| 1093 | }
|
|---|
| 1094 |
|
|---|
| 1095 | return OSSERR_SUCCESS;
|
|---|
| 1096 | }
|
|---|
| 1097 | //******************************************************************************
|
|---|
| 1098 | //******************************************************************************
|
|---|
| 1099 | OSSRET OSS32_APMSuspend()
|
|---|
| 1100 | {
|
|---|
| 1101 | int i;
|
|---|
| 1102 | struct pci_driver *driver;
|
|---|
| 1103 |
|
|---|
| 1104 | dprintf(("OSS32_APMSuspend 1"));
|
|---|
| 1105 |
|
|---|
| 1106 | for(i=0;i<MAX_PCI_DEVICES;i++)
|
|---|
| 1107 | {
|
|---|
| 1108 | if(pci_devices[i].devfn)
|
|---|
| 1109 | {
|
|---|
| 1110 | RMSetHandles(pci_devices[i].hAdapter); /* DAZ - dirty hack */
|
|---|
| 1111 | driver = pci_devices[i].pcidriver;
|
|---|
| 1112 | if(driver && driver->suspend) {
|
|---|
| 1113 | driver->suspend(&pci_devices[i], SNDRV_CTL_POWER_D3cold);
|
|---|
| 1114 | }
|
|---|
| 1115 | }
|
|---|
| 1116 | }
|
|---|
| 1117 |
|
|---|
| 1118 | dprintf(("OSS32_APMSuspend 2"));
|
|---|
| 1119 | return OSSERR_SUCCESS;
|
|---|
| 1120 | }
|
|---|
| 1121 |
|
|---|
| 1122 | #ifdef USE_MSI
|
|---|
| 1123 | extern int __syscall UniMsiAlloc(USHORT usBusDevFunc, ULONG *pulCount, UCHAR *pucIrq);
|
|---|
| 1124 | int snd_pci_enable_msi(struct pci_dev *dev)
|
|---|
| 1125 | {
|
|---|
| 1126 | ULONG p;
|
|---|
| 1127 | UCHAR irq;
|
|---|
| 1128 |
|
|---|
| 1129 | if (dev->irq_pin)
|
|---|
| 1130 | {
|
|---|
| 1131 | p = 1; /* int count */
|
|---|
| 1132 | if (UniMsiAlloc((dev->bus->number<<8) | dev->devfn, &p, &irq)) return -1;
|
|---|
| 1133 | /* we have an msi interrupt */
|
|---|
| 1134 | dev->irq = irq;
|
|---|
| 1135 | dev->irq_pin = 0;
|
|---|
| 1136 | }
|
|---|
| 1137 | return 0;
|
|---|
| 1138 | }
|
|---|
| 1139 | #else
|
|---|
| 1140 | int snd_pci_enable_msi(struct pci_dev *dev)
|
|---|
| 1141 | {
|
|---|
| 1142 | return -1;
|
|---|
| 1143 | }
|
|---|
| 1144 | #endif
|
|---|
| 1145 |
|
|---|
| 1146 | /**
|
|---|
| 1147 | * pci_status_get_and_clear_errors - return and clear error bits in PCI_STATUS
|
|---|
| 1148 | * @pdev: the PCI device
|
|---|
| 1149 | *
|
|---|
| 1150 | * Returns error bits set in PCI_STATUS and clears them.
|
|---|
| 1151 | */
|
|---|
| 1152 | int pci_status_get_and_clear_errors(struct pci_dev *pdev)
|
|---|
| 1153 | {
|
|---|
| 1154 | u16 status;
|
|---|
| 1155 | int ret;
|
|---|
| 1156 |
|
|---|
| 1157 | ret = pci_read_config_word(pdev, PCI_STATUS, &status);
|
|---|
| 1158 | if (ret != PCIBIOS_SUCCESSFUL)
|
|---|
| 1159 | return -EIO;
|
|---|
| 1160 |
|
|---|
| 1161 | status &= PCI_STATUS_ERROR_BITS;
|
|---|
| 1162 | if (status)
|
|---|
| 1163 | pci_write_config_word(pdev, PCI_STATUS, status);
|
|---|
| 1164 |
|
|---|
| 1165 | return status;
|
|---|
| 1166 | }
|
|---|
| 1167 |
|
|---|