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

/** @file
 *
 * ISAPnP bus support
 *
 * Etherboot orignally gained ISAPnP support in a very limited way for
 * the 3c515 NIC.  The current implementation is almost a complete
 * rewrite based on the ISAPnP specification, with passing reference
 * to the Linux ISAPnP code.
 *
 * There can be only one ISAPnP bus in a system.  Once the read port
 * is known and all cards have been allocated CSNs, there's nothing to
 * be gained by re-scanning for cards.
 *
 * External code (e.g. the ISAPnP ROM prefix) may already know the
 * read port address, in which case it can store it in
 * #isapnp_read_port.  Note that setting the read port address in this
 * way will prevent further isolation from taking place; you should
 * set the read port address only if you know that devices have
 * already been allocated CSNs.
 *
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <gpxe/io.h>
#include <unistd.h>
#include <gpxe/isapnp.h>

/**
 * ISAPnP Read Port address.
 *
 * ROM prefix may be able to set this address, which is why this is
 * non-static.
 */
uint16_t isapnp_read_port;

static void isapnpbus_remove ( struct root_device *rootdev );

/*
 * ISAPnP utility functions
 *
 */

#define ISAPNP_CARD_ID_FMT "ID %04x:%04x (\"%s\") serial %x"
#define ISAPNP_CARD_ID_DATA(identifier)					  \
	(identifier)->vendor_id, (identifier)->prod_id,			  \
	isa_id_string ( (identifier)->vendor_id, (identifier)->prod_id ), \
	(identifier)->serial
#define ISAPNP_DEV_ID_FMT "ID %04x:%04x (\"%s\")"
#define ISAPNP_DEV_ID_DATA(isapnp)					  \
	(isapnp)->vendor_id, (isapnp)->prod_id,				  \
	isa_id_string ( (isapnp)->vendor_id, (isapnp)->prod_id )

static inline void isapnp_write_address ( unsigned int address ) {
	outb ( address, ISAPNP_ADDRESS );
}

static inline void isapnp_write_data ( unsigned int data ) {
	outb ( data, ISAPNP_WRITE_DATA );
}

static inline unsigned int isapnp_read_data ( void ) {
	return inb ( isapnp_read_port );
}

static inline void isapnp_write_byte ( unsigned int address,
				       unsigned int value ) {
	isapnp_write_address ( address );
	isapnp_write_data ( value );
}

static inline unsigned int isapnp_read_byte ( unsigned int address ) {
	isapnp_write_address ( address );
	return isapnp_read_data ();
}

static inline unsigned int isapnp_read_word ( unsigned int address ) {
	/* Yes, they're in big-endian order */
	return ( ( isapnp_read_byte ( address ) << 8 )
		 | isapnp_read_byte ( address + 1 ) );
}

/** Inform cards of a new read port address */
static inline void isapnp_set_read_port ( void ) {
	isapnp_write_byte ( ISAPNP_READPORT, ( isapnp_read_port >> 2 ) );
}

/**
 * Enter the Isolation state.
 *
 * Only cards currently in the Sleep state will respond to this
 * command.
 */
static inline void isapnp_serialisolation ( void ) {
	isapnp_write_address ( ISAPNP_SERIALISOLATION );
}

/**
 * Enter the Wait for Key state.
 *
 * All cards will respond to this command, regardless of their current
 * state.
 */
static inline void isapnp_wait_for_key ( void ) {
	isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
}

/**
 * Reset (i.e. remove) Card Select Number.
 *
 * Only cards currently in the Sleep state will respond to this
 * command.
 */
static inline void isapnp_reset_csn ( void ) {
	isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
}

/**
 * Place a specified card into the Config state.
 *
 * @v csn		Card Select Number
 * @ret None		-
 * @err None		-
 *
 * Only cards currently in the Sleep, Isolation, or Config states will
 * respond to this command.  The card that has the specified CSN will
 * enter the Config state, all other cards will enter the Sleep state.
 */
static inline void isapnp_wake ( uint8_t csn ) {
	isapnp_write_byte ( ISAPNP_WAKE, csn );
}

static inline unsigned int isapnp_read_resourcedata ( void ) {
	return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
}

static inline unsigned int isapnp_read_status ( void ) {
	return isapnp_read_byte ( ISAPNP_STATUS );
}

