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

Last change on this file since 615 was 615, checked in by Paul Smedley, 5 years ago

Add source for uniaud32 based on code from linux kernel 5.4.86

File size: 32.8 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;
207pr_warn("params set");
208
209 // Subsystem ID
210 pci_read_config_word(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &pcidev->subsystem_vendor);
211 pci_read_config_word(pcidev, PCI_SUBSYSTEM_ID, &pcidev->subsystem_device);
212
213 // revision
214 pci_read_config_byte(pcidev, PCI_REVISION_ID, &pcidev->revision);
215
216 // I/O and MEM
217 resNo = 0;
218 for( addr = PCI_BASE_ADDRESS_0; addr <= PCI_BASE_ADDRESS_5; addr += 4 )
219 {
220 pci_read_config_dword(pcidev, addr, &ulTmp1);
221 if( ulTmp1 != 0 && ulTmp1 != 0xffffffff )
222 {
223 pci_write_config_dword(pcidev, addr, 0xffffffff);
224 pci_read_config_dword(pcidev, addr, &ulTmp2);
225 pci_write_config_dword(pcidev, addr, ulTmp1);
226
227 if( ulTmp1 & PCI_BASE_ADDRESS_SPACE_IO )
228 {
229 pcidev->resource[resNo].flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
230 pcidev->resource[resNo].start = ulTmp1 & PCI_BASE_ADDRESS_IO_MASK;
231 pcidev->resource[resNo].end = pcidev->resource[resNo].start +
232 ~(ulTmp2 & PCI_BASE_ADDRESS_IO_MASK) + 1;
233 }
234 else
235 {
236 pcidev->resource[resNo].flags = IORESOURCE_MEM | IORESOURCE_MEM_WRITEABLE;
237 pcidev->resource[resNo].start = ulTmp1 & PCI_BASE_ADDRESS_MEM_MASK;
238 pcidev->resource[resNo].end = pcidev->resource[resNo].start +
239 ~(ulTmp2 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
240 }
241
242 resNo++;
243 }
244 }
245
246 // IRQ and PIN
247 pci_read_config_dword(pcidev, PCI_INTERRUPT_LINE, &ulTmp1);
248 //rprintf(("pci_query_device: PCI config IRQ=%d", ulTmp1&0xff));
249 if( (u8)ulTmp1 && (u8)ulTmp1 != 0xff )
250 {
251 pcidev->irq_resource[0].flags = IORESOURCE_IRQ;
252 pcidev->irq_resource[0].start = pcidev->irq_resource[0].end = ulTmp1 & 0xffff;
253 pcidev->irq = (u8)ulTmp1; // This is the interrupt used for init time processing
254 pcidev->irq_pin = ulTmp1>>8;
255 }
256
257 return ((busNr << 8) | PCI_DEVFN(devNr, funcNr));
258 } /* for funcNr */
259 funcNr = 0;
260 } /* for devNr */
261 devNr = 0;
262 }
263 outl(cfgaddrreg, PCI_CONFIG_ADDRESS);
264 return 0;
265}
266
267/**
268 * Called by: snd_pci_dev_present, various sound drivers
269 *
270 * Find the requested device
271 */
272struct pci_dev *pci_find_device (unsigned int vendor, unsigned int device, struct pci_dev *from)
273{
274 int i;
275 struct pci_device_id id_table;
276
277 for(i=0; i<MAX_PCI_DEVICES; i++)
278 {
279 if ( pci_devices[i].devfn && (pci_devices[i].vendor == vendor) && (pci_devices[i].device == device) ) return &pci_devices[i];
280 }
281
282 for(i=0; i<MAX_PCI_DEVICES; i++)
283 {
284 if(pci_devices[i].devfn == 0)
285 {
286 memset(&id_table, 0, sizeof(id_table));
287 id_table.vendor = vendor;
288 id_table.device = device;
289 if( pci_query_device(&id_table, (struct pci_dev near *)&pci_devices[i], 0) ) return &pci_devices[i];
290 else break;
291 }
292 }
293
294 return NULL;
295}
296
297/** __request_region
298 */
299struct resource * __request_region(struct resource *a, unsigned long start, unsigned long n, const char *name)
300{
301 struct resource *resource;
302
303 if(a->flags & IORESOURCE_MEM) {
304 if(RMRequestMem(/*hResMgr,*/ start, n) == FALSE) {
305 printk("RMRequestIO failed for io %x, length %x\n", start, n);
306 return NULL;
307 }
308 }
309 else if(a->flags & IORESOURCE_IO) {
310 if(RMRequestIO(/*hResMgr,*/ start, n) == FALSE) {
311 printk("RMRequestIO failed for io %x, length %x\n", start, n);
312 return NULL;
313 }
314 }
315
316 resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
317 if (resource == NULL)
318 return NULL;
319 resource->name = name;
320 resource->start = start;
321 resource->end = start + n; // - 1;
322 resource->flags = a->flags;
323 resource->parent =
324 resource->child = NULL;
325
326 // insert in list
327 resource->sibling = a->sibling;
328 a->sibling = resource;
329
330 return resource;
331}
332
333/**
334 */
335void __release_region(struct resource *a, unsigned long start, unsigned long n)
336{
337 struct resource *resource;
338 struct resource **ppres = &a->sibling;
339 unsigned long end = start + n; // - 1;
340
341 while( *ppres )
342 {
343 resource = *ppres;
344
345 if( resource->start == start && resource->end == end )
346 {
347 // remove from list
348 *ppres = resource->sibling;
349 kfree(resource);
350 return;
351 }
352
353 ppres = &resource->sibling;
354 }
355}
356
357/**
358 */
359int pci_get_flags (struct pci_dev *dev, int n_base)
360{
361 if(n_base >= DEVICE_COUNT_RESOURCE || !dev->resource[n_base].flags) {
362 DebugInt3();
363 return 0;
364 }
365 return dev->resource[n_base].flags;
366}
367
368/**
369 */
370int pcibios_present(void)
371{
372 printk("pcibios_present -> pretend BIOS present\n");
373 return 1;
374}
375
376/**
377 */
378struct pci_dev *pci_find_slot (unsigned int bus, unsigned int devfn)
379{
380 printk("pci_find_slot %d %x not implemented!!\n", bus, devfn);
381 DebugInt3();
382 return NULL;
383}
384
385/**
386 */
387int pci_dma_supported(struct pci_dev *dev, unsigned long mask)
388{
389 printk("pci_dma_supported: return TRUE\n");
390 return 1;
391}
392
393/**
394 */
395int pci_find_capability(struct pci_dev *dev, int cap)
396{
397 u16 status;
398 u8 pos, id;
399 int ttl = 48;
400
401 pci_read_config_word(dev, PCI_STATUS, &status);
402 if (!(status & PCI_STATUS_CAP_LIST))
403 return 0;
404 pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &pos);
405 while (ttl-- && pos >= 0x40) {
406 pos &= ~3;
407 pci_read_config_byte(dev, pos + PCI_CAP_LIST_ID, &id);
408 if (id == 0xff)
409 break;
410 if (id == cap)
411 return pos;
412 pci_read_config_byte(dev, pos + PCI_CAP_LIST_NEXT, &pos);
413 }
414 return 0;
415}
416
417/**
418 * Set power management state of a device. For transitions from state D3
419 * it isn't as straightforward as one could assume since many devices forget
420 * their configuration space during wakeup. Returns old power state.
421 */
422int pci_set_power_state(struct pci_dev *dev, int new_state)
423{
424 u32 base[5], romaddr;
425 u16 pci_command, pwr_command;
426 u8 pci_latency, pci_cacheline;
427 int i, old_state;
428 int pm = pci_find_capability(dev, PCI_CAP_ID_PM);
429
430 if (!pm)
431 return 0;
432 pci_read_config_word(dev, pm + PCI_PM_CTRL, &pwr_command);
433 old_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
434 if (old_state == new_state)
435 return old_state;
436 if (old_state == 3) {
437 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
438 pci_write_config_word(dev, PCI_COMMAND, pci_command & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
439 for (i = 0; i < 5; i++)
440 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + i*4, &base[i]);
441 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &romaddr);
442 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
443 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_cacheline);
444 pci_write_config_word(dev, pm + PCI_PM_CTRL, new_state);
445 for (i = 0; i < 5; i++)
446 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + i*4, base[i]);
447 pci_write_config_dword(dev, PCI_ROM_ADDRESS, romaddr);
448 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
449 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cacheline);
450 pci_write_config_byte(dev, PCI_LATENCY_TIMER, pci_latency);
451 pci_write_config_word(dev, PCI_COMMAND, pci_command);
452 } else
453 pci_write_config_word(dev, pm + PCI_PM_CTRL, (pwr_command & ~PCI_PM_CTRL_STATE_MASK) | new_state);
454 return old_state;
455}
456
457/**
458 * Initialize device before it's used by a driver. Ask low-level code
459 * to enable I/O and memory. Wake up the device if it was suspended.
460 * Beware, this function can fail.
461 */
462int pci_enable_device(struct pci_dev *dev)
463{
464 u16 pci_command;
465
466 dprintf(("pci_enable_device %x\n", dev));
467
468 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
469 pci_write_config_word(dev, PCI_COMMAND, pci_command | (PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
470 pci_set_power_state(dev, 0);
471 return 0;
472}
473
474/** pci_register_driver
475 *
476 * probes and registers a sound driver with RM.
477 *
478 * Returns: number of cards found.
479 */
480int pci_register_driver(struct pci_driver *driver)
481{
482 int iNumCards, iTmp;
483 ULONG ulLast;
484 struct pci_dev *pcidev;
485 struct pci_device_id IdTable;
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(("pci_register_driver: query_device found %x %x:%x class=%x checking %s",
509 ulLast, pcidev->vendor, pcidev->device, pcidev->class, driver->name));
510
511 for( iTableIx = 0; driver->id_table[iTableIx].vendor; iTableIx++)
512 {
513 struct pci_device_id const *pDriverId = &driver->id_table[iTableIx];
514
515 if ( (pDriverId->class) && ((pcidev->class & pDriverId->class_mask) != pDriverId->class) ) continue;
516 if (pDriverId->vendor != pcidev->vendor) continue;
517 if ( (pDriverId->device != PCI_ANY_ID) && (pDriverId->device != pcidev->device) ) continue;
518
519 rprintf(("pci_register_driver: matched %d %x:%x/%x with %x:%x/%x %x (%s)", iTableIx,
520 pcidev->vendor, pcidev->device, pcidev->class,
521 pDriverId->vendor, pDriverId->device, pDriverId->class, pDriverId->class_mask, driver->name));
522
523 if ((iAdapterNumber >= 0) && (iAdapter < iAdapterNumber))
524 {
525 iAdapter++;
526 continue;
527 }
528
529 RMInit();
530 if (driver->probe(pcidev, pDriverId) == 0)
531 {
532 pcidev->pcidriver = (void *)driver;
533 pcidev->current_state = 4;
534
535 // create adapter
536 RMDone((pcidev->device << 16) | pcidev->vendor, &pcidev->hAdapter, &pcidev->hDevice);
537 iNumCards++;
538 pcidev = NULL; /* we need a new slot */
539 break;
540 }
541 RMDone(0, 0, 0);
542 } /* for id_table loop */
543
544 if (pcidev)
545 {
546 kfree(pcidev->bus);
547 pcidev->devfn = 0;
548 }
549 else
550 {
551 if (iAdapterNumber >= 0) break;
552 /* find another empty slot */
553 for (iTmp=0; iTmp<MAX_PCI_DEVICES; iTmp++)
554 {
555 if (pci_devices[iTmp].devfn == 0) break;
556 }
557 if (iTmp >= MAX_PCI_DEVICES) break;
558 pcidev = &pci_devices[iTmp];
559 }
560 } /* pci_query_device loop */
561
562 return iNumCards;
563}
564
565/**
566 */
567int pci_module_init(struct pci_driver *drv)
568{
569 int res = pci_register_driver(drv);
570 if (res == 0) return -ENODEV;
571 return res;
572}
573
574/**
575 */
576int pci_unregister_driver(struct pci_driver *driver)
577{
578 struct pci_dev *pcidev;
579 int i, j;
580
581 for (i=0; driver->id_table[i].vendor; i++) {
582 for(j=0; j<MAX_PCI_DEVICES; j++) {
583 pcidev = &pci_devices[j];
584 if (pcidev->devfn == 0) continue;
585 if (pcidev->vendor != driver->id_table[i].vendor) continue;
586 if ( (driver->id_table[i].device != PCI_ANY_ID) && (pcidev->device != driver->id_table[i].device) ) continue;
587 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));
588 if (driver->remove) driver->remove(pcidev);
589 kfree(pcidev->bus);
590 pcidev->devfn = 0;
591 }
592 }
593 return 0;
594}
595
596/**
597 */
598void pci_set_master(struct pci_dev *dev)
599{
600 u16 cmd;
601
602 pci_read_config_word(dev, PCI_COMMAND, &cmd);
603 if (! (cmd & PCI_COMMAND_MASTER)) {
604 dprintf(("pci_set_master %x", dev));
605 cmd |= PCI_COMMAND_MASTER;
606 pci_write_config_word(dev, PCI_COMMAND, cmd);
607 }
608 return;
609}
610
611/**
612 * Register a device with power management
613 */
614struct pm_dev *pm_register(pm_dev_t type, unsigned long id, pm_callback callback)
615{
616 dprintf(("pm_register STUB"));
617 DebugInt3();
618 return NULL;
619}
620
621/**
622 * Unregister a device with power management
623 */
624void pm_unregister(struct pm_dev *dev)
625{
626 dprintf(("pm_unregister STUB"));
627}
628
629/**
630 */
631int __compat_get_order(unsigned long size)
632{
633 int order;
634
635 size = (size-1) >> (PAGE_SHIFT-1);
636 order = -1;
637 do {
638 size >>= 1;
639 order++;
640 } while (size);
641 return order;
642}
643
644/**
645 */
646void *pci_alloc_consistent(struct pci_dev *hwdev,
647 long size, dma_addr_t *dma_handle)
648{
649 void *ret = NULL;
650 int gfp = GFP_ATOMIC;
651 int order;
652 dprintf(("pci_alloc_consistent %d mask %x", size, (hwdev) ? hwdev->dma_mask : 0));
653 if (hwdev == NULL || hwdev->dma_mask != 0xffffffff) {
654 dprintf(("pci_alloc_consistent"));
655pr_warn("dma_mask = %x",hwdev->dma_mask);
656 //try not to exhaust low memory (< 16mb) so allocate from the high region first
657 //if that doesn't satisfy the dma mask requirement, then get it from the low
658 //region anyway
659 if(hwdev->dma_mask > 0x00ffffff) {
660 dprintf(("pci_alloc_consistent2"));
661 order = __compat_get_order(size);
662 dprintf(("pci_alloc_consistent3"));
663 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
664 dprintf(("pci_alloc_consistent4"));
665 *dma_handle = virt_to_bus(ret);
666 if(*dma_handle > hwdev->dma_mask) {
667 dprintf(("pci_alloc_consistent5"));
668 free_pages((unsigned long)ret, __compat_get_order(size));
669 dprintf(("pci_alloc_consistent6"));
670 //be sure and allocate below 16 mb
671 gfp |= GFP_DMA;
672 ret = NULL;
673 }
674 dprintf(("pci_alloc_consistent6a"));
675 }
676 else { //must always allocate below 16 mb
677 dprintf(("pci_alloc_consistent7"));
678 gfp |= GFP_DMA;
679 }
680 dprintf(("pci_alloc_consistent7a"));
681 }
682 dprintf(("pci_alloc_consistent8"));
683 if(ret == NULL) {
684 dprintf(("pci_alloc_consistent9"));
685 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
686 }
687 dprintf(("pci_alloc_consistent10"));
688 if (ret != NULL) {
689 dprintf(("pci_alloc_consistent11"));
690 memset(ret, 0, size);
691 *dma_handle = virt_to_bus(ret);
692 }
693 return ret;
694}
695
696#if 0
697void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
698 dma_addr_t *dma_handle)
699 {
700 return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC);
701 }
702#endif
703#if 0
704void *dma_alloc_coherent(struct device *dev, size_t size,
705 dma_addr_t *dma_handle, gfp_t gfp)
706{
707 void *ret = NULL;
708 int order;
709
710 dprintf(("dma_alloc_coherent %d mask %x", size, (dev) ? dev->dma_mask : 0));
711 if (dev == NULL || *dev->dma_mask != 0xffffffff) {
712 dprintf(("dma_alloc_coherent"));
713 //try not to exhaust low memory (< 16mb) so allocate from the high region first
714 //if that doesn't satisfy the dma mask requirement, then get it from the low
715 //region anyway
716 if(*dev->dma_mask > 0x00ffffff) {
717 dprintf(("dma_alloc_coherent2"));
718 order = __compat_get_order(size);
719 ret = (void *)__get_free_pages(gfp|GFP_DMAHIGHMEM, order);
720 *dma_handle = virt_to_bus(ret);
721 if(*dma_handle > *dev->dma_mask) {
722 dprintf(("dma_alloc_coherent3"));
723 free_pages((unsigned long)ret, __compat_get_order(size));
724 //be sure and allocate below 16 mb
725 gfp |= GFP_DMA;
726 ret = NULL;
727 }
728 dprintf(("dma_alloc_coherent3a"));
729 }
730 else { //must always allocate below 16 mb
731 dprintf(("dma_alloc_coherent4"));
732 gfp |= GFP_DMA;
733 }
734 }
735 if(ret == NULL) {
736 dprintf(("dma_alloc_coherent5"));
737 ret = (void *)__get_free_pages(gfp, __compat_get_order(size));
738 }
739
740 if (ret != NULL) {
741 memset(ret, 0, size);
742 *dma_handle = virt_to_bus(ret);
743 }
744 return ret;
745
746}
747#endif
748
749int dma_supported(struct device *dev, u64 mask)
750{
751pr_warn("dma_supported");
752 return 1;
753}
754
755int dma_set_coherent_mask(struct device *dev, u64 mask)
756{
757pr_warn("dma_set_coherent_mask");
758 /*
759 * Truncate the mask to the actually supported dma_addr_t width to
760 * avoid generating unsupportable addresses.
761 */
762 mask = (dma_addr_t)mask;
763
764 if (!dma_supported(dev, mask))
765 return -EIO;
766
767 dev->coherent_dma_mask = mask;
768 return 0;
769}
770
771int dma_set_mask(struct device *dev, u64 mask)
772{
773pr_warn("dma_set_mask");
774 /*
775 * Truncate the mask to the actually supported dma_addr_t width to
776 * avoid generating unsupportable addresses.
777 */
778 mask = (dma_addr_t)mask;
779
780 if (!dev->dma_mask || !dma_supported(dev, mask))
781 return -EIO;
782
783 *dev->dma_mask = mask;
784 return 0;
785}
786
787/**
788 */
789void pci_free_consistent(struct pci_dev *hwdev, long size,
790 void *vaddr, dma_addr_t dma_handle)
791{
792 free_pages((unsigned long)vaddr, __compat_get_order(size));
793}
794
795/**
796 */
797void pci_set_driver_data (struct pci_dev *dev, void *driver_data)
798{
799 if (dev)
800 dev->driver_data = driver_data;
801}
802
803/**
804 */
805void *pci_get_driver_data (struct pci_dev *dev)
806{
807 if (dev)
808 return dev->driver_data;
809 return 0;
810}
811
812/**
813 */
814unsigned long pci_get_dma_mask (struct pci_dev *dev)
815{
816 if (dev)
817 return dev->dma_mask;
818 return 0;
819}
820
821/**
822 */
823int release_resource(struct resource *newres)
824{
825 return 0;
826}
827
828/**
829 */
830int pci_set_latency_time(struct pci_dev *dev, int latency)
831{
832 pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
833 return 0;
834}
835
836/**
837 * pci_save_state - save the PCI configuration space of a device before suspending
838 * @dev: - PCI device that we're dealing with
839 * @buffer: - buffer to hold config space context
840 *
841 * @buffer must be large enough to hold the entire PCI 2.2 config space
842 * (>= 64 bytes).
843 */
844int pci_orig_save_state(struct pci_dev *dev, u32 *buffer)
845{
846 int i;
847 if (buffer) {
848 /* XXX: 100% dword access ok here? */
849 for (i = 0; i < 16; i++)
850 pci_read_config_dword(dev, i * 4,&buffer[i]);
851 }
852 return 0;
853}
854
855/**
856 * pci_restore_state - Restore the saved state of a PCI device
857 * @dev: - PCI device that we're dealing with
858 * @buffer: - saved PCI config space
859 *
860 */
861int pci_orig_restore_state(struct pci_dev *dev, u32 *buffer)
862{
863 int i;
864
865 if (buffer) {
866 for (i = 0; i < 16; i++)
867 pci_write_config_dword(dev,i * 4, buffer[i]);
868 }
869 /*
870 * otherwise, write the context information we know from bootup.
871 * This works around a problem where warm-booting from Windows
872 * combined with a D3(hot)->D0 transition causes PCI config
873 * header data to be forgotten.
874 */
875 else {
876 for (i = 0; i < 6; i ++)
877 pci_write_config_dword(dev,
878 PCI_BASE_ADDRESS_0 + (i * 4),
879 dev->resource[i].start);
880 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
881 }
882 return 0;
883}
884
885struct saved_config_tbl {
886 struct pci_dev *pci;
887 u32 config[16];
888};
889static struct saved_config_tbl saved_tbl[16];
890
891int pci_save_state(struct pci_dev *pci)
892{
893 int i;
894 /* FIXME: mutex needed for race? */
895 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
896 if (! saved_tbl[i].pci) {
897 saved_tbl[i].pci = pci;
898 pci_orig_save_state(pci, saved_tbl[i].config);
899 return 1;
900 }
901 }
902 printk(KERN_DEBUG "snd: no pci config space found!\n");
903 return 0;
904}
905
906int pci_restore_state(struct pci_dev *pci)
907{
908 int i;
909 /* FIXME: mutex needed for race? */
910 for (i = 0; i < ARRAY_SIZE(saved_tbl); i++) {
911 if (saved_tbl[i].pci == pci) {
912 saved_tbl[i].pci = NULL;
913 pci_orig_restore_state(pci, saved_tbl[i].config);
914 return 0;
915 }
916 }
917 printk(KERN_DEBUG "snd: no saved pci config!\n");
918 return 1;
919}
920
921void pci_disable_device(struct pci_dev *dev)
922{
923 u16 pci_command;
924
925 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
926 if (pci_command & PCI_COMMAND_MASTER) {
927 pci_command &= ~PCI_COMMAND_MASTER;
928 pci_write_config_word(dev, PCI_COMMAND, pci_command);
929 }
930}
931
932int pci_request_region(struct pci_dev *pdev, int bar, char *res_name)
933{
934 int flags;
935
936 if (pci_resource_len(pdev, bar) == 0)
937 return 0;
938 flags = pci_get_flags(pdev, bar);
939 if (flags & IORESOURCE_IO) {
940 if (check_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
941 goto err_out;
942 request_region(pci_resource_start(pdev, bar),
943 pci_resource_len(pdev, bar), res_name);
944 }
945 else if (flags & IORESOURCE_MEM) {
946 if (check_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)))
947 goto err_out;
948 request_mem_region(pci_resource_start(pdev, bar),
949 pci_resource_len(pdev, bar), res_name);
950 }
951
952 return 0;
953
954err_out:
955 printk(KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
956 flags & IORESOURCE_IO ? "I/O" : "mem",
957 bar + 1, /* PCI BAR # */
958 pci_resource_len(pdev, bar), pci_resource_start(pdev, bar),
959 res_name);
960 return -EBUSY;
961}
962
963void pci_release_region(struct pci_dev *pdev, int bar)
964{
965 int flags;
966
967 if (pci_resource_len(pdev, bar) == 0)
968 return;
969 flags = pci_get_flags(pdev, bar);
970 if (flags & IORESOURCE_IO) {
971 release_region(pci_resource_start(pdev, bar),
972 pci_resource_len(pdev, bar));
973 }
974 else if (flags & IORESOURCE_MEM) {
975 release_mem_region(pci_resource_start(pdev, bar),
976 pci_resource_len(pdev, bar));
977 }
978}
979
980int pci_request_regions(struct pci_dev *pdev, char *res_name)
981{
982 int i;
983
984 for (i = 0; i < 6; i++)
985 if (pci_request_region(pdev, i, res_name))
986 goto err;
987 return 0;
988 err:
989 while (--i >= 0)
990 pci_release_region(pdev, i);
991 return -EBUSY;
992}
993
994void pci_release_regions(struct pci_dev *pdev)
995{
996 int i;
997 for (i = 0; i < 6; i++)
998 pci_release_region(pdev, i);
999}
1000
1001const struct pci_device_id * pci_match_id(const struct pci_device_id *ids, struct pci_dev *dev)
1002{
1003 u16 subsystem_vendor, subsystem_device;
1004
1005 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1006 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subsystem_device);
1007
1008 while (ids->vendor || ids->subvendor || ids->class_mask) {
1009 if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
1010 (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
1011 (ids->subvendor == PCI_ANY_ID || ids->subvendor == subsystem_vendor) &&
1012 (ids->subdevice == PCI_ANY_ID || ids->subdevice == subsystem_device) &&
1013 !((ids->class ^ dev->class) & ids->class_mask))
1014 return ids;
1015 ids++;
1016 }
1017 return NULL;
1018}
1019
1020/** snd_pci_dev_present
1021 * Called by: various sound drivers
1022 */
1023int snd_pci_dev_present(const struct pci_device_id *ids)
1024{
1025 while (ids->vendor || ids->subvendor)
1026 {
1027 if (pci_find_device(ids->vendor, ids->subvendor, NULL)) return 1;
1028 ids++;
1029 }
1030 return 0;
1031}
1032
1033struct pci_driver_mapping {
1034 struct pci_dev *dev;
1035 struct pci_driver *drv;
1036 unsigned long dma_mask;
1037 void *driver_data;
1038 u32 saved_config[16];
1039};
1040
1041#define PCI_MAX_MAPPINGS 64
1042static struct pci_driver_mapping drvmap [PCI_MAX_MAPPINGS] = { { NULL, } , };
1043
1044
1045static struct pci_driver_mapping *get_pci_driver_mapping(struct pci_dev *dev)
1046{
1047 int i;
1048
1049 for (i = 0; i < PCI_MAX_MAPPINGS; i++)
1050 if (drvmap[i].dev == dev)
1051 return &drvmap[i];
1052 return NULL;
1053}
1054
1055struct pci_driver *snd_pci_compat_get_pci_driver(struct pci_dev *dev)
1056{
1057 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1058 if (map)
1059 return map->drv;
1060 return NULL;
1061}
1062#if 0
1063void * pci_get_drvdata (struct pci_dev *dev)
1064{
1065 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1066 if (map)
1067 return map->driver_data;
1068 return NULL;
1069}
1070
1071
1072void pci_set_drvdata (struct pci_dev *dev, void *driver_data)
1073{
1074 struct pci_driver_mapping *map = get_pci_driver_mapping(dev);
1075 if (map)
1076 map->driver_data = driver_data;
1077}
1078#endif
1079
1080
1081//******************************************************************************
1082//******************************************************************************
1083OSSRET OSS32_APMResume()
1084{
1085 int i;
1086 struct pci_driver *driver;
1087
1088 dprintf(("OSS32_APMResume"));
1089
1090 for(i=0;i<MAX_PCI_DEVICES;i++)
1091 {
1092 if(pci_devices[i].devfn)
1093 {
1094 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
1095 driver = pci_devices[i].pcidriver;
1096 if(driver && driver->resume) {
1097 driver->resume(&pci_devices[i]);
1098 }
1099 }
1100 }
1101
1102 return OSSERR_SUCCESS;
1103}
1104//******************************************************************************
1105//******************************************************************************
1106OSSRET OSS32_APMSuspend()
1107{
1108 int i;
1109 struct pci_driver *driver;
1110
1111 dprintf(("OSS32_APMSuspend 1"));
1112
1113 for(i=0;i<MAX_PCI_DEVICES;i++)
1114 {
1115 if(pci_devices[i].devfn)
1116 {
1117 RMSetHandles(pci_devices[i].hAdapter, pci_devices[i].hDevice); /* DAZ - dirty hack */
1118 driver = pci_devices[i].pcidriver;
1119 if(driver && driver->suspend) {
1120 driver->suspend(&pci_devices[i], SNDRV_CTL_POWER_D3cold);
1121 }
1122 }
1123 }
1124
1125 dprintf(("OSS32_APMSuspend 2"));
1126 return OSSERR_SUCCESS;
1127}
1128
1129#ifdef USE_MSI
1130extern int __syscall UniMsiAlloc(USHORT usBusDevFunc, ULONG *pulCount, UCHAR *pucIrq);
1131int snd_pci_enable_msi(struct pci_dev *dev)
1132{
1133 ULONG p;
1134 UCHAR irq;
1135
1136 if (dev->irq_pin)
1137 {
1138 p = 1; /* int count */
1139 if (UniMsiAlloc((dev->bus->number<<8) | dev->devfn, &p, &irq)) return -1;
1140 /* we have an msi interrupt */
1141 dev->irq = irq;
1142 dev->irq_pin = 0;
1143 }
1144 return 0;
1145}
1146#else
1147int snd_pci_enable_msi(struct pci_dev *dev)
1148{
1149 return -1;
1150}
1151#endif
1152
Note: See TracBrowser for help on using the repository browser.