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

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

interrupt fixes

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