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

📄 cardmgr.c

📁 pcmcia source code
💻 C
📖 第 1 页 / 共 3 页
字号:
/*======================================================================    PCMCIA Card Manager daemon    cardmgr.c 1.176 2002/07/03 06:45:54    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 <syslog.h>#include <getopt.h>#include <signal.h>#include <sys/time.h>#include <sys/ioctl.h>#include <sys/wait.h>#include <sys/stat.h>#include <sys/utsname.h>#include <sys/file.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>#include "cardmgr.h"/*====================================================================*/typedef struct socket_info_t {    int			fd;    int			state;    card_info_t		*card;    bind_info_t		*bind[MAX_BINDINGS];    mtd_ident_t		*mtd[2*CISTPL_MAX_DEVICES];} socket_info_t;#define SOCKET_PRESENT	0x01#define SOCKET_READY	0x02#define SOCKET_HOTPLUG	0x04/* Linked list of resource adjustments */struct adjust_list_t *root_adjust = NULL;/* Linked list of device definitions */struct device_info_t *root_device = NULL;/* Special pointer to "anonymous" card definition */struct card_info_t *blank_card = NULL;/* Linked list of card definitions */struct card_info_t *root_card = NULL;/* Linked list of function definitions */struct card_info_t *root_func = NULL;/* Linked list of MTD definitions */struct mtd_ident_t *root_mtd = NULL;/* Default MTD */struct mtd_ident_t *default_mtd = NULL;static int sockets;static struct socket_info_t socket[MAX_SOCKS];/* Default path for config file, device scripts */#ifdef ETCstatic char *configpath = ETC;#elsestatic char *configpath = "/etc/pcmcia";#endif/* Default path for pid file */static char *pidfile = "/var/run/cardmgr.pid";/* Default path for finding modules */static char *modpath = NULL;/* Default path for socket info table */static char *stabfile;/* If set, don't generate beeps when cards are inserted */static int be_quiet = 0;/* If set, use modprobe instead of insmod */static int do_modprobe = 0;/* If set, configure already inserted cards, then exit */static int one_pass = 0;/* Extra message logging? */static int verbose = 0;/*====================================================================*/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;}int open_dev(dev_t dev, int mode){    static char *paths[] = {	"/var/lib/pcmcia", "/var/run", "/dev", "/tmp", NULL    };    static int nd = 0;    char **p, fn[64];    int fd;    int o_mode = (mode & S_IWRITE) ? O_RDWR : O_RDONLY;    for (p = paths; *p; p++) {	sprintf(fn, "%s/cm-%d-%d", *p, getpid(), nd++);	if (mknod(fn, mode, dev) == 0) {	    fd = open(fn, o_mode);	    if (fd < 0)		fd = open(fn, O_NONBLOCK|o_mode);	    unlink(fn);	    if (fd >= 0)		return fd;	    if (errno == ENODEV)		break;	}    }    return -1;}int open_sock(int sock, int mode){    dev_t dev = (major<<8)+sock;    return open_dev(dev, mode);}/*======================================================================    xlate_scsi_name() is a sort-of-hack used to deduce the minor    device numbers of SCSI devices, from the information available to    the low-level driver.    ======================================================================*/#include <linux/major.h>#include <scsi/scsi.h>#define VERSION(v,p,s) (((v)<<16)+(p<<8)+s)#if (LINUX_VERSION_CODE < VERSION(2,1,126))#define SCSI_DISK0_MAJOR SCSI_DISK_MAJOR#endifstatic int xlate_scsi_name(bind_info_t *bind){    int i, fd, mode, minor;    u_long arg[2], id1, id2;    id1 = strtol(bind->name+3, NULL, 16);    if ((bind->major == SCSI_DISK0_MAJOR) ||	(bind->major == SCSI_CDROM_MAJOR))	mode = S_IREAD|S_IFBLK;    else	mode = S_IREAD|S_IFCHR;        for (i = 0; i < 16; i++) {	minor = (bind->major == SCSI_DISK0_MAJOR) ? (i<<4) : i;	fd = open_dev((bind->major<<8)+minor, mode);	if (fd < 0)	    continue;	if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, arg) == 0) {	    id2 = (arg[0]&0x0f) + ((arg[0]>>4)&0xf0) +		((arg[0]>>8)&0xf00) + ((arg[0]>>12)&0xf000);	    if (id1 == id2) {		close(fd);		switch (bind->major) {		case SCSI_DISK0_MAJOR:		case SCSI_GENERIC_MAJOR:		    sprintf(bind->name+2, "%c", 'a'+i); break;		case SCSI_CDROM_MAJOR:		    sprintf(bind->name, "scd%d", i); break;		case SCSI_TAPE_MAJOR:		    sprintf(bind->name+2, "%d", i); break;		}		bind->minor = minor;		return 0;	    }	}	close(fd);    }    return -1;}/*====================================================================*/#define BEEP_TIME 150#define BEEP_OK   1000#define BEEP_WARN 2000#define BEEP_ERR  4000#include <sys/kd.h>static void beep(unsigned int ms, unsigned int freq){    int fd, arg;    if (be_quiet)	return;    fd = open("/dev/console", O_RDWR);    if (fd < 0)	return;    arg = (ms << 16) | freq;    ioctl(fd, KDMKTONE, arg);    close(fd);    usleep(ms*1000);}/*====================================================================*/static void write_pid(void){    FILE *f;    f = fopen(pidfile, "w");    if (f == NULL)	syslog(LOG_WARNING, "could not open %s: %m", pidfile);    else {	fprintf(f, "%d\n", getpid());	fclose(f);    }}static void write_stab(void){    int i, j, k;    FILE *f;    socket_info_t *s;    bind_info_t *bind;    f = fopen(stabfile, "w");    if (f == NULL) {	syslog(LOG_WARNING, "fopen(stabfile) failed: %m");	return;    }    if (flock(fileno(f), LOCK_EX) != 0) {	syslog(LOG_ERR, "flock(stabfile) failed: %m");	return;    }    for (i = 0; i < sockets; i++) {	s = &socket[i];	fprintf(f, "Socket %d: ", i);	if (!(s->state & SOCKET_PRESENT)) {	    fprintf(f, "empty\n");	} else if (s->state & SOCKET_HOTPLUG) {	    fprintf(f, "CardBus hotplug device\n");	} else if (!s->card) {	    fprintf(f, "unsupported card\n");	} else {	    fprintf(f, "%s\n", s->card->name);	    for (j = 0; j < s->card->bindings; j++)		for (k = 0, bind = s->bind[j];		     bind != NULL;		     k++, bind = bind->next) {		    char *class = s->card->device[j]->class;		    fprintf(f, "%d\t%s\t%s\t%d\t%s",			    i, (class ? class : "none"),			    bind->dev_info, k, bind->name);		    if (bind->major)			fprintf(f, "\t%d\t%d\n",				bind->major, bind->minor);		    else			fputc('\n', f);		}	}    }    fflush(f);    flock(fileno(f), LOCK_UN);    fclose(f);}/*====================================================================*/static int get_tuple(int ns, cisdata_t code, ds_ioctl_arg_t *arg){    socket_info_t *s = &socket[ns];        arg->tuple.DesiredTuple = code;    arg->tuple.Attributes = 0;    if (ioctl(s->fd, DS_GET_FIRST_TUPLE, arg) != 0)	return -1;    arg->tuple.TupleOffset = 0;    if (ioctl(s->fd, DS_GET_TUPLE_DATA, arg) != 0) {	syslog(LOG_INFO, "error reading CIS data on socket %d: %m", ns);	return -1;    }    if (ioctl(s->fd, DS_PARSE_TUPLE, arg) != 0) {	syslog(LOG_INFO, "error parsing CIS on socket %d: %m", ns);	return -1;    }    return 0;}/*======================================================================    Code to fetch a 2.4 kernel's hot plug PCI driver list    This is distasteful but is the best I could come up with.======================================================================*/typedef struct pci_id {    u_short vendor, device;    struct pci_id *next;} pci_id_t;static int get_pci_id(int ns, pci_id_t *id){    socket_info_t *s = &socket[ns];    config_info_t config;    config.Function = config.ConfigBase = 0;    if ((ioctl(s->fd, DS_GET_CONFIGURATION_INFO, &config) != 0) ||	(config.IntType != INT_CARDBUS) || !config.ConfigBase)	return 0;    id->vendor = config.ConfigBase & 0xffff;    id->device = config.ConfigBase >> 16;    return 1;}/*====================================================================*/static void log_card_info(cistpl_vers_1_t *vers,			  cistpl_manfid_t *manfid,			  cistpl_funcid_t *funcid,			  pci_id_t *pci_id){    char v[256] = "";    int i;    static char *fn[] = {	"multi", "memory", "serial", "parallel", "fixed disk",	"video", "network", "AIMS", "SCSI"    };        if (vers) {	for (i = 0; i < vers->ns; i++)	    sprintf(v+strlen(v), "%s\"%s\"",		    (i>0) ? ", " : "", vers->str+vers->ofs[i]);	syslog(LOG_INFO, "  product info: %s", v);    } else {	syslog(LOG_INFO, "  no product info available");    }    *v = '\0';    if (manfid->manf != 0)	sprintf(v, "  manfid: 0x%04x, 0x%04x",		manfid->manf, manfid->card);    if (funcid->func != 0xff)	sprintf(v+strlen(v), "  function: %d (%s)", funcid->func,		fn[funcid->func]);    if (strlen(v) > 0) syslog(LOG_INFO, "%s", v);    if (pci_id->vendor != 0)	syslog(LOG_INFO, "  PCI id: 0x%04x, 0x%04x",	       pci_id->vendor, pci_id->device);}static card_info_t *lookup_card(int ns){    socket_info_t *s = &socket[ns];    card_info_t *card = NULL;    ds_ioctl_arg_t arg;    cistpl_vers_1_t *vers = NULL;    cistpl_manfid_t manfid = { 0, 0 };    pci_id_t pci_id = { 0, 0 };    cistpl_funcid_t funcid = { 0xff, 0xff };    cs_status_t status;    int i, ret, has_cis = 0;    /* Do we have a CIS structure? */    ret = ioctl(s->fd, DS_VALIDATE_CIS, &arg);    has_cis = ((ret == 0) && (arg.cisinfo.Chains > 0));        /* Try to read VERS_1, MANFID tuples */    if (has_cis) {	/* rule of thumb: cards with no FUNCID, but with common memory	   device geometry information, are probably memory cards */	if (get_tuple(ns, CISTPL_FUNCID, &arg) == 0)	    memcpy(&funcid, &arg.tuple_parse.parse.funcid,		   sizeof(funcid));	else if (get_tuple(ns, CISTPL_DEVICE_GEO, &arg) == 0)	    funcid.func = CISTPL_FUNCID_MEMORY;	if (get_tuple(ns, CISTPL_MANFID, &arg) == 0)	    memcpy(&manfid, &arg.tuple_parse.parse.manfid,		   sizeof(manfid));	if (get_tuple(ns, CISTPL_VERS_1, &arg) == 0)	    vers = &arg.tuple_parse.parse.version_1;	for (card = root_card; card; card = card->next) {	    if (card->ident_type &		~(VERS_1_IDENT|MANFID_IDENT|TUPLE_IDENT))		continue;	    if (card->ident_type & VERS_1_IDENT) {		if (vers == NULL)		    continue;		for (i = 0; i < card->id.vers.ns; i++) {		    if (strcmp(card->id.vers.pi[i], "*") == 0)			continue;		    if (i >= vers->ns)			break;		    if (strcmp(card->id.vers.pi[i],			       vers->str+vers->ofs[i]) != 0)			break;		}		if (i < card->id.vers.ns)		    continue;	    }	    	    if (card->ident_type & MANFID_IDENT) {		if ((manfid.manf != card->manfid.manf) ||		    (manfid.card != card->manfid.card))		    continue;	    }			    if (card->ident_type & TUPLE_IDENT) {		arg.tuple.DesiredTuple = card->id.tuple.code;		arg.tuple.Attributes = 0;		ret = ioctl(s->fd, DS_GET_FIRST_TUPLE, &arg);		if (ret != 0) continue;		arg.tuple.TupleOffset = card->id.tuple.ofs;		ret = ioctl(s->fd, DS_GET_TUPLE_DATA, &arg);		if (ret != 0) continue;		if (strncmp((char *)arg.tuple_parse.data,			    card->id.tuple.info,			    strlen(card->id.tuple.info)) != 0)		    continue;	    }	    break; /* we have a match */	}    }    /* Check PCI vendor/device info */    status.Function = 0;    ioctl(s->fd, DS_GET_STATUS, &status);    if (status.CardState & CS_EVENT_CB_DETECT) {	if (get_pci_id(ns, &pci_id)) {	    if (!card) {		for (card = root_card; card; card = card->next)		    if ((card->ident_type == PCI_IDENT) &&			(pci_id.vendor == card->manfid.manf) &&			(pci_id.device == card->manfid.card))

⌨️ 快捷键说明

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