You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

isapnp.c 20KB

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