📄 ecard.c
字号:
/* * linux/arch/arm/kernel/ecard.c * * Copyright 1995-1998 Russell King * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Find all installed expansion cards, and handle interrupts from them. * * Created from information from Acorns RiscOS3 PRMs * * 08-Dec-1996 RMK Added code for the 9'th expansion card - the ether * podule slot. * 06-May-1997 RMK Added blacklist for cards whose loader doesn't work. * 12-Sep-1997 RMK Created new handling of interrupt enables/disables * - cards can now register their own routine to control * interrupts (recommended). * 29-Sep-1997 RMK Expansion card interrupt hardware not being re-enabled * on reset from Linux. (Caused cards not to respond * under RiscOS without hard reset). * 15-Feb-1998 RMK Added DMA support * 12-Sep-1998 RMK Added EASI support * 10-Jan-1999 RMK Run loaders in a simulated RISC OS environment. * 17-Apr-1999 RMK Support for EASI Type C cycles. */#define ECARD_C#define __KERNEL_SYSCALLS__#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/interrupt.h>#include <linux/mm.h>#include <linux/malloc.h>#include <linux/proc_fs.h>#include <linux/init.h>#include <asm/dma.h>#include <asm/ecard.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/pgalloc.h>#include <asm/mmu_context.h>#ifndef CONFIG_ARCH_RPC#define HAVE_EXPMASK#endifenum req { req_readbytes, req_reset};struct ecard_request { enum req req; ecard_t *ec; unsigned int address; unsigned int length; unsigned int use_loader; void *buffer;};struct expcard_blacklist { unsigned short manufacturer; unsigned short product; const char *type;};static ecard_t *cards;static ecard_t *slot_to_expcard[MAX_ECARDS];static unsigned int ectcr;#ifdef HAS_EXPMASKstatic unsigned int have_expmask;#endif/* List of descriptions of cards which don't have an extended * identification, or chunk directories containing a description. */static struct expcard_blacklist __initdata blacklist[] = { { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }};asmlinkage extern intecard_loader_reset(volatile unsigned char *pa, loader_t loader);asmlinkage extern intecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);extern int setup_arm_irq(int, struct irqaction *);extern void do_ecard_IRQ(int, struct pt_regs *);static voidecard_irq_noexpmask(int intr_no, void *dev_id, struct pt_regs *regs);static struct irqaction irqexpansioncard = { ecard_irq_noexpmask, SA_INTERRUPT, 0, "expansion cards", NULL, NULL};static inline unsigned shortecard_getu16(unsigned char *v){ return v[0] | v[1] << 8;}static inline signed longecard_gets24(unsigned char *v){ return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);}static inline ecard_t *slot_to_ecard(unsigned int slot){ return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;}/* ===================== Expansion card daemon ======================== *//* * Since the loader programs on the expansion cards need to be run * in a specific environment, create a separate task with this * environment up, and pass requests to this task as and when we * need to. * * This should allow 99% of loaders to be called from Linux. * * From a security standpoint, we trust the card vendors. This * may be a misplaced trust. */#define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)#define POD_INT_ADDR(x) ((volatile unsigned char *)\ ((BUS_ADDR((x)) - IO_BASE) + IO_START))static voidecard_task_reset(struct ecard_request *req){ if (req->ec == NULL) { ecard_t *ec; for (ec = cards; ec; ec = ec->next) if (ec->loader) ecard_loader_reset(POD_INT_ADDR(ec->podaddr), ec->loader); } else if (req->ec->loader) ecard_loader_reset(POD_INT_ADDR(req->ec->podaddr), req->ec->loader);}static voidecard_task_readbytes(struct ecard_request *req){ unsigned char *buf = (unsigned char *)req->buffer; volatile unsigned char *base_addr = (volatile unsigned char *)POD_INT_ADDR(req->ec->podaddr); unsigned int len = req->length; if (req->ec->slot_no == 8) { /* * The card maintains an index which * increments the address into a 4096-byte * page on each access. We need to keep * track of the counter. */ static unsigned int index; unsigned int offset, page; unsigned char byte = 0; /* keep gcc quiet */ offset = req->address & 4095; page = req->address >> 12; if (page > 256) return; page *= 4; if (offset == 0 || index > offset) { /* * We need to reset the index counter. */ *base_addr = 0; index = 0; } while (index <= offset) { byte = base_addr[page]; index += 1; } while (len--) { *buf++ = byte; if (len) { byte = base_addr[page]; index += 1; } } } else { unsigned int off = req->address; if (!req->use_loader || !req->ec->loader) { off *= 4; while (len--) { *buf++ = base_addr[off]; off += 4; } } else { while(len--) { /* * The following is required by some * expansion card loader programs. */ *(unsigned long *)0x108 = 0; *buf++ = ecard_loader_read(off++, base_addr, req->ec->loader); } } }}#ifdef CONFIG_CPU_32static pid_t ecard_pid;static wait_queue_head_t ecard_wait;static wait_queue_head_t ecard_done;static struct ecard_request *ecard_req;/* to be removed when exec_mmap becomes extern */static int exec_mmap(void){ struct mm_struct * mm, * old_mm; old_mm = current->mm; if (old_mm && atomic_read(&old_mm->mm_users) == 1) { flush_cache_mm(old_mm); mm_release(); exit_mmap(old_mm); flush_tlb_mm(old_mm); return 0; } mm = mm_alloc(); if (mm) { struct mm_struct *active_mm = current->active_mm; current->mm = mm; current->active_mm = mm; activate_mm(active_mm, mm); mm_release(); if (old_mm) { if (active_mm != old_mm) BUG(); mmput(old_mm); return 0; } mmdrop(active_mm); return 0; } return -ENOMEM;}/* * Set up the expansion card * daemon's environment. */static void ecard_init_task(int force){ /* We want to set up the page tables for the following mapping: * Virtual Physical * 0x03000000 0x03000000 * 0x03010000 unmapped * 0x03210000 0x03210000 * 0x03400000 unmapped * 0x08000000 0x08000000 * 0x10000000 unmapped * * FIXME: we don't follow this 100% yet. */ pgd_t *src_pgd, *dst_pgd; unsigned int dst_addr = IO_START; if (!force) exec_mmap(); src_pgd = pgd_offset(current->mm, IO_BASE); dst_pgd = pgd_offset(current->mm, dst_addr); while (dst_addr < IO_START + IO_SIZE) { *dst_pgd++ = *src_pgd++; dst_addr += PGDIR_SIZE; } flush_tlb_range(current->mm, IO_START, IO_START + IO_SIZE); dst_addr = EASI_START; src_pgd = pgd_offset(current->mm, EASI_BASE); dst_pgd = pgd_offset(current->mm, dst_addr); while (dst_addr < EASI_START + EASI_SIZE) { *dst_pgd++ = *src_pgd++; dst_addr += PGDIR_SIZE; } flush_tlb_range(current->mm, EASI_START, EASI_START + EASI_SIZE);}static intecard_task(void * unused){ struct task_struct *tsk = current; tsk->session = 1; tsk->pgrp = 1; /* * We don't want /any/ signals, not even SIGKILL */ sigfillset(&tsk->blocked); sigemptyset(&tsk->pending.signal); recalc_sigpending(tsk); strcpy(tsk->comm, "kecardd"); /* * Set up the environment */ ecard_init_task(0); while (1) { struct ecard_request *req; do { req = xchg(&ecard_req, NULL); if (req == NULL) { sigemptyset(&tsk->pending.signal); interruptible_sleep_on(&ecard_wait); } } while (req == NULL); switch (req->req) { case req_readbytes: ecard_task_readbytes(req); break; case req_reset: ecard_task_reset(req); break; } wake_up(&ecard_done); }}/* * Wake the expansion card daemon to action our request. * * FIXME: The test here is not sufficient to detect if the * kcardd is running. */static inline voidecard_call(struct ecard_request *req){ /* * If we're called from task 0, or from an * interrupt (will be keyboard interrupt), * we forcefully set up the memory map, and * call the loader. We can't schedule, or * sleep for this call. */ if ((current == &init_task || in_interrupt()) && req->req == req_reset && req->ec == NULL) { ecard_init_task(1); ecard_task_reset(req); } else { if (ecard_pid <= 0) ecard_pid = kernel_thread(ecard_task, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND); ecard_req = req; wake_up(&ecard_wait); sleep_on(&ecard_done); }}#else/* * On 26-bit processors, we don't need the kcardd thread to access the * expansion card loaders. We do it directly. */static inline voidecard_call(struct ecard_request *req){ if (req->req == req_reset) ecard_task_reset(req); else ecard_task_readbytes(req);}#endif/* ======================= Mid-level card control ===================== *//* * This is called to reset the loaders for each expansion card on reboot. * * This is required to make sure that the card is in the correct state * that RiscOS expects it to be. */voidecard_reset(int slot){ struct ecard_request req; req.req = req_reset; if (slot < 0) req.ec = NULL; else req.ec = slot_to_ecard(slot); ecard_call(&req);#ifdef HAS_EXPMASK if (have_expmask && slot < 0) { have_expmask |= ~0; EXPMASK_ENABLE = have_expmask; }#endif}static voidecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld){ struct ecard_request req; req.req = req_readbytes; req.ec = ec; req.address = off; req.length = len; req.use_loader = useld; req.buffer = addr; ecard_call(&req);}int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num){ struct ex_chunk_dir excd; int index = 16; int useld = 0; if (!ec->cid.cd) return 0; while(1) { ecard_readbytes(&excd, ec, index, 8, useld); index += 8; if (c_id(&excd) == 0) { if (!useld && ec->loader) { useld = 1; index = 0; continue; } return 0; } if (c_id(&excd) == 0xf0) { /* link */ index = c_start(&excd); continue; } if (c_id(&excd) == 0x80) { /* loader */ if (!ec->loader) { ec->loader = (loader_t)kmalloc(c_len(&excd), GFP_KERNEL); if (ec->loader) ecard_readbytes(ec->loader, ec, (int)c_start(&excd), c_len(&excd), useld); else return 0; } continue; } if (c_id(&excd) == id && num-- == 0) break; } if (c_id(&excd) & 0x80) { switch (c_id(&excd) & 0x70) { case 0x70: ecard_readbytes((unsigned char *)excd.d.string, ec, (int)c_start(&excd), c_len(&excd), useld); break; case 0x00: break; } } cd->start_offset = c_start(&excd); memcpy(cd->d.string, excd.d.string, 256); return 1;}/* ======================= Interrupt control ============================ */static void ecard_def_irq_enable(ecard_t *ec, int irqnr){#ifdef HAS_EXPMASK if (irqnr < 4 && have_expmask) { have_expmask |= 1 << irqnr; EXPMASK_ENABLE = have_expmask; }#endif}static void ecard_def_irq_disable(ecard_t *ec, int irqnr){#ifdef HAS_EXPMASK if (irqnr < 4 && have_expmask) { have_expmask &= ~(1 << irqnr); EXPMASK_ENABLE = have_expmask; }#endif}static int ecard_def_irq_pending(ecard_t *ec){ return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;}static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr){ panic("ecard_def_fiq_enable called - impossible");}static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr){ panic("ecard_def_fiq_disable called - impossible");}static int ecard_def_fiq_pending(ecard_t *ec){ return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;}static expansioncard_ops_t ecard_default_ops = { ecard_def_irq_enable, ecard_def_irq_disable,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -