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

📄 cardctl.c

📁 pcmcia source code
💻 C
📖 第 1 页 / 共 2 页
字号:
/*======================================================================    PCMCIA device control program    cardctl.c 1.67 2001/12/01 01:19:22    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.======================================================================*/#include <sys/types.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <ctype.h>#include <sys/time.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <sys/wait.h>#include <sys/stat.h>#include <pcmcia/version.h>#include <pcmcia/config.h>#include <pcmcia/cs_types.h>#include <pcmcia/cs.h>#include <pcmcia/cistpl.h>#include <pcmcia/ds.h>/*====================================================================*/#ifdef ETCstatic char *configpath = ETC;#elsestatic char *configpath = "/etc/pcmcia";#endifstatic char *scheme, *stabfile;/*====================================================================*/static int major = 0;static int lookup_dev(char *name){    FILE *f;    int n;    char s[32], t[32];        f = fopen("/proc/devices", "r");    if (f == NULL)	return -errno;    while (fgets(s, 32, f) != NULL) {	if (sscanf(s, "%d %s", &n, t) == 2)	    if (strcmp(name, t) == 0)		break;    }    fclose(f);    if (strcmp(name, t) == 0)	return n;    else	return -ENODEV;} /* lookup_dev *//*====================================================================*/static int open_sock(int sock){    static char *paths[] = {	"/var/lib/pcmcia", "/var/run", "/dev", "/tmp", NULL    };    int fd;    char **p, fn[64];    dev_t dev = (major<<8) + sock;    for (p = paths; *p; p++) {	sprintf(fn, "%s/cc-%d", *p, getpid());	if (mknod(fn, (S_IFCHR|S_IREAD|S_IWRITE), dev) == 0) {	    fd = open(fn, O_RDONLY);	    unlink(fn);	    if (fd >= 0)		return fd;	    if (errno == ENODEV)		break;	}    }    return -1;} /* open_sock *//*====================================================================*/static void print_status(cs_status_t *status){    char *v = "5";    if (status->Function == 0) {	printf("  ");	if (status->CardState & CS_EVENT_3VCARD)	    v = "3.3";	else if (status->CardState & CS_EVENT_XVCARD)	    v = "X.X";	if (status->CardState & CS_EVENT_CB_DETECT)	    printf("%sV CardBus card", v);	else if (status->CardState & CS_EVENT_CARD_DETECT)	    printf("%sV 16-bit PC Card", v);	else	    printf("no card");	if (status->CardState & CS_EVENT_PM_SUSPEND)	    printf(" [suspended]");	printf("\n");    }    if ((status->CardState & CS_EVENT_PM_SUSPEND) ||	!(status->CardState & CS_EVENT_CARD_DETECT))	return;    printf("  function %d: ", status->Function);    printf("%s", (status->CardState & CS_EVENT_READY_CHANGE)	   ? "[ready]" : "[busy]");    if (status->CardState & CS_EVENT_WRITE_PROTECT)	printf(", [wp]");    if (status->CardState & CS_EVENT_BATTERY_DEAD)	printf(", [bat dead]");    if (status->CardState & CS_EVENT_BATTERY_LOW)	printf(", [bat low]");    if (status->CardState & CS_EVENT_REQUEST_ATTENTION)	printf(", [req attn]");    printf("\n");} /* print_status *//*====================================================================*/static void print_config(config_info_t *config){    if (config->Function == 0) {	printf("  Vcc %.1fV  Vpp1 %.1fV  Vpp2 %.1fV\n",	       config->Vcc/10.0, config->Vpp1/10.0, config->Vpp2/10.0);	if (!(config->Attributes & CONF_VALID_CLIENT))	    return;	printf("  interface type is ");	switch (config->IntType) {	case INT_MEMORY:	    printf("\"memory-only\"\n"); break;	case INT_MEMORY_AND_IO:	    printf("\"memory and I/O\"\n"); break;	case INT_CARDBUS:	    printf("\"cardbus\"\n"); break;	}	if (config->AssignedIRQ != 0) {	    printf("  irq %d", config->AssignedIRQ);	    switch (config->IRQAttributes & IRQ_TYPE) {	    case IRQ_TYPE_EXCLUSIVE:		printf(" [exclusive]"); break;	    case IRQ_TYPE_TIME:		printf(" [multiplexed]"); break;	    case IRQ_TYPE_DYNAMIC_SHARING:		printf(" [shared]"); break;	    }	    if (config->IRQAttributes & IRQ_PULSE_ALLOCATED)		printf(" [pulse]");	    else		printf(" [level]");	    if (!(config->Attributes & CONF_ENABLE_IRQ))		printf(" [disabled]");	    printf("\n");	}	if (config->Attributes & CONF_ENABLE_DMA)	    printf("  DMA mode is enabled\n");	if (config->Attributes & CONF_ENABLE_SPKR)	    printf("  speaker output is enabled\n");        }    if (!(config->Attributes & CONF_VALID_CLIENT))	return;        printf("  function %d:\n", config->Function);    if (config->CardValues) {	printf("    config base %#06x\n", config->ConfigBase);	printf("    ");	if (config->CardValues & CV_OPTION_VALUE)	    printf("  option 0x%02x", config->Option);	if (config->CardValues & CV_STATUS_VALUE)	    printf(" status 0x%02x", config->Status);	if (config->CardValues & CV_PIN_REPLACEMENT)	    printf(" pin 0x%02x", config->Pin);	if (config->CardValues & CV_COPY_VALUE)	    printf(" copy 0x%02x", config->Copy);	if (config->CardValues & CV_EXT_STATUS)	    printf(" ext 0x%02x", config->ExtStatus);	printf("\n");    }    if (config->NumPorts1 > 0) {	printf("    io %#06x-%#06x", config->BasePort1,	       config->BasePort1 + config->NumPorts1 - 1);	if (config->IntType == INT_CARDBUS) {	    printf(" [32bit]\n");	} else {	    if (config->Attributes1 & IO_SHARED)		printf(" [shared]");	    if (config->Attributes1 & IO_FORCE_ALIAS_ACCESS)		printf(" [alias]");	    switch (config->Attributes1 & IO_DATA_PATH_WIDTH) {	    case IO_DATA_PATH_WIDTH_8:		printf(" [8bit]\n"); break;	    case IO_DATA_PATH_WIDTH_16:		printf(" [16bit]\n"); break;	    case IO_DATA_PATH_WIDTH_AUTO:		printf(" [auto]\n"); break;	    }	}    }    if (config->NumPorts2 > 0) {	printf("    io %#06x-%#06x", config->BasePort2,	       config->BasePort2 + config->NumPorts2 - 1);	if (config->Attributes2 & IO_SHARED)	    printf(" [shared]");	if (config->Attributes2 & IO_FORCE_ALIAS_ACCESS)	    printf(" [alias]");	switch (config->Attributes2 & IO_DATA_PATH_WIDTH) {	case IO_DATA_PATH_WIDTH_8:	    printf(" [8bit]\n"); break;	case IO_DATA_PATH_WIDTH_16:	    printf(" [16bit]\n"); break;	case IO_DATA_PATH_WIDTH_AUTO:	    printf(" [auto]\n"); break;	}    }} /* print_config *//*====================================================================*/static void print_windows(int fd){    ds_ioctl_arg_t arg1, arg2;    int ret;    win_req_t *win = &arg1.win_info.window;    memreq_t *req = &arg2.win_info.map;        ret = ioctl(fd, DS_GET_FIRST_WINDOW, &arg1);    while (ret == 0) {	arg2.win_info.handle = arg1.win_info.handle;	ioctl(fd, DS_GET_MEM_PAGE, &arg2);	printf("  memory 0x%04x-0x%04x @ 0x%08lx",	       req->CardOffset, req->CardOffset+win->Size-1,	       win->Base);	if (win->Attributes & WIN_MEMORY_TYPE_AM)	    printf(" [attr]");	if (!(win->Attributes & WIN_ENABLE))	    printf(" [disabled]");	if (win->Attributes & WIN_USE_WAIT)	    printf(" [wait]");	switch (win->Attributes & WIN_DATA_WIDTH) {	case WIN_DATA_WIDTH_8:	    printf(" [8bit]\n"); break;	case WIN_DATA_WIDTH_16:	    printf(" [16bit]\n"); break;	case WIN_DATA_WIDTH_32:	    printf(" [32bit]\n"); break;	}	ret = ioctl(fd, DS_GET_NEXT_WINDOW, &arg1);    }}/*====================================================================*/static int get_tuple(int fd, cisdata_t code, ds_ioctl_arg_t *arg){    arg->tuple.DesiredTuple = code;    arg->tuple.Attributes = TUPLE_RETURN_COMMON;    arg->tuple.TupleOffset = 0;    if ((ioctl(fd, DS_GET_FIRST_TUPLE, arg) == 0) &&	(ioctl(fd, DS_GET_TUPLE_DATA, arg) == 0) &&	(ioctl(fd, DS_PARSE_TUPLE, arg) == 0))	return 0;    else	return -1;}static void print_ident(int fd){    ds_ioctl_arg_t arg;    cistpl_vers_1_t *vers = &arg.tuple_parse.parse.version_1;    cistpl_manfid_t *manfid = &arg.tuple_parse.parse.manfid;    cistpl_funcid_t *funcid = &arg.tuple_parse.parse.funcid;    config_info_t config;    int i;    static char *fn[] = {	"multifunction", "memory", "serial", "parallel",	"fixed disk", "video", "network", "AIMS", "SCSI"    };    if (get_tuple(fd, CISTPL_VERS_1, &arg) == 0) {	printf("  product info: ");	for (i = 0; i < vers->ns; i++)	    printf("%s\"%s\"", (i>0) ? ", " : "",		   vers->str+vers->ofs[i]);	printf("\n");    } else {	printf("  no product info available\n");    }    if (get_tuple(fd, CISTPL_MANFID, &arg) == 0)	printf("  manfid: 0x%04x, 0x%04x\n",	       manfid->manf, manfid->card);    if (get_tuple(fd, CISTPL_FUNCID, &arg) == 0)	printf("  function: %d (%s)\n", funcid->func,	       fn[funcid->func]);    config.Function = config.ConfigBase = 0;    if ((ioctl(fd, DS_GET_CONFIGURATION_INFO, &config) == 0) &&	(config.IntType == INT_CARDBUS) && config.ConfigBase)	printf("  PCI id: 0x%04x, 0x%04x\n",	       config.ConfigBase & 0xffff,	       config.ConfigBase >> 16);}/*====================================================================*/typedef enum cmd_t {    C_STATUS, C_CONFIG, C_IDENT, C_SUSPEND,    C_RESUME, C_RESET, C_EJECT, C_INSERT} cmd_t;static char *cmdname[] = {    "status", "config", "ident", "suspend",    "resume", "reset", "eject", "insert"};#define NCMD (sizeof(cmdname)/sizeof(char *))static int do_cmd(int fd, int cmd){

⌨️ 快捷键说明

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