summaryrefslogtreecommitdiff
path: root/usr/src/cmd/pcmciad/pcmciad.c
blob: d99ca961019b67e89a4e355934e24e6531a925c6 (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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *  PCMCIA User Daemon
 *	This is a newer daemon than the original version.
 *	It is a restructuring to use multiple threads
 *	and LWP in order to not lose any events and
 *	to better be able to service devices.  It has
 *	hooks for using external processing functions
 *	of various types.
 *
 *	If you add a new built-in handler, please keep its functions
 *	grouped together.
 *
 *	The prototype external shared object code assumes that you
 *	have three entry points defined.  It doesn't load global.
 *	The entry points are:
 *	void handler(char *, char *, int, int, void *)
 *	void init(void)
 *	void fini(void)
 *
 *	If init() doesn't exist it isn't called and fini() currently
 *	isn't ever called.  handler() must exist.  They must use these
 *	these specific names.
 *
 *	init() is called just before the first time the handler is to
 *	be called.  This allows any initial setup that might be
 *	necessary.  It is only called once.
 *
 *	External handlers can reference symbols in the main program
 *	but the symbols will be invisible across handlers.
 */

#include	<stdio.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	<ctype.h>
#include	<fcntl.h>
#include	<sys/types.h>
#include	<sys/fcntl.h>
#define	_KERNEL
#include	<sys/dditypes.h>
#undef _KERNEL
#include	<sys/sunddi.h>
#include	<sys/ddi_impldefs.h>
#include	<sys/pctypes.h>
#include	<sys/stream.h>
#include	<stropts.h>
#include	<sys/modctl.h>
#include	<sys/signal.h>
#include	<sys/sad.h>
#include	<sys/pem.h>
#include	<sys/pcmcia.h>
#include	<sys/sservice.h>
#include	<string.h>
#include	<search.h>
#include	<ftw.h>
#include	<pwd.h>
#include	<grp.h>

/* memory card support headers */
#include	<errno.h>
#include	<dirent.h>
#include	<signal.h>
#include	<limits.h>
#include	<sys/stat.h>
#include	<sys/mnttab.h>
#include	<sys/wait.h>
#include	<sys/dkio.h>

#include	<thread.h>
#include	<synch.h>

#include	<dlfcn.h>

/*
 * pcmciad
 *
 * the basic daemon is a multithreaded entity that listens for
 * PCMCIA events and acts on them.
 * one thread just reads the event driver and constructs work requests
 * on a per-socket work queue.  A wakeup is given to the work thread
 * for that work queue.  When the work thread wakes up, it starts
 * processing its queue one item at a time until the queue is empty.
 * It then goes to sleep and waits to get another work item.
 *
 * One additional thread is used for processing special commands
 * that need to be serialized in order to avoid problems.  The list of
 * commands is currently "drvconfig, disks".
 *
 * note that the work to be done is driver dependent.
 * The daemon makes every attempt to identify what needs to be done
 * and call the driver specific functions.  If nothing is found then
 * a default behavior is used (modload the driver and then call
 * devlinks is the built in behavior)
 */

/*
 * The macros defined here that are used to specify the encoding
 *	of the socket and function number fields must match the
 *	functionality of the similar macros defined in cs_priv.h
 * Since this daemon code is going to be completely replaced in
 *	Solaris 2.7 by the generic DDI hotplug framework, no
 *	effort was made to include cs_priv.h
 * The following is taken from cs_priv.h:
 *      The encoding of the socket number is:
 *
 *              xxxxxxxx | xxxxgfff | xxxxxxxx | xsssssss
 *
 *      g - global CIS bit
 *      f - function number bit
 *      s - socket number bit
 *      x - don't care bits
 */
#define	SOCKET(socket)		((socket) & (PCMCIA_MAX_SOCKETS - 1))
#define	FUNCTION(socket)	(((socket) >> 16) & (PCMCIA_MAX_FUNCTIONS - 1))
#define	XSOCKET(socket, func)	(((socket & (PCMCIA_MAX_SOCKETS - 1))) | \
				(((func) & (PCMCIA_MAX_FUNCTIONS - 1)) << 16))

/*
 * daemon global variables and datatypes
 */
/* prototypes for per driver functions */
static void serial(char *, char *, int, int, void *);
static void serinit(void);

static void defhandler(char *, char *, int, int, void *);

typedef void (* function_t)(char *, char *, int, int, void *);
typedef void (* if_func_t)(void);

static void memory(char *, char *, int, int, void *);
static void meminit(void);

static void disk(char *, char *, int, int, void *);
static void diskinit(void);

#define	MAX_DRIVERS		128

#define	DRV_NOT_INIT		0x0002
#define	DRV_NOT_LOAD		0x0001
#define	DRV_SHARED_OBJ		0x0004
#define	DRV_SHELL_SCRIPT	0x0008

struct driver_specific {
	char	*driver;
	char	*class;
	uint_t   flags;
	function_t handler;
	if_func_t init;
	if_func_t fini;
	void	*handle;
};
static struct driver_specific driver_map[MAX_DRIVERS] = {
	{"pcser", "serial", DRV_NOT_INIT, serial, serinit, NULL},
	{"pcmem", "memory", DRV_NOT_INIT, memory, meminit, NULL},
	{"pcata", "disk", DRV_NOT_INIT, disk, diskinit, NULL}
};

static int num_drivers = 3;

struct work {
	struct work *next, *prev;
	union em_primitives *prim;
};

struct tasklist {
	struct work	work;
	mutex_t		lock;
	cond_t		sleep;
	thread_t	thread;
	function_t	drivers[8];
};
static struct tasklist	**tasklists;

struct driver_aliases {
	struct driver_aliases *next;
	char	*name;
	char	*alias;
};
static struct driver_aliases	*aliases = NULL;

#define	DBG_ALL		0xffff
#define	DBG_SETUP	0x0001
#define	DBG_MODLOAD	0x0002
#define	DBG_TASK	0x0004
#define	DBG_DEVSETUP	0x0008
#define	DBG_SYSCALL	0x0010
#define	DBG_ALIASING	0x0080

static int debug = 0;

static int pem;			/* event manager descriptor */

/*
 * Global mutexes to single-thread certain card handler
 *	routines.
 */
static void init_global_locks();
#ifndef	lint
static mutex_t serinit_lock;
static mutex_t meminit_lock;
#endif

#define	DEFAULT_DIR	"/usr/lib/pcmcia"

static char *default_dir = DEFAULT_DIR;
static char *device = "/dev/pem";
static char *pcmcia_root_dir = "/devices";

#ifndef	lint
static char *pcmcia_driver_class = "root";
#endif

static int num_sockets;		/* current number of sockets */

static int task_state = 0;	/* zero is not running */

/* prototypes used internally */
static char *find_alias(char *);
static void init_aliases(char *);
static void drv_init_table(char *);
static void em_init(int);
static void setup_autopush(char *);
static void *event_read(void *);

