source: GPL/branches/uniaud32-next/lib32/pci.c@ 644

Last change on this file since 644 was 637, checked in by David Azarewicz, 5 years ago

Fix some easy warnings.

File size: 33.0 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#define CONFIG_PM
28#include "linux.h"
29#include <linux/init.h>
30#include <linux/poll.h>
31#include <linux/dma-mapping.h>
32#include <asm/uaccess.h>
33#include <asm/hardirq.h>
34#include <asm/io.h>
35#include <sound/config.h>
36#include <sound/core.h>
37#include <sound/asound.h>
38
39#define LINUX
40#include <ossidc.h>
41#include <stacktoflat.h>
42#include <dbgos2.h>
43#include <osspci.h>
44
45#define MAX_PCI_BUSSES 256
46#define MAX_PCI_DEVICES 16
47
48struct pci_dev pci_devices[MAX_PCI_DEVICES] = {0};
49//struct pci_bus pci_busses[MAX_PCI_BUSSES] = {0};
50
51extern int nrCardsDetected;
52extern int iAdapterNumber;
53
54
55#define PCI_CONFIG_ENABLE 0x80000000
56#define PCI_CONFIG_ADDRESS 0xCF8
57#define PCI_CONFIG_DATA 0xCFC
58
59//******************************************************************************
60#define CONFIG_CMD(dev, where) \
61 (PCI_CONFIG_ENABLE | (dev->bus->number<<16) | (dev->devfn<<8) | (where & ~3))
62//******************************************************************************
63int pci_read_config_byte(struct pci_dev *dev, int where, u8 *value)
64{
65 outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
66 *value = inb(PCI_CONFIG_DATA + (where&3));
67 return PCIBIOS_SUCCESSFUL;
68}
69//******************************************************************************
70//******************************************************************************
71int pci_read_config_word(struct pci_dev *dev, int where, u16 *value)
72{
73 outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
74 *value = inw(PCI_CONFIG_DATA + (where&2));
75 return PCIBIOS_SUCCESSFUL;
76}
77//******************************************************************************
78//******************************************************************************
79int pci_read_config_dword(struct pci_dev *dev, int where, u32 *value)
80{
81 outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
82 *value = inl(PCI_CONFIG_DATA);
83 return PCIBIOS_SUCCESSFUL;
84}
85//******************************************************************************
86//******************************************************************************
87int pci_write_config_byte(struct pci_dev *dev, int where, u8 value)
88{
89 outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
90 outb(value, PCI_CONFIG_DATA + (where&3));
91 return PCIBIOS_SUCCESSFUL;
92}
93//******************************************************************************
94//******************************************************************************
95int pci_write_config_word(struct pci_dev *dev, int where, u16 value)
96{
97 outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
98 outw(value, PCI_CONFIG_DATA + (where&2));
99 return PCIBIOS_SUCCESSFUL;
100}
101//******************************************************************************
102//******************************************************************************
103int pci_write_config_dword(struct pci_dev *dev, int where, u32 value)
104{
105 outl(CONFIG_CMD(dev,where), PCI_CONFIG_ADDRESS);
106 outl(value, PCI_CONFIG_DATA);
107 return PCIBIOS_SUCCESSFUL;
108}
109//******************************************************************************
110//******************************************************************************
111int pcidev_prepare(struct pci_dev *dev)
112{
113 dprintf(("pcidev_prepare %x not implemented", dev));
114 return 1; //todo: correct return value??
115}
116//******************************************************************************
117//******************************************************************************
118int pcidev_activate(struct pci_dev *dev)
119{
120 dprintf(("pcidev_activate %x not implemented", dev));
121 return 1; //todo: correct return value??
122}
123//******************************************************************************
124//******************************************************************************
125int pcidev_deactivate(struct pci_dev *dev)
126{
127 dprintf(("pcidev_deactivate %x not implemented", dev));
128 return 1; //todo: correct return value??
129}
130
131/**
132 * Called by: pci_find_device, register_driver
133 *
134 * Find the next matching PCI device starting with the device specified by ulLast
135 * Returns: the found device, pcidev filled in. Returns zero if no device found.
136 */
137 static ULONG pci_query_device(const struct pci_device_id *pIdTable, struct pci_dev near *pcidev, ULONG ulLast)
138{
139 int resNo, addr;
140 u32 devNr, busNr, funcNr, detectedId, cfgaddrreg, ulPciAdr, ulTmp1, ulTmp2;
141 u8 headerType;
142
143 busNr = (ulLast >> 8) & 0xff;
144 devNr = PCI_SLOT(ulLast);
145 funcNr = PCI_FUNC(ulLast);
146 if (ulLast) funcNr++;
147
148 cfgaddrreg = inl(PCI_CONFIG_ADDRESS);
149 for ( ; busNr<MAX_PCI_BUSSES; busNr++) //BusNumber<255
150 {
151 for( ; devNr<32; devNr++)
152 {
153 for( ; funcNr<8; funcNr++)
154 {
155 headerType = 0;
156 ulPciAdr = PCI_CONFIG_ENABLE | (busNr<<16) | (devNr<<11) | (funcNr<<8);
157 outl(ulPciAdr, PCI_CONFIG_ADDRESS);
158 detectedId = inl(PCI_CONFIG_DATA);
159
160 if ( detectedId == 0xffffffff )
161 {
162 if ( funcNr == 0 ) break; /* if func 0 isn't there, the others aren't either */
163 continue;
164 }
165
166 outl(ulPciAdr + PCI_CLASS_REVISION, PCI_CONFIG_ADDRESS);
167 ulTmp2 = inl(PCI_CONFIG_DATA) >> 8; /* get class */
168
169 //dprintf(("Found: %x Class=%x need: %x%x class=%x", detectedId, ulTmp2, pIdTable->device&0xffff, pIdTable->vendor, pIdTable->class));
170
171 if ( pIdTable->class )
172 {
173 if ( (ulTmp2 & pIdTable->class_mask) != pIdTable->class ) continue;
174 }
175
176 if ( pIdTable->vendor && (pIdTable->vendor != (detectedId & 0xffff)) ) continue;
177 if ( pIdTable->device && (pIdTable->device != PCI_ANY_ID) && (pIdTable->device != (detectedId >> 16)) ) continue;
178
179 outl(ulPciAdr | (PCI_HEADER_TYPE & ~3), PCI_CONFIG_ADDRESS);
180 headerType = inb(PCI_CONFIG_DATA + (PCI_HEADER_TYPE & 3));
181
182 if ( (headerType & 0x7f) != PCI_HEADER_TYPE_NORMAL ) continue;
183
184 memset((void near *)pcidev, 0, sizeof(struct pci_dev));
185
186 pcidev->class = ulTmp2;
187 pcidev->vendor = detectedId & 0xffff;
188 pcidev->device = detectedId >> 16;
189 //pcidev->bus = &pci_busses[busNr];
190 pcidev->bus = kmalloc(sizeof(struct pci_bus), GFP_KERNEL);
191 if (pcidev->bus == NULL) return 0;
192 memset (pcidev->bus, 0, sizeof(struct pci_bus));
193 pcidev->bus->number = busNr;
194 pcidev->devfn = PCI_DEVFN(devNr, funcNr);
195 pcidev->hdr_type = headerType & 0x7f;
196
197 pcidev->prepare = pcidev_prepare;
198 pcidev->activate = pcidev_activate;
199 pcidev->deactivate = pcidev_deactivate;
200 pcidev->active = 1;
201 pcidev->ro = 0;
202 pcidev->sibling = NULL;
203 pcidev->next = NULL;
204 pcidev->dma_mask = 0xffffffff;
205 pcidev->dev.dma_mask = &pcidev->dma_mask;
206 pcidev->dev.coherent_dma_mask = 0xffffffffull;
207
208 // Subsystem ID
209 pci_read_config_word(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &pcidev->subsystem_vendor);
210 pci_read_config_word(pcidev, PCI_SUBSYSTEM_ID, &pcidev->subsystem_device);
211
212 // revision
213 pci_read_config_byte(pcidev, PCI_REVISION_ID, &pcidev->revision);
214
215 // I/O and MEM
216 resNo = 0;
217 for( addr = PCI_BASE_ADDRESS_0; addr <= PCI_BASE_ADDRESS_5; addr += 4 )
218 {
219 pci_read_config_dword(pcidev, addr, &ulTmp1);
220 if( ulTmp1 != 0 && ulTmp1 != 0xffffffff )
221 {
222 pci_write_config_dword(pcidev, addr, 0xffffffff);
223 pci_read_config_dword(pcidev, addr, &ulTmp2);
224 pci_write_config_dword(pcidev, addr, ulTmp1);
225
226 if( ulTmp1 & PCI_BASE_ADDRESS_SPACE_IO )
227 {
228 pcidev->resource[resNo].flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
229 pcidev->resource[resNo].start = ulTmp1 & PCI_BASE_ADDRESS_IO_MASK;
230 pcidev->resource[resNo].end = pcidev->resource[resNo].start +
231 ~(ulTmp2 & PCI_BASE_ADDRESS_IO_MASK) + 1;
232 }
233 else
234 {
235 pcidev->resource[resNo].flags = IORESOURCE_MEM | IORESOURCE_MEM_WRITEABLE;
236 pcidev->resource[resNo].start = ulTmp1 & PCI_BASE_ADDRESS_MEM_MASK;
237 pcidev->resource[resNo].end = pcidev->resource[resNo].start +
238 ~(ulTmp2 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
239 }
240
241 resNo++;
242 }
243 }
244
245 // IRQ and PIN
246 pci_read_config_dword(pcidev, PCI_INTERRUPT_LINE, &ulTmp1);
247 //rprintf(("pci_query_device: PCI config IRQ=%d", ulTmp1&0xff));
248 if( (u8)ulTmp1 && (u8)ulTmp1 != 0xff )
249 {
250 pcidev->irq_resource[0].flags = IORESOURCE_IRQ;
251 pcidev->irq_resource[0].start = pcidev->irq_resource[0].end = ulTmp1 & 0xffff;
252 pcidev->irq = (u8)ulTmp1; // This is the interrupt used for init time processing
253 pcidev->irq_pin = ulTmp1>>8;
254 }
255
256 return ((busNr << 8) | PCI_DEVFN(devNr, funcNr));
257 } /* for funcNr */
258 funcNr = 0;
259 } /* for devNr */
260 devNr = 0;
261 }
262 outl(cfgaddrreg, PCI_CONFIG_ADDRESS);
263 return 0;
264}
265
266/**
267 * Called by: snd_pci_dev_present, various sound drivers
268 *
269 * Find the requested device
270 */
271struct pci_dev *pci_find_device (unsigned int vendor, unsigned int device, struct pci_dev *from)
272{
273 int i;
274 struct pci_device_id id_table;
275
276 for(i=0; i<MAX_PCI_DEVICES; i++)
277 {
278 if ( pci_devices[i].devfn && (pci_devices[i].vendor == vendor) && (pci_devices[i].device == device) ) return &pci_devices[i];
279 }
280
281 for(i=0; i<MAX_PCI_DEVICES; i++)
282 {
283 if(pci_devices[i].devfn == 0)
284 {
285 memset(&id_table, 0, sizeof(id_table));
286 id_table.vendor = vendor;
287 id_table.device = device;
288 if( pci_query_device(&id_table, (struct pci_dev near *)&pci_devices[i], 0) ) return &pci_devices[i];
289 else break;
290 }
291 }
292
293 return NULL;
294}
295
296/** __request_region
297 */
298struct resource * __request_region(struct resource *a, unsigned long start, unsigned long n, const char *name)
299{
300 struct resource *resource;
301
302 if(a->flags & IORESOURCE_MEM) {
303 if(RMRequestMem(/*hResMgr,*/ start, n) == FALSE) {
304 printk("RMRequestIO failed for io %x, length %x\n", start, n);
305 return NULL;
306 }
307 }
308 else if(a->flags & IORESOURCE_IO) {
309 if(RMRequestIO(/*hResMgr,*/ start, n) == FALSE) {
310 printk("RMRequestIO failed for io %x, length %x\n", start, n);
311 return NULL;
312 }
313 }
314
315 resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
316 if (resource == NULL)
317 return NULL;
318 resource->name = name;
319 resource->start = start;
320 resource->end = start + n; // - 1;
321 resource->flags = a->flags;
322 resource->parent =
323 resource->child = NULL;
324
325 // insert in list
326 resource->sibling = a->sibling;
327 a->sibling = resource;
328
329 return resource;
330}
331
332/**
333 */
334void __release_region(struct resource *a, unsigned long start, unsigned long n)
335{
336 struct resource *resource;
337 struct resource **ppres = &a->sibling;
338 unsigned long end = start + n; // - 1;
339
340 while( *ppres )
341 {
342 resource = *ppres;
343
344 if( resource->start == start && resource->end == end )
345 {
346 // remove from list
347 *ppres = resource->sibling;
348 kfree(resource);
349 return;
350 }
351
352 ppres = &resource->sibling;
353 }
354}
355
356/**
357 */
358int pci_get_flags (struct pci_dev *dev, int n_base)
359{
360 if(n_base >= DEVICE_COUNT_RESOURCE || !dev->resource[n_base].flags) {
361 DebugInt3();
362 return 0;
363 }
364 return dev->resource[n_base].flags;
365}
366
367/**
368 */
369int pcibios_present(void)
370{
371 printk("pcibios_present -> pretend BIOS present\n");
372 return 1;
373}
374
375/**
376 */
377struct pci_dev *pci_find_slot (unsigned int bus, unsigned int devfn)
378{
379 printk("pci_find_slot %d %x not implemented!!\n", bus, devfn);
380 DebugInt3();
381 return NULL;
382}
383
384/**
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/**
393 */
394int pci_find_capability(struct pci_dev *dev, int cap)
395{
396 u16 status;
397 u8 pos, id;
398 int ttl = 48;
399
400 pci_read_config_word(dev, PCI_STATUS, &status);
401 if (!(status & PCI_STATUS_CAP_LIST))
402 return 0;
403 pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &pos);
404 while (ttl-- && pos >= 0x40) {
405 pos &= ~3;
406 pci_read_config_byte(dev, pos + PCI_CAP_LIST_ID, &id);
407 if (id == 0xff)
408 break;
409 if (id == cap)
410 return pos;
411 pci_read_config_byte(dev, pos + PCI_CAP_LIST_NEXT, &pos);
412 }
413 return 0;
414}
415
416/**
417 * Set power management state of a device. For transitions from state D3
418 * it isn't as straightforward as one could assume since many devices forget
419 * their configuration space during wakeup. Returns old power state.
420 */
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 */
461int pci_enable_device(struct pci_dev *dev)
462{
463 u16 pci_command;
464
465 dprintf(("pci_enable_device %x\n", dev));
466
467 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
468 pci_write_config_word(dev, PCI_COMMAND, pci_command | (PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
469 pci_set_power_state(dev, 0);
470 return 0;
471}
472
473/** pci_register_driver
474 *
475 * probes and registers a sound driver with RM.
476 *
477 * Returns: number of cards found.
478 */
479int pci_register_driver(struct pci_driver *driver)
480{
481 int iNumCards, iTmp;
482 ULONG ulLast;
483 struct pci_dev *pcidev;
484 struct pci_device_id IdTable;
485 USHORT usVendor, usDevice;
486 int iAdapter = 0;
487
488 if (!driver->probe) return 0;
489
490 iNumCards = 0;
491
492 /* find an empty slot */
493 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
494 {
495 if (pci_devices[iTmp].devfn == 0) break;
496 }
497 if (iTmp >= MAX_PCI_DEVICES) return 0;
498 pcidev = &pci_devices[iTmp];
499
500 memset(&IdTable, 0, sizeof(IdTable));
501 IdTable.class = 0x000400 << 8; /* Any multimedia device */
502 IdTable.class_mask = 0xffff00 << 8;
503 ulLast = 0;
504 while( (ulLast = pci_query_device(&IdTable, pcidev, ulLast)) != 0 )
505 {
506 int iTableIx;
507
508 rprintf((__func__": query_device found %x %x:%x class=%x checking %s",
509 ulLast, pcidev->vendor, pcidev->device, pcidev->class, driver->name));
510
511 usVendor = 0;
512 usDevice = 0;
513
514 for( iTableIx = 0; driver->id_table[iTableIx].vendor; iTableIx++)
515 {
516 struct pci_device_id const *pDriverId = &driver->id_table[iTableIx];
517
518 if ( (pDriverId->class) && ((pcidev->class & pDriverId->class_mask) != pDriverId->class) ) continue;
519 if (pDriverId->vendor != pcidev->vendor) continue;
520 if ( (pDriverId->device != PCI_ANY_ID) && (pDriverId->device != pcidev->device) ) continue;
521
522 /* skip a duplicate device that could be matched by both and exact match and a class match */
523 if (usVendor == pcidev->vendor && usDevice == pcidev->device) continue;
524 usVendor = pcidev->vendor;
525 usDevice = pcidev->device;
526
527 rprintf((__func__": matched %d %x:%x/%x with %x:%x/%x %x (%s)", iTableIx,
528 pcidev->vendor, pcidev->device, pcidev->class,
529 pDriverId->vendor, pDriverId->device, pDriverId->class, pDriverId->class_mask, driver->name));
530
531 if ((iAdapterNumber >= 0) && (iAdapter < iAdapterNumber))
532 {
533 rprintf((__func__": AdapterNumber=%x skipping Adapter=%x", iAdapterNumber, iAdapter));
534 iAdapter++;
535 continue;
536 }
537
538 RMInit();
539 if (driver->probe(pcidev, pDriverId) == 0)
540 {
541 pcidev->pcidriver = (void *)driver;
542 pcidev->current_state = 4;
543
544 // create adapter
545 RMDone((pcidev->device << 16) | pcidev->vendor, &pcidev->hAdapter, &pcidev->hDevice);
546 iNumCards++;
547 pcidev = NULL; /* we need a new slot */
548 break;
549 }
550 RMDone(0, 0, 0);
551 } /* for id_table loop */
552
553 if (pcidev)
554 {
555 kfree(pcidev->bus);
556 pcidev->devfn = 0;
557 }
558 else
559 {
560 if (iAdapterNumber >= 0) break;
561 /* find another empty slot */
562 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
563 {
564 if (pci_devices[iTmp].devfn == 0) break;
565 }
566 if (iTmp >= MAX_PCI_DEVICES) break;
567 pcidev = &pci_devices[iTmp];
568 }
569 } /* pci_query_device loop */
570
571 return iNumCards;
572}
573
574/**
575 */
576int pci_module_init(struct pci_driver *drv)
577{
578 int res = pci_register_driver(drv);
579 if (res == 0) return -ENODEV;
580 return res;
581}
582
583/**
584 */
585int pci_unregister_driver(struct pci_driver *driver)
586{
587 struct pci_dev *pcidev;
588 int i, j;
589
590 for (i=0; driver->id_table[i].vendor; i++) {
591 for(j=0; j<MAX_PCI_DEVICES; j++) {
592 pcidev = &pci_devices[j];
593 if (pcidev->devfn == 0) continue;
594 if (pcidev->vendor != driver->id_table[i].vendor) continue;
595 if ( (driver->id_table[i].device != PCI_ANY_ID) && (pcidev->device != driver->id_table[i].device) ) continue;
596 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));
597 if (driver->remove) driver->remove(pcidev);
598 kfree(pcidev->bus);
599 pcidev->devfn = 0;
600 }
601 }
602 return 0;
603}
604
605/**
606 */
607void pci_set_master(struct pci_dev *dev)
608{
609 u16 cmd;
610
611 pci_read_config_word(dev, PCI_COMMAND, &cmd);
612 if (! (cmd & PCI_COMMAND_MASTER)) {
613 dprintf(("pci_set_master %x", dev));
614 cmd |= PCI_COMMAND_MASTER;
615 pci_write_config_word(dev, PCI_COMMAND, cmd);
616 }
617 return;
618}
619
620/**
621 * Register a device with power management
622 */
623struct pm_dev *pm_register(pm_dev_t type, unsigned long id, pm_callback callback)
624{
625 dprintf(("pm_register STUB"));
626 DebugInt3();
627 return NULL;
628}
629
630/**
631 * Unregister a device with power management
632 */
633void pm_unregister(struct pm_dev *dev)
634{
635 dprintf(("pm_unregister STUB"));
636}
637
638/**
639 */
640int __compat_get_order(unsigned long size)
641{
642 int order;
643
644 size = (size-1) >> (PAGE_SHIFT-1);
645 order = -1;
646 do {
647 size >>= 1;
648 order++;
649 } while (size);
650 return order;
651}
652
653/**
654 */
655void *pci_alloc_consistent(struct pci_dev *hwdev,
656 long size, dma_addr_t *dma_handle)
657{
658 void *ret = NULL;
659 int gfp = GFP_ATOMIC;
660 int order;
661 dprintf(("pci_alloc_consistent %d mask %x", size, (hwdev) ? hwdev->dma_mask : 0));
662 if (hwdev == NULL || hwdev->dma_mask != 0xffffffff) {
663 //try not to exhaust low memory (< 16mb) so allocate from the high region first
664 //if that doesn't satisfy the dma mask requirement, then get it from the low
665 //region anyway
666 if(hwdev->dma_mask > 0x00ffffff) {
667 order = __compat_get_order(size);
668 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
669 *dma_handle = virt_to_bus(ret);
670 if(*dma_handle > hwdev->dma_mask) {
671 free_pages((unsigned long)ret, __compat_get_order(size));
672 //be sure and allocate below 16 mb
673 gfp |= GFP_DMA;
674 ret = NULL;
675 }
676 }
677 else { //must always allocate below 16 mb
678 gfp |= GFP_DMA;
679 }
680 }
681 if(ret == NULL) {
682 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
683 }
684 if (ret != NULL) {
685 memset(ret, 0, size);
686 *dma_handle = virt_to_bus(ret);
687 }
688 return ret;
689}
690
691#if 0
692void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
693 dma_addr_t *dma_handle)
694 {
695 return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC);
696 }
697#endif
698#if 0
699void *dma_alloc_coherent(struct device *dev, size_t size,
700 dma_addr_t *dma_handle, gfp_t gfp)
701{
702 void *ret = NULL;
703 int order;
704
705 dprintf(("dma_alloc_coherent %d mask %x", size, (dev) ? dev->dma_mask : 0));
706 if (dev == NULL || *dev->dma_mask != 0xffffffff) {
707 dprintf(("dma_alloc_coherent"));
708 //try not to exhaust low memory (< 16mb) so allocate from the high region first
709 //if that doesn't satisfy the dma mask requirement, then get it from the low
710 //region anyway
711 if(*dev->dma_mask > 0x00ffffff) {
712 dprintf(("dma_alloc_coherent2"));
713 order = __compat_get_order(size);
714 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
715 *dma_handle = virt_to_bus(ret);
716 if(*dma_handle > *dev->dma_mask) {
717 dprintf(("dma_alloc_coherent3"));
718 free_pages((unsigned long)ret, __compat_get_order(size));
719 //be sure and allocate below 16 mb
720 gfp |= GFP_DMA;
721 ret = NULL;
722 }
723 dprintf(("dma_alloc_coherent3a"));
724 }
725 else { //must always allocate below 16 mb
726 dprintf(("dma_alloc_coherent4"));
727 gfp |= GFP_DMA;
728 }
729 }
730 if(ret == NULL) {
731 dprintf(("dma_alloc_coherent5"));
732 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
733 }
734
735 if (ret != NULL) {
736 memset(ret, 0, size);
737 *dma_handle = virt_to_bus(ret);
738 }
739 return ret;
740
741}
742#endif
743
744int dma_supported(struct device *dev, u64 mask)
745{
746 return 1;
747}
748
749int dma_set_coherent_mask(struct device *dev, u64 mask)
750{
751 /*
752 * Truncate the mask to the actually supported dma_addr_t width to
753 * avoid generating unsupportable addresses.
754 */
755 mask = (dma_addr_t)mask;
756
757 if (!dma_supported(dev, mask))
758 return -EIO;
759
760 dev->coherent_dma_mask = mask;
761 return 0;
762}
763
764int dma_set_mask(struct device *dev, u64 mask)
765{
766 /*
767 * Truncate the mask to the actually supported dma_addr_t width to
768 * avoid generating unsupportable addresses.
769 */
770 mask = (dma_addr_t)mask;
771
772 if (!dev->dma_mask || !dma_supported(dev, mask))
773 return -EIO;
774
775 *dev->dma_mask = mask;
776 return 0;
777}
778
779/**
780 */
781void pci_free_consistent(struct pci_dev *hwdev, long size,
782 void *vaddr, dma_addr_t dma_handle)
783{
784 free_pages((unsigned long)vaddr, __compat_get_order(size));
785}
786
787/**
788 */
789void pci_set_driver_data (struct pci_dev *dev, void *driver_data)
790{
791 if (dev)
792 dev->driver_data = driver_data;
793}
794
795/**
796 */
797void *pci_get_driver_data (struct pci_dev *dev)
798{
799 if (dev)
800 return dev->driver_data;
801 return 0;
802}
803
804/**
805 */
806unsigned long pci_get_dma_mask (struct pci_dev *dev)
807{
808 if (dev)
809 return dev->dma_mask;
810 return 0;
811}
812
813/**
814 */
815int release_resource(struct resource *newres)
816{
817 return 0;
818}
819
820/**
821 */
822int pci_set_latency_time(struct pci_dev *dev, int latency)
823{
824 pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
825 return 0;
826}
827
828/**
829 * pci_save_state - save the PCI configuration space of a device before suspending
830 * @dev: - PCI device that we're dealing with
831 * @buffer: - buffer to hold config space context
832 *
833 * @buffer must be large enough to hold the entire PCI 2.2 config space
834 * (>= 64 bytes).
835 */
836int pci_orig_save_state(struct pci_dev *dev, u32 *buffer)
837{
838 int i;
839 if (buffer) {
840 /* XXX: 100% dword access ok here? */
841 for (i = 0; i < 16; i++)
842 pci_read_config_dword(dev, i * 4,&buffer[i]);
843 }
844 return 0;
845}
846
847/**
848 * pci_restore_state - Restore the saved state of a PCI device
849 * @dev: - PCI device that we're dealing with
850 * @buffer: - saved PCI config space
851 *
852 */
853int pci_orig_restore_state(struct pci_dev *dev, u32 *buffer)
854{
855 int i;
856
857 if (buffer) {
858 for (i = 0; i < 16; i++)
859 pci_write_config_dword(dev,i * 4, buffer[i]);
860 }
861 /*
862 * otherwise, write the context information we know from bootup.
863 * This works around a problem where warm-booting from Windows
864 * combined with a D3(hot)->D0 transition causes PCI config
865 * header data to be forgotten.
866 */
867 else {
868 for (i = 0; i < 6; i ++)
869 pci_write_config_dword(dev,
870 PCI_BASE_ADDRESS_0 + (i * 4),
871 dev->resource[i].start);
872 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
873 }
874 return 0;
875}
876
877struct saved_config_tbl {
878 struct pci_dev *pci;
879 u32 config[16];
880};
881static struct saved_config_tbl saved_tbl[16];
882
883int pci_save_state(struct pci_dev *pci)
884{
885 int i;
886 /* FIXME: mutex needed for race? */
887 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
888 if (! saved_tbl[i].pci) {
889 saved_tbl[i].pci = pci;
890 pci_orig_save_state(pci, saved_tbl[i].config);
891 return 1;
892 }
893 }
894 printk(KERN_DEBUG "snd: no pci config space found!\n");
895 return 0;
896}
897
898int pci_restore_state(struct pci_dev *pci)
899{
900 int i;
901 /* FIXME: mutex needed for race? */
902 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
903 if (saved_tbl[i].pci == pci) {
904 saved_tbl[i].pci = NULL;
905 pci_orig_restore_state(pci, saved_tbl[i].config);
906 return 0;
907 }
908 }
909 printk(KERN_DEBUG "snd: no saved pci config!\n");
910 return 1;
911}
912
913void pci_disable_device(struct pci_dev *dev)
914{
915 u16 pci_command;
916
917 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
918 if (pci_command & PCI_COMMAND_MASTER) {
919 pci_command &= ~PCI_COMMAND_MASTER;
920 pci_write_config_word(dev, PCI_COMMAND, pci_command);
921 }
922}
923
924int pci_request_region(struct pci_dev *pdev, int bar, char *res_name)
925{
926 int flags;
927
928 if (pci_resource_len(pdev, bar) == 0)
929 return 0;
930 flags = pci_get_flags(pdev, bar);
931 if (flags & IORESOURCE_IO) {
932 if (check_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
933 goto err_out;
934 request_region(pci_resource_start(pdev, bar),
935 pci_resource_len(pdev, bar), res_name);
936 }
937 else if (flags & IORESOURCE_MEM) {
938 if (check_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
939 goto err_out;
940 request_mem_region(pci_resource_start(pdev, bar),
941 pci_resource_len(pdev, bar), res_name);
942 }
943
944 return 0;
945
946err_out:
947 printk(KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
948 flags & IORESOURCE_IO ? "I/O" : "mem",
949 bar + 1, /* PCI BAR # */
950 pci_resource_len(pdev, bar), pci_resource_start(pdev, bar),
951 res_name);
952 return -EBUSY;
953}
954
955void pci_release_region(struct pci_dev *pdev, int bar)
956{
957 int flags;
958
959 if (pci_resource_len(pdev, bar) == 0)
960 return;
961 flags = pci_get_flags(pdev, bar);
962 if (flags & IORESOURCE_IO) {
963 release_region(pci_resource_start(pdev, bar),
964 pci_resource_len(pdev, bar));
965 }
966 else if (flags & IORESOURCE_MEM) {
967 release_mem_region(pci_resource_start(pdev, bar),
968 pci_resource_len(pdev, bar));
969 }
970}
971
972int pci_request_regions(struct pci_dev *pdev, char *res_name)
973{
974 int i;
975
976 for (i = 0; i < 6; i++)
977 if (pci_request_region(pdev, i, res_name))
978 goto err;
979 return 0;
980 err:
981 while (--i >= 0)
982 pci_release_region(pdev, i);
983 return -EBUSY;
984}
985
986void pci_release_regions(struct pci_dev *pdev)
987{
988 int i;
989 for (i = 0; i < 6; i++)
990 pci_release_region(pdev, i);
991}
992
993const struct pci_device_id * pci_match_id(const struct pci_device_id *ids, struct pci_dev *dev)
994{
995 u16 subsystem_vendor, subsystem_device;
996
997 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
998 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subsystem_device);
999
1000 while (ids->vendor || ids->subvendor || ids->class_mask) {
1001 if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
1002 (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
1003 (ids->subvendor == PCI_ANY_ID || ids->subvendor == subsystem_vendor) &&
1004 (ids->subdevice == PCI_ANY_ID || ids->subdevice == subsystem_device) &&
1005 !((ids->class ^ dev->class) & ids->class_mask))
1006 return ids;
1007 ids++;
1008 }
1009 return NULL;
1010}
1011
1012/** snd_pci_dev_present
1013 * Called by: various sound drivers
1014 */
1015int snd_pci_dev_present(const struct pci_device_id *ids)
1016{
1017 while (ids->vendor || ids->subvendor)
1018 {
1019 if (pci_find_device(ids->vendor, ids->subvendor, NULL)) return 1;
1020 ids++;
1021 }
1022 return 0;
1023}
1024
1025struct pci_driver_mapping {
1026 struct pci_dev *dev;
1027 struct pci_driver *drv;
1028 unsigned long dma_mask;
1029 void *driver_data;
1030 u32 saved_config[16];
1031};
1032
1033#define PCI_MAX_MAPPINGS 64
1034static struct pci_driver_mapping drvmap [PCI_MAX_MAPPINGS] = { { NULL, } , };
1035
1036
1037static struct pci_driver_mapping *get_pci_driver_mapping(struct pci_dev *dev)
1038{
1039 int i;
1040
1041 for (i = 0; i < PCI_MAX_MAPPINGS; i++)
1042 if (drvmap[i].dev == dev)
1043 return &drvmap[i];
1044 return NULL;
1045}
1046
1047struct pci_driver *snd_pci_compat_get_pci_driver(struct pci_dev *dev)
1048{
1049 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1050 if (map)
1051 return map->drv;
1052 return NULL;
1053}
1054#if 0
1055void * pci_get_drvdata (struct pci_dev *dev)
1056{
1057 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1058 if (map)
1059 return map->driver_data;
1060 return NULL;
1061}
1062
1063
1064void pci_set_drvdata (struct pci_dev *dev, void *driver_data)
1065{
1066 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1067 if (map)
1068 map->driver_data = driver_data;
1069}
1070#endif
1071
1072
1073//******************************************************************************
1074//******************************************************************************
1075OSSRET OSS32_APMResume()
1076{
1077 int i;
1078 struct pci_driver *driver;
1079
1080 dprintf(("OSS32_APMResume"));
1081
1082 for(i=0;i<MAX_PCI_DEVICES;i++)
1083 {
1084 if(pci_devices[i].devfn)
1085 {
1086 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
1087 driver = pci_devices[i].pcidriver;
1088 if(driver && driver->resume) {
1089 driver->resume(&pci_devices[i]);
1090 }
1091 }
1092 }
1093
1094 return OSSERR_SUCCESS;
1095}
1096//******************************************************************************
1097//******************************************************************************
1098OSSRET OSS32_APMSuspend()
1099{
1100 int i;
1101 struct pci_driver *driver;
1102
1103 dprintf(("OSS32_APMSuspend 1"));
1104
1105 for(i=0;i<MAX_PCI_DEVICES;i++)
1106 {
1107 if(pci_devices[i].devfn)
1108 {
1109 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
1110 driver = pci_devices[i].pcidriver;
1111 if(driver && driver->suspend) {
1112 driver->suspend(&pci_devices[i], SNDRV_CTL_POWER_D3cold);
1113 }
1114 }
1115 }
1116
1117 dprintf(("OSS32_APMSuspend 2"));
1118 return OSSERR_SUCCESS;
1119}
1120
1121#ifdef USE_MSI
1122extern int __syscall UniMsiAlloc(USHORT usBusDevFunc, ULONG *pulCount, UCHAR *pucIrq);
1123int snd_pci_enable_msi(struct pci_dev *dev)
1124{
1125 ULONG p;
1126 UCHAR irq;
1127
1128 if (dev->irq_pin)
1129 {
1130 p = 1; /* int count */
1131 if (UniMsiAlloc((dev->bus->number<<8) | dev->devfn, &p, &irq)) return -1;
1132 /* we have an msi interrupt */
1133 dev->irq = irq;
1134 dev->irq_pin = 0;
1135 }
1136 return 0;
1137}
1138#else
1139int snd_pci_enable_msi(struct pci_dev *dev)
1140{
1141 return -1;
1142}
1143#endif
1144
1145/**
1146 * pci_status_get_and_clear_errors - return and clear error bits in PCI_STATUS
1147 * @pdev: the PCI device
1148 *
1149 * Returns error bits set in PCI_STATUS and clears them.
1150 */
1151int pci_status_get_and_clear_errors(struct pci_dev *pdev)
1152{
1153 u16 status;
1154 int ret;
1155
1156 ret = pci_read_config_word(pdev, PCI_STATUS, &status);
1157 if (ret != PCIBIOS_SUCCESSFUL)
1158 return -EIO;
1159
1160 status &= PCI_STATUS_ERROR_BITS;
1161 if (status)
1162 pci_write_config_word(pdev, PCI_STATUS, status);
1163
1164 return status;
1165}
1166
Note: See TracBrowser for help on using the repository browser.