pnp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 1996, Sujal M. Patel
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
  29. */
  30. #include <sys/param.h>
  31. #include <sys/systm.h>
  32. #include <sys/kernel.h>
  33. #include <sys/module.h>
  34. #include <sys/bus.h>
  35. #include <sys/endian.h>
  36. #include <sys/malloc.h>
  37. #include <isa/isavar.h>
  38. #include <isa/pnpreg.h>
  39. #include <isa/pnpvar.h>
  40. #include <machine/bus.h>
  41. typedef struct _pnp_id {
  42. uint32_t vendor_id;
  43. uint32_t serial;
  44. u_char checksum;
  45. } pnp_id;
  46. struct pnp_set_config_arg {
  47. int csn; /* Card number to configure */
  48. int ldn; /* Logical device on card */
  49. };
  50. struct pnp_quirk {
  51. uint32_t vendor_id; /* Vendor of the card */
  52. uint32_t logical_id; /* ID of the device with quirk */
  53. int type;
  54. #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */
  55. #define PNP_QUIRK_EXTRA_IO 2 /* Has extra io ports */
  56. int arg1;
  57. int arg2;
  58. };
  59. struct pnp_quirk pnp_quirks[] = {
  60. /*
  61. * The Gravis UltraSound needs register 0xf2 to be set to 0xff
  62. * to enable power.
  63. * XXX need to know the logical device id.
  64. */
  65. { 0x0100561e /* GRV0001 */, 0,
  66. PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
  67. /*
  68. * An emu8000 does not give us other than the first
  69. * port.
  70. */
  71. { 0x26008c0e /* SB16 */, 0x21008c0e,
  72. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  73. { 0x42008c0e /* SB32(CTL0042) */, 0x21008c0e,
  74. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  75. { 0x44008c0e /* SB32(CTL0044) */, 0x21008c0e,
  76. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  77. { 0x49008c0e /* SB32(CTL0049) */, 0x21008c0e,
  78. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  79. { 0xf1008c0e /* SB32(CTL00f1) */, 0x21008c0e,
  80. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  81. { 0xc1008c0e /* SB64(CTL00c1) */, 0x22008c0e,
  82. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  83. { 0xc5008c0e /* SB64(CTL00c5) */, 0x22008c0e,
  84. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  85. { 0xe4008c0e /* SB64(CTL00e4) */, 0x22008c0e,
  86. PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
  87. { 0 }
  88. };
  89. /* The READ_DATA port that we are using currently */
  90. static int pnp_rd_port;
  91. static void pnp_send_initiation_key(void);
  92. static int pnp_get_serial(pnp_id *p);
  93. static int pnp_isolation_protocol(device_t parent);
  94. static void
  95. pnp_write(int d, u_char r)
  96. {
  97. outb (_PNP_ADDRESS, d);
  98. outb (_PNP_WRITE_DATA, r);
  99. }
  100. /*
  101. * Send Initiation LFSR as described in "Plug and Play ISA Specification",
  102. * Intel May 94.
  103. */
  104. static void
  105. pnp_send_initiation_key(void)
  106. {
  107. int cur, i;
  108. /* Reset the LSFR */
  109. outb(_PNP_ADDRESS, 0);
  110. outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
  111. cur = 0x6a;
  112. outb(_PNP_ADDRESS, cur);
  113. for (i = 1; i < 32; i++) {
  114. cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
  115. outb(_PNP_ADDRESS, cur);
  116. }
  117. }
  118. /*
  119. * Get the device's serial number. Returns 1 if the serial is valid.
  120. */
  121. static int
  122. pnp_get_serial(pnp_id *p)
  123. {
  124. int i, bit, valid = 0, sum = 0x6a;
  125. u_char *data = (u_char *)p;
  126. bzero(data, sizeof(char) * 9);
  127. outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
  128. for (i = 0; i < 72; i++) {
  129. bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
  130. DELAY(250); /* Delay 250 usec */
  131. /* Can't Short Circuit the next evaluation, so 'and' is last */
  132. bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
  133. DELAY(250); /* Delay 250 usec */
  134. valid = valid || bit;
  135. if (i < 64)
  136. sum = (sum >> 1) |
  137. (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
  138. data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
  139. }
  140. valid = valid && (data[8] == sum);
  141. return (valid);
  142. }
  143. /*
  144. * Fill's the buffer with resource info from the device.
  145. * Returns the number of characters read.
  146. */
  147. static int
  148. pnp_get_resource_info(u_char *buffer, int len)
  149. {
  150. int i, j, count;
  151. u_char temp;
  152. count = 0;
  153. for (i = 0; i < len; i++) {
  154. outb(_PNP_ADDRESS, PNP_STATUS);
  155. for (j = 0; j < 100; j++) {
  156. if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
  157. break;
  158. DELAY(10);
  159. }
  160. if (j == 100) {
  161. printf("PnP device failed to report resource data\n");
  162. return (count);
  163. }
  164. outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
  165. temp = inb((pnp_rd_port << 2) | 0x3);
  166. if (buffer != NULL)
  167. buffer[i] = temp;
  168. count++;
  169. }
  170. return (count);
  171. }
  172. /*
  173. * This function is called after the bus has assigned resource
  174. * locations for a logical device.
  175. */
  176. static void
  177. pnp_set_config(void *arg, struct isa_config *config, int enable)
  178. {
  179. int csn = ((struct pnp_set_config_arg *) arg)->csn;
  180. int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
  181. int i;
  182. /*
  183. * First put all cards into Sleep state with the initiation
  184. * key, then put our card into Config state.
  185. */
  186. pnp_send_initiation_key();
  187. pnp_write(PNP_WAKE, csn);
  188. /*
  189. * Select our logical device so that we can program it.
  190. */
  191. pnp_write(PNP_SET_LDN, ldn);
  192. /*
  193. * Constrain the number of resources we will try to program
  194. */
  195. if (config->ic_nmem > ISA_PNP_NMEM) {
  196. printf("too many ISA memory ranges (%d > %d)\n",
  197. config->ic_nmem, ISA_PNP_NMEM);
  198. config->ic_nmem = ISA_PNP_NMEM;
  199. }
  200. if (config->ic_nport > ISA_PNP_NPORT) {
  201. printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport,
  202. ISA_PNP_NPORT);
  203. config->ic_nport = ISA_PNP_NPORT;
  204. }
  205. if (config->ic_nirq > ISA_PNP_NIRQ) {
  206. printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq,
  207. ISA_PNP_NIRQ);
  208. config->ic_nirq = ISA_PNP_NIRQ;
  209. }
  210. if (config->ic_ndrq > ISA_PNP_NDRQ) {
  211. printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq,
  212. ISA_PNP_NDRQ);
  213. config->ic_ndrq = ISA_PNP_NDRQ;
  214. }
  215. /*
  216. * Now program the resources.
  217. */
  218. for (i = 0; i < config->ic_nmem; i++) {
  219. uint32_t start;
  220. uint32_t size;
  221. /* XXX: should handle memory control register, 32 bit memory */
  222. if (config->ic_mem[i].ir_size == 0) {
  223. pnp_write(PNP_MEM_BASE_HIGH(i), 0);
  224. pnp_write(PNP_MEM_BASE_LOW(i), 0);
  225. pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
  226. pnp_write(PNP_MEM_RANGE_LOW(i), 0);
  227. } else {
  228. start = config->ic_mem[i].ir_start;
  229. size = config->ic_mem[i].ir_size;
  230. if (start & 0xff)
  231. panic("pnp_set_config: bogus memory assignment");
  232. pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
  233. pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
  234. pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
  235. pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
  236. }
  237. }
  238. for (; i < ISA_PNP_NMEM; i++) {
  239. pnp_write(PNP_MEM_BASE_HIGH(i), 0);
  240. pnp_write(PNP_MEM_BASE_LOW(i), 0);
  241. pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
  242. pnp_write(PNP_MEM_RANGE_LOW(i), 0);
  243. }
  244. for (i = 0; i < config->ic_nport; i++) {
  245. uint32_t start;
  246. if (config->ic_port[i].ir_size == 0) {
  247. pnp_write(PNP_IO_BASE_HIGH(i), 0);
  248. pnp_write(PNP_IO_BASE_LOW(i), 0);
  249. } else {
  250. start = config->ic_port[i].ir_start;
  251. pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
  252. pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
  253. }
  254. }
  255. for (; i < ISA_PNP_NPORT; i++) {
  256. pnp_write(PNP_IO_BASE_HIGH(i), 0);
  257. pnp_write(PNP_IO_BASE_LOW(i), 0);
  258. }
  259. for (i = 0; i < config->ic_nirq; i++) {
  260. int irq;
  261. /* XXX: interrupt type */
  262. if (config->ic_irqmask[i] == 0) {
  263. pnp_write(PNP_IRQ_LEVEL(i), 0);
  264. pnp_write(PNP_IRQ_TYPE(i), 2);
  265. } else {
  266. irq = ffs(config->ic_irqmask[i]) - 1;
  267. pnp_write(PNP_IRQ_LEVEL(i), irq);
  268. pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
  269. }
  270. }
  271. for (; i < ISA_PNP_NIRQ; i++) {
  272. /*
  273. * IRQ 0 is not a valid interrupt selection and
  274. * represents no interrupt selection.
  275. */
  276. pnp_write(PNP_IRQ_LEVEL(i), 0);
  277. pnp_write(PNP_IRQ_TYPE(i), 2);
  278. }
  279. for (i = 0; i < config->ic_ndrq; i++) {
  280. int drq;
  281. if (config->ic_drqmask[i] == 0) {
  282. pnp_write(PNP_DMA_CHANNEL(i), 4);
  283. } else {
  284. drq = ffs(config->ic_drqmask[i]) - 1;
  285. pnp_write(PNP_DMA_CHANNEL(i), drq);
  286. }
  287. }
  288. for (; i < ISA_PNP_NDRQ; i++) {
  289. /*
  290. * DMA channel 4, the cascade channel is used to
  291. * indicate no DMA channel is active.
  292. */
  293. pnp_write(PNP_DMA_CHANNEL(i), 4);
  294. }
  295. pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
  296. /*
  297. * Wake everyone up again, we are finished.
  298. */
  299. pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
  300. }
  301. /*
  302. * Process quirks for a logical device.. The card must be in Config state.
  303. */
  304. void
  305. pnp_check_quirks(uint32_t vendor_id, uint32_t logical_id, int ldn,
  306. struct isa_config *config)
  307. {
  308. struct pnp_quirk *qp;
  309. for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
  310. if (qp->vendor_id == vendor_id
  311. && (qp->logical_id == 0 || qp->logical_id == logical_id)) {
  312. switch (qp->type) {
  313. case PNP_QUIRK_WRITE_REG:
  314. pnp_write(PNP_SET_LDN, ldn);
  315. pnp_write(qp->arg1, qp->arg2);
  316. break;
  317. case PNP_QUIRK_EXTRA_IO:
  318. if (config == NULL)
  319. break;
  320. if (qp->arg1 != 0) {
  321. config->ic_nport++;
  322. config->ic_port[config->ic_nport - 1] = config->ic_port[0];
  323. config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
  324. config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
  325. }
  326. if (qp->arg2 != 0) {
  327. config->ic_nport++;
  328. config->ic_port[config->ic_nport - 1] = config->ic_port[0];
  329. config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
  330. config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
  331. }
  332. break;
  333. }
  334. }
  335. }
  336. }
  337. /*
  338. * Scan Resource Data for Logical Devices.
  339. *
  340. * This function exits as soon as it gets an error reading *ANY*
  341. * Resource Data or it reaches the end of Resource Data. In the first
  342. * case the return value will be TRUE, FALSE otherwise.
  343. */
  344. static int
  345. pnp_create_devices(device_t parent, pnp_id *p, int csn,
  346. u_char *resources, int len)
  347. {
  348. u_char tag, *resp, *resinfo, *startres = NULL;
  349. int large_len, scanning = len, retval = FALSE;
  350. uint32_t logical_id;
  351. device_t dev = 0;
  352. int ldn = 0;
  353. struct pnp_set_config_arg *csnldn;
  354. char buf[100];
  355. char *desc = NULL;
  356. resp = resources;
  357. while (scanning > 0) {
  358. tag = *resp++;
  359. scanning--;
  360. if (PNP_RES_TYPE(tag) != 0) {
  361. /* Large resource */
  362. if (scanning < 2) {
  363. scanning = 0;
  364. continue;
  365. }
  366. large_len = resp[0] + (resp[1] << 8);
  367. resp += 2;
  368. if (scanning < large_len) {
  369. scanning = 0;
  370. continue;
  371. }
  372. resinfo = resp;
  373. resp += large_len;
  374. scanning -= large_len;
  375. if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
  376. if (dev) {
  377. /*
  378. * This is an optional device
  379. * identifier string. Skip it
  380. * for now.
  381. */
  382. continue;
  383. }
  384. /* else mandately card identifier string */
  385. if (large_len > sizeof(buf) - 1)
  386. large_len = sizeof(buf) - 1;
  387. bcopy(resinfo, buf, large_len);
  388. /*
  389. * Trim trailing spaces.
  390. */
  391. while (buf[large_len-1] == ' ')
  392. large_len--;
  393. buf[large_len] = '\0';
  394. desc = buf;
  395. continue;
  396. }
  397. continue;
  398. }
  399. /* Small resource */
  400. if (scanning < PNP_SRES_LEN(tag)) {
  401. scanning = 0;
  402. continue;
  403. }
  404. resinfo = resp;
  405. resp += PNP_SRES_LEN(tag);
  406. scanning -= PNP_SRES_LEN(tag);
  407. switch (PNP_SRES_NUM(tag)) {
  408. case PNP_TAG_LOGICAL_DEVICE:
  409. /*
  410. * Parse the resources for the previous
  411. * logical device (if any).
  412. */
  413. if (startres) {
  414. pnp_parse_resources(dev, startres,
  415. resinfo - startres - 1, ldn);
  416. dev = 0;
  417. startres = NULL;
  418. }
  419. /*
  420. * A new logical device. Scan for end of
  421. * resources.
  422. */
  423. bcopy(resinfo, &logical_id, 4);
  424. pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
  425. dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, DEVICE_UNIT_ANY);
  426. if (desc)
  427. device_set_desc_copy(dev, desc);
  428. else
  429. device_set_desc_copy(dev,
  430. pnp_eisaformat(logical_id));
  431. isa_set_vendorid(dev, p->vendor_id);
  432. isa_set_serial(dev, p->serial);
  433. isa_set_logicalid(dev, logical_id);
  434. isa_set_configattr(dev,
  435. ISACFGATTR_CANDISABLE | ISACFGATTR_DYNAMIC);
  436. csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
  437. if (!csnldn) {
  438. device_printf(parent, "out of memory\n");
  439. scanning = 0;
  440. break;
  441. }
  442. csnldn->csn = csn;
  443. csnldn->ldn = ldn;
  444. ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config,
  445. csnldn);
  446. isa_set_pnp_csn(dev, csn);
  447. isa_set_pnp_ldn(dev, ldn);
  448. ldn++;
  449. startres = resp;
  450. break;
  451. case PNP_TAG_END:
  452. if (!startres) {
  453. device_printf(parent, "malformed resources\n");
  454. scanning = 0;
  455. break;
  456. }
  457. pnp_parse_resources(dev, startres,
  458. resinfo - startres - 1, ldn);
  459. dev = 0;
  460. startres = NULL;
  461. scanning = 0;
  462. break;
  463. default:
  464. /* Skip this resource */
  465. break;
  466. }
  467. }
  468. return (retval);
  469. }
  470. /*
  471. * Read 'amount' bytes of resources from the card, allocating memory
  472. * as needed. If a buffer is already available, it should be passed in
  473. * '*resourcesp' and its length in '*spacep'. The number of resource
  474. * bytes already in the buffer should be passed in '*lenp'. The memory
  475. * allocated will be returned in '*resourcesp' with its size and the
  476. * number of bytes of resources in '*spacep' and '*lenp' respectively.
  477. *
  478. * XXX: Multiple problems here, we forget to free() stuff in one
  479. * XXX: error return, and in another case we free (*resourcesp) but
  480. * XXX: don't tell the caller.
  481. */
  482. static int
  483. pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
  484. {
  485. u_char *resources = *resourcesp;
  486. u_char *newres;
  487. int space = *spacep;
  488. int len = *lenp;
  489. if (space == 0) {
  490. space = 1024;
  491. resources = malloc(space, M_TEMP, M_NOWAIT);
  492. if (!resources)
  493. return (ENOMEM);
  494. }
  495. if (len + amount > space) {
  496. int extra = 1024;
  497. while (len + amount > space + extra)
  498. extra += 1024;
  499. newres = malloc(space + extra, M_TEMP, M_NOWAIT);
  500. if (!newres) {
  501. /* XXX: free resources */
  502. return (ENOMEM);
  503. }
  504. bcopy(resources, newres, len);
  505. free(resources, M_TEMP);
  506. resources = newres;
  507. space += extra;
  508. }
  509. if (pnp_get_resource_info(resources + len, amount) != amount)
  510. return (EINVAL);
  511. len += amount;
  512. *resourcesp = resources;
  513. *spacep = space;
  514. *lenp = len;
  515. return (0);
  516. }
  517. /*
  518. * Read all resources from the card, allocating memory as needed. If a
  519. * buffer is already available, it should be passed in '*resourcesp'
  520. * and its length in '*spacep'. The memory allocated will be returned
  521. * in '*resourcesp' with its size and the number of bytes of resources
  522. * in '*spacep' and '*lenp' respectively.
  523. */
  524. static int
  525. pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
  526. {
  527. u_char *resources = *resourcesp;
  528. int space = *spacep;
  529. int len = 0;
  530. int error, done;
  531. u_char tag;
  532. error = 0;
  533. done = 0;
  534. while (!done) {
  535. error = pnp_read_bytes(1, &resources, &space, &len);
  536. if (error)
  537. goto out;
  538. tag = resources[len-1];
  539. if (PNP_RES_TYPE(tag) == 0) {
  540. /*
  541. * Small resource, read contents.
  542. */
  543. error = pnp_read_bytes(PNP_SRES_LEN(tag),
  544. &resources, &space, &len);
  545. if (error)
  546. goto out;
  547. if (PNP_SRES_NUM(tag) == PNP_TAG_END)
  548. done = 1;
  549. } else {
  550. /*
  551. * Large resource, read length and contents.
  552. */
  553. error = pnp_read_bytes(2, &resources, &space, &len);
  554. if (error)
  555. goto out;
  556. error = pnp_read_bytes(resources[len-2]
  557. + (resources[len-1] << 8), &resources, &space,
  558. &len);
  559. if (error)
  560. goto out;
  561. }
  562. }
  563. out:
  564. *resourcesp = resources;
  565. *spacep = space;
  566. *lenp = len;
  567. return (error);
  568. }
  569. /*
  570. * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
  571. * value (caller should try multiple READ_DATA locations before giving
  572. * up). Upon exiting, all cards are aware that they should use
  573. * pnp_rd_port as the READ_DATA port.
  574. *
  575. * In the first pass, a csn is assigned to each board and pnp_id's
  576. * are saved to an array, pnp_devices. In the second pass, each
  577. * card is woken up and the device configuration is called.
  578. */
  579. static int
  580. pnp_isolation_protocol(device_t parent)
  581. {
  582. int csn;
  583. pnp_id id;
  584. int found = 0, len;
  585. u_char *resources = NULL;
  586. int space = 0;
  587. int error;
  588. /*
  589. * Put all cards into the Sleep state so that we can clear
  590. * their CSNs.
  591. */
  592. pnp_send_initiation_key();
  593. /*
  594. * Clear the CSN for all cards.
  595. */
  596. pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
  597. /*
  598. * Move all cards to the Isolation state.
  599. */
  600. pnp_write(PNP_WAKE, 0);
  601. /*
  602. * Tell them where the read point is going to be this time.
  603. */
  604. pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
  605. for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
  606. /*
  607. * Start the serial isolation protocol.
  608. */
  609. outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
  610. DELAY(1000); /* Delay 1 msec */
  611. if (pnp_get_serial(&id)) {
  612. /*
  613. * We have read the id from a card
  614. * successfully. The card which won the
  615. * isolation protocol will be in Isolation
  616. * mode and all others will be in Sleep.
  617. * Program the CSN of the isolated card
  618. * (taking it to Config state) and read its
  619. * resources, creating devices as we find
  620. * logical devices on the card.
  621. */
  622. pnp_write(PNP_SET_CSN, csn);
  623. if (bootverbose)
  624. printf("Reading PnP configuration for %s.\n",
  625. pnp_eisaformat(id.vendor_id));
  626. error = pnp_read_resources(&resources, &space, &len);
  627. if (error)
  628. break;
  629. pnp_create_devices(parent, &id, csn, resources, len);
  630. found++;
  631. } else
  632. break;
  633. /*
  634. * Put this card back to the Sleep state and
  635. * simultaneously move all cards which don't have a
  636. * CSN yet to Isolation state.
  637. */
  638. pnp_write(PNP_WAKE, 0);
  639. }
  640. /*
  641. * Unless we have chosen the wrong read port, all cards will
  642. * be in Sleep state. Put them back into WaitForKey for
  643. * now. Their resources will be programmed later.
  644. */
  645. pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
  646. /*
  647. * Cleanup.
  648. */
  649. if (resources)
  650. free(resources, M_TEMP);
  651. return (found);
  652. }
  653. /*
  654. * pnp_identify()
  655. *
  656. * autoconfiguration of pnp devices. This routine just runs the
  657. * isolation protocol over several ports, until one is successful.
  658. *
  659. * may be called more than once ?
  660. *
  661. */
  662. static void
  663. pnp_identify(driver_t *driver, device_t parent)
  664. {
  665. int num_pnp_devs;
  666. /* Try various READ_DATA ports from 0x203-0x3ff */
  667. for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
  668. if (bootverbose)
  669. printf("pnp_identify: Trying Read_Port at %x\n",
  670. (pnp_rd_port << 2) | 0x3);
  671. num_pnp_devs = pnp_isolation_protocol(parent);
  672. if (num_pnp_devs)
  673. break;
  674. }
  675. if (bootverbose)
  676. printf("PNP Identify complete\n");
  677. }
  678. static device_method_t pnp_methods[] = {
  679. /* Device interface */
  680. DEVMETHOD(device_identify, pnp_identify),
  681. { 0, 0 }
  682. };
  683. static driver_t pnp_driver = {
  684. "pnp",
  685. pnp_methods,
  686. 1, /* no softc */
  687. };
  688. DRIVER_MODULE(pnp, isa, pnp_driver, 0, 0);