static void task_create(int);
static void task_init(int);

static int is_blank(char *);

int
main(int argc, char *argv[])
{
	sigset_t	sigs;
	int		c;


	while ((c = getopt(argc, argv, "Dd:l:")) != EOF) {
		switch (c) {
		case 'D':
			debug = (debug << 1) | 1;
			break;
		case 'd':
			device = optarg;
			break;
		case 'l':
			default_dir = optarg;
			break;
		default:
			(void) fprintf(stderr,
				"usage: pcmciad [-D] [-d device]\n");
			exit(1);
		}
	}

	if (debug)
		(void) printf("debug level 0x%x\n", debug);

	(void) chdir(default_dir);
	(void) init_aliases("/etc/driver_aliases");
	(void) drv_init_table(default_dir);
	(void) init_global_locks();

	pem = open(device, O_RDWR);
	if (pem < 0) {
		if (debug)
			perror(device);
		exit(2);
	}
	if (!debug) {
		(void) close(0);
		(void) close(1);
		(void) close(2);
		if (fork() == 0) {
			(void) setsid();
		} else {
			exit(0);
		}
	} else {
		setbuf(stdout, NULL);
	}

	(void) em_init(pem);

	task_state = 1;		/* mark as running */
	(void) task_init(num_sockets);

#ifndef	lint
	(void) thr_setconcurrency(thr_getconcurrency() + 1);

	if (debug) {
		(void) printf("concurrency == %d\n",
					thr_getconcurrency());
	}
#endif

	/*
	 * the per-socket threads are up and running
	 * so we now start the work finding task
	 */
#ifdef	lint

	(void *) event_read(0);
#else
	if (thr_create(NULL, 0, event_read, (void *) pem,
		THR_BOUND | THR_NEW_LWP | THR_DAEMON, NULL) != 0)
		exit(0x20);
#endif

	(void) sigfillset(&sigs);

#ifndef	lint
	if (thr_sigsetmask(SIG_BLOCK, &sigs, NULL) < 0)
		perror("thr_sigsetmask");
#endif
	(void) sigpause(1);
	return (0x22);
}

/*
 * is_blank() returns 1 (true) if a line specified is composed of
 * whitespace characters only. otherwise, it returns 0 (false).
 *
 * Note. the argument (line) must be null-terminated.
 */
static int
is_blank(char *line)
{
	for (/* nothing */; *line != '\0'; line++)
		if (!isspace(*line))
			return (0);
	return (1);
}

/*
 * alias handling
 * we need to be able to find the mapping between certain
 * device names and the appropriate driver.  This code
 * searches the driver alias list.  It needs to know when
 * to re-read and reconstruct the list in the event of new
 * aliases being added.
 */

/*
 * init_aliases(filename)
 *	read in the aliases file and construct a fast lookup
 *	list.  We don't want to read every time since that could
 *	take too long in multiple event conditions.
 */

static void
init_aliases(char *path)
{
	FILE *file;
	char name[128], alias[128], line[256], *cp;
	int i, len;
	struct driver_aliases *nalias;

	file = fopen(path, "r");
	if (file != NULL) {
		while (fgets(line, sizeof (line), file) != NULL) {
			/* cut off comments starting with '#' */
			if ((cp = strchr(line, '#')) != NULL)
				*cp = '\0';
			/* ignore comment or blank lines */
			if (is_blank(line))
				continue;
			/* sanity-check */
			if (sscanf(line, "%s %[^\n]\n", name, alias) != 2)
				continue;
			if (alias[0] == '"') {
				len = strlen(alias);
				for (i = 1; i < len; i++)
					alias[i-1] = alias[i];
				alias[len-2] = '\0';
			}
			nalias = malloc(sizeof (struct driver_aliases));
			if (nalias == NULL)
				break;
			nalias->name = strdup(name);
			nalias->alias = strdup(alias);
			nalias->next = aliases;
			aliases = nalias;
			if (debug & DBG_ALIASING) {
				(void) printf("add alias: %s <- %s\n",
							name, alias);
			}
		}
		(void) fclose(file);
	}
}

/*
 * find_alias(name)
 *	look up name in the alias list and return either the alias
 *	or NULL (if none found).
 */

char *
find_alias(char *name)
{
	struct driver_aliases *alias;
	for (alias = aliases; alias != NULL; alias = alias->next) {
		if (debug & DBG_ALIASING) {
			(void) printf("alias: %s == %s [name = %s]\n",
					name, alias->alias, alias->name);
		}
		if (strcmp(name, alias->alias) == 0)
			return (alias->name);
	}
	return (NULL);
}

/*
 * driver handler lookup, initialization functions
 */

static void
drv_init_table(char *dir)
{
	DIR *dfd;
	char driver[64], *cp;
	struct dirent *dirx;
	int drvr, found = 0, flags;

	dfd = opendir(dir);
	if (dfd == NULL) {
		return;
	}
	for (; num_drivers < MAX_DRIVERS; ) {
		dirx = readdir(dfd);
		if (dirx == NULL)
			break;
		/* skip dot names */
		if (dirx->d_name[0] == '.')
			continue;
		cp = strchr(dirx->d_name, '.');
		if (cp == NULL) {
			/*
			 * not a .so so skip for now
			 * later could be a shell script or
			 * other scripting function or a
			 * directory for class specific
			 * drivers
			 */
			if (debug & DBG_DEVSETUP) {
				(void) printf("Skipping: %s\n",
							dirx->d_name);
			}
			continue;
		}
		/*
		 * as support is added for different types of objects,
		 * the appropriate check and setup should be done
		 * here. Some possible future additions:
		 * .sh (shell), .pl (perl) and .exe (executable)
		 */
		if (strcmp(cp, ".so.1") == 0) {
			/*
			 * we have an object so claim it
			 */
			(void) memset(driver, '\0', sizeof (driver));
			(void) strncpy(driver, dirx->d_name,
						cp - dirx->d_name);
			found = 1;
			flags = DRV_NOT_INIT |
				DRV_NOT_LOAD | DRV_SHARED_OBJ;
		}
		if (found) {
			/* want to replace if new module found */
			for (drvr = 0; drvr <= num_drivers; drvr++)
				if (strcmp(driver_map[drvr].driver, driver) ==
				    0)
					break;
			driver_map[drvr].driver = strdup(driver);
			driver_map[drvr].class = "*";
			driver_map[drvr].flags = flags;
			if (debug & DBG_DEVSETUP)
				(void) printf("Found %s (%s) "
					"as shared object\n",
					dirx->d_name,
					driver_map[drvr].driver);
			num_drivers++;
		}
	}
}

static int
drv_load(struct driver_specific *driver)
{
	void *handle;
	char path[256];

	if (driver == NULL)
		return (0);
	if (driver->flags & DRV_SHARED_OBJ &&
				driver->flags & DRV_NOT_LOAD) {
		(void) sprintf(path, "%s/%s.so.1", default_dir,
						driver->driver);
		handle = dlopen(path, RTLD_LAZY);
		if (handle == NULL)
			return (0);
		/* now have a handle so get the three functions */
		driver->handle = handle;
		driver->init = (if_func_t)dlsym(handle, "init");
		driver->handler = (function_t)dlsym(handle, "handler");
		driver->fini = (if_func_t)dlsym(handle, "fini");
		if (driver->handler != NULL)
			driver->flags &= ~DRV_NOT_LOAD;
		if (driver->init == NULL)
			driver->flags &= ~DRV_NOT_INIT;
		return (1);
	}
	return (0);
}

static void *
find_handler(char *name)
{
	char *alias;
	int i;
	alias = find_alias(name);
	if (alias != NULL) {
		for (i = 0; i < num_drivers; i ++) {
			if (strcmp(name, driver_map[i].driver) == 0 ||
			    strcmp(alias, driver_map[i].driver) == 0) {
				if (debug)
					(void) printf(
						"found handler for %s "
						"(flags=%x)\n",
						driver_map[i].driver,
						driver_map[i].flags);
				if (driver_map[i].flags & DRV_NOT_LOAD) {
					(void) printf("load driver\n");
					if (drv_load(&driver_map[i])) {
						driver_map[i].init();
						driver_map[i].flags &=
							~DRV_NOT_INIT;
					}
				}
				if (driver_map[i].flags & DRV_NOT_INIT) {
					driver_map[i].init();
					driver_map[i].flags &= ~DRV_NOT_INIT;
				}

				return ((void *)driver_map[i].handler);
			}
		}
	}

	return ((void *) defhandler);
}

/*
 * fix_device_path(root, dev)
 *	for a new device that was created during a MODCONFIG, it
 *	is necessary to either run "drvconfig" which takes too long
 *	or patch the new name which ended up with a ".." prepended
 *	to the name.
 */

static void
fix_device_path(char *root, char *dev)
{
	char *rootdir, *cp, *lastpart, *newname;

	if ((rootdir = (char *)malloc(strlen(root) + strlen(dev) + 4))
							== NULL) {
	    if (debug & DBG_DEVSETUP) {
		perror("pcmciad: fixup_devnames malloc");
	    }
	    return;
	}
	if ((newname = (char *)malloc(strlen(root) + strlen(dev) + 4))
							== NULL) {
	    if (debug) {
		perror("pcmciad: fixup_devnames malloc");
	    }
	    return;
	}

	if (debug & DBG_DEVSETUP) {
		(void) printf("fixup_devnames(%s, %s)\n", root, dev);
	}
	lastpart = strrchr(dev, '/');
	if (lastpart == NULL)
		return;
	lastpart++;
	if (debug & DBG_DEVSETUP)
		(void) printf("\tlastpart = %s\n", lastpart);

	(void) sprintf(rootdir, "%s%s", root, dev);
	(void) sprintf(newname, "%s%s", root, dev);
	/* get last component to replace */
	cp = strrchr(rootdir, '/') + 1;
	(void) sprintf(cp, "..%s", lastpart);
	if (debug & DBG_DEVSETUP) {
	    (void) printf("\tfixup_devnames: starting at [%s]\n",
			rootdir);
	    (void) printf("fixup_devnames: rename(%s, %s)\n",
			rootdir, newname);
	}
	(void) rename(rootdir, newname);
	free(rootdir);
	free(newname);

}

/*
 * initialize the event interface.
 */

static void
em_init(int fd)
{
	uchar_t			*evp;
	em_init_req_t		*init;
	em_adapter_info_req_t	*info;
	em_adapter_info_ack_t	*infoack;
	struct strbuf		strbuf;
	int			flags, i;
	char			buff[1024];


	if (debug & DBG_SETUP) {
		(void) printf("em_init(%d)\n", fd);
	}

	/* LINTED lint won't shut up about this line */
	init = (em_init_req_t *)buff;
	(void) memset(buff, '\0', sizeof (buff));

	init->em_primitive = EM_INIT_REQ;
	init->em_logical_socket = -1; /* all sockets */
	init->em_event_mask_offset = sizeof (em_init_req_t);
	init->em_event_mask_length = sizeof (ulong_t);

	evp = (uchar_t *)buff + sizeof (em_init_req_t);
	/* LINTED lint won't shut up about this line */
	*(ulong_t *)evp = -1;	/* all events */

	strbuf.maxlen = sizeof (em_init_req_t) + sizeof (ulong_t);
	strbuf.len = strbuf.maxlen;
	strbuf.buf = buff;

	if (putmsg(fd, &strbuf, NULL, 0) < 0) {
		perror("putmsg");
		exit(2);
	}

	if (debug & DBG_SETUP) {
		(void) printf("\tsent EM_INIT_REQ\n");
	}

	strbuf.maxlen = sizeof (buff);
	flags = 0;
	if (getmsg(fd, &strbuf, NULL, &flags) < 0) {
		perror("em_init: getmsg");
		exit(3);
	}

	if (debug & DBG_SETUP) {
		(void) printf("\treceived primitive: %d\n",
		    (int)init->em_primitive);
	}

	/* LINTED lint won't shut up about this line */
	info = (em_adapter_info_req_t *)buff;
	info->em_primitive = EM_ADAPTER_INFO_REQ;
	strbuf.maxlen = sizeof (em_adapter_info_req_t);
	strbuf.len = strbuf.maxlen;
	strbuf.buf = buff;
	if (putmsg(fd, &strbuf, NULL, 0) < 0) {
		perror("putmsg");
		exit(2);
	}

	if (debug & DBG_SETUP) {
		(void) printf("\tsent EM_ADAPTER_INFO_REQ\n");
	}

	strbuf.maxlen = sizeof (buff);
	flags = 0;
	if (getmsg(fd, &strbuf, NULL, &flags) < 0) {
		perror("em_init: getmsg");
		exit(3);
	}

	/* LINTED lint won't shut up about this line */
	infoack = (em_adapter_info_ack_t *)buff;
	if (infoack->em_primitive == EM_ADAPTER_INFO_ACK) {
		if (debug & DBG_SETUP) {
			(void) printf("adapter info:\n");
			(void) printf("\tsockets: %d\n",
			    (int)infoack->em_num_sockets);
			(void) printf("\twindows: %d\n",
			    (int)infoack->em_num_windows);
		}

		num_sockets = infoack->em_num_sockets;
		for (i = 0; i < num_sockets; i++) {
			em_ident_socket_req_t *ident;
			int f;
			for (f = 0; f < PCMCIA_MAX_FUNCTIONS; f++) {
				/* LINTED lint won't shut up about this line */
				ident = (em_ident_socket_req_t *)buff;
				ident->em_primitive = EM_IDENT_SOCKET_REQ;
				ident->em_socket = XSOCKET(i, f);
				strbuf.maxlen = sizeof (em_ident_socket_req_t);
				strbuf.len = strbuf.maxlen;
				strbuf.buf = buff;
				if (putmsg(fd, &strbuf, NULL, 0) < 0) {
					perror("putmsg");
				}
			}
		}
	} else {
		if (debug & DBG_TASK) {
			(void) printf("\treceived primitive: %d\n",
				(int)infoack->em_primitive);
		}
	}
}

/*
 * event_read(fd)
 *	read event from the event manager driver.
 *	This is run as a bound thread so that it won't ever
 *	have to wait for a thread to become available.
 *	This is the whole driving force behind the tasks.
 */

static char cbuff[4096];
static char dbuff[4096];

static void *
event_read(void *xx)
{
	union em_primitives *prim;
	struct strbuf ctl, data;
	struct tasklist *task;
	int fd = (int)xx;
	int running;
	sigset_t sigs;

	/*
	 * before doing anything else, make sure other threads
	 * won't kill us with spurious signals
	 */
	(void) sigfillset(&sigs);
#ifndef	lint
	if (thr_sigsetmask(SIG_BLOCK, &sigs, NULL) < 0)
		perror("thr_sigsetmask");
#endif

	for (running = 1; running; ) {
		int flags = 0;
		data.maxlen = sizeof (dbuff);
		data.buf = dbuff;

		ctl.maxlen = sizeof (cbuff);
		ctl.buf = cbuff;

		(void) memset(cbuff, '\0', sizeof (cbuff));

		if (getmsg(fd, &ctl, &data, &flags) < 0) {
			if (errno != EINTR && errno != EBUSY &&
						errno != ENOENT) {
				perror("event_read: getmsg");
				exit(0x10);
			} else
				continue;
		}

		/* LINTED lint won't shut up about this line */
		prim = (union em_primitives *)cbuff;
		if (prim->em_primitive != EM_EVENT_IND) {
			(void) fprintf(stderr,
					"unexpected indicator\n");
		} else {
			struct work *work;
			/*
			 * now have an event indicator
			 * find the socket and dup the message then
			 * queue the message onto the work list
			 */
			prim = (union em_primitives *)malloc(ctl.len);
			if (prim == NULL) {
				continue;
			}
			(void) memcpy((char *)prim, cbuff, ctl.len);
			task = tasklists[
			    SOCKET(prim->event_ind.em_logical_socket)];
			work = (struct work *)malloc(sizeof (struct work));
			if (work == NULL) {
				free(prim);
				continue;
			}
			work->prim = prim;

			if (debug & DBG_TASK) {
			    (void) printf("socket %d: function %d about to "
						"queue work\n",
				(int)
				SOCKET(prim->event_ind.em_logical_socket),
				(int)
				FUNCTION(prim->event_ind.em_logical_socket));
			}
#ifndef	lint
			(void) mutex_lock(&task->lock);
#endif
			insque((struct qelem *)work,
				(struct qelem *)task->work.prev);
			if (debug & DBG_TASK)
				(void) printf("...done\n");
#ifndef	lint
			(void) cond_broadcast(&task->sleep);
			(void) mutex_unlock(&task->lock);
#endif
		}
	}
	/* NOTREACHED */
	return ((void *) NULL);
}

/*
 * event_process(socket, prim)
 *	when a socket thread wakes up and finds work
 *	it calls this function to decode the event
 *	indication.  Currently, only events are processed
 * Only the sockert number is passed in the "socket"
 *	arguemnt - the function number is not encoded
 *	in the "socket" argument.
 */
static void
event_process(int socket, union em_primitives *prim)
{
	struct tasklist *task;
	int function, i;
	char *name = "*";
	char *class = "*";
	struct pcm_make_dev	*md = NULL;
	void *arg = NULL;

	function = FUNCTION(prim->event_ind.em_logical_socket);
	socket = SOCKET(socket);
	task = tasklists[socket];

	if (debug & DBG_TASK) {
		(void) printf("event_process(%d, %d, %x)\n",
				socket, function, (int)prim);
	}

	switch (prim->em_primitive) {
		/* general "event" indication */
	case EM_EVENT_IND:
		/*
		 * some events are handled in here directly
		 * or are done here and in the specific code
		 */
		if (debug & DBG_TASK) {
			(void) printf("event = %d\n",
				(int)prim->event_ind.em_event);
		}
		switch (prim->event_ind.em_event) {
		case PCE_CARD_INSERT:
			/*
			 * card insertion must be preprocessed
			 * in the daemon common code since we
			 * don't know the drivers yet.
			 * it is a time for cleanup. The driver
			 * specific code will get this on ident.
			 */
			for (i = 0; i < PCMCIA_MAX_FUNCTIONS; i++)
				task->drivers[i] = defhandler;
			return;

		case PCE_CARD_REMOVAL:
			/*
			 * driver specific processing is
			 * all we need to do. cleanup on insert.
			 */
			break;

		case PCE_DEV_IDENT:
			/*
			 * do a modload to make things start running.
			 * this only does something the first time
			 * but that time is needed.
			 */
			name = ((char *)prim) +
				prim->event_ind.em_event_info_offset;
			arg = (void *)name;

			/*
			 * now identify the driver and setup
			 * per driver function info
			 */
			task->drivers[function] =
				(function_t)find_handler(name);
			task->drivers[function](name, "*",
						PCE_CARD_INSERT,
						socket, NULL);
			break;

		case PCE_INIT_DEV:
			/* LINTED lint won't shut up about this line */
			md = (struct pcm_make_dev *)(((char *)prim) +
				prim->event_ind.em_event_info_offset);
			if (debug & DBG_DEVSETUP) {
			    (void) printf("prim=%x, offset = %x (%s)\n",
				(int)prim,
				(int)prim->event_ind.em_event_info_offset,
				md->op == SS_CSINITDEV_CREATE_DEVICE ?
				"create" : "remove");
			}
			if (debug & DBG_DEVSETUP)
				(void) printf("\tmd = %x\n", (int)md);
			arg = (void *)md;
			break;

		default:
			arg = (void *) (prim);
			break;
		}
		task->drivers[function](name, class,
					prim->event_ind.em_event,
					socket, arg);
	}
}

/*
 * task_socket(socket)
 *	this is the thread code for per-socket tasks.
 *	it will check for work and sleep on its condition
 *	variable when there is no work.
 */

static void *
task_socket(void *socket_val)
{
	struct tasklist *task;
	int socket = (int)socket_val;
	struct work *work;

	task = tasklists[socket];
	while (task_state) {
		if (debug & DBG_TASK) {
		    (void) printf(
			"task socket(%d) checking for work %x:%x\n",
			socket, (int)task->work.next,
			(int)task->work.prev);
		}
#ifndef	lint
		(void) mutex_lock(&task->lock);
		while (task->work.next == &task->work) {
			/* nothing to do so go to sleep */
			(void) cond_wait(&task->sleep, &task->lock);
		}
#endif
		if (debug & DBG_TASK) {
			(void) printf("task socket(%d) have work\n",
								socket);
		}
		/* we have a work item so get it and remove from list */
		work = task->work.next;
		remque((struct qelem *)task->work.next);
#ifndef	lint
		(void) mutex_unlock(&task->lock);
#endif
		/* we have work item isolated so process it */
		(void) event_process(socket, work->prim);
		free(work->prim);
		free(work);
	}
	return (0);
}

