📄 rsrc_mgr.c
字号:
/*====================================================================== Resource management routines rsrc_mgr.c 1.88 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. ======================================================================*/#define __NO_VERSION__#include <linux/config.h>#include <linux/module.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/ioport.h>#include <linux/timer.h>#include <linux/spinlock.h>#include <asm/irq.h>#include <asm/io.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"/*====================================================================*//* Parameters that can be set with 'insmod' */#define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")INT_MODULE_PARM(probe_mem, 1); /* memory probe? */#ifdef CONFIG_ISAINT_MODULE_PARM(probe_io, 1); /* IO port probe? */INT_MODULE_PARM(mem_limit, 0x10000);#endif/*====================================================================== The resource_map_t structures are used to track what resources are available for allocation for PC Card devices.======================================================================*/typedef struct resource_map_t { u_long base, num; struct resource_map_t *next;} resource_map_t;/* Memory resource database */static resource_map_t mem_db = { 0, 0, &mem_db };/* IO port resource database */static resource_map_t io_db = { 0, 0, &io_db };#ifdef CONFIG_ISAtypedef struct irq_info_t { u_int Attributes; int time_share, dyn_share; struct socket_info_t *Socket;} irq_info_t;/* Table of IRQ assignments */static irq_info_t irq_table[NR_IRQS] = { { 0, 0, 0 }, /* etc */ };#endif/*====================================================================== Linux resource management extensions ======================================================================*/#ifndef CONFIG_PNP_BIOS#define check_io_region(b,n) (0)#endif#if defined(CONFIG_PNP_BIOS) || !defined(HAVE_MEMRESERVE)#ifdef USE_SPIN_LOCKSstatic spinlock_t rsrc_lock = SPIN_LOCK_UNLOCKED;#endiftypedef struct resource_entry_t { u_long base, num; char *name; struct resource_entry_t *next;} resource_entry_t;/* Ordered linked lists of allocated IO and memory blocks */#ifdef CONFIG_PNP_BIOSstatic resource_entry_t io_list = { 0, 0, NULL, NULL };#endif#ifndef HAVE_MEMRESERVEstatic resource_entry_t mem_list = { 0, 0, NULL, NULL };#endifstatic resource_entry_t *find_gap(resource_entry_t *root, resource_entry_t *entry){ resource_entry_t *p; if (entry->base > entry->base+entry->num-1) return NULL; for (p = root; ; p = p->next) { if ((p != root) && (p->base+p->num-1 >= entry->base)) { p = NULL; break; } if ((p->next == NULL) || (p->next->base > entry->base+entry->num-1)) break; } return p;}static int register_my_resource(resource_entry_t *list, u_long base, u_long num, char *name){ u_long flags; resource_entry_t *p, *entry; entry = kmalloc(sizeof(resource_entry_t), GFP_ATOMIC); if (!entry) return -ENOMEM; entry->base = base; entry->num = num; entry->name = name; spin_lock_irqsave(&rsrc_lock, flags); p = find_gap(list, entry); if (p == NULL) { spin_unlock_irqrestore(&rsrc_lock, flags); kfree(entry); return -EBUSY; } entry->next = p->next; p->next = entry; spin_unlock_irqrestore(&rsrc_lock, flags); return 0;}static void release_my_resource(resource_entry_t *list, u_long base, u_long num){ u_long flags; resource_entry_t *p, *q; spin_lock_irqsave(&rsrc_lock, flags); for (p = list; ; p = q) { q = p->next; if (q == NULL) break; if ((q->base == base) && (q->num == num)) { p->next = q->next; kfree(q); spin_unlock_irqrestore(&rsrc_lock, flags); return; } } spin_unlock_irqrestore(&rsrc_lock, flags); return;}static int check_my_resource(resource_entry_t *list, u_long base, u_long num){ if (register_my_resource(list, base, num, NULL) != 0) return -EBUSY; release_my_resource(list, base, num); return 0;}#ifdef CONFIG_PNP_BIOSint check_io_region(u_long base, u_long num){ return check_my_resource(&io_list, base, num);}void request_io_region(u_long base, u_long num, char *name){ register_my_resource(&io_list, base, num, name);}void release_io_region(u_long base, u_long num){ release_my_resource(&io_list, base, num);}#ifdef HAS_PROC_BUSint proc_read_io(char *buf, char **start, off_t pos, int count, int *eof, void *data){ resource_entry_t *r; u_long flags; char *p = buf; spin_lock_irqsave(&rsrc_lock, flags); for (r = io_list.next; r; r = r->next) p += sprintf(p, "%04lx-%04lx : %s\n", r->base, r->base+r->num-1, r->name); spin_unlock_irqrestore(&rsrc_lock, flags); return (p - buf);}#endif#endif#ifndef HAVE_MEMRESERVEint check_mem_region(u_long base, u_long num){ return check_my_resource(&mem_list, base, num);}void request_mem_region(u_long base, u_long num, char *name){ register_my_resource(&mem_list, base, num, name);}void release_mem_region(u_long base, u_long num){ release_my_resource(&mem_list, base, num);}#ifdef HAS_PROC_BUSint proc_read_mem(char *buf, char **start, off_t pos, int count, int *eof, void *data){ resource_entry_t *r; u_long flags; char *p = buf; spin_lock_irqsave(&rsrc_lock, flags); for (r = mem_list.next; r; r = r->next) p += sprintf(p, "%08lx-%08lx : %s\n", r->base, r->base+r->num-1, r->name); spin_unlock_irqrestore(&rsrc_lock, flags); return (p - buf);}#endif#endif#endif /* defined(CONFIG_PNP_BIOS) || !defined(HAVE_MEMRESERVE) *//*====================================================================== These manage the internal databases of available resources. ======================================================================*/static int add_interval(resource_map_t *map, u_long base, u_long num){ resource_map_t *p, *q; for (p = map; ; p = p->next) { if ((p != map) && (p->base+p->num-1 >= base)) return -1; if ((p->next == map) || (p->next->base > base+num-1)) break; } q = kmalloc(sizeof(resource_map_t), GFP_KERNEL); if (!q) return CS_OUT_OF_RESOURCE; q->base = base; q->num = num; q->next = p->next; p->next = q; return CS_SUCCESS;}/*====================================================================*/static int sub_interval(resource_map_t *map, u_long base, u_long num){ resource_map_t *p, *q; for (p = map; ; p = q) { q = p->next; if (q == map) break; if ((q->base+q->num > base) && (base+num > q->base)) { if (q->base >= base) { if (q->base+q->num <= base+num) { /* Delete whole block */ p->next = q->next; kfree(q); /* don't advance the pointer yet */ q = p; } else { /* Cut off bit from the front */ q->num = q->base + q->num - base - num; q->base = base + num; } } else if (q->base+q->num <= base+num) { /* Cut off bit from the end */ q->num = base - q->base; } else { /* Split the block into two pieces */ p = kmalloc(sizeof(resource_map_t), GFP_KERNEL); if (!p) return CS_OUT_OF_RESOURCE; p->base = base+num; p->num = q->base+q->num - p->base; q->num = base - q->base; p->next = q->next ; q->next = p; } } } return CS_SUCCESS;}/*====================================================================== These routines examine a region of IO or memory addresses to determine what ranges might be genuinely available. ======================================================================*/#ifdef CONFIG_ISAstatic void do_io_probe(ioaddr_t base, ioaddr_t num){ ioaddr_t i, j, bad, any; u_char *b, hole, most; printk(KERN_INFO "cs: IO port probe 0x%04x-0x%04x:", base, base+num-1); /* First, what does a floating port look like? */ b = kmalloc(256, GFP_KERNEL); if (!b) { printk(KERN_INFO " kmalloc failed!\n"); return; } memset(b, 0, 256); for (i = base, most = 0; i < base+num; i += 8) { if (check_region(i, 8) || check_io_region(i, 8)) continue; hole = inb(i); for (j = 1; j < 8; j++) if (inb(i+j) != hole) break; if ((j == 8) && (++b[hole] > b[most])) most = hole; if (b[most] == 127) break; } kfree(b); bad = any = 0; for (i = base; i < base+num; i += 8) { if (check_region(i, 8) || check_io_region(i, 8)) continue; for (j = 0; j < 8; j++) if (inb(i+j) != most) break; if (j < 8) { if (!any) printk(" excluding"); if (!bad) bad = any = i; } else { if (bad) { sub_interval(&io_db, bad, i-bad); printk(" %#04x-%#04x", bad, i-1); bad = 0; } } } if (bad) { if ((num > 16) && (bad == base) && (i == base+num)) { printk(" nothing: probe failed.\n"); return; } else { sub_interval(&io_db, bad, i-bad); printk(" %#04x-%#04x", bad, i-1); } } printk(any ? "\n" : " clean.\n");}static int io_scan; /* = 0 */static void invalidate_io(void){ io_scan = 0;}static void validate_io(void){ resource_map_t *m; if (!probe_io || io_scan++) return; for (m = io_db.next; m != &io_db; m = m->next) do_io_probe(m->base, m->num);}#else /* CONFIG_ISA */#define validate_io() do { } while (0)#define invalidate_io() do { } while (0)#endif /* CONFIG_ISA *//*====================================================================== The memory probe. If the memory list includes a 64K-aligned block below 1MB, we probe in 64K chunks, and as soon as we accumulate at least mem_limit free space, we quit. ======================================================================*/static int do_mem_probe(u_long base, u_long num, int (*is_valid)(u_long), int (*do_cksum)(u_long)){ u_long i, j, bad, fail, step; printk(KERN_INFO "cs: memory probe 0x%06lx-0x%06lx:", base, base+num-1); bad = fail = 0; step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -