summaryrefslogtreecommitdiffstats
path: root/sys/pccard/pccard.c
blob: 16f3a5117891a75c15d51994fc39a99cf7d3f8ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
 *	pccard.c - Interface code for PC-CARD controllers.
 *
 *	June 1995, Andrew McRae (andrew@mega.com.au)
 *-------------------------------------------------------------------------
 *
 * Copyright (c) 2001 M. Warner Losh.  All rights reserved.
 * Copyright (c) 1995 Andrew McRae.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#define OBSOLETE_IN_6

#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/sysctl.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/poll.h>
#include <sys/bus.h>
#include <sys/proc.h>
#include <machine/bus.h>

#include <pccard/cardinfo.h>
#include <pccard/driver.h>
#include <pccard/slot.h>
#include <pccard/pccard_nbk.h>

#include <machine/md_var.h>

static int		allocate_driver(struct slot *, struct dev_desc *);
static void		inserted(void *);
static void		disable_slot(struct slot *);
static void		disable_slot_to(struct slot *);
static void		power_off_slot(void *);

/*
 *	The driver interface for read/write uses a block
 *	of memory in the ISA I/O memory space allocated via
 *	an ioctl setting.
 *
 *	Now that we have different bus attachments, we should really
 *	use a better algorythm to allocate memory.
 */
static unsigned long pccard_mem;	/* Physical memory */
static unsigned char *pccard_kmem;	/* Kernel virtual address */
static struct resource *pccard_mem_res;
static int pccard_mem_rid;

static	d_open_t	crdopen;
static	d_close_t	crdclose;
static	d_read_t	crdread;
static	d_write_t	crdwrite;
static	d_ioctl_t	crdioctl;
static	d_poll_t	crdpoll;

#if __FreeBSD_version < 500000
#define CDEV_MAJOR 50
#else
#define CDEV_MAJOR MAJOR_AUTO
#endif
static struct cdevsw crd_cdevsw = {
	.d_open =	crdopen,
	.d_close =	crdclose,
	.d_read =	crdread,
	.d_write =	crdwrite,
	.d_ioctl =	crdioctl,
	.d_poll =	crdpoll,
	.d_name =	"crd",
	.d_maj =	CDEV_MAJOR,
};

/*
 *	Power off the slot.
 *	(doing it immediately makes the removal of some cards unstable)
 */
static void
power_off_slot(void *arg)
{
	struct slot *slt = (struct slot *)arg;
	int s;

	/*
	 * The following will generate an interrupt.  So, to hold off
	 * the interrupt unitl after disable runs so that we can get rid
	 * rid of the interrupt before it becomes unsafe to touch the
	 * device.
	 *
	 * XXX In current, the spl stuff is a nop.
	 */
	s = splhigh();
	/* Power off the slot. */
	slt->pwr_off_pending = 0;
	slt->ctrl->disable(slt);
	splx(s);
}

/*
 *	disable_slot - Disables the slot by removing
 *	the power and unmapping the I/O
 */
static void
disable_slot(struct slot *slt)
{
	device_t pccarddev;
	device_t *kids;
	int nkids;
	int i;
	int ret;

	/*
	 * Note that a race condition is possible here; if a
	 * driver is accessing the device and it is removed, then
	 * all bets are off...
	 */
	pccarddev = slt->dev;
	device_get_children(pccarddev, &kids, &nkids);
	for (i = 0; i < nkids; i++) {
		if ((ret = device_delete_child(pccarddev, kids[i])) != 0)
			printf("pccard: delete of %s failed: %d\n",
				device_get_nameunit(kids[i]), ret);
 	}
	free(kids, M_TEMP);

	/* Power off the slot 1/2 second after removal of the card */
	slt->poff_ch = timeout(power_off_slot, (caddr_t)slt, hz / 2);
	slt->pwr_off_pending = 1;
}

static void
disable_slot_to(struct slot *slt)
{
	disable_slot(slt);
	if (slt->state == empty)
		printf("pccard: card removed, slot %d\n", slt->slotnum);
	else
		printf("pccard: card deactivated, slot %d\n", slt->slotnum);
	pccard_remove_beep();
	selwakeup(&slt->selp);
}

/*
 *	pccard_init_slot - Initialize the slot controller and attach various
 * things to it.  We also make the device for it.  We create the device that
 * will be exported to devfs.
 */