/*
 * task_create(socket)
 *	create a task (structure and thread) for specified socket
 */
static void
task_create(int socket)
{
	int		err = 0;
	struct tasklist *task;


	task = (struct tasklist *)malloc(sizeof (struct tasklist));
	tasklists[socket] = task;
#ifndef	lint
	(void) cond_init(&task->sleep, USYNC_THREAD, 0);
	(void) mutex_init(&task->lock, USYNC_THREAD, 0);
#endif
	task->work.next = &task->work;
	task->work.prev = &task->work;
	/*
	 * create a thread for the socket/tasklist
	 * note that the work list is empty so the thread will
	 * immediately block in a wait on the condition.
	 * nothing will add a work item until all threads are
	 * created so no need to start it suspended
	 */
#ifdef	lint
	(void *) task_socket(0);
	if (err) {
#else
	if ((err = thr_create(NULL, 0, task_socket, (void *) socket,
					0, &task->thread)) != 0) {
#endif
		errno = 0;
		if (debug & DBG_SETUP)
			(void) fprintf(stderr,
					"nothread on %d (err=%d)\n",
					socket, err);
		perror("thr_create");
	}
}


/*
 * task_init(sockets)
 *	given the number of sockets, go through and initialize
 *	a task list and thread for them.
 */
static void
task_init(int sockets)
{
	int i;

	/*
	 * in order to deal with new sockets coming online use
	 * sockets * 2 as the preallocated list.  We may have to
	 * revisit if we can get more than twice the number dynamically.
	 */
	tasklists = (struct tasklist **)malloc(sizeof (struct tasklist *) *
						(num_sockets * 2));
	if (tasklists == NULL)
		exit(4);
	for (i = 0; i < sockets; i++) {
		(void) task_create(i);
	}
}

/*
 * defhandler(driver, class, event, socket, data)
 *	default handler for drivers that don't need much
 */

static void
defhandler(char *driver, char *class, int event, int socket, void *data)
{
	if (debug & DBG_TASK) {
		(void) printf("defhandler(%s, %s, %x, %x, %x)\n",
			driver, class, event, socket, (int)data);
	}

	switch (event) {
	case PCE_INIT_DEV:
		(void) system("/usr/sbin/devlinks");
		break;
	}
}


/* serial handler functions */

#define	SER_MODE_TERM_DEFAULT	0666	/* rw-rw-rw- */
#define	SER_TERM_UID_DEFAULT	0	/* root UID */
#define	SER_TERM_GID_DEFAULT	3	/* sys GID */
#define	SER_TERM_USER_DEFAULT	"root"
#define	SER_TERM_GROUP_DEFAULT	"sys"

#define	SER_MODE_CUA_DEFAULT	0600	/* rw------- */
#define	SER_CUA_UID_DEFAULT	5	/* uucp UID */
#define	SER_CUA_GID_DEFAULT	5	/* uucp GID */
#define	SER_CUA_USER_DEFAULT	"uucp"
#define	SER_CUA_GROUP_DEFAULT	"uucp"

static int ser_mode_term;
static int ser_term_uid;
static int ser_term_gid;

static int ser_mode_cua;
static int ser_cua_uid;
static int ser_cua_gid;

static void
serinit()
{
	struct passwd *pw;
	struct group *gr;

#ifndef	lint
	(void) mutex_lock(&serinit_lock);
#endif

	setpwent();
	setgrent();

	/*
	 * Setup the dial-in devices
	 * /dev/term devices are linked
	 *	to these.
	 */
	ser_mode_term = SER_MODE_TERM_DEFAULT;
	ser_term_uid = SER_TERM_UID_DEFAULT;
	ser_term_gid = SER_TERM_GID_DEFAULT;

	if ((pw = getpwnam(SER_TERM_USER_DEFAULT)) != NULL) {
	    if ((gr = getgrnam(SER_TERM_GROUP_DEFAULT)) != NULL) {
		ser_term_uid = pw->pw_uid;
		ser_term_gid = gr->gr_gid;
	    } /* getgrnam */
	} /* getpwnam */

	/*
	 * Setup the dial-out devices
	 * /dev/cua devices are linked
	 *	to these.
	 */
	ser_mode_cua = SER_MODE_CUA_DEFAULT;
	ser_cua_uid = SER_CUA_UID_DEFAULT;
	ser_cua_gid = SER_CUA_GID_DEFAULT;

	if ((pw = getpwnam(SER_CUA_USER_DEFAULT)) != NULL) {
	    if ((gr = getgrnam(SER_CUA_GROUP_DEFAULT)) != NULL) {
		ser_cua_uid = pw->pw_uid;
		ser_cua_gid = gr->gr_gid;
	    } /* getgrnam */
	} /* getpwnam */

	endgrent();
	endpwent();

#ifndef	lint
	(void) mutex_unlock(&serinit_lock);
#endif
}

/*
 * serial(driver, class, event, socket, data)
 *	default serial device handler
 */
static void
serial(char *driver, char *class, int event, int socket, void *data)
{
	char path[MAXPATHLEN];
	char device[MODMAXNAMELEN];
	int ser_mode, ser_uid, ser_gid;
#ifdef	lint
	driver = driver;
	class = class;
#endif

	socket = SOCKET(socket);

	if (debug & DBG_TASK) {
		(void) printf("serial(0x%x, 0x%x, 0x%x)\n",
					event, socket, (int)data);
	}

	switch (event) {
	case PCE_INIT_DEV:
		{
			struct pcm_make_dev *md = (struct pcm_make_dev *)data;
			char *cp, *dir;

			if (md->op == SS_CSINITDEV_REMOVE_DEVICE)
				break;

			cp = strrchr(md->path, ',');
			setup_autopush(md->driver);
			if (cp && strcmp(cp, ",cu") == 0) {
				dir = "cua";
				ser_mode = ser_mode_cua;
				ser_uid = ser_cua_uid;
				ser_gid = ser_cua_gid;
			} else {
				dir = "term";
				ser_mode = ser_mode_term;
				ser_uid = ser_term_uid;
				ser_gid = ser_term_gid;
			}
			(void) sprintf(device, "/dev/%s/pc%d",
							dir, socket);
			(void) sprintf(path, "../../devices%s",
							md->path);
			if (debug & DBG_TASK) {
				(void) printf("symlink(%s, %s)\n",
						path, device);
			}
			(void) unlink(device);
			if (symlink(path, device) < 0 &&
						debug & DBG_SYSCALL) {
				char error[64];
				(void) sprintf(error, "symlink: %s",
								device);
				perror(device);
			}
			if (chmod(device, ser_mode) < 0)
				perror("chmod");
			if (chown(device, ser_uid, ser_gid) < 0)
				perror("chown");
		}
		break;
	}
}

static void
setup_autopush(char *driver)
{
#ifdef	lint
	driver = driver;
	return;
#else
	major_t			major_num;
	struct strapush		push;
	int			sadfd;


	if ((modctl(MODGETMAJBIND, driver, strlen(driver) + 1,
		    &major_num)) < 0) {
		if (debug & DBG_SYSCALL) {
			perror("modctl(MODGETMAJBIND)");
		}
	    return;
	} else {
	    if (debug & DBG_SYSCALL) {
		(void) printf("\tdriver = [%s], major_num = %d\n",
		    driver, (int)major_num);
	    }

	    push.sap_major = major_num;
	    push.sap_minor = 0;
	    push.sap_lastminor = 255;
	    push.sap_cmd = SAP_ALL;

	    /* XXX need to look at /etc/iu.ap instead */
	    (void) strcpy(push.sap_list[0], "ldterm");
	    (void) strcpy(push.sap_list[1], "ttcompat");
	    push.sap_npush = 2;

	    if ((sadfd = open(ADMINDEV, O_RDWR)) < 0) {
		if (debug & DBG_SYSCALL) {
		    perror("open(ADMINDEV)");
		}
		return;
	    } else {
		if (ioctl(sadfd, SAD_SAP, &push) < 0) {
			if (debug & DBG_SYSCALL) {
				perror("\tioctl(SAD_SAP)");
			}
		}
		(void) close(sadfd);
	    }
	}
#endif
}


/*
 * Common define for the memory() and disk() function handler
 */
#define	VOLDSK_PATH	"/vol/dev/dsk"
#define	VOLRDSK_PATH	"/vol/dev/rdsk"
#define	DEVDSK_PATH	"/dev/dsk"
#define	DEVRDSK_PATH	"/dev/rdsk"
#define	START_CHAR	'c'
#define	PDIR		"/var/run/pcmcia" /* piped directory */


/* code for the memory() function handler */
#define	VOLD_TYPE_PCRAM	"pcram"
#define	PCMEM_PATH	"/pcmem"
#define	PCRAM_FILE	"/var/run/pcmcia/pcram" /* special pipe file */

static void unmount_media(long, char *);
static void signal_vold(long, char *);
int volmgt_running();
char *media_findname(char *);

/*
 * memory(driver, class, event, socket data)
 *	this is the default memory card handler
 */

static void
meminit()
{
	static void makepdir();

#ifndef	lint
	mutex_lock(&meminit_lock);
#endif

	(void) makepdir();

#ifndef	lint
	mutex_unlock(&meminit_lock);
#endif
}

static void
memory(char *driver, char *class, int event, int socket, void *data)
{
	struct pcm_make_dev *md = (struct pcm_make_dev *)data;
#ifdef	lint
	driver = NULL;
	class = NULL;
#endif

	socket = SOCKET(socket);

	if (debug & DBG_TASK) {
		(void) printf("memory(%x, %x, %x)\n",
					event, socket, (int)data);
	}
	switch (event) {
	case PCE_INIT_DEV:
		if ((md->op == SS_CSINITDEV_CREATE_DEVICE) &&
					(md->flags != PCM_EVENT_MORE)) {
			if (debug & DBG_TASK) {
			    (void) printf("Run disks cmd for memory\n");
			}
			(void) system("/usr/sbin/disks");
		}
		signal_vold(socket, md->path);
		break;
	case PCE_DEV_IDENT:
		break;
	case PCE_CARD_REMOVAL:
		unmount_media(socket, VOLD_TYPE_PCRAM);
		break;
	}
}


/* code for the disk() function handler */
			/*
			 * Note that PSARC/1994/238 requires to use
			 *	"/pcdisk" directory name instead of
			 *	"/pcata"
			 */
#define	PCDISK_PATH	"/pcdisk"
#define	PCATA_FILE	"/tmp/.pcmcia/pcata" /* special pipe file */

/*
 * disk(driver, class, event, socket data)
 *	this is the default disk card handler
 */

static void
diskinit()
{
	/*
	 * Later - Create /tmp/.pcmcia/pcata pipe file
	 *	to support pcata/volmgt.
	 */

	/*
	 * XXX Don't forget to consider whether or not you need to
	 *	single-thread this code like serinit and meminit
	 *	does by using a global mutex!
	 */
}

static void
disk(char *driver, char *class, int event, int socket, void *data)
{
	struct pcm_make_dev *md = (struct pcm_make_dev *)data;
#ifdef	lint
	driver = driver;
	class = class;
#endif


	if (debug & DBG_TASK) {
		(void) printf("disk(%x, %x, %x)\n",
					event, socket, (int)data);
	}
	switch (event) {
	case PCE_INIT_DEV:
		if ((md->op == SS_CSINITDEV_CREATE_DEVICE) &&
					(md->flags != PCM_EVENT_MORE)) {
			if (debug & DBG_TASK) {
			    (void) printf("Run disks cmd for disk\n");
			}
			(void) system("/usr/sbin/disks");
		}
		/*
		 * LATER - add support volmgt/pcata
		 *	signal_vold(...);
		 */
		break;
	case PCE_DEV_IDENT:
		break;
	case PCE_CARD_REMOVAL:
		/*
		 * LATER - add support volmgt/pcata
		 *	unmount_media(...);
		 */
		break;
	}
}



/* code for the pcmem/volmgt support */

/*
 * get_devrdsk
 *	Given cn[tn]dnsn or cn[tn] (vold alias) path,
 *	read the link path from /dev/rdsk and compare
 *	to see if it matches with device_type and
 *	the socket information.
 *	And return a complete /dev/rdsk/cn[tn]dnsn
 *
 */
static const char *
get_devrdsk(long socket, char *path, char *device_type)
{
	DIR		*dskdirp;
	struct dirent	*dskentp;
	struct stat	sb;
	const char	*devp = NULL;
	char		namebuf[MAXNAMELEN];
	char		linkbuf[MAXNAMELEN];
	int		linksize;
	int		found;
	int		gotit;
	int		i, searchlen;


	if ((dskdirp = opendir(DEVRDSK_PATH)) == NULL) {
	    if (debug) {
		(void) printf(
			"get_devrdsk: Error opening directory %s\n",
			DEVRDSK_PATH);
	    }
	    return (NULL);
	}

	while (dskentp = readdir(dskdirp)) {

		/*
		 * skip . and .. and
		 *	anything else starting with dot)
		 */
		if (dskentp->d_name[0] == '.') {
			continue;
		}

		/*
		 * Silently Ignore for now any names not
		 * stating with START_CHAR
		 */
		if (dskentp->d_name[0] != START_CHAR) {
			continue;
		}

		/*
		 * Skip if path [cntn] is not a subset of
		 * dskentp->d_name [cntndnsn]
		 */
		if (strncmp(dskentp->d_name, path, strlen(path)) != 0) {
			continue;
		}

		/*
		 * found a name that matches!
		 */
		(void) sprintf(namebuf, "%s/%s", DEVRDSK_PATH,
		    dskentp->d_name);

		if (lstat(namebuf, &sb) < 0) {
		    if (debug) {
			(void) printf("\tget_devrdsk: Cannot stat %s\n",
				namebuf);
		    }
		    continue;
		}

		found = 0;
		if (S_ISLNK(sb.st_mode)) {
			linksize = readlink(namebuf, linkbuf,
							MAXNAMELEN);
			if (linksize <= 0) {
			    if (debug) {
				(void) printf("\tget_devrdsk: "
			    "Could not read symbolic link %s\n",
					namebuf);
			    }
			    continue;
			}
			linkbuf[linksize] = '\0';

			if (debug & DBG_DEVSETUP) {
			    (void) printf(
				"\tget_devrdsk: check path %s\n",
				linkbuf);
			}

			/* Make sure it is a right device_type */
			devp = strstr(linkbuf, device_type);
			if (devp == NULL)
				continue;

			/*
			 * If it is VOLD_TYPE_PCRAM, search backward
			 *	until '@' character, then a number
			 *	after '@' is a socket number
			 *
			 * ../../devices/../\
			 *	MemoryAliasName@SocketN[,FunctN]/\
			 *	pcram:tn,dn:dev,raw
			 */
			searchlen = strlen(linkbuf) - strlen(devp);
			gotit = 0;
			for (i = searchlen; i > 0; i--) {
				if (*devp == '@') {
					gotit++;
					break;
				}
				devp--;
			}
			if (gotit) {
				/* Get socket info. */
				devp++;
				if (debug & DBG_DEVSETUP) {
					(void) printf(
				"\tget_devrdsk: devp=%s\n", devp);
				}
				if (socket == (long)atoi(devp)) {
					found++;
					/* exit readdir() loop */
					break;
				}
			} else {
				if (debug) {
					(void) printf(
				"\tget_devrdsk: Invalid path [%s] "
				"for device_type [%s]\n",
						linkbuf, device_type);
				}
			}

		}  /* if (S_ISLNK) */
	}  /* while (dskentp) */

	(void) closedir(dskdirp);
	return (found ? namebuf : NULL);
}

/*
 * unmount_media - Unmount PCMCIA media file system
 *
 * If the user accidentally removes the card without
 * using eject(1) command, this routine is called for unmounting
 * a mounted file system assuming that the directory is not busy
 */
static void
unmount_media(long socket, char *device_type)
{
	static void	start_unmount(char *, char *);
	static FILE	*fp = NULL;
	struct mnttab	mnt;
	const char	*nvp;
	char		pname[100];
	int		isit_voldp, isit_pcmemp, isit_devp;
	int		dnlen;


	/* mtab is gone... let it go */
	if ((fp = fopen(MNTTAB, "r")) == NULL) {
		perror(MNTTAB);
		goto out;
	}

	while (getmntent(fp, &mnt) == 0) {

		isit_voldp = strncmp(mnt.mnt_special, VOLDSK_PATH,
		    strlen(VOLDSK_PATH));
		isit_pcmemp = strncmp(mnt.mnt_mountp, PCMEM_PATH,
		    strlen(PCMEM_PATH));
		isit_devp = strncmp(mnt.mnt_special, DEVDSK_PATH,
		    strlen(DEVDSK_PATH));

		/*
		 * Skip if mnt_special is not a VOLDSK_PATH
		 * or DEVDSK_PATH
		 */
		if ((isit_voldp == 0) && (isit_pcmemp == 0) ||
		    (isit_devp == 0)) {

			/* Check for cn[tn]dnsn in /dev/dsk */
			nvp = strchr(mnt.mnt_special, START_CHAR);
			(void) strcpy(pname, nvp);

			/*
			 * extract cn[tn]dnsn from
			 * /vol/dev/dsk/cn[tn]dnsn/...
			 */
			if (isit_voldp == 0) {
				dnlen = strcspn(pname, "/");
				pname[dnlen] = NULL;
			}

			if (get_devrdsk(socket, pname, device_type)
							!= NULL) {
			    start_unmount(mnt.mnt_special,
						mnt.mnt_mountp);
			}
		}
	}
out:
	(void) fclose(fp);
}

/*
 * start_unmount - Start to unmount mounting directory
 *
 * Using mnt_special for unmounting vold path, and
 * mnt_mountp for regular umount(1M)
 */
static void
start_unmount(char *mnt_special, char *mnt_mountp)
{
	static int	req_vold_umount(char *);
	static int	do_umount(char *);
	int		err = 0;

	/*
	 * If vold is running we have to request the vold
	 * to unmount the file system (sigh!) in order to
	 * to clean up /vol enviroment (?)
	 */
#ifdef	lint
	/* LINTED */
	if (0) {
#else
	if (volmgt_running()) {
#endif
		if (req_vold_umount(mnt_special) == 0) {
			err++;
		}
	} else {

		/*
		 * Great! we can do a simple umount(1M)
		 * if the vold is not runing by umount <mnt_mountp>
		 * (including /pcmem/<mnt_mountp> after
		 * vold is disabled
		 *
		 * OR when the user removes the memory card WITHOUT
		 * using eject(1) command
		 */
		if (do_umount(mnt_mountp) == 0) {
			err++;
		}
	}

	if (err && debug) {
		(void) fprintf(stderr, ("start_unmount: %s is busy\n"),
		    mnt_mountp);
	}
}

/*
 * req_vold_umount - Request vold to unmount
 *	/vol/dev/rdsk/cntndnsn/..
 *
 * If vold is running, this routine is called when the user
 * removes a PC card WITHOUT using eject(1) command
 */
static int
req_vold_umount(char *path)
{
	int		fd;
	int		rval = 0;
	const char	*rawpath;



	/* Convert to "raw" path (rdsk) for DKIOCEJECT ioctl() */
#ifdef	lint
	rawpath = path;
#else
	rawpath = (char *)media_findname(path);
#endif
	if ((fd = open(rawpath, O_RDONLY|O_NDELAY)) < 0) {
		if (debug) {
			if (errno == EBUSY) {
				(void) printf(
				    "\treq_vold_umount: %s is busy\n",
				    rawpath);
				perror(rawpath);
			}
		}
		goto out;
	}

	if (debug) {
		(void) printf(
		    "\treq_vold_umount: Unmount vold path [%s]\n",
		    rawpath);
	}

	/*
	 * This simulates the volmgt eject(1) command
	 * to request the vold to eject/umount and cleanup
	 * its enviroment after unmount so we can use the same
	 * slot for different PC card
	 */
	if (ioctl(fd, DKIOCEJECT, 0) < 0) {
		/* suppose to see ENOSYS from pcmem driver */
		/* or ENOENT since card is gone */
		if (errno != ENOSYS && errno != ENOENT) {
			/* Could be EBUSY [16] (Mount device busy) */
			if (debug) {
				(void) printf(
			"\treq_vold_umount: DKIOCEJECT errno [%d]\n",
				    errno);
			}
		goto out;
	    }
	}
	rval = 1;
out:
	(void) close(fd);
	return (rval);
}


/*
 * do_umount - Unmount a file system when volmgt is not running
 */
static int
do_umount(char *path)
{
	pid_t	pid;
	int	fd;


	/*
	 * Use fork1 instead since this is a Solaris thread program
	 */
	if ((pid = fork1()) == 0) {
		/* the child */
		/* get rid of those nasty err messages */
		fd = open("/dev/null", O_RDWR);
		(void) dup2(fd, 0);
		(void) dup2(fd, 1);
		(void) dup2(fd, 2);
		(void) execl("/etc/umount", "/etc/umount", path, NULL);
		(void) fprintf(stderr,
		    "pcmciad: exec of  \"/etc/umount %s\" failed; %s\n",
		    path, strerror(errno));
		return (-1);
	}

	/* the parent */
	/* wait for the umount command to exit */
	(void) waitpid(pid, NULL, 0);
	if (debug) {
		(void) printf("\tdo_umount: \"/etc/umount %s\"\n",
								path);
	}

	return (1);
}

/*
 * get_rdsk_path
 *	Given /devices/.. path, return cntndnsn path in
 *	/dev/rdsk that matches it
 */
static const char *
get_rdsk_path(char *dev)
{
	char		*res = NULL;
	char		path_buf[MAXPATHLEN+1];
	struct stat	sb_targ;
	struct stat	sb;
	DIR		*dp = NULL;
	struct dirent	*ent;


	/* get the dev_t for our target */
	(void) sprintf(path_buf, "%s%s", pcmcia_root_dir,
			dev);

	if (stat(path_buf, &sb_targ) < 0) {
		if (debug) {
			(void) printf(
		"\tget_rdsk_path: error: can't stat %s (%s)\n",
				path_buf, strerror(errno));
		}
		goto dun;
	}

	/* Must be a raw device */
	if ((sb_targ.st_mode & S_IFMT) != S_IFCHR) {
		if (debug) {
			(void) printf(
		"\tget_rdsk_path: Not a raw device [%s]\n",
				path_buf);
		}
		goto dun;
	}

	/* scan the disks directory for the "right device" */
	if ((dp = opendir(DEVRDSK_PATH)) == NULL) {
		if (debug) {
			(void) printf(
		"\terror: can't open directory %s (%s)\n",
				DEVRDSK_PATH, strerror(errno));
		}
		goto dun;
	}

	while ((ent = readdir(dp)) != NULL) {
		if (ent->d_name[0] == '.') {
			continue;
		}
		(void) sprintf(path_buf, "%s/%s", DEVRDSK_PATH,
			ent->d_name);
		if (stat(path_buf, &sb) < 0) {
			if (debug) {
				(void) printf(
			"\terror: can't stat \"%s\" (%s)\n",
					path_buf, strerror(errno));
			}
			continue;
		}

		if (sb.st_rdev != sb_targ.st_rdev) {
			continue;
		}

		/* found it! */
		res = strdup(path_buf);
		if (debug) {
			(void) printf(
		"\tget_rdsk_path: found \"%s\"\n",
				res);
		}
		break;
	}

dun:
	if (dp != NULL) {
		(void) closedir(dp);
	}
	return (res);

}

/*
 * signal_vold - tell vold that a new path has been added
 */
static void
signal_vold(long socket, char *device)
{
	static void		wr_to_pipe(char *, char *, int);
	static const char	*get_rdsk_path(char *);
	const char		*rpath;


	if (debug) {
		(void) printf("\tsignal_vold: entering for \"%s\"\n",
			device);
	}

#ifndef	lint
	/* Do not write to the pipe if vold is not running */
	if (volmgt_running() == 0) {
	    if (debug) {
		(void) printf("\tsignal_vold: volmgt NOT running\n");
	    }
	    return;
	}
#endif

	if ((rpath = get_rdsk_path(device)) == NULL) {
		/*
		 * disks(1) command does not work correctly
		 * or the devices can not be found in devfs tree
		 */
		if (debug) {
		    (void) fprintf(stderr,
			("\tsignal_vold: error - get NULL devpath\n"));
		}
		return;
	}

	wr_to_pipe("insert", (char *)rpath, socket);
}

static void
wr_to_pipe(char *event, char *path, int socket)
{
	static int	fd = -1;
	char		buf[BUFSIZ];


	/* open a named pipe without blocking */
	if (fd < 0) {
		if ((fd = open(PCRAM_FILE, O_WRONLY | O_NDELAY)) < 0) {
			/*
			 * May be reader process does NOT open
			 * the other end ofthe pipe yet
			 */
			if (debug) {
				(void) printf(
	"wr_to_pipe: open(\"%s\") failed (errno %d, NO reader?)\n",
					PCRAM_FILE, errno);
			}
			return;
		}
	}

	(void) sprintf(buf, "%s, %s, %d", event, path, socket);
	(void) write(fd, buf, (unsigned int) strlen(buf));
	(void) write(fd, "\n", 1);
	if (debug) {
		(void) printf("\twr_to_pipe: wrote: \"%s\"\n", buf);
	}
}
/*
 * Create PCMCIA pipe directory
 */
static void
makepdir()
{
	extern int	errno;


	/* Make a pipe directory */
	if (debug) {
		(void) printf("\nmakepdir: \tmkdir %s\n", PDIR);
	}

	if (mkdir(PDIR, 0755) < 0) {
	    if (errno != EEXIST) {
		if (debug) {
			(void) fprintf(stderr, (
		"error: can't create pipe directory %s (%s)\n"),
			    PDIR, strerror(errno));
		}
		return;
	    } /* !EEXIST */
	} /* mkdir */

	/* Make a fifo special named pipe file */
	if (debug) {
		(void) printf("\t\tmknod %s\n", PCRAM_FILE);
	}

	if (mknod(PCRAM_FILE, (mode_t)(S_IFIFO|0600), NULL) < 0) {
	    if (errno != EEXIST) {
		if (debug) {
			(void) fprintf(stderr, (
			    "error: can't create named pipe %s (%s)\n"),
			    PCRAM_FILE, strerror(errno));
		}
	    }
	}
}

static void
init_global_locks()
{
#ifndef	lint
	(void) mutex_init(&serinit_lock, USYNC_THREAD, 0);
	(void) mutex_init(&meminit_lock, USYNC_THREAD, 0);
#endif
}