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

Last change on this file since 713 was 713, checked in by Paul Smedley, 3 years ago

Fixes

File size: 33.3 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 mem %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 * pcim_enable_device - Managed pci_enable_device()
458 * @pdev: PCI device to be initialized
459 *
460 * Managed pci_enable_device().
461 */
462int pcim_enable_device(struct pci_dev *pdev)
463{
464 int rc;
465
466 rc = pci_enable_device(pdev);
467 return rc;
468}
469
470/**
471 * Initialize device before it's used by a driver. Ask low-level code
472 * to enable I/O and memory. Wake up the device if it was suspended.
473 * Beware, this function can fail.
474 */
475int pci_enable_device(struct pci_dev *dev)
476{
477 u16 pci_command;
478
479 dprintf(("pci_enable_device %x\n", dev));
480
481 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
482 pci_write_config_word(dev, PCI_COMMAND, pci_command | (PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
483 pci_set_power_state(dev, 0);
484 return 0;
485}
486
487/** pci_register_driver
488 *
489 * probes and registers a sound driver with RM.
490 *
491 * Returns: number of cards found.
492 */
493int pci_register_driver(struct pci_driver *driver)
494{
495 int iNumCards, iTmp;
496 ULONG ulLast;
497 struct pci_dev *pcidev;
498 struct pci_device_id IdTable;
499 USHORT usVendor, usDevice;
500 int iAdapter = 0;
501
502 if (!driver->probe) return 0;
503
504 iNumCards = 0;
505
506 /* find an empty slot */
507 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
508 {
509 if (pci_devices[iTmp].devfn == 0) break;
510 }
511 if (iTmp >= MAX_PCI_DEVICES) return 0;
512 pcidev = &pci_devices[iTmp];
513
514 memset(&IdTable, 0, sizeof(IdTable));
515 IdTable.class = 0x000400 << 8; /* Any multimedia device */
516 IdTable.class_mask = 0xffff00 << 8;
517 ulLast = 0;
518 while( (ulLast = pci_query_device(&IdTable, pcidev, ulLast)) != 0 )
519 {
520 int iTableIx;
521
522 rprintf((__func__": query_device found %x %04x:%04x class=%x checking %s",
523 ulLast, pcidev->vendor, pcidev->device, pcidev->class, driver->name));
524
525 usVendor = 0;
526 usDevice = 0;
527
528 for( iTableIx = 0; driver->id_table[iTableIx].vendor; iTableIx++)
529 {
530 struct pci_device_id const *pDriverId = &driver->id_table[iTableIx];
531
532 if ( (pDriverId->class) && ((pcidev->class & pDriverId->class_mask) != pDriverId->class) ) continue;
533 if (pDriverId->vendor != pcidev->vendor) continue;
534 if ( (pDriverId->device != PCI_ANY_ID) && (pDriverId->device != pcidev->device) ) continue;
535
536 /* skip a duplicate device that could be matched by both and exact match and a class match */
537 if (usVendor == pcidev->vendor && usDevice == pcidev->device) continue;
538 usVendor = pcidev->vendor;
539 usDevice = pcidev->device;
540
541 rprintf((__func__": matched %d %x:%x/%x with %x:%x/%x %x (%s)", iTableIx,
542 pcidev->vendor, pcidev->device, pcidev->class,
543 pDriverId->vendor, pDriverId->device, pDriverId->class, pDriverId->class_mask, driver->name));
544
545 if ((iAdapterNumber >= 0) && (iAdapter < iAdapterNumber))
546 {
547 rprintf((__func__": AdapterNumber=%x skipping Adapter=%x", iAdapterNumber, iAdapter));
548 iAdapter++;
549 continue;
550 }
551
552 if (driver->probe(pcidev, pDriverId) == 0)
553 {
554 pcidev->pcidriver = (void *)driver;
555 pcidev->current_state = 4;
556
557 // create adapter
558 RMCreateAdapterU32((pcidev->device << 16) | pcidev->vendor, &pcidev->hAdapter, ulLast, iNumCards);
559
560 iNumCards++;
561 pcidev = NULL; /* we need a new slot */
562 break;
563 }
564 // release resources which were possibly allocated during probe()
565 RMDeallocRes();
566 } /* for id_table loop */
567
568 if (pcidev)
569 {
570 kfree(pcidev->bus);
571 pcidev->devfn = 0;
572 }
573 else
574 {
575 if (iAdapterNumber >= 0) break;
576 /* find another empty slot */
577 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
578 {
579 if (pci_devices[iTmp].devfn == 0) break;
580 }
581 if (iTmp >= MAX_PCI_DEVICES) break;
582 pcidev = &pci_devices[iTmp];
583 }
584 } /* pci_query_device loop */
585
586 return iNumCards;
587}
588
589/**
590 */
591int pci_module_init(struct pci_driver *drv)
592{
593 int res = pci_register_driver(drv);
594 if (res == 0) return -ENODEV;
595 return res;
596}
597
598/**
599 */
600int pci_unregister_driver(struct pci_driver *driver)
601{
602 struct pci_dev *pcidev;
603 int i, j;
604
605 for (i=0; driver->id_table[i].vendor; i++) {
606 for(j=0; j<MAX_PCI_DEVICES; j++) {
607 pcidev = &pci_devices[j];
608 if (pcidev->devfn == 0) continue;
609 if (pcidev->vendor != driver->id_table[i].vendor) continue;
610 if ( (driver->id_table[i].device != PCI_ANY_ID) && (pcidev->device != driver->id_table[i].device) ) continue;
611 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));
612 if (driver->remove) driver->remove(pcidev);
613 kfree(pcidev->bus);
614 pcidev->devfn = 0;
615 }
616 }
617 return 0;
618}
619
620/**
621 */
622void pci_set_master(struct pci_dev *dev)
623{
624 u16 cmd;
625
626 pci_read_config_word(dev, PCI_COMMAND, &cmd);
627 if (! (cmd & PCI_COMMAND_MASTER)) {
628 dprintf(("pci_set_master %x", dev));
629 cmd |= PCI_COMMAND_MASTER;
630 pci_write_config_word(dev, PCI_COMMAND, cmd);
631 }
632 return;
633}
634
635/**
636 * Register a device with power management
637 */
638struct pm_dev *pm_register(pm_dev_t type, unsigned long id, pm_callback callback)
639{
640 dprintf(("pm_register STUB"));
641 DebugInt3();
642 return NULL;
643}
644
645/**
646 * Unregister a device with power management
647 */
648void pm_unregister(struct pm_dev *dev)
649{
650 dprintf(("pm_unregister STUB"));
651}
652
653/**
654 */
655int __compat_get_order(unsigned long size)
656{
657 int order;
658
659 size = (size-1) >> (PAGE_SHIFT-1);
660 order = -1;
661 do {
662 size >>= 1;
663 order++;
664 } while (size);
665 return order;
666}
667
668/**
669 */
670void *pci_alloc_consistent(struct pci_dev *hwdev,
671 long size, dma_addr_t *dma_handle)
672{
673 void *ret = NULL;
674 int gfp = GFP_ATOMIC;
675 int order;
676 dprintf(("pci_alloc_consistent %d mask %x", size, (hwdev) ? hwdev->dma_mask : 0));
677 if (hwdev == NULL || hwdev->dma_mask != 0xffffffff) {
678 //try not to exhaust low memory (< 16mb) so allocate from the high region first
679 //if that doesn't satisfy the dma mask requirement, then get it from the low
680 //region anyway
681 if(hwdev->dma_mask > 0x00ffffff) {
682 order = __compat_get_order(size);
683 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
684 *dma_handle = virt_to_bus(ret);
685 if(*dma_handle > hwdev->dma_mask) {
686 free_pages((unsigned long)ret, __compat_get_order(size));
687 //be sure and allocate below 16 mb
688 gfp |= GFP_DMA;
689 ret = NULL;
690 }
691 }
692 else { //must always allocate below 16 mb
693 gfp |= GFP_DMA;
694 }
695 }
696 if(ret == NULL) {
697 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
698 }
699 if (ret != NULL) {
700 memset(ret, 0, size);
701 *dma_handle = virt_to_bus(ret);
702 }
703 return ret;
704}
705
706#if 0
707void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
708 dma_addr_t *dma_handle)
709 {
710 return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC);
711 }
712#endif
713#if 0
714void *dma_alloc_coherent(struct device *dev, size_t size,
715 dma_addr_t *dma_handle, gfp_t gfp)
716{
717 void *ret = NULL;
718 int order;
719
720 dprintf(("dma_alloc_coherent %d mask %x", size, (dev) ? dev->dma_mask : 0));
721 if (dev == NULL || *dev->dma_mask != 0xffffffff) {
722 dprintf(("dma_alloc_coherent"));
723 //try not to exhaust low memory (< 16mb) so allocate from the high region first
724 //if that doesn't satisfy the dma mask requirement, then get it from the low
725 //region anyway
726 if(*dev->dma_mask > 0x00ffffff) {
727 dprintf(("dma_alloc_coherent2"));
728 order = __compat_get_order(size);
729 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
730 *dma_handle = virt_to_bus(ret);
731 if(*dma_handle > *dev->dma_mask) {
732 dprintf(("dma_alloc_coherent3"));
733 free_pages((unsigned long)ret, __compat_get_order(size));
734 //be sure and allocate below 16 mb
735 gfp |= GFP_DMA;
736 ret = NULL;
737 }
738 dprintf(("dma_alloc_coherent3a"));
739 }
740 else { //must always allocate below 16 mb
741 dprintf(("dma_alloc_coherent4"));
742 gfp |= GFP_DMA;
743 }
744 }
745 if(ret == NULL) {
746 dprintf(("dma_alloc_coherent5"));
747 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
748 }
749
750 if (ret != NULL) {
751 memset(ret, 0, size);
752 *dma_handle = virt_to_bus(ret);
753 }
754 return ret;
755
756}
757#endif
758
759int dma_supported(struct device *dev, u64 mask)
760{
761 return 1;
762}
763
764int dma_set_coherent_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 (!dma_supported(dev, mask))
773 return -EIO;
774
775 dev->coherent_dma_mask = mask;
776 return 0;
777}
778
779int dma_set_mask(struct device *dev, u64 mask)
780{
781 /*
782 * Truncate the mask to the actually supported dma_addr_t width to
783 * avoid generating unsupportable addresses.
784 */
785 mask = (dma_addr_t)mask;
786
787 if (!dev->dma_mask || !dma_supported(dev, mask))
788 return -EIO;
789
790 *dev->dma_mask = mask;
791 return 0;
792}
793
794/**
795 */
796void pci_free_consistent(struct pci_dev *hwdev, long size,
797 void *vaddr, dma_addr_t dma_handle)
798{
799 free_pages((unsigned long)vaddr, __compat_get_order(size));
800}
801
802/**
803 */
804void pci_set_driver_data (struct pci_dev *dev, void *driver_data)
805{
806 if (dev)
807 dev->driver_data = driver_data;
808}
809
810/**
811 */
812void *pci_get_driver_data (struct pci_dev *dev)
813{
814 if (dev)
815 return dev->driver_data;
816 return 0;
817}
818
819/**
820 */
821unsigned long pci_get_dma_mask (struct pci_dev *dev)
822{
823 if (dev)
824 return dev->dma_mask;
825 return 0;
826}
827
828/**
829 */
830int release_resource(struct resource *newres)
831{
832 return 0;
833}
834
835/**
836 */
837int pci_set_latency_time(struct pci_dev *dev, int latency)
838{
839 pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
840 return 0;
841}
842
843/**
844 * pci_save_state - save the PCI configuration space of a device before suspending
845 * @dev: - PCI device that we're dealing with
846 * @buffer: - buffer to hold config space context
847 *
848 * @buffer must be large enough to hold the entire PCI 2.2 config space
849 * (>= 64 bytes).
850 */
851int pci_orig_save_state(struct pci_dev *dev, u32 *buffer)
852{
853 int i;
854 if (buffer) {
855 /* XXX: 100% dword access ok here? */
856 for (i = 0; i < 16; i++)
857 pci_read_config_dword(dev, i * 4,&buffer[i]);
858 }
859 return 0;
860}
861
862/**
863 * pci_restore_state - Restore the saved state of a PCI device
864 * @dev: - PCI device that we're dealing with
865 * @buffer: - saved PCI config space
866 *
867 */
868int pci_orig_restore_state(struct pci_dev *dev, u32 *buffer)
869{
870 int i;
871
872 if (buffer) {
873 for (i = 0; i < 16; i++)
874 pci_write_config_dword(dev,i * 4, buffer[i]);
875 }
876 /*
877 * otherwise, write the context information we know from bootup.
878 * This works around a problem where warm-booting from Windows
879 * combined with a D3(hot)->D0 transition causes PCI config
880 * header data to be forgotten.
881 */
882 else {
883 for (i = 0; i < 6; i ++)
884 pci_write_config_dword(dev,
885 PCI_BASE_ADDRESS_0 + (i * 4),
886 dev->resource[i].start);
887 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
888 }
889 return 0;
890}
891
892struct saved_config_tbl {
893 struct pci_dev *pci;
894 u32 config[16];
895};
896static struct saved_config_tbl saved_tbl[16];
897
898int pci_save_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) {
904 saved_tbl[i].pci = pci;
905 pci_orig_save_state(pci, saved_tbl[i].config);
906 return 1;
907 }
908 }
909 printk(KERN_DEBUG "snd: no pci config space found!\n");
910 return 0;
911}
912
913int pci_restore_state(struct pci_dev *pci)
914{
915 int i;
916 /* FIXME: mutex needed for race? */
917 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
918 if (saved_tbl[i].pci == pci) {
919 saved_tbl[i].pci = NULL;
920 pci_orig_restore_state(pci, saved_tbl[i].config);
921 return 0;
922 }
923 }
924 printk(KERN_DEBUG "snd: no saved pci config!\n");
925 return 1;
926}
927
928void pci_disable_device(struct pci_dev *dev)
929{
930 u16 pci_command;
931
932 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
933 if (pci_command & PCI_COMMAND_MASTER) {
934 pci_command &= ~PCI_COMMAND_MASTER;
935 pci_write_config_word(dev, PCI_COMMAND, pci_command);
936 }
937}
938
939int pci_request_region(struct pci_dev *pdev, int bar, char *res_name)
940{
941 int flags;
942
943 if (pci_resource_len(pdev, bar) == 0)
944 return 0;
945 flags = pci_get_flags(pdev, bar);
946 if (flags & IORESOURCE_IO) {
947 if (check_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
948 goto err_out;
949 request_region(pci_resource_start(pdev, bar),
950 pci_resource_len(pdev, bar), res_name);
951 }
952 else if (flags & IORESOURCE_MEM) {
953 if (check_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
954 goto err_out;
955 request_mem_region(pci_resource_start(pdev, bar),
956 pci_resource_len(pdev, bar), res_name);
957 }
958
959 return 0;
960
961err_out:
962 printk(KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
963 flags & IORESOURCE_IO ? "I/O" : "mem",
964 bar + 1, /* PCI BAR # */
965 pci_resource_len(pdev, bar), pci_resource_start(pdev, bar),
966 res_name);
967 return -EBUSY;
968}
969
970void pci_release_region(struct pci_dev *pdev, int bar)
971{
972 int flags;
973
974 if (pci_resource_len(pdev, bar) == 0)
975 return;
976 flags = pci_get_flags(pdev, bar);
977 if (flags & IORESOURCE_IO) {
978 release_region(pci_resource_start(pdev, bar),
979 pci_resource_len(pdev, bar));
980 }
981 else if (flags & IORESOURCE_MEM) {
982 release_mem_region(pci_resource_start(pdev, bar),
983 pci_resource_len(pdev, bar));
984 }
985}
986
987int pci_request_regions(struct pci_dev *pdev, char *res_name)
988{
989 int i;
990
991 for (i = 0; i < 6; i++)
992 if (pci_request_region(pdev, i, res_name))
993 goto err;
994 return 0;
995 err:
996 while (--i >= 0)
997 pci_release_region(pdev, i);
998 return -EBUSY;
999}
1000
1001void pci_release_regions(struct pci_dev *pdev)
1002{
1003 int i;
1004 for (i = 0; i < 6; i++)
1005 pci_release_region(pdev, i);
1006}
1007
1008const struct pci_device_id * pci_match_id(const struct pci_device_id *ids, struct pci_dev *dev)
1009{
1010 u16 subsystem_vendor, subsystem_device;
1011
1012 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1013 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subsystem_device);
1014
1015 while (ids->vendor || ids->subvendor || ids->class_mask) {
1016 if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
1017 (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
1018 (ids->subvendor == PCI_ANY_ID || ids->subvendor == subsystem_vendor) &&
1019 (ids->subdevice == PCI_ANY_ID || ids->subdevice == subsystem_device) &&
1020 !((ids->class ^ dev->class) & ids->class_mask))
1021 return ids;
1022 ids++;
1023 }
1024 return NULL;
1025}
1026
1027/** snd_pci_dev_present
1028 * Called by: various sound drivers
1029 */
1030int snd_pci_dev_present(const struct pci_device_id *ids)
1031{
1032 while (ids->vendor || ids->subvendor)
1033 {
1034 if (pci_find_device(ids->vendor, ids->subvendor, NULL)) return 1;
1035 ids++;
1036 }
1037 return 0;
1038}
1039
1040struct pci_driver_mapping {
1041 struct pci_dev *dev;
1042 struct pci_driver *drv;
1043 unsigned long dma_mask;
1044 void *driver_data;
1045 u32 saved_config[16];
1046};
1047
1048#define PCI_MAX_MAPPINGS 64
1049static struct pci_driver_mapping drvmap [PCI_MAX_MAPPINGS] = { { NULL, } , };
1050
1051
1052static struct pci_driver_mapping *get_pci_driver_mapping(struct pci_dev *dev)
1053{
1054 int i;
1055
1056 for (i = 0; i < PCI_MAX_MAPPINGS; i++)
1057 if (drvmap[i].dev == dev)
1058 return &drvmap[i];
1059 return NULL;
1060}
1061
1062struct pci_driver *snd_pci_compat_get_pci_driver(struct pci_dev *dev)
1063{
1064 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1065 if (map)
1066 return map->drv;
1067 return NULL;
1068}
1069#if 0
1070void * pci_get_drvdata (struct pci_dev *dev)
1071{
1072 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1073 if (map)
1074 return map->driver_data;
1075 return NULL;
1076}
1077
1078
1079void pci_set_drvdata (struct pci_dev *dev, void *driver_data)
1080{
1081 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1082 if (map)
1083 map->driver_data = driver_data;
1084}
1085#endif
1086
1087
1088//******************************************************************************
1089//******************************************************************************
1090OSSRET OSS32_APMResume()
1091{
1092 int i;
1093 struct pci_driver *driver;
1094
1095 dprintf(("OSS32_APMResume"));
1096
1097 for(i=0;i<MAX_PCI_DEVICES;i++)
1098 {
1099 if(pci_devices[i].devfn)
1100 {
1101 RMSetHandles(pci_devices[i].hAdapter); /* DAZ - dirty hack */
1102 driver = pci_devices[i].pcidriver;
1103 if(driver && driver->resume) {
1104 driver->resume(&pci_devices[i]);
1105 }
1106 }
1107 }
1108
1109 return OSSERR_SUCCESS;
1110}
1111//******************************************************************************
1112//******************************************************************************
1113OSSRET OSS32_APMSuspend()
1114{
1115 int i;
1116 struct pci_driver *driver;
1117
1118 dprintf(("OSS32_APMSuspend 1"));
1119
1120 for(i=0;i<MAX_PCI_DEVICES;i++)
1121 {
1122 if(pci_devices[i].devfn)
1123 {
1124 RMSetHandles(pci_devices[i].hAdapter); /* DAZ - dirty hack */
1125 driver = pci_devices[i].pcidriver;
1126 if(driver && driver->suspend) {
1127 driver->suspend(&pci_devices[i], SNDRV_CTL_POWER_D3cold);
1128 }
1129 }
1130 }
1131
1132 dprintf(("OSS32_APMSuspend 2"));
1133 return OSSERR_SUCCESS;
1134}
1135
1136#ifdef USE_MSI
1137extern int __syscall UniMsiAlloc(USHORT usBusDevFunc, ULONG *pulCount, UCHAR *pucIrq);
1138int snd_pci_enable_msi(struct pci_dev *dev)
1139{
1140 ULONG p;
1141 UCHAR irq;
1142
1143 if (dev->irq_pin)
1144 {
1145 p = 1; /* int count */
1146 if (UniMsiAlloc((dev->bus->number<<8) | dev->devfn, &p, &irq)) return -1;
1147 /* we have an msi interrupt */
1148 dev->irq = irq;
1149 dev->irq_pin = 0;
1150 }
1151 return 0;
1152}
1153#else
1154int snd_pci_enable_msi(struct pci_dev *dev)
1155{
1156 return -1;
1157}
1158#endif
1159
1160/**
1161 * pci_status_get_and_clear_errors - return and clear error bits in PCI_STATUS
1162 * @pdev: the PCI device
1163 *
1164 * Returns error bits set in PCI_STATUS and clears them.
1165 */
1166int pci_status_get_and_clear_errors(struct pci_dev *pdev)
1167{
1168 u16 status;
1169 int ret;
1170
1171 ret = pci_read_config_word(pdev, PCI_STATUS, &status);
1172 if (ret != PCIBIOS_SUCCESSFUL)
1173 return -EIO;
1174
1175 status &= PCI_STATUS_ERROR_BITS;
1176 if (status)
1177 pci_write_config_word(pdev, PCI_STATUS, status);
1178
1179 return status;
1180}
1181
Note: See TracBrowser for help on using the repository browser.