/**
 * Assign a Card Select Number to a card, and enter the Config state.
 *
 * @v csn		Card Select Number
 *
 * Only cards in the Isolation state will respond to this command.
 * The isolation protocol is designed so that only one card will
 * remain in the Isolation state by the time the isolation protocol
 * completes.
 */
static inline void isapnp_write_csn ( unsigned int csn ) {
	isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
}

static inline void isapnp_logicaldevice ( unsigned int logdev ) {
	isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
}

static inline void isapnp_activate ( unsigned int logdev ) {
	isapnp_logicaldevice ( logdev );
	isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
}

static inline void isapnp_deactivate ( unsigned int logdev ) {
	isapnp_logicaldevice ( logdev );
	isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
}

static inline unsigned int isapnp_read_iobase ( unsigned int index ) {
	return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
}

static inline unsigned int isapnp_read_irqno ( unsigned int index ) {
	return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
}

static void isapnp_delay ( void ) {
	udelay ( 1000 );
}

/**
 * Linear feedback shift register.
 *
 * @v lfsr		Current value of the LFSR
 * @v input_bit		Current input bit to the LFSR
 * @ret lfsr		Next value of the LFSR
 *
 * This routine implements the linear feedback shift register as
 * described in Appendix B of the PnP ISA spec.  The hardware
 * implementation uses eight D-type latches and two XOR gates.  I
 * think this is probably the smallest possible implementation in
 * software.  Six instructions when input_bit is a constant 0 (for
 * isapnp_send_key).  :)
 */
static inline unsigned int isapnp_lfsr_next ( unsigned int lfsr,
					      unsigned int input_bit ) {
	register uint8_t lfsr_next;

	lfsr_next = lfsr >> 1;
	lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
	return lfsr_next;
}

/**
 * Send the ISAPnP initiation key.
 *
 * Sending the key causes all ISAPnP cards that are currently in the
 * Wait for Key state to transition into the Sleep state.
 */
static void isapnp_send_key ( void ) {
	unsigned int i;
	unsigned int lfsr;

	isapnp_delay();
	isapnp_write_address ( 0x00 );
	isapnp_write_address ( 0x00 );

	lfsr = ISAPNP_LFSR_SEED;
	for ( i = 0 ; i < 32 ; i++ ) {
		isapnp_write_address ( lfsr );
		lfsr = isapnp_lfsr_next ( lfsr, 0 );
	}
}

/**
 * Compute ISAPnP identifier checksum
 *
 * @v identifier	ISAPnP identifier
 * @ret checksum	Expected checksum value
 */
static unsigned int isapnp_checksum ( struct isapnp_identifier *identifier ) {
	unsigned int i, j;
	unsigned int lfsr;
	unsigned int byte;

	lfsr = ISAPNP_LFSR_SEED;
	for ( i = 0 ; i < 8 ; i++ ) {
		byte = * ( ( ( uint8_t * ) identifier ) + i );
		for ( j = 0 ; j < 8 ; j++ ) {
			lfsr = isapnp_lfsr_next ( lfsr, byte );
			byte >>= 1;
		}
	}
	return lfsr;
}

/*
 * Read a byte of resource data from the current location
 *
 * @ret byte		Byte of resource data
 */
static inline unsigned int isapnp_peek_byte ( void ) {
	unsigned int i;

	/* Wait for data to be ready */
	for ( i = 0 ; i < 20 ; i++ ) {
		if ( isapnp_read_status() & 0x01 ) {
			/* Byte ready - read it */
			return isapnp_read_resourcedata();
		}
		isapnp_delay();
	}
	/* Data never became ready - return 0xff */
	return 0xff;
}

/**
 * Read resource data.
 *
 * @v buf		Buffer in which to store data, or NULL
 * @v bytes		Number of bytes to read
 *
 * Resource data is read from the current location.  If #buf is NULL,
 * the data is discarded.
 */
static void isapnp_peek ( void *buf, size_t len ) {
	unsigned int i;
	unsigned int byte;

	for ( i = 0 ; i < len ; i++) {
		byte = isapnp_peek_byte();
		if ( buf )
			* ( ( uint8_t * ) buf + i ) = byte;
	}
}

/**
 * Find a tag within the resource data.
 *
 * @v wanted_tag	The tag that we're looking for
 * @v buf		Buffer in which to store the tag's contents
 * @v len		Length of buffer
 * @ret rc		Return status code
 *
 * Scan through the resource data until we find a particular tag, and
 * read its contents into a buffer.
 */
static int isapnp_find_tag ( unsigned int wanted_tag, void *buf, size_t len ) {
	unsigned int tag;
	unsigned int tag_len;

	DBG2 ( "ISAPnP read tag" );
	do {
		tag = isapnp_peek_byte();
		if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
			tag_len = ISAPNP_SMALL_TAG_LEN ( tag );
			tag = ISAPNP_SMALL_TAG_NAME ( tag );
		} else {
			tag_len = ( isapnp_peek_byte() +
				    ( isapnp_peek_byte() << 8 ) );
			tag = ISAPNP_LARGE_TAG_NAME ( tag );
		}
		DBG2 ( " %02x (%02x)", tag, tag_len );
		if ( tag == wanted_tag ) {
			if ( len > tag_len )
				len = tag_len;
			isapnp_peek ( buf, len );
			DBG2 ( "\n" );
			return 0;
		} else {
			isapnp_peek ( NULL, tag_len );
		}
	} while ( tag != ISAPNP_TAG_END );
	DBG2 ( "\n" );
	return -ENOENT;
}

/**
 * Find specified Logical Device ID tag
 *
 * @v logdev		Logical device ID
 * @v logdevid		Logical device ID structure to fill in
 * @ret rc		Return status code
 */
static int isapnp_find_logdevid ( unsigned int logdev,
				  struct isapnp_logdevid *logdevid ) {
	unsigned int i;
	int rc;

	for ( i = 0 ; i <= logdev ; i++ ) {
		if ( ( rc = isapnp_find_tag ( ISAPNP_TAG_LOGDEVID, logdevid,
					      sizeof ( *logdevid ) ) ) != 0 )
			return rc;
	}
	return 0;
}

/**
 * Try isolating ISAPnP cards at the current read port.
 *
 * @ret \>0		Number of ISAPnP cards found
 * @ret 0		There are no ISAPnP cards in the system
 * @ret \<0		A conflict was detected; try a new read port
 * @err None		-
 *
 * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
 * gives the best overview of what happens here.
 */
static int isapnp_try_isolate ( void ) {
	struct isapnp_identifier identifier;
	unsigned int i, j;
	unsigned int seen_55aa, seen_life;
	unsigned int csn = 0;
	unsigned int data;
	unsigned int byte;

	DBG ( "ISAPnP attempting isolation at read port %04x\n",
	      isapnp_read_port );

	/* Place all cards into the Sleep state, whatever state
	 * they're currently in.
	 */
	isapnp_wait_for_key();
	isapnp_send_key();

	/* Reset all assigned CSNs */
	isapnp_reset_csn();
	isapnp_delay();
	isapnp_delay();
	
	/* Place all cards into the Isolation state */
	isapnp_wait_for_key ();
	isapnp_send_key();
	isapnp_wake ( 0x00 );
	
	/* Set the read port */
	isapnp_set_read_port();
	isapnp_delay();

	while ( 1 ) {

		/* All cards that do not have assigned CSNs are
		 * currently in the Isolation state, each time we go
		 * through this loop.
		 */

		/* Initiate serial isolation */
		isapnp_serialisolation();
		isapnp_delay();

		/* Read identifier serially via the ISAPnP read port. */
		memset ( &identifier, 0, sizeof ( identifier ) );
		seen_55aa = seen_life = 0;
		for ( i = 0 ; i < 9 ; i++ ) {
			byte = 0;
			for ( j = 0 ; j < 8 ; j++ ) {
				data = isapnp_read_data();
				isapnp_delay();
				data = ( ( data << 8 ) | isapnp_read_data() );
				isapnp_delay();
				byte >>= 1;
				if (  data != 0xffff ) {
					seen_life++;
					if ( data == 0x55aa ) {
						byte |= 0x80;
						seen_55aa++;
					}
				}
			}
			*( ( ( uint8_t * ) &identifier ) + i ) = byte;
		}

		/* If we didn't see any 55aa patterns, stop here */
		if ( ! seen_55aa ) {
			if ( csn ) {
				DBG ( "ISAPnP found no more cards\n" );
			} else {
				if ( seen_life ) {
					DBG ( "ISAPnP saw life but no cards, "
					      "trying new read port\n" );
					csn = -1;
				} else {
					DBG ( "ISAPnP saw no signs of life, "
					      "abandoning isolation\n" );
				}
			}
			break;
		}

		/* If the checksum was invalid stop here */
		if ( identifier.checksum != isapnp_checksum ( &identifier) ) {
			DBG ( "ISAPnP found malformed card "
			      ISAPNP_CARD_ID_FMT "\n  with checksum %02x "
			      "(should be %02x), trying new read port\n",
			      ISAPNP_CARD_ID_DATA ( &identifier ),
			      identifier.checksum,
			      isapnp_checksum ( &identifier) );
			csn = -1;
			break;
		}

		/* Give the device a CSN */
		csn++;
		DBG ( "ISAPnP found card " ISAPNP_CARD_ID_FMT
		      ", assigning CSN %02x\n",
		      ISAPNP_CARD_ID_DATA ( &identifier ), csn );
		
		isapnp_write_csn ( csn );
		isapnp_delay();

		/* Send this card back to Sleep and force all cards
		 * without a CSN into Isolation state
		 */
		isapnp_wake ( 0x00 );
		isapnp_delay();
	}

	/* Place all cards in Wait for Key state */
	isapnp_wait_for_key();

	/* Return number of cards found */
	if ( csn > 0 ) {
		DBG ( "ISAPnP found %d cards at read port %04x\n",
		      csn, isapnp_read_port );
	}
	return csn;
}

