xref: /illumos-gate/usr/src/uts/common/pcmcia/nexus/pcmcia.c (revision 8489c77cb9814bd11aa62863c3b4e5c69ec9ab0d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PCMCIA NEXUS
30  *	The PCMCIA module is a generalized interface for
31  *	implementing PCMCIA nexus drivers.  It preserves
32  *	the logical socket name space while allowing multiple
33  *	instances of the hardware to be properly represented
34  *	in the device tree.
35  *
36  *	The nexus also exports events to an event manager
37  *	driver if it has registered.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/user.h>
43 #include <sys/buf.h>
44 #include <sys/file.h>
45 #include <sys/uio.h>
46 #include <sys/conf.h>
47 #include <sys/stat.h>
48 #include <sys/autoconf.h>
49 #include <sys/vtoc.h>
50 #include <sys/dkio.h>
51 #include <sys/ddi.h>
52 #include <sys/debug.h>
53 #include <sys/sunddi.h>
54 #include <sys/sunndi.h>
55 #include <sys/cred.h>
56 #include <sys/kstat.h>
57 #include <sys/kmem.h>
58 #include <sys/modctl.h>
59 #include <sys/kobj.h>
60 #include <sys/callb.h>
61 #include <sys/param.h>
62 #include <sys/thread.h>
63 #include <sys/proc.h>
64 
65 #include <sys/pctypes.h>
66 #include <sys/pcmcia.h>
67 #include <sys/sservice.h>
68 #include <pcmcia/sys/cs_types.h>
69 #include <pcmcia/sys/cis.h>
70 #include <pcmcia/sys/cis_handlers.h>
71 #include <pcmcia/sys/cs.h>
72 #include <pcmcia/sys/cs_priv.h>
73 
74 #ifdef sparc
75 #include <sys/ddi_subrdefs.h>
76 
77 #elif defined(__x86) || defined(__amd64)
78 #include <sys/mach_intr.h>
79 #endif
80 
81 #undef SocketServices
82 
83 /* some bus specific stuff */
84 
85 /* need PCI regspec size for worst case at present */
86 #include <sys/pci.h>
87 
88 typedef struct pcmcia_logical_socket {
89 	int			ls_socket; /* adapter's socket number */
90 	uint32_t		ls_flags;
91 	struct pcmcia_adapter	*ls_adapter;
92 	pcmcia_if_t		*ls_if;
93 	dev_info_t		*ls_sockdrv;
94 	dev_info_t		*ls_dip[PCMCIA_MAX_FUNCTIONS];
95 	dev_info_t		*ls_mfintr_dip;
96 	int			ls_functions;
97 	uint32_t		ls_cs_events;
98 	uint32_t		ls_intr_pri;
99 	uint32_t		ls_intr_vec;
100 	int			ls_intrrefs;
101 	struct intrspec		ls_intrspec; /* MFC intrspec */
102 	inthandler_t		*ls_inthandlers; /* for multifunction cards */
103 	ddi_iblock_cookie_t	ls_iblk;
104 	ddi_idevice_cookie_t	ls_idev;
105 	kmutex_t		ls_ilock;
106 	int			ls_error; /* error for CS return */
107 } pcmcia_logical_socket_t;
108 
109 /*
110  * entry points used by the true nexus
111  */
112 int pcmcia_detach(dev_info_t *, ddi_detach_cmd_t);
113 int pcmcia_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
114 int pcmcia_prop_op(dev_t, dev_info_t *, dev_info_t *, ddi_prop_op_t,
115 			int, char *, caddr_t, int *);
116 void pcmcia_set_assigned(dev_info_t *, int, ra_return_t *);
117 int pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
118     ddi_intr_handle_impl_t *hdlp, void *result);
119 
120 /*
121  * prototypes used internally by the nexus and sometimes Card Services
122  */
123 int SocketServices(int function, ...);
124 
125 
126 void *CISParser(int function, ...);
127 extern void *(*cis_parser)(int, ...);
128 
129 struct regspec *pcmcia_cons_regspec(dev_info_t *, int, uchar_t *,
130 					ra_return_t *);
131 
132 static int (*pcmcia_card_services)(int, ...) = NULL;
133 
134 /*
135  * variables used in the logical/physical mappings
136  * that the nexus common code maintains.
137  */
138 struct pcmcia_adapter *pcmcia_adapters[PCMCIA_MAX_ADAPTERS];
139 int    pcmcia_num_adapters;
140 pcmcia_logical_socket_t *pcmcia_sockets[PCMCIA_MAX_SOCKETS];
141 int    pcmcia_num_sockets;
142 pcmcia_logical_window_t *pcmcia_windows[PCMCIA_MAX_WINDOWS];
143 int    pcmcia_num_windows;
144 struct power_entry pcmcia_power_table[PCMCIA_MAX_POWER];
145 int	pcmcia_num_power;
146 
147 struct pcmcia_mif *pcmcia_mif_handlers = NULL;
148 pcm_dev_node_t *pcmcia_devnodes = NULL;
149 
150 kmutex_t pcmcia_global_lock;
151 kcondvar_t pcmcia_condvar;
152 kmutex_t pcmcia_enum_lock;
153 
154 /*
155  * Mapping of the device "type" to names acceptable to
156  * the DDI
157  */
158 static char *pcmcia_dev_type[] = {
159 	"multifunction",
160 	"byte",
161 	"serial",
162 	"parallel",
163 	"block",
164 	"display",
165 	"network",
166 	"block",
167 	"byte"
168 };
169 
170 char *pcmcia_default_pm_mode = "parental-suspend-resume";
171 
172 /*
173  * generic names from the approved list:
174  *	disk tape pci sbus scsi token-ring isa keyboard display mouse
175  *	audio ethernet timer memory parallel serial rtc nvram scanner
176  *	floppy(controller) fddi isdn atm ide pccard video-in video-out
177  * in some cases there will need to be device class dependent names.
178  * network -> ethernet, token-ring, etc.
179  * this list is a first guess and is used when all else fails.
180  */
181 
182 char *pcmcia_generic_names[] = {
183 	"multifunction",
184 	"memory",
185 	"serial",
186 	"parallel",
187 	"disk",
188 	"video",		/* no spec for video-out yet */
189 	"network",
190 	"aims",
191 	"scsi",
192 	"security"
193 };
194 
195 #define	PCM_GENNAME_SIZE	(sizeof (pcmcia_generic_names) / \
196 					sizeof (char *))
197 #define	PCMCIA_MAP_IO	0x0
198 #define	PCMCIA_MAP_MEM	0x1
199 
200 /*
201  * The following should be 2^^n - 1
202  */
203 #define	PCMCIA_SOCKET_BITS	0x7f
204 
205 #ifdef PCMCIA_DEBUG
206 int pcmcia_debug = 0x0;
207 static void pcmcia_dump_minors(dev_info_t *);
208 #endif
209 
210 static f_tt *pcmcia_cs_event = NULL;
211 int pcmcia_timer_id;
212 dev_info_t	*pcmcia_dip;
213 /*
214  * XXX - See comments in cs.c
215  */
216 static f_tt *pcmcia_cis_parser = NULL;
217 
218 extern struct pc_socket_services pc_socket_services;
219 
220 /* some function declarations */
221 static int pcm_adapter_callback(dev_info_t *, int, int, int);
222 extern void pcmcia_init_adapter(anp_t *, dev_info_t *);
223 extern void pcmcia_find_cards(anp_t *);
224 extern void pcmcia_merge_power(struct power_entry *);
225 extern void pcmcia_do_resume(int, pcmcia_logical_socket_t *);
226 extern void pcmcia_resume(int, pcmcia_logical_socket_t *);
227 extern void pcmcia_do_suspend(int, pcmcia_logical_socket_t *);
228 extern void pcm_event_manager(int, int, void *);
229 static void pcmcia_create_dev_info(int);
230 static int pcmcia_create_device(ss_make_device_node_t *);
231 static void pcmcia_init_devinfo(dev_info_t *, struct pcm_device_info *);
232 void pcmcia_fix_string(char *str);
233 dev_info_t *pcmcia_number_socket(dev_info_t *, int);
234 static int pcmcia_merge_conf(dev_info_t *);
235 static uint32_t pcmcia_mfc_intr(caddr_t, caddr_t);
236 void pcmcia_free_resources(dev_info_t *);
237 static void pcmcia_ppd_free(struct pcmcia_parent_private *ppd);
238 int pcmcia_get_intr(dev_info_t *, int);
239 int pcmcia_return_intr(dev_info_t *, int);
240 int pcmcia_ra_alloc(dev_info_t *, ndi_ra_request_t *, ra_return_t *, char *);
241 int pcmcia_ra_free(dev_info_t *, ra_return_t *, char *);
242 
243 extern int cs_init(void);
244 extern int cs_deinit(void);
245 extern void cisp_init(void);
246 extern void cis_deinit(void);
247 
248 /*
249  * non-DDI compliant functions are listed here
250  * some will be declared while others that have
251  * entries in .h files. All will be commented on.
252  *
253  * with declarations:
254  *	ddi_add_child
255  *	ddi_binding_name
256  *	ddi_bus_prop_op
257  *	ddi_ctlops
258  *	ddi_find_devinfo
259  *	ddi_get_name_addr
260  *	ddi_get_parent_data
261  *	ddi_hold_installed_driver
262  *	ddi_name_to_major
263  *	ddi_node_name
264  *	ddi_pathname
265  *	ddi_rele_driver
266  *	ddi_set_name_addr
267  *	ddi_set_parent_data
268  *	ddi_unorphan_devs
269  *	i_ddi_bind_node_to_driver
270  *	i_ddi_bind_node_to_driver
271  *	i_ddi_bus_map
272  *	i_ddi_map_fault
273  *	i_ddi_mem_alloc
274  *	i_ddi_mem_alloc
275  *	i_ddi_mem_free
276  *	i_ddi_mem_free
277  *	modload
278  *	modunload
279  */
280 
281 extern void ddi_unorphan_devs(major_t);
282 
283 /* Card&Socket Services entry points */
284 static int GetCookiesAndDip(sservice_t *);
285 static int SSGetAdapter(get_adapter_t *);
286 static int SSGetPage(get_page_t *);
287 static int SSGetSocket(get_socket_t *);
288 static int SSGetStatus(get_ss_status_t *);
289 static int SSGetWindow(get_window_t *);
290 static int SSInquireAdapter(inquire_adapter_t *);
291 static int SSInquireSocket(inquire_socket_t *);
292 static int SSInquireWindow(inquire_window_t *);
293 static int SSResetSocket(int, int);
294 static int SSSetPage(set_page_t *);
295 static int SSSetSocket(set_socket_t *);
296 static int SSSetWindow(set_window_t *);
297 static int SSSetIRQHandler(set_irq_handler_t *);
298 static int SSClearIRQHandler(clear_irq_handler_t *);
299 
300 static struct modldrv modlmisc = {
301 	&mod_miscops,		/* Type of module. This one is a driver */
302 	"PCMCIA Nexus Support %I%", /* Name of the module. */
303 };
304 
305 static struct modlinkage modlinkage = {
306 	MODREV_1, (void *)&modlmisc, NULL
307 };
308 
309 int
310 _init()
311 {
312 	int	ret;
313 
314 	cisp_init();
315 
316 	if (cs_init() != CS_SUCCESS) {
317 	    if (cs_deinit() != CS_SUCCESS)
318 		cmn_err(CE_CONT, "pcmcia: _init cs_deinit error\n");
319 	    return (-1);
320 	}
321 
322 	mutex_init(&pcmcia_global_lock, NULL, MUTEX_DEFAULT, NULL);
323 	cv_init(&pcmcia_condvar, NULL, CV_DRIVER, NULL);
324 	mutex_init(&pcmcia_enum_lock, NULL, MUTEX_DEFAULT, NULL);
325 
326 	if ((ret = mod_install(&modlinkage)) != 0) {
327 		mutex_destroy(&pcmcia_global_lock);
328 		cv_destroy(&pcmcia_condvar);
329 		mutex_destroy(&pcmcia_enum_lock);
330 	}
331 	return (ret);
332 }
333 
334 int
335 _fini()
336 {
337 	int	ret;
338 
339 	if ((ret = mod_remove(&modlinkage)) == 0) {
340 		mutex_destroy(&pcmcia_global_lock);
341 		cv_destroy(&pcmcia_condvar);
342 		mutex_destroy(&pcmcia_enum_lock);
343 		cis_deinit();
344 		if (cs_deinit() != CS_SUCCESS) {
345 			cmn_err(CE_CONT, "pcmcia: _fini cs_deinit error\n");
346 		}
347 	}
348 	return (ret);
349 }
350 
351 int
352 _info(struct modinfo *modinfop)
353 {
354 	return (mod_info(&modlinkage, modinfop));
355 }
356 
357 extern pri_t minclsyspri;
358 
359 /*
360  * pcmcia_attach()
361  *	the attach routine must make sure that everything needed is present
362  *	including real hardware.  The sequence of events is:
363  *		attempt to load all adapter drivers
364  *		attempt to load Card Services (which _depends_on pcmcia)
365  *		initialize logical sockets
366  *		report the nexus exists
367  */
368 
369 int
370 pcmcia_attach(dev_info_t *dip, anp_t *adapter)
371 {
372 	int count, done, i;
373 
374 #if defined(PCMCIA_DEBUG)
375 	if (pcmcia_debug) {
376 		cmn_err(CE_CONT, "pcmcia_attach: dip=0x%p adapter=0x%p\n",
377 		    (void *)dip, (void *)adapter);
378 	}
379 #endif
380 
381 	pcmcia_dip = dip;
382 
383 	mutex_enter(&pcmcia_enum_lock);
384 	mutex_enter(&pcmcia_global_lock);
385 	if (pcmcia_num_adapters == 0) {
386 		pcmcia_cis_parser = (f_tt *)CISParser;
387 		cis_parser = (void *(*)(int, ...)) CISParser;
388 		pcmcia_cs_event = (f_tt *)cs_event;
389 		cs_socket_services = SocketServices;
390 		/* tell CS we are up with basic init level */
391 		(void) cs_event(PCE_SS_INIT_STATE, PCE_SS_STATE_INIT, 0);
392 	}
393 
394 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
395 	    PCM_DEVICETYPE, "pccard");
396 
397 	ddi_report_dev(dip);	/* directory/device naming */
398 
399 	/*
400 	 * now setup any power management stuff necessary.
401 	 * we do it here in order to ensure that all PC Card nexi
402 	 * implement it.
403 	 */
404 
405 	if (pm_create_components(dip, 1) != DDI_SUCCESS) {
406 		cmn_err(CE_WARN, "%s: not power managed\n",
407 			ddi_get_name_addr(dip));
408 	} else {
409 		pm_set_normal_power(dip, 0, 1);
410 	}
411 
412 	/*
413 	 * setup the info necessary for Card Services/SocketServices
414 	 * and notify CS when ready.
415 	 */
416 
417 	pcmcia_free_resources(dip);
418 	pcmcia_init_adapter(adapter, dip);
419 	/* exit mutex so CS can run for any cards found */
420 	mutex_exit(&pcmcia_global_lock);
421 
422 	/*
423 	 * make sure the devices are identified before
424 	 * returning.  We do this by checking each socket to see if
425 	 * a card is present.  If there is one, and there isn't a dip,
426 	 * we can't be done.  We scan the list of sockets doing the
427 	 * check. if we aren't done, wait for a condition variable to
428 	 * wakeup.
429 	 * Because we can miss a wakeup and because things can
430 	 * take time, we do eventually give up and have a timeout.
431 	 */
432 
433 	for (count = 0, done = 0;
434 	    done == 0 && count < max(pcmcia_num_sockets, 16);
435 	    count++) {
436 		done = 1;
437 		/* block CS while checking so we don't miss anything */
438 		mutex_enter(&pcmcia_global_lock);
439 		for (i = 0; i < pcmcia_num_sockets; i++) {
440 			get_ss_status_t status;
441 			if (pcmcia_sockets[i] == NULL)
442 				continue;
443 			bzero(&status, sizeof (status));
444 			status.socket = i;
445 			if (SSGetStatus(&status) == SUCCESS) {
446 				if (status.CardState & SBM_CD &&
447 				    pcmcia_sockets[i]->ls_dip[0] == NULL) {
448 					done = 0;
449 				}
450 			}
451 		}
452 		/* only wait if we aren't done with this set */
453 		if (!done) {
454 			mutex_exit(&pcmcia_global_lock);
455 			delay(10); /* give up CPU for a time */
456 			mutex_enter(&pcmcia_global_lock);
457 		}
458 		mutex_exit(&pcmcia_global_lock);
459 	}
460 
461 	mutex_exit(&pcmcia_enum_lock);
462 	return (DDI_SUCCESS);
463 }
464 
465 /*
466  * pcmcia_detach
467  *	unload everything and then detach the nexus
468  */
469 /* ARGSUSED */
470 int
471 pcmcia_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
472 {
473 	switch (cmd) {
474 	case DDI_DETACH:
475 		pm_destroy_components(dip);
476 		return (DDI_SUCCESS);
477 
478 	/*
479 	 * resume from a checkpoint
480 	 * We don't do anything special here since the adapter
481 	 * driver will generate resume events that we intercept
482 	 * and convert to insert events.
483 	 */
484 	case DDI_SUSPEND:
485 	case DDI_PM_SUSPEND:
486 		return (DDI_SUCCESS);
487 
488 	default:
489 		return (DDI_FAILURE);
490 	}
491 }
492 
493 /*
494  * card_services_error()
495  *	used to make 2.4/2.5 drivers get an error when
496  *	they try to initialize.
497  */
498 static int
499 card_services_error()
500 {
501 	return (CS_BAD_VERSION);
502 }
503 static int (*cs_error_ptr)() = card_services_error;
504 
505 /*
506  * pcmcia_ctlops
507  *	handle the nexus control operations for the cases where
508  *	a PC Card driver gets called and we need to modify the
509  *	devinfo structure or otherwise do bus specific operations
510  */
511 int
512 pcmcia_ctlops(dev_info_t *dip, dev_info_t *rdip,
513 	ddi_ctl_enum_t ctlop, void *arg, void *result)
514 {
515 	int e;
516 	char name[64];
517 	struct pcmcia_parent_private *ppd;
518 	power_req_t *pm;
519 
520 #if defined(PCMCIA_DEBUG)
521 	if (pcmcia_debug) {
522 		cmn_err(CE_CONT, "pcmcia_ctlops(%p, %p, %d, %p, %p)\n",
523 			(void *)dip, (void *)rdip, ctlop, (void *)arg,
524 			(void *)result);
525 		if (rdip != NULL && ddi_get_name(rdip) != NULL)
526 			cmn_err(CE_CONT, "\t[%s]\n", ddi_get_name(rdip));
527 	}
528 #endif
529 
530 	switch (ctlop) {
531 	case DDI_CTLOPS_REPORTDEV:
532 		if (rdip == (dev_info_t *)0)
533 			return (DDI_FAILURE);
534 
535 		if (strcmp("pcs", ddi_node_name(rdip)) == 0)
536 			cmn_err(CE_CONT, "?PCCard socket %d at %s@%s\n",
537 				ddi_get_instance(rdip),
538 				ddi_driver_name(dip), ddi_get_name_addr(dip));
539 		else
540 			cmn_err(CE_CONT, "?%s%d at %s@%s in socket %d\n",
541 				ddi_driver_name(rdip),
542 				ddi_get_instance(rdip),
543 				ddi_driver_name(dip),
544 				ddi_get_name_addr(dip),
545 				CS_GET_SOCKET_NUMBER(
546 				    ddi_getprop(DDI_DEV_T_NONE, rdip,
547 				    DDI_PROP_DONTPASS,
548 				    PCM_DEV_SOCKET, -1)));
549 
550 		return (DDI_SUCCESS);
551 
552 	case DDI_CTLOPS_INITCHILD:
553 		/*
554 		 * we get control here before the child is called.
555 		 * we can change things if necessary.  This is where
556 		 * the CardServices hook gets planted.
557 		 */
558 #if defined(PCMCIA_DEBUG)
559 		if (pcmcia_debug) {
560 			cmn_err(CE_CONT, "pcmcia: init child: %s(%d) @%p\n",
561 				ddi_node_name(arg), ddi_get_instance(arg),
562 				(void *)arg);
563 			if (DEVI(arg)->devi_binding_name != NULL)
564 				cmn_err(CE_CONT, "\tbinding_name=%s\n",
565 					DEVI(arg)->devi_binding_name);
566 			if (DEVI(arg)->devi_node_name != NULL)
567 				cmn_err(CE_CONT, "\tnode_name=%s\n",
568 					DEVI(arg)->devi_node_name);
569 		}
570 #endif
571 
572 		ppd = (struct pcmcia_parent_private *)
573 			ddi_get_parent_data((dev_info_t *)arg);
574 		if (ppd == NULL)
575 			return (DDI_FAILURE);
576 
577 		if (strcmp("pcs", ddi_node_name((dev_info_t *)arg)) == 0) {
578 			if (ppd == NULL)
579 				return (DDI_FAILURE);
580 			(void) sprintf(name, "%x",
581 			    (int)ppd->ppd_reg[0].phys_hi);
582 			ddi_set_name_addr((dev_info_t *)arg, name);
583 			return (DDI_SUCCESS);
584 		}
585 
586 		/*
587 		 * We don't want driver.conf files that stay in
588 		 * pseudo device form.	It is acceptable to have
589 		 * .conf files add properties only.
590 		 */
591 		if (ndi_dev_is_persistent_node((dev_info_t *)arg) == 0) {
592 			(void) pcmcia_merge_conf((dev_info_t *)arg);
593 			cmn_err(CE_WARN, "%s%d: %s.conf invalid",
594 				ddi_get_name((dev_info_t *)arg),
595 				ddi_get_instance((dev_info_t *)arg),
596 				ddi_get_name((dev_info_t *)arg));
597 			return (DDI_FAILURE);
598 		}
599 
600 
601 #if defined(PCMCIA_DEBUG)
602 		if (pcmcia_debug && ppd != NULL) {
603 			cmn_err(CE_CONT, "\tnreg=%x, intr=%x, socket=%x,"
604 				" function=%x, active=%x, flags=%x\n",
605 				ppd->ppd_nreg, ppd->ppd_intr,
606 				ppd->ppd_socket, ppd->ppd_function,
607 				ppd->ppd_active, ppd->ppd_flags);
608 		}
609 #endif
610 
611 		/*
612 		 * make sure names are relative to socket number
613 		 */
614 		if (ppd->ppd_function > 0) {
615 			int sock;
616 			int func;
617 			sock = ppd->ppd_socket;
618 			func = ppd->ppd_function;
619 			(void) sprintf(name, "%x,%x", sock, func);
620 		} else {
621 			(void) sprintf(name, "%x", ppd->ppd_socket);
622 		}
623 		ddi_set_name_addr((dev_info_t *)arg, name);
624 
625 #if defined(PCMCIA_DEBUG)
626 		if (pcmcia_debug)
627 			cmn_err(CE_CONT, "pcmcia: system init done for %s [%s] "
628 				"nodeid: %x @%s\n",
629 				ddi_get_name(arg), ddi_get_name_addr(arg),
630 				DEVI(arg)->devi_nodeid, name);
631 		if (pcmcia_debug > 1)
632 			pcmcia_dump_minors((dev_info_t *)arg);
633 #endif
634 
635 		return (DDI_SUCCESS);
636 
637 	case DDI_CTLOPS_UNINITCHILD:
638 
639 #if defined(PCMCIA_DEBUG)
640 		if (pcmcia_debug) {
641 			cmn_err(CE_CONT, "pcmcia: uninit child: %s(%d) @%p\n",
642 				ddi_node_name(arg), ddi_get_instance(arg),
643 				(void *)arg);
644 			if (DEVI(arg)->devi_binding_name != NULL)
645 				cmn_err(CE_CONT, "\tbinding_name=%s\n",
646 					DEVI(arg)->devi_binding_name);
647 			if (DEVI(arg)->devi_node_name != NULL)
648 				cmn_err(CE_CONT, "\tnode_name=%s\n",
649 					DEVI(arg)->devi_node_name);
650 		}
651 #endif
652 
653 		ddi_set_name_addr((dev_info_t *)arg, NULL);
654 		ddi_remove_minor_node((dev_info_t *)arg, NULL);
655 		return (DDI_SUCCESS);
656 
657 	case DDI_CTLOPS_SLAVEONLY:
658 		/* PCMCIA devices can't ever be busmaster until CardBus */
659 		ppd = (struct pcmcia_parent_private *)
660 			ddi_get_parent_data(rdip);
661 		if (ppd != NULL && ppd->ppd_flags & PPD_CB_BUSMASTER)
662 			return (DDI_FAILURE); /* at most */
663 		return (DDI_SUCCESS);
664 
665 	case DDI_CTLOPS_SIDDEV:
666 		/* in general this is true. */
667 		return (DDI_SUCCESS);
668 
669 	case DDI_CTLOPS_NREGS:
670 		ppd = (struct pcmcia_parent_private *)
671 			ddi_get_parent_data(rdip);
672 		if (ppd != NULL)
673 			*((uint32_t *)result) = (ppd->ppd_nreg);
674 		else
675 			*((uint32_t *)result) = 0;
676 		return (DDI_SUCCESS);
677 
678 	case DDI_CTLOPS_REGSIZE:
679 		ppd = (struct pcmcia_parent_private *)
680 			ddi_get_parent_data(rdip);
681 		if (ppd != NULL && ppd->ppd_nreg > 0)
682 			*((off_t *)result) =  sizeof (struct pcm_regs);
683 		else
684 			*((off_t *)result) = 0;
685 		return (DDI_SUCCESS);
686 
687 	case DDI_CTLOPS_POWER:
688 		ppd = (struct pcmcia_parent_private *)
689 			ddi_get_parent_data(rdip);
690 
691 		if (ppd == NULL)
692 			return (DDI_FAILURE);
693 		/*
694 		 * if this is not present, don't bother (claim success)
695 		 * since it is already in the right state.  Don't
696 		 * do any resume either since the card insertion will
697 		 * happen independently.
698 		 */
699 		if (!ppd->ppd_active)
700 			return (DDI_SUCCESS);
701 		for (e = 0; e < pcmcia_num_adapters; e++)
702 			if (pcmcia_adapters[e] ==
703 			    pcmcia_sockets[ppd->ppd_socket]->ls_adapter)
704 				break;
705 		if (e == pcmcia_num_adapters)
706 			return (DDI_FAILURE);
707 		pm = (power_req_t *)arg;
708 #if defined(PCMCIA_DEBUG)
709 		if (pcmcia_debug) {
710 			cmn_err(CE_WARN, "power: %d: %p, %d, %d [%s]\n",
711 				pm->request_type,
712 				(void *)pm->req.set_power_req.who,
713 				pm->req.set_power_req.cmpt,
714 				pm->req.set_power_req.level,
715 				ddi_get_name_addr(rdip));
716 		}
717 #endif
718 		e = ppd->ppd_socket;
719 		switch (pm->request_type) {
720 		case PMR_SUSPEND:
721 			if (!(pcmcia_sockets[e]->ls_flags &
722 			    PCS_SUSPENDED)) {
723 				pcmcia_do_suspend(ppd->ppd_socket,
724 				    pcmcia_sockets[e]);
725 			}
726 			ppd->ppd_flags |= PPD_SUSPENDED;
727 			return (DDI_SUCCESS);
728 		case PMR_RESUME:
729 			/* for now, we just succeed since the rest is done */
730 			return (DDI_SUCCESS);
731 		case PMR_SET_POWER:
732 			/*
733 			 * not sure how to handle power control
734 			 * for now, we let the child handle it itself
735 			 */
736 			(void) pcmcia_power(pm->req.set_power_req.who,
737 				pm->req.set_power_req.cmpt,
738 				pm->req.set_power_req.level);
739 			break;
740 		default:
741 			break;
742 		}
743 		return (DDI_FAILURE);
744 		/* These CTLOPS will need to be implemented for new form */
745 		/* let CardServices know about this */
746 
747 	default:
748 		/* if we don't understand, pass up the tree */
749 		/* most things default to general ops */
750 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
751 	}
752 }
753 
754 struct pcmcia_props {
755 	char *name;
756 	int   len;
757 	int   prop;
758 } pcmcia_internal_props[] = {
759 	{ PCM_DEV_ACTIVE, 0, PCMCIA_PROP_ACTIVE },
760 	{ PCM_DEV_R2TYPE, 0, PCMCIA_PROP_R2TYPE },
761 	{ PCM_DEV_CARDBUS, 0, PCMCIA_PROP_CARDBUS },
762 	{ CS_PROP, sizeof (void *), PCMCIA_PROP_OLDCS },
763 	{ "reg", 0, PCMCIA_PROP_REG },
764 	{ "interrupts", sizeof (int), PCMCIA_PROP_INTR },
765 	{ "pm-hardware-state", 0, PCMCIA_PROP_DEFAULT_PM },
766 };
767 
768 /*
769  * pcmcia_prop_decode(name)
770  *	decode the name and determine if this is a property
771  *	we construct on the fly, one we have on the prop list
772  *	or one that requires calling the CIS code.
773  */
774 static int
775 pcmcia_prop_decode(char *name)
776 {
777 	int i;
778 	if (strncmp(name, "cistpl_", 7) == 0)
779 		return (PCMCIA_PROP_CIS);
780 
781 	for (i = 0; i < (sizeof (pcmcia_internal_props) /
782 	    sizeof (struct pcmcia_props)); i++) {
783 		if (strcmp(name, pcmcia_internal_props[i].name) == 0)
784 			return (i);
785 	}
786 
787 	return (PCMCIA_PROP_UNKNOWN);
788 }
789 
790 /*
791  * pcmcia_prop_op()
792  *	we don't have properties in PROM per se so look for them
793  *	only in the devinfo node.  Future may allow us to find
794  *	certain CIS tuples via this interface if a user asks for
795  *	a property of the form "cistpl-<tuplename>" but not yet.
796  *
797  *	The addition of 1275 properties adds to the necessity.
798  */
799 int
800 pcmcia_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
801     ddi_prop_op_t prop_op, int mod_flags,
802     char *name, caddr_t valuep, int *lengthp)
803 {
804 	int len, proplen, which, flags;
805 	caddr_t buff, propptr;
806 	struct pcmcia_parent_private *ppd;
807 
808 	len = *lengthp;
809 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(ch_dip);
810 
811 	switch (which = pcmcia_prop_decode(name)) {
812 	default:
813 		if (ppd == NULL)
814 			return (DDI_PROP_NOT_FOUND);
815 
816 		/* note that proplen may get modified */
817 		proplen = pcmcia_internal_props[which].len;
818 		switch (pcmcia_internal_props[which].prop) {
819 		case PCMCIA_PROP_DEFAULT_PM:
820 			propptr = pcmcia_default_pm_mode;
821 			proplen = strlen(propptr) + 1;
822 			break;
823 		case PCMCIA_PROP_OLDCS:
824 			propptr = (caddr_t)&cs_error_ptr;
825 			break;
826 		case PCMCIA_PROP_REG:
827 			propptr = (caddr_t)ppd->ppd_reg;
828 			proplen = ppd->ppd_nreg * sizeof (struct pcm_regs);
829 			break;
830 		case PCMCIA_PROP_INTR:
831 			propptr = (caddr_t)&ppd->ppd_intr;
832 			break;
833 
834 		/* the next set are boolean values */
835 		case PCMCIA_PROP_ACTIVE:
836 			propptr = NULL;
837 			if (!ppd->ppd_active) {
838 				return (DDI_PROP_NOT_FOUND);
839 			}
840 			break;
841 		case PCMCIA_PROP_R2TYPE:
842 			propptr = NULL;
843 			if (ppd->ppd_flags & PPD_CARD_CARDBUS)
844 				return (DDI_PROP_NOT_FOUND);
845 			break;
846 		case PCMCIA_PROP_CARDBUS:
847 			propptr = NULL;
848 			if (!(ppd->ppd_flags * PPD_CARD_CARDBUS))
849 				return (DDI_PROP_NOT_FOUND);
850 			break;
851 		}
852 
853 		break;
854 
855 	case PCMCIA_PROP_CIS:
856 		/*
857 		 * once we have the lookup code in place
858 		 * it is sufficient to break out of the switch
859 		 * once proplen and propptr are set.
860 		 * The common prop_op code deals with the rest.
861 		 */
862 	case PCMCIA_PROP_UNKNOWN:
863 		return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
864 		    mod_flags | DDI_PROP_NOTPROM,
865 		    name, valuep, lengthp));
866 	}
867 
868 	if (prop_op == PROP_LEN) {
869 		/* just the length */
870 		*lengthp = proplen;
871 		return (DDI_PROP_SUCCESS);
872 	}
873 	switch (prop_op) {
874 	case PROP_LEN_AND_VAL_ALLOC:
875 		if (mod_flags & DDI_PROP_CANSLEEP)
876 			flags = KM_SLEEP;
877 		else
878 			flags = KM_NOSLEEP;
879 		buff = kmem_alloc((size_t)proplen, flags);
880 		if (buff == NULL)
881 			return (DDI_PROP_NO_MEMORY);
882 		*(caddr_t *)valuep = (caddr_t)buff;
883 		break;
884 	case PROP_LEN_AND_VAL_BUF:
885 		buff = (caddr_t)valuep;
886 		if (len < proplen)
887 			return (DDI_PROP_BUF_TOO_SMALL);
888 		break;
889 	default:
890 		break;
891 	}
892 
893 	if (proplen > 0)
894 		bcopy(propptr, buff, proplen);
895 	*lengthp = proplen;
896 	return (DDI_PROP_SUCCESS);
897 }
898 
899 
900 struct regspec *
901 pcmcia_rnum_to_regspec(dev_info_t *dip, int rnumber)
902 {
903 	struct pcmcia_parent_private *ppd;
904 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
905 	if (ppd->ppd_nreg < rnumber)
906 		return (NULL);
907 	return ((struct regspec *)&ppd->ppd_reg[rnumber]);
908 }
909 
910 struct regspec *
911 pcmcia_rnum_to_mapped(dev_info_t *dip, int rnumber)
912 {
913 	struct pcmcia_parent_private *ppd;
914 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
915 	if (ppd->ppd_nreg < rnumber)
916 		return (NULL);
917 	if (ppd->ppd_assigned == NULL)
918 		return (NULL);
919 	if (ppd->ppd_assigned[rnumber].phys_len == 0)
920 		return (NULL);
921 	else
922 		return ((struct regspec *)&ppd->ppd_assigned[rnumber]);
923 }
924 
925 int
926 pcmcia_find_rnum(dev_info_t *dip, struct regspec *reg)
927 {
928 	struct pcmcia_parent_private *ppd;
929 	struct regspec *regp;
930 	int i;
931 
932 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
933 	if (ppd == NULL)
934 		return (-1);
935 	for (regp = (struct regspec *)ppd->ppd_reg, i = 0;
936 	    i < ppd->ppd_nreg; i++, regp++) {
937 		if (bcmp(reg, regp, sizeof (struct regspec)) == 0)
938 			return (i);
939 	}
940 	for (regp = (struct regspec *)ppd->ppd_assigned, i = 0;
941 	    i < ppd->ppd_nreg; i++, regp++) {
942 		if (bcmp(reg, regp, sizeof (struct regspec)) == 0)
943 			return (i);
944 	}
945 
946 	return (-1);
947 }
948 
949 int
950 pcmcia_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
951 	off_t offset, off_t len, caddr_t *vaddrp)
952 {
953 	struct pcm_regs *regs, *mregs = NULL, tmp_reg;
954 	ddi_map_req_t mr = *mp;
955 	ra_return_t ret;
956 	int check, rnum = -1;
957 	uint32_t base;
958 	uchar_t regbuf[sizeof (pci_regspec_t)];
959 
960 	mp = &mr;		/* a copy of original request */
961 
962 	/* check for register number */
963 	switch (mp->map_type) {
964 	case DDI_MT_REGSPEC:
965 		regs = (struct pcm_regs *)mp->map_obj.rp;
966 		mregs = (struct pcm_regs *)mp->map_obj.rp;
967 		/*
968 		 * when using regspec, must not be relocatable
969 		 * and should be from assigned space.
970 		 */
971 		if (!PC_REG_RELOC(regs->phys_hi))
972 			return (DDI_FAILURE);
973 		rnum = pcmcia_find_rnum(rdip, (struct regspec *)mregs);
974 		break;
975 	case DDI_MT_RNUMBER:
976 		regs = (struct pcm_regs *)
977 			pcmcia_rnum_to_regspec(rdip, mp->map_obj.rnumber);
978 		mregs = (struct pcm_regs *)
979 			pcmcia_rnum_to_mapped(rdip, mp->map_obj.rnumber);
980 		rnum = mp->map_obj.rnumber;
981 		if (regs == NULL)
982 			return (DDI_FAILURE);
983 		mp->map_type = DDI_MT_REGSPEC;
984 		mp->map_obj.rp = (struct regspec *)mregs;
985 		break;
986 	default:
987 		return (DDI_ME_INVAL);
988 	}
989 
990 	/* basic sanity checks */
991 	switch (mp->map_op) {
992 	default:
993 		return (DDI_ME_UNIMPLEMENTED);
994 	case DDI_MO_UNMAP:
995 		if (mregs == NULL)
996 			return (DDI_FAILURE);
997 		regs = mregs;
998 		break;
999 	case DDI_MO_MAP_LOCKED:
1000 	case DDI_MO_MAP_HANDLE:
1001 		panic("unsupported bus operation");
1002 		/*NOTREACHED*/
1003 	}
1004 
1005 	/*
1006 	 * we need a private copy for manipulation and
1007 	 * calculation of the correct ranges
1008 	 */
1009 	tmp_reg = *regs;
1010 	mp->map_obj.rp = (struct regspec *)(regs = &tmp_reg);
1011 	base = regs->phys_lo;
1012 	if (base == 0 && offset != 0) {
1013 		/*
1014 		 * for now this is an error.  What does it really mean
1015 		 * to ask for an offset from an address that hasn't
1016 		 * been allocated yet.
1017 		 */
1018 		return (DDI_ME_INVAL);
1019 	}
1020 	regs->phys_lo += (uint32_t)offset;
1021 	if (len != 0) {
1022 		if (len > regs->phys_len) {
1023 			return (DDI_ME_INVAL);
1024 		}
1025 		regs->phys_len = len;
1026 	}
1027 
1028 	/*
1029 	 * basic sanity is checked so now make sure
1030 	 * we can actually allocate something for this
1031 	 * request and then convert to a "standard"
1032 	 * regspec for the next layer up (pci/isa/rootnex/etc.)
1033 	 */
1034 
1035 	switch (PC_GET_REG_TYPE(regs->phys_hi)) {
1036 	case PC_REG_SPACE_IO:
1037 		check = PCA_RES_NEED_IO;
1038 		break;
1039 	case PC_REG_SPACE_MEMORY:
1040 		check = PCA_RES_NEED_MEM;
1041 		break;
1042 	default:
1043 		/* not a valid register type */
1044 		return (DDI_FAILURE);
1045 	}
1046 
1047 	mr.map_type = DDI_MT_REGSPEC;
1048 	ret.ra_addr_hi = 0;
1049 	ret.ra_addr_lo = regs->phys_lo;
1050 	ret.ra_len = regs->phys_len;
1051 	mr.map_obj.rp = pcmcia_cons_regspec(dip,
1052 	    (check == PCA_RES_NEED_IO) ?
1053 	    PCMCIA_MAP_IO : PCMCIA_MAP_MEM,
1054 	    regbuf, &ret);
1055 	switch (mp->map_op) {
1056 	case DDI_MO_UNMAP:
1057 		pcmcia_set_assigned(rdip, rnum, NULL);
1058 		break;
1059 	default:
1060 		break;
1061 	}
1062 	return (ddi_map(dip, &mr, (off_t)0, (off_t)0, vaddrp));
1063 }
1064 
1065 /*
1066  * pcmcia_cons_regspec()
1067  * based on parent's bus type, construct a regspec that is usable
1068  * by that parent to map the resource into the system.
1069  */
1070 #define	PTYPE_PCI	1
1071 #define	PTYPE_ISA	0
1072 struct regspec *
1073 pcmcia_cons_regspec(dev_info_t *dip, int type, uchar_t *buff, ra_return_t *ret)
1074 {
1075 	int ptype = -1, len, bus;
1076 	char device_type[MODMAXNAMELEN + 1];
1077 	dev_info_t *pdip;
1078 	struct regspec *defreg;
1079 	pci_regspec_t *pcireg;
1080 
1081 	pdip = ddi_get_parent(dip);
1082 	if (pdip != ddi_root_node()) {
1083 		/* we're not a child of root so find out what */
1084 		len = sizeof (device_type);
1085 		if (ddi_prop_op(DDI_DEV_T_ANY, pdip, PROP_LEN_AND_VAL_BUF, 0,
1086 			"device_type", (caddr_t)device_type, &len) ==
1087 		    DDI_PROP_SUCCESS) {
1088 			/* check things out */
1089 			if (strcmp(device_type, "pci") == 0)
1090 				ptype = PTYPE_PCI;
1091 			else if (strcmp(device_type, "isa") == 0)
1092 				ptype = PTYPE_ISA;
1093 		}
1094 	}
1095 	switch (ptype) {
1096 	case PTYPE_PCI:
1097 		/* XXX need to look at carefully */
1098 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1099 			"reg", (caddr_t)&pcireg, &len) == DDI_SUCCESS) {
1100 			bus = PCI_REG_BUS_G(pcireg->pci_phys_hi);
1101 			kmem_free(pcireg, len);
1102 		} else {
1103 			bus = 0;
1104 		}
1105 		pcireg = (pci_regspec_t *)buff;
1106 		pcireg->pci_phys_hi = (type == PCMCIA_MAP_IO ? PCI_ADDR_IO :
1107 			PCI_ADDR_MEM32) | PCI_RELOCAT_B | (bus << 16);
1108 		pcireg->pci_phys_mid = ret->ra_addr_hi;
1109 		pcireg->pci_phys_low = ret->ra_addr_lo;
1110 		if (type == PCMCIA_MAP_IO)
1111 			pcireg->pci_phys_low &= 0xFFFF;
1112 		pcireg->pci_size_hi = 0;
1113 		pcireg->pci_size_low = ret->ra_len;
1114 		break;
1115 	default:
1116 		/* default case is to use struct regspec */
1117 		defreg = (struct regspec *)buff;
1118 		defreg->regspec_bustype = type == PCMCIA_MAP_IO ? 1 : 0;
1119 		defreg->regspec_addr = ret->ra_addr_lo;
1120 		defreg->regspec_size = ret->ra_len;
1121 		break;
1122 	}
1123 	return ((struct regspec *)buff);
1124 }
1125 
1126 /*
1127  * pcmcia_init_adapter
1128  *	Initialize the per-adapter structures and check to see if
1129  *	there are possible other instances coming.
1130  */
1131 void
1132 pcmcia_init_adapter(anp_t *adapter, dev_info_t *dip)
1133 {
1134 	int i, n;
1135 	pcmcia_if_t *ls_if;
1136 
1137 	i = pcmcia_num_adapters++;
1138 	pcmcia_adapters[i] = kmem_zalloc(sizeof (struct pcmcia_adapter),
1139 	    KM_SLEEP);
1140 	pcmcia_adapters[i]->pca_dip = dip;
1141 	/* should this be pca_winshift??? */
1142 	pcmcia_adapters[i]->pca_module = ddi_name_to_major(ddi_get_name(dip));
1143 	pcmcia_adapters[i]->pca_unit = ddi_get_instance(dip);
1144 	pcmcia_adapters[i]->pca_iblock = adapter->an_iblock;
1145 	pcmcia_adapters[i]->pca_idev = adapter->an_idev;
1146 	pcmcia_adapters[i]->pca_if = ls_if = adapter->an_if;
1147 	pcmcia_adapters[i]->pca_number = i;
1148 	(void) strcpy(pcmcia_adapters[i]->pca_name, ddi_get_name(dip));
1149 	pcmcia_adapters[i]->
1150 		pca_name[sizeof (pcmcia_adapters[i]->pca_name) - 1] = NULL;
1151 
1152 	if (ls_if != NULL) {
1153 		inquire_adapter_t conf;
1154 		int sock, win;
1155 
1156 		if (ls_if->pcif_inquire_adapter != NULL)
1157 			GET_CONFIG(ls_if, dip, &conf);
1158 
1159 		/* resources - assume worst case and fix from there */
1160 		pcmcia_adapters[i]->pca_flags = PCA_RES_NEED_IRQ |
1161 			PCA_RES_NEED_IO | PCA_RES_NEED_MEM;
1162 		/* indicate first socket not initialized */
1163 		pcmcia_adapters[i]->pca_first_socket = -1;
1164 
1165 		if (conf.ResourceFlags & RES_OWN_IRQ)
1166 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_IRQ;
1167 		if (conf.ResourceFlags & RES_OWN_IO)
1168 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_IO;
1169 		if (conf.ResourceFlags & RES_OWN_MEM)
1170 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_MEM;
1171 		if (conf.ResourceFlags & RES_IRQ_SHAREABLE)
1172 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_SHAREABLE;
1173 		if (conf.ResourceFlags & RES_IRQ_NEXUS)
1174 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_SMI_SHARE;
1175 
1176 		/* need to know interrupt limitations */
1177 		if (conf.ActiveLow) {
1178 			pcmcia_adapters[i]->pca_avail_intr = conf.ActiveLow;
1179 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_ISA;
1180 		} else
1181 			pcmcia_adapters[i]->pca_avail_intr = conf.ActiveHigh;
1182 
1183 		/* power entries for adapter */
1184 		pcmcia_adapters[i]->pca_power =
1185 			conf.power_entry;
1186 		pcmcia_adapters[i]->pca_numpower =
1187 			conf.NumPower;
1188 
1189 		for (n = 0; n < conf.NumPower; n++)
1190 			pcmcia_merge_power(&conf.power_entry[n]);
1191 
1192 		/* now setup the per socket info */
1193 		for (sock = 0; sock < conf.NumSockets;
1194 		    sock++) {
1195 			dev_info_t *sockdrv = NULL;
1196 			sockdrv = pcmcia_number_socket(dip, sock);
1197 			if (sockdrv == NULL)
1198 				n = sock + pcmcia_num_sockets;
1199 			else {
1200 				n = ddi_get_instance(sockdrv);
1201 			}
1202 			/* make sure we know first socket on adapter */
1203 			if (pcmcia_adapters[i]->pca_first_socket == -1)
1204 				pcmcia_adapters[i]->pca_first_socket = n;
1205 
1206 			/*
1207 			 * the number of sockets is weird.
1208 			 * we might have only two sockets but
1209 			 * due to persistence of instances we
1210 			 * will need to call them something other
1211 			 * than 0 and 1.  So, we use the largest
1212 			 * instance number as the number and
1213 			 * have some that just don't get used.
1214 			 */
1215 			if (n >= pcmcia_num_sockets)
1216 				pcmcia_num_sockets = n + 1;
1217 #if defined(PCMCIA_DEBUG)
1218 			if (pcmcia_debug) {
1219 				cmn_err(CE_CONT,
1220 					"pcmcia_init: new socket added %d "
1221 					"(%d)\n",
1222 					n, pcmcia_num_sockets);
1223 			}
1224 #endif
1225 
1226 			pcmcia_sockets[n] =
1227 				kmem_zalloc(sizeof (pcmcia_logical_socket_t),
1228 				KM_SLEEP);
1229 			pcmcia_sockets[n]->ls_socket = sock;
1230 			pcmcia_sockets[n]->ls_if = ls_if;
1231 			pcmcia_sockets[n]->ls_adapter =
1232 				pcmcia_adapters[i];
1233 			pcmcia_sockets[n]->ls_cs_events = 0L;
1234 			pcmcia_sockets[n]->ls_sockdrv = sockdrv;
1235 			/* Prototype of intrspec */
1236 			pcmcia_sockets[n]->ls_intr_pri =
1237 				adapter->an_ipl;
1238 #if defined(PCMCIA_DEBUG)
1239 			if (pcmcia_debug)
1240 				cmn_err(CE_CONT,
1241 					"phys sock %d, log sock %d\n",
1242 					sock, n);
1243 #endif
1244 			mutex_init(&pcmcia_sockets[n]->ls_ilock, NULL,
1245 				MUTEX_DRIVER, *adapter->an_iblock);
1246 		}
1247 
1248 		pcmcia_adapters[i]->pca_numsockets = conf.NumSockets;
1249 		/* now setup the per window information */
1250 		for (win = 0; win < conf.NumWindows; win++) {
1251 			n = win + pcmcia_num_windows;
1252 			pcmcia_windows[n] =
1253 				kmem_zalloc(sizeof (pcmcia_logical_window_t),
1254 				    KM_SLEEP);
1255 			pcmcia_windows[n]->lw_window = win;
1256 			pcmcia_windows[n]->lw_if = ls_if;
1257 			pcmcia_windows[n]->lw_adapter =
1258 				pcmcia_adapters[i];
1259 		}
1260 		pcmcia_num_windows += conf.NumWindows;
1261 		SET_CALLBACK(ls_if, dip,
1262 		    pcm_adapter_callback, i);
1263 
1264 		/* now tell CS about each socket */
1265 		for (sock = 0; sock < pcmcia_num_sockets; sock++) {
1266 #if defined(PCMCIA_DEBUG)
1267 			if (pcmcia_debug) {
1268 				cmn_err(CE_CONT,
1269 					"pcmcia_init: notify CS socket %d "
1270 					"sockp=%p\n",
1271 					sock, (void *)pcmcia_sockets[sock]);
1272 			}
1273 #endif
1274 			if (pcmcia_sockets[sock] == NULL ||
1275 			    (pcmcia_sockets[sock]->ls_flags &
1276 				PCS_SOCKET_ADDED)) {
1277 				/* skip the ones that are done already */
1278 				continue;
1279 			}
1280 			pcmcia_sockets[sock]->ls_flags |= PCS_SOCKET_ADDED;
1281 			if (cs_event(PCE_ADD_SOCKET, sock, 0) !=
1282 			    CS_SUCCESS) {
1283 				/* flag socket as broken */
1284 				pcmcia_sockets[sock]->ls_flags = 0;
1285 			} else {
1286 				pcm_event_manager(PCE_ADD_SOCKET,
1287 				    sock, NULL);
1288 			}
1289 		}
1290 
1291 	}
1292 #if defined(PCMCIA_DEBUG)
1293 	if (pcmcia_debug) {
1294 		cmn_err(CE_CONT, "logical sockets:\n");
1295 		for (i = 0; i < pcmcia_num_sockets; i++) {
1296 			if (pcmcia_sockets[i] == NULL)
1297 				continue;
1298 			cmn_err(CE_CONT,
1299 				"\t%d: phys sock=%d, if=%p, adapt=%p\n",
1300 				i, pcmcia_sockets[i]->ls_socket,
1301 				(void *)pcmcia_sockets[i]->ls_if,
1302 				(void *)pcmcia_sockets[i]->ls_adapter);
1303 		}
1304 		cmn_err(CE_CONT, "logical windows:\n");
1305 		for (i = 0; i < pcmcia_num_windows; i++) {
1306 			cmn_err(CE_CONT,
1307 				"\t%d: phys_window=%d, if=%p, adapt=%p\n",
1308 				i, pcmcia_windows[i]->lw_window,
1309 				(void *)pcmcia_windows[i]->lw_if,
1310 				(void *)pcmcia_windows[i]->lw_adapter);
1311 		}
1312 		cmn_err(CE_CONT, "\tpcmcia_num_power=%d\n", pcmcia_num_power);
1313 		for (n = 0; n < pcmcia_num_power; n++)
1314 			cmn_err(CE_CONT,
1315 				"\t\tPowerLevel: %d\tValidSignals: %x\n",
1316 				pcmcia_power_table[n].PowerLevel,
1317 				pcmcia_power_table[n].ValidSignals);
1318 	}
1319 #endif
1320 }
1321 
1322 /*
1323  * pcmcia_find_cards()
1324  *	check the adapter to see if there are cards present at
1325  *	driver attach time.  If there are, generate an artificial
1326  *	card insertion event to get CS running and the PC Card ultimately
1327  *	identified.
1328  */
1329 void
1330 pcmcia_find_cards(anp_t *adapt)
1331 {
1332 	int i;
1333 	get_ss_status_t status;
1334 	for (i = 0; i < pcmcia_num_sockets; i++) {
1335 		if (pcmcia_sockets[i] &&
1336 		    pcmcia_sockets[i]->ls_if == adapt->an_if) {
1337 			/* check the status */
1338 			status.socket = i;
1339 			if (SSGetStatus(&status) == SUCCESS &&
1340 			    status.IFType != IF_CARDBUS &&
1341 			    status.CardState & SBM_CD &&
1342 			    pcmcia_sockets[i]->ls_dip[0] == NULL) {
1343 				(void) cs_event(PCE_CARD_INSERT, i, 0);
1344 				delay(1);
1345 			}
1346 		}
1347 	}
1348 }
1349 
1350 /*
1351  * pcmcia_number_socket(dip, adapt)
1352  *	we determine socket number by creating a driver for each
1353  *	socket on the adapter and then forcing it to attach.  This
1354  *	results in an instance being assigned which becomes the
1355  *	logical socket number.	If it fails, then we are the first
1356  *	set of sockets and renumbering occurs later.  We do this
1357  *	one socket at a time and return the dev_info_t so the
1358  *	instance number can be used.
1359  */
1360 dev_info_t *
1361 pcmcia_number_socket(dev_info_t *dip, int localsocket)
1362 {
1363 	dev_info_t *child = NULL;
1364 	struct pcmcia_parent_private *ppd;
1365 
1366 	if (ndi_devi_alloc(dip, "pcs", (pnode_t)DEVI_SID_NODEID,
1367 	    &child) == NDI_SUCCESS) {
1368 		ppd = kmem_zalloc(sizeof (struct pcmcia_parent_private),
1369 		    KM_SLEEP);
1370 		ppd->ppd_reg = kmem_zalloc(sizeof (struct pcm_regs), KM_SLEEP);
1371 		ppd->ppd_nreg = 1;
1372 		ppd->ppd_reg[0].phys_hi = localsocket;
1373 		ddi_set_parent_data(child, (caddr_t)ppd);
1374 		if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
1375 			(void) ndi_devi_free(child);
1376 			child = NULL;
1377 		}
1378 	}
1379 	return (child);
1380 }
1381 
1382 /*
1383  * pcm_phys_to_log_socket()
1384  *	from an adapter and socket number return the logical socket
1385  */
1386 int
1387 pcm_phys_to_log_socket(struct pcmcia_adapter *adapt, int socket)
1388 {
1389 	register pcmcia_logical_socket_t *sockp;
1390 	int i;
1391 
1392 	for (i = 0, sockp = pcmcia_sockets[0];
1393 		i < pcmcia_num_sockets; i++, sockp = pcmcia_sockets[i]) {
1394 		if (sockp == NULL)
1395 			continue;
1396 		if (sockp->ls_socket == socket && sockp->ls_adapter == adapt)
1397 			break;
1398 	}
1399 	if (i >= pcmcia_num_sockets) {
1400 #if defined(PCMCIA_DEBUG)
1401 		if (pcmcia_debug)
1402 			cmn_err(CE_CONT,
1403 				"\tbad socket/adapter: %x/%p != %x/%x\n",
1404 				socket, (void *)adapt, pcmcia_num_sockets,
1405 				pcmcia_num_adapters);
1406 #endif
1407 		return (-1);
1408 	}
1409 
1410 	return (i);		/* want logical socket */
1411 }
1412 
1413 /*
1414  * pcm_adapter_callback()
1415  *	this function is called back by the adapter driver at interrupt time.
1416  *	It is here that events should get generated for the event manager if it
1417  *	is present.  It would also be the time where a device information
1418  *	tree could be constructed for a card that was added in if we
1419  *	choose to create them dynamically.
1420  */
1421 
1422 #if defined(PCMCIA_DEBUG)
1423 char *cblist[] = {
1424 	"removal",
1425 	"insert",
1426 	"ready",
1427 	"battery-warn",
1428 	"battery-dead",
1429 	"status-change",
1430 	"write-protect", "reset", "unlock", "client-info", "eject-complete",
1431 	"eject-request", "erase-complete", "exclusive-complete",
1432 	"exclusive-request", "insert-complete", "insert-request",
1433 	"reset-complete", "reset-request", "timer-expired",
1434 	"resume", "suspend"
1435 };
1436 #endif
1437 
1438 /*ARGSUSED*/
1439 static int
1440 pcm_adapter_callback(dev_info_t *dip, int adapter, int event, int socket)
1441 {
1442 	pcmcia_logical_socket_t *sockp;
1443 
1444 #if defined(PCMCIA_DEBUG)
1445 	if (pcmcia_debug) {
1446 		cmn_err(CE_CONT, "pcm_adapter_callback: %p %x %x %x: ",
1447 			(void *)dip, adapter, event, socket);
1448 		cmn_err(CE_CONT, "[%s]\n", cblist[event]);
1449 	}
1450 #endif
1451 
1452 	if (adapter >= pcmcia_num_adapters || adapter < 0) {
1453 #if defined(PCMCIA_DEBUG)
1454 		if (pcmcia_debug)
1455 			cmn_err(CE_CONT, "\tbad adapter number: %d : %d\n",
1456 				adapter, pcmcia_num_adapters);
1457 #endif
1458 		return (1);
1459 	}
1460 
1461 	/* get the logical socket since that is what CS knows */
1462 	socket = pcm_phys_to_log_socket(pcmcia_adapters[adapter], socket);
1463 	if (socket == -1) {
1464 		cmn_err(CE_WARN, "pcmcia callback - bad logical socket\n");
1465 		return (0);
1466 	}
1467 	sockp = pcmcia_sockets[socket];
1468 	switch (event) {
1469 	case -1:		/* special case of adapter going away */
1470 	case PCE_CARD_INSERT:
1471 		sockp->ls_cs_events |= PCE_E2M(PCE_CARD_INSERT) |
1472 			PCE_E2M(PCE_CARD_REMOVAL);
1473 		break;
1474 	case PCE_CARD_REMOVAL:
1475 				/* disable interrupts at this point */
1476 		sockp->ls_cs_events |= PCE_E2M(PCE_CARD_INSERT) |
1477 			PCE_E2M(PCE_CARD_REMOVAL);
1478 		/* remove children that never attached */
1479 
1480 		break;
1481 	case PCE_PM_RESUME:
1482 		pcmcia_do_resume(socket, sockp);
1483 		/* event = PCE_CARD_INSERT; */
1484 		break;
1485 	case PCE_PM_SUSPEND:
1486 		pcmcia_do_suspend(socket, sockp);
1487 		/* event = PCE_CARD_REMOVAL; */
1488 		break;
1489 	default:
1490 		/* nothing to do */
1491 		break;
1492 	}
1493 
1494 #if defined(PCMCIA_DEBUG)
1495 	if (pcmcia_debug) {
1496 		cmn_err(CE_CONT,
1497 			"\tevent %d, event mask=%x, match=%x (log socket=%d)\n",
1498 			event,
1499 			(int)sockp->ls_cs_events,
1500 			(int)(sockp->ls_cs_events & PCE_E2M(event)), socket);
1501 	}
1502 #endif
1503 
1504 	if (pcmcia_cs_event && sockp->ls_cs_events & (1 << event)) {
1505 #if defined(PCMCIA_DEBUG)
1506 		if (pcmcia_debug)
1507 			cmn_err(CE_CONT, "\tcalling CS event handler (%p) "
1508 				"with event=%d\n",
1509 				(void *)pcmcia_cs_event, event);
1510 #endif
1511 		CS_EVENT(event, socket, 0);
1512 	}
1513 
1514 	/* let the event manager(s) know about the event */
1515 	pcm_event_manager(event, socket, NULL);
1516 
1517 	return (0);
1518 }
1519 
1520 /*
1521  * pcm_event_manager()
1522  *	checks for registered management driver callback handlers
1523  *	if there are any, call them if the event warrants it
1524  */
1525 void
1526 pcm_event_manager(int event, int socket, void *arg)
1527 {
1528 	struct pcmcia_mif *mif;
1529 
1530 	for (mif = pcmcia_mif_handlers; mif != NULL; mif = mif->mif_next) {
1531 #if defined(PCMCIA_DEBUG)
1532 		if (pcmcia_debug)
1533 			cmn_err(CE_CONT,
1534 				"pcm_event_manager: event=%d, mif_events=%x"
1535 				" (tst:%d)\n",
1536 				event, (int)*(uint32_t *)mif->mif_events,
1537 				PR_GET(mif->mif_events, event));
1538 #endif
1539 		if (PR_GET(mif->mif_events, event)) {
1540 			mif->mif_function(mif->mif_id, event, socket, arg);
1541 		}
1542 	}
1543 
1544 }
1545 
1546 /*
1547  * pcm_search_devinfo(dev_info_t *, pcm_device_info *, int)
1548  * search for an immediate child node to the nexus and not siblings of nexus
1549  * and not grandchildren.  We follow the same sequence that name binding
1550  * follows so we match same class of device (modem == modem) and don't
1551  * have to depend on features that might not exist.
1552  */
1553 dev_info_t *
1554 pcm_search_devinfo(dev_info_t *self, struct pcm_device_info *info, int socket)
1555 {
1556 	char bf[256];
1557 	struct pcmcia_parent_private *ppd;
1558 	dev_info_t *dip;
1559 	int circular;
1560 
1561 #if defined(PCMCIA_DEBUG)
1562 	if (pcmcia_debug)
1563 		cmn_err(CE_CONT,
1564 		    "pcm_search_devinfo: socket=%x [%s|%s|%s] pd_flags=%x\n",
1565 		    socket, info->pd_bind_name, info->pd_generic_name,
1566 		    info->pd_vers1_name, info->pd_flags);
1567 #endif
1568 
1569 	ndi_devi_enter(self, &circular);
1570 	/* do searches in compatible property order */
1571 	for (dip = (dev_info_t *)DEVI(self)->devi_child;
1572 	    dip != NULL;
1573 	    dip = (dev_info_t *)DEVI(dip)->devi_sibling) {
1574 		int ppd_socket;
1575 		ppd = (struct pcmcia_parent_private *)
1576 			ddi_get_parent_data(dip);
1577 		if (ppd == NULL) {
1578 #if defined(PCMCIA_DEBUG)
1579 			cmn_err(CE_WARN, "No parent private data\n");
1580 #endif
1581 			continue;
1582 		}
1583 		ppd_socket = CS_MAKE_SOCKET_NUMBER(ppd->ppd_socket,
1584 		    ppd->ppd_function);
1585 #if defined(PCMCIA_DEBUG)
1586 		if (pcmcia_debug) {
1587 			cmn_err(CE_CONT, "\tbind=[%s], node=[%s]\n",
1588 				DEVI(dip)->devi_binding_name,
1589 				DEVI(dip)->devi_node_name);
1590 		}
1591 #endif
1592 		if (info->pd_flags & PCM_NAME_VERS1) {
1593 			(void) strcpy(bf, info->pd_vers1_name);
1594 			pcmcia_fix_string(bf);
1595 			if (DEVI(dip)->devi_binding_name &&
1596 			    strcmp(DEVI(dip)->devi_binding_name, bf) == 0 &&
1597 			    socket == ppd_socket)
1598 				break;
1599 		}
1600 		if ((info->pd_flags & (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) ==
1601 		    (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) {
1602 			(void) sprintf(bf, "%s,%x", info->pd_bind_name,
1603 				info->pd_function);
1604 			if (strcmp(bf, DEVI(dip)->devi_binding_name) == 0 &&
1605 			    socket == ppd->ppd_socket)
1606 				break;
1607 		}
1608 		if (info->pd_flags & PCM_NAME_1275) {
1609 			if (DEVI(dip)->devi_binding_name &&
1610 			    strcmp(DEVI(dip)->devi_binding_name,
1611 				info->pd_bind_name) == 0 &&
1612 			    socket == ppd_socket)
1613 				break;
1614 		}
1615 		if (info->pd_flags & PCM_NAME_GENERIC) {
1616 			(void) sprintf(bf, "%s,%s", PCMDEV_NAMEPREF,
1617 				info->pd_generic_name);
1618 			if (DEVI(dip)->devi_binding_name &&
1619 			    strcmp(DEVI(dip)->devi_binding_name, bf) == 0 &&
1620 			    socket == ppd_socket)
1621 				break;
1622 		}
1623 		if (info->pd_flags & PCM_NAME_GENERIC) {
1624 			if (DEVI(dip)->devi_binding_name &&
1625 			    strcmp(DEVI(dip)->devi_binding_name,
1626 				info->pd_generic_name) == 0 &&
1627 			    socket == ppd_socket)
1628 				break;
1629 		}
1630 		if (info->pd_flags & PCM_NO_CONFIG) {
1631 			if (DEVI(dip)->devi_binding_name &&
1632 			    strcmp(DEVI(dip)->devi_binding_name,
1633 					"pccard,memory") == 0 &&
1634 			    socket == ppd_socket)
1635 				break;
1636 		}
1637 	}
1638 	ndi_devi_exit(self, circular);
1639 	return (dip);
1640 }
1641 
1642 /*
1643  * pcm_find_devinfo()
1644  *	this is a wrapper around DDI calls to "find" any
1645  *	devinfo node and then from there find the one associated
1646  *	with the socket
1647  */
1648 dev_info_t *
1649 pcm_find_devinfo(dev_info_t *pdip, struct pcm_device_info *info, int socket)
1650 {
1651 	dev_info_t *dip;
1652 
1653 	dip = pcm_search_devinfo(pdip, info, socket);
1654 	if (dip == NULL)
1655 		return (NULL);
1656 	/*
1657 	 * we have at least a base level dip
1658 	 * see if there is one (this or a sibling)
1659 	 * that has the correct socket number
1660 	 * if there is, return that one else
1661 	 * NULL so a new one is created
1662 	 */
1663 #if defined(PCMCIA_DEBUG)
1664 	if (pcmcia_debug)
1665 		cmn_err(CE_CONT, "find: initial dip = %p, socket=%d, name=%s "
1666 			"(instance=%d, socket=%d, name=%s)\n",
1667 			(void *)dip, socket, info->pd_bind_name,
1668 			ddi_get_instance(dip),
1669 			ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1670 			PCM_DEV_SOCKET, -1),
1671 			ddi_get_name(dip));
1672 #endif
1673 
1674 #if defined(PCMCIA_DEBUG)
1675 	if (pcmcia_debug && dip != NULL)
1676 		cmn_err(CE_CONT, "\treturning non-NULL dip (%s)\n",
1677 			ddi_get_name(dip));
1678 #endif
1679 	return (dip);
1680 }
1681 
1682 /*
1683  * pcm_find_parent_dip(socket)
1684  *	find the correct parent dip for this logical socket
1685  */
1686 dev_info_t *
1687 pcm_find_parent_dip(int socket)
1688 {
1689 	if ((socket < 0 || socket >= pcmcia_num_sockets) ||
1690 	    pcmcia_sockets[socket] == NULL)
1691 		return (NULL);
1692 	return (pcmcia_sockets[socket]->ls_adapter->pca_dip);
1693 }
1694 
1695 /*
1696  * pcmcia_set_em_handler()
1697  *	This is called by the management and event driver to tell
1698  *	the nexus what to call.	 Multiple drivers are allowed
1699  *	but normally only one will exist.
1700  */
1701 int
1702 pcmcia_set_em_handler(int (*handler)(), caddr_t events, int elen,
1703 			uint32_t id, void **cs, void **ss)
1704 {
1705 	struct pcmcia_mif *mif, *tmp;
1706 
1707 	if (handler == NULL) {
1708 		/* NULL means remove the handler based on the ID */
1709 		if (pcmcia_mif_handlers == NULL)
1710 			return (0);
1711 		mutex_enter(&pcmcia_global_lock);
1712 		if (pcmcia_mif_handlers->mif_id == id) {
1713 			mif = pcmcia_mif_handlers;
1714 			pcmcia_mif_handlers = mif->mif_next;
1715 			kmem_free(mif, sizeof (struct pcmcia_mif));
1716 		} else {
1717 			for (mif = pcmcia_mif_handlers;
1718 			    mif->mif_next != NULL &&
1719 			    mif->mif_next->mif_id != id;
1720 			    mif = mif->mif_next)
1721 				;
1722 			if (mif->mif_next != NULL &&
1723 			    mif->mif_next->mif_id == id) {
1724 				tmp = mif->mif_next;
1725 				mif->mif_next = tmp->mif_next;
1726 				kmem_free(tmp, sizeof (struct pcmcia_mif));
1727 			}
1728 		}
1729 		mutex_exit(&pcmcia_global_lock);
1730 	} else {
1731 
1732 		if (pcmcia_num_adapters == 0) {
1733 			return (ENXIO);
1734 		}
1735 		if (elen > EM_EVENTSIZE)
1736 			return (EINVAL);
1737 
1738 		mif = (struct pcmcia_mif *)
1739 			kmem_zalloc(sizeof (struct pcmcia_mif),
1740 			    KM_NOSLEEP);
1741 		if (mif == NULL)
1742 			return (ENOSPC);
1743 
1744 		mif->mif_function = (void (*)())handler;
1745 		bcopy(events, mif->mif_events, elen);
1746 		mif->mif_id = id;
1747 		mutex_enter(&pcmcia_global_lock);
1748 		mif->mif_next = pcmcia_mif_handlers;
1749 		pcmcia_mif_handlers = mif;
1750 		if (cs != NULL)
1751 			*cs = (void *)pcmcia_card_services;
1752 		if (ss != NULL) {
1753 			*ss = (void *)SocketServices;
1754 		}
1755 
1756 		mutex_exit(&pcmcia_global_lock);
1757 	}
1758 	return (0);
1759 }
1760 
1761 /*
1762  * pcm_fix_bits(uchar_t *data, int num, int dir)
1763  *	shift socket bits left(0) or right(0)
1764  *	This is used when mapping logical and physical
1765  */
1766 void
1767 pcm_fix_bits(socket_enum_t src, socket_enum_t dst, int num, int dir)
1768 {
1769 	int i;
1770 
1771 	PR_ZERO(dst);
1772 
1773 	if (dir == 0) {
1774 				/* LEFT */
1775 		for (i = 0; i <= (sizeof (dst) * PR_WORDSIZE) - num; i++) {
1776 			if (PR_GET(src, i))
1777 				PR_SET(dst, i + num);
1778 		}
1779 	} else {
1780 				/* RIGHT */
1781 		for (i = num; i < sizeof (dst) * PR_WORDSIZE; i++) {
1782 			if (PR_GET(src, i))
1783 			    PR_SET(dst, i - num);
1784 		}
1785 	}
1786 }
1787 
1788 uint32_t
1789 genmask(int len)
1790 {
1791 	uint32_t mask;
1792 	for (mask = 0; len > 0; len--) {
1793 		mask |= 1 << (len - 1);
1794 	}
1795 	return (mask);
1796 }
1797 
1798 int
1799 genp2(int val)
1800 {
1801 	int i;
1802 	if (val == 0)
1803 		return (0);
1804 	for (i = 0; i < 32; i++)
1805 		if (val > (1 << i))
1806 			return (i);
1807 	return (0);
1808 }
1809 
1810 #if defined(PCMCIA_DEBUG)
1811 char *ssfuncs[128] = {
1812 	"GetAdapter", "GetPage", "GetSocket", "GetStatus", "GetWindow",
1813 	"InquireAdapter", "InquireSocket", "InquireWindow", "ResetSocket",
1814 	"SetPage", "SetAdapter", "SetSocket", "SetWindow", "SetIRQHandler",
1815 	"ClearIRQHandler",
1816 	/* 15 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1817 	/* 25 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1818 	/* 35 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1819 	/* 45 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1820 	/* 55 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1821 	/* 65 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1822 	/* 75 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1823 	/* 85 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1824 	/* 95 */ NULL, NULL, NULL,
1825 	"CSIsActiveDip",
1826 	"CSInitDev", "CSRegister", "CSCISInit", "CSUnregister",
1827 	"CISGetAddress", "CISSetAddress", "CSCardRemoved", "CSGetCookiesAndDip"
1828 };
1829 #endif
1830 
1831 /*
1832  * SocketServices
1833  *	general entrypoint for Card Services to find
1834  *	Socket Services.  Finding the entry requires
1835  *	a _depends_on[] relationship.
1836  *
1837  *	In some cases, the work is done locally but usually
1838  *	the parameters are adjusted and the adapter driver
1839  *	code asked to do the work.
1840  */
1841 int
1842 SocketServices(int function, ...)
1843 {
1844 	va_list arglist;
1845 	uint32_t args[16];
1846 	csregister_t *reg;
1847 	sservice_t *serv;
1848 	dev_info_t *dip;
1849 	int socket, func;
1850 	int error = SUCCESS;
1851 	pcmcia_logical_socket_t *sockp;
1852 
1853 	va_start(arglist, function);
1854 
1855 #if defined(PCMCIA_DEBUG)
1856 	if (pcmcia_debug > 1)
1857 		cmn_err(CE_CONT, "SocketServices called for function %d [%s]\n",
1858 			function,
1859 			((function < 128) && ssfuncs[function] != NULL) ?
1860 			ssfuncs[function] : "UNKNOWN");
1861 #endif
1862 	switch (function) {
1863 	case CSRegister:
1864 	case CISGetAddress:
1865 	case CISSetAddress:
1866 
1867 		reg = va_arg(arglist, csregister_t *);
1868 
1869 		if (reg->cs_magic != PCCS_MAGIC ||
1870 		    reg->cs_version != PCCS_VERSION) {
1871 			cmn_err(CE_WARN,
1872 				"pcmcia: CSRegister (%x, %x, %p, %p) *ERROR*",
1873 				reg->cs_magic, reg->cs_version,
1874 				(void *)reg->cs_card_services,
1875 				(void *)reg->cs_event);
1876 			error = BAD_FUNCTION;
1877 			break;
1878 		}
1879 
1880 		switch (function) {
1881 		case CISGetAddress:
1882 			reg->cs_event = pcmcia_cis_parser;
1883 			break;
1884 		case CISSetAddress:
1885 			pcmcia_cis_parser = reg->cs_event;
1886 			break;
1887 		case CSRegister:
1888 			break;
1889 		}
1890 		break;
1891 
1892 	case CSUnregister:
1893 		break;
1894 
1895 	case CSCISInit:
1896 		args[0] = va_arg(arglist, int);
1897 #if defined(PCMCIA_DEBUG)
1898 		if (pcmcia_debug)
1899 			cmn_err(CE_CONT,
1900 				"CSCISInit: CIS is initialized on socket %d\n",
1901 				(int)args[0]);
1902 #endif
1903 		/*
1904 		 * now that the CIS has been parsed (there may not
1905 		 * be one but the work is done) we can create the
1906 		 * device information structures.
1907 		 *
1908 		 * we serialize the node creation to avoid problems
1909 		 * with initial probe/attach of nexi.
1910 		 */
1911 
1912 		mutex_enter(&pcmcia_global_lock);
1913 		pcmcia_create_dev_info(args[0]);
1914 		cv_broadcast(&pcmcia_condvar); /* wakeup the nexus attach */
1915 		mutex_exit(&pcmcia_global_lock);
1916 		break;
1917 
1918 	case CSInitDev:
1919 #if defined(PCMCIA_DEBUG)
1920 		if (pcmcia_debug)
1921 			cmn_err(CE_CONT, "CSInitDev: initialize device\n");
1922 #endif
1923 		/*
1924 		 * this is where we create the /devices entries
1925 		 * that let us out into the world
1926 		 */
1927 
1928 		(void) pcmcia_create_device(va_arg(arglist,
1929 		    ss_make_device_node_t *));
1930 		break;
1931 
1932 	case CSCardRemoved:
1933 		args[0] = va_arg(arglist, uint32_t);
1934 		socket = CS_GET_SOCKET_NUMBER(args[0]);
1935 		func = CS_GET_FUNCTION_NUMBER(args[0]);
1936 #if defined(PCMCIA_DEBUG)
1937 		if (pcmcia_debug)
1938 			cmn_err(CE_CONT,
1939 				"CSCardRemoved! (socket=%d)\n", (int)args[0]);
1940 #endif
1941 		if (socket >= pcmcia_num_sockets)
1942 			break;
1943 
1944 		sockp = pcmcia_sockets[socket];
1945 		if (sockp == NULL) {
1946 			cmn_err(CE_WARN,
1947 			    "pcmcia: bad socket = %x", socket);
1948 			break;
1949 		}
1950 
1951 		for (func = 0; func < sockp->ls_functions; func++) {
1952 			/*
1953 			 * break the association of dip and socket
1954 			 * for all functions on that socket
1955 			 */
1956 			dip = sockp->ls_dip[func];
1957 			sockp->ls_dip[func] = NULL;
1958 			if (dip != NULL) {
1959 				struct pcmcia_parent_private *ppd;
1960 				ppd = (struct pcmcia_parent_private *)
1961 				    ddi_get_parent_data(dip);
1962 				ppd->ppd_active = 0;
1963 				(void) ndi_devi_offline(dip,
1964 				    NDI_DEVI_REMOVE);
1965 			}
1966 #if defined(PCMCIA_DEBUG)
1967 			else {
1968 				if (pcmcia_debug)
1969 					cmn_err(CE_CONT,
1970 					    "CardRemoved: no "
1971 					    "dip present "
1972 					    "on socket %d!\n",
1973 					    (int)args[0]);
1974 			}
1975 #endif
1976 		}
1977 		if (sockp->ls_flags & PCS_SUSPENDED) {
1978 			mutex_enter(&pcmcia_global_lock);
1979 			sockp->ls_flags &= ~PCS_SUSPENDED;
1980 			cv_broadcast(&pcmcia_condvar);
1981 			mutex_exit(&pcmcia_global_lock);
1982 		}
1983 		break;
1984 
1985 	case CSGetCookiesAndDip:
1986 		serv = va_arg(arglist, sservice_t *);
1987 		if (serv != NULL)
1988 			error = GetCookiesAndDip(serv);
1989 		else
1990 			error = BAD_SOCKET;
1991 		break;
1992 
1993 	case CSGetActiveDip:
1994 		/*
1995 		 * get the dip associated with the card currently
1996 		 * in the specified socket
1997 		 */
1998 		args[0] = va_arg(arglist, uint32_t);
1999 		socket = CS_GET_SOCKET_NUMBER(args[0]);
2000 		func = CS_GET_FUNCTION_NUMBER(args[0]);
2001 		error = (long)pcmcia_sockets[socket]->ls_dip[func];
2002 		break;
2003 
2004 		/*
2005 		 * the remaining entries are SocketServices calls
2006 		 */
2007 	case SS_GetAdapter:
2008 		error = SSGetAdapter(va_arg(arglist, get_adapter_t *));
2009 		break;
2010 	case SS_GetPage:
2011 		error = SSGetPage(va_arg(arglist, get_page_t *));
2012 		break;
2013 	case SS_GetSocket:
2014 		error = SSGetSocket(va_arg(arglist, get_socket_t *));
2015 		break;
2016 	case SS_GetStatus:
2017 		error = SSGetStatus(va_arg(arglist, get_ss_status_t *));
2018 		break;
2019 	case SS_GetWindow:
2020 		error = SSGetWindow(va_arg(arglist, get_window_t *));
2021 		break;
2022 	case SS_InquireAdapter:
2023 		error = SSInquireAdapter(va_arg(arglist, inquire_adapter_t *));
2024 		break;
2025 	case SS_InquireSocket:
2026 		error = SSInquireSocket(va_arg(arglist, inquire_socket_t *));
2027 		break;
2028 	case SS_InquireWindow:
2029 		error = SSInquireWindow(va_arg(arglist, inquire_window_t *));
2030 		break;
2031 	case SS_ResetSocket:
2032 		args[0] = va_arg(arglist, uint32_t);
2033 		args[1] = va_arg(arglist, int);
2034 		error = SSResetSocket(args[0], args[1]);
2035 		break;
2036 	case SS_SetPage:
2037 		error = SSSetPage(va_arg(arglist, set_page_t *));
2038 		break;
2039 	case SS_SetSocket:
2040 		error = SSSetSocket(va_arg(arglist, set_socket_t *));
2041 		break;
2042 	case SS_SetWindow:
2043 		error = SSSetWindow(va_arg(arglist, set_window_t *));
2044 		break;
2045 	case SS_SetIRQHandler:
2046 		error = SSSetIRQHandler(va_arg(arglist, set_irq_handler_t *));
2047 		break;
2048 	case SS_ClearIRQHandler:
2049 		error = SSClearIRQHandler(va_arg(arglist,
2050 		    clear_irq_handler_t *));
2051 		break;
2052 	default:
2053 		error = BAD_FUNCTION;
2054 		break;
2055 	}
2056 	va_end(arglist);
2057 	return (error);
2058 }
2059 
2060 /*
2061  * pcmcia_merge_power()
2062  *	The adapters may have different power tables so it
2063  *	is necessary to construct a single power table that
2064  *	can be used throughout the system.  The result is
2065  *	a merger of all capabilities.  The nexus adds
2066  *	power table entries one at a time.
2067  */
2068 void
2069 pcmcia_merge_power(struct power_entry *power)
2070 {
2071 	int i;
2072 	struct power_entry pwr;
2073 
2074 	pwr = *power;
2075 
2076 	for (i = 0; i < pcmcia_num_power; i++) {
2077 		if (pwr.PowerLevel == pcmcia_power_table[i].PowerLevel) {
2078 			if (pwr.ValidSignals ==
2079 				pcmcia_power_table[i].ValidSignals) {
2080 				return;
2081 			} else {
2082 				/* partial match */
2083 				pwr.ValidSignals &=
2084 					~pcmcia_power_table[i].ValidSignals;
2085 			}
2086 		}
2087 	}
2088 	/* what's left becomes a new entry */
2089 	if (pcmcia_num_power == PCMCIA_MAX_POWER)
2090 		return;
2091 	pcmcia_power_table[pcmcia_num_power++] = pwr;
2092 }
2093 
2094 /*
2095  * pcmcia_do_suspend()
2096  *	tell CS that a suspend has happened by passing a
2097  *	card removal event.  Then cleanup the socket state
2098  *	to fake the cards being removed so resume works
2099  */
2100 void
2101 pcmcia_do_suspend(int socket, pcmcia_logical_socket_t *sockp)
2102 {
2103 	get_ss_status_t stat;
2104 	struct pcmcia_adapter *adapt;
2105 	pcmcia_if_t *ls_if;
2106 	dev_info_t *dip;
2107 	int i;
2108 
2109 #ifdef	XXX
2110 	if (pcmcia_cs_event == NULL) {
2111 		return;
2112 	}
2113 #endif
2114 
2115 	ls_if = sockp->ls_if;
2116 	adapt = sockp->ls_adapter;
2117 
2118 	if (ls_if == NULL || ls_if->pcif_get_status == NULL) {
2119 		return;
2120 	}
2121 
2122 	stat.socket = socket;
2123 #if defined(PCMCIA_DEBUG)
2124 	if (pcmcia_debug) {
2125 		cmn_err(CE_CONT,
2126 			"pcmcia_do_suspend(%d, %p)\n", socket, (void *)sockp);
2127 	}
2128 #endif
2129 
2130 	if (GET_STATUS(ls_if, adapt->pca_dip, &stat) != SUCCESS)
2131 		return;
2132 
2133 	/*
2134 	 * If there is a card in the socket, then we need to send
2135 	 *	everyone a PCE_CARD_REMOVAL event, and remove the
2136 	 *	card active property.
2137 	 */
2138 
2139 	for (i = 0; i < sockp->ls_functions; i++) {
2140 		struct pcmcia_parent_private *ppd;
2141 		dip = sockp->ls_dip[i];
2142 		if (dip != NULL) {
2143 			ppd = (struct pcmcia_parent_private *)
2144 				ddi_get_parent_data(dip);
2145 			ppd->ppd_flags |= PPD_SUSPENDED;
2146 		}
2147 #if 0
2148 		sockp->ls_dip[i] = NULL;
2149 #endif
2150 	}
2151 	sockp->ls_flags |= PCS_SUSPENDED;
2152 
2153 	if (pcmcia_cs_event &&
2154 	    (sockp->ls_cs_events & (1 << PCE_PM_SUSPEND))) {
2155 		CS_EVENT(PCE_PM_SUSPEND, socket, 0);
2156 	}
2157 	pcm_event_manager(PCE_PM_SUSPEND, socket, NULL);
2158 }
2159 
2160 /*
2161  * pcmcia_do_resume()
2162  *	tell CS that a suspend has happened by passing a
2163  *	card removal event.  Then cleanup the socket state
2164  *	to fake the cards being removed so resume works
2165  */
2166 void
2167 pcmcia_do_resume(int socket, pcmcia_logical_socket_t *sockp)
2168 {
2169 	get_ss_status_t stat;
2170 	struct pcmcia_adapter *adapt;
2171 	pcmcia_if_t *ls_if;
2172 
2173 #ifdef	XXX
2174 	if (pcmcia_cs_event == NULL) {
2175 		return;
2176 	}
2177 #endif
2178 
2179 	ls_if = sockp->ls_if;
2180 	adapt = sockp->ls_adapter;
2181 
2182 	if (ls_if == NULL || ls_if->pcif_get_status == NULL) {
2183 		return;
2184 	}
2185 
2186 	stat.socket = socket;
2187 #if defined(PCMCIA_DEBUG)
2188 	if (pcmcia_debug) {
2189 		cmn_err(CE_CONT,
2190 			"pcmcia_do_resume(%d, %p)\n", socket, (void *)sockp);
2191 	}
2192 #endif
2193 	if (GET_STATUS(ls_if, adapt->pca_dip, &stat) ==
2194 	    SUCCESS) {
2195 
2196 #if defined(PCMCIA_DEBUG)
2197 		if (pcmcia_debug)
2198 			cmn_err(CE_CONT, "\tsocket=%x, CardState=%x\n",
2199 				socket, stat.CardState);
2200 #endif
2201 #if 0
2202 		/* now have socket info -- do we have events? */
2203 		if ((stat.CardState & SBM_CD) == SBM_CD) {
2204 			if (pcmcia_cs_event &&
2205 			    (sockp->ls_cs_events & (1 << PCE_CARD_INSERT))) {
2206 				CS_EVENT(PCE_CARD_INSERT, socket, 0);
2207 			}
2208 
2209 			/* we should have card removed from CS soon */
2210 			pcm_event_manager(PCE_CARD_INSERT, socket, NULL);
2211 		}
2212 #else
2213 		if (pcmcia_cs_event &&
2214 		    (sockp->ls_cs_events & (1 << PCE_PM_SUSPEND))) {
2215 			CS_EVENT(PCE_PM_RESUME, socket, 0);
2216 			CS_EVENT(PCE_CARD_REMOVAL, socket, 0);
2217 			if ((stat.CardState & SBM_CD) == SBM_CD)
2218 				CS_EVENT(PCE_CARD_INSERT, socket, 0);
2219 		}
2220 #endif
2221 	}
2222 }
2223 
2224 /*
2225  * pcmcia_map_power_set()
2226  *	Given a power table entry and level, find it in the
2227  *	master table and return the index in the adapter table.
2228  */
2229 static int
2230 pcmcia_map_power_set(struct pcmcia_adapter *adapt, int level, int which)
2231 {
2232 	int plevel, i;
2233 	struct power_entry *pwr = (struct power_entry *)adapt->pca_power;
2234 	plevel = pcmcia_power_table[level].PowerLevel;
2235 	/* mask = pcmcia_power_table[level].ValidSignals; */
2236 	for (i = 0; i < adapt->pca_numpower; i++)
2237 		if (plevel == pwr[i].PowerLevel &&
2238 		    pwr[i].ValidSignals & which)
2239 			return (i);
2240 	return (0);
2241 }
2242 
2243 /*
2244  * pcmcia_map_power_get()
2245  *	Given an adapter power entry, find the appropriate index
2246  *	in the master table.
2247  */
2248 static int
2249 pcmcia_map_power_get(struct pcmcia_adapter *adapt, int level, int which)
2250 {
2251 	int plevel, i;
2252 	struct power_entry *pwr = (struct power_entry *)adapt->pca_power;
2253 	plevel = pwr[level].PowerLevel;
2254 	/* mask = pwr[level].ValidSignals; */
2255 	for (i = 0; i < pcmcia_num_power; i++)
2256 		if (plevel == pcmcia_power_table[i].PowerLevel &&
2257 		    pcmcia_power_table[i].ValidSignals & which)
2258 			return (i);
2259 	return (0);
2260 }
2261 
2262 /*
2263  * XXX - SS really needs a way to allow the caller to express
2264  *	interest in PCE_CARD_STATUS_CHANGE events.
2265  */
2266 static uint32_t
2267 pcm_event_map[32] = {
2268 	PCE_E2M(PCE_CARD_WRITE_PROTECT)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2269 	PCE_E2M(PCE_CARD_UNLOCK)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2270 	PCE_E2M(PCE_EJECTION_REQUEST)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2271 	PCE_E2M(PCE_INSERTION_REQUEST)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2272 	PCE_E2M(PCE_CARD_BATTERY_WARN)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2273 	PCE_E2M(PCE_CARD_BATTERY_DEAD)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2274 	PCE_E2M(PCE_CARD_READY)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2275 	PCE_E2M(PCE_CARD_REMOVAL)|PCE_E2M(PCE_CARD_INSERT)|
2276 					PCE_E2M(PCE_CARD_STATUS_CHANGE),
2277 	PCE_E2M(PCE_PM_SUSPEND)|PCE_E2M(PCE_PM_RESUME),
2278 };
2279 
2280 static int
2281 pcm_mapevents(uint32_t eventmask)
2282 {
2283 	uint32_t mask;
2284 	int i;
2285 
2286 	for (i = 0, mask = 0; eventmask && i < 32; i++) {
2287 		if (eventmask & (1 << i)) {
2288 			mask |= pcm_event_map[i];
2289 			eventmask &= ~(1 << i);
2290 		}
2291 	}
2292 	return (mask);
2293 }
2294 
2295 
2296 /*
2297  * PCMCIA Generic Naming Support
2298  *
2299  * With 2.6, PCMCIA naming moves to the 1275 and generic naming model.
2300  * Consequently, the whole naming mechanism is to be changed.  This is
2301  * not backward compatible with the current names but that isn't a problem
2302  * due to so few drivers existing.
2303  *
2304  * For cards with a device_id tuple, a generic name will be used.
2305  * if there is no device_id, then the 1275 name will be used if possible.
2306  * The 1275 name is of the form pccardNNNN,MMMM from the manfid tuple.
2307  * if there is not manfid tuple, an attempt will be made to bind the
2308  * node to the version_1 strings.
2309  *
2310  * In all cases, a "compatible" property is created with a number
2311  * of names.  The most generic name will be last in the list.
2312  */
2313 
2314 /*
2315  * pcmcia_fix_string()
2316  * want to avoid special characters in alias strings so convert
2317  * to something innocuous
2318  */
2319 
2320 void
2321 pcmcia_fix_string(char *str)
2322 {
2323 	for (; str && *str; str++) {
2324 		switch (*str) {
2325 			case ' ':
2326 			case '\t':
2327 				*str = '_';
2328 				break;
2329 		}
2330 	}
2331 }
2332 
2333 void
2334 pcmcia_1275_name(int socket, struct pcm_device_info *info,
2335 			client_handle_t handle)
2336 {
2337 	cistpl_manfid_t manfid;
2338 	cistpl_jedec_t jedec;
2339 	tuple_t tuple;
2340 	int i;
2341 
2342 	tuple.Socket = socket;
2343 
2344 	/* get MANFID if it exists -- this is most important form */
2345 	tuple.DesiredTuple = CISTPL_MANFID;
2346 	tuple.Attributes = 0;
2347 	if ((i = csx_GetFirstTuple(handle, &tuple)) ==
2348 	    SUCCESS) {
2349 		i = csx_Parse_CISTPL_MANFID(handle, &tuple,
2350 		    &manfid);
2351 		if (i == SUCCESS) {
2352 			(void) sprintf(info->pd_bind_name, "%s%x,%x",
2353 				PCMDEV_NAMEPREF,
2354 				manfid.manf, manfid.card);
2355 			info->pd_flags |= PCM_NAME_1275;
2356 		}
2357 	} else {
2358 		tuple.Attributes = 0;
2359 		tuple.DesiredTuple = CISTPL_JEDEC_A;
2360 		if ((i = csx_GetFirstTuple(handle, &tuple)) ==
2361 		    SUCCESS) {
2362 			i = csx_Parse_CISTPL_JEDEC_A(handle, &tuple,
2363 			    &jedec);
2364 			if (i == SUCCESS) {
2365 				(void) sprintf(info->pd_bind_name, "%s%x,%x",
2366 					PCMDEV_NAMEPREF,
2367 					jedec.jid[0].id, jedec.jid[0].info);
2368 				info->pd_flags |= PCM_NAME_1275;
2369 			}
2370 		}
2371 	}
2372 }
2373 
2374 void
2375 pcmcia_vers1_name(int socket, struct pcm_device_info *info,
2376 			client_handle_t handle)
2377 {
2378 	cistpl_vers_1_t vers1;
2379 	tuple_t tuple;
2380 	int which = 0;
2381 	int i, len, space;
2382 
2383 	tuple.Socket = socket;
2384 	info->pd_vers1_name[0] = '\0';
2385 
2386 	/* Version 1 strings */
2387 	tuple.DesiredTuple = CISTPL_VERS_1;
2388 	tuple.Attributes = 0;
2389 	if (!which &&
2390 	    (i = csx_GetFirstTuple(handle, &tuple)) ==
2391 		SUCCESS) {
2392 		i = csx_Parse_CISTPL_VERS_1(handle, &tuple, &vers1);
2393 		if (i == SUCCESS) {
2394 			for (i = 0, len = 0, space = 0; i < vers1.ns; i++) {
2395 			    if ((space + len + strlen(info->pd_vers1_name)) >=
2396 				sizeof (info->pd_vers1_name))
2397 				    break;
2398 			    if (space) {
2399 				    info->pd_vers1_name[len++] = ',';
2400 			    }
2401 			    (void) strcpy(info->pd_vers1_name + len,
2402 				(char *)vers1.pi[i]);
2403 			    len += strlen((char *)vers1.pi[i]);
2404 			    /* strip trailing spaces off of string */
2405 			    while (info->pd_vers1_name[len - 1] == ' ' &&
2406 				    len > 0)
2407 				    len--;
2408 			    space = 1;
2409 			}
2410 			info->pd_vers1_name[len] = '\0';
2411 			info->pd_flags |= PCM_NAME_VERS1;
2412 		}
2413 	}
2414 }
2415 
2416 
2417 int
2418 pcmcia_get_funce(client_handle_t handle, tuple_t *tuple)
2419 {
2420 	int ret = 0;
2421 
2422 	tuple->Attributes = 0;
2423 	while (csx_GetNextTuple(handle, tuple) == SUCCESS) {
2424 		if (tuple->TupleCode == CISTPL_FUNCID) {
2425 			break;
2426 		}
2427 		if (tuple->TupleCode == CISTPL_FUNCE) {
2428 			ret = 1;
2429 			break;
2430 		}
2431 		tuple->Attributes = 0;
2432 	}
2433 	return (ret);
2434 }
2435 
2436 char *pcmcia_lan_types[] = {
2437 	"arcnet",
2438 	"ethernet",
2439 	"token-ring",
2440 	"localtalk",
2441 	"fddi",
2442 	"atm",
2443 	"wireless",
2444 	"reserved"
2445 };
2446 
2447 void
2448 pcmcia_generic_name(int socket, struct pcm_device_info *info,
2449 			client_handle_t handle)
2450 {
2451 	cistpl_funcid_t funcid;
2452 	cistpl_funce_t funce;
2453 	tuple_t tuple;
2454 	int which = 0;
2455 	int i;
2456 
2457 	tuple.Socket = socket;
2458 
2459 	tuple.DesiredTuple = CISTPL_FUNCID;
2460 	tuple.Attributes = 0;
2461 	if ((i = csx_GetFirstTuple(handle, &tuple)) ==
2462 	    SUCCESS) {
2463 		/*
2464 		 * need to make sure that CISTPL_FUNCID is not
2465 		 * present in both a global and local CIS for MF
2466 		 * cards.  3COM seems to do this erroneously
2467 		 */
2468 
2469 		if (info->pd_flags & PCM_MULTI_FUNCTION &&
2470 		    tuple.Flags & CISTPLF_GLOBAL_CIS) {
2471 			tuple_t ltuple;
2472 			ltuple = tuple;
2473 			ltuple.DesiredTuple = CISTPL_FUNCID;
2474 			ltuple.Attributes = 0;
2475 			if ((i = csx_GetNextTuple(handle, &ltuple)) ==
2476 			    SUCCESS) {
2477 				/* this is the per-function funcid */
2478 				tuple = ltuple;
2479 			}
2480 		}
2481 
2482 		i = csx_Parse_CISTPL_FUNCID(handle, &tuple, &funcid);
2483 		if (i == SUCCESS) {
2484 			/* in case no function extension */
2485 			if (funcid.function < PCM_GENNAME_SIZE)
2486 				(void) strcpy(info->pd_generic_name,
2487 				    pcmcia_generic_names[funcid.function]);
2488 			else
2489 				(void) sprintf(info->pd_generic_name,
2490 					"class,%x",
2491 					funcid.function);
2492 		}
2493 		info->pd_type = funcid.function;
2494 		switch (funcid.function) {
2495 		case TPLFUNC_LAN:
2496 			which = pcmcia_get_funce(handle, &tuple);
2497 			if (which) {
2498 				i = csx_Parse_CISTPL_FUNCE(handle,
2499 				    &tuple,
2500 				    &funce, TPLFUNC_LAN);
2501 				if (i == SUCCESS) {
2502 					i = funce.data.lan.tech;
2503 					if (i > sizeof (pcmcia_lan_types) /
2504 					    sizeof (char *)) {
2505 						break;
2506 					}
2507 					(void) strcpy(info->pd_generic_name,
2508 						pcmcia_lan_types[i]);
2509 				}
2510 			}
2511 			break;
2512 		case TPLFUNC_VIDEO:
2513 #ifdef future_pcmcia_spec
2514 			which = pcmcia_get_funce(handle, &tuple);
2515 			if (which) {
2516 				i = csx_Parse_CISTPL_FUNCE(handle,
2517 				    &tuple,
2518 				    &funce, TPLFUNC_VIDEO);
2519 				if (i == SUCCESS) {
2520 					i = funce.video.tech;
2521 					if (i > sizeof (pcmcia_lan_types) /
2522 					    sizeof (char *)) {
2523 						break;
2524 					}
2525 					(void) strcpy(info->pd_generic_names,
2526 						pcmcia_lan_types[i]);
2527 				}
2528 			}
2529 #endif
2530 			break;
2531 		}
2532 		info->pd_flags |= PCM_NAME_GENERIC;
2533 	} else {
2534 		/* if no FUNCID, do we have CONFIG */
2535 		tuple.DesiredTuple = CISTPL_CONFIG;
2536 		tuple.Attributes = 0;
2537 		if (csx_GetFirstTuple(handle, &tuple) != SUCCESS) {
2538 			info->pd_flags |= PCM_NO_CONFIG | PCM_NAME_GENERIC;
2539 			(void) strcpy(info->pd_generic_name,
2540 				pcmcia_generic_names[PCM_TYPE_MEMORY]);
2541 			info->pd_type = PCM_TYPE_MEMORY;
2542 		}
2543 	}
2544 }
2545 
2546 
2547 /*
2548  * pcmcia_add_compatible()
2549  * add the cached compatible property list.
2550  */
2551 void
2552 pcmcia_add_compatible(dev_info_t *dip, struct pcm_device_info *info)
2553 {
2554 	int length = 0, i;
2555 	char buff[MAXNAMELEN];
2556 	char *compat_name[8];
2557 	int ci = 0;
2558 
2559 	bzero(compat_name, sizeof (compat_name));
2560 
2561 	if (info->pd_flags & PCM_NAME_VERS1) {
2562 		(void) sprintf(buff, "%s,%s", PCMDEV_NAMEPREF,
2563 		    info->pd_vers1_name);
2564 		pcmcia_fix_string(buff); /* don't want spaces */
2565 		length = strlen(buff) + 1;
2566 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2567 		(void) strcpy(compat_name[ci++], buff);
2568 	}
2569 
2570 	if ((info->pd_flags & (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) ==
2571 	    (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) {
2572 		(void) sprintf(buff, "%s,%x", info->pd_bind_name,
2573 		    info->pd_function);
2574 		length = strlen(buff) + 1;
2575 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2576 		(void) strcpy(compat_name[ci++], buff);
2577 	}
2578 
2579 	if (info->pd_flags & PCM_NAME_1275) {
2580 		length = strlen(info->pd_bind_name) + 1;
2581 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2582 		(void) strcpy(compat_name[ci++], info->pd_bind_name);
2583 	}
2584 
2585 	if (info->pd_flags & PCM_NAME_GENERIC) {
2586 		if (strncmp(info->pd_generic_name, "class,", 6) == 0) {
2587 			/* no generic without "pccard" */
2588 			(void) sprintf(buff, "%s%s", PCMDEV_NAMEPREF,
2589 				info->pd_generic_name);
2590 		} else {
2591 			/* first pccard,generic-name */
2592 			(void) sprintf(buff, "%s,%s", PCMDEV_NAMEPREF,
2593 				info->pd_generic_name);
2594 		}
2595 		length = strlen(buff) + 1;
2596 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2597 		(void) strcpy(compat_name[ci++], buff);
2598 
2599 		/* now the simple generic name */
2600 		length = strlen(info->pd_generic_name) + 1;
2601 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2602 		(void) strcpy(compat_name[ci++], info->pd_generic_name);
2603 	}
2604 
2605 	if (info->pd_flags & PCM_NO_CONFIG) {
2606 		char *mem = "pccard,memory";
2607 		/*
2608 		 * I/O cards are required to have a config tuple.
2609 		 * there are some that violate the spec and don't
2610 		 * but it is most likely that this is a memory card
2611 		 * so tag it as such.  "memory" is more general
2612 		 * than other things so needs to come last.
2613 		 */
2614 		length = strlen(mem) + 1;
2615 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2616 		(void) strcpy(compat_name[ci++], mem);
2617 	}
2618 
2619 	if (ci == 0)
2620 		return;
2621 
2622 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
2623 	    "compatible", (char **)compat_name, ci) != DDI_PROP_SUCCESS)
2624 		cmn_err(CE_WARN, "pcmcia: unable to create compatible prop");
2625 
2626 	for (i = 0; i < ci; i++)
2627 		kmem_free(compat_name[i], strlen(compat_name[i]) + 1);
2628 }
2629 /*
2630  * CIS parsing and other PC Card specific code
2631  */
2632 
2633 /*
2634  * pcmcia_get_mem_regs()
2635  */
2636 static int
2637 pcmcia_get_mem_regs(struct pcm_regs *regs, struct pcm_device_info *info,
2638 			int type, int pctype)
2639 {
2640 	int num_regs = 0;
2641 	tuple_t tuple;
2642 	cistpl_device_t device;
2643 	uint32_t curr_base;
2644 	int ret, len;
2645 	int space;
2646 
2647 	/*
2648 	 * current plan for reg spec:
2649 	 * device_a will be accumulated to determine max size of
2650 	 * attribute memory.  device for common.  Then config
2651 	 * tuples to get a worst case I/O size.
2652 	 */
2653 	bzero(&tuple, sizeof (tuple));
2654 	tuple.Socket = info->pd_socket;
2655 
2656 	tuple.DesiredTuple = (cisdata_t)type;
2657 
2658 	space = (type == CISTPL_DEVICE_A) ? PC_REG_SPACE_ATTRIBUTE :
2659 		PC_REG_SPACE_MEMORY;
2660 	if ((ret = csx_GetFirstTuple(info->pd_handle, &tuple)) == CS_SUCCESS) {
2661 		bzero(&device, sizeof (device));
2662 
2663 		if (type == CISTPL_DEVICE)
2664 			ret = csx_Parse_CISTPL_DEVICE(info->pd_handle, &tuple,
2665 			    &device);
2666 		else
2667 			ret = csx_Parse_CISTPL_DEVICE_A(info->pd_handle, &tuple,
2668 			    &device);
2669 
2670 		if (ret == CS_SUCCESS) {
2671 		    curr_base = 0;
2672 		    for (ret = 0; ret < device.num_devices;
2673 			ret++) {
2674 			    /* need to order these for real mem first */
2675 			    if (device.devnode[ret].type !=
2676 				CISTPL_DEVICE_DTYPE_NULL) {
2677 				    /* how to represent types??? */
2678 				    regs[num_regs].phys_hi =
2679 					PC_REG_PHYS_HI(0, 0,
2680 					    pctype,
2681 					    space,
2682 					    info->pd_socket,
2683 					    info->pd_function,
2684 					    0);
2685 				    regs[num_regs].phys_lo = curr_base;
2686 				    len = device.devnode[ret].size_in_bytes;
2687 				    curr_base += len;
2688 				    regs[num_regs].phys_len = len;
2689 				    num_regs++;
2690 			    } else {
2691 				/*
2692 				 * NULL device is a "hole"
2693 				 */
2694 				    curr_base +=
2695 					    device.devnode[ret].size_in_bytes;
2696 			    }
2697 			}
2698 	    }
2699 	}
2700 	return (num_regs);
2701 }
2702 
2703 /*
2704  *
2705  */
2706 static int
2707 pcmcia_get_io_regs(struct pcm_regs *regs, struct pcm_device_info *info,
2708 		    int pctype)
2709 {
2710 	int num_regs = 0;
2711 	tuple_t tuple;
2712 	uint32_t curr_base;
2713 	int len, curr, i, curr_len;
2714 	cistpl_config_t config;
2715 	cistpl_cftable_entry_t cftable;
2716 	struct pcm_regs tmp[16];
2717 	int found = 0;
2718 
2719 	bzero(&tuple, sizeof (tuple));
2720 	tuple.DesiredTuple = CISTPL_CONFIG;
2721 	tuple.Socket = info->pd_socket;
2722 	tuple.Attributes = 0;
2723 	curr_base = 0;
2724 	len = 0;
2725 
2726 	if (csx_GetFirstTuple(info->pd_handle, &tuple) == CS_SUCCESS) {
2727 	    if (csx_Parse_CISTPL_CONFIG(info->pd_handle,
2728 					&tuple, &config) != CS_SUCCESS) {
2729 		info->pd_flags |= PCM_NO_CONFIG; /* must be memory */
2730 		return (0);
2731 	}
2732 	curr = 0;
2733 
2734 	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
2735 	tuple.Socket = info->pd_socket;
2736 	tuple.Attributes = 0;
2737 	bzero(tmp, sizeof (tmp));
2738 	while (csx_GetNextTuple(info->pd_handle,
2739 				&tuple) == CS_SUCCESS) {
2740 	    bzero(&cftable, sizeof (cftable));
2741 	    if (csx_Parse_CISTPL_CFTABLE_ENTRY(info->pd_handle,
2742 						&tuple, &cftable) ==
2743 		CS_SUCCESS) {
2744 		if (cftable.flags & CISTPL_CFTABLE_TPCE_FS_IO) {
2745 		    /* we have an I/O entry */
2746 		    if (cftable.io.flags &
2747 			CISTPL_CFTABLE_TPCE_FS_IO_RANGE) {
2748 			len = cftable.io.addr_lines;
2749 			if (len != 0)
2750 				len = 1 << len;
2751 			for (i = 0; i < cftable.io.ranges && curr < 16; i++) {
2752 			    curr_base = cftable.io.range[i].addr;
2753 			    curr_len = cftable.io.range[i].length;
2754 			    if (curr_len == 0)
2755 				    curr_len = len;
2756 			    if (len != 0 || cftable.io.addr_lines == 0) {
2757 				/* we have potential relocation */
2758 				int mask;
2759 				mask = cftable.io.addr_lines ?
2760 						cftable.io.addr_lines :
2761 							genp2(len);
2762 				mask = genmask(mask);
2763 				if ((mask & curr_base) == 0) {
2764 					/* more accurate length */
2765 					regs->phys_len = curr_len;
2766 					regs->phys_lo = 0;
2767 					regs->phys_hi =
2768 					    PC_REG_PHYS_HI(0,
2769 						0,
2770 						pctype,
2771 						PC_REG_SPACE_IO,
2772 						info->pd_socket,
2773 						info->pd_function,
2774 						0);
2775 					num_regs++;
2776 					found = 2;
2777 					break;
2778 				}
2779 			    }
2780 			    tmp[curr].phys_len = curr_len;
2781 			    tmp[curr].phys_lo = curr_base;
2782 			    curr++;
2783 			    found = 1;
2784 			}
2785 			if (found == 2)
2786 				break;
2787 		    } else {
2788 			/* no I/O range so just a mask */
2789 			regs->phys_len = 1 << cftable.io.addr_lines;
2790 			regs->phys_hi =
2791 				PC_REG_PHYS_HI(0,
2792 						0,
2793 						pctype,
2794 						PC_REG_SPACE_IO,
2795 						info->pd_socket,
2796 						info->pd_function,
2797 						0);
2798 			regs->phys_lo = 0;
2799 			num_regs++;
2800 			regs++;
2801 			/* quit on "good" entry */
2802 			break;
2803 		    }
2804 		    /* was this the last CFTABLE Entry? */
2805 		    if (config.last == cftable.index)
2806 			    break;
2807 		}
2808 	    }
2809 	}
2810 	if (found == 1) {
2811 		/*
2812 		 * have some non-relocatable values
2813 		 * so we include them all for now
2814 		 */
2815 		for (i = 0; i < curr && num_regs < 8; i++) {
2816 		    regs->phys_len = tmp[i].phys_len;
2817 		    regs->phys_lo = tmp[i].phys_lo;
2818 		    regs->phys_hi = PC_REG_PHYS_HI(1, 0, pctype,
2819 			    PC_REG_SPACE_IO, info->pd_socket,
2820 			    info->pd_function, 0);
2821 		    regs++;
2822 		    num_regs++;
2823 		}
2824 	    }
2825 	}
2826 	return (num_regs);
2827 }
2828 
2829 /*
2830  * pcmcia_create_regs()
2831  *	create a valid set of regspecs for the card
2832  *	The first one is always for CIS access and naming
2833  */
2834 /*ARGSUSED*/
2835 static void
2836 pcmcia_find_regs(dev_info_t *dip, struct pcm_device_info *info,
2837 			struct pcmcia_parent_private *ppd)
2838 {
2839 	struct pcm_regs regs[32]; /* assume worst case */
2840 	int num_regs = 0;
2841 	int len;
2842 	int bustype;
2843 
2844 	if (ppd->ppd_flags & PPD_CARD_CARDBUS) {
2845 		/* always have a CIS map */
2846 		regs[0].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_CARDBUS,
2847 						PC_REG_SPACE_CONFIG,
2848 						info->pd_socket,
2849 						info->pd_function, 0);
2850 		bustype = PC_REG_TYPE_CARDBUS;
2851 	} else {
2852 		/* always have a CIS map */
2853 		regs[0].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_16BIT,
2854 						PC_REG_SPACE_ATTRIBUTE,
2855 						info->pd_socket,
2856 						info->pd_function, 0);
2857 		bustype = PC_REG_TYPE_16BIT;
2858 	}
2859 	regs[0].phys_lo = 0;	/* always starts at zero */
2860 	regs[0].phys_len = 0;
2861 	num_regs++;
2862 	/*
2863 	 * need to search CIS for other memory instances
2864 	 */
2865 
2866 	if (info->pd_flags & PCM_OTHER_NOCIS) {
2867 		/* special case of memory only card without CIS */
2868 		regs[1].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_16BIT,
2869 						PC_REG_SPACE_MEMORY,
2870 						info->pd_socket,
2871 						info->pd_function, 0);
2872 		regs[1].phys_lo = 0;
2873 		regs[1].phys_len = PCM_MAX_R2_MEM;
2874 		num_regs++;
2875 	} else {
2876 		/*
2877 		 * want to get any other memory and/or I/O regions
2878 		 * on the card and represent them here.
2879 		 */
2880 		num_regs += pcmcia_get_mem_regs(&regs[num_regs], info,
2881 						CISTPL_DEVICE_A, bustype);
2882 		num_regs += pcmcia_get_mem_regs(&regs[num_regs], info,
2883 						CISTPL_DEVICE, bustype);
2884 
2885 		/* now look for an I/O space to configure */
2886 		num_regs += pcmcia_get_io_regs(&regs[num_regs], info,
2887 						bustype);
2888 
2889 	}
2890 
2891 	len = num_regs * sizeof (uint32_t) * 3;
2892 	ppd->ppd_nreg = num_regs;
2893 	ppd->ppd_reg = kmem_alloc(len, KM_SLEEP);
2894 	bcopy(regs, ppd->ppd_reg, len);
2895 	len = sizeof (struct pcm_regs) * ppd->ppd_nreg;
2896 	ppd->ppd_assigned = kmem_zalloc(len, KM_SLEEP);
2897 }
2898 
2899 
2900 /*
2901  * pcmcia_need_intr()
2902  *	check to see if an interrupt tuple exists.
2903  *	existence means we need one in the intrspec.
2904  */
2905 static int
2906 pcmcia_need_intr(int socket, struct pcm_device_info *info)
2907 {
2908 	cistpl_config_t config;
2909 	cistpl_cftable_entry_t cftable;
2910 	tuple_t tuple;
2911 	int i;
2912 
2913 	bzero(&tuple, sizeof (tuple));
2914 	tuple.DesiredTuple = CISTPL_CONFIG;
2915 	tuple.Socket = socket;
2916 	tuple.Attributes = 0;
2917 	if (csx_GetFirstTuple(info->pd_handle, &tuple) != CS_SUCCESS) {
2918 		return (0);
2919 	}
2920 #if defined(PCMCIA_DEBUG)
2921 	if (pcmcia_debug) {
2922 		cmn_err(CE_CONT, "pcmcia_need_intr: have config tuple\n");
2923 	}
2924 #endif
2925 	bzero(&config, sizeof (config));
2926 	if (csx_Parse_CISTPL_CONFIG(info->pd_handle,
2927 	    &tuple, &config) != CS_SUCCESS) {
2928 		cmn_err(CE_WARN, "pcmcia: config failed to parse\n");
2929 		return (0);
2930 	}
2931 
2932 	for (cftable.index = (int)-1, i = -1;
2933 		i != config.last; i = cftable.index) {
2934 		tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
2935 		tuple.Attributes = 0;
2936 		if (csx_GetNextTuple(info->pd_handle,
2937 		    &tuple) != CS_SUCCESS) {
2938 			cmn_err(CE_WARN, "pcmcia: get cftable failed\n");
2939 			break;
2940 		}
2941 		bzero(&cftable, sizeof (cftable));
2942 		if (csx_Parse_CISTPL_CFTABLE_ENTRY(info->pd_handle,
2943 		    &tuple, &cftable) !=
2944 		    CS_SUCCESS) {
2945 			cmn_err(CE_WARN, "pcmcia: parse cftable failed\n");
2946 			break;
2947 		}
2948 #if defined(PCMCIA_DEBUG)
2949 		if (pcmcia_debug)
2950 			cmn_err(CE_CONT, "\t%x: flags=%x (%x)\n",
2951 				i, cftable.flags,
2952 				cftable.flags & CISTPL_CFTABLE_TPCE_FS_IRQ);
2953 #endif
2954 		if (cftable.flags & CISTPL_CFTABLE_TPCE_FS_IRQ)
2955 			return (1);
2956 	}
2957 	return (0);
2958 
2959 }
2960 
2961 /*
2962  * pcmcia_num_funcs()
2963  *	look for a CISTPL_LONGLINK_MFC
2964  *	if there is one, return the number of functions
2965  *	if there isn't one, then there is one function
2966  */
2967 static int
2968 pcmcia_num_funcs(int socket, client_handle_t handle)
2969 {
2970 	int count = 1;
2971 	cistpl_longlink_mfc_t mfc;
2972 	tuple_t tuple;
2973 
2974 	bzero(&tuple, sizeof (tuple_t));
2975 	tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
2976 	tuple.Socket = socket;
2977 	tuple.Attributes = 0;
2978 	if (csx_GetFirstTuple(handle, &tuple) == CS_SUCCESS) {
2979 		/* this is a multifunction card */
2980 		if (csx_ParseTuple(handle, &tuple, (cisparse_t *)&mfc,
2981 		    CISTPL_LONGLINK_MFC) == CS_SUCCESS) {
2982 			count = mfc.nfuncs;
2983 		}
2984 	}
2985 	return (count);
2986 }
2987 
2988 client_handle_t pcmcia_cs_handle;
2989 
2990 /*
2991  * pcmcia_create_dev_info(socket)
2992  *	either find or create the device information structure
2993  *	for the card(s) just inserted.	We don't care about removal yet.
2994  *	In any case, we will only do this at CS request
2995  */
2996 static void
2997 pcmcia_create_dev_info(int socket)
2998 {
2999 	struct pcm_device_info card_info;
3000 	client_reg_t reg;
3001 	cisinfo_t cisinfo;
3002 	int i;
3003 	dev_info_t *pdip;
3004 	static int handle_def = 0;
3005 
3006 #if defined(PCMCIA_DEBUG)
3007 	if (pcmcia_debug)
3008 		cmn_err(CE_CONT, "create dev_info_t for device in socket %d\n",
3009 			socket);
3010 #endif
3011 
3012 	/*
3013 	 * before we can do anything else, we need the parent
3014 	 * devinfo of the socket.  This gets things in the right
3015 	 * place in the device tree.
3016 	 */
3017 
3018 	pdip = pcm_find_parent_dip(socket);
3019 	if (pdip == NULL)
3020 		return;
3021 
3022 	/* Card Services calls needed to get CIS info */
3023 	reg.dip = NULL;
3024 	reg.Attributes = INFO_SOCKET_SERVICES;
3025 	reg.EventMask = 0;
3026 	reg.event_handler = NULL;
3027 	reg.Version = CS_VERSION;
3028 
3029 	bzero(&card_info, sizeof (card_info));
3030 
3031 	if (handle_def == 0) {
3032 		if (csx_RegisterClient(&pcmcia_cs_handle,
3033 		    &reg) != CS_SUCCESS) {
3034 #if defined(PCMCIA_DEBUG)
3035 			if (pcmcia_debug)
3036 				cmn_err(CE_CONT,
3037 					"pcmcia: RegisterClient failed\n");
3038 #endif
3039 			return;
3040 		}
3041 		handle_def++;
3042 	}
3043 	card_info.pd_handle = pcmcia_cs_handle;
3044 
3045 #if defined(PCMCIA_DEBUG)
3046 	if (pcmcia_debug)
3047 		cmn_err(CE_CONT,
3048 			"pcmcia_create_dev_info: handle = %x\n",
3049 			(int)card_info.pd_handle);
3050 #endif
3051 	card_info.pd_type = -1; /* no type to start */
3052 	card_info.pd_socket = socket;
3053 	card_info.pd_function = 0;
3054 	pcmcia_sockets[socket]->ls_functions = 1; /* default */
3055 
3056 	cisinfo.Socket = socket;
3057 
3058 	if ((i = csx_ValidateCIS(card_info.pd_handle,
3059 	    &cisinfo)) != SUCCESS ||
3060 	    cisinfo.Tuples == 0) {
3061 		/* no CIS means memory */
3062 		(void) strcpy(card_info.pd_generic_name, "memory");
3063 		card_info.pd_flags |= PCM_NAME_GENERIC |
3064 			PCM_OTHER_NOCIS | PCM_NAME_1275;
3065 		(void) strcpy(card_info.pd_bind_name, "pccard,memory");
3066 		(void) strcpy(card_info.pd_generic_name, "memory");
3067 		card_info.pd_type = PCM_TYPE_MEMORY;
3068 	} else {
3069 		int functions, lsocket;
3070 		card_info.pd_tuples = cisinfo.Tuples;
3071 
3072 		/*
3073 		 * how many functions on the card?
3074 		 * we need to know and then we do one
3075 		 * child node for each function using
3076 		 * the function specific tuples.
3077 		 */
3078 		lsocket = CS_MAKE_SOCKET_NUMBER(socket, CS_GLOBAL_CIS);
3079 		functions = pcmcia_num_funcs(lsocket,
3080 		    card_info.pd_handle);
3081 		pcmcia_sockets[socket]->ls_functions = functions;
3082 		if (functions > 1) {
3083 			card_info.pd_flags |= PCM_MULTI_FUNCTION;
3084 		}
3085 		for (i = 0; i < functions; i++) {
3086 		    register int flags;
3087 		    lsocket = CS_MAKE_SOCKET_NUMBER(socket, i);
3088 		    card_info.pd_socket = socket;
3089 		    card_info.pd_function = i;
3090 			/*
3091 			 * new name construction
3092 			 */
3093 		    if (functions != 1) {
3094 			    /* need per function handle */
3095 			    card_info.pd_function = i;
3096 			    /* get new handle */
3097 		    }
3098 		    pcmcia_1275_name(lsocket, &card_info,
3099 			card_info.pd_handle);
3100 		    pcmcia_vers1_name(lsocket, &card_info,
3101 			card_info.pd_handle);
3102 		    pcmcia_generic_name(lsocket, &card_info,
3103 			card_info.pd_handle);
3104 		    flags = card_info.pd_flags;
3105 		    if (!(flags & PCM_NAME_1275)) {
3106 			if (flags & PCM_NAME_VERS1) {
3107 			    (void) strcpy(card_info.pd_bind_name,
3108 				PCMDEV_NAMEPREF);
3109 			    card_info.pd_bind_name[sizeof (PCMDEV_NAMEPREF)] =
3110 				',';
3111 			    (void) strncpy(card_info.pd_bind_name +
3112 				sizeof (PCMDEV_NAMEPREF),
3113 				card_info.pd_vers1_name,
3114 				MODMAXNAMELEN -
3115 				sizeof (PCMDEV_NAMEPREF));
3116 			    pcmcia_fix_string(card_info.pd_bind_name);
3117 			} else {
3118 				/*
3119 				 * have a CIS but not the right info
3120 				 * so treat as generic "pccard"
3121 				 */
3122 				(void) strcpy(card_info.pd_generic_name,
3123 					"pccard,memory");
3124 				card_info.pd_flags |= PCM_NAME_GENERIC;
3125 				(void) strcpy(card_info.pd_bind_name,
3126 					"pccard,memory");
3127 			}
3128 		    }
3129 		    pcmcia_init_devinfo(pdip, &card_info);
3130 		}
3131 		return;
3132 	}
3133 
3134 	pcmcia_init_devinfo(pdip, &card_info);
3135 }
3136 
3137 /*
3138  * pcmcia_init_devinfo()
3139  *	if there isn't a device info structure, create one
3140  *	if there is, we don't do much.
3141  *
3142  *	Note: this will need updating as 1275 finalizes their spec.
3143  */
3144 static void
3145 pcmcia_init_devinfo(dev_info_t *pdip, struct pcm_device_info *info)
3146 {
3147 	int unit;
3148 	dev_info_t *dip;
3149 	char *name;
3150 	struct pcmcia_parent_private *ppd;
3151 
3152 #if defined(PCMCIA_DEBUG)
3153 	if (pcmcia_debug)
3154 		cmn_err(CE_CONT, "init_devinfo(%s, %d)\n", info->pd_bind_name,
3155 			info->pd_socket);
3156 #endif
3157 
3158 	/*
3159 	 * find out if there is already an instance of this
3160 	 * device.  We don't want to create a new one unnecessarily
3161 	 */
3162 	unit = CS_MAKE_SOCKET_NUMBER(info->pd_socket, info->pd_function);
3163 
3164 	dip = pcm_find_devinfo(pdip, info, unit);
3165 	if ((dip != NULL) && (ddi_getprop(DDI_DEV_T_NONE, dip,
3166 	    DDI_PROP_DONTPASS, PCM_DEV_SOCKET, -1) != -1)) {
3167 		/* it already exist but isn't a .conf file */
3168 
3169 #if defined(PCMCIA_DEBUG)
3170 		if (pcmcia_debug)
3171 			cmn_err(CE_CONT, "\tfound existing device node (%s)\n",
3172 				ddi_get_name(dip));
3173 #endif
3174 		if (strlen(info->pd_vers1_name) > 0)
3175 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
3176 			    dip, PCM_DEV_MODEL, info->pd_vers1_name);
3177 
3178 		ppd = (struct pcmcia_parent_private *)
3179 			ddi_get_parent_data(dip);
3180 
3181 		pcmcia_sockets[info->pd_socket]->ls_dip[info->pd_function] =
3182 		    dip;
3183 
3184 		ppd->ppd_active = 1;
3185 
3186 		if (ndi_devi_online(dip, 0) == NDI_FAILURE) {
3187 			pcmcia_sockets[info->pd_socket]-> \
3188 			    ls_dip[info->pd_function] = NULL;
3189 			ppd->ppd_active = 0;
3190 		}
3191 	} else {
3192 
3193 		char *dtype;
3194 
3195 #if defined(PCMCIA_DEBUG)
3196 		if (pcmcia_debug)
3197 			cmn_err(CE_CONT, "pcmcia: create child [%s](%d): %s\n",
3198 			    info->pd_bind_name, info->pd_socket,
3199 			    info->pd_generic_name);
3200 #endif
3201 
3202 		if (info->pd_flags & PCM_NAME_GENERIC)
3203 			name = info->pd_generic_name;
3204 		else
3205 			name = info->pd_bind_name;
3206 
3207 		if (ndi_devi_alloc(pdip, name, (pnode_t)DEVI_SID_NODEID,
3208 		    &dip) !=
3209 		    NDI_SUCCESS) {
3210 			cmn_err(CE_WARN,
3211 			    "pcmcia: unable to create device [%s](%d)\n",
3212 			    name, info->pd_socket);
3213 			return;
3214 		}
3215 		/*
3216 		 * construct the "compatible" property if the device
3217 		 * has a generic name
3218 		 */
3219 		pcmcia_add_compatible(dip, info);
3220 
3221 		ppd = kmem_zalloc(sizeof (struct pcmcia_parent_private),
3222 			    KM_SLEEP);
3223 
3224 		ppd->ppd_socket = info->pd_socket;
3225 		ppd->ppd_function = info->pd_function;
3226 
3227 		/*
3228 		 * add the "socket" property
3229 		 * the value of this property contains the logical PCMCIA
3230 		 * socket number the device has been inserted in, along
3231 		 * with the function # if the device is part of a
3232 		 * multi-function device.
3233 		 */
3234 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3235 		    PCM_DEV_SOCKET, unit);
3236 
3237 		if (info->pd_flags & PCM_MULTI_FUNCTION)
3238 			ppd->ppd_flags |= PPD_CARD_MULTI;
3239 
3240 		/*
3241 		 * determine all the properties we need for PPD
3242 		 * then create the properties
3243 		 */
3244 		/* socket is unique */
3245 		pcmcia_find_regs(dip, info, ppd);
3246 
3247 		ppd->ppd_intr = pcmcia_need_intr(unit, info);
3248 
3249 		if (ppd->ppd_nreg > 0)
3250 			(void) ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
3251 			    "reg", (int *)ppd->ppd_reg, ppd->ppd_nreg *
3252 			    sizeof (struct pcm_regs) / sizeof (int));
3253 		if (ppd->ppd_intr) {
3254 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
3255 			    "interrupts", ppd->ppd_intr);
3256 			ppd->ppd_intrspec =
3257 			    kmem_zalloc(sizeof (struct intrspec), KM_SLEEP);
3258 		}
3259 
3260 		/* set parent private - our own format */
3261 		ddi_set_parent_data(dip, (caddr_t)ppd);
3262 
3263 		/* init the device type */
3264 		if (info->pd_type >= 0 &&
3265 		    info->pd_type < (sizeof (pcmcia_dev_type) /
3266 		    (sizeof (char *))))
3267 			dtype = pcmcia_dev_type[info->pd_type];
3268 		else
3269 			dtype = "unknown";
3270 
3271 		if (strlen(info->pd_vers1_name) > 0)
3272 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
3273 			    dip, PCM_DEV_MODEL, info->pd_vers1_name);
3274 
3275 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
3276 		    PCM_DEVICETYPE, dtype);
3277 
3278 		/* set PC Card as active and present in socket */
3279 		pcmcia_sockets[info->pd_socket]->ls_dip[info->pd_function] =
3280 			dip;
3281 
3282 		ppd->ppd_active = 1;
3283 
3284 		/*
3285 		 * We should not call ndi_devi_online here if
3286 		 * pcmcia attach is in progress. This causes a deadlock.
3287 		 */
3288 		if (pcmcia_dip != dip) {
3289 			if (ndi_devi_online_async(dip, 0)
3290 			    != NDI_SUCCESS) {
3291 				pcmcia_sockets[info->pd_socket]->\
3292 				ls_dip[info->pd_function] = NULL;
3293 				pcmcia_ppd_free(ppd);
3294 				(void) ndi_devi_free(dip);
3295 				return;
3296 			}
3297 		}
3298 
3299 #if defined(PCMCIA_DEBUG)
3300 	if (pcmcia_debug)
3301 		cmn_err(CE_CONT, "\tjust added \"active\" to %s in %d\n",
3302 			ddi_get_name(dip), info->pd_socket);
3303 #endif
3304 	}
3305 
3306 	/*
3307 	 * inform the event manager that a child was added
3308 	 * to the device tree.
3309 	 */
3310 	pcm_event_manager(PCE_DEV_IDENT, unit, ddi_get_name(dip));
3311 
3312 #if defined(PCMCIA_DEBUG)
3313 	if (pcmcia_debug > 1) {
3314 		pcmcia_dump_minors(dip);
3315 	}
3316 #endif
3317 }
3318 
3319 /*
3320  * free any allocated parent-private data
3321  */
3322 static void
3323 pcmcia_ppd_free(struct pcmcia_parent_private *ppd)
3324 {
3325 	size_t len;
3326 
3327 	if (ppd->ppd_nreg != 0) {
3328 		len = ppd->ppd_nreg * sizeof (uint32_t) * 3;
3329 		kmem_free(ppd->ppd_reg, len);
3330 		len = sizeof (struct pcm_regs) * ppd->ppd_nreg;
3331 		kmem_free(ppd->ppd_assigned, len);
3332 	}
3333 
3334 	/*
3335 	 * pcmcia only allocates 1 intrspec today
3336 	 */
3337 	if (ppd->ppd_intr != 0) {
3338 		len = sizeof (struct intrspec) * ppd->ppd_intr;
3339 		kmem_free(ppd->ppd_intrspec, len);
3340 	}
3341 
3342 	kmem_free(ppd, sizeof (*ppd));
3343 }
3344 
3345 
3346 /*
3347  * pcmcia_get_devinfo(socket)
3348  *	entry point to allow finding the device info structure
3349  *	for a given logical socket.  Used by event manager
3350  */
3351 dev_info_t *
3352 pcmcia_get_devinfo(int socket)
3353 {
3354 	int func = CS_GET_FUNCTION_NUMBER(socket);
3355 	socket = CS_GET_SOCKET_NUMBER(socket);
3356 	if (pcmcia_sockets[socket])
3357 		return (pcmcia_sockets[socket]->ls_dip[func]);
3358 	return ((dev_info_t *)NULL);
3359 }
3360 
3361 /*
3362  * CSGetCookiesAndDip()
3363  *	get info needed by CS to setup soft interrupt handler and provide
3364  *		socket-specific adapter information
3365  */
3366 static int
3367 GetCookiesAndDip(sservice_t *serv)
3368 {
3369 	pcmcia_logical_socket_t *socket;
3370 	csss_adapter_info_t *ai;
3371 	int sock;
3372 
3373 	sock = CS_GET_SOCKET_NUMBER(serv->get_cookies.socket);
3374 
3375 	if (sock >= pcmcia_num_sockets ||
3376 	    (int)serv->get_cookies.socket < 0)
3377 		return (BAD_SOCKET);
3378 
3379 	socket = pcmcia_sockets[sock];
3380 	ai = &serv->get_cookies.adapter_info;
3381 	serv->get_cookies.dip = socket->ls_adapter->pca_dip;
3382 	serv->get_cookies.iblock = socket->ls_adapter->pca_iblock;
3383 	serv->get_cookies.idevice = socket->ls_adapter->pca_idev;
3384 
3385 	/*
3386 	 * Setup the adapter info for Card Services
3387 	 */
3388 	(void) strcpy(ai->name, socket->ls_adapter->pca_name);
3389 	ai->major = socket->ls_adapter->pca_module;
3390 	ai->minor = socket->ls_adapter->pca_unit;
3391 	ai->number = socket->ls_adapter->pca_number;
3392 	ai->num_sockets = socket->ls_adapter->pca_numsockets;
3393 	ai->first_socket = socket->ls_adapter->pca_first_socket;
3394 
3395 	return (SUCCESS);
3396 }
3397 
3398 /*
3399  * Note:
3400  *	The following functions that start with 'SS'
3401  *	implement SocketServices interfaces.  They
3402  *	simply map the socket and/or window number to
3403  *	the adapter specific number based on the general
3404  *	value that CardServices uses.
3405  *
3406  *	See the descriptions in SocketServices for
3407  *	details.  Also refer to specific adapter drivers
3408  *	for implementation reference.
3409  */
3410 
3411 static int
3412 SSGetAdapter(get_adapter_t *adapter)
3413 {
3414 	int n;
3415 	get_adapter_t info;
3416 
3417 	adapter->state = (unsigned)0xFFFFFFFF;
3418 	adapter->SCRouting = 0xFFFFFFFF;
3419 
3420 	for (n = 0; n < pcmcia_num_adapters; n++) {
3421 		GET_ADAPTER(pcmcia_adapters[n]->pca_if,
3422 			pcmcia_adapters[n]->pca_dip, &info);
3423 		adapter->state &= info.state;
3424 		adapter->SCRouting &= info.SCRouting;
3425 	}
3426 
3427 	return (SUCCESS);
3428 }
3429 
3430 static int
3431 SSGetPage(get_page_t *page)
3432 {
3433 	pcmcia_logical_window_t *window;
3434 	get_page_t newpage;
3435 	int retval, win;
3436 
3437 	if (page->window > pcmcia_num_windows) {
3438 		return (BAD_WINDOW);
3439 	}
3440 
3441 	window = pcmcia_windows[page->window];
3442 	newpage = *page;
3443 	win = newpage.window = window->lw_window; /* real window */
3444 
3445 	retval = GET_PAGE(window->lw_if, window->lw_adapter->pca_dip,
3446 		&newpage);
3447 	if (retval == SUCCESS) {
3448 		*page = newpage;
3449 		page->window = win;
3450 	}
3451 	return (retval);
3452 }
3453 
3454 static int
3455 SSGetSocket(get_socket_t *socket)
3456 {
3457 	int retval, sock;
3458 	get_socket_t newsocket;
3459 	pcmcia_logical_socket_t *sockp;
3460 
3461 	sock = socket->socket;
3462 	if (sock > pcmcia_num_sockets ||
3463 		(sockp = pcmcia_sockets[sock]) == NULL) {
3464 		return (BAD_SOCKET);
3465 	}
3466 
3467 	newsocket = *socket;
3468 	newsocket.socket = sockp->ls_socket;
3469 	retval = GET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3470 	    &newsocket);
3471 	if (retval == SUCCESS) {
3472 		newsocket.VccLevel = pcmcia_map_power_get(sockp->ls_adapter,
3473 		    newsocket.VccLevel,
3474 		    VCC);
3475 		newsocket.Vpp1Level = pcmcia_map_power_get(sockp->ls_adapter,
3476 		    newsocket.Vpp1Level,
3477 		    VPP1);
3478 		newsocket.Vpp2Level = pcmcia_map_power_get(sockp->ls_adapter,
3479 		    newsocket.Vpp2Level,
3480 		    VPP2);
3481 		*socket = newsocket;
3482 		socket->socket = sock;
3483 	}
3484 
3485 	return (retval);
3486 }
3487 
3488 static int
3489 SSGetStatus(get_ss_status_t *status)
3490 {
3491 	get_ss_status_t newstat;
3492 	int sock, retval;
3493 	pcmcia_logical_socket_t *sockp;
3494 
3495 	sock = status->socket;
3496 	if (sock > pcmcia_num_sockets ||
3497 		(sockp = pcmcia_sockets[sock]) == NULL) {
3498 		return (BAD_SOCKET);
3499 	}
3500 
3501 	newstat = *status;
3502 	newstat.socket = sockp->ls_socket;
3503 	retval = GET_STATUS(sockp->ls_if, sockp->ls_adapter->pca_dip,
3504 	    &newstat);
3505 	if (retval == SUCCESS) {
3506 		*status = newstat;
3507 		status->socket = sock;
3508 	}
3509 
3510 	return (retval);
3511 }
3512 
3513 static int
3514 SSGetWindow(get_window_t *window)
3515 {
3516 	int win, retval;
3517 	get_window_t newwin;
3518 	pcmcia_logical_window_t *winp;
3519 
3520 	win = window->window;
3521 	winp = pcmcia_windows[win];
3522 	newwin = *window;
3523 	newwin.window = winp->lw_window;
3524 
3525 	retval = GET_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip,
3526 	    &newwin);
3527 	if (retval == SUCCESS) {
3528 		newwin.socket = winp->lw_socket;
3529 		newwin.window = win;
3530 		*window = newwin;
3531 	}
3532 	return (retval);
3533 }
3534 
3535 /*
3536  * SSInquireAdapter()
3537  *	Get the capabilities of the "generic" adapter
3538  *	we are exporting to CS.
3539  */
3540 static int
3541 SSInquireAdapter(inquire_adapter_t *adapter)
3542 {
3543 	adapter->NumSockets = pcmcia_num_sockets;
3544 	adapter->NumWindows = pcmcia_num_windows;
3545 	adapter->NumEDCs = 0;
3546 	/*
3547 	 * notes: Adapter Capabilities are going to be difficult to
3548 	 * determine with reliability.	Fortunately, most of them
3549 	 * don't matter under Solaris or can be handled transparently
3550 	 */
3551 	adapter->AdpCaps = 0;	/* need to fix these */
3552 	/*
3553 	 * interrupts need a little work.  For x86, the valid IRQs will
3554 	 * be restricted to those that the system has exported to the nexus.
3555 	 * for SPARC, it will be the DoRight values.
3556 	 */
3557 	adapter->ActiveHigh = 0;
3558 	adapter->ActiveLow = 0;
3559 	adapter->power_entry = pcmcia_power_table; /* until we resolve this */
3560 	adapter->NumPower = pcmcia_num_power;
3561 	return (SUCCESS);
3562 }
3563 
3564 static int
3565 SSInquireSocket(inquire_socket_t *socket)
3566 {
3567 	int retval, sock;
3568 	inquire_socket_t newsocket;
3569 	pcmcia_logical_socket_t *sockp;
3570 
3571 	sock = socket->socket;
3572 	if (sock > pcmcia_num_sockets ||
3573 		(sockp = pcmcia_sockets[sock]) == NULL)
3574 		return (BAD_SOCKET);
3575 	newsocket = *socket;
3576 	newsocket.socket = sockp->ls_socket;
3577 	retval = INQUIRE_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3578 	    &newsocket);
3579 	if (retval == SUCCESS) {
3580 		*socket = newsocket;
3581 		socket->socket = sock;
3582 	}
3583 	return (retval);
3584 }
3585 
3586 static int
3587 SSInquireWindow(inquire_window_t *window)
3588 {
3589 	int retval, win;
3590 	pcmcia_logical_window_t *winp;
3591 	inquire_window_t newwin;
3592 	int slide;
3593 
3594 	win = window->window;
3595 	if (win > pcmcia_num_windows)
3596 		return (BAD_WINDOW);
3597 
3598 	winp = pcmcia_windows[win];
3599 	newwin = *window;
3600 	newwin.window = winp->lw_window;
3601 	retval = INQUIRE_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip,
3602 	    &newwin);
3603 #if defined(PCMCIA_DEBUG)
3604 		if (pcmcia_debug > 1)
3605 			cmn_err(CE_CONT, "SSInquireWindow: win=%d, pwin=%d\n",
3606 				win, newwin.window);
3607 #endif
3608 	if (retval == SUCCESS) {
3609 		*window = newwin;
3610 		/* just in case */
3611 		window->iowin_char.IOWndCaps &= ~WC_BASE;
3612 		slide = winp->lw_adapter->pca_first_socket;
3613 		/*
3614 		 * note that sockets are relative to the adapter.
3615 		 * we have to adjust the bits to show a logical
3616 		 * version.
3617 		 */
3618 
3619 		pcm_fix_bits(newwin.Sockets, window->Sockets, slide, 0);
3620 
3621 #if defined(PCMCIA_DEBUG)
3622 		if (pcmcia_debug > 1) {
3623 			cmn_err(CE_CONT, "iw: orig bits=%x, new bits=%x\n",
3624 				(int)*(uint32_t *)newwin.Sockets,
3625 				(int)*(uint32_t *)window->Sockets);
3626 			cmn_err(CE_CONT, "\t%x.%x.%x\n", window->WndCaps,
3627 				window->mem_win_char.MemWndCaps,
3628 				window->mem_win_char.MinSize);
3629 		}
3630 #endif
3631 		window->window = win;
3632 	}
3633 	return (retval);
3634 }
3635 
3636 static int
3637 SSResetSocket(int socket, int mode)
3638 {
3639 	pcmcia_logical_socket_t *sockp;
3640 
3641 	if (socket >= pcmcia_num_sockets ||
3642 		(sockp = pcmcia_sockets[socket]) == NULL)
3643 		return (BAD_SOCKET);
3644 
3645 	return (RESET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3646 	    sockp->ls_socket, mode));
3647 }
3648 
3649 static int
3650 SSSetPage(set_page_t *page)
3651 {
3652 	int window, retval;
3653 	set_page_t newpage;
3654 	pcmcia_logical_window_t *winp;
3655 
3656 	window = page->window;
3657 	if (window > pcmcia_num_windows) {
3658 #if defined(PCMCIA_DEBUG)
3659 		if (pcmcia_debug > 1)
3660 			cmn_err(CE_CONT, "SSSetPage: window=%d (of %d)\n",
3661 				window, pcmcia_num_windows);
3662 #endif
3663 		return (BAD_WINDOW);
3664 	}
3665 
3666 	winp = pcmcia_windows[window];
3667 	newpage = *page;
3668 	newpage.window = winp->lw_window;
3669 	retval = SET_PAGE(winp->lw_if, winp->lw_adapter->pca_dip, &newpage);
3670 	if (retval == SUCCESS) {
3671 		newpage.window = window;
3672 		*page = newpage;
3673 	}
3674 #if defined(PCMCIA_DEBUG)
3675 	if ((pcmcia_debug > 1) && retval != SUCCESS)
3676 		cmn_err(CE_CONT, "\tSetPage: returning error %x\n", retval);
3677 #endif
3678 	return (retval);
3679 }
3680 
3681 static int
3682 SSSetWindow(set_window_t *win)
3683 {
3684 	int socket, window, retval, func;
3685 	set_window_t newwin;
3686 	pcmcia_logical_window_t *winp;
3687 	pcmcia_logical_socket_t *sockp;
3688 
3689 	window = win->window;
3690 	if (window > pcmcia_num_windows)
3691 		return (BAD_WINDOW);
3692 
3693 	socket = CS_GET_SOCKET_NUMBER(win->socket);
3694 	func = CS_GET_FUNCTION_NUMBER(win->socket);
3695 
3696 	if (socket > pcmcia_num_sockets ||
3697 		(sockp = pcmcia_sockets[socket]) == NULL) {
3698 		return (BAD_SOCKET);
3699 	}
3700 
3701 	winp = pcmcia_windows[window];
3702 	winp->lw_socket = win->socket; /* reverse map */
3703 	newwin = *win;
3704 	newwin.window = winp->lw_window;
3705 	newwin.socket = sockp->ls_socket;
3706 	newwin.child = sockp->ls_dip[func]; /* so we carry the dip around */
3707 
3708 	retval = SET_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip, &newwin);
3709 	if (retval == SUCCESS) {
3710 		newwin.window = window;
3711 		newwin.socket = winp->lw_socket;
3712 		*win = newwin;
3713 	}
3714 	return (retval);
3715 }
3716 
3717 static int
3718 SSSetSocket(set_socket_t *socket)
3719 {
3720 	int sock, retval;
3721 	pcmcia_logical_socket_t *sockp;
3722 	set_socket_t newsock;
3723 
3724 	sock = socket->socket;
3725 	if (sock > pcmcia_num_sockets ||
3726 		(sockp = pcmcia_sockets[sock]) == NULL) {
3727 		return (BAD_SOCKET);
3728 	}
3729 
3730 	newsock = *socket;
3731 	/* note: we force CS to always get insert/removal events */
3732 	sockp->ls_cs_events = pcm_mapevents(newsock.SCIntMask) |
3733 		PCE_E2M(PCE_CARD_INSERT) | PCE_E2M(PCE_CARD_REMOVAL);
3734 #if defined(PCMCIA_DEBUG)
3735 	if (pcmcia_debug > 1)
3736 		cmn_err(CE_CONT,
3737 			"SetSocket: SCIntMask = %x\n", newsock.SCIntMask);
3738 #endif
3739 	newsock.socket = sockp->ls_socket;
3740 	newsock.VccLevel = pcmcia_map_power_set(sockp->ls_adapter,
3741 	    newsock.VccLevel, VCC);
3742 	newsock.Vpp1Level = pcmcia_map_power_set(sockp->ls_adapter,
3743 	    newsock.Vpp1Level, VPP1);
3744 	newsock.Vpp2Level = pcmcia_map_power_set(sockp->ls_adapter,
3745 	    newsock.Vpp2Level, VPP2);
3746 	retval = SET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3747 	    &newsock);
3748 	if (retval == SUCCESS) {
3749 		newsock.socket = sock;
3750 		newsock.VccLevel = pcmcia_map_power_get(sockp->ls_adapter,
3751 		    newsock.VccLevel,
3752 		    VCC);
3753 		newsock.Vpp1Level = pcmcia_map_power_get(sockp->ls_adapter,
3754 		    newsock.Vpp1Level,
3755 		    VPP1);
3756 		newsock.Vpp2Level = pcmcia_map_power_get(sockp->ls_adapter,
3757 		    newsock.Vpp2Level,
3758 		    VPP2);
3759 		*socket = newsock;
3760 		if (socket->IREQRouting & IRQ_ENABLE) {
3761 			sockp->ls_flags |= PCS_IRQ_ENABLED;
3762 		} else {
3763 			sockp->ls_flags &= ~PCS_IRQ_ENABLED;
3764 		}
3765 	}
3766 	return (retval);
3767 }
3768 
3769 /*
3770  * SSSetIRQHandler()
3771  *	arrange for IRQ to be allocated if appropriate and always
3772  *	arrange that PC Card interrupt handlers get called.
3773  */
3774 static int
3775 SSSetIRQHandler(set_irq_handler_t *handler)
3776 {
3777 	int sock, retval, func;
3778 	pcmcia_logical_socket_t *sockp;
3779 	struct pcmcia_parent_private *ppd;
3780 	dev_info_t *dip;
3781 	ddi_iblock_cookie_t iblk;
3782 	ddi_idevice_cookie_t idev;
3783 
3784 	sock = CS_GET_SOCKET_NUMBER(handler->socket);
3785 	func = CS_GET_FUNCTION_NUMBER(handler->socket);
3786 	if (sock > pcmcia_num_sockets ||
3787 		(sockp = pcmcia_sockets[sock]) == NULL) {
3788 		return (BAD_SOCKET);
3789 	}
3790 #if defined(PCMCIA_DEBUG)
3791 	if (pcmcia_debug) {
3792 
3793 		cmn_err(CE_CONT, "SSSetIRQHandler: socket=%x, function=%x\n",
3794 			sock, func);
3795 		cmn_err(CE_CONT, "\thandler(%p): socket=%x, irq=%x, id=%x\n",
3796 			(void *)handler->handler, handler->socket, handler->irq,
3797 			handler->handler_id);
3798 	}
3799 #endif
3800 	dip = sockp->ls_dip[func];
3801 
3802 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
3803 
3804 	handler->iblk_cookie = &iblk;
3805 	handler->idev_cookie = &idev;
3806 
3807 	retval = ddi_add_intr(dip, 0, handler->iblk_cookie,
3808 	    handler->idev_cookie,
3809 	    (uint32_t(*)(caddr_t)) handler->handler,
3810 	    handler->arg1);
3811 
3812 	if (retval == DDI_SUCCESS) {
3813 		handler->iblk_cookie = &sockp->ls_iblk;
3814 		handler->idev_cookie = &sockp->ls_idev;
3815 		handler->irq = ppd->ppd_intrspec->intrspec_vec;
3816 		retval = SUCCESS;
3817 	} else {
3818 		retval = sockp->ls_error;
3819 	}
3820 	return (retval);
3821 }
3822 
3823 /*
3824  * SSClearIRQHandler()
3825  *	Arrange to have the interrupt handler specified removed
3826  *	from the interrupt list.
3827  */
3828 static int
3829 SSClearIRQHandler(clear_irq_handler_t *handler)
3830 {
3831 	int sock, func;
3832 	pcmcia_logical_socket_t *sockp;
3833 	dev_info_t *dip;
3834 
3835 	sock = CS_GET_SOCKET_NUMBER(handler->socket);
3836 	func = CS_GET_FUNCTION_NUMBER(handler->socket);
3837 
3838 #if defined(PCMCIA_DEBUG)
3839 	if (pcmcia_debug) {
3840 
3841 		cmn_err(CE_CONT,
3842 			"SSClearIRQHandler: socket=%x, function=%x\n",
3843 			sock, func);
3844 		cmn_err(CE_CONT,
3845 			"\thandler(%p): socket=%x, id=%x\n",
3846 			(void *)handler, handler->socket,
3847 			handler->handler_id);
3848 	}
3849 #endif
3850 
3851 	if (sock > pcmcia_num_sockets ||
3852 		(sockp = pcmcia_sockets[sock]) == NULL) {
3853 		return (BAD_SOCKET);
3854 	}
3855 	dip = sockp->ls_dip[func];
3856 	if (dip) {
3857 		ddi_remove_intr(dip, 0, NULL);
3858 		return (SUCCESS);
3859 	}
3860 	return (BAD_SOCKET);
3861 }
3862 
3863 
3864 /*
3865  * pcm_pathname()
3866  *	make a partial path from dip.
3867  *	used to mknods relative to /devices/pcmcia/
3868  *
3869  * XXX - we now use ddi_get_name_addr to get the "address" portion
3870  *	of the name; that way, we only have to modify the name creation
3871  *	algorithm in one place
3872  */
3873 static void
3874 pcm_pathname(dev_info_t *dip, char *name, char *path)
3875 {
3876 	(void) sprintf(path, "%s@%s:%s", ddi_node_name(dip),
3877 	    ddi_get_name_addr(dip), name);
3878 }
3879 
3880 /*
3881  * pcmcia_create_device()
3882  *	create the /devices entries for the driver
3883  *	it is assumed that the PC Card driver will do a
3884  *	RegisterClient for each subdevice.
3885  *	The device type string is encoded here to match
3886  *	the standardized names when possible.
3887  * XXX - note that we may need to provide a way for the
3888  *	caller to specify the complete name string that
3889  *	we pass to ddi_set_name_addr
3890  */
3891 static int
3892 pcmcia_create_device(ss_make_device_node_t *init)
3893 {
3894 	int err = SUCCESS;
3895 	struct pcm_make_dev device;
3896 	struct dev_ops *ops;
3897 	major_t major;
3898 
3899 	/*
3900 	 * Now that we have the name, create it.
3901 	 */
3902 
3903 	bzero(&device, sizeof (device));
3904 	if (init->flags & SS_CSINITDEV_CREATE_DEVICE) {
3905 		if ((err = ddi_create_minor_node(init->dip,
3906 		    init->name,
3907 		    init->spec_type,
3908 		    init->minor_num,
3909 		    init->node_type,
3910 		    0)) != DDI_SUCCESS) {
3911 #if defined(PCMCIA_DEBUG)
3912 			if (pcmcia_debug)
3913 				cmn_err(CE_CONT,
3914 					"pcmcia_create_device: failed "
3915 					"create\n");
3916 #endif
3917 			return (BAD_ATTRIBUTE);
3918 		}
3919 
3920 		major = ddi_name_to_major(ddi_binding_name(init->dip));
3921 		ops = ddi_get_driver(init->dip);
3922 		LOCK_DEV_OPS(&devnamesp[major].dn_lock);
3923 		INCR_DEV_OPS_REF(ops);
3924 		(void) ddi_pathname(init->dip, device.path);
3925 		DECR_DEV_OPS_REF(ops);
3926 		UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
3927 		(void) sprintf(device.path + strlen(device.path), ":%s",
3928 		    init->name);
3929 
3930 		(void) strcpy(device.driver, ddi_binding_name(init->dip));
3931 #if defined(PCMCIA_DEBUG)
3932 		if (pcmcia_debug)
3933 			cmn_err(CE_CONT,
3934 				"pcmcia_create_device: created %s "
3935 				"from %s [%s]\n",
3936 				device.path, init->name, device.driver);
3937 #endif
3938 		device.dev =
3939 		    makedevice(ddi_name_to_major(ddi_get_name(init->dip)),
3940 		    init->minor_num);
3941 		device.flags |= (init->flags & SS_CSINITDEV_MORE_DEVICES) ?
3942 		    PCM_EVENT_MORE : 0;
3943 		device.type = init->spec_type;
3944 		device.op = SS_CSINITDEV_CREATE_DEVICE;
3945 		device.socket = ddi_getprop(DDI_DEV_T_ANY, init->dip,
3946 		    DDI_PROP_CANSLEEP, PCM_DEV_SOCKET,
3947 		    -1);
3948 	} else if (init->flags & SS_CSINITDEV_REMOVE_DEVICE) {
3949 		device.op = SS_CSINITDEV_REMOVE_DEVICE;
3950 		device.socket = ddi_getprop(DDI_DEV_T_ANY, init->dip,
3951 		    DDI_PROP_CANSLEEP, PCM_DEV_SOCKET,
3952 		    -1);
3953 		if (init->name != NULL)
3954 			(void) strcpy(device.path, init->name);
3955 		device.dev =
3956 		    makedevice(ddi_name_to_major(ddi_get_name(init->dip)),
3957 		    0);
3958 		ddi_remove_minor_node(init->dip, init->name);
3959 	}
3960 
3961 	/*
3962 	 *	we send an event for ALL devices created.
3963 	 *	To do otherwise ties us to using drvconfig
3964 	 *	forever.  There are relatively few devices
3965 	 *	ever created so no need to do otherwise.
3966 	 *	The existence of the event manager must never
3967 	 *	be visible to a PCMCIA device driver.
3968 	 */
3969 	pcm_event_manager(PCE_INIT_DEV, device.socket, &device);
3970 
3971 	return (err);
3972 }
3973 
3974 /*
3975  * pcmcia_get_minors()
3976  *	We need to traverse the minor node list of the
3977  *	dip if there are any.  This takes two passes;
3978  *	one to get the count and buffer size and the
3979  *	other to actually copy the data into the buffer.
3980  *	The framework requires that the dip be locked
3981  *	during this time to avoid breakage as well as the
3982  *	driver being locked.
3983  */
3984 int
3985 pcmcia_get_minors(dev_info_t *dip, struct pcm_make_dev **minors)
3986 {
3987 	int count = 0;
3988 	struct ddi_minor_data *dp;
3989 	struct pcm_make_dev *md;
3990 	int socket;
3991 	major_t major;
3992 	struct dev_ops *ops;
3993 
3994 	socket = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3995 	    PCM_DEV_SOCKET, -1);
3996 	mutex_enter(&(DEVI(dip)->devi_lock));
3997 	if (DEVI(dip)->devi_minor != (struct ddi_minor_data *)NULL) {
3998 		for (dp = DEVI(dip)->devi_minor;
3999 			dp != (struct ddi_minor_data *)NULL;
4000 			dp = dp->next) {
4001 			count++; /* have one more */
4002 		}
4003 		/* we now know how many nodes to allocate */
4004 		md = kmem_zalloc(count * sizeof (struct pcm_make_dev),
4005 		    KM_NOSLEEP);
4006 		if (md != NULL) {
4007 			*minors = md;
4008 			for (dp = DEVI(dip)->devi_minor;
4009 				dp != (struct ddi_minor_data *)NULL;
4010 				dp = dp->next, md++) {
4011 #if defined(PCMCIA_DEBUG)
4012 				if (pcmcia_debug > 1) {
4013 					cmn_err(CE_CONT,
4014 						"pcmcia_get_minors: name=%s,"
4015 						"socket=%d, stype=%x, "
4016 						"ntype=%s, dev_t=%x",
4017 						dp->ddm_name,
4018 						socket,
4019 						dp->ddm_spec_type,
4020 						dp->ddm_node_type,
4021 						(int)dp->ddm_dev);
4022 					cmn_err(CE_CONT,
4023 						"\tbind name = %s\n",
4024 						ddi_binding_name(dip));
4025 				}
4026 #endif
4027 				md->socket = socket;
4028 				md->op = SS_CSINITDEV_CREATE_DEVICE;
4029 				md->dev = dp->ddm_dev;
4030 				md->type = dp->ddm_spec_type;
4031 				(void) strcpy(md->driver,
4032 				    ddi_binding_name(dip));
4033 				major = ddi_name_to_major(md->driver);
4034 				ops = ddi_get_driver(dip);
4035 				LOCK_DEV_OPS(&devnamesp[major].dn_lock);
4036 				pcm_pathname(dip, dp->ddm_name, md->path);
4037 				INCR_DEV_OPS_REF(ops);
4038 				(void) ddi_pathname(dip, md->path);
4039 				DECR_DEV_OPS_REF(ops);
4040 				UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
4041 				(void) sprintf(md->path + strlen(md->path),
4042 					":%s", dp->ddm_name);
4043 				if (dp->next == NULL)
4044 					/* no more */
4045 					md->flags |= PCM_EVENT_MORE;
4046 			}
4047 		} else {
4048 			count = 0;
4049 		}
4050 	}
4051 	mutex_exit(&(DEVI(dip)->devi_lock));
4052 	return (count);
4053 }
4054 
4055 #if defined(PCMCIA_DEBUG)
4056 static char *ddmtypes[] = { "minor", "alias", "default", "internal" };
4057 
4058 static void
4059 pcmcia_dump_minors(dev_info_t *dip)
4060 {
4061 	int count = 0;
4062 	struct ddi_minor_data *dp;
4063 	int unit, major;
4064 	dev_info_t *np;
4065 
4066 	unit = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4067 	    PCM_DEV_SOCKET, -1);
4068 	cmn_err(CE_CONT,
4069 		"pcmcia_dump_minors: dip=%p, socket=%d\n", (void *)dip, unit);
4070 
4071 	major = ddi_driver_major(dip);
4072 	if (major != -1) {
4073 		for (np = devnamesp[major].dn_head; np != NULL;
4074 		    np = (dev_info_t *)DEVI(np)->devi_next) {
4075 			char *cf2 = "";
4076 			char *cur = "";
4077 			if (i_ddi_node_state(np) == DS_READY)
4078 				cf2 = "DS_READY";
4079 			if (np == dip)
4080 				cur = "CUR";
4081 			cmn_err(CE_CONT, "\tsibs: %s %s %s\n",
4082 				ddi_binding_name(np), cf2, cur);
4083 
4084 			mutex_enter(&(DEVI(np)->devi_lock));
4085 			if (DEVI(np)->devi_minor !=
4086 			    (struct ddi_minor_data *)NULL) {
4087 				for (dp = DEVI(np)->devi_minor;
4088 				    dp != (struct ddi_minor_data *)NULL;
4089 				    dp = dp->next) {
4090 					count++; /* have one more */
4091 				}
4092 				for (dp = DEVI(dip)->devi_minor;
4093 				    dp != (struct ddi_minor_data *)NULL;
4094 				    dp = dp->next) {
4095 					cmn_err(CE_CONT, "\ttype=%s, name=%s,"
4096 						"socket=%d, stype=%x, "
4097 						"ntype=%s, dev_t=%x",
4098 						ddmtypes[dp->type],
4099 						dp->ddm_name,
4100 						unit,
4101 						dp->ddm_spec_type,
4102 						dp->ddm_node_type,
4103 						(int)dp->ddm_dev);
4104 					cmn_err(CE_CONT, "\tbind name = %s\n",
4105 						ddi_binding_name(np));
4106 				}
4107 			}
4108 			mutex_exit(&(DEVI(np)->devi_lock));
4109 		}
4110 	}
4111 }
4112 #endif
4113 
4114 /*
4115  * experimental merging code
4116  * what are the things that we should merge on?
4117  *	match something by name in the "compatible" property
4118  *	restrict to a specific "socket"
4119  *	restrict to a specific "instance"
4120  */
4121 /*ARGSUSED*/
4122 static int
4123 pcmcia_merge_conf(dev_info_t *dip)
4124 {
4125 	return (0);		/* merge failed */
4126 }
4127 
4128 /*
4129  * pcmcia_mfc_intr()
4130  *	Multifunction Card interrupt handler
4131  *	While some adapters share interrupts at the lowest
4132  *	level, some can't.  In order to be consistent, we
4133  *	split multifunction cards out with this intercept and
4134  *	allow the low level to do what is best for it.
4135  *	the arg is a pcmcia_socket structure and all interrupts
4136  *	are per-socket in this case.  We also have the option
4137  *	to optimize if the cards support it.  It also means
4138  *	that we can use the INTRACK mode if it proves desirable
4139  */
4140 /*ARGSUSED*/
4141 static uint32_t
4142 pcmcia_mfc_intr(caddr_t arg1, caddr_t arg2)
4143 {
4144 	pcmcia_logical_socket_t *sockp;
4145 	inthandler_t *intr, *first;
4146 	int done, result;
4147 
4148 	sockp = (pcmcia_logical_socket_t *)arg1;
4149 
4150 #if defined(PCMCIA_DEBUG)
4151 	if (pcmcia_debug > 1) {
4152 		cmn_err(CE_CONT, "pcmcia_mfc_intr sockp=%p"
4153 		    " ls_inthandlers=%p\n"
4154 		    "\t ls_flags=0x%x PCS_IRQ_ENABLED=0x%x \n",
4155 		    (void *) sockp, (void *) sockp->ls_inthandlers,
4156 		    sockp->ls_flags, PCS_IRQ_ENABLED);
4157 	}
4158 #endif
4159 
4160 	if (sockp == NULL || sockp->ls_inthandlers == NULL ||
4161 	    !(sockp->ls_flags & PCS_IRQ_ENABLED))
4162 		return (DDI_INTR_UNCLAIMED);
4163 
4164 	mutex_enter(&sockp->ls_ilock);
4165 	for (done = 0, result = 0, first = intr = sockp->ls_inthandlers;
4166 		intr != NULL && !done; intr = intr->next) {
4167 		result |= intr->intr(intr->arg1, intr->arg2);
4168 		if (intr->next == first)
4169 			done++;
4170 	}
4171 	if (intr == NULL) {
4172 		cmn_err(CE_WARN, "pcmcia_mfc_intr: bad MFC handler list");
4173 	}
4174 	if (sockp->ls_inthandlers)
4175 		sockp->ls_inthandlers = sockp->ls_inthandlers->next;
4176 
4177 	mutex_exit(&sockp->ls_ilock);
4178 	return (result ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
4179 }
4180 
4181 /*
4182  * pcmcia_power(dip)
4183  *	control power for nexus and children
4184  */
4185 int
4186 pcmcia_power(dev_info_t *dip, int component, int level)
4187 {
4188 #if 0
4189 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
4190 	int i;
4191 	/*
4192 	 * for now, we only have one component.  Should there be one per-socket?
4193 	 * the level is only one (power on or off)
4194 	 */
4195 	if (component != 0 || level > 1)
4196 		return (DDI_FAILURE);
4197 
4198 	for (i = 0; i < pcic->pc_numsockets; i++) {
4199 		if (pcic->pc_callback)
4200 			PC_CALLBACK(dip, pcic->pc_cb_arg,
4201 			    (level == 0) ? PCE_PM_SUSPEND :
4202 			    PCE_PM_RESUME,
4203 			    i);
4204 	}
4205 #else
4206 	cmn_err(CE_WARN, "pcmcia_power: component=%d, level=%d for %s",
4207 		component, level, ddi_get_name_addr(dip));
4208 	return (DDI_FAILURE);
4209 #endif
4210 }
4211 
4212 void
4213 pcmcia_begin_resume(dev_info_t *dip)
4214 {
4215 	int i;
4216 	struct pcmcia_adapter *adapt = NULL;
4217 	for (i = 0; i < pcmcia_num_adapters; i++) {
4218 		if (pcmcia_adapters[i]->pca_dip == dip) {
4219 			adapt = pcmcia_adapters[i];
4220 			break;
4221 		}
4222 	}
4223 	if (adapt == NULL)
4224 		return;
4225 
4226 	for (i = 0; i < adapt->pca_numsockets; i++) {
4227 		int s;
4228 		s = adapt->pca_first_socket + i;
4229 		if (pcmcia_sockets[s]->ls_flags & PCS_SUSPENDED) {
4230 			if (pcmcia_sockets[s]->ls_flags &
4231 			    (1 << PCE_PM_RESUME)) {
4232 				(void) cs_event(PCE_PM_RESUME, s, 0);
4233 				pcm_event_manager(PCE_PM_RESUME, s, NULL);
4234 			}
4235 			(void) cs_event(PCE_CARD_REMOVAL, s, 0);
4236 			pcm_event_manager(PCE_CARD_REMOVAL, s, NULL);
4237 		}
4238 	}
4239 }
4240 
4241 void
4242 pcmcia_wait_insert(dev_info_t *dip)
4243 {
4244 	int i, f, tries, done;
4245 	clock_t tm;
4246 	struct pcmcia_adapter *adapt = NULL;
4247 	anp_t *nexus;
4248 
4249 	for (i = 0; i < pcmcia_num_adapters; i++) {
4250 		if (pcmcia_adapters[i]->pca_dip == dip) {
4251 			adapt = pcmcia_adapters[i];
4252 			break;
4253 		}
4254 	}
4255 	if (adapt == NULL)
4256 		return;
4257 
4258 	for (tries = adapt->pca_numsockets * 10; tries > 0; tries--) {
4259 		done = 1;
4260 		mutex_enter(&pcmcia_global_lock);
4261 		for (i = 0; i < adapt->pca_numsockets; i++) {
4262 			int s;
4263 			s = adapt->pca_first_socket + i;
4264 			for (f = 0; f < PCMCIA_MAX_FUNCTIONS; f++)
4265 				if (pcmcia_sockets[s] &&
4266 				    pcmcia_sockets[s]->ls_flags &
4267 				    PCS_SUSPENDED) {
4268 					done = 0;
4269 					break;
4270 				}
4271 		}
4272 		if (!done) {
4273 			tm = ddi_get_lbolt();
4274 			(void) cv_timedwait(&pcmcia_condvar,
4275 			    &pcmcia_global_lock,
4276 			    tm + drv_usectohz(100000));
4277 		} else {
4278 			tries = 0;
4279 		}
4280 		mutex_exit(&pcmcia_global_lock);
4281 	}
4282 
4283 	nexus = (anp_t *)ddi_get_driver_private(dip);
4284 	pcmcia_find_cards(nexus);
4285 }
4286 
4287 int
4288 pcmcia_map_reg(dev_info_t *pdip, dev_info_t *dip, ra_return_t *ra,
4289 		uint32_t state, caddr_t *base,
4290 		ddi_acc_handle_t *handle, ddi_device_acc_attr_t *attrib,
4291 		uint32_t req_base)
4292 {
4293 	struct pcmcia_parent_private *ppd;
4294 	int rnum = 0, type = PCMCIA_MAP_MEM;
4295 	ddi_map_req_t mr;
4296 	ddi_acc_hdl_t *hp;
4297 	int result;
4298 	struct regspec *reg;
4299 	ddi_device_acc_attr_t attr;
4300 
4301 	if (dip != NULL) {
4302 		ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
4303 		if (ppd == NULL)
4304 			return (DDI_FAILURE);
4305 		for (rnum = 1; rnum < ppd->ppd_nreg; rnum++) {
4306 			struct pcm_regs *p;
4307 			p = &ppd->ppd_reg[rnum];
4308 			if (state & WS_IO) {
4309 				/* need I/O */
4310 				type = PCMCIA_MAP_IO;
4311 				/*
4312 				 * We want to find an IO regspec. When we
4313 				 *	find one, it either has to match
4314 				 *	the caller's requested base address
4315 				 *	or it has to be relocatable.
4316 				 * We match on the requested base address
4317 				 *	rather than the allocated base
4318 				 *	address so that we handle the case
4319 				 *	of adapters that have IO window base
4320 				 *	relocation registers.
4321 				 */
4322 				if ((p->phys_hi &
4323 				    PC_REG_SPACE(PC_REG_SPACE_IO)) &&
4324 					((req_base == p->phys_lo) ||
4325 					!(p->phys_hi & PC_REG_RELOC(1))))
4326 				    break;
4327 			} else {
4328 				/* need memory */
4329 				type = PCMCIA_MAP_MEM;
4330 				if (p->phys_hi &
4331 				    PC_REG_SPACE(PC_REG_SPACE_MEMORY|
4332 				    PC_REG_SPACE_ATTRIBUTE))
4333 					break;
4334 			}
4335 		}
4336 		if (rnum >= ppd->ppd_nreg)
4337 			return (DDI_FAILURE);
4338 	} else if (state & WS_IO) {
4339 		return (DDI_FAILURE);
4340 	}
4341 
4342 	reg = kmem_zalloc(sizeof (pci_regspec_t), KM_SLEEP);
4343 	reg = pcmcia_cons_regspec(pdip, type, (uchar_t *)reg, ra);
4344 
4345 	if (attrib == NULL ||
4346 	    attrib->devacc_attr_version != DDI_DEVICE_ATTR_V0) {
4347 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4348 		attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
4349 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4350 	} else {
4351 		attr = *attrib;
4352 	}
4353 	/*
4354 	 * Allocate and initialize the common elements of data access handle.
4355 	 */
4356 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
4357 	hp = impl_acc_hdl_get(*handle);
4358 	hp->ah_vers = VERS_ACCHDL;
4359 	hp->ah_dip = dip != NULL ? dip : pdip;
4360 	hp->ah_rnumber = rnum;
4361 	hp->ah_offset = 0;
4362 	hp->ah_len = ra->ra_len;
4363 	hp->ah_acc = attr;
4364 
4365 	/*
4366 	 * Set up the mapping request and call to parent.
4367 	 */
4368 	mr.map_op = DDI_MO_MAP_LOCKED;
4369 	mr.map_type = DDI_MT_REGSPEC;
4370 	mr.map_obj.rp = reg;
4371 	mr.map_prot = PROT_READ | PROT_WRITE;
4372 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
4373 	mr.map_handlep = hp;
4374 	mr.map_vers = DDI_MAP_VERSION;
4375 
4376 	result = ddi_map(pdip, &mr, 0, ra->ra_len, base);
4377 	if (result != DDI_SUCCESS) {
4378 		impl_acc_hdl_free(*handle);
4379 		*handle = (ddi_acc_handle_t)NULL;
4380 		kmem_free(reg, sizeof (pci_regspec_t));
4381 	} else {
4382 		hp->ah_addr = *base;
4383 		if (mr.map_op == DDI_MO_UNMAP)
4384 			ra = NULL;
4385 		if (dip != NULL)
4386 			pcmcia_set_assigned(dip, rnum, ra);
4387 	}
4388 
4389 	return (result);
4390 }
4391 
4392 struct pcmcia_adapter *
4393 pcmcia_get_adapter(dev_info_t *dip)
4394 {
4395 	int i;
4396 
4397 	for (i = 0; i < pcmcia_num_adapters; i++) {
4398 		if (pcmcia_adapters[i] &&
4399 		    pcmcia_adapters[i]->pca_dip == dip) {
4400 			return (pcmcia_adapters[i]);
4401 		}
4402 	}
4403 	return (NULL);
4404 }
4405 
4406 
4407 void
4408 pcmcia_set_assigned(dev_info_t *dip, int rnum, ra_return_t *ret)
4409 {
4410 	struct pcmcia_parent_private *ppd;
4411 	struct pcm_regs *reg, *assign;
4412 
4413 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
4414 	if (ppd) {
4415 		reg = &ppd->ppd_reg[rnum];
4416 		assign = &ppd->ppd_assigned[rnum];
4417 		if (ret) {
4418 			if (assign->phys_hi == 0) {
4419 				assign->phys_hi = reg->phys_hi;
4420 				assign->phys_lo = ret->ra_addr_lo;
4421 				assign->phys_len = ret->ra_len;
4422 			} else if (assign->phys_lo != ret->ra_addr_lo) {
4423 #ifdef PCMCIA_DEBUG
4424 				cmn_err(CE_WARN, "pcmcia: bad address:"
4425 					"%s=<%x,%x>",
4426 					ddi_get_name_addr(dip),
4427 					ret->ra_addr_lo, assign->phys_lo);
4428 #else
4429 				cmn_err(CE_WARN, "!pcmcia: bad address:"
4430 					"%s=<%x,%x>",
4431 					ddi_get_name_addr(dip),
4432 					ret->ra_addr_lo, (int)assign->phys_lo);
4433 #endif
4434 			}
4435 			assign->phys_hi = PC_INCR_REFCNT(assign->phys_hi);
4436 		} else {
4437 			int i;
4438 			assign->phys_hi = PC_DECR_REFCNT(assign->phys_hi);
4439 			i = PC_GET_REG_REFCNT(assign->phys_hi);
4440 			if (i == 0) {
4441 				assign->phys_hi = 0;
4442 				assign->phys_lo = 0;
4443 				assign->phys_len = 0;
4444 			}
4445 		}
4446 	}
4447 }
4448 
4449 int
4450 pcmcia_alloc_mem(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret)
4451 {
4452 	return (pcmcia_ra_alloc(dip, req, ret, NDI_RA_TYPE_MEM));
4453 }
4454 
4455 int
4456 pcmcia_alloc_io(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret)
4457 {
4458 	return (pcmcia_ra_alloc(dip, req, ret, NDI_RA_TYPE_IO));
4459 }
4460 
4461 int
4462 pcmcia_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret,
4463 		char *type)
4464 {
4465 	int rval;
4466 	uint64_t base = 0;
4467 	uint64_t len = 0;
4468 
4469 	/*
4470 	 * Allocate space from busra resource list
4471 	 * should not return an address > 32 bits
4472 	 */
4473 
4474 	if ((ndi_ra_alloc(dip, req, &base, &len, type, NDI_RA_PASS)
4475 							== NDI_FAILURE) ||
4476 	    ((base >> 32) != 0)) {
4477 		ret->ra_addr_lo = 0;
4478 		ret->ra_len = 0;
4479 		rval = DDI_FAILURE;
4480 	} else {
4481 		ret->ra_addr_lo =  base & 0xffffffff;
4482 		ret->ra_len = len;
4483 		rval = DDI_SUCCESS;
4484 	}
4485 	ret->ra_addr_hi = 0;
4486 	return (rval);
4487 }
4488 
4489 int
4490 pcmcia_free_mem(dev_info_t *dip, ra_return_t *ret)
4491 {
4492 	return (pcmcia_ra_free(dip, ret, NDI_RA_TYPE_MEM));
4493 }
4494 
4495 int
4496 pcmcia_free_io(dev_info_t *dip, ra_return_t *ret)
4497 {
4498 	return (pcmcia_ra_free(dip, ret, NDI_RA_TYPE_IO));
4499 }
4500 
4501 int
4502 pcmcia_ra_free(dev_info_t *dip, ra_return_t *ret, char *type)
4503 {
4504 	if (ndi_ra_free(dip, (uint64_t)ret->ra_addr_lo, (uint64_t)ret->ra_len,
4505 	    type, NDI_RA_PASS) == NDI_SUCCESS) {
4506 		return (DDI_SUCCESS);
4507 	} else {
4508 		return (DDI_FAILURE);
4509 	}
4510 }
4511 
4512 
4513 /*
4514  * when the low level device configuration does resource assignment
4515  * (devconf) then free the allocated resources so we can reassign them
4516  * later.  Walk the child list to get them.
4517  */
4518 void
4519 pcmcia_free_resources(dev_info_t *self)
4520 {
4521 	struct regspec *assigned;
4522 	int len;
4523 	dev_info_t *dip;
4524 	int circular;
4525 
4526 	ndi_devi_enter(self, &circular);
4527 	/* do searches in compatible property order */
4528 	for (dip = (dev_info_t *)DEVI(self)->devi_child;
4529 	    dip != NULL;
4530 	    dip = (dev_info_t *)DEVI(dip)->devi_sibling) {
4531 		len = 0;
4532 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
4533 		    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP,
4534 		    "assigned-addresses",
4535 		    (caddr_t)&assigned,
4536 		    &len) == DDI_PROP_SUCCESS) {
4537 			/*
4538 			 * if there are assigned resources at this point,
4539 			 * then the OBP or devconf have assigned them and
4540 			 * they need to be freed.
4541 			 */
4542 			kmem_free(assigned, len);
4543 		}
4544 	}
4545 	ndi_devi_exit(self, circular);
4546 }
4547 
4548 /*
4549  * this is the equivalent of pcm_get_intr using ra_allocs.
4550  * returns -1 if failed, otherwise returns the allocated irq.
4551  * The input request, if less than zero it means not a specific
4552  * irq requested. If larger then 0 then we are requesting that specific
4553  * irq
4554  */
4555 int
4556 pcmcia_get_intr(dev_info_t *dip, int request)
4557 {
4558 	ndi_ra_request_t req;
4559 	uint64_t base;
4560 	uint64_t len;
4561 	int err;
4562 
4563 	bzero(&req, sizeof (req));
4564 	base = 0;
4565 	len = 1;
4566 	if (request >= 0) {
4567 		req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
4568 		req.ra_len = 1;
4569 		req.ra_addr = (uint64_t)request;
4570 	}
4571 
4572 	req.ra_boundbase = 0;
4573 	req.ra_boundlen = 0xffffffffUL;
4574 	req.ra_flags |= NDI_RA_ALLOC_BOUNDED;
4575 
4576 	err = ndi_ra_alloc(dip, &req, &base, &len, NDI_RA_TYPE_INTR,
4577 	    NDI_RA_PASS);
4578 
4579 	if (err == NDI_FAILURE) {
4580 		return (-1);
4581 	} else {
4582 		return ((int)base);
4583 	}
4584 }
4585 
4586 
4587 int
4588 pcmcia_return_intr(dev_info_t *dip, int request)
4589 {
4590 	if ((ndi_ra_free(dip, (uint64_t)request, 1, NDI_RA_TYPE_INTR,
4591 	    NDI_RA_PASS)) == NDI_SUCCESS) {
4592 		return (0);
4593 	} else
4594 		return (-1);
4595 
4596 }
4597 
4598 #ifdef sparc
4599 
4600 int
4601 pcmcia_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
4602     ddi_intr_handle_impl_t *hdlp)
4603 {
4604 
4605 	struct pcmcia_parent_private *ppd;
4606 	pcmcia_logical_socket_t *sockp;
4607 	int socket, ret;
4608 	struct pcmcia_adapter *adapt;
4609 	set_irq_handler_t handler;
4610 	struct intrspec *pispec;
4611 
4612 #if defined(PCMCIA_DEBUG)
4613 	if (pcmcia_debug) {
4614 		cmn_err(CE_CONT,
4615 		    "pcmcia_add_intr_impl() entered "
4616 		    "dip=%p rdip=%p hdlp=%p \n",
4617 		    (void *)dip, (void *)rdip, (void *)hdlp);
4618 	}
4619 #endif
4620 
4621 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
4622 	socket = ppd->ppd_socket;
4623 	sockp = pcmcia_sockets[socket];
4624 	adapt = sockp->ls_adapter;
4625 
4626 #if defined(PCMCIA_DEBUG)
4627 	if (pcmcia_debug) {
4628 		cmn_err(CE_CONT, "pcmcia_add_intr_impl()"
4629 		    " ppd_flags=0X%x PPD_CARD_MULTI=0X%x\n"
4630 		    " ppd_intrspec=%p ls_inthandlers=%p\n",
4631 		    ppd->ppd_flags, PPD_CARD_MULTI,
4632 		    (void *) ppd->ppd_intrspec,
4633 		    (void *)sockp->ls_inthandlers);
4634 	}
4635 #endif
4636 
4637 	/*
4638 	 * calculate IPL level when we support multiple levels
4639 	 */
4640 	pispec = ppd->ppd_intrspec;
4641 	if (pispec == NULL) {
4642 		sockp->ls_error = BAD_IRQ;
4643 		return (DDI_FAILURE);
4644 	}
4645 
4646 	handler.socket = sockp->ls_socket;
4647 	handler.irq = 0;	/* default case */
4648 	handler.handler = (f_tt *)hdlp->ih_cb_func;
4649 	handler.arg1 = hdlp->ih_cb_arg1;
4650 	handler.arg2 = hdlp->ih_cb_arg2;
4651 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
4652 
4653 	/*
4654 	 * check if multifunction and do the right thing
4655 	 * we put an intercept in between the mfc handler and
4656 	 * us so we can catch and process.  We might be able
4657 	 * to optimize this depending on the card features
4658 	 * (a future option).
4659 	 */
4660 	if (ppd->ppd_flags & PPD_CARD_MULTI) {
4661 		inthandler_t *intr;
4662 		/*
4663 		 * note that the first function is a special
4664 		 * case since it sets things up.  We fall through
4665 		 * to the lower code and get the hardware set up.
4666 		 * subsequent times we just lock the list and insert
4667 		 * the handler and all is well.
4668 		 */
4669 		intr = kmem_zalloc(sizeof (inthandler_t), KM_NOSLEEP);
4670 		if (intr == NULL) {
4671 			sockp->ls_error = BAD_IRQ;
4672 			return (DDI_FAILURE);
4673 		}
4674 		intr->intr = hdlp->ih_cb_func;
4675 		intr->handler_id = (uint_t)(uintptr_t)rdip;
4676 		intr->arg1 = hdlp->ih_cb_arg1;
4677 		intr->arg2 = hdlp->ih_cb_arg2;
4678 		intr->socket = socket;
4679 
4680 		mutex_enter(&sockp->ls_ilock);
4681 		if (sockp->ls_inthandlers == NULL) {
4682 			intr->next = intr->prev = intr;
4683 			sockp->ls_inthandlers = intr;
4684 			sockp->ls_mfintr_dip = rdip;
4685 			mutex_exit(&sockp->ls_ilock);
4686 
4687 			/*
4688 			 * replace first function handler with
4689 			 * the mfc handler
4690 			 */
4691 			handler.handler =  (f_tt *)pcmcia_mfc_intr;
4692 			handler.arg1 = (caddr_t)sockp;
4693 			handler.arg2 = NULL;
4694 		} else {
4695 			insque(intr, sockp->ls_inthandlers);
4696 			mutex_exit(&sockp->ls_ilock);
4697 
4698 			pispec->intrspec_vec = sockp->ls_intr_vec;
4699 			pispec->intrspec_pri = sockp->ls_intr_pri;
4700 			hdlp->ih_pri = sockp->ls_intr_pri;
4701 
4702 			return (DDI_SUCCESS);
4703 		}
4704 	}
4705 
4706 #if defined(PCMCIA_DEBUG)
4707 	if (pcmcia_debug) {
4708 		cmn_err(CE_CONT, "pcmcia_add_intr_impl() let adapter do it\n");
4709 	}
4710 #endif
4711 	pispec->intrspec_func = (uint32_t (*)())handler.handler;
4712 
4713 	/* set default IPL then check for override */
4714 
4715 	pispec->intrspec_pri = sockp->ls_intr_pri;
4716 	hdlp->ih_pri = pispec->intrspec_pri;
4717 
4718 #if defined(PCMCIA_DEBUG)
4719 	if (pcmcia_debug) {
4720 		cmn_err(CE_CONT, "pcmcia_add_intr_impl() socket=%d irq=%d"
4721 		    " handler_id=0X%x handler=%p arg1=%p arg2=%p\n",
4722 		    handler.socket, handler.irq,
4723 		    handler.handler_id, (void *)handler.handler, handler.arg1,
4724 		    handler.arg2);
4725 	}
4726 #endif
4727 
4728 	if ((ret = SET_IRQ(sockp->ls_if, adapt->pca_dip, &handler)) !=
4729 	    SUCCESS) {
4730 		sockp->ls_error = ret;
4731 		return (DDI_FAILURE);
4732 	}
4733 
4734 #if defined(PCMCIA_DEBUG)
4735 	if (pcmcia_debug) {
4736 		cmn_err(CE_CONT, "pcmcia_add_intr_impl()"
4737 		    " iblk_cookie=%p idev_cookie=%p\n"
4738 		    " ls_flags=0X%x PCS_COOKIES_VALID=0X%x\n",
4739 		    (void *)handler.iblk_cookie,
4740 		    (void *)handler.idev_cookie,
4741 		    sockp->ls_flags, PCS_COOKIES_VALID);
4742 	}
4743 #endif
4744 
4745 	if (!(sockp->ls_flags & PCS_COOKIES_VALID)) {
4746 		hdlp->ih_pri = (uint_t)(uintptr_t)*handler.iblk_cookie;
4747 		sockp->ls_iblk = *handler.iblk_cookie;
4748 		sockp->ls_idev = *handler.idev_cookie;
4749 		sockp->ls_flags |= PCS_COOKIES_VALID;
4750 	}
4751 
4752 	return (DDI_SUCCESS);
4753 }
4754 
4755 void
4756 pcmcia_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
4757     ddi_intr_handle_impl_t *hdlp)
4758 {
4759 
4760 	struct pcmcia_parent_private *ppd;
4761 	pcmcia_logical_socket_t *sockp;
4762 	clear_irq_handler_t handler;
4763 	struct intrspec *pispec;
4764 	int socket;
4765 
4766 #if defined(PCMCIA_DEBUG)
4767 	if (pcmcia_debug) {
4768 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl() entered"
4769 		    " dip=%p rdip=%p hdlp=%p\n",
4770 		    (void *)dip, (void *)rdip, (void *)hdlp);
4771 	}
4772 #endif
4773 
4774 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
4775 	socket = ppd->ppd_socket;
4776 	sockp = pcmcia_sockets[socket];
4777 	pispec = ppd->ppd_intrspec;
4778 
4779 #if defined(PCMCIA_DEBUG)
4780 	if (pcmcia_debug) {
4781 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl()"
4782 		    " ls_inthandlers=%p ls_intrspec=%p\n",
4783 		    (void *)sockp->ls_inthandlers,
4784 		    (void *)&sockp->ls_intrspec);
4785 	}
4786 #endif
4787 
4788 	/* first handle the multifunction case since it is simple */
4789 	mutex_enter(&sockp->ls_ilock);
4790 	if (sockp->ls_inthandlers != NULL) {
4791 		/* we must be MFC */
4792 		inthandler_t *intr;
4793 		int remhandler = 0;
4794 		intr = sockp->ls_inthandlers;
4795 
4796 		/* Check if there is only one handler left */
4797 		if ((intr->next == intr) && (intr->prev == intr)) {
4798 			if (intr->handler_id == (unsigned)(uintptr_t)rdip) {
4799 				sockp->ls_inthandlers = NULL;
4800 				remhandler++;
4801 				kmem_free(intr, sizeof (inthandler_t));
4802 			}
4803 		} else {
4804 			inthandler_t *first;
4805 			int done;
4806 
4807 			for (done = 0, first = intr; !done; intr = intr->next) {
4808 				if (intr->next == first)
4809 					done++;
4810 				if (intr->handler_id ==
4811 				    (unsigned)(uintptr_t)rdip) {
4812 					done++;
4813 
4814 					/*
4815 					 * If we're about to remove the
4816 					 *	handler at the head of
4817 					 *	the list, make the next
4818 					 *	handler in line the head.
4819 					 */
4820 					if (sockp->ls_inthandlers == intr)
4821 						sockp->ls_inthandlers =
4822 						    intr->next;
4823 
4824 					remque(intr);
4825 					kmem_free(intr, sizeof (inthandler_t));
4826 					break;
4827 				} /* handler_id */
4828 			} /* for */
4829 		} /* intr->next */
4830 
4831 		if (!remhandler) {
4832 			mutex_exit(&sockp->ls_ilock);
4833 			return;
4834 		}
4835 
4836 		/* need to get the dip that was used to add the handler */
4837 		rdip = sockp->ls_mfintr_dip;
4838 	}
4839 
4840 	mutex_exit(&sockp->ls_ilock);
4841 
4842 #if defined(PCMCIA_DEBUG)
4843 	if (pcmcia_debug) {
4844 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl()"
4845 		    " pispec=%p rdip=%p\n",
4846 		    (void *)pispec, (void *)rdip);
4847 	}
4848 #endif
4849 
4850 	handler.socket = sockp->ls_socket;
4851 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
4852 	handler.handler = (f_tt *)pispec->intrspec_func;
4853 	CLEAR_IRQ(sockp->ls_if, dip, &handler);
4854 }
4855 
4856 
4857 /* Consolidated interrupt processing interface */
4858 /*ARGSUSED*/
4859 int
4860 pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
4861     ddi_intr_handle_impl_t *hdlp, void *result)
4862 {
4863 	int	ret = DDI_SUCCESS;
4864 
4865 #if defined(PCMCIA_DEBUG)
4866 	if (pcmcia_debug) {
4867 		cmn_err(CE_CONT, "pcmcia_intr_ops() intr_op=%d\n",
4868 		    (int)intr_op);
4869 	}
4870 #endif
4871 
4872 	switch (intr_op) {
4873 	case DDI_INTROP_GETCAP:
4874 		*(int *)result = DDI_INTR_FLAG_LEVEL;
4875 		break;
4876 	case DDI_INTROP_SETCAP:
4877 		ret = DDI_ENOTSUP;
4878 		break;
4879 	case DDI_INTROP_ALLOC:
4880 		*(int *)result = hdlp->ih_scratch1;
4881 		break;
4882 	case DDI_INTROP_FREE:
4883 		break;
4884 	case DDI_INTROP_GETPRI:
4885 		if (pcmcia_add_intr_impl(dip, rdip, hdlp) != DDI_SUCCESS)
4886 			return (DDI_FAILURE);
4887 		*(int *)result = hdlp->ih_pri;
4888 		pcmcia_remove_intr_impl(dip, rdip, hdlp);
4889 		break;
4890 	case DDI_INTROP_SETPRI:
4891 		break;
4892 	case DDI_INTROP_ADDISR:
4893 		ret = pcmcia_add_intr_impl(dip, rdip, hdlp);
4894 		break;
4895 	case DDI_INTROP_REMISR:
4896 		pcmcia_remove_intr_impl(dip, rdip, hdlp);
4897 		break;
4898 	case DDI_INTROP_ENABLE:
4899 	case DDI_INTROP_DISABLE:
4900 		break;
4901 	case DDI_INTROP_NINTRS:
4902 	case DDI_INTROP_NAVAIL:
4903 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
4904 		break;
4905 	case DDI_INTROP_SUPPORTED_TYPES:
4906 		/* PCI nexus driver supports only fixed interrupts */
4907 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
4908 		    DDI_INTR_TYPE_FIXED : 0;
4909 		break;
4910 	default:
4911 		ret = DDI_ENOTSUP;
4912 		break;
4913 	}
4914 
4915 	return (ret);
4916 }
4917 
4918 #elif defined(__x86) || defined(__amd64)
4919 
4920 static struct intrspec	*pcmcia_intr_get_ispec(dev_info_t *, int,
4921 			    pcmcia_logical_socket_t **);
4922 static struct intrspec	*pcmcia_intr_add_isr(dev_info_t *, dev_info_t *,
4923 			    ddi_intr_handle_impl_t *);
4924 static int		pcmcia_intr_enable_isr(dev_info_t *, dev_info_t *,
4925 			    ddi_intr_handle_impl_t *);
4926 static void		pcmcia_intr_remove_isr(dev_info_t *, dev_info_t *,
4927 			    ddi_intr_handle_impl_t *);
4928 static void		pcmcia_intr_disable_isr(dev_info_t *, dev_info_t *,
4929 			    ddi_intr_handle_impl_t *);
4930 
4931 /*
4932  * pcmcia_intr_get_ispec:
4933  *	This is mostly copied from older 'pcmcia_get_intrspec' function
4934  */
4935 static struct intrspec *
4936 pcmcia_intr_get_ispec(dev_info_t *rdip, int inum,
4937     pcmcia_logical_socket_t **sockp)
4938 {
4939 	int				socket;
4940 	struct intrspec			*intrspec;
4941 	struct pcmcia_parent_private	*ppd;
4942 
4943 	if ((int)inum > 0 || (ddi_getprop(DDI_DEV_T_ANY, rdip,
4944 	    DDI_PROP_DONTPASS, "interrupts", -1) < 0))
4945 		return (NULL);
4946 
4947 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
4948 	if (ppd == NULL || ppd->ppd_intrspec == NULL)
4949 		return (NULL);
4950 
4951 	if ((socket = ppd->ppd_socket) < 0)
4952 		return (NULL);
4953 
4954 	if ((*sockp = pcmcia_sockets[socket]) == NULL)
4955 		return (NULL);
4956 
4957 	intrspec = ppd->ppd_intrspec;
4958 	if (intrspec->intrspec_vec == 0 && (*sockp)->ls_intr_vec != 0)
4959 		intrspec->intrspec_vec = (*sockp)->ls_intr_vec;
4960 
4961 	return (intrspec);
4962 }
4963 
4964 static struct intrspec *
4965 pcmcia_intr_add_isr(dev_info_t *dip, dev_info_t *rdip,
4966     ddi_intr_handle_impl_t *hdlp)
4967 {
4968 	int				socket;
4969 	struct intrspec			*ispecp;
4970 	struct pcmcia_adapter		*adapt;
4971 	pcmcia_logical_socket_t		*sockp;
4972 	struct pcmcia_parent_private	*ppd;
4973 
4974 #if defined(PCMCIA_DEBUG)
4975 	if (pcmcia_debug)
4976 		cmn_err(CE_CONT, "pcmcia_intr_add_isr: "
4977 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
4978 		    (void *)dip, (void *)rdip, (void *)hdlp);
4979 #endif	/* PCMCIA_DEBUG */
4980 
4981 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
4982 	socket = ppd->ppd_socket;
4983 	sockp = pcmcia_sockets[socket];
4984 	adapt = sockp->ls_adapter;
4985 
4986 	ispecp = ppd->ppd_intrspec;
4987 	if (ispecp == NULL) {
4988 		sockp->ls_error = BAD_IRQ;
4989 		return (ispecp);
4990 	}
4991 
4992 	/*
4993 	 * check if multifunction and do the right thing
4994 	 * we put an intercept in between the mfc handler and us so we can
4995 	 * catch and process. We might be able to optimize this depending
4996 	 * on the card features (a future option).
4997 	 */
4998 	if (ppd->ppd_flags & PPD_CARD_MULTI &&
4999 	    hdlp->ih_cb_func != pcmcia_mfc_intr) {
5000 		inthandler_t *intr;
5001 
5002 		/*
5003 		 * note that the first function is a special case since it
5004 		 * sets things up.  We fall through to the lower code and
5005 		 * get the hardware set up. Subsequent times we just lock
5006 		 * the list and insert the handler and all is well.
5007 		 */
5008 		intr = kmem_zalloc(sizeof (inthandler_t), KM_NOSLEEP);
5009 		if (intr == NULL) {
5010 			sockp->ls_error = BAD_IRQ;
5011 			return (NULL);
5012 		}
5013 
5014 		intr->intr = (uint32_t (*)())hdlp->ih_cb_func;
5015 		intr->handler_id = (uint32_t)(uintptr_t)rdip;
5016 		intr->arg1 = hdlp->ih_cb_arg1;
5017 		intr->arg2 = hdlp->ih_cb_arg2;
5018 		intr->socket = socket;
5019 		mutex_enter(&sockp->ls_ilock);
5020 		if (sockp->ls_inthandlers == NULL) {
5021 			intr->next = intr->prev = intr;
5022 			sockp->ls_inthandlers = intr;
5023 			sockp->ls_mfintr_dip = rdip;
5024 		} else {
5025 			insque(intr, sockp->ls_inthandlers);
5026 		}
5027 		mutex_exit(&sockp->ls_ilock);
5028 		return (ispecp);
5029 	}
5030 
5031 	/*
5032 	 * Do we need to allocate an IRQ at this point or not?
5033 	 */
5034 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
5035 		int i, irq;
5036 
5037 		/*
5038 		 * this adapter needs IRQ allocations
5039 		 * this is only necessary if it is the first function on the
5040 		 * card being setup. The socket will keep the allocation info
5041 		 */
5042 		/* all functions use same intrspec except mfc handler */
5043 		if (hdlp->ih_cb_func == pcmcia_mfc_intr) {
5044 			/*
5045 			 * We treat this special in order to allow things to
5046 			 * work properly for MFC cards. The intrspec for the
5047 			 * mfc dispatcher is intercepted and taken from the
5048 			 * logical socket in order to not be trying to
5049 			 * multiplex the meaning when ENABLE is called.
5050 			 */
5051 			ispecp = &sockp->ls_intrspec;
5052 			((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispecp;
5053 		}
5054 
5055 		if (adapt->pca_flags & PCA_IRQ_ISA) {
5056 			for (irq = -1, i = 1; irq == -1 && i < 16; i++) {
5057 				/* find available and usable IRQ level */
5058 				if (adapt->pca_avail_intr & (1 << i))
5059 					irq = pcmcia_get_intr(dip, i);
5060 			}
5061 		}
5062 		if (irq < 0) {
5063 			sockp->ls_error = NO_RESOURCE;
5064 			return (NULL);
5065 		}
5066 		hdlp->ih_vector = sockp->ls_intr_vec = irq;
5067 
5068 
5069 #if defined(PCMCIA_DEBUG)
5070 		if (pcmcia_debug)
5071 			cmn_err(CE_CONT, "allocated irq=%x\n", irq);
5072 #endif	/* PCMCIA_DEBUG */
5073 
5074 		ispecp->intrspec_vec = sockp->ls_intr_vec;
5075 		ispecp->intrspec_pri = sockp->ls_intr_pri;
5076 		return (ispecp);
5077 	}
5078 
5079 	if (ispecp->intrspec_func != NULL)
5080 		ispecp->intrspec_func = hdlp->ih_cb_func;
5081 
5082 	/* set default IPL then check for override */
5083 	ispecp->intrspec_pri = sockp->ls_intr_pri;
5084 	return (ispecp);
5085 }
5086 
5087 
5088 static int
5089 pcmcia_intr_enable_isr(dev_info_t *dip, dev_info_t *rdip,
5090     ddi_intr_handle_impl_t *hdlp)
5091 {
5092 	int				socket, ret;
5093 	int				irq = 0;	/* default case */
5094 	dev_info_t			*parent = ddi_root_node();
5095 	struct intrspec			*ispecp;
5096 	set_irq_handler_t		handler;
5097 	struct pcmcia_adapter		*adapt;
5098 	pcmcia_logical_socket_t		*sockp;
5099 	struct pcmcia_parent_private	*ppd;
5100 
5101 #if defined(PCMCIA_DEBUG)
5102 	if (pcmcia_debug)
5103 		cmn_err(CE_CONT, "pcmcia_intr_enable_isr: "
5104 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5105 		    (void *)dip, (void *)rdip, (void *)hdlp);
5106 #endif	/* PCMCIA_DEBUG */
5107 
5108 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
5109 	socket = ppd->ppd_socket;
5110 	sockp = pcmcia_sockets[socket];
5111 	adapt = sockp->ls_adapter;
5112 
5113 	ispecp = ppd->ppd_intrspec;
5114 	ASSERT(ispecp);
5115 
5116 	mutex_enter(&sockp->ls_ilock);
5117 	if ((sockp->ls_inthandlers != NULL) &&
5118 	    ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp !=
5119 	    &sockp->ls_intrspec) {
5120 		inthandler_t *intr = sockp->ls_inthandlers;
5121 
5122 		ASSERT(ppd->ppd_flags & PPD_CARD_MULTI);
5123 
5124 		/* Only one handler. So, call ddi_add_intr on it */
5125 		if ((intr->next == intr) && (intr->prev == intr)) {
5126 			hdlp->ih_cb_func = pcmcia_mfc_intr;
5127 			hdlp->ih_cb_arg1 = (caddr_t)sockp;
5128 			hdlp->ih_cb_arg2 = NULL;
5129 
5130 			ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->
5131 			    bus_intr_op))(parent, rdip, DDI_INTROP_ENABLE,
5132 			    hdlp, NULL);
5133 
5134 			if (ret == DDI_FAILURE) {
5135 				sockp->ls_inthandlers = NULL;
5136 				kmem_free(intr, sizeof (inthandler_t));
5137 				sockp->ls_error = BAD_IRQ;
5138 				mutex_exit(&sockp->ls_ilock);
5139 				return (ret);
5140 			}
5141 		}
5142 		mutex_exit(&sockp->ls_ilock);
5143 		hdlp->ih_vector = ispecp->intrspec_vec = sockp->ls_intr_vec;
5144 		hdlp->ih_pri = sockp->ls_intr_pri;
5145 		sockp->ls_iblk = (ddi_iblock_cookie_t)(uintptr_t)
5146 		    sockp->ls_intr_pri;
5147 		sockp->ls_idev.idev_vector = (ushort_t)hdlp->ih_vector;
5148 		sockp->ls_idev.idev_priority = (ushort_t)sockp->ls_intr_pri;
5149 		return (DDI_SUCCESS);
5150 	}
5151 	mutex_exit(&sockp->ls_ilock);
5152 
5153 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
5154 		if (hdlp->ih_cb_func == pcmcia_mfc_intr)
5155 			ispecp = (struct intrspec *)&sockp->ls_intrspec;
5156 
5157 		/* XXX: remove it later as this is done in _add_isr as well */
5158 		ispecp->intrspec_vec = sockp->ls_intr_vec;
5159 		ispecp->intrspec_pri = sockp->ls_intr_pri;
5160 
5161 		/* Enable interrupts */
5162 		ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->bus_intr_op))(
5163 		    parent, rdip, DDI_INTROP_ENABLE, hdlp, NULL);
5164 
5165 		sockp->ls_iblk = (ddi_iblock_cookie_t)(uintptr_t)
5166 		    sockp->ls_intr_pri;
5167 		sockp->ls_idev.idev_vector = (ushort_t)sockp->ls_intr_vec;
5168 		sockp->ls_idev.idev_priority = (ushort_t)sockp->ls_intr_pri;
5169 
5170 		if (ret != DDI_SUCCESS)
5171 			sockp->ls_error = BAD_IRQ;
5172 		return (ret);
5173 	}
5174 
5175 #if defined(PCMCIA_DEBUG)
5176 	if (pcmcia_debug)
5177 		cmn_err(CE_CONT, "pcmcia_intr_enable_isr; let adapter do it\n");
5178 #endif	/* PCMCIA_DEBUG */
5179 
5180 	handler.socket = sockp->ls_socket;
5181 	handler.irq = irq;
5182 	handler.handler = (f_tt *)hdlp->ih_cb_func;
5183 	handler.arg1 = hdlp->ih_cb_arg1;
5184 	handler.arg2 = hdlp->ih_cb_arg2;
5185 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
5186 	if (ispecp->intrspec_func != NULL)
5187 		ispecp->intrspec_func = hdlp->ih_cb_func;
5188 
5189 	/* set default IPL then check for override */
5190 	ispecp->intrspec_pri = sockp->ls_intr_pri;
5191 
5192 	if ((ret = SET_IRQ(sockp->ls_if, adapt->pca_dip, &handler)) !=
5193 	    SUCCESS) {
5194 		sockp->ls_error = ret;
5195 		return (DDI_FAILURE);
5196 	}
5197 	ispecp->intrspec_func = hdlp->ih_cb_func;
5198 	if (!(sockp->ls_flags & PCS_COOKIES_VALID)) {
5199 		sockp->ls_iblk = *handler.iblk_cookie;
5200 		sockp->ls_idev = *handler.idev_cookie;
5201 		sockp->ls_flags |= PCS_COOKIES_VALID;
5202 	}
5203 	return (DDI_SUCCESS);
5204 }
5205 
5206 /* ARGSUSED */
5207 static void
5208 pcmcia_intr_remove_isr(dev_info_t *dip, dev_info_t *rdip,
5209     ddi_intr_handle_impl_t *hdlp)
5210 {
5211 	int				done, remhandler = 0;
5212 	inthandler_t			*intr, *first;
5213 	struct intrspec			*ispecp;
5214 	pcmcia_logical_socket_t		*sockp;
5215 
5216 #if defined(PCMCIA_DEBUG)
5217 	if (pcmcia_debug)
5218 		cmn_err(CE_CONT, "pcmcia_intr_remove_isr: "
5219 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5220 		    (void *)dip, (void *)rdip, (void *)hdlp);
5221 #endif	/* PCMCIA_DEBUG */
5222 
5223 	ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
5224 	ASSERT(ispecp);
5225 
5226 	/* first handle the multifunction case since it is simple */
5227 	mutex_enter(&sockp->ls_ilock);
5228 	if (sockp->ls_inthandlers != NULL &&
5229 	    ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp !=
5230 	    &sockp->ls_intrspec) {
5231 
5232 		intr = sockp->ls_inthandlers;
5233 
5234 		/* Check if there is only one handler left */
5235 		if ((intr->next == intr) && (intr->prev == intr)) {
5236 			if (intr->handler_id == (uint32_t)(uintptr_t)rdip) {
5237 				sockp->ls_inthandlers = NULL;
5238 				remhandler++;
5239 				kmem_free(intr, sizeof (inthandler_t));
5240 			}
5241 
5242 		} else {
5243 			for (done = 0, first = intr; !done; intr = intr->next) {
5244 				if (intr->next == first)
5245 					done++;
5246 				if (intr->handler_id ==
5247 				    (uint32_t)(uintptr_t)rdip) {
5248 					done++;
5249 
5250 					/*
5251 					 * If we're about to remove the handler
5252 					 * at the head of the list, make the
5253 					 * next handler in line the head.
5254 					 */
5255 					if (sockp->ls_inthandlers == intr)
5256 					    sockp->ls_inthandlers = intr->next;
5257 
5258 					remque(intr);
5259 					kmem_free(intr, sizeof (inthandler_t));
5260 					break;
5261 				} /* handler_id */
5262 			} /* end of for */
5263 		} /* end of if intr->next */
5264 
5265 		if (!remhandler) {
5266 			mutex_exit(&sockp->ls_ilock);
5267 			return;
5268 		}
5269 	}
5270 	mutex_exit(&sockp->ls_ilock);
5271 
5272 	if (sockp->ls_adapter->pca_flags & PCA_RES_NEED_IRQ) {
5273 		sockp->ls_intr_vec = 0;
5274 		ispecp->intrspec_vec = 0;
5275 	}
5276 }
5277 
5278 
5279 static void
5280 pcmcia_intr_disable_isr(dev_info_t *dip, dev_info_t *rdip,
5281     ddi_intr_handle_impl_t *hdlp)
5282 {
5283 	int				socket, ret;
5284 	dev_info_t			*parent;
5285 	struct intrspec			*ispecp;
5286 	clear_irq_handler_t		handler;
5287 	struct pcmcia_adapter		*adapt;
5288 	pcmcia_logical_socket_t		*sockp;
5289 	struct pcmcia_parent_private	*ppd;
5290 	ihdl_plat_t			*ihdl_plat_datap =
5291 					    (ihdl_plat_t *)hdlp->ih_private;
5292 
5293 #if defined(PCMCIA_DEBUG)
5294 	if (pcmcia_debug)
5295 		cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
5296 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5297 		    (void *)dip, (void *)rdip, (void *)hdlp);
5298 #endif	/* PCMCIA_DEBUG */
5299 
5300 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
5301 	socket = ppd->ppd_socket;
5302 	sockp = pcmcia_sockets[socket];
5303 	adapt = sockp->ls_adapter;
5304 	ispecp = ppd->ppd_intrspec;
5305 	ASSERT(ispecp);
5306 
5307 	mutex_enter(&sockp->ls_ilock);
5308 	if (sockp->ls_inthandlers != NULL &&
5309 	    ihdl_plat_datap->ip_ispecp != &sockp->ls_intrspec) {
5310 		inthandler_t	*intr = sockp->ls_inthandlers;
5311 
5312 		/* Check if there is only one handler left */
5313 		if ((intr->next == intr) && (intr->prev == intr)) {
5314 			if (intr->handler_id != (uint32_t)(uintptr_t)rdip)
5315 				/*
5316 				 * need to get the dip that was
5317 				 * used to add the handler
5318 				 */
5319 				rdip = sockp->ls_mfintr_dip;
5320 				ispecp = (struct intrspec *)&sockp->ls_intrspec;
5321 		} else {
5322 			/* Don't call cleanup if list still has members */
5323 			mutex_exit(&sockp->ls_ilock);
5324 			return;
5325 		}
5326 	}
5327 	mutex_exit(&sockp->ls_ilock);
5328 
5329 	if (ihdl_plat_datap->ip_ispecp ==
5330 	    (struct intrspec *)&sockp->ls_intrspec)
5331 		ispecp = ihdl_plat_datap->ip_ispecp;
5332 
5333 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
5334 		ret = ispecp->intrspec_vec;
5335 		parent = ddi_root_node();
5336 		ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->bus_intr_op))(
5337 		    parent, rdip, DDI_INTROP_DISABLE, hdlp, NULL);
5338 		(void) pcmcia_return_intr(dip, hdlp->ih_vector);
5339 #if defined(PCMCIA_DEBUG)
5340 		if (pcmcia_debug)
5341 			cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
5342 			    "INTROP_DISABLE returned %x\n", ret);
5343 #endif	/* PCMCIA_DEBUG */
5344 	} else {
5345 		handler.socket = sockp->ls_socket;
5346 		handler.handler_id = (uint32_t)(uintptr_t)rdip;
5347 		handler.handler = (f_tt *)ispecp->intrspec_func;
5348 		ret = CLEAR_IRQ(sockp->ls_if, dip, &handler);
5349 #if defined(PCMCIA_DEBUG)
5350 		if (pcmcia_debug)
5351 			cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
5352 			    "CLEAR_IRQ returned %x\n", ret);
5353 #endif	/* PCMCIA_DEBUG */
5354 	}
5355 }
5356 
5357 /* Consolidated interrupt processing interface */
5358 int
5359 pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5360     ddi_intr_handle_impl_t *hdlp, void *result)
5361 {
5362 	struct intrspec		*ispecp;
5363 	pcmcia_logical_socket_t	*sockp;
5364 
5365 #if defined(PCMCIA_DEBUG)
5366 	if (pcmcia_debug)
5367 		cmn_err(CE_CONT, "pcmcia_intr_ops: "
5368 		    "dip=0x%p rdip=0x%p op=0x%x hdlp=0x%p\n",
5369 		    (void *)dip, (void *)rdip, intr_op, (void *)hdlp);
5370 #endif	/* PCMCIA_DEBUG */
5371 
5372 	switch (intr_op) {
5373 	case DDI_INTROP_SUPPORTED_TYPES:
5374 		if (ddi_get_parent_data(rdip) == NULL) {
5375 			*(int *)result = 0;
5376 			return (DDI_FAILURE);
5377 		}
5378 		*(int *)result = DDI_INTR_TYPE_FIXED;
5379 		break;
5380 	case DDI_INTROP_GETCAP:
5381 		*(int *)result = DDI_INTR_FLAG_LEVEL;
5382 		break;
5383 	case DDI_INTROP_NINTRS:
5384 	case DDI_INTROP_NAVAIL:
5385 		if (i_ddi_get_intx_nintrs(rdip) == 0) {
5386 			*(int *)result = 0;
5387 			return (DDI_FAILURE);
5388 		}
5389 		*(int *)result = 1;	/* for PCMCIA there is only one intr */
5390 		break;
5391 	case DDI_INTROP_ALLOC:
5392 		if ((ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum,
5393 		    &sockp)) == NULL)
5394 			return (DDI_FAILURE);
5395 		*(int *)result = hdlp->ih_scratch1;
5396 		break;
5397 	case DDI_INTROP_FREE:
5398 		break;
5399 	case DDI_INTROP_GETPRI:
5400 		ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
5401 		if (ispecp == NULL) {
5402 			*(int *)result = 0;
5403 			return (DDI_FAILURE);
5404 		}
5405 
5406 		*(int *)result = ispecp->intrspec_pri = sockp->ls_intr_pri;
5407 		break;
5408 	case DDI_INTROP_SETPRI:
5409 		if (*(int *)result > LOCK_LEVEL)
5410 			return (DDI_FAILURE);
5411 		ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
5412 		ASSERT(ispecp);
5413 		ispecp->intrspec_pri = sockp->ls_intr_pri = *(int *)result;
5414 		break;
5415 	case DDI_INTROP_ADDISR:
5416 		if ((ispecp = pcmcia_intr_add_isr(dip, rdip, hdlp)) == NULL)
5417 			return (DDI_FAILURE);
5418 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispecp;
5419 		break;
5420 	case DDI_INTROP_REMISR:
5421 		pcmcia_intr_remove_isr(dip, rdip, hdlp);
5422 		break;
5423 	case DDI_INTROP_ENABLE:
5424 		if (pcmcia_intr_enable_isr(dip, rdip, hdlp) != DDI_SUCCESS)
5425 			return (DDI_FAILURE);
5426 		break;
5427 	case DDI_INTROP_DISABLE:
5428 		pcmcia_intr_disable_isr(dip, rdip, hdlp);
5429 		break;
5430 	default:
5431 		return (DDI_ENOTSUP);
5432 	}
5433 
5434 	return (DDI_SUCCESS);
5435 }
5436 #endif
5437