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