⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bulkmem.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*======================================================================    PCMCIA Bulk Memory Services    bulkmem.c 1.38 2000/09/25 19:29:51    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 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/module.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/malloc.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/timer.h>#define IN_CARD_SERVICES#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"/*======================================================================    This function handles submitting an MTD request, and retrying    requests when an MTD is busy.    An MTD request should never block.    ======================================================================*/static int do_mtd_request(memory_handle_t handle, mtd_request_t *req,			  caddr_t buf){    int ret, tries;    client_t *mtd;    socket_info_t *s;        mtd = handle->mtd;    if (mtd == NULL)	return CS_GENERAL_FAILURE;    s = SOCKET(mtd);    for (ret = tries = 0; tries < 100; tries++) {	mtd->event_callback_args.mtdrequest = req;	mtd->event_callback_args.buffer = buf;	ret = EVENT(mtd, CS_EVENT_MTD_REQUEST, CS_EVENT_PRI_LOW);	if (ret != CS_BUSY)	    break;	switch (req->Status) {	case MTD_WAITREQ:	    /* Not that we should ever need this... */	    interruptible_sleep_on_timeout(&mtd->mtd_req, HZ);	    break;	case MTD_WAITTIMER:	case MTD_WAITRDY:	    interruptible_sleep_on_timeout(&mtd->mtd_req, req->Timeout*HZ/1000);	    req->Function |= MTD_REQ_TIMEOUT;	    break;	case MTD_WAITPOWER:	    interruptible_sleep_on(&mtd->mtd_req);	    break;	}	if (signal_pending(current))	    printk(KERN_NOTICE "cs: do_mtd_request interrupted!\n");    }    if (tries == 20) {	printk(KERN_NOTICE "cs: MTD request timed out!\n");	ret = CS_GENERAL_FAILURE;    }    wake_up_interruptible(&mtd->mtd_req);    retry_erase_list(&mtd->erase_busy, 0);    return ret;} /* do_mtd_request *//*======================================================================    This stuff is all for handling asynchronous erase requests.  It    is complicated because all the retry stuff has to be dealt with    in timer interrupts or in the card status event handler.======================================================================*/static void insert_queue(erase_busy_t *head, erase_busy_t *entry){    DEBUG(2, "cs: adding 0x%p to queue 0x%p\n", entry, head);    entry->next = head;    entry->prev = head->prev;    head->prev->next = entry;    head->prev = entry;}static void remove_queue(erase_busy_t *entry){    DEBUG(2, "cs: unqueueing 0x%p\n", entry);    entry->next->prev = entry->prev;    entry->prev->next = entry->next;}static void retry_erase(erase_busy_t *busy, u_int cause){    eraseq_entry_t *erase = busy->erase;    mtd_request_t req;    client_t *mtd;    socket_info_t *s;    int ret;    DEBUG(2, "cs: trying erase request 0x%p...\n", busy);    if (busy->next)	remove_queue(busy);    req.Function = MTD_REQ_ERASE | cause;    req.TransferLength = erase->Size;    req.DestCardOffset = erase->Offset + erase->Handle->info.CardOffset;    req.MediaID = erase->Handle->MediaID;    mtd = erase->Handle->mtd;    s = SOCKET(mtd);    mtd->event_callback_args.mtdrequest = &req;    ret = EVENT(mtd, CS_EVENT_MTD_REQUEST, CS_EVENT_PRI_LOW);    if (ret == CS_BUSY) {	DEBUG(2, "  Status = %d, requeueing.\n", req.Status);	switch (req.Status) {	case MTD_WAITREQ:	case MTD_WAITPOWER:	    insert_queue(&mtd->erase_busy, busy);	    break;	case MTD_WAITTIMER:	case MTD_WAITRDY:	    if (req.Status == MTD_WAITRDY)		insert_queue(&s->erase_busy, busy);	    mod_timer(&busy->timeout, jiffies + req.Timeout*HZ/1000);	    break;	}    } else {	/* update erase queue status */	DEBUG(2, "  Ret = %d\n", ret);	switch (ret) {	case CS_SUCCESS:	    erase->State = ERASE_PASSED; break;	case CS_WRITE_PROTECTED:	    erase->State = ERASE_MEDIA_WRPROT; break;	case CS_BAD_OFFSET:	    erase->State = ERASE_BAD_OFFSET; break;	case CS_BAD_SIZE:	    erase->State = ERASE_BAD_SIZE; break;	case CS_NO_CARD:	    erase->State = ERASE_BAD_SOCKET; break;	default:	    erase->State = ERASE_FAILED; break;	}	busy->client->event_callback_args.info = erase;	EVENT(busy->client, CS_EVENT_ERASE_COMPLETE, CS_EVENT_PRI_LOW);	kfree(busy);	/* Resubmit anything waiting for a request to finish */	wake_up_interruptible(&mtd->mtd_req);	retry_erase_list(&mtd->erase_busy, 0);    }} /* retry_erase */void retry_erase_list(erase_busy_t *list, u_int cause){    erase_busy_t tmp = *list;    DEBUG(2, "cs: rescanning erase queue list 0x%p\n", list);    if (list->next == list)	return;    /* First, truncate the original list */    list->prev->next = &tmp;    list->next->prev = &tmp;    list->prev = list->next = list;    tmp.prev->next = &tmp;    tmp.next->prev = &tmp;    /* Now, retry each request, in order. */    while (tmp.next != &tmp)	retry_erase(tmp.next, cause);} /* retry_erase_list */static void handle_erase_timeout(u_long arg){    DEBUG(0, "cs: erase timeout for entry 0x%lx\n", arg);    retry_erase((erase_busy_t *)arg, MTD_REQ_TIMEOUT);}static void setup_erase_request(client_handle_t handle, eraseq_entry_t *erase){    erase_busy_t *busy;    region_info_t *info;        if (CHECK_REGION(erase->Handle))	erase->State = ERASE_BAD_SOCKET;    else {	info = &erase->Handle->info;	if ((erase->Offset >= info->RegionSize) ||	    (erase->Offset & (info->BlockSize-1)))	    erase->State = ERASE_BAD_OFFSET;	else if ((erase->Offset+erase->Size > info->RegionSize) ||		 (erase->Size & (info->BlockSize-1)))	    erase->State = ERASE_BAD_SIZE;	else {	    erase->State = 1;	    busy = kmalloc(sizeof(erase_busy_t), GFP_KERNEL);	    busy->erase = erase;	    busy->client = handle;	    init_timer(&busy->timeout);	    busy->timeout.data = (u_long)busy;	    busy->timeout.function = &handle_erase_timeout;	    busy->prev = busy->next = NULL;	    retry_erase(busy, 0);	}    }} /* setup_erase_request *//*======================================================================    MTD helper functions======================================================================*/static int mtd_modify_window(window_handle_t win, mtd_mod_win_t *req){    if ((win == NULL) || (win->magic != WINDOW_MAGIC))	return CS_BAD_HANDLE;    win->ctl.flags = MAP_16BIT | MAP_ACTIVE;    if (req->Attributes & WIN_USE_WAIT)	win->ctl.flags |= MAP_USE_WAIT;    if (req->Attributes & WIN_MEMORY_TYPE)	win->ctl.flags |= MAP_ATTRIB;    win->ctl.speed = req->AccessSpeed;    win->ctl.card_start = req->CardOffset;    win->sock->ss_entry->set_mem_map(win->sock->sock, &win->ctl);    return CS_SUCCESS;}static int mtd_set_vpp(client_handle_t handle, mtd_vpp_req_t *req){    socket_info_t *s;    if (CHECK_HANDLE(handle))	return CS_BAD_HANDLE;    if (req->Vpp1 != req->Vpp2)	return CS_BAD_VPP;    s = SOCKET(handle);    s->socket.Vpp = req->Vpp1;    if (s->ss_entry->set_socket(s->sock, &s->socket))	return CS_BAD_VPP;    return CS_SUCCESS;}static int mtd_rdy_mask(client_handle_t handle, mtd_rdy_req_t *req){    socket_info_t *s;    if (CHECK_HANDLE(handle))	return CS_BAD_HANDLE;    s = SOCKET(handle);    if (req->Mask & CS_EVENT_READY_CHANGE)	s->socket.csc_mask |= SS_READY;    else	s->socket.csc_mask &= ~SS_READY;    if (s->ss_entry->set_socket(s->sock, &s->socket))	return CS_GENERAL_FAILURE;    return CS_SUCCESS;}int MTDHelperEntry(int func, void *a1, void *a2){    switch (func) {    case MTDRequestWindow:    {	window_handle_t w;        int ret = pcmcia_request_window(a1, a2, &w);        (window_handle_t *)a1 = w;	return  ret;    }        break;    case MTDReleaseWindow:	return pcmcia_release_window(a1);    case MTDModifyWindow:	return mtd_modify_window(a1, a2); break;    case MTDSetVpp:	return mtd_set_vpp(a1, a2); break;    case MTDRDYMask:	return mtd_rdy_mask(a1, a2); break;    default:	return CS_UNSUPPORTED_FUNCTION; break;    }} /* MTDHelperEntry *//*======================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -