source: GPL/branches/uniaud32-2.1.x/lib32/pci.c@ 548

Last change on this file since 548 was 548, checked in by David Azarewicz, 14 years ago

APIC interrupt fixes from trunk

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