1 /**************************************************************************
2 *
3 *    isapnp.c -- Etherboot isapnp support for the 3Com 3c515
4 *    Written 2002-2003 by Timothy Legge <tlegge@rogers.com>
5 *
6 *    This program is free software; you can redistribute it and/or modify
7 *    it under the terms of the GNU General Public License as published by
8 *    the Free Software Foundation; either version 2 of the License, or
9 *    (at your option) any later version.
10 *
11 *    This program is distributed in the hope that it will be useful,
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *    GNU General Public License for more details.
15 *
16 *    You should have received a copy of the GNU General Public License
17 *    along with this program; if not, write to the Free Software
18 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 *    02110-1301, USA.
20 *
21 *    Portions of this code:
22 *	Copyright (C) 2001  P.J.H.Fox (fox@roestock.demon.co.uk)
23 *
24 *
25 *    REVISION HISTORY:
26 *    ================
27 *    Version 0.1 April 26, 2002 TJL
28 *    Version 0.2 01/08/2003	TJL Moved outside the 3c515.c driver file
29 *    Version 0.3 Sept 23, 2003	timlegge Change delay to currticks
30 *
31 *
32 *    Generalised into an ISAPnP bus that can be used by more than just
33 *    the 3c515 by Michael Brown <mbrown@fensystems.co.uk>
34 *
35 ***************************************************************************/
36 
37 /** @file
38  *
39  * ISAPnP bus support
40  *
41  * Etherboot orignally gained ISAPnP support in a very limited way for
42  * the 3c515 NIC.  The current implementation is almost a complete
43  * rewrite based on the ISAPnP specification, with passing reference
44  * to the Linux ISAPnP code.
45  *
46  * There can be only one ISAPnP bus in a system.  Once the read port
47  * is known and all cards have been allocated CSNs, there's nothing to
48  * be gained by re-scanning for cards.
49  *
50  * External code (e.g. the ISAPnP ROM prefix) may already know the
51  * read port address, in which case it can store it in
52  * #isapnp_read_port.  Note that setting the read port address in this
53  * way will prevent further isolation from taking place; you should
54  * set the read port address only if you know that devices have
55  * already been allocated CSNs.
56  *
57  */
58 
59 FILE_LICENCE ( GPL2_OR_LATER );
60 
61 #include <stdint.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <stdio.h>
65 #include <errno.h>
66 #include <ipxe/io.h>
67 #include <unistd.h>
68 #include <ipxe/isapnp.h>
69 
70 /**
71  * ISAPnP Read Port address.
72  *
73  * ROM prefix may be able to set this address, which is why this is
74  * non-static.
75  */
76 uint16_t isapnp_read_port;
77 
78 static void isapnpbus_remove ( struct root_device *rootdev );
79 
80 /*
81  * ISAPnP utility functions
82  *
83  */
84 
85 #define ISAPNP_CARD_ID_FMT "ID %04x:%04x (\"%s\") serial %x"
86 #define ISAPNP_CARD_ID_DATA(identifier)					  \
87 	(identifier)->vendor_id, (identifier)->prod_id,			  \
88 	isa_id_string ( (identifier)->vendor_id, (identifier)->prod_id ), \
89 	(identifier)->serial
90 #define ISAPNP_DEV_ID_FMT "ID %04x:%04x (\"%s\")"
91 #define ISAPNP_DEV_ID_DATA(isapnp)					  \
92 	(isapnp)->vendor_id, (isapnp)->prod_id,				  \
93 	isa_id_string ( (isapnp)->vendor_id, (isapnp)->prod_id )
94 
isapnp_write_address(unsigned int address)95 static inline void isapnp_write_address ( unsigned int address ) {
96 	outb ( address, ISAPNP_ADDRESS );
97 }
98 
isapnp_write_data(unsigned int data)99 static inline void isapnp_write_data ( unsigned int data ) {
100 	outb ( data, ISAPNP_WRITE_DATA );
101 }
102 
isapnp_read_data(void)103 static inline unsigned int isapnp_read_data ( void ) {
104 	return inb ( isapnp_read_port );
105 }
106 
isapnp_write_byte(unsigned int address,unsigned int value)107 static inline void isapnp_write_byte ( unsigned int address,
108 				       unsigned int value ) {
109 	isapnp_write_address ( address );
110 	isapnp_write_data ( value );
111 }
112 
isapnp_read_byte(unsigned int address)113 static inline unsigned int isapnp_read_byte ( unsigned int address ) {
114 	isapnp_write_address ( address );
115 	return isapnp_read_data ();
116 }
117 
isapnp_read_word(unsigned int address)118 static inline unsigned int isapnp_read_word ( unsigned int address ) {
119 	/* Yes, they're in big-endian order */
120 	return ( ( isapnp_read_byte ( address ) << 8 )
121 		 | isapnp_read_byte ( address + 1 ) );
122 }
123 
124 /** Inform cards of a new read port address */
isapnp_set_read_port(void)125 static inline void isapnp_set_read_port ( void ) {
126 	isapnp_write_byte ( ISAPNP_READPORT, ( isapnp_read_port >> 2 ) );
127 }
128 
129 /**
130  * Enter the Isolation state.
131  *
132  * Only cards currently in the Sleep state will respond to this
133  * command.
134  */
isapnp_serialisolation(void)135 static inline void isapnp_serialisolation ( void ) {
136 	isapnp_write_address ( ISAPNP_SERIALISOLATION );
137 }
138 
139 /**
140  * Enter the Wait for Key state.
141  *
142  * All cards will respond to this command, regardless of their current
143  * state.
144  */
isapnp_wait_for_key(void)145 static inline void isapnp_wait_for_key ( void ) {
146 	isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
147 }
148 
149 /**
150  * Reset (i.e. remove) Card Select Number.
151  *
152  * Only cards currently in the Sleep state will respond to this
153  * command.
154  */
isapnp_reset_csn(void)155 static inline void isapnp_reset_csn ( void ) {
156 	isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
157 }
158 
159 /**
160  * Place a specified card into the Config state.
161  *
162  * @v csn		Card Select Number
163  * @ret None		-
164  * @err None		-
165  *
166  * Only cards currently in the Sleep, Isolation, or Config states will
167  * respond to this command.  The card that has the specified CSN will
168  * enter the Config state, all other cards will enter the Sleep state.
169  */
isapnp_wake(uint8_t csn)170 static inline void isapnp_wake ( uint8_t csn ) {
171 	isapnp_write_byte ( ISAPNP_WAKE, csn );
172 }
173 
isapnp_read_resourcedata(void)174 static inline unsigned int isapnp_read_resourcedata ( void ) {
175 	return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
176 }
177 
isapnp_read_status(void)178 static inline unsigned int isapnp_read_status ( void ) {
179 	return isapnp_read_byte ( ISAPNP_STATUS );
180 }
181 
182 /**
183  * Assign a Card Select Number to a card, and enter the Config state.
184  *
185  * @v csn		Card Select Number
186  *
187  * Only cards in the Isolation state will respond to this command.
188  * The isolation protocol is designed so that only one card will
189  * remain in the Isolation state by the time the isolation protocol
190  * completes.
191  */
isapnp_write_csn(unsigned int csn)192 static inline void isapnp_write_csn ( unsigned int csn ) {
193 	isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
194 }
195 
isapnp_logicaldevice(unsigned int logdev)196 static inline void isapnp_logicaldevice ( unsigned int logdev ) {
197 	isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
198 }
199 
isapnp_activate(unsigned int logdev)200 static inline void isapnp_activate ( unsigned int logdev ) {
201 	isapnp_logicaldevice ( logdev );
202 	isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
203 }
204 
isapnp_deactivate(unsigned int logdev)205 static inline void isapnp_deactivate ( unsigned int logdev ) {
206 	isapnp_logicaldevice ( logdev );
207 	isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
208 }
209 
isapnp_read_iobase(unsigned int index)210 static inline unsigned int isapnp_read_iobase ( unsigned int index ) {
211 	return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
212 }
213 
isapnp_read_irqno(unsigned int index)214 static inline unsigned int isapnp_read_irqno ( unsigned int index ) {
215 	return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
216 }
217 
isapnp_delay(void)218 static void isapnp_delay ( void ) {
219 	udelay ( 1000 );
220 }
221 
222 /**
223  * Linear feedback shift register.
224  *
225  * @v lfsr		Current value of the LFSR
226  * @v input_bit		Current input bit to the LFSR
227  * @ret lfsr		Next value of the LFSR
228  *
229  * This routine implements the linear feedback shift register as
230  * described in Appendix B of the PnP ISA spec.  The hardware
231  * implementation uses eight D-type latches and two XOR gates.  I
232  * think this is probably the smallest possible implementation in
233  * software.  Six instructions when input_bit is a constant 0 (for
234  * isapnp_send_key).  :)
235  */
isapnp_lfsr_next(unsigned int lfsr,unsigned int input_bit)236 static inline unsigned int isapnp_lfsr_next ( unsigned int lfsr,
237 					      unsigned int input_bit ) {
238 	register uint8_t lfsr_next;
239 
240 	lfsr_next = lfsr >> 1;
241 	lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
242 	return lfsr_next;
243 }
244 
245 /**
246  * Send the ISAPnP initiation key.
247  *
248  * Sending the key causes all ISAPnP cards that are currently in the
249  * Wait for Key state to transition into the Sleep state.
250  */
isapnp_send_key(void)251 static void isapnp_send_key ( void ) {
252 	unsigned int i;
253 	unsigned int lfsr;
254 
255 	isapnp_delay();
256 	isapnp_write_address ( 0x00 );
257 	isapnp_write_address ( 0x00 );
258 
259 	lfsr = ISAPNP_LFSR_SEED;
260 	for ( i = 0 ; i < 32 ; i++ ) {
261 		isapnp_write_address ( lfsr );
262 		lfsr = isapnp_lfsr_next ( lfsr, 0 );
263 	}
264 }
265 
266 /**
267  * Compute ISAPnP identifier checksum
268  *
269  * @v identifier	ISAPnP identifier
270  * @ret checksum	Expected checksum value
271  */
isapnp_checksum(struct isapnp_identifier * identifier)272 static unsigned int isapnp_checksum ( struct isapnp_identifier *identifier ) {
273 	unsigned int i, j;
274 	unsigned int lfsr;
275 	unsigned int byte;
276 
277 	lfsr = ISAPNP_LFSR_SEED;
278 	for ( i = 0 ; i < 8 ; i++ ) {
279 		byte = * ( ( ( uint8_t * ) identifier ) + i );
280 		for ( j = 0 ; j < 8 ; j++ ) {
281 			lfsr = isapnp_lfsr_next ( lfsr, byte );
282 			byte >>= 1;
283 		}
284 	}
285 	return lfsr;
286 }
287 
288 /*
289  * Read a byte of resource data from the current location
290  *
291  * @ret byte		Byte of resource data
292  */
isapnp_peek_byte(void)293 static inline unsigned int isapnp_peek_byte ( void ) {
294 	unsigned int i;
295 
296 	/* Wait for data to be ready */
297 	for ( i = 0 ; i < 20 ; i++ ) {
298 		if ( isapnp_read_status() & 0x01 ) {
299 			/* Byte ready - read it */
300 			return isapnp_read_resourcedata();
301 		}
302 		isapnp_delay();
303 	}
304 	/* Data never became ready - return 0xff */
305 	return 0xff;
306 }
307 
308 /**
309  * Read resource data.
310  *
311  * @v buf		Buffer in which to store data, or NULL
312  * @v bytes		Number of bytes to read
313  *
314  * Resource data is read from the current location.  If #buf is NULL,
315  * the data is discarded.
316  */
isapnp_peek(void * buf,size_t len)317 static void isapnp_peek ( void *buf, size_t len ) {
318 	unsigned int i;
319 	unsigned int byte;
320 
321 	for ( i = 0 ; i < len ; i++) {
322 		byte = isapnp_peek_byte();
323 		if ( buf )
324 			* ( ( uint8_t * ) buf + i ) = byte;
325 	}
326 }
327 
328 /**
329  * Find a tag within the resource data.
330  *
331  * @v wanted_tag	The tag that we're looking for
332  * @v buf		Buffer in which to store the tag's contents
333  * @v len		Length of buffer
334  * @ret rc		Return status code
335  *
336  * Scan through the resource data until we find a particular tag, and
337  * read its contents into a buffer.
338  */
isapnp_find_tag(unsigned int wanted_tag,void * buf,size_t len)339 static int isapnp_find_tag ( unsigned int wanted_tag, void *buf, size_t len ) {
340 	unsigned int tag;
341 	unsigned int tag_len;
342 
343 	DBG2 ( "ISAPnP read tag" );
344 	do {
345 		tag = isapnp_peek_byte();
346 		if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
347 			tag_len = ISAPNP_SMALL_TAG_LEN ( tag );
348 			tag = ISAPNP_SMALL_TAG_NAME ( tag );
349 		} else {
350 			tag_len = ( isapnp_peek_byte() +
351 				    ( isapnp_peek_byte() << 8 ) );
352 			tag = ISAPNP_LARGE_TAG_NAME ( tag );
353 		}
354 		DBG2 ( " %02x (%02x)", tag, tag_len );
355 		if ( tag == wanted_tag ) {
356 			if ( len > tag_len )
357 				len = tag_len;
358 			isapnp_peek ( buf, len );
359 			DBG2 ( "\n" );
360 			return 0;
361 		} else {
362 			isapnp_peek ( NULL, tag_len );
363 		}
364 	} while ( tag != ISAPNP_TAG_END );
365 	DBG2 ( "\n" );
366 	return -ENOENT;
367 }
368 
369 /**
370  * Find specified Logical Device ID tag
371  *
372  * @v logdev		Logical device ID
373  * @v logdevid		Logical device ID structure to fill in
374  * @ret rc		Return status code
375  */
isapnp_find_logdevid(unsigned int logdev,struct isapnp_logdevid * logdevid)376 static int isapnp_find_logdevid ( unsigned int logdev,
377 				  struct isapnp_logdevid *logdevid ) {
378 	unsigned int i;
379 	int rc;
380 
381 	for ( i = 0 ; i <= logdev ; i++ ) {
382 		if ( ( rc = isapnp_find_tag ( ISAPNP_TAG_LOGDEVID, logdevid,
383 					      sizeof ( *logdevid ) ) ) != 0 )
384 			return rc;
385 	}
386 	return 0;
387 }
388 
389 /**
390  * Try isolating ISAPnP cards at the current read port.
391  *
392  * @ret \>0		Number of ISAPnP cards found
393  * @ret 0		There are no ISAPnP cards in the system
394  * @ret \<0		A conflict was detected; try a new read port
395  * @err None		-
396  *
397  * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
398  * gives the best overview of what happens here.
399  */
isapnp_try_isolate(void)400 static int isapnp_try_isolate ( void ) {
401 	struct isapnp_identifier identifier;
402 	unsigned int i, j;
403 	unsigned int seen_55aa, seen_life;
404 	unsigned int csn = 0;
405 	unsigned int data;
406 	unsigned int byte;
407 
408 	DBG ( "ISAPnP attempting isolation at read port %04x\n",
409 	      isapnp_read_port );
410 
411 	/* Place all cards into the Sleep state, whatever state
412 	 * they're currently in.
413 	 */
414 	isapnp_wait_for_key();
415 	isapnp_send_key();
416 
417 	/* Reset all assigned CSNs */
418 	isapnp_reset_csn();
419 	isapnp_delay();
420 	isapnp_delay();
421 
422 	/* Place all cards into the Isolation state */
423 	isapnp_wait_for_key ();
424 	isapnp_send_key();
425 	isapnp_wake ( 0x00 );
426 
427 	/* Set the read port */
428 	isapnp_set_read_port();
429 	isapnp_delay();
430 
431 	while ( 1 ) {
432 
433 		/* All cards that do not have assigned CSNs are
434 		 * currently in the Isolation state, each time we go
435 		 * through this loop.
436 		 */
437 
438 		/* Initiate serial isolation */
439 		isapnp_serialisolation();
440 		isapnp_delay();
441 
442 		/* Read identifier serially via the ISAPnP read port. */
443 		memset ( &identifier, 0, sizeof ( identifier ) );
444 		seen_55aa = seen_life = 0;
445 		for ( i = 0 ; i < 9 ; i++ ) {
446 			byte = 0;
447 			for ( j = 0 ; j < 8 ; j++ ) {
448 				data = isapnp_read_data();
449 				isapnp_delay();
450 				data = ( ( data << 8 ) | isapnp_read_data() );
451 				isapnp_delay();
452 				byte >>= 1;
453 				if (  data != 0xffff ) {
454 					seen_life++;
455 					if ( data == 0x55aa ) {
456 						byte |= 0x80;
457 						seen_55aa++;
458 					}
459 				}
460 			}
461 			*( ( ( uint8_t * ) &identifier ) + i ) = byte;
462 		}
463 
464 		/* If we didn't see any 55aa patterns, stop here */
465 		if ( ! seen_55aa ) {
466 			if ( csn ) {
467 				DBG ( "ISAPnP found no more cards\n" );
468 			} else {
469 				if ( seen_life ) {
470 					DBG ( "ISAPnP saw life but no cards, "
471 					      "trying new read port\n" );
472 					csn = -1;
473 				} else {
474 					DBG ( "ISAPnP saw no signs of life, "
475 					      "abandoning isolation\n" );
476 				}
477 			}
478 			break;
479 		}
480 
481 		/* If the checksum was invalid stop here */
482 		if ( identifier.checksum != isapnp_checksum ( &identifier) ) {
483 			DBG ( "ISAPnP found malformed card "
484 			      ISAPNP_CARD_ID_FMT "\n  with checksum %02x "
485 			      "(should be %02x), trying new read port\n",
486 			      ISAPNP_CARD_ID_DATA ( &identifier ),
487 			      identifier.checksum,
488 			      isapnp_checksum ( &identifier) );
489 			csn = -1;
490 			break;
491 		}
492 
493 		/* Give the device a CSN */
494 		csn++;
495 		DBG ( "ISAPnP found card " ISAPNP_CARD_ID_FMT
496 		      ", assigning CSN %02x\n",
497 		      ISAPNP_CARD_ID_DATA ( &identifier ), csn );
498 
499 		isapnp_write_csn ( csn );
500 		isapnp_delay();
501 
502 		/* Send this card back to Sleep and force all cards
503 		 * without a CSN into Isolation state
504 		 */
505 		isapnp_wake ( 0x00 );
506 		isapnp_delay();
507 	}
508 
509 	/* Place all cards in Wait for Key state */
510 	isapnp_wait_for_key();
511 
512 	/* Return number of cards found */
513 	if ( csn > 0 ) {
514 		DBG ( "ISAPnP found %d cards at read port %04x\n",
515 		      csn, isapnp_read_port );
516 	}
517 	return csn;
518 }
519 
520 /**
521  * Find a valid read port and isolate all ISAPnP cards.
522  *
523  */
isapnp_isolate(void)524 static void isapnp_isolate ( void ) {
525 	for ( isapnp_read_port = ISAPNP_READ_PORT_START ;
526 	      isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
527 	      isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
528 		/* Avoid problematic locations such as the NE2000
529 		 * probe space
530 		 */
531 		if ( ( isapnp_read_port >= 0x280 ) &&
532 		     ( isapnp_read_port <= 0x380 ) )
533 			continue;
534 
535 		/* If we detect any ISAPnP cards at this location, stop */
536 		if ( isapnp_try_isolate() >= 0 )
537 			return;
538 	}
539 }
540 
541 /**
542  * Activate or deactivate an ISAPnP device.
543  *
544  * @v isapnp		ISAPnP device
545  * @v activation	True to enable, False to disable the device
546  * @ret None		-
547  * @err None		-
548  *
549  * This routine simply activates the device in its current
550  * configuration, or deactivates the device.  It does not attempt any
551  * kind of resource arbitration.
552  *
553  */
isapnp_device_activation(struct isapnp_device * isapnp,int activation)554 void isapnp_device_activation ( struct isapnp_device *isapnp,
555 				int activation ) {
556 	/* Wake the card and select the logical device */
557 	isapnp_wait_for_key ();
558 	isapnp_send_key ();
559 	isapnp_wake ( isapnp->csn );
560 	isapnp_logicaldevice ( isapnp->logdev );
561 
562 	/* Activate/deactivate the logical device */
563 	isapnp_activate ( activation );
564 	isapnp_delay();
565 
566 	/* Return all cards to Wait for Key state */
567 	isapnp_wait_for_key ();
568 
569 	DBG ( "ISAPnP %s device %02x:%02x\n",
570 	      ( activation ? "activated" : "deactivated" ),
571 	      isapnp->csn, isapnp->logdev );
572 }
573 
574 /**
575  * Probe an ISAPnP device
576  *
577  * @v isapnp		ISAPnP device
578  * @ret rc		Return status code
579  *
580  * Searches for a driver for the ISAPnP device.  If a driver is found,
581  * its probe() routine is called.
582  */
isapnp_probe(struct isapnp_device * isapnp)583 static int isapnp_probe ( struct isapnp_device *isapnp ) {
584 	struct isapnp_driver *driver;
585 	struct isapnp_device_id *id;
586 	unsigned int i;
587 	int rc;
588 
589 	DBG ( "Adding ISAPnP device %02x:%02x (%04x:%04x (\"%s\") "
590 	      "io %x irq %d)\n", isapnp->csn, isapnp->logdev,
591 	      isapnp->vendor_id, isapnp->prod_id,
592 	      isa_id_string ( isapnp->vendor_id, isapnp->prod_id ),
593 	      isapnp->ioaddr, isapnp->irqno );
594 
595 	for_each_table_entry ( driver, ISAPNP_DRIVERS ) {
596 		for ( i = 0 ; i < driver->id_count ; i++ ) {
597 			id = &driver->ids[i];
598 			if ( id->vendor_id != isapnp->vendor_id )
599 				continue;
600 			if ( ISA_PROD_ID ( id->prod_id ) !=
601 			     ISA_PROD_ID ( isapnp->prod_id ) )
602 				continue;
603 			isapnp->driver = driver;
604 			isapnp->dev.driver_name = id->name;
605 			DBG ( "...using driver %s\n", isapnp->dev.driver_name );
606 			if ( ( rc = driver->probe ( isapnp, id ) ) != 0 ) {
607 				DBG ( "......probe failed\n" );
608 				continue;
609 			}
610 			return 0;
611 		}
612 	}
613 
614 	DBG ( "...no driver found\n" );
615 	return -ENOTTY;
616 }
617 
618 /**
619  * Remove an ISAPnP device
620  *
621  * @v isapnp		ISAPnP device
622  */
isapnp_remove(struct isapnp_device * isapnp)623 static void isapnp_remove ( struct isapnp_device *isapnp ) {
624 	isapnp->driver->remove ( isapnp );
625 	DBG ( "Removed ISAPnP device %02x:%02x\n",
626 	      isapnp->csn, isapnp->logdev );
627 }
628 
629 /**
630  * Probe ISAPnP root bus
631  *
632  * @v rootdev		ISAPnP bus root device
633  *
634  * Scans the ISAPnP bus for devices and registers all devices it can
635  * find.
636  */
isapnpbus_probe(struct root_device * rootdev)637 static int isapnpbus_probe ( struct root_device *rootdev ) {
638 	struct isapnp_device *isapnp = NULL;
639 	struct isapnp_identifier identifier;
640 	struct isapnp_logdevid logdevid;
641 	unsigned int csn;
642 	unsigned int logdev;
643 	int rc;
644 
645 	/* Perform isolation if it hasn't yet been done */
646 	if ( ! isapnp_read_port )
647 		isapnp_isolate();
648 
649 	for ( csn = 1 ; csn <= 0xff ; csn++ ) {
650 		for ( logdev = 0 ; logdev <= 0xff ; logdev++ ) {
651 
652 			/* Allocate struct isapnp_device */
653 			if ( ! isapnp )
654 				isapnp = malloc ( sizeof ( *isapnp ) );
655 			if ( ! isapnp ) {
656 				rc = -ENOMEM;
657 				goto err;
658 			}
659 			memset ( isapnp, 0, sizeof ( *isapnp ) );
660 			isapnp->csn = csn;
661 			isapnp->logdev = logdev;
662 
663 			/* Wake the card */
664 			isapnp_wait_for_key();
665 			isapnp_send_key();
666 			isapnp_wake ( csn );
667 
668 			/* Read the card identifier */
669 			isapnp_peek ( &identifier, sizeof ( identifier ) );
670 
671 			/* No card with this CSN; stop here */
672 			if ( identifier.vendor_id & 0x80 )
673 				goto done;
674 
675 			/* Find the Logical Device ID tag */
676 			if ( ( rc = isapnp_find_logdevid ( logdev,
677 							   &logdevid ) ) != 0){
678 				/* No more logical devices; go to next CSN */
679 				break;
680 			}
681 
682 			/* Select the logical device */
683 			isapnp_logicaldevice ( logdev );
684 
685 			/* Populate struct isapnp_device */
686 			isapnp->vendor_id = logdevid.vendor_id;
687 			isapnp->prod_id = logdevid.prod_id;
688 			isapnp->ioaddr = isapnp_read_iobase ( 0 );
689 			isapnp->irqno = isapnp_read_irqno ( 0 );
690 
691 			/* Return all cards to Wait for Key state */
692 			isapnp_wait_for_key();
693 
694 			/* Add to device hierarchy */
695 			snprintf ( isapnp->dev.name,
696 				   sizeof ( isapnp->dev.name ),
697 				   "ISAPnP%02x:%02x", csn, logdev );
698 			isapnp->dev.desc.bus_type = BUS_TYPE_ISAPNP;
699 			isapnp->dev.desc.vendor = isapnp->vendor_id;
700 			isapnp->dev.desc.device = isapnp->prod_id;
701 			isapnp->dev.desc.ioaddr = isapnp->ioaddr;
702 			isapnp->dev.desc.irq = isapnp->irqno;
703 			isapnp->dev.parent = &rootdev->dev;
704 			list_add ( &isapnp->dev.siblings,
705 				   &rootdev->dev.children );
706 			INIT_LIST_HEAD ( &isapnp->dev.children );
707 
708 			/* Look for a driver */
709 			if ( isapnp_probe ( isapnp ) == 0 ) {
710 				/* isapnpdev registered, we can drop our ref */
711 				isapnp = NULL;
712 			} else {
713 				/* Not registered; re-use struct */
714 				list_del ( &isapnp->dev.siblings );
715 			}
716 		}
717 	}
718 
719  done:
720 	free ( isapnp );
721 	return 0;
722 
723  err:
724 	free ( isapnp );
725 	isapnpbus_remove ( rootdev );
726 	return rc;
727 }
728 
729 /**
730  * Remove ISAPnP root bus
731  *
732  * @v rootdev		ISAPnP bus root device
733  */
isapnpbus_remove(struct root_device * rootdev)734 static void isapnpbus_remove ( struct root_device *rootdev ) {
735 	struct isapnp_device *isapnp;
736 	struct isapnp_device *tmp;
737 
738 	list_for_each_entry_safe ( isapnp, tmp, &rootdev->dev.children,
739 				   dev.siblings ) {
740 		isapnp_remove ( isapnp );
741 		list_del ( &isapnp->dev.siblings );
742 		free ( isapnp );
743 	}
744 }
745 
746 /** ISAPnP bus root device driver */
747 static struct root_driver isapnp_root_driver = {
748 	.probe = isapnpbus_probe,
749 	.remove = isapnpbus_remove,
750 };
751 
752 /** ISAPnP bus root device */
753 struct root_device isapnp_root_device __root_device = {
754 	.dev = { .name = "ISAPnP" },
755 	.driver = &isapnp_root_driver,
756 };
757