struct slot *
pccard_init_slot(device_t dev, struct slot_ctrl *ctrl)
{
	int		slotno;
	struct slot	*slt;

	slt = PCCARD_DEVICE2SOFTC(dev);
	slotno = device_get_unit(dev);
	slt->dev = dev;
	slt->d = make_dev(&crd_cdevsw, slotno, 0, 0, 0600, "card%d", slotno);
	slt->d->si_drv1 = slt;
	slt->ctrl = ctrl;
	slt->slotnum = slotno;
	callout_handle_init(&slt->insert_ch);
	callout_handle_init(&slt->poff_ch);

	return (slt);
}

/*
 *	allocate_driver - Create a new device entry for this
 *	slot, and attach a driver to it.
 */
static int
allocate_driver(struct slot *slt, struct dev_desc *desc)
{
	struct pccard_devinfo *devi;
	device_t pccarddev;
	int err, irq = 0;
	device_t child;
	device_t *devs;
	int count;

	pccarddev = slt->dev;
	err = device_get_children(pccarddev, &devs, &count);
	if (err != 0)
		return (err);
	free(devs, M_TEMP);
	if (count) {
		device_printf(pccarddev,
		    "Can not attach more than one child.\n");
		return (EIO);
	}
	irq = ffs(desc->irqmask) - 1;
	MALLOC(devi, struct pccard_devinfo *, sizeof(*devi), M_DEVBUF,
	    M_WAITOK | M_ZERO);
	strcpy(devi->name, desc->name);
	/*
	 *	Create an entry for the device under this slot.
	 */
	devi->running = 1;
	devi->slt = slt;
	bcopy(desc->misc, devi->misc, sizeof(desc->misc));
	strcpy(devi->manufstr, desc->manufstr);
	strcpy(devi->versstr, desc->versstr);
	strcpy(devi->cis3str, desc->cis3str);
	strcpy(devi->cis4str, desc->cis4str);
	devi->manufacturer = desc->manufacturer;
	devi->product = desc->product;
	devi->prodext = desc->prodext;
	resource_list_init(&devi->resources);
	child = device_add_child(pccarddev, devi->name, desc->unit);
	if (child == NULL) {
		if (desc->unit != -1)
			device_printf(pccarddev,
			    "Unit %d failed for %s, try a different unit\n",
			    desc->unit, devi->name);
		else
			device_printf(pccarddev,
			    "No units available for %s.  Impossible?\n",
			    devi->name);
		return (EIO);
	}
	device_set_flags(child, desc->flags);
	device_set_ivars(child, devi);
	if (bootverbose) {
		device_printf(pccarddev, "Assigning %s:",
		    device_get_nameunit(child));
		if (desc->iobase)
			printf(" io 0x%x-0x%x",
			    desc->iobase, desc->iobase + desc->iosize - 1);
		if (irq)
			printf(" irq %d", irq);
		if (desc->mem)
			printf(" mem 0x%lx-0x%lx", desc->mem,
			    desc->mem + desc->memsize - 1);
		printf(" flags 0x%x\n", desc->flags);
	}
	err = bus_set_resource(child, SYS_RES_IOPORT, 0, desc->iobase,
	    desc->iosize);
	if (err)
		goto err;
	if (irq)
		err = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
	if (err)
		goto err;
	if (desc->memsize) {
		err = bus_set_resource(child, SYS_RES_MEMORY, 0, desc->mem,
		    desc->memsize);
		if (err)
			goto err;
	}
	err = device_probe_and_attach(child);
	/*
	 * XXX We unwisely assume that the detach code won't run while the
	 * XXX the attach code is attaching.  Someone should put some
	 * XXX interlock code.  This can happen if probe/attach takes a while
	 * XXX and the user ejects the card, which causes the detach
	 * XXX function to be called.
	 */
	strncpy(desc->name, device_get_nameunit(child), sizeof(desc->name));
	desc->name[sizeof(desc->name) - 1] = '\0';
err:
	if (err)
		device_delete_child(pccarddev, child);
	return (err);
}

/*
 *	card insert routine - Called from a timeout to debounce
 *	insertion events.
 */
static void
inserted(void *arg)
{
	struct slot *slt = arg;

	slt->state = filled;
	/*
	 * Disable any pending timeouts for this slot, and explicitly
	 * power it off right now.  Then, re-enable the power using
	 * the (possibly new) power settings.
	 */
	untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
	power_off_slot(slt);

	/*
	 *	Enable 5V to the card so that the CIS can be read.  Well,
	 * enable the most natural voltage so that the CIS can be read.
	 */
	slt->pwr.vcc = -1;
	slt->pwr.vpp = -1;
	slt->ctrl->power(slt);

	printf("pccard: card inserted, slot %d\n", slt->slotnum);
	pccard_insert_beep();
	slt->ctrl->reset(slt);
}

/*
 *	Card event callback. Called at splhigh to prevent
 *	device interrupts from interceding.
 */
void
pccard_event(struct slot *slt, enum card_event event)
{
	if (slt->insert_seq) {
		slt->insert_seq = 0;
		untimeout(inserted, (void *)slt, slt->insert_ch);
	}

	switch(event) {
	case card_removed:
	case card_deactivated:
		if (slt->state == filled || slt->state == inactive) {
			if (event == card_removed)
				slt->state = empty;
			else
				slt->state = inactive;
			disable_slot_to(slt);
		}
		break;
	case card_inserted:
		slt->insert_seq = 1;
		slt->insert_ch = timeout(inserted, (void *)slt, hz/4);
		break;
	}
}

/*
 *	Device driver interface.
 */
static	int
crdopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
{
	struct slot *slt = PCCARD_DEV2SOFTC(dev);

	if (slt == NULL)
		return (ENXIO);
	if (slt->rwmem == 0)
		slt->rwmem = MDF_ATTR;
	return (0);
}

/*
 *	Close doesn't de-allocate any resources, since
 *	slots may be assigned to drivers already.
 */
static	int
crdclose(dev_t dev, int fflag, int devtype, d_thread_t *td)
{
	return (0);
}

/*
 *	read interface. Map memory at lseek offset,
 *	then transfer to user space.
 */
static	int
crdread(dev_t dev, struct uio *uio, int ioflag)
{
	struct slot *slt = PCCARD_DEV2SOFTC(dev);
	struct mem_desc *mp, oldmap;
	unsigned char *p;
	unsigned int offs;
	int error = 0, win, count;

	if (slt == 0 || slt->state != filled)
		return (ENXIO);
	if (pccard_mem == 0)
		return (ENOMEM);
	for (win = slt->ctrl->maxmem - 1; win >= 0; win--)
		if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
			break;
	if (win < 0)
		return (EBUSY);
	mp = &slt->mem[win];
	oldmap = *mp;
	mp->flags = slt->rwmem | MDF_ACTIVE;
	while (uio->uio_resid && error == 0) {
		mp->card = uio->uio_offset;
		mp->size = PCCARD_MEMSIZE;
		mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
		if ((error = slt->ctrl->mapmem(slt, win)) != 0)
			break;
		offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
		p = pccard_kmem + offs;
		count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
		error = uiomove(p, count, uio);
	}
	/*
	 *	Restore original map.
	 */
	*mp = oldmap;
	slt->ctrl->mapmem(slt, win);

	return (error);
}

/*
 *	crdwrite - Write data to card memory.
 *	Handles wrap around so that only one memory
 *	window is used.
 */
static	int
crdwrite(dev_t dev, struct uio *uio, int ioflag)
{
	struct slot *slt = PCCARD_DEV2SOFTC(dev);
	struct mem_desc *mp, oldmap;
	unsigned char *p;
	unsigned int offs;
	int error = 0, win, count;

	if (slt == 0 || slt->state != filled)
		return (ENXIO);
	if (pccard_mem == 0)
		return (ENOMEM);
	for (win = slt->ctrl->maxmem - 1; win >= 0; win--)
		if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
			break;
	if (win < 0)
		return (EBUSY);
	mp = &slt->mem[win];
	oldmap = *mp;
	mp->flags = slt->rwmem | MDF_ACTIVE;
	while (uio->uio_resid && error == 0) {
		mp->card = uio->uio_offset;
		mp->size = PCCARD_MEMSIZE;
		mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
		if ((error = slt->ctrl->mapmem(slt, win)) != 0)
			break;
		offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
		p = pccard_kmem + offs;
		count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
		error = uiomove(p, count, uio);
	}
	/*
	 *	Restore original map.
	 */
	*mp = oldmap;
	slt->ctrl->mapmem(slt, win);

	return (error);
}

