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