source: GPL/trunk/lib32/pci.c@ 602

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

Changed behavior of /A paramater.

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 ((iAdapterNumber >= 0) && (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
536 if (pcidev)
537 {
538 kfree(pcidev->bus);
539 pcidev->devfn = 0;
540 }
541 else
542 {
543 if (iAdapterNumber >= 0) break;
544 /* find another empty slot */
545 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
546 {
547 if (pci_devices[iTmp].devfn == 0) break;
548 }
549 if (iTmp >= MAX_PCI_DEVICES) break;
550 pcidev = &pci_devices[iTmp];
551 }
552 } /* pci_query_device loop */
553
554 return iNumCards;
555}
556
557/**
558 */
559int pci_module_init(struct pci_driver *drv)
560{
561 int res = pci_register_driver(drv);
562 if (res == 0) return -ENODEV;
563 return res;
564}
565
566/**
567 */
568int pci_unregister_driver(struct pci_driver *driver)
569{
570 struct pci_dev *pcidev;
571 int i, j;
572
573 for (i=0; driver->id_table[i].vendor; i++) {
574 for(j=0; j<MAX_PCI_DEVICES; j++) {
575 pcidev = &pci_devices[j];
576 if (pcidev->devfn == 0) continue;
577 if (pcidev->vendor != driver->id_table[i].vendor) continue;
578 if ( (driver->id_table[i].device != PCI_ANY_ID) && (pcidev->device != driver->id_table[i].device) ) continue;
579 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));
580 if (driver->remove) driver->remove(pcidev);
581 kfree(pcidev->bus);
582 pcidev->devfn = 0;
583 }
584 }
585 return 0;
586}
587
588/**
589 */
590void pci_set_master(struct pci_dev *dev)
591{
592 u16 cmd;
593
594 pci_read_config_word(dev, PCI_COMMAND, &cmd);
595 if (! (cmd & PCI_COMMAND_MASTER)) {
596 dprintf(("pci_set_master %x", dev));
597 cmd |= PCI_COMMAND_MASTER;
598 pci_write_config_word(dev, PCI_COMMAND, cmd);
599 }
600 return;
601}
602
603/**
604 * Register a device with power management
605 */
606struct pm_dev *pm_register(pm_dev_t type, unsigned long id, pm_callback callback)
607{
608 dprintf(("pm_register STUB"));
609 DebugInt3();
610 return NULL;
611}
612
613/**
614 * Unregister a device with power management
615 */
616void pm_unregister(struct pm_dev *dev)
617{
618 dprintf(("pm_unregister STUB"));
619}
620
621/**
622 */
623int __compat_get_order(unsigned long size)
624{
625 int order;
626
627 size = (size-1) >> (PAGE_SHIFT-1);
628 order = -1;
629 do {
630 size >>= 1;
631 order++;
632 } while (size);
633 return order;
634}
635
636/**
637 */
638void *pci_alloc_consistent(struct pci_dev *hwdev,
639 long size, dma_addr_t *dma_handle)
640{
641 void *ret = NULL;
642 int gfp = GFP_ATOMIC;
643 int order;
644 dprintf(("pci_alloc_consistent %d mask %x", size, (hwdev) ? hwdev->dma_mask : 0));
645 if (hwdev == NULL || hwdev->dma_mask != 0xffffffff) {
646 //try not to exhaust low memory (< 16mb) so allocate from the high region first
647 //if that doesn't satisfy the dma mask requirement, then get it from the low
648 //regino anyway
649 if(hwdev->dma_mask > 0x00ffffff) {
650 order = __compat_get_order(size);
651 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
652 *dma_handle = virt_to_bus(ret);
653 if(*dma_handle > hwdev->dma_mask) {
654 free_pages((unsigned long)ret, __compat_get_order(size));
655 //be sure and allocate below 16 mb
656 gfp |= GFP_DMA;
657 ret = NULL;
658 }
659 }
660 else { //must always allocate below 16 mb
661 gfp |= GFP_DMA;
662 }
663 }
664 if(ret == NULL) {
665 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
666 }
667
668 if (ret != NULL) {
669 memset(ret, 0, size);
670 *dma_handle = virt_to_bus(ret);
671 }
672 return ret;
673}
674
675/**
676 */
677void pci_free_consistent(struct pci_dev *hwdev, long size,
678 void *vaddr, dma_addr_t dma_handle)
679{
680 free_pages((unsigned long)vaddr, __compat_get_order(size));
681}
682
683/**
684 */
685void pci_set_driver_data (struct pci_dev *dev, void *driver_data)
686{
687 if (dev)
688 dev->driver_data = driver_data;
689}
690
691/**
692 */
693void *pci_get_driver_data (struct pci_dev *dev)
694{
695 if (dev)
696 return dev->driver_data;
697 return 0;
698}
699
700/**
701 */
702unsigned long pci_get_dma_mask (struct pci_dev *dev)
703{
704 if (dev)
705 return dev->dma_mask;
706 return 0;
707}
708
709/**
710 */
711int release_resource(struct resource *newres)
712{
713 return 0;
714}
715
716/**
717 */
718int pci_set_latency_time(struct pci_dev *dev, int latency)
719{
720 pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
721 return 0;
722}
723
724/**
725 * pci_save_state - save the PCI configuration space of a device before suspending
726 * @dev: - PCI device that we're dealing with
727 * @buffer: - buffer to hold config space context
728 *
729 * @buffer must be large enough to hold the entire PCI 2.2 config space
730 * (>= 64 bytes).
731 */
732int pci_orig_save_state(struct pci_dev *dev, u32 *buffer)
733{
734 int i;
735 if (buffer) {
736 /* XXX: 100% dword access ok here? */
737 for (i = 0; i < 16; i++)
738 pci_read_config_dword(dev, i * 4,&buffer[i]);
739 }
740 return 0;
741}
742
743/**
744 * pci_restore_state - Restore the saved state of a PCI device
745 * @dev: - PCI device that we're dealing with
746 * @buffer: - saved PCI config space
747 *
748 */
749int pci_orig_restore_state(struct pci_dev *dev, u32 *buffer)
750{
751 int i;
752
753 if (buffer) {
754 for (i = 0; i < 16; i++)
755 pci_write_config_dword(dev,i * 4, buffer[i]);
756 }
757 /*
758 * otherwise, write the context information we know from bootup.
759 * This works around a problem where warm-booting from Windows
760 * combined with a D3(hot)->D0 transition causes PCI config
761 * header data to be forgotten.
762 */
763 else {
764 for (i = 0; i < 6; i ++)
765 pci_write_config_dword(dev,
766 PCI_BASE_ADDRESS_0 + (i * 4),
767 dev->resource[i].start);
768 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
769 }
770 return 0;
771}
772
773struct saved_config_tbl {
774 struct pci_dev *pci;
775 u32 config[16];
776};
777static struct saved_config_tbl saved_tbl[16];
778
779int pci_save_state(struct pci_dev *pci)
780{
781 int i;
782 /* FIXME: mutex needed for race? */
783 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
784 if (! saved_tbl[i].pci) {
785 saved_tbl[i].pci = pci;
786 pci_orig_save_state(pci, saved_tbl[i].config);
787 return 1;
788 }
789 }
790 printk(KERN_DEBUG "snd: no pci config space found!\n");
791 return 0;
792}
793
794int pci_restore_state(struct pci_dev *pci)
795{
796 int i;
797 /* FIXME: mutex needed for race? */
798 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
799 if (saved_tbl[i].pci == pci) {
800 saved_tbl[i].pci = NULL;
801 pci_orig_restore_state(pci, saved_tbl[i].config);
802 return 0;
803 }
804 }
805 printk(KERN_DEBUG "snd: no saved pci config!\n");
806 return 1;
807}
808
809void pci_disable_device(struct pci_dev *dev)
810{
811 u16 pci_command;
812
813 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
814 if (pci_command & PCI_COMMAND_MASTER) {
815 pci_command &= ~PCI_COMMAND_MASTER;
816 pci_write_config_word(dev, PCI_COMMAND, pci_command);
817 }
818}
819
820int pci_request_region(struct pci_dev *pdev, int bar, char *res_name)
821{
822 int flags;
823
824 if (pci_resource_len(pdev, bar) == 0)
825 return 0;
826 flags = pci_get_flags(pdev, bar);
827 if (flags & IORESOURCE_IO) {
828 if (check_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
829 goto err_out;
830 request_region(pci_resource_start(pdev, bar),
831 pci_resource_len(pdev, bar), res_name);
832 }
833 else if (flags & IORESOURCE_MEM) {
834 if (check_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
835 goto err_out;
836 request_mem_region(pci_resource_start(pdev, bar),
837 pci_resource_len(pdev, bar), res_name);
838 }
839
840 return 0;
841
842err_out:
843 printk(KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
844 flags & IORESOURCE_IO ? "I/O" : "mem",
845 bar + 1, /* PCI BAR # */
846 pci_resource_len(pdev, bar), pci_resource_start(pdev, bar),
847 res_name);
848 return -EBUSY;
849}
850
851void pci_release_region(struct pci_dev *pdev, int bar)
852{
853 int flags;
854
855 if (pci_resource_len(pdev, bar) == 0)
856 return;
857 flags = pci_get_flags(pdev, bar);
858 if (flags & IORESOURCE_IO) {
859 release_region(pci_resource_start(pdev, bar),
860 pci_resource_len(pdev, bar));
861 }
862 else if (flags & IORESOURCE_MEM) {
863 release_mem_region(pci_resource_start(pdev, bar),
864 pci_resource_len(pdev, bar));
865 }
866}
867
868int pci_request_regions(struct pci_dev *pdev, char *res_name)
869{
870 int i;
871
872 for (i = 0; i < 6; i++)
873 if (pci_request_region(pdev, i, res_name))
874 goto err;
875 return 0;
876 err:
877 while (--i >= 0)
878 pci_release_region(pdev, i);
879 return -EBUSY;
880}
881
882void pci_release_regions(struct pci_dev *pdev)
883{
884 int i;
885 for (i = 0; i < 6; i++)
886 pci_release_region(pdev, i);
887}
888
889const struct pci_device_id * pci_match_device(const struct pci_device_id *ids, struct pci_dev *dev)
890{
891 u16 subsystem_vendor, subsystem_device;
892
893 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
894 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subsystem_device);
895
896 while (ids->vendor || ids->subvendor || ids->class_mask) {
897 if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
898 (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
899 (ids->subvendor == PCI_ANY_ID || ids->subvendor == subsystem_vendor) &&
900 (ids->subdevice == PCI_ANY_ID || ids->subdevice == subsystem_device) &&
901 !((ids->class ^ dev->_class) & ids->class_mask))
902 return ids;
903 ids++;
904 }
905 return NULL;
906}
907
908/** snd_pci_dev_present
909 * Called by: various sound drivers
910 */
911int snd_pci_dev_present(const struct pci_device_id *ids)
912{
913 while (ids->vendor || ids->subvendor)
914 {
915 if (pci_find_device(ids->vendor, ids->subvendor, NULL)) return 1;
916 ids++;
917 }
918 return 0;
919}
920
921struct pci_driver_mapping {
922 struct pci_dev *dev;
923 struct pci_driver *drv;
924 unsigned long dma_mask;
925 void *driver_data;
926 u32 saved_config[16];
927};
928
929#define PCI_MAX_MAPPINGS 64
930static struct pci_driver_mapping drvmap [PCI_MAX_MAPPINGS] = { { NULL, } , };
931
932
933static struct pci_driver_mapping *get_pci_driver_mapping(struct pci_dev *dev)
934{
935 int i;
936
937 for (i = 0; i < PCI_MAX_MAPPINGS; i++)
938 if (drvmap[i].dev == dev)
939 return &drvmap[i];
940 return NULL;
941}
942
943struct pci_driver *snd_pci_compat_get_pci_driver(struct pci_dev *dev)
944{
945 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
946 if (map)
947 return map->drv;
948 return NULL;
949}
950#if 0
951void * pci_get_drvdata (struct pci_dev *dev)
952{
953 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
954 if (map)
955 return map->driver_data;
956 return NULL;
957}
958
959
960void pci_set_drvdata (struct pci_dev *dev, void *driver_data)
961{
962 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
963 if (map)
964 map->driver_data = driver_data;
965}
966#endif
967
968
969//******************************************************************************
970//******************************************************************************
971OSSRET OSS32_APMResume()
972{
973 int i;
974 struct pci_driver *driver;
975
976 dprintf(("OSS32_APMResume"));
977
978 for(i=0;i<MAX_PCI_DEVICES;i++)
979 {
980 if(pci_devices[i].devfn)
981 {
982 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
983 driver = pci_devices[i].pcidriver;
984 if(driver && driver->resume) {
985 driver->resume(&pci_devices[i]);
986 }
987 }
988 }
989
990 return OSSERR_SUCCESS;
991}
992//******************************************************************************
993//******************************************************************************
994OSSRET OSS32_APMSuspend()
995{
996 int i;
997 struct pci_driver *driver;
998
999 dprintf(("OSS32_APMSuspend 1"));
1000
1001 for(i=0;i<MAX_PCI_DEVICES;i++)
1002 {
1003 if(pci_devices[i].devfn)
1004 {
1005 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
1006 driver = pci_devices[i].pcidriver;
1007 if(driver && driver->suspend) {
1008 driver->suspend(&pci_devices[i], SNDRV_CTL_POWER_D3cold);
1009 }
1010 }
1011 }
1012
1013 dprintf(("OSS32_APMSuspend 2"));
1014 return OSSERR_SUCCESS;
1015}
1016
Note: See TracBrowser for help on using the repository browser.