📄 cardbus.c
字号:
/*====================================================================== Cardbus device configuration cardbus.c 1.85 2002/06/29 06:23:09 The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The initial developer of the original code is David A. Hinds <dahinds@users.sourceforge.net>. Portions created by David A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights Reserved. Alternatively, the contents of this file may be used under the terms of the GNU General Public License version 2 (the "GPL"), in which case the provisions of the GPL are applicable instead of the above. If you wish to allow the use of your version of this file only under the terms of the GPL and not to allow others to use your version of this file under the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under either the MPL or the GPL. These routines handle allocating resources for Cardbus cards, as well as setting up and shutting down Cardbus sockets. They are called from cs.c in response to Request/ReleaseConfiguration and Request/ReleaseIO calls. ======================================================================*/#define __NO_VERSION__#include <linux/module.h>#include <linux/kernel.h>#include <linux/config.h>#include <linux/string.h>#include <linux/slab.h>#include <linux/mm.h>#include <linux/pci.h>#include <linux/ioport.h>#include <asm/irq.h>#include <asm/io.h>#ifndef PCMCIA_DEBUG#define PCMCIA_DEBUG 1#endifstatic int pc_debug = PCMCIA_DEBUG;#define IN_CARD_SERVICES#include <pcmcia/version.h>#include <pcmcia/cs_types.h>#include <pcmcia/ss.h>#include <pcmcia/cs.h>#include <pcmcia/bulkmem.h>#include <pcmcia/cistpl.h>#include "cs_internal.h"/*====================================================================*/#define FIND_FIRST_BIT(n) ((n) - ((n) & ((n)-1)))#define pci_readb pci_read_config_byte#define pci_writeb pci_write_config_byte#define pci_readw pci_read_config_word#define pci_writew pci_write_config_word#define pci_readl pci_read_config_dword#define pci_writel pci_write_config_dword#define CB_BAR(n) (PCI_BASE_ADDRESS_0+(4*(n)))#define CB_ROM_BASE 0x0030/* Offsets in the Expansion ROM Image Header */#define ROM_SIGNATURE 0x0000 /* 2 bytes */#define ROM_DATA_PTR 0x0018 /* 2 bytes *//* Offsets in the CardBus PC Card Data Structure */#define PCDATA_SIGNATURE 0x0000 /* 4 bytes */#define PCDATA_VPD_PTR 0x0008 /* 2 bytes */#define PCDATA_LENGTH 0x000a /* 2 bytes */#define PCDATA_REVISION 0x000c#define PCDATA_IMAGE_SZ 0x0010 /* 2 bytes */#define PCDATA_ROM_LEVEL 0x0012 /* 2 bytes */#define PCDATA_CODE_TYPE 0x0014#define PCDATA_INDICATOR 0x0015#if (LINUX_VERSION_CODE >= VERSION(2,1,103))#define NEW_LINUX_PCI#endif#if (LINUX_VERSION_CODE >= VERSION(2,3,40))#define NEWER_LINUX_PCI#endiftypedef struct cb_config_t { u_int size[7]; struct pci_dev dev;#ifndef NEW_LINUX_PCI struct { u_long base_address[6]; u_long rom_address; } extra;#endif} cb_config_t;#if (LINUX_VERSION_CODE >= VERSION(2,3,15))#define BASE(cb,n) ((cb).dev.resource[n].start)#define FLAGS(cb,n) ((cb).dev.resource[n].flags)#define ROM(cb) ((cb).dev.resource[6].start)#else#ifdef NEW_LINUX_PCI#define BASE(cb,n) ((cb).dev.base_address[n])#define ROM(cb) ((cb).dev.rom_address)#else#define BASE(cb,n) ((cb).extra.base_address[n])#define ROM(cb) ((cb).extra.rom_address)#endif#define FLAGS(cb,n) BASE(cb,n)#endif/* There are three classes of bridge maps: IO ports, non-prefetchable memory, and prefetchable memory */typedef enum { B_IO, B_M1, B_M2 } map_type;static const u_int map_flags[] = { PCI_BASE_ADDRESS_SPACE_IO, PCI_BASE_ADDRESS_SPACE_MEMORY, PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_PREFETCH};/*===================================================================== Expansion ROM's have a special layout, and pointers specify an image number and an offset within that image. check_rom() verifies that the expansion ROM exists and has the standard layout. xlate_rom_addr() converts an image/offset address to an absolute offset from the ROM's base address. =====================================================================*/static int check_rom(u_char *b, u_long len){ u_int img = 0, ofs = 0, sz; u_short data; DEBUG(0, "ROM image dump:\n"); while ((readb(b) == 0x55) && (readb(b+1) == 0xaa)) { data = readb(b+ROM_DATA_PTR) + (readb(b+ROM_DATA_PTR+1) << 8); sz = 512 * (readb(b+data+PCDATA_IMAGE_SZ) + (readb(b+data+PCDATA_IMAGE_SZ+1) << 8)); DEBUG(0, " image %d: 0x%06x-0x%06x, signature %c%c%c%c\n", img, ofs, ofs+sz-1, readb(b+data+PCDATA_SIGNATURE), readb(b+data+PCDATA_SIGNATURE+1), readb(b+data+PCDATA_SIGNATURE+2), readb(b+data+PCDATA_SIGNATURE+3)); ofs += sz; img++; if ((readb(b+data+PCDATA_INDICATOR) & 0x80) || (sz == 0) || (ofs >= len)) break; b += sz; } return img;}static u_int xlate_rom_addr(u_char *b, u_int addr){ u_int img = 0, ofs = 0, sz; u_short data; while ((readb(b) == 0x55) && (readb(b+1) == 0xaa)) { if (img == (addr >> 28)) return (addr & 0x0fffffff) + ofs; data = readb(b+ROM_DATA_PTR) + (readb(b+ROM_DATA_PTR+1) << 8); sz = 512 * (readb(b+data+PCDATA_IMAGE_SZ) + (readb(b+data+PCDATA_IMAGE_SZ+1) << 8)); if ((sz == 0) || (readb(b+data+PCDATA_INDICATOR) & 0x80)) break; b += sz; ofs += sz; img++; } return 0;}/*===================================================================== These are similar to setup_cis_mem and release_cis_mem for 16-bit cards. The "result" that is used externally is the cb_cis_virt pointer in the socket_info_t structure. =====================================================================*/static int cb_setup_cis_mem(socket_info_t *s, int space){ cb_bridge_map *m = &s->cb_cis_map; cb_config_t *c = s->cb_config; u_long base = 0; u_int sz, br; if (space == s->cb_cis_space) return CS_SUCCESS; else if (s->cb_cis_virt) cb_release_cis_mem(s); DEBUG(1, "cs: cb_setup_cis_mem(space %d)\n", space); /* If socket is configured, then use existing memory mapping */ if (s->lock_count) { s->cb_cis_virt = ioremap(BASE(s->cb_config[0], space-1), s->cb_config[0].size[space-1] & ~3); s->cb_cis_space = space; return CS_SUCCESS; } /* Not configured? Then set up temporary map */ br = (space == 7) ? CB_ROM_BASE : CB_BAR(space-1); pci_writel(&c[0].dev, br, 0xffffffff); pci_readl(&c[0].dev, br, &sz); sz &= PCI_BASE_ADDRESS_MEM_MASK; sz = FIND_FIRST_BIT(sz); if (sz < PAGE_SIZE) sz = PAGE_SIZE; if (find_mem_region(&base, sz, sz, 0, "cb_enabler") != 0) { printk(KERN_NOTICE "cs: could not allocate %dK memory for" " CardBus socket %d\n", sz/1024, s->sock); return CS_OUT_OF_RESOURCE; } s->cb_cis_space = space; s->cb_cis_virt = ioremap(base, sz); DEBUG(1, " phys 0x%08lx-0x%08lx, virt 0x%08lx\n", base, base+sz-1, (u_long)s->cb_cis_virt); pci_writel(&c[0].dev, br, base | 1); pci_writeb(&c[0].dev, PCI_COMMAND, PCI_COMMAND_MEMORY); m->map = 0; m->flags = MAP_ACTIVE; m->start = base; m->stop = base+sz-1; s->ss_entry(s->sock, SS_SetBridge, m); if ((space == 7) && (check_rom(s->cb_cis_virt, sz) == 0)) { printk(KERN_NOTICE "cs: no valid ROM images found!\n"); return CS_READ_FAILURE; } return CS_SUCCESS;}void cb_release_cis_mem(socket_info_t *s){ cb_bridge_map *m = &s->cb_cis_map; u_int br; if (s->cb_cis_virt) { DEBUG(1, "cs: cb_release_cis_mem()\n"); iounmap(s->cb_cis_virt); s->cb_cis_virt = NULL; s->cb_cis_space = 0; } if (m->start) { cb_config_t *c = s->cb_config; /* This is overkill: we probably only need to release the memory region, but the rest should be safe */ br = (s->cb_cis_space == 7) ? CB_ROM_BASE : CB_BAR(s->cb_cis_space-1); m->map = 0; m->flags = 0; s->ss_entry(s->sock, SS_SetBridge, m); pci_writeb(&c[0].dev, PCI_COMMAND, 0); pci_writel(&c[0].dev, br, 0); release_mem_region(m->start, m->stop - m->start + 1); m->start = 0; }}/*===================================================================== This is used by the CIS processing code to read CIS information from a CardBus device. =====================================================================*/void read_cb_mem(socket_info_t *s, u_char fn, int space, u_int addr, u_int len, void *ptr){ cb_config_t *c = s->cb_config; DEBUG(3, "cs: read_cb_mem(%d, %#x, %u)\n", space, addr, len); if (space == 0) { if (addr+len > 0x100) goto fail; for (; len; addr++, ptr++, len--) pci_readb(&c[fn].dev, addr, (u_char *)ptr); } else { if (cb_setup_cis_mem(s, space) != 0) goto fail; if (space == 7) { addr = xlate_rom_addr(s->cb_cis_virt, addr); if (addr == 0) goto fail; } if (addr+len > s->cb_cis_map.stop - s->cb_cis_map.start) goto fail; for (; len; addr++, ptr++, len--) *(u_char *)ptr = readb(s->cb_cis_virt+addr); } return; fail: memset(ptr, 0xff, len); return;}/*===================================================================== cb_alloc() and cb_free() allocate and free the kernel data structures for a Cardbus device, and handle the lowest level PCI device setup issues. =====================================================================*/int cb_alloc(socket_info_t *s){ u_short vend, v, dev; u_char hdr, fn, bus = s->cap.cardbus; cb_config_t *c; struct pci_dev tmp; int i; if (s->cb_config) return CS_SUCCESS; tmp.bus = s->cap.cb_bus; tmp.devfn = 0;#ifdef NEWER_LINUX_PCI list_add(&tmp.global_list, &pci_devices);#else tmp.next = pci_devices; pci_devices = &tmp;#endif /* The APA1480 did not like reading the vendor ID first */ pci_readw(&tmp, PCI_DEVICE_ID, &dev); pci_readw(&tmp, PCI_VENDOR_ID, &vend); printk(KERN_INFO "cs: cb_alloc(bus %d): vendor 0x%04x, " "device 0x%04x\n", bus, vend, dev); pci_readb(&tmp, PCI_HEADER_TYPE, &hdr); if (hdr & 0x80) { /* Count functions */ for (fn = 0; fn < 8; fn++) { tmp.devfn = fn; pci_readw(&tmp, PCI_VENDOR_ID, &v); if (v != vend) break; } } else fn = 1; s->functions = fn; c = kmalloc(fn * sizeof(struct cb_config_t), GFP_ATOMIC); if (!c) return CS_OUT_OF_RESOURCE; memset(c, 0, fn * sizeof(struct cb_config_t)); s->cb_config = c; for (i = 0; i < fn; i++) { c[i].dev.bus = s->cap.cb_bus; c[i].dev.devfn = i;#ifdef NEWER_LINUX_PCI list_add_tail(&c[i].dev.bus_list, &s->cap.cb_bus->devices);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -