summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/pcmcia/cardmgr.c
blob: f461ed1806946d1353b8435037c711a1bc517b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
 * Guillaume Cottenceau (gc@mandrakesoft.com)
 *
 * Copyright 2000 MandrakeSoft
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* Code comes from /anonymous@projects.sourceforge.net:/pub/pcmcia-cs/pcmcia-cs-3.1.23.tar.bz2
 *
 *   Licence of this code follows:

    PCMCIA Card Manager daemon

    cardmgr.c 1.150 2000/12/14 17:12:59

    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.
    
 */


#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.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 "../log.h"
#include "modules.h"

#include "cardmgr.h"
#include "pcmcia.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_BOUND	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];

static char *configpath = "/etc/pcmcia";

/* Default path for socket info table */
static char *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) {
		bzero(t, 32);
		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;
}

static int open_dev(dev_t dev, int mode)
{
	char * fn = "/tmp/cardmgr_tmp";
	int fd;
	unlink(fn);
	if (mknod(fn, mode, dev) != 0)
		return -1;
	fd = open(fn, (mode&S_IWRITE) ? O_RDWR : O_RDONLY);
	if (fd < 0)
		fd = open(fn, O_NONBLOCK|((mode&S_IWRITE) ? O_RDWR : O_RDONLY));
	unlink(fn);
	return fd;
}

static 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>

static 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;
}


/*====================================================================*/

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) {
		log_message("CM: fopen(stabfile) failed: %m");
		return;
	}
	if (flock(fileno(f), LOCK_EX) != 0) {
		log_message("CM: flock(stabfile) failed: %m");
		return;
	}
	for (i = 0; i < sockets; i++) {
		s = &socket[i];
		if (!(s->state & SOCKET_PRESENT))
			fprintf(f, "Socket %d: empty\n", i);
		else if (!s->card)
			fprintf(f, "Socket %d: unsupported card\n", i);
		else {
			fprintf(f, "Socket %d: %s\n", i, 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);
	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) {
		log_message("CM: error reading CIS data on socket %d: %m", ns);
		return -1;
	}
	if (ioctl(s->fd, DS_PARSE_TUPLE, arg) != 0) {
		log_message("CM: error parsing CIS on socket %d: %m", ns);
		return -1;
	}
	return 0;
}

/*====================================================================*/

typedef struct {
	u_short vendor, device;
} pci_id_t;


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;
	config_info_t config;
	int i, ret, match;
	int 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;

		match = 0;
		for (card = root_card; card; card = card->next) {
			switch (card->ident_type) {
		
			case VERS_1_IDENT:
				if (vers == NULL)
					break;
				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)
					break;
				match = 1;
				break;

			case MANFID_IDENT:
				if ((manfid.manf == card->id.manfid.manf) &&
				    (manfid.card == card->id.manfid.card))
					match = 1;
				break;
		
			case 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) break;
				arg.tuple.TupleOffset = card->id.tuple.ofs;
				ret = ioctl(s->fd, DS_GET_TUPLE_DATA, &arg);
				if (ret != 0) break;
				if (strncmp((char *)arg.tuple_parse.data,
					    card->id.tuple.info,
					    strlen(card->id.tuple.info)) != 0)
					break;
				match = 1;
				break;

			default:
				/* Skip */
				break;
			}
			if (match) break;
		}
	}

	/* Check PCI vendor/device info */
	status.Function = config.Function = config.ConfigBase = 0;
	if ((ioctl(s->fd, DS_GET_CONFIGURATION_INFO, &config) == 0) &&
	    (config.IntType == INT_CARDBUS)) {
		pci_id.vendor = config.ConfigBase & 0xffff;
		pci_id.device = config.ConfigBase >> 16;
		if (!card) {
			for (card = root_card; card; card = card->next)
				if ((card->ident_type == PCI_IDENT) &&
				    (pci_id.vendor == card->id.manfid.manf) &&
				    (pci_id.device == card->id.manfid.card))
					break;
		}
	}
    
	/* Try for a FUNCID match */
	if (!card && (funcid.func != 0xff)) {
		for (card = root_func; card; card = card->next)
			if (card->id.func.funcid == funcid.func)
				break;
	}

	if (card) {
		log_message("CM: socket %d: %s", ns, card->name);
		return card;
	}

	status.Function = 0;
	if (!blank_card || (status.CardState & CS_EVENT_CB_DETECT) ||
	    manfid.manf || manfid.card || pci_id.vendor || vers) {
		log_message("CM: unsupported card in socket %d", ns);
		return NULL;
	} else {
		card = blank_card;
		log_message("CM: socket %d: %s", ns, card->name);
		return card;
	}
}


static void cardmgr_fail(void)
{
	log_message("CM: cardmgr: failed");
}
	
/*====================================================================*/

static int load_config(void)
{
	if (chdir(configpath)) {
		log_message("CM: chdir to %s failed: %m", configpath);
		return -1;
	}

	if (parse_configfile("config"))
		return -1;

	if (!root_device)
		log_message("CM: no device drivers defined");

	if (!root_card && !root_func)
		log_message("CM: no cards defined");

	return 0;
}


/*====================================================================*/

static void install_module(char *mod, char *opts)
{
	my_insmod(mod, ANY_DRIVER_TYPE, opts);
}

/*====================================================================*/

static mtd_ident_t *lookup_mtd(region_info_t *region)
{
	mtd_ident_t *mtd;
	int match = 0;
    
	for (mtd = root_mtd; mtd; mtd = mtd->next) {
		switch (mtd->mtd_type) {
		case JEDEC_MTD:
			if ((mtd->jedec_mfr == region->JedecMfr) &&
			    (mtd->jedec_info == region->JedecInfo)) {
				match = 1;
				break;
			}
		case DTYPE_MTD:
			break;
		default:
			break;
		}
		if (match) break;
	}
	if (mtd)
		return mtd;
	else
		return default_mtd;
}

/*====================================================================*/

static void bind_mtd(int sn)
{
	socket_info_t *s = &socket[sn];
	region_info_t region;
	bind_info_t bind;
	mtd_info_t mtd_info;
	mtd_ident_t *mtd;
	int i, attr, ret, nr;

	nr = 0;
	for (attr = 0; attr < 2; attr++) {
		region.Attributes = attr;
		ret = ioctl(s->fd, DS_GET_FIRST_REGION, &region);
		while (ret == 0) {
			mtd = lookup_mtd(&region);
			if (mtd) {
				/* Have we seen this MTD before? */
				for (i = 0; i < nr; i++)
					if (s->mtd[i] == mtd) break;
				if (i == nr) {
					install_module(mtd->module, mtd->opts);
					s->mtd[nr] = mtd;
					mtd->refs++;
					nr++;
				}
				log_message("CM:   %s memory region at 0x%lx: %s",
					    attr ? "Attribute" : "Common", (long unsigned int) region.CardOffset,
					    mtd->name);
				/* Bind MTD to this region */
				strcpy(mtd_info.dev_info, s->mtd[i]->module);
				mtd_info.Attributes = region.Attributes;
				mtd_info.CardOffset = region.CardOffset;
				if (ioctl(s->fd, DS_BIND_MTD, &mtd_info) != 0) {
					log_message(  "bind MTD '%s' to region at 0x%lx failed: %m",
						      (char *)mtd_info.dev_info, (long unsigned int) region.CardOffset);
				}
			}
			ret = ioctl(s->fd, DS_GET_NEXT_REGION, &region);
		}
	}
	s->mtd[nr] = NULL;
    
	/* Now bind each unique MTD as a normal client of this socket */
	for (i = 0; i < nr; i++) {
		strcpy(bind.dev_info, s->mtd[i]->module);
		bind.function = 0;
		if (ioctl(s->fd, DS_BIND_REQUEST, &bind) != 0)
			log_message("CM: bind MTD '%s' to socket %d failed: %m",
				    (char *)bind.dev_info, sn);
	}
}

/*====================================================================*/

static void update_cis(socket_info_t *s)
{
	cisdump_t cis;
	FILE *f = fopen(s->card->cis_file, "r");
	if (f == NULL)
		log_message("CM: could not open '%s': %m", s->card->cis_file);
	else {
		cis.Length = fread(cis.Data, 1, CISTPL_MAX_CIS_SIZE, f);
		fclose(f);
		if (ioctl(s->fd, DS_REPLACE_CIS, &cis) != 0)
			log_message("CM: could not replace CIS: %m");
	}
}

/*====================================================================*/

static void do_insert(int sn)
{
	socket_info_t *s = &socket[sn];
	card_info_t *card;
	device_info_t **dev;
	bind_info_t *bind, **tail;
	int i, j, ret;

	/* Already identified? */
	if (s->card && (s->card != blank_card))
		return;
    
	log_message("CM: initializing socket %d", sn);
	card = lookup_card(sn);
	/* Make sure we've learned something new before continuing */
	if (card == s->card)
		return;
	s->card = card;
	card->refs++;
	if (card->cis_file)
		update_cis(s);

	dev = card->device;

	/* Set up MTD's */
	for (i = 0; i < card->bindings; i++)
		if (dev[i]->needs_mtd)
			break;

	if (i < card->bindings)
		bind_mtd(sn);

	/* Install kernel modules */
	for (i = 0; i < card->bindings; i++) {
		dev[i]->refs++;
		for (j = 0; j < dev[i]->modules; j++)
			install_module(dev[i]->module[j], dev[i]->opts[j]);
	}
    
	/* Bind drivers by their dev_info identifiers */
	for (i = 0; i < card->bindings; i++) {
		bind = calloc(1, sizeof(bind_info_t));
		strcpy((char *)bind->dev_info, (char *)dev[i]->dev_info);
		if (strcmp(bind->dev_info, "cb_enabler") == 0)
			bind->function = BIND_FN_ALL;
		else
			bind->function = card->dev_fn[i];
		if (ioctl(s->fd, DS_BIND_REQUEST, bind) != 0) {
			if (errno == EBUSY) {
				log_message("CM: '%s' already bound to socket %d",
					    (char *)bind->dev_info, sn);
			} else {
				log_message("CM: bind '%s' to socket %d failed: %m",
					    (char *)bind->dev_info, sn);
				write_stab();
				return;
			}
		}

		for (ret = j = 0; j < 10; j++) {
			ret = ioctl(s->fd, DS_GET_DEVICE_INFO, bind);
			if ((ret == 0) || (errno != EAGAIN))
				break;
			usleep(100000);
		}
		if (ret != 0) {
			log_message("CM: get dev info on socket %d failed: %m",
				    sn);
			ioctl(s->fd, DS_UNBIND_REQUEST, bind);
			write_stab();
			return;
		}
		tail = &s->bind[i];
		while (ret == 0) {
			bind_info_t *old;
			if ((strlen(bind->name) > 3) && (bind->name[2] == '#'))
				xlate_scsi_name(bind);
			old = *tail = bind; tail = (bind_info_t **)&bind->next;
			bind = (bind_info_t *)malloc(sizeof(bind_info_t));
			memcpy(bind, old, sizeof(bind_info_t));
			ret = ioctl(s->fd, DS_GET_NEXT_DEVICE, bind);
		}
		*tail = NULL; free(bind);
		write_stab();
	}
}

/*====================================================================*/

static void wait_for_pending(void)
{
	cs_status_t status;
	int i;
	status.Function = 0;
	for (;;) {
		usleep(100000);
		for (i = 0; i < sockets; i++)
			if ((ioctl(socket[i].fd, DS_GET_STATUS, &status) == 0) && (status.CardState & CS_EVENT_CARD_INSERTION))
				break;
		if (i == sockets)
			break;
	}
}

/*====================================================================*/

