source: GPL/branches/DAZ/lib32/pci.c

Last change on this file was 580, checked in by David Azarewicz, 11 years ago

Cleanup build environment

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