/**
 * Find a valid read port and isolate all ISAPnP cards.
 *
 */
static void isapnp_isolate ( void ) {
	for ( isapnp_read_port = ISAPNP_READ_PORT_START ;
	      isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
	      isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
		/* Avoid problematic locations such as the NE2000
		 * probe space
		 */
		if ( ( isapnp_read_port >= 0x280 ) &&
		     ( isapnp_read_port <= 0x380 ) )
			continue;
		
		/* If we detect any ISAPnP cards at this location, stop */
		if ( isapnp_try_isolate() >= 0 )
			return;
	}
}

/**
 * Activate or deactivate an ISAPnP device.
 *
 * @v isapnp		ISAPnP device
 * @v activation	True to enable, False to disable the device
 * @ret None		-
 * @err None		-
 *
 * This routine simply activates the device in its current
 * configuration, or deactivates the device.  It does not attempt any
 * kind of resource arbitration.
 *
 */
void isapnp_device_activation ( struct isapnp_device *isapnp,
				int activation ) {
	/* Wake the card and select the logical device */
	isapnp_wait_for_key ();
	isapnp_send_key ();
	isapnp_wake ( isapnp->csn );
	isapnp_logicaldevice ( isapnp->logdev );

	/* Activate/deactivate the logical device */
	isapnp_activate ( activation );
	isapnp_delay();

	/* Return all cards to Wait for Key state */
	isapnp_wait_for_key ();

	DBG ( "ISAPnP %s device %02x:%02x\n",
	      ( activation ? "activated" : "deactivated" ),
	      isapnp->csn, isapnp->logdev );
}

/**
 * Probe an ISAPnP device
 *
 * @v isapnp		ISAPnP device
 * @ret rc		Return status code
 *
 * Searches for a driver for the ISAPnP device.  If a driver is found,
 * its probe() routine is called.
 */
static int isapnp_probe ( struct isapnp_device *isapnp ) {
	struct isapnp_driver *driver;
	struct isapnp_device_id *id;
	unsigned int i;
	int rc;

	DBG ( "Adding ISAPnP device %02x:%02x (%04x:%04x (\"%s\") "
	      "io %x irq %d)\n", isapnp->csn, isapnp->logdev,
	      isapnp->vendor_id, isapnp->prod_id,
	      isa_id_string ( isapnp->vendor_id, isapnp->prod_id ),
	      isapnp->ioaddr, isapnp->irqno );

	for_each_table_entry ( driver, ISAPNP_DRIVERS ) {
		for ( i = 0 ; i < driver->id_count ; i++ ) {
			id = &driver->ids[i];
			if ( id->vendor_id != isapnp->vendor_id )
				continue;
			if ( ISA_PROD_ID ( id->prod_id ) !=
			     ISA_PROD_ID ( isapnp->prod_id ) )
				continue;
			isapnp->driver = driver;
			isapnp->driver_name = id->name;
			DBG ( "...using driver %s\n", isapnp->driver_name );
			if ( ( rc = driver->probe ( isapnp, id ) ) != 0 ) {
				DBG ( "......probe failed\n" );
				continue;
			}
			return 0;
		}
	}

	DBG ( "...no driver found\n" );
	return -ENOTTY;
}

/**
 * Remove an ISAPnP device
 *
 * @v isapnp		ISAPnP device
 */