/*
 *	ioctl calls - allows setting/getting of memory and I/O
 *	descriptors, and assignment of drivers.
 */
static	int
crdioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
{
	u_int32_t	addr;
	int		err;
	struct io_desc	*ip;
	struct mem_desc *mp;
	device_t	pccarddev;
	int		pwval;
	int		s;
	struct slot	*slt = PCCARD_DEV2SOFTC(dev);
/*XXX*/#if __FreeBSD_version < 5000000		/* 4.x compatibility only. */
	struct dev_desc d;
	struct dev_desc_old *odp;
/*XXX*/#endif

	if (slt == 0 && cmd != PIOCRWMEM)
		return (ENXIO);
	switch(cmd) {
	default:
		if (slt->ctrl->ioctl)
			return (slt->ctrl->ioctl(slt, cmd, data));
		return (ENOTTY);
	/*
	 * Get slot state.
	 */
	case PIOCGSTATE:
		s = splhigh();
		((struct slotstate *)data)->state = slt->state;
		((struct slotstate *)data)->laststate = slt->laststate;
		slt->laststate = slt->state;
		splx(s);
		((struct slotstate *)data)->maxmem = slt->ctrl->maxmem;
		((struct slotstate *)data)->maxio = slt->ctrl->maxio;
		((struct slotstate *)data)->irqs = 0;
		break;
	/*
	 * Get memory context.
	 */
	case PIOCGMEM:
		s = ((struct mem_desc *)data)->window;
		if (s < 0 || s >= slt->ctrl->maxmem)
			return (EINVAL);
		mp = &slt->mem[s];
		((struct mem_desc *)data)->flags = mp->flags;
		((struct mem_desc *)data)->start = mp->start;
		((struct mem_desc *)data)->size = mp->size;
		((struct mem_desc *)data)->card = mp->card;
		break;
	/*
	 * Set memory context. If context already active, then unmap it.
	 * It is hard to see how the parameters can be checked.
	 * At the very least, we only allow root to set the context.
	 */
	case PIOCSMEM:
		if (suser(td))
			return (EPERM);
		if (slt->state != filled)
			return (ENXIO);
		s = ((struct mem_desc *)data)->window;
		if (s < 0 || s >= slt->ctrl->maxmem)
			return (EINVAL);
		slt->mem[s] = *((struct mem_desc *)data);
		return (slt->ctrl->mapmem(slt, s));
	/*
	 * Get I/O port context.
	 */
	case PIOCGIO:
		s = ((struct io_desc *)data)->window;
		if (s < 0 || s >= slt->ctrl->maxio)
			return (EINVAL);
		ip = &slt->io[s];
		((struct io_desc *)data)->flags = ip->flags;
		((struct io_desc *)data)->start = ip->start;
		((struct io_desc *)data)->size = ip->size;
		break;
	/*
	 * Set I/O port context.
	 */
	case PIOCSIO:
		if (suser(td))
			return (EPERM);
		if (slt->state != filled)
			return (ENXIO);
		s = ((struct io_desc *)data)->window;
		if (s < 0 || s >= slt->ctrl->maxio)
			return (EINVAL);
		slt->io[s] = *((struct io_desc *)data);
		/* XXX Don't actually map */
		return (0);
		break;
	/*
	 * Set memory window flags for read/write interface.
	 */
	case PIOCRWFLAG:
		slt->rwmem = *(int *)data;
		break;
	/*
	 * Set the memory window to be used for the read/write interface.
	 */
	case PIOCRWMEM:
		if (*(unsigned long *)data == 0) {
			*(unsigned long *)data = pccard_mem;
			break;
		}
		if (suser(td))
			return (EPERM);
		/*
		 * Validate the memory by checking it against the I/O
		 * memory range. It must also start on an aligned block size.
		 */
		if (*(unsigned long *)data & (PCCARD_MEMSIZE-1))
			return (EINVAL);
		pccarddev = PCCARD_DEV2SOFTC(dev)->dev;
		pccard_mem_rid = 0;
		addr = *(unsigned long *)data;
		if (pccard_mem_res)
			bus_release_resource(pccarddev, SYS_RES_MEMORY,
			    pccard_mem_rid, pccard_mem_res);
		pccard_mem_res = bus_alloc_resource(pccarddev, SYS_RES_MEMORY,
		    &pccard_mem_rid, addr, addr, PCCARD_MEMSIZE,
		    RF_ACTIVE | rman_make_alignment_flags(PCCARD_MEMSIZE));
		if (pccard_mem_res == NULL)
			return (EINVAL);
		pccard_mem = rman_get_start(pccard_mem_res);
		pccard_kmem = rman_get_virtual(pccard_mem_res);
		break;
	/*
	 * Set power values.
	 */
	case PIOCSPOW:
		slt->pwr = *(struct power *)data;
		return (slt->ctrl->power(slt));
	/*
	 * Allocate a driver to this slot.
	 */
	case PIOCSDRV:
		if (suser(td))
			return (EPERM);
		err = allocate_driver(slt, (struct dev_desc *)data);
		if (!err)
			pccard_success_beep();
		else
			pccard_failure_beep();
		return (err);
/***/#if __FreeBSD_version < 5000000		/* 4.x compatibility only. */
	case PIOCSDRVOLD:
		if (suser(td))
			return (EPERM);
		odp = (struct dev_desc_old *) data;
		strlcpy(d.name, odp->name, sizeof(d.name));
		d.unit = odp->unit;
		d.mem = odp->mem;
		d.memsize = odp->memsize;
		d.iobase = odp->iobase;
		d.iosize = odp->iosize;
		d.irqmask = odp->irqmask;
		d.flags = odp->flags;
		memcpy(d.misc, odp->misc, sizeof(odp->misc));
		strlcpy(d.manufstr, odp->manufstr, sizeof(d.manufstr));
		strlcpy(d.versstr, odp->versstr, sizeof(d.versstr));
		*d.cis3str = '\0';
		*d.cis4str = '\0';
		d.manufacturer = odp->manufacturer;
		d.product = odp->product;
		d.prodext = odp->prodext;
		err = allocate_driver(slt, &d);
		if (!err)
			pccard_success_beep();
		else
			pccard_failure_beep();
		return (err);
/***/#endif
	/*
	 * Virtual removal/insertion
	 */
	case PIOCSVIR:
		pwval = *(int *)data;
		if (!pwval) {
			if (slt->state != filled)
				return (EINVAL);
			pccard_event(slt, card_deactivated);
		} else {
			if (slt->state != empty && slt->state != inactive)
				return (EINVAL);
			pccard_event(slt, card_inserted);
		}
		break;
	case PIOCSBEEP:
		if (pccard_beep_select(*(int *)data)) {
			return (EINVAL);
		}
		break;
	}
	return (0);
}

