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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. * However, we shouldn't make scanning the ISAPnP bus an INIT_FN(),
  49. * because even ISAPnP probing can still screw up other devices on the
  50. * ISA bus. We therefore probe only when we are first asked to find
  51. * an ISAPnP device.
  52. *
  53. * External code (e.g. the ISAPnP ROM prefix) may already know the
  54. * read port address, in which case it can store it in
  55. * #isapnp_read_port. Note that setting the read port address in this
  56. * way will prevent further isolation from taking place; you should
  57. * set the read port address only if you know that devices have
  58. * already been allocated CSNs.
  59. *
  60. */
  61. #include "string.h"
  62. #include "timer.h"
  63. #include "io.h"
  64. #include "console.h"
  65. #include "dev.h"
  66. #include "isapnp.h"
  67. /**
  68. * ISAPnP Read Port address.
  69. *
  70. */
  71. uint16_t isapnp_read_port;
  72. /**
  73. * Highest assigned CSN.
  74. *
  75. * Note that @b we do not necessarily assign CSNs; it could be done by
  76. * the PnP BIOS instead. We therefore set this only when we first try
  77. * to Wake[CSN] a device and find that there's nothing there. Page 16
  78. * (PDF page 22) of the ISAPnP spec states that "Valid Card Select
  79. * Numbers for identified ISA cards range from 1 to 255 and must be
  80. * assigned sequentially starting from 1", so we are (theoretically,
  81. * at least) safe to assume that there are no ISAPnP cards at CSNs
  82. * higher than the first unused CSN.
  83. *
  84. */
  85. static uint8_t isapnp_max_csn = 0xff;
  86. /*
  87. * ISAPnP utility functions
  88. *
  89. */
  90. #define ISAPNP_CARD_ID_FMT "ID %hx:%hx (\"%s\") serial %x"
  91. #define ISAPNP_CARD_ID_DATA(identifier) \
  92. (identifier)->vendor_id, (identifier)->prod_id, \
  93. isa_id_string ( (identifier)->vendor_id, (identifier)->prod_id ), \
  94. (identifier)->serial
  95. #define ISAPNP_DEV_ID_FMT "ID %hx:%hx (\"%s\")"
  96. #define ISAPNP_DEV_ID_DATA(isapnp) \
  97. (isapnp)->vendor_id, (isapnp)->prod_id, \
  98. isa_id_string ( (isapnp)->vendor_id, (isapnp)->prod_id )
  99. static inline void isapnp_write_address ( uint8_t address ) {
  100. outb ( address, ISAPNP_ADDRESS );
  101. }
  102. static inline void isapnp_write_data ( uint8_t data ) {
  103. outb ( data, ISAPNP_WRITE_DATA );
  104. }
  105. static inline uint8_t isapnp_read_data ( void ) {
  106. return inb ( isapnp_read_port );
  107. }
  108. static inline void isapnp_write_byte ( uint8_t address, uint8_t value ) {
  109. isapnp_write_address ( address );
  110. isapnp_write_data ( value );
  111. }
  112. static inline uint8_t isapnp_read_byte ( uint8_t address ) {
  113. isapnp_write_address ( address );
  114. return isapnp_read_data ();
  115. }
  116. static inline uint16_t isapnp_read_word ( uint8_t address ) {
  117. /* Yes, they're in big-endian order */
  118. return ( ( isapnp_read_byte ( address ) << 8 )
  119. + isapnp_read_byte ( address + 1 ) );
  120. }
  121. /** Inform cards of a new read port address */
  122. static inline void isapnp_set_read_port ( void ) {
  123. isapnp_write_byte ( ISAPNP_READPORT, isapnp_read_port >> 2 );
  124. }
  125. /**
  126. * Enter the Isolation state.
  127. *
  128. * Only cards currently in the Sleep state will respond to this
  129. * command.
  130. *
  131. */
  132. static inline void isapnp_serialisolation ( void ) {
  133. isapnp_write_address ( ISAPNP_SERIALISOLATION );
  134. }
  135. /**
  136. * Enter the Wait for Key state.
  137. *
  138. * All cards will respond to this command, regardless of their current
  139. * state.
  140. *
  141. */
  142. static inline void isapnp_wait_for_key ( void ) {
  143. isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
  144. }
  145. /**
  146. * Reset (i.e. remove) Card Select Number.
  147. *
  148. * Only cards currently in the Sleep state will respond to this
  149. * command.
  150. *
  151. */
  152. static inline void isapnp_reset_csn ( void ) {
  153. isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
  154. }
  155. /**
  156. * Place a specified card into the Config state.
  157. *
  158. * @v csn Card Select Number
  159. * @ret None -
  160. * @err None -
  161. *
  162. * Only cards currently in the Sleep, Isolation, or Config states will
  163. * respond to this command. The card that has the specified CSN will
  164. * enter the Config state, all other cards will enter the Sleep state.
  165. *
  166. */
  167. static inline void isapnp_wake ( uint8_t csn ) {
  168. isapnp_write_byte ( ISAPNP_WAKE, csn );
  169. }
  170. static inline uint8_t isapnp_read_resourcedata ( void ) {
  171. return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
  172. }
  173. static inline uint8_t isapnp_read_status ( void ) {
  174. return isapnp_read_byte ( ISAPNP_STATUS );
  175. }
  176. /**
  177. * Assign a Card Select Number to a card, and enter the Config state.
  178. *
  179. * @v csn Card Select Number
  180. * @ret None -
  181. * @err None -
  182. *
  183. * Only cards in the Isolation state will respond to this command.
  184. * The isolation protocol is designed so that only one card will
  185. * remain in the Isolation state by the time the isolation protocol
  186. * completes.
  187. *
  188. */
  189. static inline void isapnp_write_csn ( uint8_t csn ) {
  190. isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
  191. }
  192. static inline void isapnp_logicaldevice ( uint8_t logdev ) {
  193. isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
  194. }
  195. static inline void isapnp_activate ( uint8_t logdev ) {
  196. isapnp_logicaldevice ( logdev );
  197. isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
  198. }
  199. static inline void isapnp_deactivate ( uint8_t logdev ) {
  200. isapnp_logicaldevice ( logdev );
  201. isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
  202. }
  203. static inline uint16_t isapnp_read_iobase ( unsigned int index ) {
  204. return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
  205. }
  206. static inline uint8_t isapnp_read_irqno ( unsigned int index ) {
  207. return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
  208. }
  209. static void isapnp_delay ( void ) {
  210. udelay ( 1000 );
  211. }
  212. /**
  213. * Linear feedback shift register.
  214. *
  215. * @v lfsr Current value of the LFSR
  216. * @v input_bit Current input bit to the LFSR
  217. * @ret lfsr Next value of the LFSR
  218. * @err None -
  219. *
  220. * This routine implements the linear feedback shift register as
  221. * described in Appendix B of the PnP ISA spec. The hardware
  222. * implementation uses eight D-type latches and two XOR gates. I
  223. * think this is probably the smallest possible implementation in
  224. * software. Six instructions when input_bit is a constant 0 (for
  225. * isapnp_send_key). :)
  226. *
  227. */
  228. static inline uint8_t isapnp_lfsr_next ( uint8_t lfsr, int input_bit ) {
  229. register uint8_t lfsr_next;
  230. lfsr_next = lfsr >> 1;
  231. lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
  232. return lfsr_next;
  233. }
  234. /**
  235. * Send the ISAPnP initiation key.
  236. *
  237. * Sending the key causes all ISAPnP cards that are currently in the
  238. * Wait for Key state to transition into the Sleep state.
  239. *
  240. */
  241. static void isapnp_send_key ( void ) {
  242. unsigned int i;
  243. uint8_t lfsr;
  244. isapnp_delay();
  245. isapnp_write_address ( 0x00 );
  246. isapnp_write_address ( 0x00 );
  247. lfsr = ISAPNP_LFSR_SEED;
  248. for ( i = 0 ; i < 32 ; i++ ) {
  249. isapnp_write_address ( lfsr );
  250. lfsr = isapnp_lfsr_next ( lfsr, 0 );
  251. }
  252. }
  253. /**
  254. * Compute ISAPnP identifier checksum
  255. *
  256. * @v identifier ISAPnP identifier
  257. * @ret checksum Expected checksum value
  258. * @err None -
  259. *
  260. */
  261. static uint8_t isapnp_checksum ( struct isapnp_identifier *identifier ) {
  262. int i, j;
  263. uint8_t lfsr;
  264. uint8_t byte;
  265. lfsr = ISAPNP_LFSR_SEED;
  266. for ( i = 0 ; i < 8 ; i++ ) {
  267. byte = ( (char *) identifier )[i];
  268. for ( j = 0 ; j < 8 ; j++ ) {
  269. lfsr = isapnp_lfsr_next ( lfsr, byte );
  270. byte >>= 1;
  271. }
  272. }
  273. return lfsr;
  274. }
  275. /*
  276. * Read a byte of resource data from the current location
  277. *
  278. */
  279. static inline uint8_t isapnp_peek_byte ( void ) {
  280. int i;
  281. /* Wait for data to be ready */
  282. for ( i = 0 ; i < 20 ; i ++ ) {
  283. if ( isapnp_read_status() & 0x01 ) {
  284. /* Byte ready - read it */
  285. return isapnp_read_resourcedata();
  286. }
  287. isapnp_delay ();
  288. }
  289. /* Data never became ready - return 0xff */
  290. return 0xff;
  291. }
  292. /**
  293. * Read resource data.
  294. *
  295. * @v buf Buffer in which to store data, or NULL
  296. * @v bytes Number of bytes to read
  297. * @ret None -
  298. * @err None -
  299. *
  300. * Resource data is read from the current location. If #buf is NULL,
  301. * the data is discarded.
  302. *
  303. */
  304. static void isapnp_peek ( uint8_t *buf, size_t bytes ) {
  305. unsigned int i;
  306. uint8_t byte;
  307. for ( i = 0 ; i < bytes ; i++) {
  308. byte = isapnp_peek_byte();
  309. if ( buf ) {
  310. buf[i] = byte;
  311. }
  312. }
  313. }
  314. /**
  315. * Find a tag within the resource data.
  316. *
  317. * @v wanted_tag The tag that we're looking for
  318. * @v buf Buffer in which to store the tag's contents
  319. * @ret True Tag was found
  320. * @ret False Tag was not found
  321. * @err None -
  322. *
  323. * Scan through the resource data until we find a particular tag, and
  324. * read its contents into a buffer. It is the caller's responsibility
  325. * to ensure that #buf is large enough to contain a tag of the
  326. * requested size.
  327. *
  328. */
  329. static int isapnp_find_tag ( uint8_t wanted_tag, uint8_t *buf ) {
  330. uint8_t tag;
  331. uint16_t len;
  332. DBG2 ( "ISAPnP read tag" );
  333. do {
  334. tag = isapnp_peek_byte();
  335. if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
  336. len = ISAPNP_SMALL_TAG_LEN ( tag );
  337. tag = ISAPNP_SMALL_TAG_NAME ( tag );
  338. } else {
  339. len = isapnp_peek_byte() + ( isapnp_peek_byte() << 8 );
  340. tag = ISAPNP_LARGE_TAG_NAME ( tag );
  341. }
  342. DBG2 ( " %hhx (%hhx)", tag, len );
  343. if ( tag == wanted_tag ) {
  344. isapnp_peek ( buf, len );
  345. DBG2 ( "\n" );
  346. return 1;
  347. } else {
  348. isapnp_peek ( NULL, len );
  349. }
  350. } while ( tag != ISAPNP_TAG_END );
  351. DBG2 ( "\n" );
  352. return 0;
  353. }
  354. /**
  355. * Try isolating ISAPnP cards at the current read port.
  356. *
  357. * @ret \>0 Number of ISAPnP cards found
  358. * @ret 0 There are no ISAPnP cards in the system
  359. * @ret \<0 A conflict was detected; try a new read port
  360. * @err None -
  361. *
  362. * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
  363. * gives the best overview of what happens here.
  364. *
  365. */
  366. static int isapnp_try_isolate ( void ) {
  367. struct isapnp_identifier identifier;
  368. unsigned int i, j;
  369. unsigned int seen_55aa, seen_life;
  370. unsigned int csn = 0;
  371. uint16_t data;
  372. uint8_t byte;
  373. DBG ( "ISAPnP attempting isolation at read port %hx\n",
  374. isapnp_read_port );
  375. /* Place all cards into the Sleep state, whatever state
  376. * they're currently in.
  377. */
  378. isapnp_wait_for_key ();
  379. isapnp_send_key ();
  380. /* Reset all assigned CSNs */
  381. isapnp_reset_csn ();
  382. isapnp_delay();
  383. isapnp_delay();
  384. /* Place all cards into the Isolation state */
  385. isapnp_wait_for_key ();
  386. isapnp_send_key ();
  387. isapnp_wake ( 0x00 );
  388. /* Set the read port */
  389. isapnp_set_read_port ();
  390. isapnp_delay();
  391. while ( 1 ) {
  392. /* All cards that do not have assigned CSNs are
  393. * currently in the Isolation state, each time we go
  394. * through this loop.
  395. */
  396. /* Initiate serial isolation */
  397. isapnp_serialisolation ();
  398. isapnp_delay();
  399. /* Read identifier serially via the ISAPnP read port. */
  400. memset ( &identifier, 0, sizeof ( identifier ) );
  401. seen_55aa = seen_life = 0;
  402. for ( i = 0 ; i < 9 ; i++ ) {
  403. byte = 0;
  404. for ( j = 0 ; j < 8 ; j++ ) {
  405. data = isapnp_read_data ();
  406. isapnp_delay();
  407. data = ( data << 8 ) | isapnp_read_data ();
  408. isapnp_delay();
  409. byte >>= 1;
  410. if ( data != 0xffff ) {
  411. seen_life++;
  412. if ( data == 0x55aa ) {
  413. byte |= 0x80;
  414. seen_55aa++;
  415. }
  416. }
  417. }
  418. ( (char *) &identifier )[i] = byte;
  419. }
  420. /* If we didn't see any 55aa patterns, stop here */
  421. if ( ! seen_55aa ) {
  422. if ( csn ) {
  423. DBG ( "ISAPnP found no more cards\n" );
  424. } else {
  425. if ( seen_life ) {
  426. DBG ( "ISAPnP saw life but no cards, "
  427. "trying new read port\n" );
  428. csn = -1;
  429. } else {
  430. DBG ( "ISAPnP saw no signs of life, "
  431. "abandoning isolation\n" );
  432. }
  433. }
  434. break;
  435. }
  436. /* If the checksum was invalid stop here */
  437. if ( identifier.checksum != isapnp_checksum ( &identifier) ) {
  438. DBG ( "ISAPnP found malformed card "
  439. ISAPNP_CARD_ID_FMT "\n with checksum %hhx "
  440. "(should be %hhx), trying new read port\n",
  441. ISAPNP_CARD_ID_DATA ( &identifier ),
  442. identifier.checksum,
  443. isapnp_checksum ( &identifier) );
  444. csn = -1;
  445. break;
  446. }
  447. /* Give the device a CSN */
  448. csn++;
  449. DBG ( "ISAPnP found card " ISAPNP_CARD_ID_FMT
  450. ", assigning CSN %hhx\n",
  451. ISAPNP_CARD_ID_DATA ( &identifier ), csn );
  452. isapnp_write_csn ( csn );
  453. isapnp_delay();
  454. /* Send this card back to Sleep and force all cards
  455. * without a CSN into Isolation state
  456. */
  457. isapnp_wake ( 0x00 );
  458. isapnp_delay();
  459. }
  460. /* Place all cards in Wait for Key state */
  461. isapnp_wait_for_key ();
  462. /* Return number of cards found */
  463. if ( csn > 0 ) {
  464. DBG ( "ISAPnP found %d cards at read port %hx\n",
  465. csn, isapnp_read_port );
  466. }
  467. return csn;
  468. }
  469. /**
  470. * Find a valid read port and isolate all ISAPnP cards.
  471. *
  472. */
  473. static void isapnp_isolate ( void ) {
  474. for ( isapnp_read_port = ISAPNP_READ_PORT_MIN ;
  475. isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
  476. isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
  477. /* Avoid problematic locations such as the NE2000
  478. * probe space
  479. */
  480. if ( ( isapnp_read_port >= 0x280 ) &&
  481. ( isapnp_read_port <= 0x380 ) )
  482. continue;
  483. /* If we detect any ISAPnP cards at this location, stop */
  484. if ( isapnp_try_isolate () >= 0 )
  485. return;
  486. }
  487. }
  488. /**
  489. * Increment a #bus_loc structure to the next possible ISAPnP
  490. * location.
  491. *
  492. * @v bus_loc Bus location
  493. * @ret True #bus_loc contains a valid ISAPnP location
  494. * @ret False There are no more valid ISAPnP locations
  495. * @err None -
  496. *
  497. * If there are no more valid locations, the #bus_loc structure will
  498. * be zeroed.
  499. *
  500. */
  501. static int isapnp_next_location ( struct bus_loc *bus_loc ) {
  502. struct isapnp_loc *isapnp_loc = ( struct isapnp_loc * ) bus_loc;
  503. /*
  504. * Ensure that there is sufficient space in the shared bus
  505. * structures for a struct isapnp_loc and a struct isapnp_dev,
  506. * as mandated by bus.h.
  507. *
  508. */
  509. BUS_LOC_CHECK ( struct isapnp_loc );
  510. BUS_DEV_CHECK ( struct isapnp_device );
  511. return ( ++isapnp_loc->logdev ? 1 : ++isapnp_loc->csn );
  512. }
  513. /**
  514. * Fill in parameters for an ISAPnP device based on CSN.
  515. *
  516. * @v bus_dev Bus device to be filled in
  517. * @v bus_loc Bus location as filled in by isapnp_next_location()
  518. * @ret True A device is present at this location
  519. * @ret False No device is present at this location
  520. * @err None -
  521. *
  522. */
  523. static int isapnp_fill_device ( struct bus_dev *bus_dev,
  524. struct bus_loc *bus_loc ) {
  525. struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
  526. struct isapnp_loc *isapnp_loc = ( struct isapnp_loc * ) bus_loc;
  527. unsigned int i;
  528. struct isapnp_identifier identifier;
  529. struct isapnp_logdevid logdevid;
  530. static struct {
  531. uint8_t csn;
  532. uint8_t first_nonexistent_logdev;
  533. } cache = { 0, 0 };
  534. /* Copy CSN and logdev to isapnp_device, set default values */
  535. isapnp->csn = isapnp_loc->csn;
  536. isapnp->logdev = isapnp_loc->logdev;
  537. isapnp->name = "?";
  538. /* CSN 0 is never valid, but may be passed in */
  539. if ( ! isapnp->csn )
  540. return 0;
  541. /* Check to see if we are already past the maximum CSN */
  542. if ( isapnp->csn > isapnp_max_csn )
  543. return 0;
  544. /* Check cache to see if we are already past the highest
  545. * logical device of this CSN
  546. */
  547. if ( ( isapnp->csn == cache.csn ) &&
  548. ( isapnp->logdev >= cache.first_nonexistent_logdev ) )
  549. return 0;
  550. /* Perform isolation if it hasn't yet been done */
  551. if ( ! isapnp_read_port )
  552. isapnp_isolate();
  553. /* Wake the card */
  554. isapnp_wait_for_key ();
  555. isapnp_send_key ();
  556. isapnp_wake ( isapnp->csn );
  557. /* Read the card identifier */
  558. isapnp_peek ( ( char * ) &identifier, sizeof ( identifier ) );
  559. /* Need to return 0 if no device exists at this CSN */
  560. if ( identifier.vendor_id & 0x80 ) {
  561. isapnp_max_csn = isapnp->csn - 1;
  562. return 0;
  563. }
  564. /* Find the Logical Device ID tag corresponding to this device */
  565. for ( i = 0 ; i <= isapnp->logdev ; i++ ) {
  566. if ( ! isapnp_find_tag ( ISAPNP_TAG_LOGDEVID,
  567. ( char * ) &logdevid ) ) {
  568. /* No tag for this device */
  569. if ( isapnp->logdev == 0 ) {
  570. DBG ( "ISAPnP found no device %hhx.0 on card "
  571. ISAPNP_CARD_ID_FMT "\n", isapnp->csn,
  572. ISAPNP_CARD_ID_DATA ( &identifier ) );
  573. }
  574. cache.csn = isapnp->csn;
  575. cache.first_nonexistent_logdev = isapnp->logdev;
  576. return 0;
  577. }
  578. }
  579. /* Read information from logdevid structure */
  580. isapnp->vendor_id = logdevid.vendor_id;
  581. isapnp->prod_id = logdevid.prod_id;
  582. /* Select the logical device */
  583. isapnp_logicaldevice ( isapnp->logdev );
  584. /* Read the current ioaddr and irqno */
  585. isapnp->ioaddr = isapnp_read_iobase ( 0 );
  586. isapnp->irqno = isapnp_read_irqno ( 0 );
  587. /* Return all cards to Wait for Key state */
  588. isapnp_wait_for_key ();
  589. DBG ( "ISAPnP found device %hhx.%hhx " ISAPNP_DEV_ID_FMT
  590. ", base %hx irq %d\n", isapnp->csn, isapnp->logdev,
  591. ISAPNP_DEV_ID_DATA ( isapnp ), isapnp->ioaddr, isapnp->irqno );
  592. DBG ( " on card " ISAPNP_CARD_ID_FMT "\n",
  593. ISAPNP_CARD_ID_DATA ( &identifier ) );
  594. return 1;
  595. }
  596. /**
  597. * Test whether or not a driver is capable of driving the device.
  598. *
  599. * @v bus_dev Bus device as filled in by isapnp_fill_device()
  600. * @v device_driver Device driver
  601. * @ret True Driver is capable of driving this device
  602. * @ret False Driver is not capable of driving this device
  603. * @err None -
  604. *
  605. */
  606. static int isapnp_check_driver ( struct bus_dev *bus_dev,
  607. struct device_driver *device_driver ) {
  608. struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
  609. struct isapnp_driver *driver
  610. = ( struct isapnp_driver * ) device_driver->bus_driver_info;
  611. unsigned int i;
  612. /* Compare against driver's ID list */
  613. for ( i = 0 ; i < driver->id_count ; i++ ) {
  614. struct isapnp_id *id = &driver->ids[i];
  615. if ( ( isapnp->vendor_id == id->vendor_id ) &&
  616. ( ISA_PROD_ID ( isapnp->prod_id ) ==
  617. ISA_PROD_ID ( id->prod_id ) ) ) {
  618. DBG ( "ISAPnP found ID %hx:%hx (\"%s\") (device %s) "
  619. "matching driver %s\n",
  620. isapnp->vendor_id, isapnp->prod_id,
  621. isa_id_string( isapnp->vendor_id,
  622. isapnp->prod_id ),
  623. id->name, device_driver->name );
  624. isapnp->name = id->name;
  625. return 1;
  626. }
  627. }
  628. return 0;
  629. }
  630. /**
  631. * Describe an ISAPnP device.
  632. *
  633. * @v bus_dev Bus device as filled in by isapnp_fill_device()
  634. * @ret string Printable string describing the device
  635. * @err None -
  636. *
  637. * The string returned by isapnp_describe_device() is valid only until
  638. * the next call to isapnp_describe_device().
  639. *
  640. */
  641. static char * isapnp_describe_device ( struct bus_dev *bus_dev ) {
  642. struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
  643. static char isapnp_description[] = "ISAPnP 00:00";
  644. sprintf ( isapnp_description + 7, "%hhx:%hhx",
  645. isapnp->csn, isapnp->logdev );
  646. return isapnp_description;
  647. }
  648. /**
  649. * Name an ISAPnP device.
  650. *
  651. * @v bus_dev Bus device as filled in by isapnp_fill_device()
  652. * @ret string Printable string naming the device
  653. * @err None -
  654. *
  655. * The string returned by isapnp_name_device() is valid only until the
  656. * next call to isapnp_name_device().
  657. *
  658. */
  659. static const char * isapnp_name_device ( struct bus_dev *bus_dev ) {
  660. struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
  661. return isapnp->name;
  662. }
  663. /*
  664. * ISAPnP bus operations table
  665. *
  666. */
  667. struct bus_driver isapnp_driver __bus_driver = {
  668. .name = "ISAPnP",
  669. .next_location = isapnp_next_location,
  670. .fill_device = isapnp_fill_device,
  671. .check_driver = isapnp_check_driver,
  672. .describe_device = isapnp_describe_device,
  673. .name_device = isapnp_name_device,
  674. };
  675. /**
  676. * Activate or deactivate an ISAPnP device.
  677. *
  678. * @v isapnp ISAPnP device
  679. * @v activation True to enable, False to disable the device
  680. * @ret None -
  681. * @err None -
  682. *
  683. * This routine simply activates the device in its current
  684. * configuration, or deactivates the device. It does not attempt any
  685. * kind of resource arbitration.
  686. *
  687. */
  688. void isapnp_device_activation ( struct isapnp_device *isapnp,
  689. int activation ) {
  690. /* Wake the card and select the logical device */
  691. isapnp_wait_for_key ();
  692. isapnp_send_key ();
  693. isapnp_wake ( isapnp->csn );
  694. isapnp_logicaldevice ( isapnp->logdev );
  695. /* Activate/deactivate the logical device */
  696. isapnp_activate ( activation );
  697. isapnp_delay();
  698. /* Return all cards to Wait for Key state */
  699. isapnp_wait_for_key ();
  700. DBG ( "ISAPnP %s device %hhx.%hhx\n",
  701. ( activation ? "activated" : "deactivated" ),
  702. isapnp->csn, isapnp->logdev );
  703. }
  704. /**
  705. * Fill in a nic structure.
  706. *
  707. * @v nic NIC structure to be filled in
  708. * @v isapnp ISAPnP device
  709. * @ret None -
  710. * @err None -
  711. *
  712. * This fills in generic NIC parameters (e.g. I/O address and IRQ
  713. * number) that can be determined directly from the ISAPnP device,
  714. * without any driver-specific knowledge.
  715. *
  716. */
  717. void isapnp_fill_nic ( struct nic *nic, struct isapnp_device *isapnp ) {
  718. /* Fill in ioaddr and irqno */
  719. nic->ioaddr = isapnp->ioaddr;
  720. nic->irqno = isapnp->irqno;
  721. /* Fill in DHCP device ID structure */
  722. nic->dhcp_dev_id.bus_type = ISA_BUS_TYPE;
  723. nic->dhcp_dev_id.vendor_id = htons ( isapnp->vendor_id );
  724. nic->dhcp_dev_id.device_id = htons ( isapnp->prod_id );
  725. }