static void isapnp_remove ( struct isapnp_device *isapnp ) {
	isapnp->driver->remove ( isapnp );
	DBG ( "Removed ISAPnP device %02x:%02x\n",
	      isapnp->csn, isapnp->logdev );
}

/**
 * Probe ISAPnP root bus
 *
 * @v rootdev		ISAPnP bus root device
 *
 * Scans the ISAPnP bus for devices and registers all devices it can
 * find.
 */
static int isapnpbus_probe ( struct root_device *rootdev ) {
	struct isapnp_device *isapnp = NULL;
	struct isapnp_identifier identifier;
	struct isapnp_logdevid logdevid;
	unsigned int csn;
	unsigned int logdev;
	int rc;

	/* Perform isolation if it hasn't yet been done */
	if ( ! isapnp_read_port )
		isapnp_isolate();

	for ( csn = 1 ; csn <= 0xff ; csn++ ) {
		for ( logdev = 0 ; logdev <= 0xff ; logdev++ ) {

			/* Allocate struct isapnp_device */
			if ( ! isapnp )
				isapnp = malloc ( sizeof ( *isapnp ) );
			if ( ! isapnp ) {
				rc = -ENOMEM;
				goto err;
			}
			memset ( isapnp, 0, sizeof ( *isapnp ) );
			isapnp->csn = csn;
			isapnp->logdev = logdev;

			/* Wake the card */
			isapnp_wait_for_key();
			isapnp_send_key();
			isapnp_wake ( csn );

			/* Read the card identifier */
			isapnp_peek ( &identifier, sizeof ( identifier ) );
			
			/* No card with this CSN; stop here */
			if ( identifier.vendor_id & 0x80 )
				goto done;

			/* Find the Logical Device ID tag */
			if ( ( rc = isapnp_find_logdevid ( logdev,
							   &logdevid ) ) != 0){
				/* No more logical devices; go to next CSN */
				break;
			}
			
			/* Select the logical device */
			isapnp_logicaldevice ( logdev );

			/* Populate struct isapnp_device */
			isapnp->vendor_id = logdevid.vendor_id;
			isapnp->prod_id = logdevid.prod_id;
			isapnp->ioaddr = isapnp_read_iobase ( 0 );
			isapnp->irqno = isapnp_read_irqno ( 0 );

			/* Return all cards to Wait for Key state */
			isapnp_wait_for_key();

			/* Add to device hierarchy */
			snprintf ( isapnp->dev.name,
				   sizeof ( isapnp->dev.name ),
				   "ISAPnP%02x:%02x", csn, logdev );
			isapnp->dev.desc.bus_type = BUS_TYPE_ISAPNP;
			isapnp->dev.desc.vendor = isapnp->vendor_id;
			isapnp->dev.desc.device = isapnp->prod_id;
			isapnp->dev.desc.ioaddr = isapnp->ioaddr;
			isapnp->dev.desc.irq = isapnp->irqno;
			isapnp->dev.parent = &rootdev->dev;
			list_add ( &isapnp->dev.siblings,
				   &rootdev->dev.children );
			INIT_LIST_HEAD ( &isapnp->dev.children );
			
			/* Look for a driver */
			if ( isapnp_probe ( isapnp ) == 0 ) {
				/* isapnpdev registered, we can drop our ref */
				isapnp = NULL;
			} else {
				/* Not registered; re-use struct */
				list_del ( &isapnp->dev.siblings );
			}
		}
	}

 done:
	free ( isapnp );
	return 0;

 err:
	free ( isapnp );
	isapnpbus_remove ( rootdev );
	return rc;
}

/**
 * Remove ISAPnP root bus
 *
 * @v rootdev		ISAPnP bus root device
 */
static void isapnpbus_remove ( struct root_device *rootdev ) {
	struct isapnp_device *isapnp;
	struct isapnp_device *tmp;

	list_for_each_entry_safe ( isapnp, tmp, &rootdev->dev.children,
				   dev.siblings ) {
		isapnp_remove ( isapnp );
		list_del ( &isapnp->dev.siblings );
		free ( isapnp );
	}
}

/** ISAPnP bus root device driver */
static struct root_driver isapnp_root_driver = {
	.probe = isapnpbus_probe,
	.remove = isapnpbus_remove,
};

/** ISAPnP bus root device */
struct root_device isapnp_root_device __root_device = {
	.dev = { .name = "ISAPnP" },
	.driver = &isapnp_root_driver,
};