/*
 *	poll - Poll on exceptions will return true
 *	when a change in card status occurs.
 */
static	int
crdpoll(dev_t dev, int events, d_thread_t *td)
{
	int	revents = 0;
	int	s;
	struct slot *slt = PCCARD_DEV2SOFTC(dev);

	if (events & (POLLIN | POLLRDNORM))
		revents |= events & (POLLIN | POLLRDNORM);

	if (events & (POLLOUT | POLLWRNORM))
		revents |= events & (POLLIN | POLLRDNORM);

	s = splhigh();
	/*
	 *	select for exception - card event.
	 */
	if (events & POLLRDBAND)
		if (slt == 0 || slt->laststate != slt->state)
			revents |= POLLRDBAND;

	if (revents == 0)
		selrecord(td, &slt->selp);

	splx(s);
	return (revents);
}

/*
 *	APM hooks for suspending and resuming.
 */
int
pccard_suspend(device_t dev)
{
	struct slot *slt = PCCARD_DEVICE2SOFTC(dev);

	/* This code stolen from pccard_event:card_removed */
	if (slt->state == filled) {
		int s = splhigh();		/* nop on current */
		disable_slot(slt);
		slt->laststate = suspend;	/* for pccardd */
		slt->state = empty;
		splx(s);
		printf("pccard: card disabled, slot %d\n", slt->slotnum);
	}
	/*
	 * Disable any pending timeouts for this slot since we're
	 * powering it down/disabling now.
	 */
	untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
	slt->ctrl->disable(slt);
	return (0);
}

int
pccard_resume(device_t dev)
{
	struct slot *slt = PCCARD_DEVICE2SOFTC(dev);

	slt->ctrl->resume(slt);
	return (0);
}
OpenPOWER on IntegriCloud