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

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

merge interrupt fixes from trunk

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 fRewired = 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 if (PicIRQ) ulTmp1 = (ulTmp1 & (~0xff)) | (PicIRQ & 0xff); // Choose Pic interrupt for init time processing
251 else if (ApicIRQ) ulTmp1 = (ulTmp1 & (~0xff)) | (ApicIRQ & 0xff);
252 rprintf(("pci_query_device: IRQs ACPI PIC=%ld APIC=%ld chosen=%d", PicIRQ, ApicIRQ, ulTmp1&0xff));
253 pcidev->picirq = PicIRQ; // Save the Pic and
254 pcidev->apicirq = ApicIRQ; // Save the Apic interrupt for switching later
255 }
256#endif /* ACPI */
257 if( (u8)ulTmp1 && (u8)ulTmp1 != 0xff ) {
258 pcidev->irq_resource[0].flags = IORESOURCE_IRQ;
259 pcidev->irq_resource[0].start = pcidev->irq_resource[0].end = ulTmp1 & 0xffff;
260 pcidev->irq = (u8)ulTmp1; // This is the interrupt used for init time processing
261 }
262
263 return (0x8000 | (busNr << 8) | PCI_DEVFN(devNr, funcNr));
264 } /* for funcNr */
265 funcNr = 0;
266 } /* for devNr */
267 devNr = 0;
268 }
269 outl(cfgaddrreg, PCI_CONFIG_ADDRESS);
270 return 0;
271}
272
273//******************************************************************************
274//******************************************************************************
275// Called from:
276//if from==NULL search pci bus
277//if from!=NULL only search already found devices starting with from
278struct pci_dev *pci_find_device (unsigned int vendor, unsigned int device, struct pci_dev *from)
279{
280 int i;
281 struct pci_device_id id_table;
282
283 for(i=0;i<MAX_PCI_DEVICES;i++) {
284 if ( pci_devices[i].devfn && (pci_devices[i].vendor == vendor) && (pci_devices[i].device == device) ) return &pci_devices[i];
285 }
286
287 for(i=0;i<MAX_PCI_DEVICES;i++) {
288 if(pci_devices[i].devfn == 0) {
289 memset(&id_table, 0, sizeof(id_table));
290 id_table.vendor = vendor;
291 id_table.device = device;
292 if( pci_query_device(&id_table, (struct pci_dev near *)&pci_devices[i], 0) ) return &pci_devices[i];
293 else break;
294 }
295 }
296
297 return NULL;
298}
299//******************************************************************************
300//******************************************************************************
301struct resource * __request_region(struct resource *a, unsigned long start,
302 unsigned long n, const char *name)
303{
304 struct resource *resource;
305
306 if(a->flags & IORESOURCE_MEM) {
307 if(RMRequestMem(/*hResMgr,*/ start, n) == FALSE) {
308 printk("RMRequestIO failed for io %x, length %x\n", start, n);
309 return NULL;
310 }
311 }
312 else if(a->flags & IORESOURCE_IO) {
313 if(RMRequestIO(/*hResMgr,*/ start, n) == FALSE) {
314 printk("RMRequestIO failed for io %x, length %x\n", start, n);
315 return NULL;
316 }
317 }
318
319 resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
320 if (resource == NULL)
321 return NULL;
322 resource->name = name;
323 resource->start = start;
324 resource->end = start + n; // - 1;
325 resource->flags = a->flags;
326 resource->parent =
327 resource->child = NULL;
328
329 // insert in list
330 resource->sibling = a->sibling;
331 a->sibling = resource;
332
333 return resource;
334}
335//******************************************************************************
336//******************************************************************************
337void __release_region(struct resource *a,
338 unsigned long start, unsigned long n)
339{
340 struct resource *resource;
341 struct resource **ppres = &a->sibling;
342 unsigned long end = start + n; // - 1;
343
344 while( *ppres )
345 {
346 resource = *ppres;
347
348 if( resource->start == start && resource->end == end )
349 {
350 // remove from list
351 *ppres = resource->sibling;
352 kfree(resource);
353 return;
354 }
355
356 ppres = &resource->sibling;
357 }
358}
359//******************************************************************************
360//******************************************************************************
361int pci_get_flags (struct pci_dev *dev, int n_base)
362{
363 if(n_base >= DEVICE_COUNT_RESOURCE || !dev->resource[n_base].flags) {
364 DebugInt3();
365 return 0;
366 }
367 return dev->resource[n_base].flags;
368}
369//******************************************************************************
370//******************************************************************************
371int pcibios_present(void)
372{
373 printk("pcibios_present -> pretend BIOS present\n");
374 return 1;
375}
376//******************************************************************************
377//******************************************************************************
378struct pci_dev *pci_find_slot (unsigned int bus, unsigned int devfn)
379{
380 printk("pci_find_slot %d %x not implemented!!\n", bus, devfn);
381 DebugInt3();
382 return NULL;
383}
384//******************************************************************************
385//******************************************************************************
386int pci_dma_supported(struct pci_dev *dev, unsigned long mask)
387{
388 printk("pci_dma_supported: return TRUE\n");
389 return 1;
390}
391//******************************************************************************
392//******************************************************************************
393int pci_find_capability(struct pci_dev *dev, int cap)
394{
395 u16 status;
396 u8 pos, id;
397 int ttl = 48;
398
399 pci_read_config_word(dev, PCI_STATUS, &status);
400 if (!(status & PCI_STATUS_CAP_LIST))
401 return 0;
402 pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &pos);
403 while (ttl-- && pos >= 0x40) {
404 pos &= ~3;
405 pci_read_config_byte(dev, pos + PCI_CAP_LIST_ID, &id);
406 if (id == 0xff)
407 break;
408 if (id == cap)
409 return pos;
410 pci_read_config_byte(dev, pos + PCI_CAP_LIST_NEXT, &pos);
411 }
412 return 0;
413}
414//******************************************************************************
415/*
416 * Set power management state of a device. For transitions from state D3
417 * it isn't as straightforward as one could assume since many devices forget
418 * their configuration space during wakeup. Returns old power state.
419 */
420//******************************************************************************
421int pci_set_power_state(struct pci_dev *dev, int new_state)
422{
423 u32 base[5], romaddr;
424 u16 pci_command, pwr_command;
425 u8 pci_latency, pci_cacheline;
426 int i, old_state;
427 int pm = pci_find_capability(dev, PCI_CAP_ID_PM);
428
429 if (!pm)
430 return 0;
431 pci_read_config_word(dev, pm + PCI_PM_CTRL, &pwr_command);
432 old_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
433 if (old_state == new_state)
434 return old_state;
435 if (old_state == 3) {
436 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
437 pci_write_config_word(dev, PCI_COMMAND, pci_command & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
438 for (i = 0; i < 5; i++)
439 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + i*4, &base[i]);
440 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &romaddr);
441 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
442 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_cacheline);
443 pci_write_config_word(dev, pm + PCI_PM_CTRL, new_state);
444 for (i = 0; i < 5; i++)
445 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + i*4, base[i]);
446 pci_write_config_dword(dev, PCI_ROM_ADDRESS, romaddr);
447 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
448 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cacheline);
449 pci_write_config_byte(dev, PCI_LATENCY_TIMER, pci_latency);
450 pci_write_config_word(dev, PCI_COMMAND, pci_command);
451 } else
452 pci_write_config_word(dev, pm + PCI_PM_CTRL, (pwr_command & ~PCI_PM_CTRL_STATE_MASK) | new_state);
453 return old_state;
454}
455//******************************************************************************
456/*
457 * Initialize device before it's used by a driver. Ask low-level code
458 * to enable I/O and memory. Wake up the device if it was suspended.
459 * Beware, this function can fail.
460 */
461//******************************************************************************
462int pci_enable_device(struct pci_dev *dev)
463{
464 u16 pci_command;
465
466 dprintf(("pci_enable_device %x\n", dev));
467
468 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
469 pci_write_config_word(dev, PCI_COMMAND, pci_command | (PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
470 pci_set_power_state(dev, 0);
471 return 0;
472}
473//******************************************************************************
474//******************************************************************************
475int pci_register_driver(struct pci_driver *driver)
476{
477 int iTableIx, iNumCards, iTmp;
478 ULONG ulLast;
479 struct pci_dev *pcidev;
480
481 if (!driver->probe) return 0;
482
483 iNumCards = 0;
484
485 /* find an empty slot */
486 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++) {
487 if (pci_devices[iTmp].devfn == 0) break;
488 }
489 if (iTmp >= MAX_PCI_DEVICES) return 0;
490 pcidev = &pci_devices[iTmp];
491
492 for( iTableIx = 0; driver->id_table[iTableIx].vendor; iTableIx++) {
493 ulLast = 0;
494 while( (ulLast = pci_query_device(&driver->id_table[iTableIx], pcidev, ulLast)) ) {
495
496 RMInit();
497 dprintf(("pci_register_driver: found=%x:%x searching for %x:%x with %s",
498 pcidev->vendor, pcidev->device, driver->id_table[iTableIx].vendor, driver->id_table[iTableIx].device, driver->name));
499
500 if(driver->probe(pcidev, &driver->id_table[iTableIx]) == 0) {
501 pcidev->pcidriver = (void *)driver;
502 pcidev->current_state = 4;
503
504 // create adapter
505 RMDone((pcidev->device << 16) | pcidev->vendor, &pcidev->hAdapter, &pcidev->hDevice);
506 iNumCards++;
507
508 /* find another empty slot */
509 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++) {
510 if (pci_devices[iTmp].devfn == 0) break;
511 }
512 if (iTmp >= MAX_PCI_DEVICES) break;
513 pcidev = &pci_devices[iTmp];
514 } else {
515 kfree(pcidev->bus);
516 pcidev->devfn = 0;
517 }
518
519 RMDone(0, 0, 0);
520 }
521 }
522
523 return iNumCards;
524}
525//******************************************************************************
526//******************************************************************************
527int pci_module_init(struct pci_driver *drv)
528{
529 int res = pci_register_driver(drv);
530 if (res == 0) return -ENODEV;
531 return res;
532}
533//******************************************************************************
534//******************************************************************************
535int pci_unregister_driver(struct pci_driver *driver)
536{
537 struct pci_dev *pcidev;
538 int i, j;
539
540 for (i=0; driver->id_table[i].vendor; i++) {
541 for(j=0; j<MAX_PCI_DEVICES; j++) {
542 pcidev = &pci_devices[j];
543 if (pcidev->devfn == 0) continue;
544 if (pcidev->vendor != driver->id_table[i].vendor) continue;
545 if ( (driver->id_table[i].device != PCI_ANY_ID) && (pcidev->device != driver->id_table[i].device) ) continue;
546 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));
547 if (driver->remove) driver->remove(pcidev);
548 kfree(pcidev->bus);
549 pcidev->devfn = 0;
550 }
551 }
552 return 0;
553}
554//******************************************************************************
555//******************************************************************************
556void pci_set_master(struct pci_dev *dev)
557{
558 u16 cmd;
559
560 pci_read_config_word(dev, PCI_COMMAND, &cmd);
561 if (! (cmd & PCI_COMMAND_MASTER)) {
562 dprintf(("pci_set_master %x", dev));
563 cmd |= PCI_COMMAND_MASTER;
564 pci_write_config_word(dev, PCI_COMMAND, cmd);
565 }
566 return;
567}
568//******************************************************************************
569// * Register a device with power management
570//******************************************************************************
571struct pm_dev *pm_register(pm_dev_t type, unsigned long id, pm_callback callback)
572{
573 dprintf(("pm_register STUB"));
574 DebugInt3();
575 return NULL;
576}
577//******************************************************************************
578// * Unregister a device with power management
579//******************************************************************************
580void pm_unregister(struct pm_dev *dev)
581{
582 dprintf(("pm_unregister STUB"));
583}
584//******************************************************************************
585//******************************************************************************
586int __compat_get_order(unsigned long size)
587{
588 int order;
589
590 size = (size-1) >> (PAGE_SHIFT-1);
591 order = -1;
592 do {
593 size >>= 1;
594 order++;
595 } while (size);
596 return order;
597}
598//******************************************************************************
599//******************************************************************************
600void *pci_alloc_consistent(struct pci_dev *hwdev,
601 long size, dma_addr_t *dma_handle)
602{
603 void *ret = NULL;
604 int gfp = GFP_ATOMIC;
605 int order;
606 dprintf(("pci_alloc_consistent %d mask %x", size, (hwdev) ? hwdev->dma_mask : 0));
607 if (hwdev == NULL || hwdev->dma_mask != 0xffffffff) {
608 //try not to exhaust low memory (< 16mb) so allocate from the high region first
609 //if that doesn't satisfy the dma mask requirement, then get it from the low
610 //regino anyway
611 if(hwdev->dma_mask > 0x00ffffff) {
612 order = __compat_get_order(size);
613 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
614 *dma_handle = virt_to_bus(ret);
615 if(*dma_handle > hwdev->dma_mask) {
616 free_pages((unsigned long)ret, __compat_get_order(size));
617 //be sure and allocate below 16 mb
618 gfp |= GFP_DMA;
619 ret = NULL;
620 }
621 }
622 else { //must always allocate below 16 mb
623 gfp |= GFP_DMA;
624 }
625 }
626 if(ret == NULL) {
627 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
628 }
629
630 if (ret != NULL) {
631 memset(ret, 0, size);
632 *dma_handle = virt_to_bus(ret);
633 }
634 return ret;
635}
636//******************************************************************************
637//******************************************************************************
638void pci_free_consistent(struct pci_dev *hwdev, long size,
639 void *vaddr, dma_addr_t dma_handle)
640{
641 free_pages((unsigned long)vaddr, __compat_get_order(size));
642}
643//******************************************************************************
644//******************************************************************************
645void pci_set_driver_data (struct pci_dev *dev, void *driver_data)
646{
647 if (dev)
648 dev->driver_data = driver_data;
649}
650//******************************************************************************
651//******************************************************************************
652void *pci_get_driver_data (struct pci_dev *dev)
653{
654 if (dev)
655 return dev->driver_data;
656 return 0;
657}
658//******************************************************************************
659//******************************************************************************
660unsigned long pci_get_dma_mask (struct pci_dev *dev)
661{
662 if (dev)
663 return dev->dma_mask;
664 return 0;
665}
666//******************************************************************************
667//******************************************************************************
668int release_resource(struct resource *newres)
669{
670 return 0;
671}
672
673//******************************************************************************
674//******************************************************************************
675int pci_set_latency_time(struct pci_dev *dev, int latency)
676{
677 pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
678 return 0;
679}
680
681//******************************************************************************
682//******************************************************************************
683/**
684 * pci_save_state - save the PCI configuration space of a device before suspending
685 * @dev: - PCI device that we're dealing with
686 * @buffer: - buffer to hold config space context
687 *
688 * @buffer must be large enough to hold the entire PCI 2.2 config space
689 * (>= 64 bytes).
690 */
691int pci_orig_save_state(struct pci_dev *dev, u32 *buffer)
692{
693 int i;
694 if (buffer) {
695 /* XXX: 100% dword access ok here? */
696 for (i = 0; i < 16; i++)
697 pci_read_config_dword(dev, i * 4,&buffer[i]);
698 }
699 return 0;
700}
701
702/**
703 * pci_restore_state - Restore the saved state of a PCI device
704 * @dev: - PCI device that we're dealing with
705 * @buffer: - saved PCI config space
706 *
707 */
708int
709pci_orig_restore_state(struct pci_dev *dev, u32 *buffer)
710{
711 int i;
712
713 if (buffer) {
714 for (i = 0; i < 16; i++)
715 pci_write_config_dword(dev,i * 4, buffer[i]);
716 }
717 /*
718 * otherwise, write the context information we know from bootup.
719 * This works around a problem where warm-booting from Windows
720 * combined with a D3(hot)->D0 transition causes PCI config
721 * header data to be forgotten.
722 */
723 else {
724 for (i = 0; i < 6; i ++)
725 pci_write_config_dword(dev,
726 PCI_BASE_ADDRESS_0 + (i * 4),
727 dev->resource[i].start);
728 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
729 }
730 return 0;
731}
732
733struct saved_config_tbl {
734 struct pci_dev *pci;
735 u32 config[16];
736};
737static struct saved_config_tbl saved_tbl[16];
738
739int pci_save_state(struct pci_dev *pci)
740{
741 int i;
742 /* FIXME: mutex needed for race? */
743 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
744 if (! saved_tbl[i].pci) {
745 saved_tbl[i].pci = pci;
746 pci_orig_save_state(pci, saved_tbl[i].config);
747 return 1;
748 }
749 }
750 printk(KERN_DEBUG "snd: no pci config space found!\n");
751 return 0;
752}
753
754int pci_restore_state(struct pci_dev *pci)
755{
756 int i;
757 /* FIXME: mutex needed for race? */
758 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
759 if (saved_tbl[i].pci == pci) {
760 saved_tbl[i].pci = NULL;
761 pci_orig_restore_state(pci, saved_tbl[i].config);
762 return 0;
763 }
764 }
765 printk(KERN_DEBUG "snd: no saved pci config!\n");
766 return 1;
767}
768
769void pci_disable_device(struct pci_dev *dev)
770{
771 u16 pci_command;
772
773 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
774 if (pci_command & PCI_COMMAND_MASTER) {
775 pci_command &= ~PCI_COMMAND_MASTER;
776 pci_write_config_word(dev, PCI_COMMAND, pci_command);
777 }
778}
779
780int pci_request_region(struct pci_dev *pdev, int bar, char *res_name)
781{
782 int flags;
783
784 if (pci_resource_len(pdev, bar) == 0)
785 return 0;
786 flags = pci_get_flags(pdev, bar);
787 if (flags & IORESOURCE_IO) {
788 if (check_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
789 goto err_out;
790 request_region(pci_resource_start(pdev, bar),
791 pci_resource_len(pdev, bar), res_name);
792 }
793 else if (flags & IORESOURCE_MEM) {
794 if (check_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
795 goto err_out;
796 request_mem_region(pci_resource_start(pdev, bar),
797 pci_resource_len(pdev, bar), res_name);
798 }
799
800 return 0;
801
802err_out:
803 printk(KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
804 flags & IORESOURCE_IO ? "I/O" : "mem",
805 bar + 1, /* PCI BAR # */
806 pci_resource_len(pdev, bar), pci_resource_start(pdev, bar),
807 res_name);
808 return -EBUSY;
809}
810
811void pci_release_region(struct pci_dev *pdev, int bar)
812{
813 int flags;
814
815 if (pci_resource_len(pdev, bar) == 0)
816 return;
817 flags = pci_get_flags(pdev, bar);
818 if (flags & IORESOURCE_IO) {
819 release_region(pci_resource_start(pdev, bar),
820 pci_resource_len(pdev, bar));
821 }
822 else if (flags & IORESOURCE_MEM) {
823 release_mem_region(pci_resource_start(pdev, bar),
824 pci_resource_len(pdev, bar));
825 }
826}
827
828int pci_request_regions(struct pci_dev *pdev, char *res_name)
829{
830 int i;
831
832 for (i = 0; i < 6; i++)
833 if (pci_request_region(pdev, i, res_name))
834 goto err;
835 return 0;
836 err:
837 while (--i >= 0)
838 pci_release_region(pdev, i);
839 return -EBUSY;
840}
841
842void pci_release_regions(struct pci_dev *pdev)
843{
844 int i;
845 for (i = 0; i < 6; i++)
846 pci_release_region(pdev, i);
847}
848
849const struct pci_device_id * pci_match_device(const struct pci_device_id *ids, struct pci_dev *dev)
850{
851 u16 subsystem_vendor, subsystem_device;
852
853 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
854 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subsystem_device);
855
856 while (ids->vendor || ids->subvendor || ids->class_mask) {
857 if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
858 (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
859 (ids->subvendor == PCI_ANY_ID || ids->subvendor == subsystem_vendor) &&
860 (ids->subdevice == PCI_ANY_ID || ids->subdevice == subsystem_device) &&
861 !((ids->class ^ dev->_class) & ids->class_mask))
862 return ids;
863 ids++;
864 }
865 return NULL;
866}
867
868int snd_pci_dev_present(const struct pci_device_id *ids)
869{
870 while (ids->vendor || ids->subvendor) {
871 if (pci_find_device(ids->vendor, ids->subvendor, NULL))
872 return 1;
873 ids++;
874 }
875 return 0;
876}
877
878struct pci_driver_mapping {
879 struct pci_dev *dev;
880 struct pci_driver *drv;
881 unsigned long dma_mask;
882 void *driver_data;
883 u32 saved_config[16];
884};
885
886#define PCI_MAX_MAPPINGS 64
887static struct pci_driver_mapping drvmap [PCI_MAX_MAPPINGS] = { { NULL, } , };
888
889
890static struct pci_driver_mapping *get_pci_driver_mapping(struct pci_dev *dev)
891{
892 int i;
893
894 for (i = 0; i < PCI_MAX_MAPPINGS; i++)
895 if (drvmap[i].dev == dev)
896 return &drvmap[i];
897 return NULL;
898}
899
900struct pci_driver *snd_pci_compat_get_pci_driver(struct pci_dev *dev)
901{
902 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
903 if (map)
904 return map->drv;
905 return NULL;
906}
907#if 0
908void * pci_get_drvdata (struct pci_dev *dev)
909{
910 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
911 if (map)
912 return map->driver_data;
913 return NULL;
914}
915
916
917void pci_set_drvdata (struct pci_dev *dev, void *driver_data)
918{
919 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
920 if (map)
921 map->driver_data = driver_data;
922}
923#endif
924
925
926//******************************************************************************
927//******************************************************************************
928OSSRET OSS32_APMResume()
929{
930 int i;
931 struct pci_driver *driver;
932
933 dprintf(("OSS32_APMResume"));
934
935 for(i=0;i<MAX_PCI_DEVICES;i++)
936 {
937 if(pci_devices[i].devfn)
938 {
939 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
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 for(i=0;i<MAX_PCI_DEVICES;i++)
959 {
960 if(pci_devices[i].devfn)
961 {
962 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
963 driver = pci_devices[i].pcidriver;
964 if(driver && driver->suspend) {
965 driver->suspend(&pci_devices[i], SNDRV_CTL_POWER_D3cold);
966 }
967 }
968 }
969
970 dprintf(("OSS32_APMSuspend 2"));
971 return OSSERR_SUCCESS;
972}
973
974void PciAdjustInterrupts() {
975 int i;
976 struct pci_dev *pcidev;
977 struct pci_driver *driver;
978 ULONG ulTmp1, rc;
979
980 for (i=0; i<MAX_PCI_DEVICES; i++) {
981 if (!pci_devices[i].devfn) continue;
982 pcidev = &pci_devices[i];
983 ulTmp1 = pcidev->irq;
984 if (pcidev->apicirq) ulTmp1 = pcidev->apicirq;
985 else if (pcidev->picirq) ulTmp1 = pcidev->picirq;
986 rprintf(("PciAdjustInterrupts: IRQs ACPI PIC=%ld APIC=%ld was=%d chosen=%ld", pcidev->picirq, pcidev->apicirq, pcidev->irq, ulTmp1));
987 if( (u8)ulTmp1 && ((u8)ulTmp1 != 0xff) && ((u8)ulTmp1 != pcidev->irq) ) {
988 RMSetHandles(pcidev->hAdapter, pcidev->hDevice); /* DAZ - dirty hack */
989 driver = pcidev->pcidriver;
990 if(driver && driver->suspend) driver->suspend(pcidev, SNDRV_CTL_POWER_D0);
991
992 pcidev->irq_resource[0].flags = IORESOURCE_IRQ;
993 pcidev->irq_resource[0].start = pcidev->irq_resource[0].end = ulTmp1 & 0xffff;
994 pcidev->irq = (u8)ulTmp1;
995
996 fRewired = TRUE;
997 // if(driver && driver->resume) driver->resume(pcidev);
998 }
999 } /* for loop */
1000}
Note: See TracBrowser for help on using the repository browser.