static void adjust_resources(void)
{
	adjust_list_t *al;
	int ret;
	char tmp[64];
	int fd = socket[0].fd;
    
	for (al = root_adjust; al; al = al->next) {
		ret = ioctl(fd, DS_ADJUST_RESOURCE_INFO, &al->adj);
		if (ret != 0) {
			switch (al->adj.Resource) {
			case RES_MEMORY_RANGE:
				sprintf(tmp, "memory %#lx-%#lx",
					al->adj.resource.memory.Base,
					al->adj.resource.memory.Base +
					al->adj.resource.memory.Size - 1);
				break;
			case RES_IO_RANGE:
				sprintf(tmp, "IO ports %#x-%#x",
					al->adj.resource.io.BasePort,
					al->adj.resource.io.BasePort +
					al->adj.resource.io.NumPorts - 1);
				break;
			case RES_IRQ:
				sprintf(tmp, "irq %u", al->adj.resource.irq.IRQ);
				break;
			}
			log_message("CM: could not adjust resource: %s: %m", tmp);
		}
	}
}
    
/*====================================================================*/

static int init_sockets(void)
{
	int fd, i;
	servinfo_t serv;

	major = lookup_dev("pcmcia");
	if (major < 0) {
		if (major == -ENODEV)
			log_message("CM: no pcmcia driver in /proc/devices");
		else
			log_message("CM: could not open /proc/devices: %m");
		return -1;
	}

	for (fd = -1, i = 0; i < MAX_SOCKS; i++) {
		fd = open_sock(i, S_IFCHR|S_IREAD|S_IWRITE);
		if (fd < 0)
			break;
		socket[i].fd = fd;
		socket[i].state = 0;
	}

	if ((fd < 0) && (errno != ENODEV) && (errno != ENOENT))
		log_message("CM: open_sock(socket %d) failed: %m", i);
	sockets = i;
	if (sockets == 0) {
		log_message("CM: no sockets found!");
		return -1;
	} else
		log_message("CM: found %d sockets", sockets);

	if (ioctl(socket[0].fd, DS_GET_CARD_SERVICES_INFO, &serv) == 0) {
		if (serv.Revision != CS_RELEASE_CODE)
			log_message("CM: warning, Card Services release does not match kernel");
	} else {
		log_message("CM: could not get CS revision info!");
		return -1;
	}
	adjust_resources();
	return 0;
}

/*====================================================================*/

int cardmgr_call(void)
{
	int i, max_fd, ret, event;
	struct timeval tv;
	fd_set fds;
	
	stabfile = "/var/run/stab";
	
	log_message("CM: cardmgr/hacked starting, version is " CS_RELEASE);
	
	if (load_config()) {
		cardmgr_fail();
		return -1;
	}
	
	if (init_sockets()) {
		cardmgr_fail();
		return -1;
	}
	
	/* If we've gotten this far, then clean up pid and stab at exit */
	write_stab();
    
	for (i = max_fd = 0; i < sockets; i++)
		max_fd = (socket[i].fd > max_fd) ? socket[i].fd : max_fd;

	/* First select() call: poll, don't wait */
	tv.tv_sec = tv.tv_usec = 0;

	/* Wait for sockets in setup-pending state to settle */
	wait_for_pending();
    

	FD_ZERO(&fds);
	for (i = 0; i < sockets; i++)
		FD_SET(socket[i].fd, &fds);

	if (select(max_fd+1, &fds, NULL, NULL, &tv) < 0) {
		log_perror("CM: select fails");
		return -1;
	}

	for (i = 0; i < sockets; i++) {
		if (!FD_ISSET(socket[i].fd, &fds))
			continue;
		ret = read(socket[i].fd, &event, 4);
		if ((ret == -1) && (errno != EAGAIN))
			log_message("CM: read(%d): %m\n", i);
		if (ret != 4)
			continue;
	    
		switch (event) {
		case CS_EVENT_CARD_INSERTION:
		case CS_EVENT_INSERTION_REQUEST:
			socket[i].state |= SOCKET_PRESENT;
		case CS_EVENT_CARD_RESET:
			socket[i].state |= SOCKET_READY;
			do_insert(i);
			break;
		case CS_EVENT_RESET_PHYSICAL:
			socket[i].state &= ~SOCKET_READY;
			break;
		}
	    
	}

	return 0;
	
}