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

Last change on this file since 593 was 593, checked in by David Azarewicz, 8 years ago

Rework device detect logic

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